Salome HOME
The following bug was fixed:
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasArray.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE
4 //
5 //  File   : SUPERVGUI_CanvasArray.cxx
6 //  Author : 
7 //  Module : SUPERV
8
9 using namespace std;
10 #include "SUPERVGUI_CanvasArray.h"
11 #include "SUPERVGUI_Main.h"
12 #include "SUPERVGUI_BrowseNodeDlg.h"
13 #include "QAD_Config.h"
14 #include "SUPERVGUI_CanvasCellNodePrs.h"
15
16 #include <qtooltip.h>
17 //#include <qpointarray.h> 
18 #define TEXT_MARGIN 5
19
20 #define ADDNODES(NodesName,LevelHasDiffNT) \
21     ncols = nodes->NodesName.length(); \
22     for (int co=0; co<ncols; co++) {  \
23       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
24       if (!LevelHasDiffNT) x = 50 + co * (CELL_WIDTH  + CELL_SPACE); \
25       else x += (CELL_WIDTH  + CELL_SPACE); \
26       cell = getCellNode(aCNode); \
27       if (cell == NULL) { \
28         cout<<" ***>  cell is NULL => create a new one"<<endl; \
29         SUPERVGUI_CanvasCellNode* aNode = new SUPERVGUI_CanvasCellNode(this, myMain, aCNode); \
30         aNode->move(x, y); \
31         aNode->show(); \
32         aNode->sync(); \
33       } else { \
34         cout<<" ***>  cell is NOT NULL => not create"<<endl; \
35         cell->move(x, y); \
36         cell->show(); \
37       } \
38       update(); \
39     }
40
41
42
43 #define ADDCONTROLNODES(NodesName,LevelHasDiffNT) \
44     ncols = nodes->NodesName.length(); \
45     if (!LevelHasDiffNT) x = 50; \
46     else x += (CELL_WIDTH  + CELL_SPACE); \
47     for (int co=0; co<ncols; co++) {  \
48       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
49       SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->NodesName[co]->Coupled()); \
50       cell = getCellNode(aCNode); \
51       if (cell == NULL) { \
52         cout<<" ***>  cell control is NULL => create a new one"<<endl; \
53         SUPERVGUI_CanvasCellNode* aNode = new SUPERVGUI_CanvasCellNode(this, myMain, aCNode); \
54         aNode->move(x, y); \
55         aNode->show(); \
56         aNode->sync(); \
57         y += (CELL_HEIGHT + CELL_SPACE); \
58         SUPERVGUI_CanvasCellNode* aNodeEnd = new SUPERVGUI_CanvasCellEndNode(this, myMain, aCNodeEnd, aNode); \
59         aNodeEnd->move(x, y); \
60         aNodeEnd->show(); \
61         aNodeEnd->sync(); \
62       } else { \
63         cout<<" ***>  cell control is NOT NULL => not create"<<endl; \
64         cell->move(x, y); \
65         cell->show(); \
66         y += (CELL_HEIGHT + CELL_SPACE); \
67         cell = getCellNode(aCNodeEnd); \
68         cell->move(x, y); \
69         cell->show(); \
70       } \
71       update(); \
72       x += (CELL_WIDTH + CELL_SPACE); \
73     }
74
75
76 // ----------------------------------------------------------
77 // SUPERVGUI_Array on QCanvas
78 // ----------------------------------------------------------
79
80 SUPERVGUI_CanvasArray::SUPERVGUI_CanvasArray(SUPERVGUI_Main* m):
81   QCanvas(),
82   myMain(m)
83 {
84   Trace("SUPERVGUI_CanvasArray::SUPERVGUI_CanvasArray");
85   //resize(GRAPH_WIDTH, GRAPH_HEIGHT);
86   resize(800, 600);
87   setDoubleBuffering(true);
88
89   QColor aColor(MAIN_COLOR);
90   QString aRed   = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorRed");
91   QString aGreen = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorGreen");
92   QString aBlue  = QAD_CONFIG->getSetting("SUPERVGraph:BackgroundColorBlue");
93   if( (!aRed.isEmpty()) && (!aGreen.isEmpty()) && (!aBlue.isEmpty()) )
94     aColor = QColor(aRed.toInt(), aGreen.toInt(), aBlue.toInt());
95   setBackgroundColor(aColor);
96   myOldThreadNum = 0;
97 }
98
99 SUPERVGUI_CanvasArray::~SUPERVGUI_CanvasArray() {
100   Trace("SUPERVGUI_Array::~SUPERVGUI_CanvasArray");
101   //delete all cells which SUPERVGUI_CanvasArray contains
102   //destroy();
103   QObjectList* aCellList = queryList("SUPERVGUI_CanvasCellNode");
104   QObjectListIt aIt(*aCellList);
105   QObject* anObj;
106   while ((anObj = aIt.current()) != 0) {
107     ++aIt;
108     delete anObj;
109   }
110
111   delete aCellList;
112
113   QObjectList* aCellEndList = queryList("SUPERVGUI_CanvasCellEndNode");
114   QObjectListIt aItEnd(*aCellEndList);
115   QObject* anObjEnd;
116   while ((anObjEnd = aItEnd.current()) != 0) {
117     ++aItEnd;
118     delete anObjEnd;
119   }
120
121   delete aCellEndList;
122 }
123
124 void SUPERVGUI_CanvasArray::sync() {
125   MESSAGE("===> SUPERVGUI_CanvasArray::sync()");
126   if (SUPERV_isNull(myMain->getDataflow())) return;
127   
128   SUPERVGUI_CanvasCellNode* ihmNode;
129   QObjectList* ihmList = queryList("SUPERVGUI_CanvasCellNode");
130   QObjectListIt it(*ihmList);
131   while ((ihmNode=(SUPERVGUI_CanvasCellNode*)it.current()) != 0) {
132     ++it;
133     ihmNode->sync();
134   }
135   delete ihmList;
136
137   SUPERVGUI_CanvasCellEndNode* ihmNodeEnd;
138   QObjectList* ihmListEnd = queryList("SUPERVGUI_CanvasCellEndNode");
139   QObjectListIt itEnd(*ihmListEnd);
140   while ((ihmNodeEnd=(SUPERVGUI_CanvasCellEndNode*)itEnd.current()) != 0) {
141     ++itEnd;
142     ihmNodeEnd->sync();
143   }
144   delete ihmListEnd;
145 }
146
147 bool SUPERVGUI_CanvasArray::create() {
148   Trace("SUPERVGUI_Array::create");
149   SUPERV_Nodes nodes = myMain->getDataflow()->Nodes();
150   int aCount = 0;
151   aCount += nodes->CNodes.length();
152   aCount += nodes->FNodes.length();
153   aCount += nodes->INodes.length();
154   aCount += nodes->GNodes.length();
155   aCount += nodes->LNodes.length();
156   aCount += nodes->SNodes.length();
157   if (aCount == 0) return true;
158
159   SUPERVGUI_CanvasCellNode* cell;
160   QString         aLabel;
161   int             x, y;
162   int             nligs = myMain->getDataflow()->LevelMax();
163   int             ncols = myMain->getDataflow()->ThreadsMax();
164   
165   // there is no any calculations
166   if (ncols == 0) return false;
167   
168   QCanvasItemList anIL1 = this->allItems();
169   cout<<" ===> begin create() : CanvasArray has "<<anIL1.count()<<" items!"<<endl;
170
171   QPen pen(Qt::SolidLine);
172   pen.setWidth(1);
173   QBrush br(MAIN_TITLE);
174   
175   if (myOldThreadNum) { //some of threads allready exist on canvas array
176     cout<<" ===> second ... creation !"<<endl;
177     QObjectList* aThreadList = queryList("QCanvasRectangle");
178     QObjectListIt aItThread(*aThreadList);
179     QCanvasRectangle* anObjThread;
180     while ((anObjThread = (QCanvasRectangle*)aItThread.current()) != 0) {
181       ++aItThread;
182       if (!anObjThread->isVisible())
183         anObjThread->show();
184     }
185     delete aThreadList;
186     
187     QObjectList* aTextList = queryList("QCanvasText");
188     QObjectListIt aItText(*aTextList);
189     QCanvasText* anObjText;
190     while ((anObjText = (QCanvasText*)aItText.current()) != 0) {
191       ++aItText;
192       if (!anObjText->isVisible())
193         anObjText->show();
194     }
195     delete aTextList;
196   }
197
198   for (int co = myOldThreadNum; co < ncols; co++) {
199     aLabel = QString("Thread %1").arg(co);
200     QRect aRect = QRect(50 + co * (CELL_WIDTH  + CELL_SPACE), 20, CELL_WIDTH, CELL_HEIGHT);
201     QCanvasRectangle* aThread = new QCanvasRectangle(aRect, this);
202     aThread->setPen(pen);
203     aThread->setBrush(br);
204     aThread->setZ(0);
205     aThread->show();
206     
207     QCanvasText* aText = new QCanvasText(aLabel, this);
208     QRect aBRect = aText->boundingRect();
209     aText->setX(aRect.x() + aRect.width()/2 - aBRect.width()/2);
210     aText->setY(aRect.y() + aRect.height()/2 - aBRect.height()/2);
211     aText->setZ(1);
212     aText->show();
213   }
214   
215   myOldThreadNum = ncols;
216   
217   QCanvasItemList anIL2 = this->allItems();
218   cout<<" ===> 1 create() : CanvasArray has "<<anIL2.count()<<" items!"<<endl;
219   
220   y = 60;
221   bool LevelHasDiffNT = false;
222   for (int li = 0; li <= nligs; li++) {
223     nodes = myMain->getDataflow()->LevelNodes(li);
224     int aSumNum = nodes->CNodes.length()+nodes->FNodes.length()+nodes->INodes.length()+
225                   nodes->GNodes.length()+nodes->LNodes.length()+nodes->SNodes.length();
226     if (nodes->CNodes.length() != 0 ) {
227       ADDNODES(CNodes,LevelHasDiffNT);
228       if (aSumNum > nodes->CNodes.length())
229         LevelHasDiffNT = true;
230     }
231     if (nodes->FNodes.length() != 0 ) {
232       ADDNODES(FNodes,LevelHasDiffNT);
233
234        QCanvasItemList anIL3 = this->allItems();
235        cout<<" ===> 2 create() : CanvasArray has "<<anIL3.count()<<" items!"<<endl;
236
237       if (aSumNum > nodes->FNodes.length())
238         LevelHasDiffNT = true;
239     }
240     if (nodes->INodes.length() != 0 ) {
241       ADDNODES(INodes,LevelHasDiffNT);
242       if (aSumNum > nodes->INodes.length())
243         LevelHasDiffNT = true;
244     }
245     if (nodes->GNodes.length() != 0 ) {
246       ADDNODES(GNodes,LevelHasDiffNT);
247       if (aSumNum > nodes->GNodes.length())
248         LevelHasDiffNT = true;
249     }
250     if (nodes->LNodes.length() != 0 ) {
251       ADDCONTROLNODES(LNodes,LevelHasDiffNT);
252       if (aSumNum > nodes->LNodes.length())
253         LevelHasDiffNT = true;
254     }
255     if (nodes->SNodes.length() != 0 ) {
256       ADDCONTROLNODES(SNodes,LevelHasDiffNT);
257       if (aSumNum > nodes->SNodes.length())
258         LevelHasDiffNT = true;
259     }
260     y += (CELL_HEIGHT + CELL_SPACE);
261
262     LevelHasDiffNT = false;
263   }
264   nodeX = 50;
265   nodeY = y + CELL_HEIGHT*2;
266
267   QCanvasItemList anIL4 = this->allItems();
268   cout<<" ===> end create() : CanvasArray has "<<anIL4.count()<<" items!"<<endl;
269
270   return true;
271
272 }
273
274 void SUPERVGUI_CanvasArray::destroy() {
275   Trace("SUPERVGUI_Array::destroy");
276   
277   QCanvasItemList anIL1 = this->allItems();
278   cout<<" ===> begin destroy() : CanvasArray has "<<anIL1.count()<<" items!"<<endl;
279   const QObjectList* anCL1 = this->children();
280   cout<<" ===> begin destroy() : CanvasArray has "<<anCL1->count()<<" children!"<<endl;
281
282   QObjectList* aCellList = queryList("SUPERVGUI_CanvasCellNode");
283   QObjectListIt aIt(*aCellList);
284   QObject* anObj;
285   while ((anObj = aIt.current()) != 0) {
286     ++aIt;
287     ((SUPERVGUI_CanvasCellNode*)anObj)->getPrs()->hide();
288   }
289
290   delete aCellList;
291
292   QObjectList* aCellEndList = queryList("SUPERVGUI_CanvasCellEndNode");
293   QObjectListIt aItEnd(*aCellEndList);
294   QObject* anObjEnd;
295   while ((anObjEnd = aItEnd.current()) != 0) {
296     ++aItEnd;
297     ((SUPERVGUI_CanvasCellEndNode*)anObjEnd)->getPrs()->hide();
298   }
299
300   delete aCellEndList;
301
302   QObjectList* aThreadList = queryList("QCanvasRectangle");
303   QObjectListIt aItThread(*aThreadList);
304   QCanvasRectangle* anObjThread;
305   while ((anObjThread = (QCanvasRectangle*)aItThread.current()) != 0) {
306     ++aItThread;
307     anObjThread->hide(); 
308   }
309
310   delete aThreadList;
311
312   QObjectList* aTextList = queryList("QCanvasText");
313   QObjectListIt aItText(*aTextList);
314   QCanvasText* anObjText;
315   while ((anObjText = (QCanvasText*)aItText.current()) != 0) {
316     ++aItText;
317     anObjText->hide(); 
318   }
319
320   delete aTextList; 
321
322   QCanvasItemList anIL2 = this->allItems();
323   cout<<" ===> end destroy() : CanvasArray has "<<anIL2.count()<<" items!"<<endl; 
324 }
325
326 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasArray::getCellNode(SUPERV::CNode_ptr theNode) const
327 {
328   return (SUPERVGUI_CanvasCellNode*) 
329     ((SUPERVGUI_CanvasArray*)this)->child(theNode->Name(), "SUPERVGUI_CanvasCellNode");
330 }
331
332 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasArray::addNode(SUPERV_CNode node) {
333   Trace("SUPERVGUI_CanvasArray::addNode");
334   SUPERVGUI_CanvasCellNode* n = new SUPERVGUI_CanvasCellNode(this, myMain, node);
335   n->move(nodeX, nodeY);
336   n->show();
337   update();
338   nodeX += NODE_DX;
339   nodeY += NODE_DY;
340   n->sync();
341   return(n);
342 }