Implemented unit tests for:
[yamf.git] / src / model / frame.cpp
blob962e9f94ab425da43a68451cba6ab44ef0341c92
1 /***************************************************************************
2 * Copyright (C) 2005 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 ***************************************************************************/
21 #include "frame.h"
22 #include "layer.h"
24 #include <dcore/debug.h>
26 #include <QGraphicsItem>
28 #include "object.h"
29 #include "scene.h"
30 #include "item/builder.h"
31 #include "model/command/manager.h"
32 #include "model/command/addobject.h"
33 #include "model/command/removeobject.h"
35 #include "model/library.h"
36 #include "item/group.h"
37 #include "item/tweener.h"
39 namespace YAMF {
40 namespace Model {
42 struct Frame::Private
44 Private() : objectCount(0)
48 QString name;
49 bool isLocked;
50 bool isVisible;
51 GraphicObjects graphics;
52 int repeat;
54 int objectCount;
57 Frame::Frame(Layer *parent) : QObject(parent), d(new Private)
59 d->name = tr("Frame");
60 d->isLocked = false;
61 d->isVisible = true;
62 d->repeat = 1;
65 Frame::~Frame()
67 d->graphics.clear(true);
68 delete d;
72 void Frame::clean()
74 d->graphics.clear(true);
77 void Frame::setFrameName(const QString &name)
79 d->name = name;
82 void Frame::setLocked(bool isLocked)
84 d->isLocked = isLocked;
87 QString Frame::frameName() const
89 return d->name;
92 bool Frame::isLocked() const
94 return d->isLocked;
97 void Frame::setVisible(bool isVisible)
99 d->isVisible = isVisible;
102 bool Frame::isVisible() const
104 return d->isVisible;
107 void Frame::addObject(Object *object)
109 Command::Manager *commandManager = project()->commandManager();
111 object->setParent(this);
113 if( QGraphicsItem *item = object->item() )
115 double zMax = 0;
116 foreach(Object *graphic, d->graphics.values() )
118 if( zMax < graphic->item()->zValue() )
120 zMax = graphic->item()->zValue();
124 item->setZValue(zMax+1);
127 if( object->objectName().isEmpty() )
129 object->setObjectName(tr("Object %1").arg(d->objectCount++));
132 Command::AddObject *command = new Command::AddObject(object, &d->graphics);
133 commandManager->addCommand(command);
137 void Frame::removeObject(Object *object)
139 Command::RemoveObject *cmd = new Command::RemoveObject(object, &d->graphics);
140 project()->commandManager()->addCommand(cmd);
142 this->scene()->removeTweeningObject(object);
145 void Frame::removeObjects(const QList<Object *> &objects)
147 Command::RemoveObject *cmd = new Command::RemoveObject(objects, &d->graphics);
148 project()->commandManager()->addCommand(cmd);
150 foreach(Object *object, objects)
151 this->scene()->removeTweeningObject(object);
154 void Frame::removeItem(QGraphicsItem *item)
156 int index = logicalIndexOf(item);
157 if( index >= 0 )
159 removeObject(graphic(index));
163 void Frame::removeItems(const QList<QGraphicsItem *> &items)
165 QList<Object *> objects;
166 foreach(QGraphicsItem *item, items)
168 int index = logicalIndexOf(item);
169 if( index >= 0 )
171 objects << graphic(index);
175 removeObjects(objects);
178 void Frame::fromXml(const QString &xml )
180 QDomDocument document;
182 if (! document.setContent(xml) )
184 return;
187 QDomElement root = document.documentElement();
189 setFrameName( root.attribute( "name", frameName() ) );
191 QDomNode n = root.firstChild();
193 while( !n.isNull() )
195 QDomElement e = n.toElement();
197 if(!e.isNull())
199 if( e.tagName() == "object" )
201 QString objectXml;
202 QTextStream ts(&objectXml);
203 ts << e;
205 Object *last = new Object(this);
206 last->fromXml(objectXml);
210 n = n.nextSibling();
214 QDomElement Frame::toXml(QDomDocument &doc) const
216 QDomElement root = doc.createElement("frame");
217 root.setAttribute("name", d->name );
218 doc.appendChild(root);
220 foreach(Object *object, d->graphics.visualValues() )
222 root.appendChild( object->toXml(doc) );
225 return root;
228 Object *Frame::addItem(QGraphicsItem *item)
230 Object *object = new Object(item, this);
231 object->setObjectName(tr("Object %1").arg(d->objectCount++));
233 addObject(object);
235 return object;
241 void Frame::replaceItem(int logicalIndex, QGraphicsItem *item)
243 Object *toReplace = this->graphic(logicalIndex);
245 if ( toReplace )
247 toReplace->setItem( item );
251 bool Frame::moveItem(int currentPosition, int newPosition)
253 D_FUNCINFO << "current "<< currentPosition << " new " << newPosition;
254 if(currentPosition == newPosition || currentPosition < 0 || currentPosition >= d->graphics.count() || newPosition < 0 || newPosition >= d->graphics.count())
256 return false;
259 if(currentPosition < newPosition)
261 for( int i = currentPosition; i < newPosition; i++)
263 double tmp = d->graphics.visualValue(i)->item()->zValue();
264 d->graphics.visualValue(i)->item()->setZValue(d->graphics.visualValue(i+1)->item()->zValue());
265 d->graphics.visualValue(i+1)->item()->setZValue(tmp);
266 d->graphics.moveVisual(i, i+1);
269 else
271 for( int i = currentPosition; i > newPosition; i--)
273 double tmp = d->graphics.visualValue(i)->item()->zValue();
274 d->graphics.visualValue(i)->item()->setZValue(d->graphics.visualValue(i-1)->item()->zValue());
275 d->graphics.visualValue(i-1)->item()->setZValue(tmp);
276 d->graphics.moveVisual(i, i-1);
279 return true;
282 void Frame::removeObjectAt(int logicalIndex)
284 if ( !d->graphics.contains(logicalIndex) )
286 return;
289 Object *object = graphic(logicalIndex);
290 removeObject(object);
293 GraphicObjects Frame::graphics() const
295 return d->graphics;
298 Object *Frame::graphic(int logicalIndex) const
300 if ( !d->graphics.contains(logicalIndex) )
302 D_FUNCINFO << " FATAL ERROR: index out of bound " << logicalIndex;
303 return 0;
306 return d->graphics.value(logicalIndex);
309 QGraphicsItem *Frame::item(int logicalIndex) const
311 Object *object = graphic(logicalIndex);
313 if ( object )
315 return object->item();
318 return 0;
321 int Frame::visualIndexOf(Object *object) const
323 return d->graphics.visualIndex(object);
326 int Frame::logicalIndexOf(Object *object) const
328 return d->graphics.logicalIndex(object);
331 int Frame::visualIndexOf(QGraphicsItem *item) const
333 foreach(Object *object, d->graphics.values() )
335 if (object->item() == item )
337 return d->graphics.visualIndex(object);
340 return -1;
343 int Frame::logicalIndexOf(QGraphicsItem *item) const
345 foreach(Object *object, d->graphics.values())
347 if (object->item() == item )
349 return d->graphics.logicalIndex(object);
353 return -1;
357 int Frame::logicalIndex() const
359 return layer()->logicalIndexOf(const_cast<Frame *>(this));
363 void Frame::setRepeat(int repeat)
365 d->repeat = repeat;
368 int Frame::repeat() const
370 return d->repeat;
373 int Frame::visualIndex() const
375 return layer()->visualIndexOf(const_cast<Frame *>(this));
378 Layer *Frame::layer() const
380 return static_cast<Layer *>(parent());
383 Scene *Frame::scene() const
385 return layer()->scene();
388 Project *Frame::project() const
390 return layer()->project();