1 /***************************************************************************
2 * Copyright (C) 2005 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 ***************************************************************************/
24 #include <dcore/debug.h>
26 #include <QGraphicsItem>
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"
44 Private() : objectCount(0)
51 GraphicObjects graphics
;
57 Frame::Frame(Layer
*parent
) : QObject(parent
), d(new Private
)
59 d
->name
= tr("Frame");
67 d
->graphics
.clear(true);
74 d
->graphics
.clear(true);
77 void Frame::setFrameName(const QString
&name
)
82 void Frame::setLocked(bool isLocked
)
84 d
->isLocked
= isLocked
;
87 QString
Frame::frameName() const
92 bool Frame::isLocked() const
97 void Frame::setVisible(bool isVisible
)
99 d
->isVisible
= isVisible
;
102 bool Frame::isVisible() const
107 void Frame::addObject(Object
*object
)
109 Command::Manager
*commandManager
= project()->commandManager();
111 object
->setParent(this);
113 if( QGraphicsItem
*item
= object
->item() )
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
);
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
);
171 objects
<< graphic(index
);
175 removeObjects(objects
);
178 void Frame::fromXml(const QString
&xml
)
180 QDomDocument document
;
182 if (! document
.setContent(xml
) )
187 QDomElement root
= document
.documentElement();
189 setFrameName( root
.attribute( "name", frameName() ) );
191 QDomNode n
= root
.firstChild();
195 QDomElement e
= n
.toElement();
199 if( e
.tagName() == "object" )
202 QTextStream
ts(&objectXml
);
205 Object
*last
= new Object(this);
206 last
->fromXml(objectXml
);
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
) );
228 Object
*Frame::addItem(QGraphicsItem
*item
)
230 Object
*object
= new Object(item
, this);
231 object
->setObjectName(tr("Object %1").arg(d
->objectCount
++));
241 void Frame::replaceItem(int logicalIndex
, QGraphicsItem
*item
)
243 Object
*toReplace
= this->graphic(logicalIndex
);
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())
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);
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);
282 void Frame::removeObjectAt(int logicalIndex
)
284 if ( !d
->graphics
.contains(logicalIndex
) )
289 Object
*object
= graphic(logicalIndex
);
290 removeObject(object
);
293 GraphicObjects
Frame::graphics() const
298 Object
*Frame::graphic(int logicalIndex
) const
300 if ( !d
->graphics
.contains(logicalIndex
) )
302 D_FUNCINFO
<< " FATAL ERROR: index out of bound " << logicalIndex
;
306 return d
->graphics
.value(logicalIndex
);
309 QGraphicsItem
*Frame::item(int logicalIndex
) const
311 Object
*object
= graphic(logicalIndex
);
315 return object
->item();
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
);
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
);
357 int Frame::logicalIndex() const
359 return layer()->logicalIndexOf(const_cast<Frame
*>(this));
363 void Frame::setRepeat(int repeat
)
368 int Frame::repeat() const
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();