NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicsbind.cpp
1 #include "qnstgraphicsbind.h"
2 
3 QnstGraphicsBind::QnstGraphicsBind(QnstGraphicsEntity* parent)
4  : QnstGraphicsEdge(parent)
5 {
6  // \todo We should have only Qnst::Bind
7  setnstType(Qnst::Condition);
8 
9  setType(Qnst::NoBindType);
10 
11  dialog = new QnstGraphicsBindDialog();
12 
13  conn = NULL;
14 
15  link = NULL;
16 }
17 
18 QnstGraphicsBind::~QnstGraphicsBind()
19 {
20  delete dialog;
21 }
22 
23 void QnstGraphicsBind::setType(Qnst::BindType type)
24 {
25  this->type = type;
26 
27  switch(type)
28  {
29  //ACTIONS
30  case Qnst::Start:
31  setnstType(Qnst::Action);
32  this->icon = ":/icon/start";
33  break;
34 
35  case Qnst::Stop:
36  setnstType(Qnst::Action);
37  this->icon = ":/icon/stop";
38  break;
39 
40  case Qnst::Resume:
41  setnstType(Qnst::Action);
42  this->icon = ":/icon/resume";
43  break;
44 
45  case Qnst::Pause:
46  setnstType(Qnst::Action);
47  this->icon = ":/icon/pause";
48  break;
49 
50  case Qnst::Set:
51  setnstType(Qnst::Action);
52  this->icon = ":/icon/set";
53  break;
54 
55  //CONDITIONS
56  case Qnst::onBegin:
57  setnstType(Qnst::Condition);
58  this->icon = ":/icon/onbegin";
59  break;
60 
61  case Qnst::onEnd:
62  setnstType(Qnst::Condition);
63  this->icon = ":/icon/onend";
64  break;
65 
66  case Qnst::onResume:
67  setnstType(Qnst::Condition);
68  this->icon = ":/icon/onresume";
69  break;
70 
71  case Qnst::onPause:
72  setnstType(Qnst::Condition);
73  this->icon = ":/icon/onpause";
74  break;
75 
76  case Qnst::onSelection:
77  setnstType(Qnst::Condition);
78  this->icon = ":/icon/onselection";
79  break;
80 
81  default:
82  if(getnstType() == Qnst::Action)
83  this->icon = ":/icon/noaction";
84  else
85  this->icon = ":/icon/nocondition";
86  break;
87  }
88 }
89 
90 Qnst::BindType QnstGraphicsBind::getType()
91 {
92  return type;
93 }
94 
95 QString QnstGraphicsBind::getIcon()
96 {
97  return icon;
98 }
99 
100 void QnstGraphicsBind::adjust()
101 {
102  // if(isCondition())
103  // adjust_condition();
104  // else
105  adjust_action();
106 }
107 
108 void QnstGraphicsBind::adjust_action()
109 {
110  if (getEntityA() != NULL && getEntityB() != NULL)
111  {
112  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
113  getEntityA()->getTop() + getEntityA()->getHeight()/2),
114  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
115  getEntityB()->getTop() + getEntityB()->getHeight()/2));
116 
117  if (getEntityA()->getncgType() == Qncg::Interface)
118  {
119  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
120  }
121 
122  if (getEntityB()->getncgType() == Qncg::Interface)
123  {
124  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
125  }
126 
127  QPointF pointa = line.p1();
128  QPointF pointb = line.p2();
129 
130  aux_adjust(pointa, pointb);
131 
132  getEntityB()->setSelectable(false);
133 
134  qreal index;
135 
136  if (pointa != pointb)
137  {
138  index = 1.0;
139 
140  int n = 0;
141 
142  while(getEntityB()->collidesWithItem(this))
143  {
144  index -= 0.01;
145 
146  if (getAngle() == 0)
147  pointb = line.pointAt(index);
148  else
149  pointb = arcPointAt(line , index);
150 
151  aux_adjust(pointa, pointb);
152 
153  if (++n > 100){ // avoiding infinity loop
154  break;
155  }
156  }
157  }
158 
159  getEntityB()->setSelectable(true);
160 
161  if (scene() != NULL)
162  scene()->update();
163  }
164 }
165 
166 void QnstGraphicsBind::adjust_condition()
167 {
168  if (getEntityA() != NULL && getEntityB() != NULL)
169  {
170  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
171  getEntityA()->getTop() + getEntityA()->getHeight()/2),
172  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
173  getEntityB()->getTop() + getEntityB()->getHeight()/2));
174 
175  if (getEntityA()->getncgType() == Qncg::Interface)
176  {
177  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
178  }
179 
180  if (getEntityB()->getncgType() == Qncg::Interface)
181  {
182  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
183  }
184 
185  QPointF pointa = line.p1();
186  QPointF pointb = line.p2();
187 
188  aux_adjust(pointa, pointb);
189 
190  getEntityA()->setSelectable(false);
191 
192  qreal index;
193 
194  if (pointa != pointb){
195 
196  index = 0;
197 
198  int n = 0;
199 
200  while(getEntityA()->collidesWithItem(this))
201  {
202  index += 0.01;
203 
204  if (getAngle() == 0)
205  pointa = line.pointAt(index);
206  else
207  pointa = arcPointAt(line , index, false);
208 
209  aux_adjust(pointa, pointb);
210 
211  if (++n > 100) // avoiding infinity loop
212  break;
213  }
214  }
215 
216  getEntityA()->setSelectable(true);
217 
218  if (scene() != NULL)
219  scene()->update();
220  }
221 }
222 
223 void QnstGraphicsBind::aux_adjust(QPointF pointa, QPointF pointb)
224 {
225  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
226  {
227  setTop(pointa.y()-4);
228  setLeft(pointa.x()-4);
229  setWidth((pointb.x()-4)-(pointa.x()-4) + 8);
230  setHeight((pointb.y()-4)-(pointa.y()-4) + 8);
231  }
232  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
233  {
234  setTop(pointa.y()-4);
235  setLeft(pointb.x()-4);
236  setWidth((pointa.x()-4)-(pointb.x()-4) + 8);
237  setHeight((pointb.y()-4)-(pointa.y()-4) + 8);
238  }
239  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
240  {
241  setTop(pointb.y()-4);
242  setLeft((pointa.x()-4));
243  setWidth((pointb.x()-4)-(pointa.x()-4) + 8);
244  setHeight((pointa.y()-4)-(pointb.y()-4) + 8);
245  }
246  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
247  {
248  setTop(pointb.y()-4);
249  setLeft(pointb.x()-4);
250  setWidth((pointa.x()-4)-(pointb.x()-4) + 8);
251  setHeight((pointa.y()-4)-(pointb.y()-4) + 8);
252  }
253 }
254 
255 
256 void QnstGraphicsBind::draw(QPainter* painter)
257 {
258  // if(isCondition())
259  // draw_condition(painter);
260  // else
261  draw_action(painter);
262 }
263 
264 void QnstGraphicsBind::draw_action(QPainter* painter)
265 {
266  if (getEntityA() != NULL && getEntityB() != NULL)
267  {
268  painter->setRenderHint(QPainter::Antialiasing, true);
269  painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
270 
271  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
272  getEntityA()->getTop() + getEntityA()->getHeight()/2),
273  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
274  getEntityB()->getTop() + getEntityB()->getHeight()/2));
275 
276  if (getEntityA()->getncgType() == Qncg::Interface)
277  {
278  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
279  }
280 
281  if (getEntityB()->getncgType() == Qncg::Interface)
282  {
283  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
284  }
285 
286  QPointF pointa = line.p1();
287  QPointF pointb = line.p2();
288 
289  if (!isInvalid() && !hasError)
290  painter->setPen(QPen(QBrush(QColor("#000000")), 1));
291  else
292  painter->setPen(QPen(QBrush(QColor(255,0,0,200)), 1, Qt::DashLine));
293 
294  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
295  {
296  if (getAngle() != 0)
297  {
298  qreal drawangle = getAdjAngle();
299 
300  QLineF localline(4+4,4+4, 4+8+getWidth()-16, 4+8+getHeight()-16);
301 
302  if (drawangle < 0)
303  {
304  drawangle = -drawangle;
305  }
306 
307  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
308 
309  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
310 
311  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
312  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
313 
314  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
315  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
316 
317  if (getAdjAngle() < 0)
318  {
319  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
320  16*((180-delta-drawangle)+180),16*drawangle);
321  }
322  else
323  {
324  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
325  ,16*(180-delta-drawangle),16*drawangle);
326  }
327  }
328  else
329  {
330  painter->drawLine(4+4,4+4, 4+8+getWidth()-16, 4+8+getHeight()-16);
331  }
332 
333  painter->setPen(Qt::NoPen);
334 
335  painter->drawPixmap(4+getWidth()-16, 4+getHeight()-16, 16, 16, QPixmap(icon));
336 
337  if (!isInvalid() && !hasError)
338  {
339  painter->setBrush(QBrush(QColor("#000000")));
340  }
341  else
342  {
343  painter->setBrush(QBrush(QColor(255,0,0,75)));
344  painter->drawEllipse(3+getWidth()-16, 3+getHeight()-16, 18, 18);
345  }
346  }
347  else if (pointa.x() > pointb.x() && pointa.y() <= pointb.y())
348  {
349  if (getAngle() != 0)
350  {
351  qreal drawangle = getAdjAngle();
352 
353  QLineF localline(4+4+getWidth()-8,4+4, 4+8, 4+8+getHeight()-16);
354 
355  if (drawangle < 0)
356  {
357  drawangle = -drawangle;
358  }
359 
360  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
361 
362  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
363 
364  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
365  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
366 
367  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
368  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
369 
370  if (getAdjAngle() < 0)
371  {
372  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
373  16*((180-delta-drawangle)+180),16*drawangle);
374  }
375  else
376  {
377  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
378  ,16*(180-delta-drawangle),16*drawangle);
379  }
380  }
381  else
382  {
383  painter->drawLine(4+4+getWidth()-8,4+4, 4+8, 4+8+getHeight()-16);
384  }
385 
386  painter->setPen(Qt::NoPen);
387 
388  painter->drawPixmap(4, 4+getHeight()-16, 16, 16, QPixmap(icon));
389 
390  if (!isInvalid() && !hasError)
391  {
392  painter->setBrush(QBrush(QColor("#000000")));
393  }
394  else
395  {
396  painter->setBrush(QBrush(QColor(255,0,0,75)));
397  painter->drawEllipse(3, 3+getHeight()-16, 18, 18);
398  }
399  }
400  else if (pointa.x() <= pointb.x() && pointa.y() > pointb.y())
401  {
402  if (getAngle() != 0)
403  {
404  qreal drawangle = getAdjAngle();
405 
406  QLineF localline(4+4, 4+4+getHeight()-8, 4+8+getWidth()-16, 4+8);
407 
408  if (drawangle < 0)
409  {
410  drawangle = -drawangle;
411  }
412 
413  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
414 
415  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
416 
417  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
418  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
419 
420  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
421  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
422 
423  if (getAdjAngle() < 0)
424  {
425  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
426  16*((180-delta-drawangle)+180),16*drawangle);
427  }
428  else
429  {
430  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
431  ,16*(180-delta-drawangle),16*drawangle);
432  }
433  }
434  else
435  {
436  painter->drawLine(4+4, 4+4+getHeight()-8, 4+8+getWidth()-16, 4+8);
437  }
438 
439  painter->setPen(Qt::NoPen);
440 
441  painter->drawPixmap(4+getWidth()-16,4,16,16, QPixmap(icon));
442 
443  if (!isInvalid() && !hasError)
444  {
445  painter->setBrush(QBrush(QColor("#000000")));
446  }
447  else
448  {
449  painter->setBrush(QBrush(QColor(255,0,0,75)));
450  painter->drawEllipse(3+getWidth()-16,3,18,18);
451  }
452  }
453  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
454  {
455  if (getAngle() != 0){
456  qreal drawangle = getAdjAngle();
457 
458  QLineF localline(4+4+getWidth()-8, 4+4+getHeight()-8, 4+8, 4+8);
459 
460  if (drawangle < 0)
461  {
462  drawangle = -drawangle;
463  }
464 
465  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
466 
467  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
468 
469  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
470  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
471 
472  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
473  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
474 
475  if (getAdjAngle() < 0)
476  {
477  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
478  16*((180-delta-drawangle)+180),16*drawangle);
479  }
480  else
481  {
482  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
483  ,16*(180-delta-drawangle),16*drawangle);
484  }
485  }
486  else
487  {
488  painter->drawLine(4+4+getWidth()-8, 4+4+getHeight()-8, 4+8, 4+8);
489  }
490 
491  painter->setPen(Qt::NoPen);
492 
493  painter->drawPixmap(4,4,16,16, QPixmap(icon));
494 
495  if (!isInvalid() && !hasError)
496  {
497  painter->setBrush(QBrush(QColor("#000000")));
498  }
499  else
500  {
501  painter->setBrush(QBrush(QColor(255,0,0,75)));
502  painter->drawEllipse(3,3,18,18);
503  }
504  }
505  }
506 }
507 
508 void QnstGraphicsBind::draw_condition(QPainter* painter)
509 {
510  if (getEntityA() != NULL && getEntityB() != NULL)
511  {
512  painter->setRenderHint(QPainter::Antialiasing, true);
513  painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
514 
515  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
516  getEntityA()->getTop() + getEntityA()->getHeight()/2),
517  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
518  getEntityB()->getTop() + getEntityB()->getHeight()/2));
519 
520  if (getEntityA()->getncgType() == Qncg::Interface)
521  {
522  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
523  }
524 
525  if (getEntityB()->getncgType() == Qncg::Interface)
526  {
527  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
528  }
529 
530  QPointF pointa = line.p1();
531  QPointF pointb = line.p2();
532 
533  if (!isInvalid() && !hasError)
534  painter->setPen(QPen(QBrush(QColor("#000000")), 1));
535  else
536  painter->setPen(QPen(QBrush(QColor(255,0,0,200)), 1, Qt::DashLine));
537 
538  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
539  {
540  if (getAngle() != 0)
541  {
542  qreal drawangle = getAdjAngle();
543 
544  QLineF localline(4+8,4+8, 4+getWidth()-4, 4+getHeight()-4);
545 
546  if (drawangle < 0)
547  {
548  drawangle = -drawangle;
549  }
550 
551  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
552 
553  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
554 
555  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
556  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
557 
558  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
559  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
560 
561  if (getAdjAngle() < 0)
562  {
563  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
564  16*((180-delta-drawangle)+180),16*drawangle);
565  }
566  else
567  {
568  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
569  ,16*(180-delta-drawangle),16*drawangle);
570  }
571  }
572  else
573  {
574  painter->drawLine(4+8,4+8, 4+getWidth()-4, 4+getHeight()-4);
575  }
576 
577  painter->setPen(Qt::NoPen);
578 
579  painter->drawPixmap(4,4,16,16, QPixmap(icon));
580 
581  if (!isInvalid() && !hasError)
582  {
583  painter->setBrush(QBrush(QColor("#000000")));
584  }
585  else
586  {
587  painter->setBrush(QBrush(QColor(255,0,0,75)));
588  painter->drawEllipse(3,3, 18, 18);
589  }
590  }
591  else if (pointa.x() >= pointb.x() && pointa.y() <= pointb.y())
592  {
593  if (getAngle() != 0)
594  {
595  qreal drawangle = getAdjAngle();
596 
597  QLineF localline(4+getWidth()-8,4+8, 4+4, 4+getHeight()-4);
598 
599  if (drawangle < 0)
600  {
601  drawangle = -drawangle;
602  }
603 
604  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
605 
606  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
607 
608  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
609  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
610 
611  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
612  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
613 
614  if (getAdjAngle() < 0)
615  {
616  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
617  16*((180-delta-drawangle)+180),16*drawangle);
618  }
619  else
620  {
621  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
622  ,16*(180-delta-drawangle),16*drawangle);
623  }
624  }
625  else
626  {
627  painter->drawLine(4+getWidth()-8,4+8, 4+4, 4+getHeight()-4);
628  }
629 
630  painter->setPen(Qt::NoPen);
631 
632  painter->drawPixmap(4+getWidth()-16,4,16,16, QPixmap(icon));
633 
634  if (!isInvalid() && !hasError)
635  {
636  painter->setBrush(QBrush(QColor("#000000")));
637  }
638  else
639  {
640  painter->setBrush(QBrush(QColor(255,0,0,75)));
641  painter->drawEllipse(3+getWidth()-16,3, 18, 18);
642  }
643  }
644  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
645  {
646  if (getAngle() != 0)
647  {
648  qreal drawangle = getAdjAngle();
649  QLineF localline(4+8, 4+getHeight()-8, 4+getWidth()-4, 4+4);
650 
651  if (drawangle < 0)
652  {
653  drawangle = -drawangle;
654  }
655 
656  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
657 
658  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
659 
660  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
661  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
662 
663  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
664  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
665 
666  if (getAdjAngle() < 0)
667  {
668  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
669  16*((180-delta-drawangle)+180),16*drawangle);
670  }
671  else
672  {
673  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
674  ,16*(180-delta-drawangle),16*drawangle);
675  }
676  }
677  else
678  {
679  painter->drawLine(4+8, 4+getHeight()-8, 4+getWidth()-4, 4+4);
680  }
681 
682  painter->setPen(Qt::NoPen);
683 
684  painter->drawPixmap(4, 4+getHeight()-16, 16, 16, QPixmap(icon));
685 
686  if (!isInvalid() && !hasError)
687  {
688  painter->setBrush(QBrush(QColor("#000000")));
689  }
690  else
691  {
692  painter->setBrush(QBrush(QColor(255,0,0,75)));
693  painter->drawEllipse(3, 3+getHeight()-16, 18, 18);
694  }
695  }
696  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
697  {
698  if (getAngle() != 0)
699  {
700  qreal drawangle = getAdjAngle();
701 
702  QLineF localline(4+getWidth()-8, 4+getHeight()-8, 4+4, 4+4);
703 
704  if (drawangle < 0)
705  {
706  drawangle = -drawangle;
707  }
708 
709  qreal R = localline.length()/(::sin(((drawangle/2)*PI)/180)*2);
710 
711  qreal delta = (180-drawangle)/2 + (360 - localline.angle());
712 
713  QPointF center_a(localline.p2().x() - ::cos((180-delta-drawangle)*PI/180)*R,
714  localline.p2().y() + ::sin((180-delta-drawangle)*PI/180)*R);
715 
716  QPointF center_b(localline.p1().x() + ::cos((180-delta-drawangle)*PI/180)*R,
717  localline.p1().y() - ::sin((180-delta-drawangle)*PI/180)*R);
718 
719  if (getAdjAngle() < 0)
720  {
721  painter->drawArc(center_b.x()-R,center_b.y()-R,2*R,2*R,
722  16*((180-delta-drawangle)+180),16*drawangle);
723  }
724  else
725  {
726  painter->drawArc(center_a.x()-R,center_a.y()-R,2*R,2*R
727  ,16*(180-delta-drawangle),16*drawangle);
728  }
729  }
730  else
731  {
732  painter->drawLine(4+getWidth()-8, 4+getHeight()-8, 4+4, 4+4);
733  }
734 
735  painter->setPen(Qt::NoPen);
736 
737  painter->drawPixmap(4+getWidth()-16, 4+getHeight()-16, 16, 16, QPixmap(icon));
738 
739  if (!isInvalid() && !hasError)
740  {
741  painter->setBrush(QBrush(QColor("#000000")));
742  }
743  else
744  {
745  painter->setBrush(QBrush(QColor(255,0,0,75)));
746  painter->drawEllipse(3+getWidth()-16, 3+getHeight()-16, 18, 18);
747  }
748  }
749  }
750 }
751 
752 void QnstGraphicsBind::delineate(QPainterPath* painter) const
753 {
754  // if(isCondition())
755  // delineate_condition(painter);
756  // else
757  delineate_action(painter);
758 }
759 
760 void QnstGraphicsBind::delineate_action(QPainterPath* painter) const
761 {
762  if (getEntityA() != NULL && getEntityB() != NULL)
763  {
764  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
765  getEntityA()->getTop() + getEntityA()->getHeight()/2),
766  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
767  getEntityB()->getTop() + getEntityB()->getHeight()/2));
768 
769  if (getEntityA()->getncgType() == Qncg::Interface)
770  {
771  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
772  }
773 
774  if (getEntityB()->getncgType() == Qncg::Interface)
775  {
776  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
777  }
778 
779  QPointF pointa = line.p1();
780  QPointF pointb = line.p2();
781 
782  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
783  {
784  painter->addEllipse(4+getWidth()-16, 4+getHeight()-16, 16, 16);
785  }
786  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
787  {
788  painter->addEllipse(4, 4+getHeight()-16, 16, 16);
789  }
790  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
791  {
792  painter->addEllipse(4+getWidth()-16,4,16,16);
793  }
794  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
795  {
796  painter->addEllipse(4,4,16,16);
797  }
798  }
799 }
800 
801 void QnstGraphicsBind::delineate_condition(QPainterPath* painter) const
802 {
803  if (getEntityA() != NULL && getEntityB() != NULL)
804  {
805  QLineF line = QLineF(QPointF(getEntityA()->getLeft() + getEntityA()->getWidth()/2,
806  getEntityA()->getTop() + getEntityA()->getHeight()/2),
807  QPointF(getEntityB()->getLeft() + getEntityB()->getWidth()/2,
808  getEntityB()->getTop() + getEntityB()->getHeight()/2));
809 
810  if (getEntityA()->getncgType() == Qncg::Interface)
811  {
812  line.setP1(getnstGraphicsParent()->mapFromItem(getEntityA()->getnstGraphicsParent(), line.p1()));
813  }
814 
815  if (getEntityB()->getncgType() == Qncg::Interface)
816  {
817  line.setP2(getnstGraphicsParent()->mapFromItem(getEntityB()->getnstGraphicsParent(), line.p2()));
818  }
819 
820  QPointF pointa = line.p1();
821  QPointF pointb = line.p2();
822 
823  if (pointa.x() <= pointb.x() && pointa.y() <= pointb.y())
824  {
825  // painter->drawLine(4+4,4+4, 4+4+getWidth()-8-2, 4+4+getHeight()-8-2);
826 
827  painter->addEllipse(4,4,16,16);
828 
829  }
830  else if (pointa.x() > pointb.x() && pointa.y() < pointb.y())
831  {
832  // painter->drawLine(4+4+getWidth()-8,4+4, 4+4, 4+4+getHeight()-8);
833 
834  painter->addEllipse(4+getWidth()-16,4,16,16);
835 
836 
837  }
838  else if (pointa.x() < pointb.x() && pointa.y() > pointb.y())
839  {
840  // painter->drawLine(4+4, 4+4+getHeight()-8, 4+4+getWidth()-8, 4+4);
841 
842  painter->addEllipse(4, 4+getHeight()-16, 16, 16);
843 
844  }
845  else if (pointa.x() > pointb.x() && pointa.y() > pointb.y())
846  {
847  // painter->drawLine(4+4+getWidth()-8, 4+4+getHeight()-8, 4+4, 4+4);
848 
849  painter->addEllipse(4+getWidth()-16, 4+getHeight()-16, 16, 16);
850  }
851  }
852 }
853 
854 void QnstGraphicsBind::addParam(QString uid, QString name, QString value)
855 {
856  if (name != "")
857  {
858  params[name] = value;
859  names_uids[name] = uid;
860 
861  emit bindParamUpdated(getnstUid(), params, names_uids);
862  }
863 }
864 
865 void QnstGraphicsBind::setParam(QString name, QString value)
866 {
867  if (name != "")
868  {
869  params[name] = value;
870 
871  emit bindParamUpdated(getnstUid(), params, names_uids);
872  }
873 }
874 
875 void QnstGraphicsBind::removeUId(QString uid)
876 {
877  QString name = names_uids.key(uid);
878 
879  if (params.contains(name))
880  {
881  params.remove(name);
882  names_uids.remove(name);
883 
884  emit bindParamUpdated(getnstUid(), params, names_uids);
885  }
886 }
887 
888 void QnstGraphicsBind::removeParam(QString name)
889 {
890  if(name != "")
891  {
892  params.remove(name);
893  names_uids.remove(name);
894 
895  emit bindParamUpdated(getnstUid(), params, names_uids);
896  }
897 }
898 
899 void QnstGraphicsBind::setConn(QnstConnector* conn)
900 {
901  this->conn = conn;
902 }
903 
904 void QnstGraphicsBind::setParams(QMap<QString, QString> params)
905 {
906  this->params = params;
907 }
908 
909 void QnstGraphicsBind::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
910 {
911  if (conn != NULL)
912  {
913  QString UID = "";
914  QMap<QString, QString> values;
915 
916  if (params.isEmpty())
917  {
918  if (isAction())
919  {
920  foreach(QString type, conn->getActions().values())
921  {
922  if (type == QnstUtil::getStrFromBindType(getType()))
923  {
924  UID = conn->getActions().key(type,"");
925  }
926  }
927 
928  QMap<QPair<QString, QString>, QString> parameters = conn->getActionParams();
929 
930  QPair<QString, QString> key;
931 
932  foreach(key , parameters.keys())
933  {
934  if (key.first == UID)
935  {
936  values[parameters.value(key)] = params.value(parameters.value(key),"");
937  }
938  }
939 
940 
941 
942  }
943  else if (isCondition())
944  {
945  foreach(QString type, conn->getConditions().values())
946  {
947  if (type == QnstUtil::getStrFromBindType(getType()))
948  {
949  UID = conn->getConditions().key(type,"");
950  }
951  }
952 
953  QMap<QPair<QString, QString>, QString> parameters = conn->getConditionParams();
954 
955  QPair<QString, QString> key;
956 
957  foreach(key , parameters.keys())
958  {
959  if (key.first == UID)
960  {
961  values[parameters.value(key)] = params.value(parameters.value(key),"");
962  }
963  }
964 
965 
966  }
967  }
968  else
969  {
970  values = params;
971  }
972 
973  dialog->init(values);
974 
975  if (dialog->exec())
976  {
977  params = dialog->getProperties();
978 
979  foreach(QString name, params.keys())
980  {
981  QMap<QString, QString> p;
982 
983  p["name"] = name;
984  p["value"] = params[name];
985 
986  if (names_uids[name].isEmpty())
987  {
988  names_uids[name] = QUuid::createUuid().toString();
989  }
990 
991  emit bindParamAdded(names_uids[name], getnstUid(), p);
992  }
993 
994  emit bindParamUpdated(getnstUid(), params, names_uids);
995  }
996  }
997  else
998  {
999  qWarning() << "[QNST] Connector is NULL. I can not load connectorParams";
1000  }
1001 }
1002 
1003 bool QnstGraphicsBind::isCondition() const
1004 {
1005  return (getnstType() == Qnst::Condition);
1006 }
1007 
1008 bool QnstGraphicsBind::isAction() const
1009 {
1010  return !isCondition();
1011 }
1012 
1013 QString QnstGraphicsBind::getRole() const
1014 {
1015  return role;
1016 }
1017 
1018 void QnstGraphicsBind::setRole(QString role)
1019 {
1020  this->role = role;
1021 }
1022 
1023 QString QnstGraphicsBind::getComponent() const
1024 {
1025  return component;
1026 }
1027 
1028 void QnstGraphicsBind::setComponent(QString component)
1029 {
1030  this->component = component;
1031 }
1032 
1033 QString QnstGraphicsBind::getComponentUid() const
1034 {
1035  return componentUID;
1036 }
1037 
1038 void QnstGraphicsBind::setComponentUid(QString componentUID)
1039 {
1040  this->componentUID = componentUID;
1041 }
1042 
1043 QString QnstGraphicsBind::getInterface() const
1044 {
1045  return interface;
1046 }
1047 
1048 void QnstGraphicsBind::setInterface(QString interface)
1049 {
1050  this->interface = interface;
1051 }
1052 
1053 QString QnstGraphicsBind::getInterfaceUid() const
1054 {
1055  return interfaceUID;
1056 }
1057 
1058 void QnstGraphicsBind::setInterfaceUid(QString interfaceUID)
1059 {
1060  this->interfaceUID = interfaceUID;
1061 }
1062 
1063 QMap<QString, QString> QnstGraphicsBind::getParams()
1064 {
1065  return params;
1066 }
1067 
1068 QMap<QString, QString> QnstGraphicsBind::getNameUIDs()
1069 {
1070  return names_uids;
1071 }
1072 
1073 void QnstGraphicsBind::setNamesUIDs(QMap<QString, QString> names_uids)
1074 {
1075  this->names_uids = names_uids;
1076 }
1077 
1078 void QnstGraphicsBind::setLink(QnstGraphicsLink *link)
1079 {
1080  this->link = link;
1081  setEntityA(link);
1082 }
1083 
1084 QnstGraphicsLink* QnstGraphicsBind::getLink()
1085 {
1086  return this->link;
1087 }
1088 
1089 void QnstGraphicsBind::setTarget(QnstGraphicsEntity *target)
1090 {
1091  setEntityB(target);
1092 }
1093 
1094 QnstGraphicsEntity *QnstGraphicsBind::getTarget()
1095 {
1096  return getEntityB();
1097 }
1098 
1099 void QnstGraphicsBind::setProperties(const QMap<QString, QString> &properties)
1100 {
1101  QnstGraphicsEdge::setProperties(properties);
1102 
1103  if(properties.contains("role"))
1104  {
1105  setRole(properties["role"]);
1106  setType(QnstUtil::getBindTypeFromStr(getRole()));
1107  }
1108 
1109  if(properties.contains("component"))
1110  setComponent("component");
1111 
1112  if(properties.contains("componentUid"))
1113  setComponentUid(properties["componentUid"]);
1114 
1115  if(properties.contains("interface"))
1116  setInterface(properties["interface"]);
1117 
1118  if(properties.contains("interfaceUid"))
1119  setInterfaceUid(properties["interfaceUid"]);
1120 }
1121 
1122 void QnstGraphicsBind::getProperties(QMap<QString, QString> &properties)
1123 {
1124  QnstGraphicsEdge::getProperties(properties);
1125 
1126  properties["role"] = getRole();
1127  properties["component"] = getComponent();
1128  properties["componentUid"] = getComponentUid();
1129  properties["interface"] = getInterface();
1130  properties["interfaceUid"] = getComponentUid();
1131 }