Salome HOME
bd3d1219f669464415f3b260989c0534b9c9c6d0
[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 "SUPERVGUI_CanvasCellNodePrs.h"
14
15 #include "SUIT_ResourceMgr.h"
16 #include "SUIT_Session.h"
17
18 #include <qtooltip.h>
19 #define TEXT_MARGIN 5
20
21 #include <iostream.h> //for debug only
22
23 #define ADDNODES(NodesName,LevelHasDiffNT) \
24     ncols = nodes->NodesName.length(); \
25     for (int co=0; co<ncols; co++) {  \
26       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
27       if (!LevelHasDiffNT) x = 50 + co * (CELL_WIDTH  + CELL_SPACE); \
28       else x += (CELL_WIDTH  + CELL_SPACE); \
29       cell = getCellNode(aCNode); \
30       if (cell == NULL) { \
31         SUPERVGUI_CanvasCellNode* aNode = new SUPERVGUI_CanvasCellNode(myMgr, this, myMain, aCNode); \
32         aNode->move(x, y); \
33         aNode->show(); \
34         aNode->sync(); \
35       } else { \
36         cell->move(x, y); \
37         cell->show(); \
38       } \
39       update(); \
40     }
41
42
43
44 #define ADDCONTROLNODES(NodesName,LevelHasDiffNT) \
45     ncols = nodes->NodesName.length(); \
46     if (!LevelHasDiffNT) x = 50; \
47     else x += (CELL_WIDTH  + CELL_SPACE); \
48     for (int co=0; co<ncols; co++) {  \
49       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
50       SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->NodesName[co]->Coupled()); \
51       cell = getCellNode(aCNode); \
52       if (cell == NULL) { \
53         SUPERVGUI_CanvasCellNode* aNode = new SUPERVGUI_CanvasCellNode( myMgr, 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( myMgr, this, myMain, aCNodeEnd, aNode); \
59         aNodeEnd->move(x, y); \
60         aNodeEnd->show(); \
61         aNodeEnd->sync(); \
62       } else { \
63         cell->move(x, y); \
64         cell->show(); \
65         y += (CELL_HEIGHT + CELL_SPACE); \
66         cell = getCellNode(aCNodeEnd); \
67         cell->move(x, y); \
68         cell->show(); \
69       } \
70       update(); \
71       if (co < (ncols-1)) x += (CELL_WIDTH + CELL_SPACE); \
72     }
73
74
75 // ----------------------------------------------------------
76 // SUPERVGUI_Array on QCanvas
77 // ----------------------------------------------------------
78
79 SUPERVGUI_CanvasArray::SUPERVGUI_CanvasArray(SUPERVGUI_Main* m, SUIT_ResourceMgr* mgr ):
80   QCanvas(),
81   myMain(m),
82   myMgr( mgr )
83 {
84   Trace("SUPERVGUI_CanvasArray::SUPERVGUI_CanvasArray");
85   //resize(GRAPH_WIDTH, GRAPH_HEIGHT);
86   resize(1000, 725);
87   setDoubleBuffering(true);
88
89   QColor aColor = mgr->colorValue( "SUPERVGraph", "Background", DEF_MAIN_COLOR );
90   //aColor = QColor(SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorRed" ), 
91   //              SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorGreen" ), 
92   //              SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorBlue" ) );
93   setBackgroundColor(aColor);
94 }
95
96 SUPERVGUI_CanvasArray::~SUPERVGUI_CanvasArray() {
97   Trace("SUPERVGUI_Array::~SUPERVGUI_CanvasArray");
98   //delete all cells which SUPERVGUI_CanvasArray contains
99   //destroy();
100   QObjectList* aCellList = queryList("SUPERVGUI_CanvasCellNode");
101   QObjectListIt aIt(*aCellList);
102   QObject* anObj;
103   while ((anObj = aIt.current()) != 0) {
104     ++aIt;
105     delete anObj;
106   }
107   delete aCellList;
108 }
109
110 void SUPERVGUI_CanvasArray::sync() {
111   if (SUPERV_isNull(myMain->getDataflow())) return;
112   
113   SUPERVGUI_CanvasCellNode* ihmNode;
114   QObjectList* ihmList = queryList("SUPERVGUI_CanvasCellNode");
115   QObjectListIt it(*ihmList);
116   while ((ihmNode=(SUPERVGUI_CanvasCellNode*)it.current()) != 0) {
117     ++it;
118     ihmNode->sync();
119   }
120   delete ihmList;
121 }
122
123 bool SUPERVGUI_CanvasArray::create() {
124   Trace("SUPERVGUI_Array::create");
125   SUPERV_Nodes nodes = myMain->getDataflow()->Nodes();
126   int aCount = 0;
127   aCount += nodes->CNodes.length();
128   aCount += nodes->FNodes.length();
129   aCount += nodes->INodes.length();
130   aCount += nodes->GNodes.length();
131   aCount += nodes->LNodes.length();
132   aCount += nodes->SNodes.length();
133   if (aCount == 0) return true;
134
135   SUPERVGUI_CanvasCellNode* cell;
136   QString         aLabel;
137   int             x, y;
138  
139   int             nligs = myMain->getDataflow()->LevelMax();
140   cout<<"-> myMain->getDataflow()->LevelMax() = "<<myMain->getDataflow()->LevelMax()<<endl;
141   int             ncols = myMain->getDataflow()->ThreadsMax();
142   cout<<"-> myMain->getDataflow()->ThreadsMax() = "<<myMain->getDataflow()->ThreadsMax()<<endl;
143   
144   // there is no any calculations
145   if (ncols == 0) return false;
146
147   QPen pen(Qt::SolidLine);
148   pen.setWidth(1);
149
150   QBrush br( myMgr->colorValue( "SUPERVGraph", "Title", DEF_MAIN_TITLE ) );
151   
152   // variables to resize canvas in table view
153   int aMaxWidth = 0;
154   int aMaxHeight = 0;
155   
156   for (int co = 0; co < ncols; co++) {
157     aLabel = QString("Thread %1").arg(co);
158     QRect aRect = QRect(50 + co * (CELL_WIDTH  + CELL_SPACE), 20, CELL_WIDTH, CELL_HEIGHT);
159     QCanvasRectangle* aThread = new QCanvasRectangle(aRect, this);
160     aThread->setPen(pen);
161     aThread->setBrush(br);
162     aThread->setZ(0);
163     aThread->show();
164     
165     QCanvasText* aText = new QCanvasText(aLabel, this);
166     QRect aBRect = aText->boundingRect();
167     aText->setX(aRect.x() + aRect.width()/2 - aBRect.width()/2);
168     aText->setY(aRect.y() + aRect.height()/2 - aBRect.height()/2);
169     aText->setZ(1);
170     aText->show();
171     
172     aMaxWidth = aRect.x() + aRect.width() + 50;
173   }
174   
175   y = 60;
176   bool LevelHasDiffNT = false;
177   for (int li = 0; li <= nligs; li++) {
178     nodes = myMain->getDataflow()->LevelNodes(li);
179     int aSumNum = nodes->CNodes.length()+nodes->FNodes.length()+nodes->INodes.length()+
180                   nodes->GNodes.length()+nodes->LNodes.length()+nodes->SNodes.length();
181     if (nodes->CNodes.length() != 0 ) {
182       ADDNODES(CNodes,LevelHasDiffNT);
183       if (aSumNum > nodes->CNodes.length())
184         LevelHasDiffNT = true;
185     }
186     if (nodes->FNodes.length() != 0 ) {
187       ADDNODES(FNodes,LevelHasDiffNT);
188       if (aSumNum > nodes->FNodes.length())
189         LevelHasDiffNT = true;
190     }
191     if (nodes->INodes.length() != 0 ) {
192       ADDNODES(INodes,LevelHasDiffNT);
193       if (aSumNum > nodes->INodes.length())
194         LevelHasDiffNT = true;
195     }
196     if (nodes->GNodes.length() != 0 ) {
197       ADDNODES(GNodes,LevelHasDiffNT);
198       if (aSumNum > nodes->GNodes.length())
199         LevelHasDiffNT = true;
200     }
201     if (nodes->LNodes.length() != 0 ) {
202       ADDCONTROLNODES(LNodes,LevelHasDiffNT);
203       if (aSumNum > nodes->LNodes.length())
204         LevelHasDiffNT = true;
205     }
206     if (nodes->SNodes.length() != 0 ) {
207       ADDCONTROLNODES(SNodes,LevelHasDiffNT);
208       if (aSumNum > nodes->SNodes.length())
209         LevelHasDiffNT = true;
210     }
211     y += (CELL_HEIGHT + CELL_SPACE);
212
213     LevelHasDiffNT = false;
214   }
215   nodeX = 50;
216   nodeY = y + CELL_HEIGHT*2;
217
218   aMaxHeight = nodeY;
219   
220   // if aMaxWidth and aMaxHeight is greater than the current size 
221   // of the canvas, we resize canvas to these new width and height
222   if ( aMaxWidth > this->width() || aMaxHeight > this->height() )
223     this->resize( aMaxWidth > this->width() ? aMaxWidth : this->width(), 
224                   aMaxHeight > this->height() ? aMaxHeight : this->height() );
225   return true;
226
227 }
228
229 void SUPERVGUI_CanvasArray::destroy() {
230   Trace("SUPERVGUI_Array::destroy");
231   
232   QObjectList* aCellList = queryList("SUPERVGUI_CanvasCellNode");
233   QObjectListIt aIt(*aCellList);
234   QObject* anObj;
235   while ((anObj = aIt.current()) != 0) {
236     ++aIt;
237     ((SUPERVGUI_CanvasCellNode*)anObj)->getPrs()->hide();
238   }
239   delete aCellList;
240
241   // mkr : delete QCanvasRectangle and QCanvasText canvas items for each Thread.
242   // Threads will be recreate when create() function is called
243   QCanvasItemList aTextList = allItems();
244   for (QCanvasItemList::Iterator aItText = aTextList.begin(); aItText != aTextList.end(); ++aItText) {
245     SUPERVGUI_CanvasCellNodePrs* aPrs = dynamic_cast<SUPERVGUI_CanvasCellNodePrs*>(*aItText);
246     if (!aPrs) {
247       (*aItText)->hide();
248       delete *aItText;
249     }
250   }
251 }
252
253 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasArray::getCellNode(SUPERV::CNode_ptr theNode) const
254 {
255   return (SUPERVGUI_CanvasCellNode*) 
256     ((SUPERVGUI_CanvasArray*)this)->child(theNode->Name(), "SUPERVGUI_CanvasCellNode");
257 }
258
259 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasArray::addNode(SUPERV_CNode node) {
260   Trace("SUPERVGUI_CanvasArray::addNode");
261   SUPERVGUI_CanvasCellNode* n = new SUPERVGUI_CanvasCellNode( myMgr, this, myMain, node);
262   n->move(nodeX, nodeY);
263   n->show();
264   update();
265   nodeX += NODE_DX;
266   nodeY += NODE_DY;
267   n->sync();
268   return(n);
269 }