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