Salome HOME
4855b18adae231260f802b0680c3b23c39977385
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasLink.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE
4 //
5 //  File   : SUPERVGUI_GanvasNodePrs.cxx
6 //  Author : Natalia KOPNOVA
7 //  Module : SUPERV
8
9 using namespace std;
10 #include "SUPERVGUI_CanvasLink.h"
11 #include "SUPERVGUI_Canvas.h"
12 #include "SUPERVGUI_CanvasPort.h"
13 #include "SUPERVGUI_Main.h"
14
15 //#define CHECKTIME
16
17 #ifdef CHECKTIME
18 #include <sys/timeb.h>
19 #endif
20
21 #define DRAW_COLOR Qt::black
22 #define SELECT_COLOR Qt::magenta
23 #define SKETCH_COLOR Qt::darkGreen
24 #define STREAM_COLOR Qt::darkRed // QColor(0, 64, 128) // Qt::blue
25
26 #define LINE_WIDTH 1
27
28
29 SUPERVGUI_CanvasLink::SUPERVGUI_CanvasLink(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Link_ptr theLink):
30   QObject(theParent),
31   myMain(theMain),
32   myLink(0),
33   myInputPort(0),
34   myOutputPort(0),
35   myHilighted(false),
36   mySelectedItem(0)
37 {
38   if (theLink && !SUPERV_isNull(theLink)) {
39     myLink = SUPERV::Link::_duplicate(theLink);
40
41     SUPERVGUI_Canvas* aCanvas = myMain->getCanvas();
42     setName(aCanvas->getLinkName(theLink));
43
44     myInputPort = aCanvas->getPort(myLink->InPort());
45     if (myInputPort) myInputPort->addLink(this);
46
47     myOutputPort = aCanvas->getPort(myLink->OutPort());
48     if (myOutputPort) myOutputPort->addLink(this);
49
50     if (myInputPort->isStream())
51       myColor = STREAM_COLOR;
52     else
53       myColor = DRAW_COLOR;
54   }
55 }
56
57 SUPERVGUI_CanvasLink::~SUPERVGUI_CanvasLink()
58 {
59   for (QCanvasItemList::Iterator it = myPrs.begin(); it != myPrs.end(); ++it) {
60     (*it)->hide();
61     delete *it;
62   }
63   if (myInputPort) myInputPort->removeLink(this);
64   if (myOutputPort) myOutputPort->removeLink(this);
65 }
66
67 void SUPERVGUI_CanvasLink::createPrs()
68 {
69   if (myLink && !SUPERV_isNull(myLink)) {
70     if (myInputPort) {
71       addPoint(myInputPort->getConnectionPoint());
72     }
73     if (!myMain->getCanvas()->isControlView()) {
74       long x, y;
75       for (int i = 0; i < myLink->CoordsSize(); i++) {
76         myLink->Coords(i+1, x, y);
77         addPoint(QPoint(x, y), i+1);
78       }
79     }
80     if (myOutputPort) {
81       addPoint(myOutputPort->getConnectionPoint());
82     }
83   }
84   setColor(myHilighted ? SELECT_COLOR : myColor);
85 }
86
87 void SUPERVGUI_CanvasLink::addPoint(const QPoint& thePoint, const int& theIndex)
88 {
89   SUPERVGUI_CanvasPointPrs* aPoint;
90   if (myPrs.empty()) {
91     aPoint = new SUPERVGUI_CanvasPointPrs(myMain->getCanvas(), this, theIndex);
92     aPoint->setColor(myColor);
93     aPoint->move(thePoint.x(), thePoint.y());
94   }
95   else {
96     SUPERVGUI_CanvasPointPrs* aPrev = (SUPERVGUI_CanvasPointPrs*) myPrs.last();
97
98     SUPERVGUI_CanvasEdgePrs* anEdge = new SUPERVGUI_CanvasEdgePrs(myMain->getCanvas(), this);
99     anEdge->setColor(myColor);
100     myPrs.append(anEdge);
101
102     aPoint = new SUPERVGUI_CanvasPointPrs(myMain->getCanvas(), this, theIndex);
103     aPoint->setColor(myColor);
104     aPoint->move(thePoint.x(), thePoint.y());
105
106     aPrev->setOutEdge(anEdge);
107     aPoint->setInEdge(anEdge);
108   }
109   myPrs.append(aPoint);
110 }
111
112 void SUPERVGUI_CanvasLink::setSelectedObject(QCanvasItem* theItem, const QPoint& thePoint)
113 {
114   mySelectedItem = theItem;
115   mySelectedPoint = thePoint;
116 }
117
118 QPopupMenu* SUPERVGUI_CanvasLink::getPopupMenu(QWidget* theParent) 
119 {
120   QPopupMenu* popup = new QPopupMenu(theParent);
121   int anItem;
122   anItem = popup->insertItem(tr("MSG_DELLINK"), this, SLOT(remove()));
123   if (myInputPort && 
124       (myInputPort->getEngine()->Kind() != SUPERV::EndSwitchParameter ||
125        myInputPort->getEngine()->Node()->Kind() != SUPERV::EndSwitchNode))
126     if (myInputPort->getEngine()->Kind() == SUPERV::LoopParameter ||
127         myOutputPort && myOutputPort->getEngine()->Kind() == SUPERV::LoopParameter)
128       popup->setItemEnabled(anItem, false);
129
130   popup->insertSeparator();
131   if (mySelectedItem) {
132     anItem = popup->insertItem(tr("MSG_ADD_POINT"), this, SLOT(addPoint()));
133     if (mySelectedItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint)
134       popup->setItemEnabled(anItem, false);
135
136     anItem = popup->insertItem(tr("MSG_DEL_POINT"), this, SLOT(removePoint()));
137     if (mySelectedItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge)
138       popup->setItemEnabled(anItem, false);
139   }
140   return popup;
141 }
142
143 void SUPERVGUI_CanvasLink::show()
144 {
145   if (myPrs.isEmpty()) createPrs();
146
147   for (QCanvasItemList::Iterator it = myPrs.begin(); it != myPrs.end(); ++it) {
148     (*it)->show();
149   }
150 }
151
152 void SUPERVGUI_CanvasLink::merge()
153 {
154   // remove old presentation
155   for (QCanvasItemList::Iterator it = myPrs.begin(); it != myPrs.end(); ++it) {
156     delete *it;
157   }
158   myPrs.clear();
159
160   // display a new one
161   show();
162 }
163
164 void SUPERVGUI_CanvasLink::setHilighted(bool state)
165 {
166   myHilighted = state;
167   setColor(myHilighted ? SELECT_COLOR : myColor);
168   if (!myPrs.isEmpty()) {
169     bool disp = myPrs.first()->isVisible();
170     if (disp) {
171       for (QCanvasItemList::Iterator it = myPrs.begin(); it != myPrs.end(); ++it) {
172         (*it)->hide(); (*it)->show();
173       }
174       myMain->getCanvas()->update();
175     }
176   }
177 }
178
179 void SUPERVGUI_CanvasLink::moveByPort(SUPERVGUI_CanvasPort* thePort, int dx, int dy)
180 {
181   if (myInputPort && myInputPort == thePort) {
182     myPrs.first()->moveBy(dx, dy);
183     return;
184   }
185   if (myOutputPort && myOutputPort == thePort) {
186     myPrs.last()->moveBy(dx, dy);
187     return;
188   }
189 }
190
191 void SUPERVGUI_CanvasLink::moveByPort(SUPERVGUI_CanvasPort* thePort)
192 {
193   QPoint p = thePort->getConnectionPoint();
194   if (myInputPort && myInputPort == thePort) {
195     myPrs.first()->move(p.x(), p.y());
196     return;
197   }
198   if (myOutputPort && myOutputPort == thePort) {
199     myPrs.last()->move(p.x(), p.y());
200     return;
201   }
202 }
203
204 void SUPERVGUI_CanvasLink::setColor(const QColor& theColor)
205 {
206   for (QCanvasItemList::Iterator it = myPrs.begin(); it != myPrs.end(); ++it) {
207     if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
208       ((SUPERVGUI_CanvasPointPrs*)(*it))->setColor(theColor);
209     }
210     else if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge) {
211       ((SUPERVGUI_CanvasEdgePrs*)(*it))->setColor(theColor);
212     }
213   }
214 }
215
216 void SUPERVGUI_CanvasLink::remove() {
217   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
218
219   QString aValue;
220   SUPERVGUI_CanvasPortIn* aPort = 0;
221   SUPERVGUI_Canvas* aCanvas = myMain->getCanvas();
222   if (myLink && !SUPERV_isNull(myLink)) {
223     if (myInputPort) {
224       if (myInputPort->getEngine()->IsParam() || myInputPort->getEngine()->IsInLine()) {
225         aValue = QString(myInputPort->getEngine()->ToString());
226         aPort = (SUPERVGUI_CanvasPortIn*) myInputPort;
227       }
228     }
229     myLink->destroy();
230   }
231   delete this;
232   if (aPort && !aValue.isEmpty() && myMain->getDataflow()->GraphLevel() == 0) {
233     aPort->setValue(aValue);
234   }
235   aCanvas->update();
236 }
237
238 void SUPERVGUI_CanvasLink::addPoint() {
239   if (mySelectedItem && mySelectedItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge) {
240     SUPERVGUI_CanvasEdgePrs* anEdge = (SUPERVGUI_CanvasEdgePrs*) mySelectedItem;
241
242     int anIndex = 1;
243     QCanvasItemList::Iterator it;
244     for (it = myPrs.begin(); it != myPrs.end(); ++it) {
245       if ((*it) == anEdge) break;
246     }
247     if (it != myPrs.begin()) {
248       --it;
249       SUPERVGUI_CanvasPointPrs* aPoint = (SUPERVGUI_CanvasPointPrs*) (*it);
250       anIndex = aPoint->getIndex()+1;
251       if (anIndex < 1) anIndex = 1;
252     }
253     if (myLink && !SUPERV_isNull(myLink)) 
254       myLink->AddCoord(anIndex, mySelectedPoint.x(), mySelectedPoint.y());
255     merge();
256     myMain->getCanvas()->update();
257   }
258 }
259
260 void SUPERVGUI_CanvasLink::removePoint() {
261   if (mySelectedItem && mySelectedItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
262     SUPERVGUI_CanvasPointPrs* aPoint = (SUPERVGUI_CanvasPointPrs*) mySelectedItem;
263     if (myLink && !SUPERV_isNull(myLink)) 
264       myLink->RemoveCoord(aPoint->getIndex());
265     merge();
266     myMain->getCanvas()->update();
267   }
268 }
269
270 /*
271 //===============================================================================
272 //  SUPERVGUI_CanvasStreamLink: new link to be created
273 //===============================================================================
274 SUPERVGUI_CanvasStreamLink::SUPERVGUI_CanvasStreamLink(QObject* theParent, SUPERVGUI_Main* theMain, 
275                                                        SUPERV::StreamLink_ptr theLink):
276   SUPERVGUI_CanvasLink(theParent, theMain, theLink)
277 {
278   if (theLink && !SUPERV_isNull(theLink)) {
279     myStreamLink = SUPERV::StreamLink::_duplicate(theLink);
280   }
281 }
282
283
284 void SUPERVGUI_CanvasStreamLink::remove() {
285   QString aValue;
286   SUPERVGUI_CanvasPortIn* aPort = 0;
287   SUPERVGUI_Canvas* aCanvas = getMain()->getCanvas();
288   if (myStreamLink && !SUPERV_isNull(myStreamLink)) {
289     if (getInputPort()) {
290       if (getInputPort()->getEngine()->IsParam() || getInputPort()->getEngine()->IsInLine()) {
291         aPort = (SUPERVGUI_CanvasPortIn*) getInputPort();
292         aValue = QString(aPort->getEngine()->ToString());
293       }
294     }
295     myStreamLink->destroy();
296   }
297   delete this;
298   if (aPort && !aValue.isEmpty()) {
299     aPort->setValue(aValue);
300   }
301   aCanvas->update();
302 }
303 */
304
305 //===============================================================================
306 //  SUPERVGUI_CanvasLinkBuilder: new link to be created
307 //===============================================================================
308 SUPERVGUI_CanvasLinkBuilder::SUPERVGUI_CanvasLinkBuilder(QObject* theParent, SUPERVGUI_Main* theMain, 
309                                                          SUPERVGUI_CanvasPort* thePort):
310   SUPERVGUI_CanvasLink(theParent, theMain),
311   myPort(thePort),
312   myFloatingEdge(0)
313 {
314   if (myPort) {
315     myPort->addLink(this);
316     addPoint(myPort->getConnectionPoint());
317   }
318   myColor = SKETCH_COLOR;
319 }
320
321 SUPERVGUI_CanvasLinkBuilder::~SUPERVGUI_CanvasLinkBuilder()
322 {
323   if (myFloatingEdge) delete myFloatingEdge;
324   if (myPort) myPort->removeLink(this);
325 }
326
327 bool SUPERVGUI_CanvasLinkBuilder::canCreateEngine(SUPERVGUI_CanvasPort* thePort)
328 {
329   if (thePort && myPort) {
330     SUPERVGUI_CanvasPort* aInPort;
331     SUPERVGUI_CanvasPort* aOutPort;
332
333     // check if ports are couple of input and output
334     if (myPort->getEngine()->IsInput()) {
335       aInPort = myPort;
336       if (thePort->getEngine()->IsInput())
337         return false;
338       aOutPort = thePort;
339     }
340     else {
341       aOutPort = myPort;
342       if (!thePort->getEngine()->IsInput())
343         return false;
344       aInPort = thePort;
345     }
346
347     // control if nodes are different, not the same node
348     QString aInNode(aInPort->getEngine()->Node()->Name());
349     QString aOutNode(aOutPort->getEngine()->Node()->Name());
350     if (aInNode.compare(aOutNode) == 0) // linking outport and inport of the same node
351       return false;
352
353     // control types of ports
354     int aInKind = aInPort->getEngine()->Kind();
355     int aOutKind = aOutPort->getEngine()->Kind();
356
357     // connect stream port with stream port only
358     if ((aInKind == SUPERV::DataStreamParameter && aOutKind != SUPERV::DataStreamParameter) ||
359         (aOutKind == SUPERV::DataStreamParameter && aInKind != SUPERV::DataStreamParameter))
360       return false;
361
362     // asv : 15.12.04 : NOT allow to connect Gate-to-InLine --> it does not make sence!
363     // Out Gate port can be connected only to In Gate port 
364     if ( aOutKind == SUPERV::GateParameter && aInKind != SUPERV::GateParameter )
365       return false;
366
367     // In Gate can be connected to (receive links from) Gate port and InLine ports (important for Switch nodes)
368     if ( aInKind == SUPERV::GateParameter && aOutKind != SUPERV::GateParameter && aOutKind != SUPERV::InLineParameter )
369         return false;
370
371     // asv : 15.12.04 : PAL7374, p.2.2 "Bugs and Improvements": multiple links into Gate port 
372     //                  for InGate it's OK to accept more than 1 link
373     // THESE NEEDS REVISION, ALSO DON'T ALLOW MANY LINKS TO "DEFAULT" PORT OF EndSwitch
374     //if ( aInPort->getEngine()->IsLinked() && aInKind != SUPERV::GateParameter ) 
375     
376     // control if port is already linked except for input inline ports of end switch node (check for EndSwitchParameter)
377     // and "Default" port of Switch node (check for aNode->isEndSwitch()).  "Default" port is linked by default, but we
378     // let it to be "re-linked" to another port. 
379     const bool isEndSwitch = ( aInKind == SUPERV::EndSwitchParameter || aInPort->getEngine()->Node()->IsEndSwitch() );
380     if ( !isEndSwitch && aInPort->getEngine()->IsLinked() ) 
381       return false;
382     
383     return true;
384   }
385   return false;
386 }
387
388 void SUPERVGUI_CanvasLinkBuilder::setCoords(SUPERV::Link_ptr theLink)
389 {
390   if (theLink) {
391     QCanvasItemList::Iterator it;
392     int anIndex = 1;
393     if (myPort->getEngine()->IsInput()) {
394       it = myPrs.begin(); ++it; // ignore the first point
395       for (; it != myPrs.end(); ++it) {
396         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
397           theLink->AddCoord(anIndex++, (int)(*it)->x(), (int)(*it)->y());
398         }
399       }
400     }
401     else {
402       it = myPrs.end(); --it;
403       for (; it != myPrs.begin(); --it) {
404         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
405           theLink->AddCoord(anIndex++, (int)(*it)->x(), (int)(*it)->y());
406         }
407       }
408     }
409   }
410 }
411
412 void SUPERVGUI_CanvasLinkBuilder::addNextPoint(const QPoint& thePoint, bool theOrtho)
413 {
414   if (myFloatingEdge) myFloatingEdge->hide();
415
416   if (!theOrtho || myPrs.empty()) {
417     addPoint(thePoint);
418   }
419   else {
420     SUPERVGUI_CanvasPointPrs* aPrev = (SUPERVGUI_CanvasPointPrs*) myPrs.last();
421     int x = (int)aPrev->x(); int y = (int)aPrev->y();
422     if (thePoint.x() != x && thePoint.y() != y) {
423       addPoint(QPoint(thePoint.x(), y), -2);
424     }
425     addPoint(thePoint);
426   }
427   show();
428 }
429
430 void SUPERVGUI_CanvasLinkBuilder::setFloatPoint(const QPoint& thePoint)
431 {
432   if (!myFloatingEdge) {
433     myFloatingEdge = new QCanvasLine(getMain()->getCanvas());
434     myFloatingEdge->setPen(QPen(myColor, LINE_WIDTH));
435   }
436   if (!myPrs.empty()) {
437     myFloatingEdge->setPoints((int)myPrs.last()->x(), (int)myPrs.last()->y(), 
438                               thePoint.x(), thePoint.y());
439     myFloatingEdge->show();
440   }
441 }
442
443 void SUPERVGUI_CanvasLinkBuilder::removeLastPoint()
444 {
445   if (myPrs.count() > 1) {
446     QPoint aLast((int)myPrs.last()->x(), (int)myPrs.last()->y());
447     QCanvasItemList::Iterator it = myPrs.end();
448     bool removed = false;
449     --it;
450     for (; it != myPrs.begin(); --it) {
451       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
452         SUPERVGUI_CanvasPointPrs* aPoint = (SUPERVGUI_CanvasPointPrs*) (*it);
453         if (aPoint->getIndex() == -1 && removed) break;
454       }
455       QCanvasItem* anItem = (*it);
456       it = myPrs.remove(it);
457       delete anItem;
458       removed = true;
459     }
460     if (removed) {
461       if (myFloatingEdge) {
462         QPoint aPoint = myFloatingEdge->endPoint();
463         myFloatingEdge->setPoints((int)myPrs.last()->x(), (int)myPrs.last()->y(),
464                                   aPoint.x(), aPoint.y());
465       }
466       else
467         setFloatPoint(aLast);
468     }
469   }
470 }
471
472 void SUPERVGUI_CanvasLinkBuilder::moveByPort(SUPERVGUI_CanvasPort* thePort, int dx, int dy)
473 {
474   if (myPort && myPort == thePort) {
475     myPrs.first()->moveBy(dx, dy);
476     return;
477   }
478 }
479
480 void SUPERVGUI_CanvasLinkBuilder::moveByPort(SUPERVGUI_CanvasPort* thePort)
481 {
482   QPoint p = thePort->getConnectionPoint();
483   if (myPort && myPort == thePort) {
484     myPrs.first()->move(p.x(), p.y());
485     return;
486   }
487 }
488
489
490 //===============================================================================
491 //  SUPERVGUI_CanvasPointPrs: link point presentation
492 //===============================================================================
493 SUPERVGUI_CanvasPointPrs::SUPERVGUI_CanvasPointPrs(QCanvas* theCanvas, 
494                                                    SUPERVGUI_CanvasLink* theLink,
495                                                    const int& theIndex):
496   QCanvasEllipse(theCanvas),
497   myLink(theLink), myIndex(theIndex),
498   myInEdge(0), myOutEdge(0), myMoving(false)
499 {
500   setSize(POINT_SIZE, POINT_SIZE);
501   setZ(-1);
502 }
503
504 int SUPERVGUI_CanvasPointPrs::rtti() const
505 {
506   return SUPERVGUI_Canvas::Rtti_LinkPoint;
507 }
508
509 void SUPERVGUI_CanvasPointPrs::setInEdge(SUPERVGUI_CanvasEdgePrs* theEdge)
510 {
511   myInEdge = theEdge;
512   theEdge->setFromPoint(this);
513 }
514
515 void SUPERVGUI_CanvasPointPrs::setOutEdge(SUPERVGUI_CanvasEdgePrs* theEdge)
516 {
517   myOutEdge = theEdge;
518   theEdge->setToPoint(this); 
519 }
520
521 void SUPERVGUI_CanvasPointPrs::moveBy(double dx, double dy)
522 {
523   QCanvasEllipse::moveBy(dx, dy);
524   if (myInEdge) myInEdge->setFromPoint(this);
525   if (myOutEdge) myOutEdge->setToPoint(this);
526   //resize canvas view if mouse is outside
527   int w = (int)(x()+dx) + width() + GRAPH_MARGIN;
528   int h = (int)(y()+dy) + height() + GRAPH_MARGIN;
529   if (canvas()->width() > w) w = canvas()->width();
530   if (canvas()->height() > h) h = canvas()->height();
531   if (canvas()->width() < w || canvas()->height() < h) canvas()->resize(w, h);
532   if (myIndex > 0 && isMoving()) {
533     myLink->getEngine()->ChangeCoord(myIndex, (int)x(), (int)y());
534   }
535 }
536
537 void SUPERVGUI_CanvasPointPrs::setColor(const QColor& theColor)
538 {
539   setBrush(theColor);
540 }
541
542 //===============================================================================
543 //  SUPERVGUI_CanvasEdgePrs: link edge presentation
544 //===============================================================================
545 SUPERVGUI_CanvasEdgePrs::SUPERVGUI_CanvasEdgePrs(QCanvas* theCanvas, 
546                                                  SUPERVGUI_CanvasLink* theLink):
547   QCanvasLine(theCanvas),
548   myLink(theLink)
549 {
550   setZ(-2);
551 }
552
553 int SUPERVGUI_CanvasEdgePrs::rtti() const
554 {
555   return SUPERVGUI_Canvas::Rtti_LinkEdge;
556 }
557
558 void SUPERVGUI_CanvasEdgePrs::setFromPoint(SUPERVGUI_CanvasPointPrs* thePoint)
559 {
560   myStartPoint = thePoint;
561   setPoints((int)(thePoint->x()), (int)(thePoint->y()), endPoint().x(), endPoint().y());
562 }
563
564 void SUPERVGUI_CanvasEdgePrs::setToPoint(SUPERVGUI_CanvasPointPrs* thePoint)
565 {
566   myEndPoint = thePoint;
567   setPoints(startPoint().x(), startPoint().y(), (int)(thePoint->x()), (int)(thePoint->y()));
568 }
569
570 void SUPERVGUI_CanvasEdgePrs::setColor(const QColor& theColor)
571 {
572   setPen(QPen(theColor, LINE_WIDTH));
573 }
574
575 void SUPERVGUI_CanvasEdgePrs::moveBy(double dx, double dy)
576 {
577   //mkr: for moving segment of link
578   if (myStartPoint && myEndPoint) {
579     myStartPoint->setMoving(true);
580     myStartPoint->moveBy(dx, dy);
581     
582     myEndPoint->setMoving(true);
583     myEndPoint->moveBy(dx,dy);
584   }
585 }
586