]> AND Private Git Repository - blast.git/blob - ConnectionItem.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
start modifying read/write blocks and project to take into account control ifaces
[blast.git] / ConnectionItem.cpp
1 #include "ConnectionItem.h"
2
3 #include "Dispatcher.h"
4 #include "Parameters.h"
5 #include "AbstractBoxItem.h"
6 #include "ConnectedInterface.h"
7 #include "InterfaceItem.h"
8 #include "AbstractBlock.h"
9
10 //int ConnectionItem::counter = 0;
11
12 ConnectionItem::ConnectionItem(InterfaceItem* _iface1,
13                                InterfaceItem* _iface2,
14                                Dispatcher* _dispatcher,
15                                Parameters* _params,
16                                QGraphicsItem *_parent) : QGraphicsItem(_parent) {
17
18
19   dispatcher = _dispatcher;
20   params = _params;
21
22   ConnectedInterface* ref1 = _iface1->refInter;
23   ConnectedInterface* ref2 = _iface2->refInter;
24   /* ref. note in .h
25      case 1 : ref1 is group interface, and ref2 block interface of a block within the group
26      case 2 : the opposite of case 1
27      case 3 : ref1 and ref2 are block interface of blocks that are both within the same parent group.
28    */
29   if (ref1->getOwner() == ref2->getOwner()->getParent()) {
30
31     if (ref1->getDirection() == AbstractInterface::Input) {
32       fromInterfaceItem = _iface1;
33       toInterfaceItem = _iface2;
34     }
35     else if (ref1->getDirection() == AbstractInterface::InOut) {
36       fromInterfaceItem = _iface1;
37       toInterfaceItem = _iface2;
38     }
39     else if (ref1->getDirection() == AbstractInterface::Output) {
40       toInterfaceItem = _iface1;
41       fromInterfaceItem = _iface2;
42     }
43   }
44   else if (ref1->getOwner()->getParent() == ref2->getOwner()) {
45
46     if (ref1->getDirection() == AbstractInterface::Input) {
47       fromInterfaceItem = _iface2;
48       toInterfaceItem = _iface1;
49     }
50     else if (ref1->getDirection() == AbstractInterface::InOut) {
51       fromInterfaceItem = _iface2;
52       toInterfaceItem = _iface1;
53     }
54     else if (ref1->getDirection() == AbstractInterface::Output) {
55       toInterfaceItem = _iface2;
56       fromInterfaceItem = _iface1;
57     }
58   }
59   // NB : this case is in fact similar to the previous. Kept here for clarity
60   else if (ref1->getOwner()->getParent() == ref2->getOwner()->getParent()) {
61
62     if (ref1->getDirection() == AbstractInterface::Input) {
63       fromInterfaceItem = _iface2;
64       toInterfaceItem = _iface1;
65     }
66     else if (ref1->getDirection() == AbstractInterface::InOut) {
67       fromInterfaceItem = _iface2;
68       toInterfaceItem = _iface1;
69     }
70     else if (ref1->getDirection() == AbstractInterface::Output) {
71       toInterfaceItem = _iface2;
72       fromInterfaceItem = _iface1;
73     }
74   }
75   // adding this to interface items
76   fromInterfaceItem->addConnectionItem(this);
77   toInterfaceItem->addConnectionItem(this);
78
79   selected = false;
80   marginConn = params->arrowLineLength+params->arrowWidth;
81
82   setFlag(ItemIsSelectable);
83   setAcceptHoverEvents(true);
84   setFlag(ItemSendsGeometryChanges);
85   setCursor(Qt::PointingHandCursor);
86   setZValue(0);
87   
88   if (fromInterfaceItem->refInter->getPurpose() == AbstractInterface::Data) {
89     visible = true;
90   }
91   else {
92     visible = false;
93   }
94
95   setPath();
96 }
97
98
99 ConnectionItem::ConnectionItem(const ConnectionItem &copy) {
100   pointFrom = copy.pointFrom;
101   pointTo = copy.pointTo;
102   interPoint1 = copy.interPoint1;
103   interPoint2 = copy.interPoint2;
104   interPoint3 = copy.interPoint3;
105   interPoint4 = copy.interPoint4;
106 }
107
108 ConnectionItem::~ConnectionItem() {
109 }
110
111 ConnectionItem::ConnectionItem() {
112 }
113
114 QPainterPath ConnectionItem::shape() const {
115   return pathShape;
116 }
117
118 QRectF ConnectionItem::boundingRect() const {
119
120   QPointF start, end;
121
122   if(pointFrom.x() < pointTo.x()){
123     start.setX(pointFrom.x()-20);
124     end.setX(pointTo.x()+20);
125   } else {
126     start.setX(pointTo.x()-20);
127     end.setX(pointFrom.x()+20);
128   }
129   if(pointFrom.y() < pointTo.y()){
130     start.setY(pointFrom.y()-20);
131     end.setY(pointTo.y()+20);
132   } else {
133     start.setY(pointTo.y()-20);
134     end.setY(pointFrom.y()+20);
135   }
136   return QRectF(start, end);
137 }
138
139 void ConnectionItem::paint(QPainter *painter,
140                            const QStyleOptionGraphicsItem *option,
141                            QWidget *widget) {
142
143   if (!visible) return;
144   
145   painter->setPen(Qt::blue);
146   if(selected){
147     painter->setPen(Qt::red);
148   }
149
150   painter->drawPath(pathPaint);
151 }
152
153 void ConnectionItem::addInterPoint(QPointF point) {
154
155 }
156
157 void ConnectionItem::setPath() {
158
159   // signals to the scene that this connection is going to change of shape.
160   prepareGeometryChange();
161
162   pointFrom = fromInterfaceItem->getEndPointInGroup();
163   pointTo = toInterfaceItem->getEndPointInGroup();
164
165   int oriFrom, oriTo;
166   oriFrom = fromInterfaceItem->getOrientation();
167   oriTo = toInterfaceItem->getOrientation();
168
169 /* NB: if src or dest is onwed by a GroupItem the orientation
170    must be changed as it is a block.
171  */
172   if(fromInterfaceItem->owner->isGroupItem()){
173     switch(fromInterfaceItem->getOrientation()){
174     case Parameters::North :
175       oriFrom = Parameters::South;
176       break;
177     case Parameters::South :
178       oriFrom = Parameters::North;
179       break;
180     case Parameters::East :
181       oriFrom = Parameters::West;
182       break;
183     case Parameters::West :
184       oriFrom = Parameters::East;
185       break;
186     }
187   }
188   if(toInterfaceItem->owner->isGroupItem()){
189     switch(toInterfaceItem->getOrientation()){
190     case Parameters::North :
191       oriTo = Parameters::South;
192       break;
193     case Parameters::South :
194       oriTo = Parameters::North;
195       break;
196     case Parameters::East :
197       oriTo = Parameters::West;
198       break;
199     case Parameters::West :
200       oriTo = Parameters::East;
201       break;
202     }
203   }
204   double gap1 = 0.0;
205   double gap2 = 0.0;
206
207   if(oriFrom == Parameters::South) {
208
209     // FROM SOUTH TO SOUTH
210     if(oriTo == Parameters::South) {
211       computeElle(oriFrom);
212     }
213     // FROM SOUTH TO NORTH
214     else if(oriTo == Parameters::North) {
215       gap1 = pointTo.y() - pointFrom.y();
216       if (gap1 > 2*marginConn) {
217         computeStaircase(oriFrom);
218       }
219       else {
220         computeEsse(oriFrom);
221       }
222     }
223     // FROM SOUTH TO EAST OR WEST
224     else if ((oriTo == Parameters::East) || (oriTo == Parameters::West)){
225
226       gap1 = pointTo.x() - pointFrom.x();
227       if (oriTo == Parameters::West) gap1 = -gap1;
228       gap2 = pointTo.y() - pointFrom.y();
229
230       if (gap1 > 0.0) {
231         if (gap2 > 0.0) {
232           computeHookSmallEnd(oriFrom,oriTo);
233         }
234         else {
235           computeOpenRect(oriFrom,oriTo);
236         }
237       }
238       else {
239         if (gap2 >= 0.0) {
240           computeCorner(oriFrom);
241         }
242         else {
243           computeHookSmallStart(oriFrom,oriTo);
244         }
245       }
246     }
247   }
248   else if(oriFrom == Parameters::North) {
249
250     // FROM NORTH TO SOUTH
251     if(oriTo == Parameters::South) {
252       gap1 = pointFrom.y() - pointTo.y();
253       if (gap1 > 2*marginConn) {
254         computeStaircase(oriFrom);
255       }
256       else {
257         computeEsse(oriFrom);
258       }
259     }
260     // FROM NORTH TO NORTH
261     else if(oriTo == Parameters::North) {
262       computeElle(oriFrom);
263     }
264     // FROM NORTH TO EAST OR WEST
265     else if ((oriTo == Parameters::East) || (oriTo == Parameters::West)){
266
267       gap1 = pointTo.x() - pointFrom.x();
268       if (oriTo == Parameters::West) gap1 = -gap1;
269       gap2 = pointFrom.y() - pointTo.y();
270
271       if (gap1 > 0.0) {
272         if (gap2 > 0.0) {
273           computeHookSmallEnd(oriFrom,oriTo);
274         }
275         else {
276           computeOpenRect(oriFrom,oriTo);
277         }
278       }
279       else {
280         if (gap2 >= 0.0) {
281           computeCorner(oriFrom);
282         }
283         else {
284           computeHookSmallStart(oriFrom,oriTo);
285         }
286       }
287     }
288   }
289   else if(oriFrom == Parameters::East) {
290     // FROM EAST TO NORTH OR SOUTH
291     if ((oriTo == Parameters::South) || (oriTo == Parameters::North)){
292
293       gap1 = pointFrom.x() - pointTo.x();
294       gap2 = pointFrom.y() - pointTo.y();
295       if (oriTo == Parameters::North) gap2 = -gap2;
296
297       if (gap1 > 0.0) {
298         if (gap2 > 0.0) {
299           computeHookSmallStart(oriFrom,oriTo);
300         }
301         else {
302           computeOpenRect(oriFrom,oriTo);
303         }
304       }
305       else {
306         if (gap2 >= 0.0) {
307           computeCorner(oriFrom);
308         }
309         else {
310           computeHookSmallEnd(oriFrom,oriTo);
311         }
312       }
313     }
314     else if(oriTo == Parameters::East) {
315       computeElle(oriFrom);
316     }
317     else if (oriTo == Parameters::West) {
318       gap1 = pointTo.x() - pointFrom.x();
319       if (gap1 > 2*marginConn) {
320         computeStaircase(oriFrom);
321       }
322       else {
323         computeEsse(oriFrom);
324       }
325     }
326   }
327   else if (oriFrom == Parameters::West) {
328
329     // FROM WEST TO NORTH OR SOUTH
330     if ((oriTo == Parameters::South) || (oriTo == Parameters::North)){
331
332       gap1 = pointTo.x() - pointFrom.x();
333       gap2 = pointFrom.y() - pointTo.y();
334       if (oriTo == Parameters::North) gap2 = -gap2;
335
336       if (gap1 > 0.0) {
337         if (gap2 > 0.0) {
338           computeHookSmallStart(oriFrom,oriTo);
339         }
340         else {
341           computeOpenRect(oriFrom,oriTo);
342         }
343       }
344       else {
345         if (gap2 >= 0.0) {
346           computeCorner(oriFrom);
347         }
348         else {
349           computeHookSmallEnd(oriFrom,oriTo);
350         }
351       }
352     }
353     else if(oriTo == Parameters::East) {
354       gap1 = pointFrom.x() - pointTo.x();
355       if (gap1 > 2*marginConn) {
356         computeStaircase(oriFrom);
357       }
358       else {
359         computeEsse(oriFrom);
360       }
361     }
362     else if (oriTo == Parameters::West) {
363       computeElle(oriFrom);
364     }
365   }
366
367   pps.setWidth(5);
368   pathShape = pps.createStroke(pathPaint);
369 }
370
371
372 void ConnectionItem::computeEsse(int orientationFrom) {
373
374   //cout << "drawing an esse" << endl;
375   pathPaint = QPainterPath(pointFrom);
376   interPoints.clear();
377   double gap = marginConn;
378   if ((orientationFrom == Parameters::North)||(orientationFrom == Parameters::West)) gap = -gap;
379   QPointF p(0.0,0.0);
380
381   if ((orientationFrom == Parameters::East)||(orientationFrom == Parameters::West)) {
382     // must draw a complete esse
383     p = QPointF(pointFrom.x()+gap,pointFrom.y());
384     pathPaint.lineTo(p);
385     interPoints.append(p);
386     p = QPointF(pointFrom.x()+gap,(pointFrom.y()+pointTo.y())/2.0);
387     pathPaint.lineTo(p);
388     interPoints.append(p);
389     p = QPointF(pointTo.x()-gap,(pointFrom.y()+pointTo.y())/2.0);
390     pathPaint.lineTo(p);
391     interPoints.append(p);
392     p = QPointF(pointTo.x()-gap,pointTo.y());
393     pathPaint.lineTo(p);
394     interPoints.append(p);
395     pathPaint.lineTo(pointTo);
396   }
397   else if ((orientationFrom == Parameters::South)||(orientationFrom == Parameters::North)) {
398
399     // must draw a complete esse
400     p = QPointF(pointFrom.x(),pointFrom.y()+gap);
401     pathPaint.lineTo(p);
402     interPoints.append(p);
403     p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointFrom.y()+gap);
404     pathPaint.lineTo(p);
405     interPoints.append(p);
406     p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointTo.y()-gap);
407     pathPaint.lineTo(p);
408     interPoints.append(p);
409     p = QPointF(pointTo.x(), pointTo.y()-gap);
410     pathPaint.lineTo(p);
411     interPoints.append(p);
412     pathPaint.lineTo(pointTo);
413   }
414 }
415
416 void ConnectionItem::computeStaircase(int orientationFrom) {
417
418   pathPaint = QPainterPath(pointFrom);
419   interPoints.clear();
420   QPointF p(0.0,0.0);
421
422   if ((orientationFrom == Parameters::East)||(orientationFrom == Parameters::West)) {
423     if (pointFrom.y() == pointTo.y()) {
424       //cout << "drawing straight line" << endl;
425       pathPaint.lineTo(pointTo);
426     }
427     else  {
428       //cout << "drawing a staircase" << endl;
429       // sufficient place to draw a simple staircase
430       p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointFrom.y());
431       pathPaint.lineTo(p);
432       interPoints.append(p);
433       p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointTo.y());
434       pathPaint.lineTo(p);
435       interPoints.append(p);
436       pathPaint.lineTo(pointTo);
437     }
438   }
439   else if ((orientationFrom == Parameters::South)||(orientationFrom == Parameters::North)) {
440     if (pointFrom.x() == pointTo.x()) {
441       //cout << "drawing straight line" << endl;
442       pathPaint.lineTo(pointTo);
443     }
444     else {
445       //cout << "drawing a staircase" << endl;
446       // sufficient place to draw a simple staircase
447       p = QPointF(pointFrom.x(),(pointFrom.y()+pointTo.y())/2.0);
448       pathPaint.lineTo(p);
449       interPoints.append(p);
450       p = QPointF(pointTo.x(),(pointFrom.y()+pointTo.y())/2.0);
451       pathPaint.lineTo(p);
452       interPoints.append(p);
453       pathPaint.lineTo(pointTo);
454     }
455   }
456 }
457
458 /* drawCorner():
459
460   A Corner has the following shape :
461   |
462   |__
463
464 */
465 void ConnectionItem::computeCorner(int orientationFrom) {
466
467   pathPaint = QPainterPath(pointFrom);
468   interPoints.clear();
469   QPointF p(0.0,0.0);
470   //cout << "drawing a corner" << endl;
471
472   if ((orientationFrom == Parameters::East)||(orientationFrom == Parameters::West)) {
473     p = QPointF(pointTo.x(),pointFrom.y());
474     pathPaint.lineTo(p);
475     interPoints.append(p);
476     pathPaint.lineTo(pointTo);
477   }
478   else if ((orientationFrom == Parameters::South)||(orientationFrom == Parameters::North)) {
479     p = QPointF(pointFrom.x(),pointTo.y());
480     pathPaint.lineTo(p);
481     interPoints.append(p);
482     pathPaint.lineTo(pointTo);
483   }
484 }
485
486 /* drawOpenRect():
487
488   A OpenRect has the following shape :
489   __
490   |
491   |_|
492 */
493 void ConnectionItem::computeOpenRect(int orientationFrom, int orientationTo) {
494   pathPaint = QPainterPath(pointFrom);
495   interPoints.clear();
496   QPointF p(0.0,0.0);
497   double gap1 = marginConn;
498   double gap2 = marginConn;
499   //cout << "drawing an OpenRect" << endl;
500
501   if ((orientationFrom == Parameters::East)||(orientationFrom == Parameters::West)) {
502     if (orientationFrom == Parameters::West) {
503       gap1 = -gap1;
504     }
505     if (orientationTo == Parameters::North) {
506       gap2 = -gap2;
507     }
508     p = QPointF(pointFrom.x()+gap1,pointFrom.y());
509     pathPaint.lineTo(p);
510     interPoints.append(p);
511     p = QPointF(pointFrom.x()+gap1,pointTo.y()+gap2);
512     pathPaint.lineTo(p);
513     interPoints.append(p);
514     p = QPointF(pointTo.x(),pointTo.y()+gap2);
515     pathPaint.lineTo(p);
516     interPoints.append(p);
517     pathPaint.lineTo(pointTo);
518
519   }
520   else if ((orientationFrom == Parameters::South)||(orientationFrom == Parameters::North)) {
521     if (orientationFrom == Parameters::North) {
522       gap1 = -gap1;
523     }
524     if (orientationTo == Parameters::West) {
525       gap2 = -gap2;
526     }
527     p = QPointF(pointFrom.x(),pointFrom.y()+gap1);
528     pathPaint.lineTo(p);
529     interPoints.append(p);
530     p = QPointF(pointTo.x()+gap2,pointFrom.y()+gap1);
531     pathPaint.lineTo(p);
532     interPoints.append(p);
533     p = QPointF(pointTo.x()+gap2,pointTo.y());
534     pathPaint.lineTo(p);
535     interPoints.append(p);
536     pathPaint.lineTo(pointTo);
537   }
538 }
539
540 /* drawHookSmallEnd():
541
542   A Hook has the following shape :
543   _
544    |
545    |_|
546    Its end has always a size of marginConn
547 */
548 void ConnectionItem::computeHookSmallEnd(int orientationFrom, int orientationTo) {
549   pathPaint = QPainterPath(pointFrom);
550   interPoints.clear();
551   QPointF p(0.0,0.0);
552   double gap = marginConn;
553   //cout << "drawing a Hook with small end" << endl;
554
555   if ((orientationFrom == Parameters::East)||(orientationFrom == Parameters::West)) {
556
557     if (orientationTo == Parameters::North) gap = -gap;
558
559     p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointFrom.y());
560     pathPaint.lineTo(p);
561     interPoints.append(p);
562     p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointTo.y()+gap);
563     pathPaint.lineTo(p);
564     interPoints.append(p);
565     p = QPointF(pointTo.x(),pointTo.y()+gap);
566     pathPaint.lineTo(p);
567     interPoints.append(p);
568     pathPaint.lineTo(pointTo);
569
570   }
571   else if ((orientationFrom == Parameters::South)||(orientationFrom == Parameters::North)) {
572
573     if (orientationTo == Parameters::West) gap = -gap;
574
575     p = QPointF(pointFrom.x(),(pointFrom.y()+pointTo.y())/2.0);
576     pathPaint.lineTo(p);
577     interPoints.append(p);
578     p = QPointF(pointTo.x()+gap,(pointFrom.y()+pointTo.y())/2.0);
579     pathPaint.lineTo(p);
580     interPoints.append(p);
581     p = QPointF(pointTo.x()+gap,pointTo.y());
582     pathPaint.lineTo(p);
583     interPoints.append(p);
584     pathPaint.lineTo(pointTo);
585   }
586 }
587
588 /* drawHookSmallStart():
589
590   A Hook has the following shape :
591   _
592    |
593    |_|
594    Its start has always a size of marginConn
595 */
596 void ConnectionItem::computeHookSmallStart(int orientationFrom, int orientationTo) {
597   pathPaint = QPainterPath(pointFrom);
598   interPoints.clear();
599   QPointF p(0.0,0.0);
600   double gap = marginConn;
601   //cout << "drawing a Hook with small start" << endl;
602
603   if ((orientationFrom == Parameters::East)||(orientationFrom == Parameters::West)) {
604
605     if (orientationFrom == Parameters::West) gap = -gap;
606
607     p = QPointF(pointFrom.x()+gap,pointFrom.y());
608     pathPaint.lineTo(p);
609     interPoints.append(p);
610     p = QPointF(pointFrom.x()+gap,(pointFrom.y()+pointTo.y())/2.0);
611     pathPaint.lineTo(p);
612     interPoints.append(p);
613     p = QPointF(pointTo.x(),(pointFrom.y()+pointTo.y())/2.0);
614     pathPaint.lineTo(p);
615     interPoints.append(p);
616     pathPaint.lineTo(pointTo);
617   }
618   else if ((orientationFrom == Parameters::South)||(orientationFrom == Parameters::North)) {
619
620     if (orientationFrom == Parameters::North) gap = -gap;
621
622     p = QPointF(pointFrom.x(),pointFrom.y()+gap);
623     pathPaint.lineTo(p);
624     interPoints.append(p);
625     p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointFrom.y()+gap);
626     pathPaint.lineTo(p);
627     interPoints.append(p);
628     p = QPointF((pointFrom.x()+pointTo.x())/2.0,pointTo.y());
629     pathPaint.lineTo(p);
630     interPoints.append(p);
631     pathPaint.lineTo(pointTo);
632   }
633 }
634
635 /* drawElle():
636
637   An Elle has the following shape :
638   |
639   |_|
640
641 */
642 void ConnectionItem::computeElle(int orientationFrom) {
643
644   pathPaint = QPainterPath(pointFrom);
645   interPoints.clear();
646   QPointF p(0.0,0.0);
647   double x;
648   double y;
649   switch(orientationFrom){
650   case Parameters::North :
651     if(pointFrom.y() < pointTo.y()) {
652       y = pointFrom.y()-marginConn;
653     }
654     else {
655       y = pointTo.y()-marginConn;
656     }
657     p = QPointF(pointFrom.x(),y);
658     pathPaint.lineTo(p);
659     interPoints.append(p);
660     p = QPointF(pointTo.x(),y);
661     pathPaint.lineTo(p);
662     interPoints.append(p);
663     pathPaint.lineTo(pointTo);
664     break;
665   case Parameters::South :
666     if(pointFrom.y() > pointTo.y()) {
667       y = pointFrom.y()+marginConn;
668     }
669     else {
670       y = pointTo.y()+marginConn;
671     }
672     p = QPointF(pointFrom.x(),y);
673     pathPaint.lineTo(p);
674     interPoints.append(p);
675     p = QPointF(pointTo.x(),y);
676     pathPaint.lineTo(p);
677     interPoints.append(p);
678     pathPaint.lineTo(pointTo);
679     break;
680   case Parameters::West :
681     if(pointFrom.x() < pointTo.x()) {
682       x = pointFrom.x()-marginConn;
683     }
684     else {
685       x = pointTo.x()-marginConn;
686     }
687     p = QPointF(x, pointFrom.y());
688     pathPaint.lineTo(p);
689     interPoints.append(p);
690     p = QPointF(x, pointTo.y());
691     pathPaint.lineTo(p);
692     interPoints.append(p);
693     pathPaint.lineTo(pointTo);
694     break;
695   case Parameters::East :
696     if(pointFrom.x() > pointTo.x()) {
697       x = pointFrom.x()+marginConn;
698     }
699     else {
700       x = pointTo.x()+marginConn;
701     }
702     p = QPointF(x, pointFrom.y());
703     pathPaint.lineTo(p);
704     interPoints.append(p);
705     p = QPointF(x, pointTo.y());
706     pathPaint.lineTo(p);
707     interPoints.append(p);
708     pathPaint.lineTo(pointTo);
709     break;
710   }
711 }
712
713 void ConnectionItem::setSelected(bool selected) {
714   this->selected = selected;
715   if(selected){
716     setZValue(50);
717   } else {
718     setZValue(0);
719   }
720 }
721
722 void ConnectionItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
723   QGraphicsItem::mousePressEvent(event);
724   setZValue(zValue()+100);  
725   setSelected(!selected);
726   update(boundingRect());
727 }
728
729 void ConnectionItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
730   QGraphicsItem::mouseReleaseEvent(event);
731   setZValue(zValue()-100);
732 }
733
734 void ConnectionItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
735   QGraphicsItem::mouseMoveEvent(event);
736 }
737
738 void ConnectionItem::contextMenuEvent(QGraphicsSceneContextMenuEvent * event) {
739   /* have to check if the connection can be removed.
740      If the from or to InterfaceItem is owned by a group item, and this item
741      is both connected to and from, thus it is impossible to remove this connection
742    */
743   bool canRemove = true;
744   InterfaceItem* groupIfaceItem = NULL;
745   if (fromInterfaceItem->getOwner()->isGroupItem()) {
746     groupIfaceItem = fromInterfaceItem;
747   }
748   else if (toInterfaceItem->getOwner()->isGroupItem()) {
749     groupIfaceItem = toInterfaceItem;
750   }
751
752   if (groupIfaceItem != NULL) {
753     ConnectedInterface* ref = groupIfaceItem->refInter;
754     if ((ref->isConnectedFrom()) && (ref->isConnectedTo())) {
755       canRemove = false;
756     }
757   }
758
759   if (canRemove) {
760     QMenu menu;
761     QAction* titleAction = menu.addAction("Connection operations");
762     titleAction->setEnabled(false);
763     menu.addSeparator();
764     QAction* removeAction = menu.addAction("Remove");
765     QAction * selectedAction= menu.exec(event->screenPos());
766
767     if(selectedAction == removeAction){
768       dispatcher->removeConnection(this);
769     }
770   }
771 }
772
773 void ConnectionItem::prepareChange() {
774   prepareGeometryChange();
775 }
776
777 QDataStream &operator <<(QDataStream &out, ConnectionItem &c) {
778   out.setVersion(QDataStream::Qt_4_8);
779
780   QByteArray connData;
781   QDataStream toWrite(&connData, QIODevice::WriteOnly);
782
783   toWrite << c.id;
784   toWrite << c.getFromInterfaceItem()->getId();
785   toWrite << c.getToInterfaceItem()->getId();
786
787   out << connData;
788
789   return out;
790 }
791
792 QDataStream &operator >>(QDataStream &in, ConnectionItem &c) {
793   in.setVersion(QDataStream::Qt_4_8);
794
795   return in;
796 }