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" << "modifyfilter" << "addtweening" << "bringforwardsitem" << "bringtofrontitem" << "sendtobackitem" << "sendbackwardsitem" << "groupitem" << "ungroupitem" << "changetext" << "changetextwidth" << "changetextfont" << "changeitembrush" << "changeitempen";
50 CommandParser::~CommandParser()
55 bool CommandParser::startTag(const QString
&tag
, const QXmlAttributes
&atts
)
57 if( atts
.count() > 0 )
59 DataSource::Values values
;
61 for(int i
= 0; i
< atts
.count(); i
++)
63 QString qname
= atts
.qName(i
);
64 values
[qname
] = atts
.value(qname
);
67 d
->currentSource
->setValues(tag
, values
);
70 if( tag
== "command" )
72 d
->commandName
= atts
.value("name");
74 if( d
->objectCommands
.contains(d
->commandName
) )
76 d
->currentSource
->setType(DataSource::Object
);
78 else if( d
->commandName
== "insertscene" || d
->commandName
== "removescene" || d
->commandName
== "movescene" || d
->commandName
== "renamescene" )
80 d
->currentSource
->setType(DataSource::Scene
);
82 else if( d
->commandName
== "insertlayer" || d
->commandName
== "removelayer" || d
->commandName
== "locklayer" || d
->commandName
== "changelayervisibility"|| d
->commandName
== "movelayer" || d
->commandName
== "renamelayer")
84 d
->currentSource
->setType(DataSource::Layer
);
86 else if( d
->commandName
== "insertframe" || d
->commandName
== "removeframe" || d
->commandName
== "lockframe" || d
->commandName
== "changeframevisibility" || d
->commandName
== "moveframe" || d
->commandName
== "renameframe" || d
->commandName
== "expandframe" )
88 d
->currentSource
->setType(DataSource::Frame
);
90 else if( d
->commandName
== "addlibraryobject" || d
->commandName
== "removelibraryobject"|| d
->commandName
== "renamelibraryobject"|| d
->commandName
== "modifysymbol" )
92 d
->currentSource
->setType(DataSource::Library
);
99 bool CommandParser::endTag(const QString
&tag
)
104 void CommandParser::text(const QString
&text
)
109 DataSource
*CommandParser::build(const YAMF::Command::Base
*command
)
111 d
->currentSource
= new DataSource
;
112 d
->currentSource
->setData("command", qVariantFromValue(command
) );
115 QString xml
= command
->toXml();
117 if( ! DCore::XmlParserBase::parse(xml
) )
119 dError() << "Failed to parse";
123 d
->currentSource
->setData("root", d
->commandName
);
125 if( d
->commandName
== "addlibraryobject" )
127 // Rewrite the xml to avoid the file tag
129 QXmlStreamReader
reader(xml
);
132 QXmlStreamWriter
writer(&xml
);
135 while(!reader
.atEnd())
138 if( reader
.tokenType() == QXmlStreamReader::StartElement
)
140 if( reader
.name().toString() == "file" )
145 else if( reader
.tokenType() == QXmlStreamReader::EndElement
)
147 if( reader
.name().toString() == "file" )
154 writer
.writeCurrentToken(reader
);
158 d
->currentSource
->setData("xml", xml
);
161 DataSource
*source
= d
->currentSource
;
162 d
->currentSource
= 0;