Salome HOME
A new dialog class for edition of Ports of an InLine nodes is introduced. This funct...
[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   QString aValue;
218   SUPERVGUI_CanvasPortIn* aPort = 0;
219   SUPERVGUI_Canvas* aCanvas = myMain->getCanvas();
220   if (myLink && !SUPERV_isNull(myLink)) {
221     if (myInputPort) {
222       if (myInputPort->getEngine()->IsParam() || myInputPort->getEngine()->IsInLine()) {
223         aValue = QString(myInputPort->getEngine()->ToString());
224         aPort = (SUPERVGUI_CanvasPortIn*) myInputPort;
225       }
226     }
227     myLink->destroy();
228   }
229   delete this;
230   if (aPort && !aValue.isEmpty() && myMain->getDataflow()->GraphLevel() == 0) {
231     aPort->setValue(aValue);
232   }
233   aCanvas->update();
234 }
235
236 void SUPERVGUI_CanvasLink::addPoint() {
237   if (mySelectedItem && mySelectedItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge) {
238     SUPERVGUI_CanvasEdgePrs* anEdge = (SUPERVGUI_CanvasEdgePrs*) mySelectedItem;
239
240     int anIndex = 1;
241     QCanvasItemList::Iterator it;
242     for (it = myPrs.begin(); it != myPrs.end(); ++it) {
243       if ((*it) == anEdge) break;
244     }
245     if (it != myPrs.begin()) {
246       --it;
247       SUPERVGUI_CanvasPointPrs* aPoint = (SUPERVGUI_CanvasPointPrs*) (*it);
248       anIndex = aPoint->getIndex()+1;
249       if (anIndex < 1) anIndex = 1;
250     }
251     if (myLink && !SUPERV_isNull(myLink)) 
252       myLink->AddCoord(anIndex, mySelectedPoint.x(), mySelectedPoint.y());
253     merge();
254     myMain->getCanvas()->update();
255   }
256 }
257
258 void SUPERVGUI_CanvasLink::removePoint() {
259   if (mySelectedItem && mySelectedItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
260     SUPERVGUI_CanvasPointPrs* aPoint = (SUPERVGUI_CanvasPointPrs*) mySelectedItem;
261     if (myLink && !SUPERV_isNull(myLink)) 
262       myLink->RemoveCoord(aPoint->getIndex());
263     merge();
264     myMain->getCanvas()->update();
265   }
266 }
267
268 /*
269 //===============================================================================
270 //  SUPERVGUI_CanvasStreamLink: new link to be created
271 //===============================================================================
272 SUPERVGUI_CanvasStreamLink::SUPERVGUI_CanvasStreamLink(QObject* theParent, SUPERVGUI_Main* theMain, 
273                                                        SUPERV::StreamLink_ptr theLink):
274   SUPERVGUI_CanvasLink(theParent, theMain, theLink)
275 {
276   if (theLink && !SUPERV_isNull(theLink)) {
277     myStreamLink = SUPERV::StreamLink::_duplicate(theLink);
278   }
279 }
280
281
282 void SUPERVGUI_CanvasStreamLink::remove() {
283   QString aValue;
284   SUPERVGUI_CanvasPortIn* aPort = 0;
285   SUPERVGUI_Canvas* aCanvas = getMain()->getCanvas();
286   if (myStreamLink && !SUPERV_isNull(myStreamLink)) {
287     if (getInputPort()) {
288       if (getInputPort()->getEngine()->IsParam() || getInputPort()->getEngine()->IsInLine()) {
289         aPort = (SUPERVGUI_CanvasPortIn*) getInputPort();
290         aValue = QString(aPort->getEngine()->ToString());
291       }
292     }
293     myStreamLink->destroy();
294   }
295   delete this;
296   if (aPort && !aValue.isEmpty()) {
297     aPort->setValue(aValue);
298   }
299   aCanvas->update();
300 }
301 */
302
303 //===============================================================================
304 //  SUPERVGUI_CanvasLinkBuilder: new link to be created
305 //===============================================================================
306 SUPERVGUI_CanvasLinkBuilder::SUPERVGUI_CanvasLinkBuilder(QObject* theParent, SUPERVGUI_Main* theMain, 
307                                                          SUPERVGUI_CanvasPort* thePort):
308   SUPERVGUI_CanvasLink(theParent, theMain),
309   myPort(thePort),
310   myFloatingEdge(0)
311 {
312   if (myPort) {
313     myPort->addLink(this);
314     addPoint(myPort->getConnectionPoint());
315   }
316   myColor = SKETCH_COLOR;
317 }
318
319 SUPERVGUI_CanvasLinkBuilder::~SUPERVGUI_CanvasLinkBuilder()
320 {
321   if (myFloatingEdge) delete myFloatingEdge;
322   if (myPort) myPort->removeLink(this);
323 }
324
325 bool SUPERVGUI_CanvasLinkBuilder::canCreateEngine(SUPERVGUI_CanvasPort* thePort)
326 {
327   bool result = false;
328   if (thePort && myPort) {
329     SUPERVGUI_CanvasPort* aInPort;
330     SUPERVGUI_CanvasPort* aOutPort;
331
332     // check if ports are couple of input and output
333     if (myPort->getEngine()->IsInput()) {
334       aInPort = myPort;
335       if (thePort->getEngine()->IsInput())
336         return false;
337       aOutPort = thePort;
338     }
339     else {
340       aOutPort = myPort;
341       if (!thePort->getEngine()->IsInput())
342         return false;
343       aInPort = thePort;
344     }
345
346     // control if node is different
347     QString aInNode(aInPort->getEngine()->Node()->Name());
348     QString aOutNode(aOutPort->getEngine()->Node()->Name());
349     if (aInNode.compare(aOutNode) == 0) 
350       return false;
351
352     // control if no port is "Default"
353     QString aInName(aInPort->getEngine()->Name());
354     QString aOutName(aOutPort->getEngine()->Name());
355     if (aInName.compare("Default") == 0 ||
356         aOutName.compare("Default") == 0)
357       return false;
358
359     // control types of ports
360     int aInKind = aInPort->getEngine()->Kind();
361     int aOutKind = aOutPort->getEngine()->Kind();
362
363     // connect stream port with stream port only
364     if ((aInKind == SUPERV::DataStreamParameter && aOutKind != SUPERV::DataStreamParameter) ||
365         (aOutKind == SUPERV::DataStreamParameter && aInKind != SUPERV::DataStreamParameter))
366       return false;
367
368     // connect gate port with gate or inline port
369     if (aInKind == SUPERV::GateParameter || aOutKind == SUPERV::GateParameter) {
370       if (aInKind != aOutKind && 
371           aInKind != SUPERV::InLineParameter && 
372           aOutKind != SUPERV::InLineParameter)
373         return false;
374     }
375
376     // control if port is already linked except for input port of end switch node
377     if (!(aInKind == SUPERV::EndSwitchParameter || 
378           aInPort->getEngine()->Node()->Kind() == SUPERV::EndSwitchNode)) {
379       if (aInPort->getEngine()->IsLinked())
380         return false;
381     }
382     result = true;
383   }
384   return result;
385 }
386
387 void SUPERVGUI_CanvasLinkBuilder::setCoords(SUPERV::Link_ptr theLink)
388 {
389   if (theLink) {
390     QCanvasItemList::Iterator it;
391     int anIndex = 1;
392     if (myPort->getEngine()->IsInput()) {
393       it = myPrs.begin(); ++it; // ignore the first point
394       for (; it != myPrs.end(); ++it) {
395         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
396           theLink->AddCoord(anIndex++, (int)(*it)->x(), (int)(*it)->y());
397         }
398       }
399     }
400     else {
401       it = myPrs.end(); --it;
402       for (; it != myPrs.begin(); --it) {
403         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
404           theLink->AddCoord(anIndex++, (int)(*it)->x(), (int)(*it)->y());
405         }
406       }
407     }
408   }
409 }
410
411 void SUPERVGUI_CanvasLinkBuilder::addNextPoint(const QPoint& thePoint, bool theOrtho)
412 {
413   if (myFloatingEdge) myFloatingEdge->hide();
414
415   if (!theOrtho || myPrs.empty()) {
416     addPoint(thePoint);
417   }
418   else {
419     SUPERVGUI_CanvasPointPrs* aPrev = (SUPERVGUI_CanvasPointPrs*) myPrs.last();
420     int x = (int)aPrev->x(); int y = (int)aPrev->y();
421     if (thePoint.x() != x && thePoint.y() != y) {
422       addPoint(QPoint(thePoint.x(), y), -2);
423     }
424     addPoint(thePoint);
425   }
426   show();
427 }
428
429 void SUPERVGUI_CanvasLinkBuilder::setFloatPoint(const QPoint& thePoint)
430 {
431   if (!myFloatingEdge) {
432     myFloatingEdge = new QCanvasLine(getMain()->getCanvas());
433     myFloatingEdge->setPen(QPen(myColor, LINE_WIDTH));
434   }
435   if (!myPrs.empty()) {
436     myFloatingEdge->setPoints((int)myPrs.last()->x(), (int)myPrs.last()->y(), 
437                               thePoint.x(), thePoint.y());
438     myFloatingEdge->show();
439   }
440 }
441
442 void SUPERVGUI_CanvasLinkBuilder::removeLastPoint()
443 {
444   if (myPrs.count() > 1) {
445     QPoint aLast((int)myPrs.last()->x(), (int)myPrs.last()->y());
446     QCanvasItemList::Iterator it = myPrs.end();
447     bool removed = false;
448     --it;
449     for (; it != myPrs.begin(); --it) {
450       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
451         SUPERVGUI_CanvasPointPrs* aPoint = (SUPERVGUI_CanvasPointPrs*) (*it);
452         if (aPoint->getIndex() == -1 && removed) break;
453       }
454       QCanvasItem* anItem = (*it);
455       it = myPrs.remove(it);
456       delete anItem;
457       removed = true;
458     }
459     if (removed) {
460       if (myFloatingEdge) {
461         QPoint aPoint = myFloatingEdge->endPoint();
462         myFloatingEdge->setPoints((int)myPrs.last()->x(), (int)myPrs.last()->y(),
463                                   aPoint.x(), aPoint.y());
464       }
465       else
466         setFloatPoint(aLast);
467     }
468   }
469 }
470
471 void SUPERVGUI_CanvasLinkBuilder::moveByPort(SUPERVGUI_CanvasPort* thePort, int dx, int dy)
472 {
473   if (myPort && myPort == thePort) {
474     myPrs.first()->moveBy(dx, dy);
475     return;
476   }
477 }
478
479 void SUPERVGUI_CanvasLinkBuilder::moveByPort(SUPERVGUI_CanvasPort* thePort)
480 {
481   QPoint p = thePort->getConnectionPoint();
482   if (myPort && myPort == thePort) {
483     myPrs.first()->move(p.x(), p.y());
484     return;
485   }
486 }
487
488
489 //===============================================================================
490 //  SUPERVGUI_CanvasPointPrs: link point presentation
491 //===============================================================================
492 SUPERVGUI_CanvasPointPrs::SUPERVGUI_CanvasPointPrs(QCanvas* theCanvas, 
493                                                    SUPERVGUI_CanvasLink* theLink,
494                                                    const int& theIndex):
495   QCanvasEllipse(theCanvas),
496   myLink(theLink), myIndex(theIndex),
497   myInEdge(0), myOutEdge(0), myMoving(false)
498 {
499   setSize(POINT_SIZE, POINT_SIZE);
500   setZ(-1);
501 }
502
503 int SUPERVGUI_CanvasPointPrs::rtti() const
504 {
505   return SUPERVGUI_Canvas::Rtti_LinkPoint;
506 }
507
508 void SUPERVGUI_CanvasPointPrs::setInEdge(SUPERVGUI_CanvasEdgePrs* theEdge)
509 {
510   myInEdge = theEdge;
511   theEdge->setFromPoint((int)x(), (int)y());
512 }
513
514 void SUPERVGUI_CanvasPointPrs::setOutEdge(SUPERVGUI_CanvasEdgePrs* theEdge)
515 {
516   myOutEdge = theEdge;
517   theEdge->setToPoint((int)x(), (int)y());
518 }
519
520 void SUPERVGUI_CanvasPointPrs::moveBy(double dx, double dy)
521 {
522   QCanvasEllipse::moveBy(dx, dy);
523   if (myInEdge) myInEdge->setFromPoint((int)x(), (int)y());
524   if (myOutEdge) myOutEdge->setToPoint((int)x(), (int)y());
525   //resize canvas view if mouse is outside
526   int w = (int)(x()+dx) + width() + GRAPH_MARGIN;
527   int h = (int)(y()+dy) + height() + GRAPH_MARGIN;
528   if (canvas()->width() > w) w = canvas()->width();
529   if (canvas()->height() > h) h = canvas()->height();
530   if (canvas()->width() < w || canvas()->height() < h) canvas()->resize(w, h);
531   if (myIndex > 0 && isMoving()) {
532     myLink->getEngine()->ChangeCoord(myIndex, (int)x(), (int)y());
533   }
534 }
535
536 void SUPERVGUI_CanvasPointPrs::setColor(const QColor& theColor)
537 {
538   setBrush(theColor);
539 }
540
541 //===============================================================================
542 //  SUPERVGUI_CanvasEdgePrs: link edge presentation
543 //===============================================================================
544 SUPERVGUI_CanvasEdgePrs::SUPERVGUI_CanvasEdgePrs(QCanvas* theCanvas, 
545                                                  SUPERVGUI_CanvasLink* theLink):
546   QCanvasLine(theCanvas),
547   myLink(theLink)
548 {
549   setZ(-2);
550 }
551
552 int SUPERVGUI_CanvasEdgePrs::rtti() const
553 {
554   return SUPERVGUI_Canvas::Rtti_LinkEdge;
555 }
556
557 void SUPERVGUI_CanvasEdgePrs::setFromPoint(int x, int y)
558 {
559   setPoints(x, y, endPoint().x(), endPoint().y());
560 }
561
562 void SUPERVGUI_CanvasEdgePrs::setToPoint(int x, int y)
563 {
564   setPoints(startPoint().x(), startPoint().y(), x, y);
565 }
566
567 void SUPERVGUI_CanvasEdgePrs::setColor(const QColor& theColor)
568 {
569   setPen(QPen(theColor, LINE_WIDTH));
570 }