Salome HOME
Add functionality for Table view based on QCanvas.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Array.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SUPERVGUI_Array.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29 #include "SUPERVGUI_Def.h"
30 #include "SUPERVGUI_Array.h"
31 #include "SUPERVGUI_Main.h"
32 #include "SUPERVGUI_ComputeNode.h"
33 #include "SUPERVGUI_BrowseNodeDlg.h"
34 #include "SUPERVGUI_Node.h"
35 #include "SUPERVGUI_GraphNode.h"
36 #include "SUPERVGUI_ControlNode.h"
37 #include <qtooltip.h>
38 #include <qlayout.h>
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       SUPERVGUI_Node* aNode = new SUPERVGUI_Cell(viewport(), main, aCNode); \
47       ResizeGraph(aNode, x, y); \
48       addChild(aNode, x, y);  \
49     }
50
51
52
53 #define ADDCONTROLNODES(NodesName,LevelHasDiffNT) \
54     ncols = nodes->NodesName.length(); \
55     if (!LevelHasDiffNT) x = 50; \
56     else x += (CELL_WIDTH  + CELL_SPACE); \
57     for (int co=0; co<ncols; co++) {  \
58       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[co]); \
59       SUPERVGUI_Node* aNode = new SUPERVGUI_Cell(viewport(), main, aCNode); \
60       ResizeGraph(aNode, x, y); \
61       addChild(aNode, x, y);  \
62       y += (CELL_HEIGHT + CELL_SPACE); \
63       SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->NodesName[co]->Coupled()); \
64       SUPERVGUI_Node* aNodeEnd = new SUPERVGUI_CellEndControl(viewport(), main, aCNodeEnd, dynamic_cast<SUPERVGUI_Cell*>(aNode)); \
65       ResizeGraph(aNodeEnd, x, y); \
66       addChild(aNodeEnd, x, y);  \
67       x += (CELL_WIDTH + CELL_SPACE); \
68     }
69
70
71
72
73 SUPERVGUI_Array::SUPERVGUI_Array(SUPERVGUI_Main* m):
74     SUPERVGUI_View(m)
75 {
76   resizeContents(GRAPH_WIDTH, GRAPH_HEIGHT);
77   setName("TableView");
78   hide();
79 }
80
81 SUPERVGUI_Array::~SUPERVGUI_Array() {
82     Trace("SUPERVGUI_Array::~SUPERVGUI_Array")
83 }
84
85 bool SUPERVGUI_Array::create() {
86   Trace("SUPERVGUI_Array::create");
87   SUPERV_Nodes nodes = main->getDataflow()->Nodes();
88   int aCount = 0;
89   aCount += nodes->CNodes.length();
90   aCount += nodes->FNodes.length();
91   aCount += nodes->INodes.length();
92   aCount += nodes->GNodes.length();
93   aCount += nodes->LNodes.length();
94   aCount += nodes->SNodes.length();
95   if (aCount == 0) {
96     show();
97     return true;
98   }
99
100   //SUPERV_Nodes    nodes;
101   SUPERVGUI_Cell* cell;
102   QString         aLabel;
103   int             x, y;
104   int             nligs = main->getDataflow()->LevelMax();
105   int             ncols = main->getDataflow()->ThreadsMax();
106   
107   // there is no any calculations
108   if (ncols == 0) return false;
109   
110   for (int co=0; co<ncols; co++) {
111     aLabel = QString("Thread %1").arg(co);
112     QLabel* aTitle = new QLabel(aLabel, viewport());
113     aTitle->setAlignment(QLabel::AlignCenter | QLabel::AlignVCenter);
114     aTitle->setLineWidth(1);
115     aTitle->setPaletteBackgroundColor(MAIN_TITLE);
116     aTitle->setMinimumSize(CELL_WIDTH, CELL_HEIGHT);
117     aTitle->setFrameStyle(QFrame::Panel | QFrame::Plain);
118     addChild(aTitle, 50 + co * (CELL_WIDTH  + CELL_SPACE), 20);
119   }
120   y = 60;
121   bool LevelHasDiffNT = false;
122   for (int li=0; li<=nligs; li++) {
123     nodes = main->getDataflow()->LevelNodes(li);
124     int aSumNum = nodes->CNodes.length()+nodes->FNodes.length()+nodes->INodes.length()+
125                   nodes->GNodes.length()+nodes->LNodes.length()+nodes->SNodes.length();
126     if (nodes->CNodes.length() != 0 ) {
127       ADDNODES(CNodes,LevelHasDiffNT);
128       if (aSumNum > nodes->CNodes.length())
129         LevelHasDiffNT = true;
130     }
131     if (nodes->FNodes.length() != 0 ) {
132       ADDNODES(FNodes,LevelHasDiffNT);
133       if (aSumNum > nodes->FNodes.length())
134         LevelHasDiffNT = true;
135     }
136     if (nodes->INodes.length() != 0 ) {
137       ADDNODES(INodes,LevelHasDiffNT);
138       if (aSumNum > nodes->INodes.length())
139         LevelHasDiffNT = true;
140     }
141     if (nodes->GNodes.length() != 0 ) {
142       ADDNODES(GNodes,LevelHasDiffNT);
143       if (aSumNum > nodes->GNodes.length())
144         LevelHasDiffNT = true;
145     }
146     if (nodes->LNodes.length() != 0 ) {
147       ADDCONTROLNODES(LNodes,LevelHasDiffNT);
148       if (aSumNum > nodes->LNodes.length())
149         LevelHasDiffNT = true;
150     }
151     if (nodes->SNodes.length() != 0 ) {
152       ADDCONTROLNODES(SNodes,LevelHasDiffNT);
153       if (aSumNum > nodes->SNodes.length())
154         LevelHasDiffNT = true;
155     }
156     y += (CELL_HEIGHT + CELL_SPACE);
157
158     LevelHasDiffNT = false;
159   }
160   nodeX = 50;
161   nodeY = y + CELL_HEIGHT*2;
162   
163   show();
164   return true;
165 }
166
167 void SUPERVGUI_Array::destroy() {
168     Trace("SUPERVGUI_Array::destroy")
169     hide();
170
171     SUPERVGUI_Cell* cell;
172     QObjectList* ihmList = queryList("SUPERVGUI_Cell");
173     QObjectListIt i(*ihmList);
174
175     while ((cell=(SUPERVGUI_Cell*)i.current()) != 0) {
176         ++i;
177         cell->updateStudy();
178         cell->close(true);
179     };
180
181     delete ihmList;
182
183     SUPERVGUI_CellEndControl* cellEC;
184     QObjectList* ihmListEC = queryList("SUPERVGUI_CellEndControl");
185     QObjectListIt iEC(*ihmListEC);
186
187     while ((cellEC=(SUPERVGUI_CellEndControl*)iEC.current()) != 0) {
188         ++iEC;
189         cellEC->updateStudy();
190         cellEC->close(true);
191     };
192
193     delete ihmListEC;
194
195 }
196
197 void SUPERVGUI_Array::sync() {
198     Trace("SUPERVGUI_Array::sync")
199     if (SUPERV_isNull(main->getDataflow())) return;
200
201     SUPERVGUI_Cell* cell;
202     QObjectList* ihmList = queryList("SUPERVGUI_Cell");
203     QObjectListIt i(*ihmList);
204     while ((cell=(SUPERVGUI_Cell*)i.current()) != 0) {
205       ++i;
206       cell->sync();
207     }
208     delete ihmList;
209 }
210
211 SUPERVGUI_Node* SUPERVGUI_Array::addNode(SUPERV_CNode node) {
212     Trace("SUPERVGUI_Array::Array")
213     SUPERVGUI_Node* n = new SUPERVGUI_ComputeNode(viewport(), main, node);
214     ResizeGraph(n, nodeX, nodeY);
215     addChild(n, nodeX, nodeY);
216     nodeX += NODE_DX;
217     nodeY += NODE_DY;
218     n->sync();
219     return(n);
220 }
221
222 void SUPERVGUI_Array::contentsMousePressEvent(QMouseEvent* e) {
223     Trace("SUPERVGUI_Array::contentsMousePressEvent")
224     if (e->button() == RightButton) {
225         myPopup->exec(e->globalPos());
226     };
227 }
228
229
230
231
232
233 // ------------------------------------
234 // Cell widget of the table
235 // ------------------------------------
236
237
238 SUPERVGUI_Cell::SUPERVGUI_Cell(QWidget* parent, SUPERVGUI_Main* m, SUPERV_CNode n):
239     SUPERVGUI_Node(parent, m, n),
240     guiNode(NULL)
241 {
242   Trace("SUPERVGUI_Cell::SUPERVGUI_Cell");
243   setLineWidth(2);
244   setFrameStyle(QFrame::Panel | QFrame::Raised);
245   
246   QHBoxLayout* aBox = new QHBoxLayout(this);
247   aBox->setMargin(3);
248   aBox->setSpacing(3);
249
250   QString aCommentVis;
251   if (getNodeType() == SUPERV::FactoryNode)
252     aCommentVis = QString(myNode->Service()->ServiceName) + QString(tr("COMMENT_FROM"))
253       + QString(getFactoryNode()->GetComponentName());
254   else 
255     //aCommentVis = tr("COMMENT_PYTHON");
256     aCommentVis = tr("COMMENT_CNODE");
257
258   component = new SUPERVGUI_Label(this, CELL_WIDTH_PART, LABEL_HEIGHT, aCommentVis, QLabel::AlignLeft);
259   connect(component, SIGNAL(MousePress(QMouseEvent*)), this, SLOT(showPopup(QMouseEvent*)));
260
261   myTitle = new SUPERVGUI_Label(this, CELL_WIDTH_PART, LABEL_HEIGHT, name(), QLabel::AlignLeft);
262   connect(myTitle, SIGNAL(MousePress(QMouseEvent*)), this, SLOT(showPopup(QMouseEvent*)));
263   myTitle->setMinimumSize(CELL_WIDTH_PART, LABEL_HEIGHT);
264   myTitle->setMaximumSize(CELL_WIDTH_PART, LABEL_HEIGHT);
265
266   //  aBox->addWidget(service);
267   aBox->addWidget(myTitle);
268   aBox->addWidget(component);
269
270   myStatus->reparent(this, pos());
271   aBox->addWidget(myStatus);
272
273   if(m->isEditable()) {
274     //  QToolTip::add(service, myNode->Name());
275     QToolTip::add(myTitle, myTitle->text());
276     QToolTip::add(component, component->text());
277   }
278
279   myPopup->setItemEnabled(myDeleteItem, false);
280   adjustSize();
281
282   myIsControl = false;
283   myIsStart = false;
284
285   //check for control nodes
286   if (n->IsLoop() || n->IsSwitch()) {
287     myIsControl = true;  
288     myIsStart = true;
289   }
290   if (n->IsEndLoop() || n->IsEndSwitch())
291     myIsControl = true;
292 }
293
294 SUPERVGUI_Cell::~SUPERVGUI_Cell() {
295   Trace("SUPERVGUI_Cell::~SUPERVGUI_Cell");
296   QToolTip::remove(myTitle);
297   if (guiNode != NULL) {
298     guiNode->close();
299   }
300 }
301
302 void SUPERVGUI_Cell::sync() {
303   Trace("SUPERVGUI_Cell::sync");
304   if (myMain == NULL) return;
305   myTitle->setText(myNode->Name());
306   //component->setText(myNode->Comment());
307   
308   if (guiNode != NULL) {
309     guiNode->sync();
310     
311     SUPERVGUI_PortIn* pi;
312     QObjectList* ihmList = guiNode->queryList("SUPERVGUI_PortIn");
313     QObjectListIt i(*ihmList);
314     while ((pi=(SUPERVGUI_PortIn*)i.current()) != 0) {
315       ++i;
316       pi->sync();
317     }
318     delete ihmList;
319     
320     SUPERVGUI_PortOut* po;
321     ihmList = guiNode->queryList("SUPERVGUI_PortOut");
322     i = *ihmList;
323     while ((po=(SUPERVGUI_PortOut*)i.current()) != 0) {
324       ++i;
325       po->sync();
326     }
327     delete ihmList;
328   }
329   SUPERVGUI_Node::sync();
330 }
331
332
333 void SUPERVGUI_Cell::updateStudy()
334 {
335   Trace("SUPERVGUI_Cell::updateStudy")
336   if (guiNode != NULL)
337     {
338       SUPERVGUI_PortOut* po;
339       QObjectList* ihmList = guiNode->queryList("SUPERVGUI_PortOut");
340       QObjectListIt i(*ihmList);
341       while ((po=(SUPERVGUI_PortOut*)i.current()) != 0)
342         {
343           ++i;
344           po->updateStudy();
345         }
346       delete ihmList;
347     }
348 }
349
350
351 void SUPERVGUI_Cell::rename() {
352   SUPERVGUI_Node::rename();
353   if (!myIsControl) { //not ControlNode
354     myTitle->setText(myNode->Name());  
355   }
356   else {
357     if (myIsStart) { //StartControlNode
358       if (myPairCell) {
359         QString nm = myNode->Name();
360         if (!nm.isEmpty()) {
361           myTitle->setText(nm);  
362           QString aStr(tr("ENDNODE_PREFIX"));
363           aStr += nm;
364           myPairCell->getTitle()->setText(aStr);
365           myPairCell->setNodeName(aStr);
366           // QToolTip::remove(myPairCell->getTitle());
367           QToolTip::add(myPairCell->getTitle(), myPairCell->getTitle()->text());  
368
369         }
370       }
371     }
372   }
373
374   // QToolTip::remove(myTitle);
375   QToolTip::add(myTitle, myTitle->text());  
376 }
377
378 void SUPERVGUI_Cell::setPairCell(SUPERVGUI_Cell* thePairCell) {
379   if (myIsControl) { //only for ControlNode
380     myPairCell = thePairCell;
381   }
382   else 
383     myPairCell = 0;
384 }
385
386 SUPERVGUI_Cell* SUPERVGUI_Cell::getPairCell() {
387   return myPairCell;
388 }
389
390 SUPERVGUI_CellEndControl::SUPERVGUI_CellEndControl(QWidget* parent, SUPERVGUI_Main* m, SUPERV_CNode n,
391                                                    SUPERVGUI_Cell* theStart)
392      :SUPERVGUI_Cell(parent, m, n)
393 {
394   myPairCell = theStart;  //set start cell for end cell as pair 
395   myPairCell->setPairCell(dynamic_cast<SUPERVGUI_Cell*>(this));  //set end cell for start cell as pair
396 }
397
398 SUPERVGUI_CellEndControl::~SUPERVGUI_CellEndControl() {
399   Trace("SUPERVGUI_CellEndControl::~SUPERVGUI_CellEndControl");
400   QToolTip::remove(myTitle);
401   if (guiNode != NULL) {
402     guiNode->close();
403   }
404 }