Salome HOME
0d2b30fc9aee301e9db83ff4a3bf6fda1d3e70f6
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasView.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SUPERVGUI_CanvasView.cxx
8 //  Author : Natalia KOPNOVA
9 //  Module : SUPERV
10
11 using namespace std;
12
13 #include "SUPERVGUI_CanvasView.h"
14 #include "SUPERVGUI_Main.h"
15 #include "SUPERVGUI_CanvasNode.h"
16 #include "SUPERVGUI_CanvasPort.h"
17 #include "SUPERVGUI_CanvasLink.h"
18 #include "SUPERVGUI_CanvasNodePrs.h"
19 #include "SUPERVGUI_Clipboard.h"
20
21 #include "SUIT_MessageBox.h"
22
23 #include <qpixmap.h>
24 #include <qcolordialog.h>
25
26 #define MARGIN 2
27
28
29 /* XPM */
30 const char* imageZoomCursor[] = { 
31 "32 32 3 1",
32 ". c None",
33 "a c #000000",
34 "# c #ffffff",
35 "................................",
36 "................................",
37 ".#######........................",
38 "..aaaaaaa.......................",
39 "................................",
40 ".............#####..............",
41 "...........##.aaaa##............",
42 "..........#.aa.....a#...........",
43 ".........#.a.........#..........",
44 ".........#a..........#a.........",
45 "........#.a...........#.........",
46 "........#a............#a........",
47 "........#a............#a........",
48 "........#a............#a........",
49 "........#a............#a........",
50 ".........#...........#.a........",
51 ".........#a..........#a.........",
52 ".........##.........#.a.........",
53 "........#####.....##.a..........",
54 ".......###aaa#####.aa...........",
55 "......###aa...aaaaa.......#.....",
56 ".....###aa................#a....",
57 "....###aa.................#a....",
58 "...###aa...............#######..",
59 "....#aa.................aa#aaaa.",
60 ".....a....................#a....",
61 "..........................#a....",
62 "...........................a....",
63 "................................",
64 "................................",
65 "................................",
66 "................................"};
67
68 QPixmap zoomPix(imageZoomCursor);
69 QCursor zoom2Cursor(zoomPix);
70 QCursor handCursor(Qt::PointingHandCursor);
71
72 #if QT_VERSION >= 0x030005
73 QCursor pan2Cursor(Qt::SizeAllCursor);
74 #else
75 QCursor pan2Cursor(SizeAllCursor);
76 #endif
77
78 SUPERVGUI_CanvasView::SUPERVGUI_CanvasView(SUPERVGUI_Canvas* theCanvas, SUPERVGUI_Main* theMain):
79   QCanvasView(theCanvas, theMain),
80   myMain(theMain), myCurrentItem(0), myHilighted(0), myLinkBuilder(0)
81 {
82   setName("CanvasView");
83
84   myIsPanBtnClicked = false;
85   myIsPanActivated = false;
86   myIsFitWRActivated = false;
87   myIsZoomActivated = false;
88   myIsLinkCreating = false;
89
90   myAddStudyItem = 0;
91   mySelectedRect = 0;
92   myCursor = cursor();
93
94   myTimer = new QTimer(this);
95   connect(myTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
96
97   myPopup = new QPopupMenu(viewport());
98
99   const bool isEdit = myMain->isEditable();
100   if (isEdit) {
101     myPopup->insertItem(tr("MSG_ADD_NODE"), myMain, SLOT(addNode()));
102
103     // Paste Node functionality
104     SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
105     myPasteNodeItem = myPopup->insertItem(tr("ITM_PASTE_NODE"), aCB, SLOT(pasteNode()));
106
107     myPopup->insertItem(tr("MSG_INS_FILE"), myMain, SLOT(insertFile()));
108     myPopup->insertSeparator();
109   }
110
111   QPopupMenu* aViewPopup = new QPopupMenu(viewport());
112   aViewPopup->insertItem(tr("POP_FULLVIEW"), myMain, SLOT(showCanvas()));
113   aViewPopup->insertItem(tr("POP_CONTROLVIEW"), myMain, SLOT(showContolFlow()));
114   aViewPopup->insertItem(tr("POP_TABLEVIEW"), myMain, SLOT(showCanvasTable()));
115
116   myPopup->insertItem(tr("POP_VIEW"), aViewPopup);
117   myPopup->insertSeparator();
118
119   QPopupMenu* aZoomPopup = new QPopupMenu(viewport());
120   aZoomPopup->insertItem("200%", this, SLOT(zoomIn()));
121   aZoomPopup->insertItem("100%", this, SLOT(zoomReset()));
122   aZoomPopup->insertItem("50%", this, SLOT(zoomOut()));
123   aZoomPopup->insertSeparator();
124   //mkr: "Fit within rectangle" functionality
125   aZoomPopup->insertItem(tr("POP_FITWITHINRECT"), this, SLOT(fitWithinRect()));
126   aZoomPopup->insertSeparator();
127   aZoomPopup->insertItem(tr("POP_FITALL"), this, SLOT(fitAll()));
128   
129
130   myPopup->insertItem(tr("POP_ZOOM"), aZoomPopup);
131   myPopup->insertSeparator();
132
133   myAddStudyItem = myPopup->insertItem(tr("MSG_ADD_STUDY"), myMain, SLOT(addDataflowToStudy()));
134   myPopup->insertItem(tr(isEdit ? "MSG_CHANGE_INFO" : "MSG_INFO"), myMain, SLOT(changeInformation()));
135   myPopup->insertSeparator();
136
137   myPopup->insertItem(tr("MSG_COPY_DATAFLOW"), myMain, SLOT(copy()));
138   myPopup->insertItem(tr("MSG_FILTER_NOTIFY"), myMain, SLOT(filterNotification()));
139
140   myPopup->insertSeparator();
141   myPopup->insertItem(tr("MSG_CHANGE_BACKGROUND"), this, SLOT(changeBackground()));
142
143   viewport()->setMouseTracking(true);
144
145   //create sketching popup menu
146   mySketchPopup = new QPopupMenu(viewport());
147   mySketchPopup->setCheckable(true);
148   myDelPntItem = mySketchPopup->insertItem(tr("MSG_DEL_LAST_PNT"), this, SLOT(deletePoint()));
149   mySketchPopup->insertItem(tr("MSG_DEL_LINK"), this, SLOT(cancelSketch()));
150   myOrtoItem = mySketchPopup->insertItem(tr("MSG_ORTHO_LINE"), this, SLOT(setOrthoMode()));
151
152   
153   // add "change dataflow parameters" popup item
154   myPopup->insertSeparator();
155   myDSParamsItem = myPopup->insertItem( tr( "MSG_SET_GRAPHPARAMS" ), myMain, SLOT( changeDSGraphParameters() ) );
156 }
157  
158
159 SUPERVGUI_CanvasView::~SUPERVGUI_CanvasView()
160 {
161   SUPERVGUI_Clipboard::setNullClipboard();
162 }
163
164 void SUPERVGUI_CanvasView::contentsMousePressEvent(QMouseEvent* theEvent) 
165 {
166   myPoint = inverseWorldMatrix().map(theEvent->pos());
167   myGlobalPoint = theEvent->globalPos();
168   myCurrentItem = 0;
169
170   // compute collision rectangle
171   QRect aSel(myPoint.x()-MARGIN, myPoint.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
172
173   if (((theEvent->button() == Qt::MidButton) &&
174        (theEvent->state() == Qt::ControlButton)) || 
175       myIsPanBtnClicked) {
176     myIsPanActivated = true;
177     myCursor = cursor();
178     setCursor(pan2Cursor);
179     return;
180   }
181
182   if (((theEvent->button() == Qt::LeftButton) &&
183        (theEvent->state() == Qt::ControlButton)) || 
184       myIsZoomActivated) {
185     myIsZoomActivated = true;
186     myCursor = cursor();
187     setCursor(zoom2Cursor);
188     return;
189   }
190
191   if ( theEvent->button() == Qt::RightButton) {
192     if (myIsLinkCreating) {
193       myMain->showPopup(mySketchPopup, theEvent);
194       return;
195     }
196
197     QCanvasItemList l = canvas()->collisions(aSel);
198     for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
199       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
200         SUPERVGUI_CanvasNodePrs* aNodePrs = (SUPERVGUI_CanvasNodePrs*) (*it);
201         QObject* anObj = aNodePrs->getObject(myPoint);
202         if (anObj->inherits("SUPERVGUI_CanvasNode")) {
203           myMain->showPopup(((SUPERVGUI_CanvasNode*)anObj)->getPopupMenu(viewport()), 
204                             theEvent);
205           return;
206         }
207         else if (anObj->inherits("SUPERVGUI_CanvasPort")) {
208           myMain->showPopup(((SUPERVGUI_CanvasPort*)anObj)->getPopupMenu(viewport()), 
209                             theEvent);
210           return;
211         }
212       }
213       if (myMain->isEditable() && !myMain->getCanvas()->isControlView()) {
214         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
215           SUPERVGUI_CanvasPointPrs* aPrs = (SUPERVGUI_CanvasPointPrs*) (*it);
216           aPrs->getLink()->setSelectedObject(aPrs, myPoint);
217           myMain->showPopup(aPrs->getLink()->getPopupMenu(viewport()), theEvent);
218           return;
219         }
220         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge) {
221           SUPERVGUI_CanvasEdgePrs* aPrs = (SUPERVGUI_CanvasEdgePrs*) (*it);
222           aPrs->getLink()->setSelectedObject(aPrs, myPoint);
223           myMain->showPopup(aPrs->getLink()->getPopupMenu(viewport()), theEvent);
224           return;
225         }
226       }
227     }
228         
229     myPopup->setItemEnabled(myAddStudyItem, !myMain->isDataflowInStudy());
230     // Paste Node functionality
231     SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard(); 
232     myPopup->setItemEnabled(myPasteNodeItem, aCB->isCopyNode() );
233        
234     myPopup->setItemEnabled( myDSParamsItem, isHavingStreamPort()/*myMain->getDataflow()->IsStreamGraph()*/ );
235
236     myMain->showPopup(myPopup, theEvent);
237     return;
238   }
239
240   if (theEvent->button() == Qt::LeftButton) {
241     if (!myIsFitWRActivated) {//not moving items if fit within rectangle 
242                               //functionality is enable
243       QCanvasItemList l = canvas()->collisions(myPoint);
244       if (myIsLinkCreating) {
245         for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
246           if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
247             SUPERVGUI_CanvasNodePrs* aNodePrs = (SUPERVGUI_CanvasNodePrs*) (*it);
248             QObject* anObj = aNodePrs->getObject(myPoint);
249             if (anObj->inherits("SUPERVGUI_CanvasPort")) {
250               endSketch((SUPERVGUI_CanvasPort*)anObj);
251               return;
252             }
253             else {
254               myCurrentItem = *it;
255               ((SUPERVGUI_CanvasNodePrs*)myCurrentItem)->setZ(2);
256               ((SUPERVGUI_CanvasNodePrs*)myCurrentItem)->setMoving(true);
257               return;
258             }
259           }
260           if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Hook) {
261             SUPERVGUI_CanvasHookPrs* aHookPrs = (SUPERVGUI_CanvasHookPrs*) (*it);
262             QObject* anObj = aHookPrs->getObject();
263             if (anObj->inherits("SUPERVGUI_CanvasPort")) {
264               endSketch((SUPERVGUI_CanvasPort*)anObj);
265               return;
266             }
267           }
268         }
269         if (myLinkBuilder) {
270           myLinkBuilder->addNextPoint(myPoint, mySketchPopup->isItemChecked(myOrtoItem));
271           canvas()->update();
272           mySketchPopup->setItemEnabled(myDelPntItem, true);
273           return;
274         }
275       }
276       
277 //  if (myMain->isEditable()) { // allow to move nodes and link point on imported graph
278       for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
279         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
280           myCurrentItem = *it;
281           ((SUPERVGUI_CanvasNodePrs*)myCurrentItem)->setZ(2);
282           ((SUPERVGUI_CanvasNodePrs*)myCurrentItem)->setMoving(true);
283           return;
284         }
285         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
286           SUPERVGUI_CanvasPointPrs* aPrs = (SUPERVGUI_CanvasPointPrs*) (*it);
287           if (aPrs->getIndex() > 0) {
288             myCurrentItem = *it;
289             aPrs->setMoving(true);
290             return;
291           }
292         }
293         if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge) {
294           //mkr: for moving segment of link
295           SUPERVGUI_CanvasEdgePrs* aPrs = (SUPERVGUI_CanvasEdgePrs*) (*it);
296           if (aPrs->getLink()->getInputPort()->getConnectionPoint() == aPrs->startPoint()
297               ||
298             aPrs->getLink()->getInputPort()->getConnectionPoint() == aPrs->endPoint()
299               ||
300               aPrs->getLink()->getOutputPort()->getConnectionPoint() == aPrs->startPoint()
301               ||
302               aPrs->getLink()->getOutputPort()->getConnectionPoint() == aPrs->endPoint()) {
303             return;
304           }
305           myCurrentItem = *it;
306           aPrs->setMoving(true);
307           return;
308         }
309       }
310 //  }
311     } 
312   }
313 }
314
315 bool busy = false;
316
317 void SUPERVGUI_CanvasView::contentsMouseMoveEvent(QMouseEvent* theEvent) 
318 {
319   if (busy) return;
320       
321   busy = true;
322   QPoint p = inverseWorldMatrix().map(theEvent->pos());
323   QPoint g = theEvent->globalPos();
324       
325   if (myTimer->isActive()) myTimer->stop();
326
327   if (myIsLinkCreating && myLinkBuilder) {
328     myLinkBuilder->setFloatPoint(p);
329     canvas()->update();
330   }
331    
332   if (myCurrentItem) {
333     //    setHilighted(0);
334     if (myCurrentItem->x() && myCurrentItem->y()) {
335       double cx = myCurrentItem->x() - myPoint.x();
336       double cy = myCurrentItem->y() - myPoint.y();
337       
338       if (p.x()+cx < 0) p.setX(-(int)cx);
339       if (p.y()+cy < 0) p.setY(-(int)cy);
340     }
341     myCurrentItem->moveBy(p.x() - myPoint.x(), 
342                           p.y() - myPoint.y());
343     myPoint = p;
344     canvas()->update();
345         
346     // scroll contents if mouse is outside
347     QRect r(contentsX(), contentsY(), visibleWidth(), visibleHeight());
348     if (!r.contains(theEvent->pos())) {
349       int dx = 0, dy = 0;
350       if (theEvent->pos().x() < r.left()) dx = theEvent->pos().x() - r.left();
351       if (theEvent->pos().x() > r.right()) dx = theEvent->pos().x() - r.right();
352       if (theEvent->pos().y() < r.top()) dy = theEvent->pos().y() - r.top();
353       if (theEvent->pos().y() > r.bottom()) dy = theEvent->pos().y() - r.bottom();
354       scrollBy(dx, dy);
355       // start timer to scroll in silent mode
356       myDX = dx; myDY = dy;
357       myTimer->start(100);
358     }
359     busy = false;
360     return;
361   }
362
363   if (myIsPanActivated) {
364     setHilighted(0);
365     scrollBy(myGlobalPoint.x() - g.x(),
366              myGlobalPoint.y() - g.y());
367     myGlobalPoint = g;
368     busy = false;
369     return;
370   }
371       
372   if (myIsZoomActivated) {
373     setHilighted(0);
374     double dx = g.x() - myGlobalPoint.x();
375     double s = 1. + fabs(dx)/10.;
376     if (dx < 0) s = 1./s;
377     
378     QWMatrix m = worldMatrix();
379     m.scale(s, s);
380     setWorldMatrix(m);
381         
382     myGlobalPoint = g;
383     busy = false;
384     return;
385   }
386
387   //mkr: "Fit within rectangle" functionality
388   if (myIsFitWRActivated) {
389     int aLX, aTY; //left x and top y
390     if (myPoint.x() < p.x()) aLX = myPoint.x();
391     else aLX = p.x();
392     if (myPoint.y() < p.y()) aTY = myPoint.y();
393     else aTY = p.y();
394     QRect aRect(aLX, aTY, abs(myPoint.x()-p.x()), abs(myPoint.y()-p.y()));
395     QCanvasRectangle* aRect1 = new QCanvasRectangle(aRect, canvas());
396
397     //hide old selected rectangle
398     if (mySelectedRect)
399       mySelectedRect->hide();
400     //draw new selected rectangle
401     QPen pen(Qt::SolidLine);
402     pen.setWidth(1);
403     aRect1->setPen(pen);
404     aRect1->setZ(3);
405     aRect1->show();
406
407     mySelectedRect = aRect1;
408     canvas()->update();
409   }
410   
411   if (!myIsLinkCreating && myMain->isEditable() &&
412       !myMain->getCanvas()->isControlView()) {
413     // compute collision rectangle
414     QRect aSel(p.x()-MARGIN, p.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
415     QCanvasItemList l = canvas()->collisions(aSel);
416     for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
417       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
418         SUPERVGUI_CanvasPointPrs* aPrs = (SUPERVGUI_CanvasPointPrs*) (*it);
419         setHilighted(aPrs->getLink());
420         busy = false;
421         return;
422       }
423       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_LinkEdge) {
424         SUPERVGUI_CanvasEdgePrs* aPrs = (SUPERVGUI_CanvasEdgePrs*) (*it);
425         setHilighted(aPrs->getLink());
426         busy = false;
427         return;
428       }
429     }
430     setHilighted(0);
431   }
432
433   busy = false;
434 }
435
436 void SUPERVGUI_CanvasView::contentsMouseReleaseEvent(QMouseEvent* theEvent) 
437 {
438   if (myTimer->isActive()) myTimer->stop();
439
440   if (myCurrentItem) {
441     if (myCurrentItem->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
442       ((SUPERVGUI_CanvasNodePrs*)myCurrentItem)->setZ(0);
443       ((SUPERVGUI_CanvasNodePrs*)myCurrentItem)->setMoving(false);
444       canvas()->update();
445     }
446     if (myCurrentItem->rtti() == SUPERVGUI_Canvas::Rtti_LinkPoint) {
447       ((SUPERVGUI_CanvasPointPrs*)myCurrentItem)->setMoving(false);
448     }
449   }
450   myCurrentItem = 0;
451
452   if (myIsPanActivated) {
453     myIsPanActivated = false;
454     myIsPanBtnClicked = false;
455     setCursor(myCursor);
456   }
457
458   if (myIsZoomActivated) {
459     myIsZoomActivated = false;
460     setCursor(myCursor);
461   }
462
463   //mkr: "Fit within rectangle" functionality
464   if (myIsFitWRActivated) {
465     if (mySelectedRect) {
466       mySelectedRect->hide();
467       mySelectedRect = 0;
468       canvas()->update();
469     }
470
471     //myPoint is the start point for selecting rectangle now
472     QPoint anEndPoint = inverseWorldMatrix().map(theEvent->pos());
473     int aLX, aTY; //left x and top y
474     if (myPoint.x() < anEndPoint.x()) aLX = myPoint.x();
475     else aLX = anEndPoint.x();
476     if (myPoint.y() < anEndPoint.y()) aTY = myPoint.y();
477     else aTY = anEndPoint.y();
478
479     //calculate width and height for new view and new zoom factor
480     double aXzoom = ((double)visibleWidth())/((double)(abs(myPoint.x()-anEndPoint.x())));
481     double aYzoom = ((double)visibleHeight())/((double)(abs(myPoint.y()-anEndPoint.y())));
482     if (aXzoom > aYzoom) aXzoom = aYzoom;
483     
484     QWMatrix m;
485     m.scale(aXzoom, aXzoom);
486     setWorldMatrix(m);
487     setContentsPos((int)(aLX*aXzoom), (int)(aTY*aYzoom));
488
489     canvas()->update();
490     
491     myIsFitWRActivated = false;
492     viewport()->setMouseTracking(true);
493     setCursor(myCursor);
494   }
495 }
496
497 void SUPERVGUI_CanvasView::contentsMouseDoubleClickEvent(QMouseEvent* theEvent)
498 {
499   QPoint p = inverseWorldMatrix().map(theEvent->pos());
500
501   // compute collision rectangle
502   //QRect aSel(p.x()-MARGIN, p.y()-MARGIN, 1+2*MARGIN, 1+2*MARGIN);
503
504   if (theEvent->button() == Qt::LeftButton) {
505     QCanvasItemList l = canvas()->collisions(p);
506     for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
507       if ((*it)->rtti() == SUPERVGUI_Canvas::Rtti_Node) {
508         SUPERVGUI_CanvasNodePrs* aNodePrs = (SUPERVGUI_CanvasNodePrs*) (*it);
509         SUPERVGUI_CanvasNode* aNode = aNodePrs->getNode();
510         if (aNode->getEngine()->IsMacro() && !myIsLinkCreating) {
511           myMain->openSubGraph(aNode->getEngine(), true);
512           return;
513         }
514       }
515     }
516   }
517 }
518
519 void SUPERVGUI_CanvasView::onDestroyed(QObject* theObject)
520 {
521   if (theObject == myHilighted)
522     myHilighted = 0;
523 }
524
525 void SUPERVGUI_CanvasView::setHilighted(SUPERVGUI_CanvasLink* theLink)
526 {
527   if (theLink == myHilighted) return;
528   if (myHilighted) {
529     disconnect(myHilighted, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
530     myHilighted->setHilighted(false);
531     myHilighted = 0;
532   }
533   if (theLink) {
534     myHilighted = theLink;
535     myHilighted->setHilighted(true);
536     connect(myHilighted, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
537   }
538 }
539
540 void SUPERVGUI_CanvasView::onTimeout() 
541 {
542   if (myCurrentItem) {
543     scrollBy(myDX, myDY);
544
545     double cx, cy;
546     inverseWorldMatrix().map((double)myDX, (double)myDY, &cx, &cy);
547     if (myCurrentItem->x()+cx < 0) cx = -myCurrentItem->x();
548     if (myCurrentItem->y()+cy < 0) cy = -myCurrentItem->y();
549     myCurrentItem->moveBy(cx, cy);
550     myPoint.setX(myPoint.x()+(int)cx);
551     myPoint.setY(myPoint.y()+(int)cy);
552     canvas()->update();
553   }
554 }
555
556 void SUPERVGUI_CanvasView::changeBackground()
557 {
558   QColor aColor = QColorDialog::getColor(canvas()->backgroundColor(), this );
559   if ( aColor.isValid() ) {
560     // change background color for canvas view
561     canvas()->setBackgroundColor(aColor);
562     setPaletteBackgroundColor(aColor.light(120));
563     // change background color for array view
564     getMain()->getArrayView()->canvas()->setBackgroundColor(aColor);
565     getMain()->getArrayView()->setPaletteBackgroundColor(aColor.light(120));
566   }
567 }
568
569 void SUPERVGUI_CanvasView::ActivatePanning()
570 {
571   myIsPanBtnClicked = true;
572 }
573
574 void SUPERVGUI_CanvasView::ResetView()
575 {
576   setContentsPos(0,0);
577   QWMatrix m;
578   setWorldMatrix(m);
579 }
580
581 void SUPERVGUI_CanvasView::startSketch(SUPERVGUI_CanvasPort* thePort)
582 {
583   if (myIsLinkCreating) return;
584
585   myIsLinkCreating = true;
586   myLinkBuilder = new SUPERVGUI_CanvasLinkBuilder(canvas(), myMain, thePort);
587   mySketchPopup->setItemEnabled(myDelPntItem, false);
588 }
589
590 void SUPERVGUI_CanvasView::endSketch(SUPERVGUI_CanvasPort* thePort)
591 {
592   if (!myIsLinkCreating) return;
593
594   if (myLinkBuilder && myLinkBuilder->canCreateEngine(thePort)) {
595     bool input = thePort->getEngine()->IsInput();
596     SUPERV_Link aLinkEngine;
597     if (thePort->getEngine()->Kind() == SUPERV::DataStreamParameter) {
598       SUPERVGUI_CanvasStreamPortIn* aInPort = (SUPERVGUI_CanvasStreamPortIn*) 
599         (input ? thePort : myLinkBuilder->getStartPort());
600       SUPERVGUI_CanvasStreamPortOut* aOutPort = (SUPERVGUI_CanvasStreamPortOut*) 
601         (input ? myLinkBuilder->getStartPort() : thePort);
602 //       aLinkEngine = myMain->getDataflow()->StreamLink(aOutPort->getStreamEngine(), 
603 //                                                    aInPort->getStreamEngine());
604       if (myMain->getDataflow()->IsStreamGraph()) {
605         SUPERV_StreamGraph aSGraph = myMain->getDataflow()->ToStreamGraph();
606         if (!SUPERV_isNull(aSGraph))
607           aLinkEngine = aSGraph->StreamLink(aOutPort->getStreamEngine(), 
608                                             aInPort->getStreamEngine());
609       }
610     }
611     else {
612       SUPERVGUI_CanvasPort* aInPort = (input ? thePort : myLinkBuilder->getStartPort());
613       SUPERVGUI_CanvasPort* aOutPort = (input ? myLinkBuilder->getStartPort() : thePort);
614       aLinkEngine = myMain->getDataflow()->Link(aOutPort->getEngine(), aInPort->getEngine());
615     }
616     if (SUPERV_isNull(aLinkEngine)) 
617       return;    
618
619     // here, in fact, aLinkEngine may NOT be a newly created link.  If a link already existed between the
620     // the 2 given ports - it will be return (NOT created again).
621     // this should be checked and new presentation should NOT be created for existing link.
622     // Solution 1: NOT to allow creation of a link if it already exists between the ports in 
623     //             myLinkBuilder->canCreateEngine()
624     // Solution 2: check here if aLinkEngine is "new" or "old"
625     // Implement 2nd solution, because canCreateEngine() checks for types of ports, etc.. - it's another thing
626     // THE CHECK:
627     QObjectList* canvasLinks = canvas()->queryList("SUPERVGUI_CanvasLink");
628     SUPERVGUI_CanvasLink* canvasLink = 0;
629     QObjectListIt it(*canvasLinks);
630     bool prsAlreadyExists = false;
631     while ( (canvasLink=(SUPERVGUI_CanvasLink*)it.current()) ) {
632       ++it;
633       SUPERV_Link existingLinkWithPrs = canvasLink->getEngine();
634       if ( !SUPERV_isNull( existingLinkWithPrs ) ) {
635         if ( existingLinkWithPrs->IsEqual( aLinkEngine ) ) {
636           prsAlreadyExists = true;
637           break;
638         }       
639       }
640     }
641     delete canvasLinks;
642     if ( prsAlreadyExists ) { // aLinkEngine is already bound with a SUPERVGUI_CanvasLink object
643       //return;  // -> if return here, than the old CanvasLink is kept
644
645       // we want to delete old and create a new CanvasLink for this Link
646       delete canvasLink;
647       canvasLink = 0;
648       // clear old corrdinates in Engine link
649       for ( int i = 1; i <= aLinkEngine->CoordsSize(); i++ )
650         aLinkEngine->RemoveCoord( i );
651       // now we are ready to set coords for a link and create a new CanvasLink presentation that will use them.
652     }
653
654     myLinkBuilder->setCoords(aLinkEngine.in());
655
656     delete myLinkBuilder;
657     myLinkBuilder = 0;
658
659     SUPERVGUI_CanvasLink* aLink = new SUPERVGUI_CanvasLink(canvas(), myMain, aLinkEngine);
660     aLink->show();
661
662     canvas()->update();
663     myIsLinkCreating = false;
664
665     // asv : 13.12.04 : introducing a check for ports' types compatibility (Bugs and Improvements p.1.16, PAL7380)
666     if ( !aLinkEngine->IsValid() ) { 
667       const int id = SUIT_MessageBox::warn2( this, tr( "TLT_INVALID_LINK" ), 
668                                              tr( "MSG_INVALID_LINK" ) + QString(" : ") + QString(myMain->getDataflow()->Messages()), 
669                                              tr( "Keep" ), tr( "Remove" ), 0, 1, 0 );
670       if ( id == 1 ) { // "Remove" was selected in Message Box
671         aLink->remove(); // the new link did not live long...
672       }
673     }
674   }
675 }
676
677 void SUPERVGUI_CanvasView::cancelSketch()
678 {
679   if (myLinkBuilder) {
680     delete myLinkBuilder;
681     myLinkBuilder = 0;
682     canvas()->update();
683   }
684   myIsLinkCreating = false;
685 }
686
687 void SUPERVGUI_CanvasView::deletePoint()
688 {
689   if (myIsLinkCreating && myLinkBuilder) {
690     myLinkBuilder->removeLastPoint();
691     canvas()->update();
692     mySketchPopup->setItemEnabled(myDelPntItem, myLinkBuilder->getPointCount());
693   }
694 }
695
696 void SUPERVGUI_CanvasView::setOrthoMode()
697 {
698   bool aIsOrtho = !mySketchPopup->isItemChecked(myOrtoItem);
699   mySketchPopup->setItemChecked(myOrtoItem, aIsOrtho);
700 }
701
702 void SUPERVGUI_CanvasView::zoomIn() 
703 {
704   QWMatrix m;
705   m.scale(2.0, 2.0);
706   setWorldMatrix(m);
707   canvas()->update();
708 }
709
710 void SUPERVGUI_CanvasView::zoomReset() 
711 {
712   QWMatrix m;
713   m.scale(1.0, 1.0);
714   setWorldMatrix(m);
715   canvas()->update();
716 }
717
718 void SUPERVGUI_CanvasView::zoomOut() 
719 {
720   QWMatrix m;
721   m.scale(0.5, 0.5);
722   setWorldMatrix(m);
723   canvas()->update();
724 }
725
726 void SUPERVGUI_CanvasView::fitAll() 
727 {
728   int w = 0, h = 0;
729   QCanvasItemList l = canvas()->allItems();
730   for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) {
731     QRect r = (*it)->boundingRect();
732     if (w < r.right()) w = r.right();
733     if (h < r.bottom()) h = r.bottom();
734   }
735   w += GRAPH_MARGIN; h += GRAPH_MARGIN;
736   double s = ((double)visibleWidth())/((double)w);
737   double s1 = ((double)visibleHeight())/((double)h);
738   if (s > s1) s = s1;
739
740   setContentsPos(0,0);
741   QWMatrix m;
742   m.scale(s, s);
743   setWorldMatrix(m);
744   canvas()->update();
745 }
746
747 void SUPERVGUI_CanvasView::fitWithinRect() 
748 {
749   //mkr: "Fit within rectangle" functionality
750   myIsFitWRActivated = true;
751   viewport()->setMouseTracking(false);
752   myCursor = cursor();
753   setCursor(handCursor);
754 }
755
756 bool SUPERVGUI_CanvasView::isHavingStreamPort() const
757 {
758   SUPERV::ListOfNodes* aNodesList = myMain->getDataflow()->Nodes();
759
760   //Computing Nodes
761   for ( int i = 0 ; i < (int) aNodesList->CNodes.length() ; i++ ) {
762     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->CNodes)[i]->StreamPorts() );
763     if ((int) aStrPortsList.length() > 0) {
764       return true;
765     }
766   }
767    
768   //FactoryNodes
769   for ( int i = 0 ; i < (int) aNodesList->FNodes.length() ; i++ ) {
770     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->FNodes)[i]->StreamPorts() );
771     if ((int) aStrPortsList.length() > 0) {
772       return true;
773     }
774   }
775         
776   //InLineNodes
777   for ( int i = 0 ; i < (int) aNodesList->INodes.length() ; i++ ) {
778     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->INodes)[i]->StreamPorts() );
779     if ((int) aStrPortsList.length() > 0) {
780       return true;
781     }
782   }
783         
784   //GOTONodes
785   for ( int i = 0 ; i < (int) aNodesList->GNodes.length() ; i++ ) {
786     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->GNodes)[i]->StreamPorts() );
787     if ((int) aStrPortsList.length() > 0) {
788       return true;
789     }
790   }
791         
792   //LoopNodes
793   for ( int i = 0 ; i < (int) aNodesList->LNodes.length() ; i++ ) {
794     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->LNodes)[i]->StreamPorts() );
795     if ((int) aStrPortsList.length() > 0) {
796      return true;
797     }
798   }
799         
800   //SwitchNodes
801   for ( int i = 0 ; i < (int) aNodesList->SNodes.length() ; i++ ) {
802     SUPERV::ListOfStreamPorts aStrPortsList = *( (aNodesList->SNodes)[i]->StreamPorts() );
803     if ((int) aStrPortsList.length() > 0) {
804       return true;
805     }
806   }
807         
808   return false;
809 }