Implemented copy, cut and paste frame
[dashstudio.git] / src / dash / commandparser.cpp
blobeabab96f6d21588ccb03a3d409bef412f41cd3b9
1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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>
32 namespace Dash {
34 struct CommandParser::Private
36 Private() : currentSource(0) {}
38 DataSource *currentSource;
39 QString commandName;
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()
52 delete d;
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);
96 return true;
99 bool CommandParser::endTag(const QString &tag)
101 return true;
104 void CommandParser::text(const QString &text)
106 Q_UNUSED(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";
120 return 0;
123 d->currentSource->setData("root", d->commandName);
125 if( d->commandName == "addlibraryobject" )
127 // Rewrite the xml to avoid the file tag
128 QString newXml;
129 QXmlStreamReader reader(xml);
131 xml = QString();
132 QXmlStreamWriter writer(&xml);
134 bool read = true;
135 while(!reader.atEnd())
137 reader.readNext();
138 if( reader.tokenType() == QXmlStreamReader::StartElement )
140 if( reader.name().toString() == "file" )
142 read = false;
145 else if( reader.tokenType() == QXmlStreamReader::EndElement )
147 if( reader.name().toString() == "file" )
149 read = true;
153 if( read )
154 writer.writeCurrentToken(reader);
158 d->currentSource->setData("xml", xml);
161 DataSource *source = d->currentSource;
162 d->currentSource = 0;
164 return source;