Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasNodePrs.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SUPERVGUI_GanvasNodePrs.cxx
23 //  Author : Natalia KOPNOVA
24 //  Module : SUPERV
25
26 #include "SUPERVGUI_CanvasNodePrs.h"
27 #include "SUPERVGUI_CanvasNode.h"
28 #include "SUPERVGUI_CanvasPort.h"
29 #include "SUPERVGUI_Canvas.h"
30 #include "SUPERVGUI_CanvasCellNodePrs.h"
31 #include "SUPERVGUI_Main.h"
32
33 #include "SUIT_ResourceMgr.h"
34
35 //#define CHECKTIME
36
37 #ifdef CHECKTIME
38 #include <sys/timeb.h>
39 #endif
40
41 //#define PORT_MARGIN 2
42 #undef PORT_HEIGHT // to avoid warning message
43 #define PORT_HEIGHT LABEL_HEIGHT
44 #define TEXT_MARGIN 5
45
46
47 //=====================================================================
48 // Node presentation
49 //=====================================================================
50 SUPERVGUI_CanvasNodePrs::SUPERVGUI_CanvasNodePrs( SUIT_ResourceMgr* mgr, QCanvas* theCanvas, 
51                                                   SUPERVGUI_CanvasNode* theNode,
52                                                   bool theCellPrs):
53   QCanvasPolygonalItem(theCanvas),
54   myNode(theNode),
55   myMgr( mgr )
56 {
57   Trace("SUPERVGUI_CanvasNodePrs::SUPERVGUI_CanvasNodePrs");
58   myWidth = LABEL_WIDTH;
59   if (2*(PORT_WIDTH+PORT_MARGIN) > myWidth)
60     myWidth = 2*(PORT_WIDTH+PORT_MARGIN);
61
62   myTitleHeight = LABEL_HEIGHT;
63   myLabelHeight = LABEL_HEIGHT;
64   myStatusHeight = LABEL_HEIGHT;
65   myPortHeight = 2*PORT_MARGIN;
66   myStreamHeight = 0;
67   myGateHeight = PORT_HEIGHT + 2*PORT_MARGIN;
68
69   myLabelVisible = true;
70   myPortVisible = true;
71   myCellPrs = theCellPrs;
72
73   myColor = mgr->colorValue( "SUPERVGraph", "NodeBody", DEF_MAIN_BACK );
74
75   if (!myCellPrs) {
76     // create in/out connection points prs
77     myPointIn = new SUPERVGUI_CanvasHookPrs(theCanvas, this, true);
78     myPointOut = new SUPERVGUI_CanvasHookPrs(theCanvas, this, false);
79
80     setZ(0);
81     setState(myNode->getEngine()->State());
82     updatePorts();
83   }
84 }
85
86
87 SUPERVGUI_CanvasNodePrs::~SUPERVGUI_CanvasNodePrs() 
88 {
89   if ( !myCellPrs ) {
90     if ( myPointIn ) {
91       delete myPointIn;
92       myPointIn = 0;
93     }
94     if ( myPointOut ) {
95       delete myPointOut;
96       myPointOut = 0;
97     }
98   }
99 }
100
101 int SUPERVGUI_CanvasNodePrs::rtti() const
102 {
103   return SUPERVGUI_Canvas::Rtti_Node;
104 }
105
106 int SUPERVGUI_CanvasNodePrs::width() const
107 {
108   return myWidth;
109 }
110
111 int SUPERVGUI_CanvasNodePrs::height() const
112 {
113   return getTitleHeight() + getLabelHeight() + getStatusHeight() +
114     getBodyHeight() + getGateHeight();
115 }
116
117 int SUPERVGUI_CanvasNodePrs::getTitleHeight() const
118 {
119   return myTitleHeight;
120 }
121
122 int SUPERVGUI_CanvasNodePrs::getLabelHeight() const
123 {
124   if (isLabelVisible())
125     return myLabelHeight;
126   return 0;
127 }
128
129 int SUPERVGUI_CanvasNodePrs::getStatusHeight() const
130 {
131   return myStatusHeight;
132 }
133
134 int SUPERVGUI_CanvasNodePrs::getBodyHeight() const
135 {
136   if (isPortVisible())
137     return myPortHeight + myStreamHeight;
138   return 0;
139 }
140
141 int SUPERVGUI_CanvasNodePrs::getPortHeight() const
142 {
143   if (isPortVisible())
144     return myPortHeight;
145   return 0;
146 }
147
148 int SUPERVGUI_CanvasNodePrs::getStreamHeight() const
149 {
150   if (isPortVisible())
151     return myStreamHeight;
152   return 0;
153 }
154
155 int SUPERVGUI_CanvasNodePrs::getGateHeight() const
156 {
157   if (isPortVisible())
158     return myGateHeight;
159   return 0;
160 }
161
162 QPointArray SUPERVGUI_CanvasNodePrs::areaPoints() const
163 {
164   int w = width();
165   int h = height()+1;
166
167   QPointArray aPnts(4);
168   aPnts[0] = QPoint((int)x(), (int)y());
169   aPnts[1] = aPnts[0] + QPoint(w, 0);
170   aPnts[2] = aPnts[1] + QPoint(0, h);
171   aPnts[3] = aPnts[0] + QPoint(0, h);
172   return aPnts;
173 }
174
175 QObject* SUPERVGUI_CanvasNodePrs::getObject(const QPoint& thePos) const
176 {
177   QObject* anObj = myNode;
178
179   // check if it's a port
180   const QObjectList* list = myNode->children();
181   if (list) {
182     QObjectListIt it(*list);
183     SUPERVGUI_CanvasPort* aPort;
184     while (QObject* obj = it.current()) {
185       ++it;
186       if (obj->inherits("SUPERVGUI_CanvasPort")) {
187         aPort = (SUPERVGUI_CanvasPort*) obj;
188         if (aPort->getPrs()->getPortRect().contains(thePos, true)) {
189           anObj = aPort;
190           break;
191         }
192       }
193     }
194   }
195   return anObj;
196 }
197
198 void SUPERVGUI_CanvasNodePrs::moveBy(double dx, double dy) 
199 {
200   int aX = (int) (x()+dx);
201   int aY = (int) (y()+dy);
202   int xx = aX - (int)x();
203   int yy = aY - (int)y();
204
205   int w = aX + width() + GRAPH_MARGIN;
206   int h = aY + height() + GRAPH_MARGIN;
207   if (canvas()->width() > w) w = canvas()->width();
208   if (canvas()->height() > h) h = canvas()->height();
209   if (canvas()->width() < w || canvas()->height() < h)
210     canvas()->resize(w, h);
211
212   // save new coordinates only if node is really moving...
213   if (isMoving()) {
214     myNode->getEngine()->Coords(aX, aY);
215   }
216
217   QCanvasPolygonalItem::moveBy(dx, dy);
218
219   // update port's rectangle
220   const QObjectList* list = myNode->children();
221   if (list) {
222     QObjectListIt it(*list);
223     SUPERVGUI_CanvasPort* aPort;
224     while (QObject* obj = it.current()) {
225       ++it;
226       if (obj->inherits("SUPERVGUI_CanvasPort")) {
227         aPort = (SUPERVGUI_CanvasPort*) obj;
228         aPort->moveBy(xx, yy);
229       }
230     }
231   }
232
233   if (!myCellPrs) {
234     myPointIn->moveBy(dx, dy);
235     myPointOut->moveBy(dx, dy);
236   }
237 }
238
239 void SUPERVGUI_CanvasNodePrs::setZ(double z)
240 {
241   QCanvasItem::setZ(z);
242
243   // update port's 
244   const QObjectList* list = myNode->children();
245   if (list) {
246     QObjectListIt it(*list);
247     SUPERVGUI_CanvasPort* aPort;
248     while (QObject* obj = it.current()) {
249       ++it;
250       if (obj->inherits("SUPERVGUI_CanvasPort")) {
251         aPort = (SUPERVGUI_CanvasPort*) obj;
252         aPort->getPrs()->setZ(z);
253       }
254     }
255   }
256 }
257
258 void SUPERVGUI_CanvasNodePrs::hideAll()
259 {
260   bool aDisp = isVisible();
261   if (aDisp) hide();
262
263   setLabelVisible(false);
264   setPortVisible(false);
265
266   if (aDisp) {
267     show();
268     //    canvas()->update();
269   }
270 }
271
272 void SUPERVGUI_CanvasNodePrs::showAll()
273 {
274   bool aDisp = isVisible();
275   if (aDisp) hide();
276
277   setLabelVisible(true);
278   setPortVisible(true);
279
280   if (aDisp) {
281     show();
282     //    canvas()->update();
283   }
284 }
285
286 void SUPERVGUI_CanvasNodePrs::setLabelVisible(bool b)
287 {
288   bool aDisp = isVisible();
289   if (aDisp) hide();
290
291   myLabelVisible = b;
292   updatePorts();
293   if (!isPortVisible() && !myCellPrs) updatePoints();
294
295   if (aDisp) {
296     show();
297     canvas()->update();
298   }
299 }
300
301 void SUPERVGUI_CanvasNodePrs::setPortVisible(bool b)
302 {
303   bool aDisp = isVisible();
304   if (aDisp) hide();
305
306   myPortVisible = b;
307   if (b) {
308     if (!myCellPrs) {
309       myPointIn->hide();
310       myPointOut->hide();
311     }
312
313     updateGates();
314   }
315   else {
316     if (!myCellPrs) {
317       updatePoints();
318       
319       myPointIn->show();
320       myPointOut->show();
321     }
322   }
323
324   const QObjectList* list = myNode->children();
325   if (list) {
326     QObjectListIt it(*list);
327     SUPERVGUI_CanvasPort* aPort;
328     while (QObject* obj = it.current()) {
329       ++it;
330       if (obj->inherits("SUPERVGUI_CanvasPort")) {
331         aPort = (SUPERVGUI_CanvasPort*) obj;
332         aPort->getPrs()->setVisible(b);
333       }
334     }
335   }
336
337   if (aDisp) {
338     show();
339     canvas()->update();
340   }
341 }
342
343 void SUPERVGUI_CanvasNodePrs::setNodeColor(const QColor& theColor)
344 {
345   myColor = theColor;
346   canvas()->setChanged(getRect());
347   canvas()->update();
348 }
349
350 void SUPERVGUI_CanvasNodePrs::updateInfo()
351 {
352   canvas()->setChanged(getTitleRect());
353   canvas()->setChanged(getLabelRect()); //node's comment is saved in engine (we can see it only 
354                                         //with help of ChangeInformation dialog),
355                                         //node's label isn't change
356   canvas()->update();
357 }
358
359 void SUPERVGUI_CanvasNodePrs::updatePoints()
360 {
361   QPoint in = getInConnectionPoint();
362   myPointIn->setCoords(in.x(), in.y());
363
364   QPoint out = getOutConnectionPoint();
365   myPointOut->setCoords(out.x(), out.y());
366 }
367
368 void SUPERVGUI_CanvasNodePrs::updatePorts() 
369 {
370   bool aDisp = isVisible();
371   if (aDisp) hide();
372
373   QRect r = getBodyRect();
374   int w = (int)(r.width()/2) - PORT_MARGIN; 
375   if (w < PORT_WIDTH) w = PORT_WIDTH;
376
377   int ix = r.x() + PORT_MARGIN;
378   int ih = r.y() + PORT_MARGIN;
379   int ox = ix + w;
380   int oh = r.y() + PORT_MARGIN;
381
382   const QObjectList* list = myNode->children();
383   if (list) {
384     QObjectListIt it(*list);
385     SUPERVGUI_CanvasPort* aPort;
386     while (QObject* obj = it.current()) {
387       ++it;
388       if (obj->inherits("SUPERVGUI_CanvasPort")) {
389         aPort = (SUPERVGUI_CanvasPort*) obj;
390         if (!aPort->getEngine()->IsGate() && !aPort->isStream()) {
391           if (aPort->getEngine()->IsInput()) {
392             aPort->getPrs()->setPortRect(QRect(ix, ih, w, PORT_HEIGHT));
393             ih += PORT_HEIGHT;
394           }
395           else {
396             aPort->getPrs()->setPortRect(QRect(ox, oh, w, PORT_HEIGHT));
397             oh += PORT_HEIGHT;
398           }
399         }
400       }
401     }
402   }
403
404   myPortHeight = (ih>oh?ih:oh) - r.y() + PORT_MARGIN;
405
406   // update stream ports
407   int sy = r.y() + myPortHeight;
408   ih = sy + PORT_MARGIN;
409   oh = sy + PORT_MARGIN;
410
411   if (list) {
412     QObjectListIt it(*list);
413     SUPERVGUI_CanvasPort* aPort;
414     while (QObject* obj = it.current()) {
415       ++it;
416       if (obj->inherits("SUPERVGUI_CanvasPort")) {
417         aPort = (SUPERVGUI_CanvasPort*) obj;
418         if (!aPort->getEngine()->IsGate() && aPort->isStream()) {
419           if (aPort->getEngine()->IsInput()) {
420             aPort->getPrs()->setPortRect(QRect(ix, ih, w, PORT_HEIGHT));
421             ih += PORT_HEIGHT;
422           }
423           else {
424             aPort->getPrs()->setPortRect(QRect(ox, oh, w, PORT_HEIGHT));
425             oh += PORT_HEIGHT;
426           }
427         }
428       }
429     }
430   }
431
432   myStreamHeight = (ih>oh?ih:oh) - sy + PORT_MARGIN;
433   if (myStreamHeight == 2*PORT_MARGIN) myStreamHeight = 0;
434
435   // can update gates only after body height will be defined
436   updateGates();
437
438   if (aDisp) {
439     show();
440     canvas()->update();
441   }
442 }
443
444 void SUPERVGUI_CanvasNodePrs::updateGates() 
445 {
446   bool aDisp = isVisible();
447   if (aDisp) hide();
448
449   QRect r = getGateRect();
450   int w = (int)(r.width()/2) - PORT_MARGIN; 
451   if (w < PORT_WIDTH) w = PORT_WIDTH;
452
453   int ix = r.x() + PORT_MARGIN;
454   int ih = r.y() + PORT_MARGIN;
455   int ox = ix + w;
456   int oh = r.y() + PORT_MARGIN;
457
458   const QObjectList* list = myNode->children();
459   if (list) {
460     QObjectListIt it(*list);
461     SUPERVGUI_CanvasPort* aPort;
462     while (QObject* obj = it.current()) {
463       ++it;
464       if (obj->inherits("SUPERVGUI_CanvasPort")) {
465         aPort = (SUPERVGUI_CanvasPort*) obj;
466         if (aPort->getEngine()->IsGate()) {
467           if (aPort->getEngine()->IsInput()) {
468             aPort->getPrs()->setPortRect(QRect(ix, ih, w, PORT_HEIGHT));
469             ih += PORT_HEIGHT;
470           }
471           else {
472             aPort->getPrs()->setPortRect(QRect(ox, oh, w, PORT_HEIGHT));
473             oh += PORT_HEIGHT;
474           }
475         }
476       }
477     }
478   }
479   
480   myGateHeight = (ih>oh?ih:oh) - r.y() + PORT_MARGIN;
481
482   if (aDisp) show();
483 }
484
485 QRect SUPERVGUI_CanvasNodePrs::getRect() const
486 {
487   return QRect((int)x(), (int)y(), width(), height());
488 }
489
490 QRect SUPERVGUI_CanvasNodePrs::getTitleRect() const
491 {
492   return QRect((int)x(), (int)y(), width(), getTitleHeight());
493 }
494
495 QRect SUPERVGUI_CanvasNodePrs::getLabelRect() const
496 {
497   return QRect((int)x(), ((int)y())+getTitleHeight(), width(), getLabelHeight());
498 }
499
500 QRect SUPERVGUI_CanvasNodePrs::getStatusRect() const
501 {
502   return QRect((int)x(), ((int)y())+getTitleHeight()+getLabelHeight(),
503                width(), getStatusHeight());
504 }
505
506 QRect SUPERVGUI_CanvasNodePrs::getBodyRect() const
507 {
508   return QRect((int)x(), ((int)y())+getTitleHeight()+getLabelHeight()+getStatusHeight(), 
509                width(), getBodyHeight());
510 }
511
512 QRect SUPERVGUI_CanvasNodePrs::getGateRect() const
513 {
514   return QRect((int)x(), ((int)y())+getTitleHeight()+getLabelHeight()+
515                getStatusHeight()+getBodyHeight(), 
516                width(), getGateHeight());
517 }
518
519 QPoint SUPERVGUI_CanvasNodePrs::getInConnectionPoint() const
520 {
521   QRect r = getGateRect();
522   int h;
523   if (isPortVisible()) {
524     h = (int)((r.top()+r.bottom())/2);
525   }
526   else {
527     h = (int)(y()+height()/2);
528   }
529   return QPoint(r.left()-POINT_SIZE, h);
530 }
531
532 QPoint SUPERVGUI_CanvasNodePrs::getOutConnectionPoint() const
533 {
534   QRect r = getGateRect();
535   int h;
536   if (isPortVisible()) {
537     h = (int)((r.top()+r.bottom())/2);
538   }
539   else {
540     h = (int)y() + (int)(height()/2);
541   }
542   return QPoint(r.right()+POINT_SIZE, h);
543 }
544
545 void drawText(QPainter& thePainter, const QString& theText, 
546               const QRect& theRect, int theHAlign = Qt::AlignAuto)
547 {
548   int flags = theHAlign | Qt::AlignVCenter;
549   QRect r(theRect.x() + TEXT_MARGIN, theRect.y(), 
550           theRect.width() - 2*TEXT_MARGIN, theRect.height());
551
552   QWMatrix aMat = thePainter.worldMatrix();
553   if (aMat.m11() != 1.0) { 
554     // for scaled picture only
555     QRect r1 = aMat.mapRect(r);
556     QFont saved = thePainter.font();
557     QFont f(saved);
558     if (f.pointSize() == -1) {
559       f.setPixelSize((int)(f.pixelSize()*aMat.m11()));
560     }
561     else {
562       f.setPointSize((int)(f.pointSize()*aMat.m11()));
563     }
564     thePainter.save();
565     QWMatrix m;
566     thePainter.setWorldMatrix(m);
567     thePainter.setFont(f);
568     thePainter.drawText(r1, flags, theText);
569     thePainter.setFont(saved);
570     thePainter.restore();
571   }
572   else {
573     thePainter.drawText(r, flags, theText);
574   }
575 }
576
577 void SUPERVGUI_CanvasNodePrs::draw(QPainter& thePainter) 
578 {
579   thePainter.setPen(pen());
580   thePainter.setBrush(nodeColor());
581   if ( !CORBA::is_nil( myNode->getMain()->getDataflow()->Node( myNode->name() ) ) ) // mkr : IPAL11360
582     drawShape(thePainter);
583 }
584
585 void SUPERVGUI_CanvasNodePrs::drawShape(QPainter& thePainter) 
586 {
587   drawTitle(thePainter);
588   if (isLabelVisible()) {
589     drawLabel(thePainter);
590   }
591   if (isPortVisible()) {
592     drawPort(thePainter);
593     drawGate(thePainter);
594   }
595   drawStatus(thePainter);
596   drawFrame(thePainter);
597 }
598
599 void SUPERVGUI_CanvasNodePrs::drawFrame(QPainter& thePainter) 
600 {
601   /* it was a good idea, but...
602      drawed polyline is out of item boundaries :-(
603   QPointArray pnts = areaPoints();
604   int n = pnts.size();
605   pnts.resize(n+1);
606   pnts[n] = pnts[0];
607   thePainter.drawPolyline(pnts);
608   */
609
610   QBrush saved = thePainter.brush();
611   thePainter.setBrush(NoBrush);
612   thePainter.drawRect(getRect());
613   thePainter.setBrush(saved);
614 }
615
616 void SUPERVGUI_CanvasNodePrs::drawTitle(QPainter& thePainter) 
617 {
618   QBrush saved = thePainter.brush();
619   QBrush br( myMgr->colorValue( "SUPERVGraph", "Title", DEF_MAIN_TITLE ) );
620   thePainter.setBrush(br);
621   drawTitleShape(thePainter);
622   thePainter.setBrush(saved);
623
624   drawText(thePainter, myNode->getEngine()->Name(), getTitleRect(), Qt::AlignHCenter);
625 }
626
627 void SUPERVGUI_CanvasNodePrs::drawTitleShape(QPainter& thePainter) 
628 {
629   thePainter.drawRect(getTitleRect());
630 }
631
632 void SUPERVGUI_CanvasNodePrs::drawLabel(QPainter& thePainter) 
633 {
634   QRect r = getLabelRect();
635
636   QPen saved = thePainter.pen();
637   thePainter.setPen(NoPen);
638   thePainter.drawRect(r);
639   thePainter.setPen(saved);
640
641   //  drawText(thePainter, myNode->getEngine()->Comment(), r);
642   drawText(thePainter, myNode->getLabelText(), r);
643 }
644
645 void SUPERVGUI_CanvasNodePrs::drawStatus(QPainter& thePainter) 
646 {
647   QRect r = getStatusRect();
648   if (isPortVisible())
649     r.setHeight(r.height()+1);
650
651   QBrush savedB = thePainter.brush();
652   thePainter.setBrush(myStatusColor);
653   drawStatusShape(thePainter);
654   thePainter.setBrush(savedB);
655
656   QRect r1(r.x(), r.y(), (int)r.width()/2, r.height());
657   drawText(thePainter, myStatus, r1, Qt::AlignHCenter);
658
659   QRect r2(r.x()+r.width()-r1.width(), r.y(), r1.width(), r.height());
660   drawText(thePainter, myTime, r2, Qt::AlignHCenter);
661 }
662
663 void SUPERVGUI_CanvasNodePrs::drawStatusShape(QPainter& thePainter) 
664 {
665   QRect r = getStatusRect();
666   if (isPortVisible())
667     r.setHeight(r.height()+1);
668   thePainter.drawRect(r);
669 }
670
671 void SUPERVGUI_CanvasNodePrs::drawPort(QPainter& thePainter) 
672 {
673   QRect r = getBodyRect();
674   r.setHeight(r.height()+1);
675
676   thePainter.drawRect(r);
677   int x0 = (r.left() + r.right())/2;
678   thePainter.drawLine(x0, r.top(), x0, r.bottom());
679   if (getStreamHeight() > 0) {
680     int y0 = r.top() + getPortHeight();
681     thePainter.drawLine(r.left(), y0, r.right(), y0);
682   }
683
684   const QObjectList* list = myNode->children();
685   if (list) {
686     QObjectListIt it(*list);
687     SUPERVGUI_CanvasPort* aPort;
688     while (QObject* obj = it.current()) {
689       ++it;
690       if (obj->inherits("SUPERVGUI_CanvasPort")) {
691         aPort = (SUPERVGUI_CanvasPort*) obj;
692
693         SUPERV_Port aPortEng = aPort->getEngine();
694         //      if ( CORBA::is_nil( aPortEng ) )
695         //        printf ( "---\n port engine IS NIL ---\n\n" );
696
697         if ( !CORBA::is_nil( aPortEng ) && !aPortEng->IsGate() ) {
698           aPort->getPrs()->draw(thePainter);
699         }
700       }
701     }
702   }
703 }
704
705 void SUPERVGUI_CanvasNodePrs::drawGate(QPainter& thePainter) 
706 {
707   QBrush saved = thePainter.brush();
708   thePainter.setBrush(green.light(170));
709
710   QRect r = getGateRect();
711   //  r.setHeight(r.height()+1);
712   thePainter.drawRect(r);
713   //int x0 = (r.left() + r.right())/2;
714   //  thePainter.drawLine(x0, r.top(), x0, r.bottom());
715
716   const QObjectList* list = myNode->children();
717   if (list) {
718     QObjectListIt it(*list);
719     SUPERVGUI_CanvasPort* aPort;
720     while (QObject* obj = it.current()) {
721       ++it;
722       if (obj->inherits("SUPERVGUI_CanvasPort")) {
723         aPort = (SUPERVGUI_CanvasPort*) obj;
724         if (aPort->getEngine()->IsGate()) {
725           aPort->getPrs()->draw(thePainter);
726         }
727       }
728     }
729   }
730
731   thePainter.setBrush(saved);
732 }
733
734 void SUPERVGUI_CanvasNodePrs::setState(SUPERV::GraphState theState)
735 {
736   switch(theState) {
737   case SUPERV_Waiting:
738     myStatus = "Waiting";
739     myStatusColor = QColor(35, 192, 255);
740     break;
741
742   case SUPERV_Running:
743   case SUPERV::ReadyState:
744     myStatus = "Running";
745     myStatusColor = QColor(32,210,32);
746     break;
747
748   case SUPERV_Suspend:
749   case SUPERV::SuspendReadyState:
750     myStatus = "Suspended";
751     myStatusColor = QColor(255,180, 0);
752     break;
753
754   case SUPERV_Done:
755     myStatus = "Finished";
756     myStatusColor = QColor(255, 158, 255);
757     break;
758
759   case SUPERV_Error: 
760     myStatus = "Aborted";
761     myStatusColor = red;
762     break;
763
764   case SUPERV_Kill:
765     myStatus = "Killed";
766     myStatusColor = red;
767     break;
768
769   case SUPERV::LoadingState:
770     myStatus = "Loading";
771     myStatusColor = QColor(56,255,56);
772     break;
773
774   default:
775     myStatus = "No Status";
776     myStatusColor = myMgr->colorValue( "SUPERVGraph", "NodeBody", DEF_MAIN_BACK );
777     break;
778   }
779
780   long sec = 0;
781   // IPAL9273, 9369, 9731
782   if ( theState != SUPERV_Kill && myNode->getMain()->getDataflow()->State() != SUPERV_Kill )
783     sec = myNode->getEngine()->CpuUsed();
784   char hms[9];
785   long s = sec/3600;
786   hms[0]=(char)(((s/10)%10)+48);
787   hms[1]=(char)((s%10)+48);
788   hms[2]=':';
789   sec = sec%3600;
790   s = sec/60;
791   hms[3]=(char)((s/10)+48);
792   hms[4]=(char)((s%10)+48);
793   hms[5]=':';
794   sec = sec%60;
795   hms[6]=(char)((sec/10)+48);
796   hms[7]=(char)((sec%10)+48);
797   hms[8]='\0';
798   myTime = QString(hms);
799
800   canvas()->setChanged(getStatusRect());
801   canvas()->update();
802 }
803
804
805 //=====================================================================
806 // Port presentation
807 //=====================================================================
808 SUPERVGUI_CanvasPortPrs::SUPERVGUI_CanvasPortPrs(QCanvas* theCanvas, 
809                                                  SUPERVGUI_CanvasPort* thePort):
810   myCanvas(theCanvas), myPort(thePort)
811 {
812   myText = getText();
813   myVisible = true;
814   myPoint = new SUPERVGUI_CanvasHookPrs(theCanvas, this, myPort->getEngine()->IsInput());
815 }
816
817 SUPERVGUI_CanvasPortPrs::~SUPERVGUI_CanvasPortPrs()
818 {
819   if (myPoint) delete myPoint;
820 }
821
822 void SUPERVGUI_CanvasPortPrs::setVisible(bool b)
823 {
824   if (myPoint) myPoint->setVisible(b);
825   myVisible = b;
826   myPort->updateLinks();
827 }
828
829 void SUPERVGUI_CanvasPortPrs::setPortRect(const QRect& theRect)
830 {
831   myRect = theRect;
832   QPoint aPnt = getConnectionPoint();
833 //   int dx = 0;
834 //   if (myPort->getEngine()->IsInput()) 
835 //     dx = POINT_SIZE;
836 //   else
837 //     dx = - POINT_SIZE;
838 //   QPoint aPnt2(aPnt.x()+dx, aPnt.y());
839
840 //   if (myLine) {
841 //     myLine->move(0, 0);
842 //     myLine->setPoints(aPnt2.x(), aPnt2.y(), aPnt.x(), aPnt.y());
843 //     if (myVisible) myLine->show();
844 //   }
845   if (myPoint) {
846     myPoint->setCoords(aPnt.x(), aPnt.y());
847     //    myPoint->move(aPnt.x(), aPnt.y());
848     if (myVisible) myPoint->show();
849   }
850   myPort->updateLinks();
851 }
852
853 void SUPERVGUI_CanvasPortPrs::moveBy(int dx, int dy)
854 {
855   myRect.moveBy(dx, dy);
856   if (myPoint) myPoint->moveBy(dx, dy);
857   //  if (myLine) myLine->moveBy(dx, dy);
858 }
859
860 void SUPERVGUI_CanvasPortPrs::setZ(double z)
861 {
862   if (myPoint) myPoint->setZ(z);
863   //  if (myLine) myLine->setZ(z);
864 }
865
866 void SUPERVGUI_CanvasPortPrs::update(bool theForce)
867 {
868   QString aNewText = getText();
869   if (theForce || myText.compare(aNewText) != 0) {
870     myText = aNewText;
871     myCanvas->setChanged(myRect);
872   }
873 }
874
875 bool SUPERVGUI_CanvasPortPrs::isHilight() const
876 {
877   SUPERV_Port aPort = myPort->getEngine();
878   bool b = false;
879   if (!aPort->IsGate()) {
880     if (aPort->IsInput()) {
881       if (aPort->HasInput() && !aPort->IsLinked())
882         b = true;
883     }
884     else if (myPort->inherits("SUPERVGUI_CanvasPortOut")) {
885       SUPERVGUI_CanvasPortOut* aPortOut = (SUPERVGUI_CanvasPortOut*) myPort;
886       if (aPortOut->isInStudy())
887         b = true;
888     }
889   }
890   return b;
891 }
892
893 bool SUPERVGUI_CanvasPortPrs::isAlert() const
894 {
895   bool b = false;
896   SUPERV_Port aPort = myPort->getEngine();
897   if (!aPort->IsGate()) {
898     if (aPort->IsInput() && !aPort->HasInput() && !aPort->IsLinked())
899       b = true;
900   }
901   return b;
902 }
903
904 QString SUPERVGUI_CanvasPortPrs::getText() const
905 {
906   SUPERV_Port aPort = myPort->getEngine();
907   QString aText;
908   if ( !CORBA::is_nil( aPort ) ) {
909     aText = aPort->Name();
910     if (aPort->IsParam() || aPort->IsInLine() || myPort->isStream()) {
911       // mkr : modifications to avoid displaying long IOR's values on nodes' ports,
912       //       display ports type from corresponding XML catalog instead.
913       QString aPortValue = aPort->ToString();
914       QString aPortType = aPort->Type();
915       if ( aPortType.compare("string") // not "string" type
916            &&
917            aPortValue.find("IOR:") >= 0 ) // has IOR:... in value
918         aText = aText + "=" + aPortType;
919       else
920         aText = aText + "=" + aPortValue;
921     }
922   }
923   //  printf( "--- return text of port : %s ---\n", aText.latin1() );
924   return aText;
925 }
926
927 int SUPERVGUI_CanvasPortPrs::getAlignment() const
928 {
929   int a = Qt::AlignAuto;
930   SUPERV_Port aPort = myPort->getEngine();
931   /*  Merge to Ecole_Ete
932   if (QString(aPort->Name()).compare(OUTVOID) == 0)
933     a = Qt::AlignRight;
934   */
935   if (aPort->IsGate()) {
936     if (aPort->IsInput())
937       a = Qt::AlignLeft;
938     else
939       a = Qt::AlignRight;
940   }
941   return a;
942 }
943
944 QPoint SUPERVGUI_CanvasPortPrs::getConnectionPoint() const
945 {
946   int x, y;
947   if (myPort->getEngine()->IsInput())
948     x = myRect.left() - PORT_MARGIN - POINT_SIZE;
949   else
950     x = myRect.right() + PORT_MARGIN + POINT_SIZE;
951   y = (int)(myRect.top() + myRect.bottom())/2;
952   return QPoint(x, y);
953 }
954
955 void SUPERVGUI_CanvasPortPrs::draw(QPainter& thePainter)
956 {
957   /*
958   QPen savedP = thePainter.pen();
959   QBrush savedB = thePainter.brush();
960
961   QPen aPen(savedP.color(), getLineWidth());
962   thePainter.setPen(aPen);
963   thePainter.setBrush(Qt::NoBrush);
964   thePainter.drawRect(myRect);
965   thePainter.setPen(savedP);
966   thePainter.setBrush(savedB);
967   */
968   QFont saved = thePainter.font();
969   QFont f(saved);
970   f.setBold(isHilight());
971   thePainter.setFont(f);
972
973   QPen savedP = thePainter.pen();
974   if (myPort->isStream())
975     thePainter.setPen(QColor(128, 64, 0));// Qt::darkCyan);
976   else if (isHilight())
977     thePainter.setPen(Qt::blue);
978   else if (isAlert())
979     thePainter.setPen(Qt::red.dark(120));
980
981   drawText(thePainter, myText, myRect, getAlignment());
982
983   thePainter.setPen(savedP);
984   thePainter.setFont(saved);
985 }
986
987
988 //=====================================================================
989 // Node presentation
990 //=====================================================================
991 SUPERVGUI_CanvasHookPrs::SUPERVGUI_CanvasHookPrs(QCanvas* theCanvas, 
992                                                  SUPERVGUI_CanvasNodePrs* theNode,
993                                                  const bool& theIn):
994   QCanvasEllipse(POINT_SIZE, POINT_SIZE, theCanvas),
995   myNodePrs(theNode), myPortPrs(0), myIn(theIn), myLine(0)
996 {
997   init(theCanvas);
998 }
999
1000 SUPERVGUI_CanvasHookPrs::SUPERVGUI_CanvasHookPrs(QCanvas* theCanvas, 
1001                                                  SUPERVGUI_CanvasPortPrs* thePort,
1002                                                  const bool& theIn):
1003   QCanvasEllipse(POINT_SIZE, POINT_SIZE, theCanvas),
1004   myNodePrs(0), myPortPrs(thePort), myIn(theIn), myLine(0)
1005 {
1006   init(theCanvas);
1007 }
1008
1009 void SUPERVGUI_CanvasHookPrs::init(QCanvas* theCanvas)
1010 {
1011   myLine = new QCanvasLine(theCanvas);
1012
1013   setBrush(Qt::black);
1014   myLine->setPen(QPen(Qt::black, 1));
1015
1016   setZ(0);
1017 }
1018
1019 SUPERVGUI_CanvasHookPrs::~SUPERVGUI_CanvasHookPrs()
1020 {
1021   hide();
1022   if ( myLine ) {
1023     delete myLine;
1024     myLine = 0;
1025   }
1026 }
1027
1028 QObject* SUPERVGUI_CanvasHookPrs::getObject() const
1029 {
1030   QObject* anObj = 0;
1031   if ( myNodePrs )
1032     anObj = myNodePrs->getNode();
1033   else if ( myPortPrs )
1034     anObj = myPortPrs->getPort();
1035   return anObj;
1036 }
1037
1038 void SUPERVGUI_CanvasHookPrs::setCoords(int x, int y)
1039 {
1040   move(x, y);
1041   if (myLine) {
1042     myLine->move(0, 0);
1043     myLine->setPoints(x+(myIn?POINT_SIZE:-POINT_SIZE), y, x, y);
1044   }
1045 }
1046
1047 void SUPERVGUI_CanvasHookPrs::setVisible(bool b)
1048 {
1049   QCanvasEllipse::setVisible(b);
1050   if (myLine) myLine->setVisible(b);
1051 }
1052
1053 void SUPERVGUI_CanvasHookPrs::moveBy(double dx, double dy)
1054 {
1055   QCanvasEllipse::moveBy(dx, dy);
1056   if (myLine) myLine->moveBy(dx, dy);
1057 }
1058
1059 void SUPERVGUI_CanvasHookPrs::setZ(double z)
1060 {
1061   QCanvasEllipse::setZ(z);
1062   if (myLine) myLine->setZ(z);
1063 }
1064
1065 int SUPERVGUI_CanvasHookPrs::rtti() const
1066 {
1067   return SUPERVGUI_Canvas::Rtti_Hook;
1068 }