1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "commandparser.h"
24 #include <QXmlStreamReader>
25 #include <QXmlStreamWriter>
27 #include <yamf/model/command/base.h>
28 #include "datasource.h"
30 #include <dcore/debug.h>
34 struct CommandParser::Private
36 Private() : currentSource(0) {}
38 DataSource
*currentSource
;
42 QStringList objectCommands
;
45 CommandParser::CommandParser() : DCore::XmlParserBase(), d(new Private
)
47 d
->objectCommands
<< "addobject" << "removeobject" << "modifyitem" << "editnodesitem" << "convertitem" << "additemfilter" << "addtweening" << "bringforwardsitem" << "bringtofrontitem" << "sendtobackitem" << "sendbackwardsitem" << "groupitem" << "ungroupitem";
51 CommandParser::~CommandParser()
56 bool CommandParser::startTag(const QString
&tag
, const QXmlAttributes
&atts
)
58 if( atts
.count() > 0 )
60 DataSource::Values values
;
62 for(int i
= 0; i
< atts
.count(); i
++)
64 QString qname
= atts
.qName(i
);
65 values
[qname
] = atts
.value(qname
);
68 d
->currentSource
->setValues(tag
, values
);
71 if( tag
== "command" )
73 d
->commandName
= atts
.value("name");
75 if( d
->objectCommands
.contains(d
->commandName
) )
77 d
->currentSource
->setType(DataSource::Object
);
79 else if( d
->commandName
== "insertscene" || d
->commandName
== "removescene" || d
->commandName
== "movescene" || d
->commandName
== "renamescene" )
81 d
->currentSource
->setType(DataSource::Scene
);
83 else if( d
->commandName
== "insertlayer" || d
->commandName
== "removelayer" || d
->commandName
== "locklayer" || d
->commandName
== "changelayervisibility"|| d
->commandName
== "movelayer" || d
->commandName
== "renamelayer")
85 d
->currentSource
->setType(DataSource::Layer
);
87 else if( d
->commandName
== "insertframe" || d
->commandName
== "removeframe" || d
->commandName
== "lockframe" || d
->commandName
== "changeframevisibility" || d
->commandName
== "moveframe" || d
->commandName
== "renameframe" || d
->commandName
== "expandframe" )
89 d
->currentSource
->setType(DataSource::Frame
);
91 else if( d
->commandName
== "addlibraryobject" || d
->commandName
== "removelibraryobject"|| d
->commandName
== "modifysymbol" )
93 d
->currentSource
->setType(DataSource::Library
);
100 bool CommandParser::endTag(const QString
&tag
)
105 void CommandParser::text(const QString
&text
)
110 DataSource
*CommandParser::build(const YAMF::Command::Base
*command
)
112 d
->currentSource
= new DataSource
;
113 d
->currentSource
->setData("command", qVariantFromValue(command
) );
116 QString xml
= command
->toXml();
118 if( ! DCore::XmlParserBase::parse(xml
) )
120 dError() << "Failed to parse";
124 d
->currentSource
->setData("root", d
->commandName
);
126 if( d
->commandName
== "addlibraryobject" )
128 // Rewrite the xml to avoid the file tag
130 QXmlStreamReader
reader(xml
);
133 QXmlStreamWriter
writer(&xml
);
136 while(!reader
.atEnd())
139 if( reader
.tokenType() == QXmlStreamReader::StartElement
)
141 if( reader
.name().toString() == "file" )
146 else if( reader
.tokenType() == QXmlStreamReader::EndElement
)
148 if( reader
.name().toString() == "file" )
155 writer
.writeCurrentToken(reader
);
159 d
->currentSource
->setData("xml", xml
);
162 DataSource
*source
= d
->currentSource
;
163 d
->currentSource
= 0;