Salome HOME
Add functionality for Table view based on QCanvas.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_ControlNode.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SUPERVGUI_ControlNode.cxx
8 //  Author : Vitaly SMETANNIKOV
9 //  Module : SUPERV
10
11 using namespace std;
12 #include "SUPERVGUI_ControlNode.h"
13 #include <qapplication.h>
14 #include "SUPERVGUI_Main.h"
15 #include <qgroupbox.h>
16
17 #define TITLECOLOR Qt::red.light()
18 #define BACKCOLOR Qt::cyan.light()
19
20 //-----------------------------------------------------------
21 //******************* Start Node ****************************
22 //-----------------------------------------------------------
23 SUPERVGUI_StartControlNode::SUPERVGUI_StartControlNode(QWidget* theParent, SUPERVGUI_Main* theMain, 
24                                                        SUPERV_CNode theNode):
25   SUPERVGUI_GraphNode(theParent, theMain, theNode) 
26 {
27   setPaletteBackgroundColor(BACKCOLOR);
28   QGridLayout* aGridLayout = new QGridLayout(this, 0, 3, 0, 0);
29   aGridLayout->setColStretch(0, 1);
30   aGridLayout->setColStretch(1, 1);
31   aGridLayout->addColSpacing(2, LABEL_HEIGHT/2);
32   
33   myTitle->setAlignment(Qt::AlignHCenter);
34   myTitle->reparent(this, pos());
35   myTitle->setPaletteBackgroundColor(TITLECOLOR);
36
37   aGridLayout->addMultiCellWidget(myTitle, 0, 0, 0, 2);
38
39   myPortsBox->reparent(this, pos());
40   aGridLayout->addMultiCellWidget(myPortsBox, 1, 1, 0, 1);
41   //  myGatesBox->setPaletteBackgroundColor(backgroundColor().dark(105));
42
43   myStatus->reparent(this, pos());
44   myTime->reparent(this, pos());
45   aGridLayout->addWidget(myStatus, 2, 0);
46   aGridLayout->addMultiCellWidget(myTime, 2, 2, 1, 2);
47
48   myPopup->insertSeparator();
49   myPortsItem = myPopup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
50
51   adjustSize();
52   setShape();
53   show();
54 }  
55
56
57 void SUPERVGUI_StartControlNode::switchPorts() {
58   bool aIsVisible = myPortsBox->isVisible();
59   if (aIsVisible) myPortsBox->hide();
60   else myPortsBox->show();
61   updateShape();
62   myPopup->setItemChecked(myPortsItem, aIsVisible);
63 }
64
65
66 void SUPERVGUI_StartControlNode::hideAll() {
67   myPortsBox->hide();
68   updateShape();
69   myPopup->setItemChecked(myPortsItem, true);
70 }
71
72
73 void SUPERVGUI_StartControlNode::showAll() {
74   myPortsBox->show();
75   updateShape();
76   myPopup->setItemChecked(myPortsItem, false);
77 }
78   
79
80 void SUPERVGUI_StartControlNode::rename() {
81   SUPERVGUI_Node::rename();
82   QString aStr(tr("ENDNODE_PREFIX"));
83   aStr+=name();
84   myEndNode->setNodeName(aStr);
85 }
86
87
88 void SUPERVGUI_StartControlNode::remove() {
89   myEndNode->deleteLinks();
90   deleteLinks();
91   myNode->destroy();
92   myMain->getGraph()->deleteNode(myEndNode);
93   myMain->getGraph()->deleteNode(this);
94 }
95
96
97 void SUPERVGUI_StartControlNode::setEndNode(SUPERVGUI_EndControlNode* theEndNode) { 
98   myEndNode = theEndNode; 
99 }
100
101 SUPERVGUI_EndControlNode* SUPERVGUI_StartControlNode::getEndNode() {
102   return myEndNode;
103 }
104
105
106 void SUPERVGUI_StartControlNode::setShape() {
107   int aH = height();
108   int aW = width();
109   int aOffset = LABEL_HEIGHT/2;
110   QPointArray aArray(8);
111   aArray.setPoint(0, aOffset, aH);
112   aArray.setPoint(1, 0, aH-LABEL_HEIGHT);
113   aArray.setPoint(2, 0, LABEL_HEIGHT);
114   aArray.setPoint(3, aOffset, 0);
115   aArray.setPoint(4, aW, 0);
116   aArray.setPoint(5, aW-aOffset, LABEL_HEIGHT);
117   aArray.setPoint(6, aW-aOffset, aH-LABEL_HEIGHT);
118   aArray.setPoint(7, aW, aH);
119   QRegion aRegion(aArray);
120   
121   setMask(aRegion);
122 }
123
124
125 void SUPERVGUI_StartControlNode::updateShape() {
126   qApp->processEvents();
127   adjustSize();
128   setShape();
129 }
130
131
132 void SUPERVGUI_StartControlNode::addOutputPort() {
133   SUPERVGUI_GraphNode::addOutputPort();
134   myEndNode->updatePorts();
135   myEndNode->updateShape();
136   updateLinksPrs(true);
137   myEndNode->updateLinksPrs(true);
138 }
139
140 void SUPERVGUI_StartControlNode::addInputPort() {
141   SUPERVGUI_GraphNode::addInputPort();
142   myEndNode->updatePorts();
143   myEndNode->updateShape();
144   updateLinksPrs(true);
145   myEndNode->updateLinksPrs(true);
146 }
147
148 void SUPERVGUI_StartControlNode::deletePort(SUPERVGUI_Port* thePort) {
149   if (getNodeType() == SUPERV::LoopNode) {
150     
151     SUPERV_Port aPortEngine = thePort->getPort();
152     QString aName(aPortEngine->Name());
153     QString aNameIn = aName + "Input";
154     QString aNameOut = aName + "Output";
155
156     SUPERVGUI_Port* aPort = (SUPERVGUI_Port*) child(aNameOut, "SUPERVGUI_Port");
157     aPort->deleteLinks();
158     aPort->close(true);
159
160     aPort = (SUPERVGUI_Port*) child(aNameIn, "SUPERVGUI_Port");
161     aPort->deleteLinks();
162     aPort->close(true);
163
164     aPort = (SUPERVGUI_Port*) myEndNode->child(aNameIn, "SUPERVGUI_Port");
165     aPort->deleteLinks();
166     aPort->close(true);
167
168     aPort = (SUPERVGUI_Port*) myEndNode->child(aNameOut, "SUPERVGUI_Port");
169     aPort->deleteLinks();
170     aPort->close(true);
171
172     myPIcount--;
173     myPOcount--;
174     
175     myEndNode->getPIcount()--;
176     myEndNode->getPOcount()--; 
177     
178     aPortEngine->destroy();
179
180     updatePorts();
181     updateShape();
182     myEndNode->updatePorts();
183     myEndNode->updateShape(); 
184   } else {
185     SUPERVGUI_GraphNode::deletePort(thePort);
186   }
187 }
188
189
190 QPoint SUPERVGUI_StartControlNode::getOutConnectPnt() {
191   return QPoint(pos().x() + width() -  LABEL_HEIGHT/2,
192                 pos().y() + (height()/2));
193 }
194
195
196 //-----------------------------------------------------------
197 //******************* End Node ******************************
198 //-----------------------------------------------------------
199
200 SUPERVGUI_EndControlNode::SUPERVGUI_EndControlNode(QWidget* theParent, SUPERVGUI_Main* theMain, 
201                                                    SUPERV_CNode theNode, SUPERVGUI_StartControlNode* theStart):
202   SUPERVGUI_GraphNode(theParent, theMain, theNode) 
203 {
204   myStartNode = theStart;
205
206   setPaletteBackgroundColor(BACKCOLOR);
207
208   QGridLayout* aGridLayout = new QGridLayout(this, 0, 3, 0, 0);
209   aGridLayout->setColStretch(1, 1);
210   aGridLayout->setColStretch(2, 1);
211   aGridLayout->addColSpacing(0, LABEL_HEIGHT/2);
212
213   myTitle->setAlignment(Qt::AlignHCenter);
214   myTitle->reparent(this, pos());
215   myTitle->setPaletteBackgroundColor(TITLECOLOR);
216   aGridLayout->addMultiCellWidget(myTitle, 0, 0, 0, 2);
217
218   myPortsBox->reparent(this, pos());  
219   
220   aGridLayout->addMultiCellWidget(myPortsBox, 1, 1, 1, 2);
221   
222   myStatus->reparent(this, pos());
223   myTime->reparent(this, pos());
224   aGridLayout->addMultiCellWidget(myStatus, 2, 2, 0, 1);
225   aGridLayout->addWidget(myTime, 2, 2);
226
227   adjustSize();
228   setShape();
229
230   myPopup->removeItem(myRenameItem);
231   myPopup->removeItem(myDeleteItem);
232   myPopup->removeItem(mySeparatorId);
233
234   myPopup->insertSeparator();
235   myPortsItem = myPopup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
236   myStartNode->setEndNode(this);
237   show();
238 }  
239
240
241 void SUPERVGUI_EndControlNode::switchPorts() {
242   bool aIsVisible = myPortsBox->isVisible();
243   if (aIsVisible) myPortsBox->hide();
244   else myPortsBox->show();
245   updateShape();
246   myPopup->setItemChecked(myPortsItem, aIsVisible);
247 }
248
249 void SUPERVGUI_EndControlNode::hideAll() {
250   myPortsBox->hide();
251   updateShape();
252   myPopup->setItemChecked(myPortsItem, true);
253 }
254
255
256 void SUPERVGUI_EndControlNode::showAll() {
257   myPortsBox->show();
258   updateShape();
259   myPopup->setItemChecked(myPortsItem, false);
260 }
261   
262
263 void SUPERVGUI_EndControlNode::setShape() {
264   int aH = height();
265   int aW = width();
266   int aOffset = LABEL_HEIGHT/2;
267   QPointArray aArray(8);
268   aArray.setPoint(0, 0, aH);
269   aArray.setPoint(1, aOffset, aH-LABEL_HEIGHT);
270   aArray.setPoint(2, aOffset, LABEL_HEIGHT);
271   aArray.setPoint(3, 0, 0);
272   aArray.setPoint(4, aW-aOffset, 0);
273   aArray.setPoint(5, aW, LABEL_HEIGHT);
274   aArray.setPoint(6, aW, aH-LABEL_HEIGHT);
275   aArray.setPoint(7, aW-aOffset, aH);
276   QRegion aRegion(aArray);
277   
278   setMask(aRegion);
279 }
280
281
282 void SUPERVGUI_EndControlNode::updateShape() {
283   qApp->processEvents();
284   adjustSize();
285   setShape();
286 }
287
288
289 QPoint SUPERVGUI_EndControlNode::getInConnectPnt() {
290   return QPoint(pos().x() + LABEL_HEIGHT/2, 
291                 pos().y() + (height()/2));
292 }
293
294
295 //-----------------------------------------------------------
296 //******************* GoTo Node *****************************
297 //-----------------------------------------------------------
298 SUPERVGUI_GotoNode::SUPERVGUI_GotoNode(QWidget* theParent, SUPERVGUI_Main* theMain, 
299                                        SUPERV_CNode theNode):  
300   SUPERVGUI_GraphNode(theParent, theMain, theNode) 
301 {
302   setPaletteBackgroundColor(BACKCOLOR);
303
304   QGridLayout* aGridLayout = new QGridLayout(this, 3, 2, 0, 0);
305
306   myTitle->setAlignment(Qt::AlignHCenter);
307   myTitle->reparent(this, pos());
308   myTitle->setPaletteBackgroundColor(Qt::green.light());
309   aGridLayout->addMultiCellWidget(myTitle, 0, 0, 0, 1);
310
311   myPortsBox->reparent(this, pos());
312   aGridLayout->addMultiCellWidget(myPortsBox, 1, 1, 0, 1);
313
314   myStatus->reparent(this, pos());
315   myTime->reparent(this, pos());
316   aGridLayout->addWidget(myStatus, 2, 0);
317   aGridLayout->addWidget(myTime, 2, 1);
318
319   myPopup->insertSeparator();
320   myPopup->insertItem("Link to Node...", this, SLOT(linkToNode()));
321
322   adjustSize();
323   setShape();
324   show();
325
326   //SUPERV_Ports aPorts = getEngine()->Ports(); //existing
327   //int n = aPorts->length();
328 }
329
330
331 void SUPERVGUI_GotoNode::setShape() {
332   int aH = height();
333   int aW = width();
334   QPointArray aArray(8);
335   aArray.setPoint(0, LABEL_HEIGHT, aH);
336   aArray.setPoint(1, 0, aH-LABEL_HEIGHT);
337   aArray.setPoint(2, 0, LABEL_HEIGHT);
338   aArray.setPoint(3, LABEL_HEIGHT, 0);
339   aArray.setPoint(4, aW-LABEL_HEIGHT, 0);
340   aArray.setPoint(5, aW, LABEL_HEIGHT);
341   aArray.setPoint(6, aW, aH-LABEL_HEIGHT);
342   aArray.setPoint(7, aW-LABEL_HEIGHT, aH);
343   QRegion aRegion(aArray);
344   
345   setMask(aRegion);
346 }
347
348
349 void SUPERVGUI_GotoNode::hideAll() {
350   myPortsBox->hide();
351   updateShape();
352   //myPopup->setItemChecked(myPortsItem, true);
353 }
354
355
356 void SUPERVGUI_GotoNode::showAll() {
357   myPortsBox->show();
358   updateShape();
359   //myPopup->setItemChecked(myPortsItem, false);
360 }
361   
362 void SUPERVGUI_GotoNode::updateShape() {
363   qApp->processEvents();
364   adjustSize();
365   setShape();
366 }
367
368
369 void SUPERVGUI_GotoNode::linkToNode() {
370   SUPERVGUI_SelectInlineDlg* aDlg = new SUPERVGUI_SelectInlineDlg(getMain());
371   if (aDlg->exec()) {
372     setLinkedNode((char*)aDlg->getName().latin1());
373     myMain->getGraph()->sync();
374   }
375   delete aDlg;
376 }
377
378
379 void SUPERVGUI_GotoNode::setLinkedNode(char* theNodeName) {
380   getGotoNode()->SetCoupled(theNodeName);
381 }
382
383
384
385 //-----------------------------------------------------------
386 //*************** Select Inline node dialog******************
387 //-----------------------------------------------------------
388
389 SUPERVGUI_SelectInlineDlg::SUPERVGUI_SelectInlineDlg(SUPERVGUI_Main* theMain)  
390   :QDialog(theMain, 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
391 {
392   setSizeGripEnabled( true );
393   setCaption(tr("TIT_FUNC_PYTHON"));
394   QGridLayout* aMainLayout = new QGridLayout(this, 2, 2, 7, 4);
395
396   QLabel* aLabel = new QLabel("Connect to", this );
397   aMainLayout->addWidget(aLabel, 0, 0);
398
399   myCombo = new QComboBox(this);
400   myCombo->clear(); //for the additional check from GUI side for bug PAL7007
401   SUPERV_Nodes aNodesList = theMain->getDataflow()->Nodes();  
402   int i;
403   for (i = 0; i < aNodesList->INodes.length(); i++) {
404     myCombo->insertItem(QString(aNodesList->INodes[i]->Name()));
405   }
406   for (i = 0; i < aNodesList->LNodes.length(); i++) {
407     myCombo->insertItem(QString(aNodesList->LNodes[i]->Name()));
408   }
409   for (i = 0; i < aNodesList->SNodes.length(); i++) {
410     myCombo->insertItem(QString(aNodesList->SNodes[i]->Name()));
411   }
412   aMainLayout->addWidget(myCombo, 0, 1);
413
414   QGroupBox* aBtnBox = new QGroupBox( this );
415   aBtnBox->setColumnLayout( 0, Qt::Vertical );
416   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
417   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
418   aBtnLayout->setAlignment( Qt::AlignTop );
419   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
420   
421   QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
422   connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
423   aBtnLayout->addWidget( aOKBtn );
424
425   aBtnLayout->addStretch();
426
427   QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
428   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
429   aBtnLayout->addWidget( aCancelBtn );
430
431   aMainLayout->addMultiCellWidget(aBtnBox, 1, 1, 0, 1);
432 }