NCL Composer  0.1.5
 All Classes Functions Variables Pages
Commands.cpp
1 /* Copyright (c) 2011-2012 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include "util/Commands.h"
11 
12 #include "modules/MessageControl.h"
13 #include "modules/ProjectControl.h"
14 
15 namespace composer {
16  namespace core {
17  namespace util {
18 
19 EditCommand::EditCommand(Project *project, Entity *entity,
20  QMap <QString, QString> newAttrs, QUndoCommand *parent)
21  : QUndoCommand(parent)
22 {
23  this->project = project;
24 
25  this->uniqueId = entity->getUniqueId();
26  QMap <QString, QString>::iterator begin, end, it;
27  entity->getAttributeIterator(begin, end);
28  for (it = begin; it != end; ++it)
29  attrs[it.key()] = it.value();
30  this->newAttrs = newAttrs;
31 
32  msgControl = PluginControl::getInstance()->getMessageControl(project);
33 }
34 
35 void EditCommand::undo()
36 {
37  msgControl->anonymousChangeEntity(this->uniqueId, this->attrs);
38 }
39 
40 void EditCommand::redo()
41 {
42  msgControl->anonymousChangeEntity(this->uniqueId, this->newAttrs);
43 }
44 
45 RemoveCommand::RemoveCommand(Project *project, Entity *entity,
46  QUndoCommand *parent)
47 {
48  this->parentUniqueId = entity->getParentUniqueId();
49  this->entity = entity;
50  this->project = project;
51 
52  msgControl = PluginControl::getInstance()->getMessageControl(project);
53 
54  first = true;
55 }
56 
57 void RemoveCommand::undo()
58 {
59  msgControl->anonymousAddEntity(this->entity, parentUniqueId);
60 
61  // I have to do this because the core is responsible to Remove the entity.
62  this->entity = entity->cloneEntity();
63 
64  first = false;
65 
66  // \todo Undo every child of the entity (recursively).
67 }
68 
69 void RemoveCommand::redo()
70 {
71  //I have to do this because the core is responsible to Remove the entity.
72  Entity *entityTmp = this->entity->cloneEntity();
73  if(first)
74  project->removeEntity(this->entity, false);
75  else
76  msgControl->anonymousRemoveEntity(this->entity->getUniqueId());
77 
78  this->entity = entityTmp;
79 }
80 
81 AddCommand::AddCommand(Project *project, Entity *entity, QString parentUniqueId,
82  QUndoCommand *parent)
83 {
84  this->parentUniqueId = parentUniqueId;
85  this->entity = entity;
86  this->project = project;
87 
88  first = true;
89 
90  msgControl = PluginControl::getInstance()->getMessageControl(project);
91 }
92 
93 void AddCommand::undo()
94 {
95  this->entity = this->entity->cloneEntity();
96 
97  msgControl->anonymousRemoveEntity(this->entity->getUniqueId());
98 
99  first = false;
100 }
101 
102 void AddCommand::redo()
103 {
104  //I have to do this because the core is responsible to Remove the entity.
105  Entity *entityTmp = this->entity->cloneEntity();
106  //TODO - calll validator to check
107  if(first)
108  project->addEntity(this->entity, parentUniqueId);
109  else
110  msgControl->anonymousAddEntity(this->entity, parentUniqueId);
111 
112  this->entity = entityTmp;
113 }
114 
115 } } } //end namespace