Implemented copy, cut and paste frame
[dashstudio.git] / src / components / timeline / timeline.cpp
blob1b8272e291c897e17dd13711c34747bdc5fb70c2
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 <QList>
22 #include <QHeaderView>
23 #include <QVBoxLayout>
24 #include <QButtonGroup>
26 #include "timeline.h"
27 #include <dgui/application.h>
28 #include <dcore/debug.h>
29 #include <dgui/iconloader.h>
31 #include <yamf/model/scene.h>
32 #include <yamf/model/layer.h>
33 #include <yamf/model/frame.h>
34 #include <yamf/model/library.h>
35 #include <yamf/model/libraryobject.h>
37 // Dash
38 #include "dash/datasource.h"
39 #include "dash/project.h"
41 #include <QDebug>
42 #include <QXmlStreamReader>
45 namespace Dash {
46 namespace Component {
48 struct TimeLine::Private
50 Private(TimeLine *q) : q(q), tlWidget(0) {}
53 TimeLine *q;
54 TimeLineWidget *tlWidget;
56 void updateScene( const Dash::DataSource * datasource, bool undo )
58 int visualIndex = datasource->values("location")["scene"].toInt();
60 QString root = datasource->values("command")["name"];
61 if( root == "insertscene")
63 if( undo )
65 tlWidget->removeScene(visualIndex);
67 else
69 tlWidget->insertScene(visualIndex, datasource->values("scene")["name"]);
70 tlWidget->setScene(visualIndex);
73 else if(root == "removescene")
75 if( undo )
78 YAMF::Model::Scene *scene = q->project()->scene(visualIndex);
80 tlWidget->insertScene(visualIndex, scene->sceneName());
82 foreach(YAMF::Model::Layer *layer , scene->layers().values())
84 int layerIndex = layer->visualIndex();
85 tlWidget->insertLayer(visualIndex, layerIndex, layer->layerName());
87 tlWidget->setLayerVisibility(visualIndex, layerIndex, layer->isVisible());
88 tlWidget->setLayerLocked(visualIndex, layerIndex, layer->isLocked());
90 foreach(YAMF::Model::Frame *frame, layer->frames().values())
92 tlWidget->insertFrame(visualIndex, layerIndex, frame->visualIndex(), frame->frameName());
94 tlWidget->setFrameVisibility(visualIndex, layerIndex, frame->visualIndex(), frame->isVisible());
95 tlWidget->setFrameLocked(visualIndex, layerIndex, frame->visualIndex(),frame->isLocked());
98 tlWidget->setScene(visualIndex);
100 else
102 tlWidget->removeScene(visualIndex);
105 else if(root == "movescene")
107 int to = datasource->values("information")["to"].toInt();
108 int from = datasource->values("information")["from"].toInt();
110 if( undo )
112 tlWidget->moveScene(to, from);
113 tlWidget->setScene(from);
115 else
117 tlWidget->moveScene(from, to);
118 tlWidget->setScene(to);
121 else if(root == "renamescene")
123 tlWidget->blockSignals(true);
124 if(undo)
126 tlWidget->renameScene(datasource->values("location")["scene"].toInt(), datasource->values("information")["oldname"]);
128 else
130 tlWidget->renameScene(datasource->values("location")["scene"].toInt(), datasource->values("information")["newname"]);
132 tlWidget->blockSignals(false);
136 void updateLayer( const Dash::DataSource * datasource, bool undo )
138 int index = datasource->values("location")["layer"].toInt();
139 QString root = datasource->values("command")["name"];
141 if(root == "insertlayer")
143 int scene = datasource->values("location")["scene"].toInt();
144 int type = datasource->values("layer")["type"].toInt();
146 if( undo )
148 tlWidget->removeLayer(scene, index);
150 else
152 switch(type)
154 case YAMF::Model::Layer::Vectorial:
156 tlWidget->insertLayer(scene, index, datasource->values("layer")["name"]);
157 tlWidget->setLayer(scene, index);
159 break;
160 case YAMF::Model::Layer::Audio:
162 tlWidget->insertAudioLayer(scene, index, datasource->values("layer")["name"], 0);
163 tlWidget->setLayer(scene, index);
168 else if(root == "removelayer")
170 int sceneIndex = datasource->values("location")["scene"].toInt();
171 // int type = datasource->values("layer")["type"].toInt();
173 if( undo )
175 YAMF::Model::Scene *scene = q->project()->scene(sceneIndex);
176 YAMF::Model::Layer *layer = scene->layer( index );
178 tlWidget->insertLayer(sceneIndex, index, layer->layerName());
180 foreach(YAMF::Model::Frame *frame, layer->frames().values())
182 tlWidget->insertFrame(sceneIndex, index, frame->visualIndex(), frame->frameName());
184 tlWidget->setFrameVisibility(sceneIndex, index, frame->visualIndex(), frame->isVisible());
185 tlWidget->setFrameLocked(sceneIndex, index, frame->visualIndex(),frame->isLocked());
188 tlWidget->setLayer(sceneIndex, index);
190 else
192 tlWidget->removeLayer(sceneIndex, index);
195 else if( root == "locklayer")
197 int scene = datasource->values("location")["scene"].toInt();
198 bool locked(datasource->values("information")["locked"].toInt());
200 if( undo )
202 tlWidget->setLayerLocked(scene, index, !locked);
204 else
206 tlWidget->setLayerLocked(scene, index, locked);
209 else if( root == "changelayervisibility")
211 int scene = datasource->values("location")["scene"].toInt();
213 bool visible(datasource->values("information")["visible"].toInt());
215 if( undo )
217 tlWidget->setLayerVisibility(scene, index, !visible);
219 else
221 tlWidget->setLayerVisibility(scene, index, visible);
224 else if(root == "movelayer")
226 int scene = datasource->values("location")["scene"].toInt();
228 int from = datasource->values("information")["from"].toInt();
229 int to = datasource->values("information")["to"].toInt();
231 if( undo )
233 tlWidget->moveLayer(scene, to, from);
235 tlWidget->setLayer(scene, to);
237 else
239 tlWidget->moveLayer(scene, from, to);
241 tlWidget->setLayer(scene, to);
245 else if(root == "renamelayer")
247 int scene = datasource->values("location")["scene"].toInt();
248 int index = datasource->values("location")["layer"].toInt();
250 tlWidget->blockSignals(true);
251 if(undo)
253 tlWidget->renameLayer(scene, index, datasource->values("information")["oldname"]);
255 else
257 tlWidget->renameLayer(scene, index, datasource->values("information")["newname"]);
259 tlWidget->blockSignals(false);
263 void updateFrame( const Dash::DataSource * datasource, bool undo )
265 int visualIndex = datasource->values("location")["frame"].toInt();
267 QString root = datasource->values("command")["name"];
268 if(root == "insertframe")
270 int scene = datasource->values("location")["scene"].toInt();
271 int layer = datasource->values("location")["layer"].toInt();
273 if( undo )
275 tlWidget->removeFrame(scene, layer, visualIndex);
277 else
279 tlWidget->insertFrame(scene, layer, visualIndex, datasource->values("frame")["name"]);
280 tlWidget->setFrame(scene, layer, visualIndex);
283 else if(root == "removeframe")
285 int sceneIndex = datasource->values("location")["scene"].toInt();
286 int layerIndex = datasource->values("location")["layer"].toInt();
288 if( undo )
290 tlWidget->insertFrame(sceneIndex, layerIndex, visualIndex, datasource->values("frame")["name"]);
292 else
294 tlWidget->removeFrame(sceneIndex, layerIndex, visualIndex);
297 else if( root == "lockframe")
299 int scene = datasource->values("location")["scene"].toInt();
300 int layer = datasource->values("location")["layer"].toInt();
302 bool locked(datasource->values("information")["locked"].toInt());
304 if( undo )
306 tlWidget->setFrameLocked(scene, layer, visualIndex, !locked);
308 else
310 tlWidget->setFrameLocked(scene, layer, visualIndex, locked);
313 else if( root == "changeframevisibility")
315 int scene = datasource->values("location")["scene"].toInt();
316 int layer = datasource->values("location")["layer"].toInt();
318 bool visible(datasource->values("information")["visible"].toInt());
320 if( undo )
322 tlWidget->setFrameVisibility(scene, layer, visualIndex, !visible);
324 else
326 tlWidget->setFrameVisibility(scene, layer, visualIndex, visible);
329 else if(root == "moveframe")
331 int scene = datasource->values("location")["scene"].toInt();
332 int layer = datasource->values("location")["layer"].toInt();
334 int from = datasource->values("information")["from"].toInt();
335 int to = datasource->values("information")["to"].toInt();
337 dfDebug << "move from " << from << " to " << to;
339 if( undo )
341 tlWidget->moveFrame(scene, layer, to, from);
342 tlWidget->setFrame(scene, layer, from);
344 else
346 tlWidget->moveFrame(scene, layer, from, to);
347 tlWidget->setFrame(scene, layer, to);
351 else if(root == "renameframe")
353 tlWidget->blockSignals(true);
354 int scene = datasource->values("location")["scene"].toInt();
355 int layer = datasource->values("location")["layer"].toInt();
357 if(undo)
359 tlWidget->renameFrame(scene, layer, datasource->values("location")["frame"].toInt(), datasource->values("information")["oldname"]);
361 else
363 tlWidget->renameFrame(scene, layer, datasource->values("location")["frame"].toInt(), datasource->values("information")["newname"]);
365 tlWidget->blockSignals(false);
367 else if(root == "expandframe")
369 int scene = datasource->values("location")["scene"].toInt();
370 int layer = datasource->values("location")["layer"].toInt();
371 int size = datasource->values("information")["size"].toInt();
373 if( undo )
375 for(int i = 0; i < size; i++)
377 tlWidget->removeFrame(scene, layer, visualIndex+1);
379 tlWidget->destroyGroupFrames(scene, layer, visualIndex, size);
381 else
383 for(int i = 1; i < size+1; i++)
385 tlWidget->insertFrame(scene, layer, visualIndex+i, datasource->values("information")["name"]);
386 tlWidget->setFrame(scene, layer, visualIndex+i);
388 tlWidget->groupFrames(scene, layer, visualIndex, visualIndex+size);
395 * @~spanish
396 * Constructor
398 TimeLine::TimeLine(Dash::Project *const project, QWidget *parent) : Widget(project, parent), d(new Private(this))
400 QVBoxLayout *layout = new QVBoxLayout(this);
401 setWindowTitle(tr("&Time Line"));
402 setWindowIcon( DGui::IconLoader::self()->load("time_line.png"));
403 d->tlWidget = new TimeLineWidget(this);
404 layout->addWidget(d->tlWidget);
406 connect(d->tlWidget, SIGNAL(postEvent(const TimeLineWidget::Event &)), this, SLOT(onEvent(const TimeLineWidget::Event &)));
407 connect(d->tlWidget, SIGNAL(frameSelected(int , int , int)), this, SLOT(onFrameSelected(int , int , int)));
411 * @~spanish
412 * Destructor
414 TimeLine::~TimeLine()
416 delete d;
420 * @~spanish
421 * Retorna los espacios de trabajo donde sera visible el componente, este componente sera visible en todos los espacios de trabajo.
423 Dash::Workspace TimeLine::workspace() const
425 return Dash::All;
429 * @~spanish
430 * Retorna la posición inicial del componente, la cual es en la parte abajo de la ventana principal.
432 Qt::DockWidgetArea TimeLine::area() const
434 return Qt::BottomDockWidgetArea;
439 * @~spanish
440 * Retorna el tipo de comandos de los interesa que el componente sea notificado de su ejecución, para este componente se notificara de la ejecución todos los comandos.
442 Dash::DataSource::Types TimeLine::sources() const
444 Dash::DataSource::Type type = Dash::DataSource::All;
445 return type;
449 * @~spanish
450 * Actualiza el componente despues de la ejecución de un comando.
452 void TimeLine::update(const Dash::DataSource *datasource, bool undo)
454 // dfDebug << undo;
455 // dfDebug << datasource->data("root").toString();
456 // dfDebug << datasource->values("location")["index"];
457 // dfDebug << "xml" << datasource->data("xml").toString();
459 switch (datasource->type())
461 case DataSource::Scene:
463 d->updateScene(datasource, undo);
465 break;
466 case DataSource::Layer:
468 d->updateLayer(datasource, undo);
470 break;
471 case DataSource::Frame:
473 d->updateFrame(datasource, undo);
475 break;
476 case DataSource::Library:
477 case DataSource::Project:
478 case DataSource::Object:
479 case DataSource::Unknown:
481 dfDebug << "Unhandled package: " << datasource->values("command")["name"];
483 break;
484 default:
492 * @~spanish
493 * Reinicia el componente, borra todas las escenas, frames y layers.
495 void TimeLine::reset()
497 d->tlWidget->closeAllScenes();
501 * @internal
503 void TimeLine::onFrameSelected(int scenePos, int layerPos, int framePos)
505 emit frameSelected(scenePos, layerPos, framePos);
509 * @internal
511 void TimeLine::onEvent(const TimeLineWidget::Event &event )
513 dfDebug << "scene = " << event.scene << " layer = " << event.layer << " frame = " << event.frame << " action " << event.action << " data " << event.data.toString();
515 switch(event.action)
517 case TimeLineWidget::InsertFrame:
519 YAMF::Model::Scene *scene = project()->scene(event.scene);
520 if(scene)
522 YAMF::Model::Layer *layer = scene->layer(event.layer);
523 if(layer)
525 layer->createFrame(event.frame);
529 break;
530 case TimeLineWidget::RemoveFrame:
532 if(event.scene > -1 && event.layer > -1 && event.frame > -1)
534 YAMF::Model::Scene *scene = project()->scene(event.scene);
535 if( scene )
537 YAMF::Model::Layer *layer = scene->layer(event.layer);
538 if( layer )
540 YAMF::Model::Frame *frame = layer->frame(event.frame);
541 if( frame )
543 layer->removeFrame(frame);
549 case TimeLineWidget::CopyFrame:
551 project()->copyFrameToClipboard(event.scene, event.layer, event.frame);
553 break;
554 case TimeLineWidget::PasteFrame:
556 project()->pasteFrameFromClipboard(event.scene, event.layer, event.frame);
558 break;
559 case TimeLineWidget::CutFrame:
561 project()->cutFrame(event.scene, event.layer, event.frame);
563 break;
564 case TimeLineWidget::MoveFrameUp:
566 YAMF::Model::Scene *scene = project()->scene(event.scene);
567 YAMF::Model::Layer *layer = scene->layer(event.layer);
569 YAMF::Model::Frame *frame = layer->frame(event.frame);
570 if(frame)
572 // int nclones = layer->clones(frame);
573 layer->moveFrame(frame->visualIndex(), frame->visualIndex()-1);
576 break;
577 case TimeLineWidget::MoveFrameDown:
579 YAMF::Model::Scene *scene = project()->scene(event.scene);
580 YAMF::Model::Layer *layer = scene->layer(event.layer);
581 YAMF::Model::Frame *frame = layer->frame(event.frame);
583 if(frame)
585 int nclones = layer->clones(frame);
586 layer->moveFrame(frame->visualIndex(), frame->visualIndex()+nclones+1);
589 break;
590 case TimeLineWidget::LockFrame:
592 YAMF::Model::Scene *scene = project()->scene(event.scene);
593 YAMF::Model::Layer *layer = scene->layer(event.layer);
594 YAMF::Model::Frame *frame = layer->frame(event.frame);
595 frame->setLocked(event.data.toBool());
597 break;
598 case TimeLineWidget::RenameFrame:
601 break;
602 case TimeLineWidget::ChangeFrameVisibility:
604 YAMF::Model::Scene *scene = project()->scene(event.scene);
605 YAMF::Model::Layer *layer = scene->layer(event.layer);
606 YAMF::Model::Frame *frame = layer->frame(event.frame);
607 frame->setVisible(event.data.toBool());
609 break;
610 case TimeLineWidget::ExpandFrame:
612 YAMF::Model::Scene *scene = project()->scene(event.scene);
613 YAMF::Model::Layer *layer = scene->layer(event.layer);
614 layer->expandFrame(event.frame, event.data.toInt());
616 break;
617 case TimeLineWidget::InsertLayer:
619 YAMF::Model::Scene *scene = project()->scene(event.scene);
620 if(scene)
622 YAMF::Model::Layer *layer = scene->createLayer( event.layer+1 );
623 if(layer)
625 layer->createFrame();
629 break;
630 case TimeLineWidget::RemoveLayer:
632 if(event.scene > -1 && event.layer > -1)
634 YAMF::Model::Scene *scene = project()->scene(event.scene);
635 YAMF::Model::Layer *layer = scene->layer(event.layer);
636 scene->removeLayer(layer);
639 break;
640 case TimeLineWidget::MoveLayerUp:
642 YAMF::Model::Scene *scene = project()->scene(event.scene);
643 scene->moveLayer(event.layer, event.layer-1);
645 break;
646 case TimeLineWidget::MoveLayerDown:
648 YAMF::Model::Scene *scene = project()->scene(event.scene);
649 scene->moveLayer(event.layer, event.layer+1);
651 break;
652 case TimeLineWidget::LockLayer:
654 YAMF::Model::Scene *scene = project()->scene(event.scene);
655 YAMF::Model::Layer *layer = scene->layer(event.layer);
656 layer->setLocked(event.data.toBool());
658 break;
659 case TimeLineWidget::RenameLayer:
661 YAMF::Model::Scene *scene = project()->scene(event.scene);
662 YAMF::Model::Layer *layer = scene->layer(event.layer);
663 layer->setLayerName(event.data.toString());
665 break;
666 case TimeLineWidget::ChangeLayerVisibility:
668 YAMF::Model::Scene *scene = project()->scene(event.scene);
669 YAMF::Model::Layer *layer = scene->layer(event.layer);
670 layer->setVisible(event.data.toBool());
672 break;
674 case TimeLineWidget::InsertScene:
676 YAMF::Model::Scene *scene = project()->createScene(event.scene+1);
677 YAMF::Model::Layer *layer = scene->createLayer();
678 layer->createFrame();
680 break;
681 case TimeLineWidget::RemoveScene:
683 if(event.scene > -1)
685 YAMF::Model::Scene *scene = project()->scene(event.scene);
686 if(scene)
688 project()->removeScene(scene);
692 break;
693 case TimeLineWidget::MoveSceneUp:
695 project()->moveScene(event.scene, event.scene-1);
697 break;
698 case TimeLineWidget::MoveSceneDown:
700 project()->moveScene(event.scene, event.scene+1);
702 break;
703 case TimeLineWidget::LockScene:
705 YAMF::Model::Scene *scene = project()->scene(event.scene);
706 scene->setLocked(event.data.toBool());
708 break;
709 case TimeLineWidget::RenameScene:
711 YAMF::Model::Scene *scene = project()->scene(event.scene);
712 scene->setSceneName(event.data.toString());
714 break;