Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasArray.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  SUPERV SUPERVGUI : GUI for Supervisor component
21 //
22 //  File   : SUPERVGUI_CanvasArray.cxx
23 //  Author : 
24 //  Module : SUPERV
25
26 #include "SUPERVGUI.h"
27 #include "SUPERVGUI_CanvasArray.h"
28 #include "SUPERVGUI_Main.h"
29 #include "SUPERVGUI_BrowseNodeDlg.h"
30 #include "SUPERVGUI_CanvasCellNodePrs.h"
31
32 #include "SUIT_ResourceMgr.h"
33 #include "SUIT_Session.h"
34
35 #include <qtooltip.h>
36 #define TEXT_MARGIN 5
37
38 #include <iostream.h> //for debug only
39
40 #define ADDNODES(NodesName,LevelHasDiffNT) \
41     ncols = nodes->NodesName.length(); \
42     for (int co=0; co<ncols; co++) {  \
43       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
44       if (!LevelHasDiffNT) x = 50 + co * (CELL_WIDTH  + CELL_SPACE); \
45       else x += (CELL_WIDTH  + CELL_SPACE); \
46       cell = getCellNode(aCNode); \
47       if (cell == NULL) { \
48         SUPERVGUI_CanvasCellNode* aNode = SUPERVGUI_CanvasCellNode::Create(myMgr, this, myMain, aCNode); \
49         aNode->move(x, y); \
50         aNode->show(); \
51         aNode->sync(); \
52       } else { \
53         cell->move(x, y); \
54         cell->show(); \
55       } \
56       update(); \
57     }
58
59
60
61 #define ADDCONTROLNODES(NodesName,LevelHasDiffNT) \
62     ncols = nodes->NodesName.length(); \
63     if (!LevelHasDiffNT) x = 50; \
64     else x += (CELL_WIDTH  + CELL_SPACE); \
65     for (int co=0; co<ncols; co++) {  \
66       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
67       SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->NodesName[co]->Coupled()); \
68       cell = getCellNode(aCNode); \
69       if (cell == NULL) { \
70         SUPERVGUI_CanvasCellNode* aNode = SUPERVGUI_CanvasCellNode::Create(myMgr, this, myMain, aCNode); \
71         aNode->move(x, y); \
72         aNode->show(); \
73         aNode->sync(); \
74         y += (CELL_HEIGHT + CELL_SPACE); \
75         SUPERVGUI_CanvasCellNode* aNodeEnd = SUPERVGUI_CanvasCellEndNode::Create(myMgr, this, myMain, aCNodeEnd, aNode); \
76         aNodeEnd->move(x, y); \
77         aNodeEnd->show(); \
78         aNodeEnd->sync(); \
79       } else { \
80         cell->move(x, y); \
81         cell->show(); \
82         y += (CELL_HEIGHT + CELL_SPACE); \
83         cell = getCellNode(aCNodeEnd); \
84         cell->move(x, y); \
85         cell->show(); \
86       } \
87       update(); \
88       if (co < (ncols-1)) x += (CELL_WIDTH + CELL_SPACE); \
89     }
90
91
92 // ----------------------------------------------------------
93 // SUPERVGUI_Array on QCanvas
94 // ----------------------------------------------------------
95
96 SUPERVGUI_CanvasArray::SUPERVGUI_CanvasArray(SUPERVGUI_Main* m, SUIT_ResourceMgr* mgr ):
97   QCanvas(),
98   myMain(m),
99   myMgr( mgr )
100 {
101   Trace("SUPERVGUI_CanvasArray::SUPERVGUI_CanvasArray");
102   //resize(GRAPH_WIDTH, GRAPH_HEIGHT);
103   resize(1000, 725);
104   setDoubleBuffering(true);
105
106   // mkr : IPAL10825 -->
107   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
108   if ( !aSupMod ) {
109     MESSAGE("NULL Supervision module!");
110     return;
111   }
112   QColor aColor =  aSupMod->getIVFBackgroundColor();
113   if ( !aColor.isValid() )
114     aColor = mgr->colorValue( "SUPERVGraph", "Background", DEF_MAIN_COLOR );
115   // <--
116   
117   //aColor = QColor(SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorRed" ), 
118   //              SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorGreen" ), 
119   //              SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorBlue" ) );
120   setBackgroundColor(aColor);
121 }
122
123 SUPERVGUI_CanvasArray::~SUPERVGUI_CanvasArray() {
124   Trace("SUPERVGUI_Array::~SUPERVGUI_CanvasArray");
125   //delete all cells which SUPERVGUI_CanvasArray contains
126   //destroy();
127   QObjectList* aCellList = queryList("SUPERVGUI_CanvasCellNode");
128   QObjectListIt aIt(*aCellList);
129   QObject* anObj;
130   while ((anObj = aIt.current()) != 0) {
131     ++aIt;
132     delete anObj;
133   }
134   delete aCellList;
135 }
136
137 void SUPERVGUI_CanvasArray::sync() {
138   if (SUPERV_isNull(myMain->getDataflow())) return;
139   
140   SUPERVGUI_CanvasCellNode* ihmNode;
141   QObjectList* ihmList = queryList("SUPERVGUI_CanvasCellNode");
142   QObjectListIt it(*ihmList);
143   while ((ihmNode=(SUPERVGUI_CanvasCellNode*)it.current()) != 0) {
144     ++it;
145     ihmNode->sync();
146   }
147   delete ihmList;
148 }
149
150 bool SUPERVGUI_CanvasArray::create() {
151   Trace("SUPERVGUI_Array::create");
152   SUPERV_Nodes nodes = myMain->getDataflow()->Nodes();
153   int aCount = 0;
154   aCount += nodes->CNodes.length();
155   aCount += nodes->FNodes.length();
156   aCount += nodes->INodes.length();
157   aCount += nodes->GNodes.length();
158   aCount += nodes->LNodes.length();
159   aCount += nodes->SNodes.length();
160   if (aCount == 0) return true;
161
162   SUPERVGUI_CanvasCellNode* cell;
163   QString         aLabel;
164   int             x, y;
165  
166   int             nligs = myMain->getDataflow()->LevelMax();
167   int             ncols = myMain->getDataflow()->ThreadsMax();
168   
169   // there is no any calculations
170   if (ncols == 0) return false;
171
172   QPen pen(Qt::SolidLine);
173   pen.setWidth(1);
174
175   QBrush br( myMgr->colorValue( "SUPERVGraph", "Title", DEF_MAIN_TITLE ) );
176   
177   // variables to resize canvas in table view
178   int aMaxWidth = 0;
179   int aMaxHeight = 0;
180   
181   for (int co = 0; co < ncols; co++) {
182     aLabel = QString("Thread %1").arg(co);
183     QRect aRect = QRect(50 + co * (CELL_WIDTH  + CELL_SPACE), 20, CELL_WIDTH, CELL_HEIGHT);
184     QCanvasRectangle* aThread = new QCanvasRectangle(aRect, this);
185     aThread->setPen(pen);
186     aThread->setBrush(br);
187     aThread->setZ(0);
188     aThread->show();
189     
190     QCanvasText* aText = new QCanvasText(aLabel, this);
191     QRect aBRect = aText->boundingRect();
192     aText->setX(aRect.x() + aRect.width()/2 - aBRect.width()/2);
193     aText->setY(aRect.y() + aRect.height()/2 - aBRect.height()/2);
194     aText->setZ(1);
195     aText->show();
196     
197     aMaxWidth = aRect.x() + aRect.width() + 50;
198   }
199   
200   y = 60;
201   bool LevelHasDiffNT = false;
202   for (int li = 0; li <= nligs; li++) {
203     nodes = myMain->getDataflow()->LevelNodes(li);
204     int aSumNum = nodes->CNodes.length()+nodes->FNodes.length()+nodes->INodes.length()+
205                   nodes->GNodes.length()+nodes->LNodes.length()+nodes->SNodes.length();
206     if (nodes->CNodes.length() != 0 ) {
207       ADDNODES(CNodes,LevelHasDiffNT);
208       if (aSumNum > nodes->CNodes.length())
209         LevelHasDiffNT = true;
210     }
211     if (nodes->FNodes.length() != 0 ) {
212       ADDNODES(FNodes,LevelHasDiffNT);
213       if (aSumNum > nodes->FNodes.length())
214         LevelHasDiffNT = true;
215     }
216     if (nodes->INodes.length() != 0 ) {
217       ADDNODES(INodes,LevelHasDiffNT);
218       if (aSumNum > nodes->INodes.length())
219         LevelHasDiffNT = true;
220     }
221     if (nodes->GNodes.length() != 0 ) {
222       ADDNODES(GNodes,LevelHasDiffNT);
223       if (aSumNum > nodes->GNodes.length())
224         LevelHasDiffNT = true;
225     }
226     if (nodes->LNodes.length() != 0 ) {
227       ADDCONTROLNODES(LNodes,LevelHasDiffNT);
228       if (aSumNum > nodes->LNodes.length())
229         LevelHasDiffNT = true;
230     }
231     if (nodes->SNodes.length() != 0 ) {
232       ADDCONTROLNODES(SNodes,LevelHasDiffNT);
233       if (aSumNum > nodes->SNodes.length())
234         LevelHasDiffNT = true;
235     }
236     y += (CELL_HEIGHT + CELL_SPACE);
237
238     LevelHasDiffNT = false;
239   }
240   nodeX = 50;
241   nodeY = y + CELL_HEIGHT*2;
242
243   aMaxHeight = nodeY;
244   
245   // if aMaxWidth and aMaxHeight is greater than the current size 
246   // of the canvas, we resize canvas to these new width and height
247   if ( aMaxWidth > this->width() || aMaxHeight > this->height() )
248     this->resize( aMaxWidth > this->width() ? aMaxWidth : this->width(), 
249                   aMaxHeight > this->height() ? aMaxHeight : this->height() );
250   return true;
251
252 }
253
254 void SUPERVGUI_CanvasArray::destroy() {
255   Trace("SUPERVGUI_Array::destroy");
256   
257   QObjectList* aCellList = queryList("SUPERVGUI_CanvasCellNode");
258   QObjectListIt aIt(*aCellList);
259   QObject* anObj;
260   while ((anObj = aIt.current()) != 0) {
261     ++aIt;
262     ((SUPERVGUI_CanvasCellNode*)anObj)->getPrs()->hide();
263   }
264   delete aCellList;
265
266   // mkr : delete QCanvasRectangle and QCanvasText canvas items for each Thread.
267   // Threads will be recreate when create() function is called
268   QCanvasItemList aTextList = allItems();
269   for (QCanvasItemList::Iterator aItText = aTextList.begin(); aItText != aTextList.end(); ++aItText) {
270     SUPERVGUI_CanvasCellNodePrs* aPrs = dynamic_cast<SUPERVGUI_CanvasCellNodePrs*>(*aItText);
271     if (!aPrs) {
272       (*aItText)->hide();
273       delete *aItText;
274     }
275   }
276 }
277
278 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasArray::getCellNode(SUPERV::CNode_ptr theNode) const
279 {
280   return (SUPERVGUI_CanvasCellNode*) 
281     ((SUPERVGUI_CanvasArray*)this)->child(theNode->Name(), "SUPERVGUI_CanvasCellNode");
282 }
283
284 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasArray::addNode(SUPERV_CNode node) {
285   Trace("SUPERVGUI_CanvasArray::addNode");
286   SUPERVGUI_CanvasCellNode* n = SUPERVGUI_CanvasCellNode::Create(myMgr, this, myMain, node);
287   n->move(nodeX, nodeY);
288   n->show();
289   update();
290   nodeX += NODE_DX;
291   nodeY += NODE_DY;
292   n->sync();
293   return(n);
294 }