NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicsentity.cpp
1 #include "qnstgraphicsentity.h"
2 #include "qnstgraphicsmedia.h"
3 #include "qnstgraphicscomposition.h"
4 
5 //
6 // ATTENTION: This code needs a refactoring.
7 //
8 
9 QnstGraphicsEntity::QnstGraphicsEntity(QnstGraphicsEntity* parent)
10  : QncgGraphicsEntity(parent), QnstEntity(parent)
11 {
12  setnstType(Qnst::NoType);
13 
14  setnstGraphicsParent(parent);
15 
16  if(parent)
17  parent->addnstGraphicsEntity(this);
18 
19  connect(this, SIGNAL(entitySelected()), SLOT(requestEntitySelection()));
20  connect(this, SIGNAL(entityAboutToChange(QMap<QString,QString>)),
21  SLOT(requestEntityPreparation(QMap<QString,QString>)));
22 
23  hover = false;
24  menu = NULL;
25  draggable = false;
26  hasError = false;
27 }
28 
29 
30 QnstGraphicsEntity::~QnstGraphicsEntity()
31 {
32 
33 }
34 
35 void QnstGraphicsEntity::setnstId(const QString &id)
36 {
37  QnstEntity::setnstId(id);
38  updateToolTip();
39 }
40 
41 QnstGraphicsEntity* QnstGraphicsEntity::getnstGraphicsParent() const
42 {
43  return parent;
44 }
45 
46 void QnstGraphicsEntity::setnstGraphicsParent(QnstGraphicsEntity* parent)
47 {
48  this->parent = parent;
49 
50  setnstParent(parent);
51  setncgGraphicsParent(parent);
52 }
53 
54 QSet<QnstGraphicsEntity*> QnstGraphicsEntity::getnstGraphicsEntities()
55 {
56  return entities;
57 }
58 
59 bool QnstGraphicsEntity::isDraggable()
60 {
61  return draggable;
62 }
63 
64 void QnstGraphicsEntity::setDraggable(bool isDraggable)
65 {
66  this->draggable = isDraggable;
67 }
68 
69 void QnstGraphicsEntity::addnstGraphicsEntity(QnstGraphicsEntity* entity)
70 {
71  if (entity != NULL)
72  {
73  // \fixme This is only an additional check. Maybe, the better way to do
74  // that could be using a set instead of a vector to entities.
75  if(!entities.contains(entity))
76  {
77  entities.insert(entity);
78 
79  // connect(entity, SIGNAL(undoRequested()), SIGNAL(undoRequested()));
80  // connect(entity, SIGNAL(redoRequested()), SIGNAL(redoRequested()));
81 
82  connect(entity, SIGNAL(cutRequested()), SIGNAL(cutRequested()));
83  connect(entity, SIGNAL(copyRequested()), SIGNAL(copyRequested()));
84  connect(entity, SIGNAL(pasteRequested()), SIGNAL(pasteRequested()));
85 
86  connect(entity, SIGNAL(deleteRequested()), SIGNAL(deleteRequested()));
87 
88  connect(entity, SIGNAL(exportRequested()), SIGNAL(exportRequested()));
89 
90  connect(entity, SIGNAL(zoominRequested()), SIGNAL(zoominRequested()));
91  connect(entity, SIGNAL(zoomoutRequested()), SIGNAL(zoomoutRequested()));
92  connect(entity, SIGNAL(zoomresetRequested()), SIGNAL(zoomresetRequested()));
93  connect(entity, SIGNAL(fullscreenRequested()),
94  SIGNAL(fullscreenRequested()));
95 
96  connect(entity, SIGNAL(entityAdded(QnstGraphicsEntity*)),
97  SIGNAL(entityAdded(QnstGraphicsEntity*)));
98  connect(entity, SIGNAL(entityChanged(QnstGraphicsEntity*)),
99  SIGNAL(entityChanged(QnstGraphicsEntity*)));
100 
101  connect(entity, SIGNAL(entityAboutToChange(QnstGraphicsEntity*,
102  QMap<QString,QString>)),
103  SIGNAL(entityAboutToChange(QnstGraphicsEntity*,
104  QMap<QString,QString>)));
105  connect(entity, SIGNAL(entityRemoved(QnstGraphicsEntity*)),
106  SIGNAL(entityRemoved(QnstGraphicsEntity*)));
107 
108  connect(entity, SIGNAL(entitySelected(QnstGraphicsEntity*)),
109  SIGNAL(entitySelected(QnstGraphicsEntity*)));
110 
111  addnstEntity(entity);
112  addncgGraphicsEntity(entity);
113 
114  if(entity)
115  entity->setnstGraphicsParent(this);
116  }
117  else
118  {
119  qWarning() << "[QNST] Warning! You are adding the same entity twice as \
120  child of " << QString().sprintf("%p", this) << __FILE__ << __LINE__;
121  }
122  }
123 }
124 
125 void QnstGraphicsEntity::removenstGraphicsEntity(QnstGraphicsEntity* entity)
126 {
127  if (entity != NULL)
128  {
129  entities.remove(entity);
130 
131  removenstEntity(entity);
132  removencgGraphicsEntity(entity);
133 
134  entity->setnstGraphicsParent(NULL);
135  }
136 }
137 
138 void QnstGraphicsEntity::requestEntityChange()
139 {
140  emit entityChanged(this);
141 }
142 
143 void QnstGraphicsEntity::requestEntityPreparation(QMap<QString, QString> props)
144 {
145  emit entityAboutToChange(this, props);
146 }
147 
148 void QnstGraphicsEntity::requestEntitySelection()
149 {
150  emit entitySelected(this);
151 }
152 
153 bool QnstGraphicsEntity::createEntity(Qnst::EntityType type)
154 {
155  // \todo Check if type is an media type allowed to me!
156  QnstGraphicsEntity *entity = QnstUtil::makeGraphicsEntity(type, this);
157 
158  if(entity == NULL) return false;
159 
160  QnstGraphicsMedia *content = dynamic_cast<QnstGraphicsMedia*>(entity);
161 
162  if(content != NULL) // If the Entity is a Media content
163  {
164  content->adjust();
165 
166  if (dropsrc != "") //if it is a drop we will keep the baseName as id
167  {
168  content->setSource(dropsrc);
169  QFileInfo file = QFileInfo(dropsrc);
170  QString nstId = file.baseName();
171  entity->setnstId(nstId);
172  dropsrc = "";
173  }
174  }
175  else
176  {
177  QnstGraphicsComposition *composition =
178  dynamic_cast<QnstGraphicsComposition*>(entity);
179 
180  //If the Entity is a Composition (i.e. Body, Context or Switch)
181  if(composition != NULL)
182  {
183  composition->adjust();
184 
185  composition->menu->actionPaste->setEnabled(menu->actionPaste->isEnabled());
186  }
187  }
188 
189  entity->adjust();
190 
191  emit entityAdded(entity);
192 }
193 
194 void QnstGraphicsEntity::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
195 {
196  QncgGraphicsEntity::contextMenuEvent(event);
197 
198  if (!event->isAccepted())
199  {
200  if (!isSelected())
201  {
202  setSelected(true);
203  emit entitySelected(this);
204  }
205 
206  if (menu != NULL)
207  menu->exec(event->screenPos());
208 
209  event->accept();
210  }
211 }
212 
213 bool QnstGraphicsEntity::hasMouseHover()
214 {
215  return hover;
216 }
217 
218 void QnstGraphicsEntity::setMouseHover(bool hover)
219 {
220  this->hover = hover;
221 }
222 
223 void QnstGraphicsEntity::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
224 {
225  QncgGraphicsEntity::hoverEnterEvent(event);
226 
227  hover = true;
228 }
229 
230 void QnstGraphicsEntity::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
231 {
232  QncgGraphicsEntity::hoverLeaveEvent(event);
233 
234  hover = false;
235 }
236 
237 void QnstGraphicsEntity::setError(bool hasError)
238 {
239  this->hasError = hasError;
240 }
241 
242 void QnstGraphicsEntity::setErrorMsg(QString erroMsg)
243 {
244  this->erroMsg = erroMsg;
245 }
246 
247 void QnstGraphicsEntity::setProperties(const QMap<QString, QString> &props)
248 {
249  if(props.contains("id"))
250  setnstId(props["id"]);
251 
252  if (props["top"] != "")
253  setTop(props["top"].toDouble());
254 
255  if (props["left"] != "")
256  setLeft(props["left"].toDouble());
257 
258  if (props["width"] != "")
259  setWidth(props["width"].toDouble());
260 
261  if (props["height"] != "")
262  setHeight(props["height"].toDouble());
263 }
264 
265 void QnstGraphicsEntity::getProperties(QMap <QString, QString> &props)
266 {
267  props["id"] = getnstId();
268 
269  props["top"] = QString::number(getTop());
270  props["left"] = QString::number(getLeft());
271  props["width"] = QString::number(getWidth());
272  props["height"] = QString::number(getHeight());
273  props["zIndex"] = QString::number(getzIndex());
274 }