Salome HOME
Join modifications from branch BR_3_1_0deb
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasCellNodePrs.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE
4 //
5 //  File   : SUPERVGUI_CanvasCellNodePrs.cxx
6 //  Author : 
7 //  Module : SUPERV
8
9 #include "SUPERVGUI_CanvasCellNodePrs.h"
10 #include "SUPERVGUI_Canvas.h"
11
12 #include "SUIT_ResourceMgr.h"
13
14 #define TEXT_MARGIN 5
15
16 // ----------------------------------------------------------
17 // Cell widget of the table on QCanvas
18 // ----------------------------------------------------------
19 SUPERVGUI_CanvasCellNodePrs::SUPERVGUI_CanvasCellNodePrs( SUIT_ResourceMgr* mgr, QCanvas* theCanvas, SUPERVGUI_CanvasCellNode* theNode):
20   myTitleWidth(CELL_WIDTH_PART),
21   myLabelWidth(CELL_WIDTH_PART),
22   myStatusWidth(CELL_WIDTH_PART), 
23   SUPERVGUI_CanvasNodePrs( mgr, theCanvas, theNode, true)
24 {
25   myPortVisible = false;
26 }
27
28 SUPERVGUI_CanvasCellNodePrs::~SUPERVGUI_CanvasCellNodePrs()
29 {
30 }
31
32 void SUPERVGUI_CanvasCellNodePrs::moveBy(double dx, double dy) {
33 //Trace("SUPERVGUI_CanvasCellNodePrs::moveBy");
34   int aX = (int) (x()+dx);
35   int aY = (int) (y()+dy);
36
37   int w = aX + width() + GRAPH_MARGIN;
38   int h = aY + height() + GRAPH_MARGIN;
39   if (canvas()->width() > w) w = canvas()->width();
40   if (canvas()->height() > h) h = canvas()->height();
41   if (canvas()->width() < w || canvas()->height() < h)
42     canvas()->resize(w, h);
43   QCanvasPolygonalItem::moveBy(dx, dy);
44 }
45
46 void SUPERVGUI_CanvasCellNodePrs::drawShape(QPainter& thePainter) {
47   drawTitle(thePainter);
48   drawLabel(thePainter);
49   drawStatus(thePainter);
50   drawFrame(thePainter);
51 }
52
53 void drawCellText(QPainter& thePainter, const QString& theText, 
54                   const QRect& theRect, int theHAlign = Qt::AlignAuto)
55 {
56   int flags = theHAlign | Qt::AlignVCenter;
57   QRect r(theRect.x() + TEXT_MARGIN, theRect.y(), 
58           theRect.width() - 2*TEXT_MARGIN, theRect.height());
59
60   QWMatrix aMat = thePainter.worldMatrix();
61   if (aMat.m11() != 1.0) { 
62     // for scaled picture only
63     QRect r1 = aMat.mapRect(r);
64     QFont saved = thePainter.font();
65     QFont f(saved);
66     if (f.pointSize() == -1) {
67       f.setPixelSize((int)(f.pixelSize()*aMat.m11()));
68     }
69     else {
70       f.setPointSize((int)(f.pointSize()*aMat.m11()));
71     }
72     thePainter.save();
73     QWMatrix m;
74     thePainter.setWorldMatrix(m);
75     thePainter.setFont(f);
76     thePainter.drawText(r1, flags, theText);
77     thePainter.setFont(saved);
78     thePainter.restore();
79   }
80   else {
81     thePainter.drawText(r, flags, theText);
82   }
83 }
84
85 void SUPERVGUI_CanvasCellNodePrs::drawTitle(QPainter& thePainter) {
86   QBrush saved = thePainter.brush();
87   if (getNode()->getEngine()->IsLoop() || getNode()->getEngine()->IsEndLoop()
88       ||
89       getNode()->getEngine()->IsSwitch() || getNode()->getEngine()->IsEndSwitch())
90     thePainter.setBrush(Qt::red.light());
91   else if (getNode()->getEngine()->IsGOTO())
92     thePainter.setBrush(Qt::green.light());
93   else {
94     QBrush br( resMgr()->colorValue( "SUPERVGraph", "Title", DEF_MAIN_TITLE ) );
95     thePainter.setBrush(br);
96   }
97   drawTitleShape(thePainter);
98   thePainter.setBrush(saved);
99
100   drawCellText(thePainter, getNode()->getEngine()->Name(), getTitleRect(), Qt::AlignLeft);
101 }
102
103 void SUPERVGUI_CanvasCellNodePrs::drawLabel(QPainter& thePainter) 
104 {
105   QRect r = getLabelRect();
106
107   QPen saved = thePainter.pen();
108   thePainter.setPen(NoPen);
109   thePainter.drawRect(r);
110   thePainter.setPen(saved);
111
112   drawCellText(thePainter, getNode()->getLabelText(), r, Qt::AlignLeft);
113 }
114
115 void SUPERVGUI_CanvasCellNodePrs::drawStatus(QPainter& thePainter) 
116 {
117   QRect r = getStatusRect();
118
119   QBrush savedB = thePainter.brush();
120   thePainter.setBrush(getStatusColor());
121   drawStatusShape(thePainter);
122   thePainter.setBrush(savedB);
123
124   drawCellText(thePainter, getStatus(), r, Qt::AlignHCenter);
125 }
126
127 QRect SUPERVGUI_CanvasCellNodePrs::getTitleRect() const
128 {
129   return QRect((int)x(), (int)y(), getTitleWidth(), getTitleHeight());
130 }
131
132 QRect SUPERVGUI_CanvasCellNodePrs::getLabelRect() const
133 {
134   return QRect(((int)x())+getTitleWidth(), (int)y(), getLabelWidth(), getLabelHeight());
135 }
136
137 QRect SUPERVGUI_CanvasCellNodePrs::getStatusRect() const
138 {
139   return QRect(((int)x())+getTitleWidth()+getLabelWidth(), (int)y(),
140                getStatusWidth(), getStatusHeight());
141 }
142
143 int SUPERVGUI_CanvasCellNodePrs::getTitleWidth() const {
144   return myTitleWidth;
145 }
146
147 int SUPERVGUI_CanvasCellNodePrs::getLabelWidth() const {
148   return myLabelWidth;
149 }
150
151 int SUPERVGUI_CanvasCellNodePrs::getStatusWidth() const {
152   return myStatusWidth;
153 }
154
155 int SUPERVGUI_CanvasCellNodePrs::width() const
156 {
157   return myTitleWidth + myLabelWidth + myStatusWidth;
158 }
159
160 int SUPERVGUI_CanvasCellNodePrs::height() const
161 {
162   return getTitleHeight();
163 }
164
165 void SUPERVGUI_CanvasCellNodePrs::setState(SUPERV::GraphState theState)
166 {
167   switch(theState) {
168   case SUPERV_Waiting:
169     setStatus("Waiting");
170     setStatusColor(QColor(35, 192, 255));
171     break;
172
173   case SUPERV_Running:
174   case SUPERV::ReadyState:
175     setStatus("Running");
176     setStatusColor(QColor(32,210,32));
177     break;
178
179   case SUPERV_Suspend:
180   case SUPERV::SuspendReadyState:
181     setStatus("Suspended");
182     setStatusColor(QColor(255,180, 0));
183     break;
184
185   case SUPERV_Done:
186     setStatus("Finished");
187     setStatusColor(QColor(255, 158, 255));
188     break;
189
190   case SUPERV_Error: 
191     setStatus("Aborted");
192     setStatusColor(Qt::red);
193     break;
194
195   case SUPERV_Kill:
196     setStatus("Killed");
197     setStatusColor(Qt::red);
198     break;
199
200   case SUPERV::LoadingState:
201     setStatus("Loading");
202     setStatusColor(QColor(56,255,56));
203     break;
204
205   default:
206     setStatus("No Status");
207     setStatusColor( resMgr()->colorValue( "SUPERVGraph", "NodeBody", DEF_MAIN_BACK ) ); // mkr : IPAL10829
208     break;
209   }
210
211   canvas()->setChanged(getStatusRect());
212   canvas()->update();
213 }