NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstaddcommand.cpp
1 #include "qnstaddcommand.h"
2 
3 #include "qnstutil.h"
4 
5 QnstAddCommand::QnstAddCommand(QnstView* view, QnstGraphicsEntity* entity)
6 {
7  this->view = view; parent = "";
8 
9  this->entity = NULL;
10 
11  copy(entity);
12 
13  ignore = true;
14 }
15 
16 QnstAddCommand::~QnstAddCommand()
17 {
18  delete entity;
19 }
20 
21 void QnstAddCommand::copy(QnstGraphicsEntity* entity)
22 {
23  this->entity = QnstUtil::makeGraphicsEntity(entity->getnstType());
24 
25  if (this->entity != NULL)
26  {
27  this->entity->setnstUid(entity->getnstUid());
28  this->entity->setnstId(entity->getnstId());
29 
30  this->entity->setUsrData(entity->getUsrData());
31 
32  this->entity->setTop(entity->getTop());
33  this->entity->setLeft(entity->getLeft());
34  this->entity->setWidth(entity->getWidth());
35  this->entity->setHeight(entity->getHeight());
36 
37  if (entity->getnstGraphicsParent() != NULL)
38  {
39  parent = entity->getnstGraphicsParent()->getnstUid();
40  }
41 
42  this->entity->setnstGraphicsParent(NULL);
43  }
44 }
45 
46 void QnstAddCommand::undo()
47 {
48  if ( !ignore && this->entity != NULL &&
49  view->entities.contains(entity->getnstUid()) )
50  {
51  view->requestEntityRemotion(view->entities[entity->getnstUid()], true);
52  }
53 }
54 
55 void QnstAddCommand::redo()
56 {
57  if (!ignore)
58  {
59  QnstGraphicsEntity* e = NULL;
60 
61  if (view->entities.contains(parent) ||
62  entity->getnstType() == Qnst::Body)
63  {
64  // instantiate the new entity
65  e = QnstUtil::makeGraphicsEntity(entity->getnstType());
66 
67  if (e != NULL)
68  {
69  e->setnstUid(entity->getnstUid());
70  e->setUsrData(entity->getUsrData());
71 
72  if (parent != "")
73  {
74  QnstGraphicsEntity* gparent = view->entities[parent];
75  gparent->addnstGraphicsEntity(e);
76 
77  e->setnstGraphicsParent(gparent);
78  }
79  else
80  {
81  e->setnstGraphicsParent(NULL);
82  }
83 
84  // \todo separate this in a function
85  e->setTop(entity->getTop());
86  e->setLeft(entity->getLeft());
87  e->setWidth(entity->getWidth());
88  e->setHeight(entity->getHeight());
89  e->adjust();
90 
91  view->requestEntityAddition(e, true);
92  }
93  }
94  }
95  else
96  {
97  ignore = false;
98  }
99 }