NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicscomposition.cpp
1 #include "qnstgraphicscomposition.h"
2 
3 #include "qnstgraphicsmedia.h"
4 #include "qnstgraphicscontext.h"
5 #include "qnstutil.h"
6 
7 #include <QDebug>
8 
9 QnstGraphicsComposition::QnstGraphicsComposition(QnstGraphicsEntity* parent)
10  : QnstGraphicsNode(parent)
11 {
12  setnstType(Qnst::Compostion);
13 
14  setColor("#EEEEEE");
15 
16  setAcceptDrops(true);
17 
18  lastw = 0;
19  lasth = 0;
20 
21  collapsed = false;
22 
23  tmp = NULL;
24 
25  //tmp = new QnstGraphicsComposition();
26  //tmp->hide();
27 
28  /* Default size and position */
29  if(parent)
30  {
31  setTop(parent->getHeight()/2 - DEFAULT_CONTEXT_HEIGHT/2);
32  setLeft(parent->getWidth()/2 - DEFAULT_CONTEXT_WIDTH/2);
33  }
34  else
35  {
36  setTop(0);
37  setLeft(0);
38  }
39 
40  setWidth(DEFAULT_CONTEXT_WIDTH);
41  setHeight(DEFAULT_CONTEXT_HEIGHT);
42 }
43 
44 QnstGraphicsComposition::~QnstGraphicsComposition()
45 {
46 
47 }
48 
49 QString QnstGraphicsComposition::getColor() const
50 {
51  return color;
52 }
53 
54 void QnstGraphicsComposition::setColor(QString color)
55 {
56  this->color = color;
57 }
58 
59 void QnstGraphicsComposition::setCollapsed(bool collapsed)
60 {
61  this->collapsed = collapsed;
62 }
63 
64 bool QnstGraphicsComposition::isCollapsed()
65 {
66  return collapsed;
67 }
68 
69 void QnstGraphicsComposition::updateToolTip()
70 {
71  QString tip = "";
72  QString name = (getnstId() != "" ? getnstId() : "?");
73 
74  if (getnstType() == Qnst::Context)
75  {
76  tip += "Context ("+name+")";
77  }
78  else if (getnstType() == Qnst::Switch)
79  {
80  tip += "Switch ("+name+")";
81  }
82  else if (getnstType() == Qnst::Body)
83  {
84  tip += "Body ("+name+")";
85  }
86  else
87  {
88  tip += "Composition ("+name+")";
89  }
90 
91  if (hasError)
92  tip += " - Error: " + erroMsg;
93 
94  setToolTip(tip);
95 }
96 
97 void QnstGraphicsComposition::draw(QPainter* painter)
98 {
99  // if (!isSelected() && hasMouseHover() && getnstType() != Qnst::Body){
100  // painter->setBrush(Qt::NoBrush);
101  // painter->setPen(QPen(QBrush(QColor("#999999")), 0, Qt::DashLine)); // 0px = cosmetic border
102 
103  // painter->drawRect(4, 4, getWidth(), getHeight());
104  // }
105 
106  if (!collapsed)
107  {
108  painter->setRenderHint(QPainter::Antialiasing,true);
109 
110  painter->setBrush(QColor(color));
111 
112  if (hasError)
113  painter->setPen(QPen(QBrush(Qt::red), 2));
114  else
115  painter->setPen(QPen(QBrush(Qt::black), 0));
116 
117  painter->drawEllipse(4, 4, getWidth()-1, getHeight()-1);
118 
119  if (isMoving())
120  {
121  painter->setBrush(Qt::NoBrush);
122  painter->setPen(QPen(QBrush(Qt::black), 0)); // 0px = cosmetic border
123 
124  painter->drawEllipse(getMoveLeft()+4-getLeft(), getMoveTop()+4-getTop(), getWidth()-1, getHeight()-1);
125 
126  }
127  else if (isResizing())
128  {
129  painter->setBrush(Qt::NoBrush);
130  painter->setPen(QPen(QBrush(Qt::black), 0)); // 0px = cosmetic border
131 
132  painter->drawEllipse(getResizeLeft()+4-getLeft(), getResizeTop()+4-getTop(), getResizeWidth()-1, getResizeHeight()-1);
133  }
134  }
135  else
136  {
137  painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
138 
139  painter->drawPixmap(4 + 8/2, 4 + 8/2, getWidth()-8, getHeight()-16-8, QPixmap(":/icon/context"));
140 
141  if (!isSelected() && hasMouseHover())
142  {
143  painter->setBrush(Qt::NoBrush);
144  painter->setPen(QPen(QBrush(QColor("#999999")), 0, Qt::DashLine)); // 0px = cosmetic border
145 
146  painter->drawRect(4, 4, getWidth(), getHeight());
147  }
148 
149  painter->setPen(QPen(QBrush(Qt::black),0));
150 
151  QString localid = (getnstId() != "" ? getnstId() : "?");
152 
153  if (localid.length() > 5)
154  {
155  localid = getnstId().replace(3,getnstId().length()-3,"...");
156  }
157 
158  painter->drawText(4 + 8/2, 4 + 8/2 + getHeight()-16-8, getWidth()-8, 16, Qt::AlignCenter, localid);
159 
160  if (isMoving())
161  {
162  painter->setBrush(Qt::NoBrush);
163  painter->setPen(QPen(QBrush(Qt::black), 0)); // 0px = cosmetic border
164 
165  painter->setRenderHint(QPainter::Antialiasing,false);
166  painter->drawRect(getMoveLeft()+4-getLeft(), getMoveTop()+4-getTop(), getWidth()-1, getHeight()-1);
167  }
168  }
169 }
170 
171 void QnstGraphicsComposition::delineate(QPainterPath* painter) const
172 {
173  painter->addEllipse(4, 4, getWidth(), getHeight());
174 }
175 
176 
177 void QnstGraphicsComposition::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
178 {
179  foreach(QUrl url, event->mimeData()->urls())
180  {
181  event->acceptProposedAction();
182 
183  return;
184  }
185 }
186 
187 void QnstGraphicsComposition::collapse()
188 {
189  mouseDoubleClickEvent(NULL);
190 }
191 
192 void QnstGraphicsComposition::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
193 {
194  if (tmp == NULL)
195  {
196  tmp = new QnstGraphicsComposition();
197  tmp->hide();
198  }
199 
200  if (collapsed)
201  {
202  setTop(getTop() - (lasth/2 - DEFAULT_MEDIA_HEIGHT/2));
203  setLeft(getLeft() - (lastw/2 - DEFAULT_MEDIA_WIDTH/2));
204  setWidth(lastw);
205  setHeight(lasth);
206 
207  foreach(QnstGraphicsEntity* e, getnstGraphicsEntities())
208  {
209  if (e->getncgType() == Qncg::Interface)
210  {
211  e->setTop(((e->getTop()*lasth)/DEFAULT_MEDIA_HEIGHT));
212  e->setLeft(((e->getLeft()*lastw)/DEFAULT_MEDIA_WIDTH));
213  }
214 
215  e->setnstGraphicsParent(this);
216  e->show();
217  e->adjust();
218 
219  if (e->getncgType() == Qncg::Interface)
220  {
221  qDebug() << e->getLeft() << e->getTop() << e->getWidth() << e->getHeight();
222  }
223  }
224 
225  setResizable(true);
226  }
227  else
228  {
229  tmp->setTop(getTop());
230  tmp->setLeft(getLeft());
231  tmp->setWidth(getWidth());
232  tmp->setHeight(getHeight());
233 
234  lastw = getWidth();
235  lasth = getHeight();
236 
237  foreach(QnstGraphicsEntity* e, getnstGraphicsEntities())
238  {
239  if (e->getncgType() != Qncg::Interface)
240  {
241  e->hide();
242  e->setnstGraphicsParent(tmp);
243  }
244  }
245 
246  setTop(getTop() + lasth/2 - DEFAULT_MEDIA_HEIGHT/2);
247  setLeft(getLeft() + lastw/2 - DEFAULT_MEDIA_WIDTH/2);
248  setWidth(DEFAULT_MEDIA_WIDTH);
249  setHeight(DEFAULT_MEDIA_HEIGHT);
250 
251  foreach(QnstGraphicsEntity* e, getnstGraphicsEntities())
252  {
253  if (e->getncgType() == Qncg::Interface)
254  {
255  e->setTop(((e->getTop()*DEFAULT_MEDIA_HEIGHT)/lasth));
256  e->setLeft(((e->getLeft()*DEFAULT_MEDIA_WIDTH)/lastw));
257 
258  e->adjust();
259  }
260  }
261 
262  setResizable(false);
263  }
264 
265  if (getnstGraphicsParent() != NULL)
266  {
267  getnstGraphicsParent()->adjust();
268  }
269 
270  setCollapsed(!collapsed);
271 }
272 
273 void QnstGraphicsComposition::dropEvent(QGraphicsSceneDragDropEvent *event)
274 {
275  foreach(QUrl url, event->mimeData()->urls())
276  {
277  QString filename = url.toLocalFile();
278  QString suffix = QFileInfo(filename).suffix().toLower();
279 
280  event->acceptProposedAction();
281  dropsrc = filename;
282 
283  createEntity(QnstUtil::getnstTypeFromExtension(suffix));
284  }
285 }
286 
287 void QnstGraphicsComposition::adjustWithSpring()
288 {
289  int it = 0;
290 
291  while(it++ < SPRING_INTERATION)
292  {
293  int N = getnstGraphicsEntities().size();
294  QPointF next[N];
295 
296  qreal vf[N];
297  qreal va[N];
298 
299  for (int I = 0; I < N; I++)
300  {
301  vf[I] = 0;
302  va[I] = 0;
303  }
304 
305  int I = 0;
306  foreach (QnstGraphicsEntity *entity, getnstGraphicsEntities())
307  {
308  if (entity->getncgType() == Qncg::Node)
309  {
310  QnstGraphicsNode* node = (QnstGraphicsNode*) entity;
311 
312  int attr = 1;
313 
314  qreal rforce = 0; // resultant vector magnitude
315  qreal rangle = 0; // resultant vector angle (direction)
316 
317  foreach(QnstGraphicsEdge* edge, node->getnstGraphicsEdges())
318  {
319  // setting entities
320  QnstGraphicsEntity* entitya;
321  QnstGraphicsEntity* entityb;
322 
323  if (edge->getEntityA() != node)
324  {
325  entitya = edge->getEntityB();
326  entityb = edge->getEntityA();
327  }
328  else
329  {
330  continue;
331  entitya = edge->getEntityA();
332  entityb = edge->getEntityB();
333  }
334 
335  // setting line
336  QLineF line;
337  line.setP1(QPointF(entitya->getLeft() + entitya->getWidth()/2,
338  entitya->getTop() + entitya->getHeight()/2));
339 
340  line.setP2(QPointF(entityb->getLeft() + entityb->getWidth()/2,
341  entityb->getTop() + entityb->getHeight()/2));
342 
343  qreal d = line.length();
344 
345  // calculating attraction
346  qreal cforce; // current vector magnitude
347  qreal cangle; // current vector angle (direction)
348 
349  attr = ((d - SPRING_LENGTH) > 0 ? 1 : -1);
350 
351  cforce = SPRING_CONSTANT*(d - SPRING_LENGTH >= 0 ? d - SPRING_LENGTH : (d - SPRING_LENGTH)*(-1));
352  cangle = line.angle();
353 
354  qreal fxa = rforce*cos(rangle*(PI / 180));
355  qreal fya = rforce*sin(rangle*(PI / 180));
356  qreal fxb = cforce*cos(cangle*(PI / 180));
357  qreal fyb = cforce*sin(cangle*(PI / 180));
358 
359  qreal fxr = fxa + fxb;
360  qreal fyr = fya + fyb;
361 
362  rforce = sqrt(pow(fxr,2) + pow(fyr,2));
363 
364  if (rforce == 0)
365  {
366  rangle = 0;
367  }
368  else if (fabs(fxr) > fabs(fyr))
369  {
370  rangle = ((180 / PI)*tanh(fyr/fxr));
371  }
372  else
373  {
374  rangle = 90 - ((180 / PI)*tanh(fxr/fyr));
375  }
376 
377  if (fxr < 0)
378  {
379  rangle += 180;
380  }
381  else if (fyr < 0 && rangle < 0)
382  {
383  rangle += 360;
384  }
385  }
386 
387  qreal fxa = vf[I]*cos(va[I]*(PI / 180));
388  qreal fya = vf[I]*sin(va[I]*(PI / 180));
389  qreal fxb = rforce*cos(rangle*(PI / 180));
390  qreal fyb = rforce*sin(rangle*(PI / 180));
391 
392  qreal fxr = fxa + fxb;
393  qreal fyr = fya + fyb;
394 
395  rforce = sqrt(pow(fxr,2) + pow(fyr,2));
396 
397  vf[I] = rforce*SPRING_DAMPING;
398 
399  if (rforce == 0)
400  {
401  rangle = 0;
402  }
403  else if (fabs(fxr) > fabs(fyr))
404  {
405  rangle = ((180 / PI)*tanh(fyr/fxr));
406  }
407  else
408  {
409  rangle = 90 - ((180 / PI)*tanh(fxr/fyr));
410  }
411 
412  if (fxr < 0)
413  {
414  rangle += 180;
415  }
416  else if (fyr < 0 && rangle < 0)
417  {
418  rangle += 360;
419  }
420 
421  va[I] = rangle;
422 
423  qreal rx = vf[I]*cos(va[I]*(PI / 180));
424  qreal ry = vf[I]*sin(va[I]*(PI / 180));
425 
426  if (rangle < 90)
427  {
428  next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
429  }
430  else if (rangle < 180)
431  {
432  next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
433  }
434  else if (rangle < 270)
435  {
436  next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
437  }
438  else
439  {
440  next[I] = QPointF(entity->getLeft() + rx*attr, entity->getTop() - ry*attr);
441  }
442  }
443 
444  I++;
445  }
446 
447  foreach (QnstGraphicsEntity *entity, getnstGraphicsEntities())
448  {
449  entity->setLeft(next[I].x());
450  entity->setTop(next[I].y());
451  }
452 
453  adjust();
454  }
455 }
456 
457 qreal QnstGraphicsComposition::getLastW()
458 {
459  return lastw;
460 }
461 
462 qreal QnstGraphicsComposition::getLastH()
463 {
464  return lasth;
465 }
466 
467 void QnstGraphicsComposition::setLastW(qreal lastW)
468 {
469  this->lastw = lastW;
470 }
471 
472 void QnstGraphicsComposition::setLastH(qreal lastH)
473 {
474  this->lasth = lastH;
475 }
476 
477 void QnstGraphicsComposition::setProperties(const QMap<QString, QString> &props)
478 {
479  QnstGraphicsNode::setProperties(props);
480 
481  if (props["collapsed"] != "")
482  {
483  if (props["collapsed"] == "1")
484  {
485  if (props["width"] != "")
486  setLastW(props["width"].toDouble());
487 
488  if (props["height"] != "")
489  setLastH(props["height"].toDouble());
490 
491  if (props["expandHeight"] != "")
492  setHeight(props["expandHeight"].toDouble());
493 
494  if (props["expandWidth"] != "")
495  setWidth(props["expandWidth"].toDouble());
496  }
497  else
498  {
499  if (props["width"] != "")
500  setWidth(props["width"].toDouble());
501 
502  if (props["height"] != "")
503  setHeight(props["height"].toDouble());
504 
505  if (props["expandHeight"] != "")
506  setLastH(props["expandHeight"].toDouble());
507 
508  if (props["expandWidth"] != "")
509  setLastW(props["expandWidth"].toDouble());
510  }
511  }
512 }
513 
514 void QnstGraphicsComposition::getProperties(QMap<QString, QString> &properties)
515 {
516  QnstGraphicsNode::getProperties(properties);
517 
518  properties["collapsed"] = QString::number(isCollapsed());
519  properties["expandWidth"] = QString::number(getWidth());
520  properties["expandHeight"] = QString::number(getHeight());
521 }
522 
523 //void QnstGraphicsComposition::setCollpsed(bool collapsed)
524 //{
525 // this->collapsed = collapsed;
526 //}