Salome HOME
SMH: 3.0.0 preparation = merged version (POLYWORK + RTVDEBUG01) + adopation for new GUI
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasControlNode.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE
4 //
5 //  File   : SUPERVGUI_CanvasControlNode.cxx
6 //  Author : Natalia KOPNOVA
7 //  Module : SUPERV
8
9 using namespace std;
10 #include "SUPERVGUI_CanvasControlNode.h"
11 #include "SUPERVGUI_CanvasControlNodePrs.h"
12 #include "SUPERVGUI_CanvasCellNodePrs.h"
13 #include "SUPERVGUI_Clipboard.h"
14 #include "SUPERVGUI_Main.h"
15 #include "SUPERVGUI.h"
16 #include "SUPERVGUI_Canvas.h"
17
18 #include "SUIT_FileDlg.h"
19 #include "SUIT_Session.h"
20
21 #include <qlabel.h>
22 #include <qlayout.h>
23
24 //=====================================================================
25 // Compute node
26 //=====================================================================
27 SUPERVGUI_CanvasComputeNode::SUPERVGUI_CanvasComputeNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
28   SUPERVGUI_CanvasNode(theParent, theMain, theNode)
29 {
30   Trace("SUPERVGUI_CanvasComputeNode::SUPERVGUI_CanvasComputeNode");
31 }
32
33 QPopupMenu* SUPERVGUI_CanvasComputeNode::getPopupMenu(QWidget* theParent) 
34 {
35   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
36
37   QPopupMenu* aShowPopup = new QPopupMenu(theParent);
38   int anItem;
39   anItem = aShowPopup->insertItem(tr("POP_SHOWTITLES"), this, SLOT(switchLabel()));
40   aShowPopup->setItemChecked(anItem, getPrs()->isLabelVisible());
41   anItem = aShowPopup->insertItem(tr("POP_SHOWPORTS"), this, SLOT(switchPorts()));
42   aShowPopup->setItemChecked(anItem, getPrs()->isPortVisible());
43
44   popup->insertSeparator();
45   popup->insertItem(tr("POP_SHOW"), aShowPopup);
46
47   return popup;
48 }
49
50
51 //=====================================================================
52 // Start control node
53 //=====================================================================
54 SUPERVGUI_CanvasStartNode::SUPERVGUI_CanvasStartNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
55   SUPERVGUI_CanvasNode(theParent, theMain, theNode), 
56   myCoupled(0)
57 {
58   Trace("SUPERVGUI_CanvasStartNode::SUPERVGUI_CanvasStartNode");
59 }
60
61 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasStartNode::createPrs() const
62 {
63   return new SUPERVGUI_CanvasStartNodePrs(getMain()->getCanvas(), 
64                                           (SUPERVGUI_CanvasStartNode*)this);
65 }
66
67 bool SUPERVGUI_CanvasStartNode::setNodeName(QString aName)
68 {
69   bool result = SUPERVGUI_CanvasNode::setNodeName(aName);
70   if (result && myCoupled) {
71     myCoupled->setNodeName(QString(tr("ENDNODE_PREFIX"))+aName);
72   }
73   return result;
74 }
75
76 QPopupMenu* SUPERVGUI_CanvasStartNode::getPopupMenu(QWidget* theParent) 
77 {
78   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
79
80   popup->insertSeparator();
81   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
82   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
83
84   return popup;
85 }
86
87 void SUPERVGUI_CanvasStartNode::remove()
88 {
89   SUPERVGUI_Canvas* aCanvas = getMain()->getCanvas();
90
91   setDestroyed();
92   SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
93   if (myCoupled) {
94
95     //set myCopyPort from Main object to empty if engine of this port is deleted
96     //check if any port of this deleted node has been copied
97     if ( aCB->isCopyPort() )
98       if ( QString(getEngine()->Name()) == QString(aCB->getCopyPort()->Node()->Name())    ||
99            QString(myCoupled->getEngine()->Name()) == QString(aCB->getCopyPort()->Node()->Name()) )
100         aCB->setCopyPort( 0 );
101     
102     myCoupled->setDestroyed();
103   }
104
105   //set myCopyNode from Main object to empty if engine of this node is deleted
106   //check if myCopyNode and this node engine is equal
107   if ( aCB->isCopyNode() )
108     if ( QString(getEngine()->Name()) == QString(aCB->getCopyNode()->Name()) ) 
109       aCB->setCopyNode( 0 );
110
111   // mkr: since the deletion of the node allow only in CANVAS view,
112   // it is necessary to remove the CanvasArray's children, which
113   // have the same CNode engine as deleting node
114   getMain()->removeArrayChild(getEngine());
115   getMain()->removeArrayChild(myCoupled->getEngine());
116
117   getEngine()->destroy();
118
119   if (myCoupled) 
120     delete myCoupled;
121
122   delete this;
123
124   aCanvas->update();
125 }
126
127 void SUPERVGUI_CanvasStartNode::onDestroyed(QObject* theObject)
128 {
129   if (!isIgnore) {
130     SUPERVGUI_CanvasNode::onDestroyed(theObject);
131     if (getEngine()->IsLoop()) merge();
132     if (myCoupled) myCoupled->merge();
133   }
134 }
135
136 void SUPERVGUI_CanvasStartNode::addInputPort()
137 {
138   SUPERVGUI_CanvasNode::addInputPort();
139   if (getEngine()->IsLoop()) merge();
140   if (myCoupled) myCoupled->merge();
141 }
142
143 void SUPERVGUI_CanvasStartNode::addOutputPort()
144 {
145   SUPERVGUI_CanvasNode::addOutputPort();
146   if (myCoupled) myCoupled->merge();
147 }
148  
149 void SUPERVGUI_CanvasStartNode::pastePort()
150 {
151   SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
152   SUPERVGUI_CanvasNode::pastePort();
153   if ( aCB->getCopyPort()->IsInput() && getEngine()->IsLoop() ) 
154     merge();
155   if ( myCoupled )
156     myCoupled->merge();
157 }
158
159 //=====================================================================
160 // End control node
161 //=====================================================================
162 SUPERVGUI_CanvasEndNode::SUPERVGUI_CanvasEndNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode,
163                                                  SUPERVGUI_CanvasStartNode* theStart):
164   SUPERVGUI_CanvasNode(theParent, theMain, theNode), 
165   myCoupled(theStart)
166 {
167   Trace("SUPERVGUI_CanvasEndNode::SUPERVGUI_CanvasEndNode");
168   myCoupled->setCoupled(this);
169 }
170
171 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasEndNode::createPrs() const
172 {
173   return new SUPERVGUI_CanvasEndNodePrs(getMain()->getCanvas(), 
174                                         (SUPERVGUI_CanvasEndNode*)this);
175 }
176
177 QPopupMenu* SUPERVGUI_CanvasEndNode::getPopupMenu(QWidget* theParent) 
178 {
179   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
180
181   popup->insertSeparator();
182   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
183   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
184
185   return popup;
186 }
187
188 void SUPERVGUI_CanvasEndNode::addInputPort()
189 {
190   SUPERVGUI_CanvasNode::addInputPort();
191   if (getEngine()->IsEndSwitch()) merge();
192 }
193
194
195 //=====================================================================
196 // Goto control node
197 //=====================================================================
198 SUPERVGUI_CanvasGotoNode::SUPERVGUI_CanvasGotoNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
199   SUPERVGUI_CanvasNode(theParent, theMain, theNode)
200 {
201   Trace("SUPERVGUI_CanvasGotoNode::SUPERVGUI_CanvasGotoNode");
202 }
203
204 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasGotoNode::createPrs() const
205 {
206   return new SUPERVGUI_CanvasGotoNodePrs(getMain()->getCanvas(), 
207                                          (SUPERVGUI_CanvasGotoNode*)this);
208 }
209
210 QPopupMenu* SUPERVGUI_CanvasGotoNode::getPopupMenu(QWidget* theParent) 
211 {
212   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
213
214   popup->insertSeparator();
215   popup->insertItem("Link to Node...", this, SLOT(linkToNode()));
216
217   popup->insertSeparator();
218   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
219   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
220
221   return popup;
222 }
223
224 void SUPERVGUI_CanvasGotoNode::linkToNode() {
225   SUPERVGUI_SelectInlineDlg* aDlg = new SUPERVGUI_SelectInlineDlg(getMain());
226   if (aDlg->exec()) {
227     QString aNodeName = aDlg->getName();
228     if (!aNodeName.isEmpty()) { //implement additional check from GUI side for bug PAL7007
229       getGotoNode()->SetCoupled(aNodeName.latin1());
230       getMain()->getCanvas()->sync();
231     }
232   }
233   delete aDlg;
234 }
235
236 //=====================================================================
237 // Macro node
238 //=====================================================================
239 SUPERVGUI_CanvasMacroNode::SUPERVGUI_CanvasMacroNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
240   SUPERVGUI_CanvasComputeNode(theParent, theMain, theNode)
241 {
242   Trace("SUPERVGUI_CanvasMacroNode::SUPERVGUI_CanvasMacroNode");
243 }
244
245 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasMacroNode::createPrs() const
246 {
247   return new SUPERVGUI_CanvasMacroNodePrs(getMain()->getCanvas(), 
248                                           (SUPERVGUI_CanvasMacroNode*)this);
249 }
250
251 QPopupMenu* SUPERVGUI_CanvasMacroNode::getPopupMenu(QWidget* theParent) 
252 {
253   QPopupMenu* popup = SUPERVGUI_CanvasComputeNode::getPopupMenu(theParent);
254
255   popup->insertSeparator(0);
256   popup->insertItem(tr("MSG_EXPORT"), this, SLOT(exportDataflow()), 0, -1, 0);
257   popup->insertItem("Open SubGraph", this, SLOT(openSubGraph()), 0, -1, 0);
258
259   return popup;
260 }
261
262 void SUPERVGUI_CanvasMacroNode::openSubGraph()
263 {
264   getMain()->openSubGraph(getEngine());
265 }
266
267 void SUPERVGUI_CanvasMacroNode::exportDataflow()
268 {
269   SUPERV_Graph aGraph;
270   if (getEngine()->IsMacro()) {
271     SUPERV_Graph aMacro = getMacroNode();
272     if (aMacro->IsStreamMacro())
273       aGraph = aMacro->StreamObjRef();
274     else
275       aGraph = aMacro->FlowObjRef();
276   }
277   if (SUPERV_isNull(aGraph)) {
278     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
279     return;
280   }
281   else {
282     QString aFileName = SUIT_FileDlg::getFileName(SUIT_Session::session()->activeApplication()->desktop(),
283                                                   "",
284                                                   "*.xml",
285                                                   tr("TTL_EXPORT_DATAFLOW"),
286                                                   false);
287     if ( !aFileName.isEmpty() ) {
288       // asv : bug [VSR Bugs and Improvements in Supervisor] 1.8 : when exporting a file, 
289       // a backup copy of an existing file must be created (in case Export fails..)
290       QString aBackupFile = SUPERVGUI::createBackupFile( aFileName );
291
292       if (!aGraph->Export(aFileName.latin1())) {
293         QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_BAD_WRITING").arg(aBackupFile));
294       }
295       // remove a backup file if export was successfull
296       else if ( !aBackupFile.isNull() && !aBackupFile.isEmpty() ) {
297         QFile::remove( aBackupFile );
298       }
299     }
300   }
301 }
302
303 //=====================================================================
304 // Cell node: node for table view
305 //=====================================================================
306 SUPERVGUI_CanvasCellNode::SUPERVGUI_CanvasCellNode(QObject* theParent, 
307                                                    SUPERVGUI_Main* theMain, 
308                                                    SUPERV_CNode theNode):
309   SUPERVGUI_CanvasNode(theParent, theMain, theNode, true)
310 {
311   Trace("SUPERVGUI_CanvasCellNode::SUPERVGUI_CanvasCellNode");
312   
313   myIsControl = false;
314   myIsStart = false;
315
316   //check for control nodes
317   if (getEngine()->IsLoop() || getEngine()->IsSwitch()) {
318     myIsControl = true;  
319     myIsStart = true;
320   }
321   if (getEngine()->IsEndLoop() || getEngine()->IsEndSwitch())
322     myIsControl = true;
323 }
324
325 SUPERVGUI_CanvasCellNode::~SUPERVGUI_CanvasCellNode()
326 {
327 }
328 /*
329 QPopupMenu* SUPERVGUI_CanvasCellNode::getPopupMenu(QWidget* theParent)
330 {
331   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
332   popup->setItemEnabled(myDeleteItem, false);
333   return popup;
334 }
335 */
336 void SUPERVGUI_CanvasCellNode::setPairCell(SUPERVGUI_CanvasCellNode* thePairCell) {
337   if (myIsControl) { //only for ControlNode
338     myPairCell = thePairCell;
339   }
340   else 
341     myPairCell = 0;
342 }
343
344 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasCellNode::getPairCell() {
345   return myPairCell;
346 }
347
348 void SUPERVGUI_CanvasCellNode::sync() {
349   const bool isExecuting = getMain()->getDataflow()->IsExecuting();
350   //if getEngine() is a MacroNode then set it state to state of its subgraph
351   if ( getEngine()->IsMacro() && isExecuting ) {
352     // get SubGraph from MacroNode
353     SUPERV_Graph aMacro = SUPERV::Graph::_narrow(getEngine());
354     if (!SUPERV_isNull(aMacro)) {
355       SUPERV_Graph aGraph;
356       if (aMacro->IsStreamMacro())
357         aGraph = aMacro->StreamObjRef();
358       else
359         aGraph = aMacro->FlowObjRef();
360       if (!SUPERV_isNull(aGraph)) {
361         if (aGraph->State() != SUPERV::UndefinedState && aGraph->State() != SUPERV::NoState)
362           getPrs()->setState(aGraph->State());
363         else 
364           getPrs()->setState(getEngine()->State());
365       }
366     }
367   }
368   else {
369     getPrs()->setState(getEngine()->State());
370   }
371 }
372 /*
373 bool SUPERVGUI_CanvasCellNode::setNodeName(QString aName) 
374 {
375   bool result = SUPERVGUI_CanvasNode::setNodeName(aName);
376   if (result && myPairCell) {
377     result = myPairCell->setNodeName(QString(tr("ENDNODE_PREFIX"))+aName);
378   }
379   return result;
380 }
381 */
382 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasCellNode::createPrs() const
383 {
384   SUPERVGUI_CanvasNodePrs* aPrs = 
385     new SUPERVGUI_CanvasCellNodePrs(getMain()->getCanvasArray(), 
386                                     (SUPERVGUI_CanvasCellNode*)this);
387   return aPrs;
388 }
389
390 SUPERVGUI_CanvasCellEndNode::SUPERVGUI_CanvasCellEndNode( QObject* theParent, 
391                                                           SUPERVGUI_Main* theMain, 
392                                                           SUPERV_CNode theNode, 
393                                                           SUPERVGUI_CanvasCellNode* theStart):
394   SUPERVGUI_CanvasCellNode(theParent, theMain, theNode)
395 {
396   //set start cell for end cell as pair 
397   myPairCell = theStart; 
398   //set end cell for start cell as pair
399   myPairCell->setPairCell(dynamic_cast<SUPERVGUI_CanvasCellNode*>(this));
400 }
401 /*
402 bool SUPERVGUI_CanvasCellEndNode::setNodeName(QString theName)
403 {
404   return SUPERVGUI_CanvasNode::setNodeName(theName);
405 }
406 */
407 //-----------------------------------------------------------
408 //*************** Select Inline node dialog******************
409 // Taken from SUPERVGUI_ControlNode.cxx without change
410 //-----------------------------------------------------------
411
412 SUPERVGUI_SelectInlineDlg::SUPERVGUI_SelectInlineDlg(SUPERVGUI_Main* theMain)  
413   :QDialog(theMain, 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
414 {
415   setSizeGripEnabled( true );
416   setCaption(tr("TIT_FUNC_PYTHON"));
417   QGridLayout* aMainLayout = new QGridLayout(this, 2, 2, 7, 4);
418
419   QLabel* aLabel = new QLabel("Connect to", this );
420   aMainLayout->addWidget(aLabel, 0, 0);
421
422   myCombo = new QComboBox(this);
423   myCombo->clear(); //for the additional check from GUI side for bug PAL7007
424   SUPERV_Nodes aNodesList = theMain->getDataflow()->Nodes();  
425   int i;
426   for (i = 0; i < aNodesList->INodes.length(); i++) {
427     myCombo->insertItem(QString(aNodesList->INodes[i]->Name()));
428   }
429   for (i = 0; i < aNodesList->LNodes.length(); i++) {
430     myCombo->insertItem(QString(aNodesList->LNodes[i]->Name()));
431   }
432   for (i = 0; i < aNodesList->SNodes.length(); i++) {
433     myCombo->insertItem(QString(aNodesList->SNodes[i]->Name()));
434   }
435   aMainLayout->addWidget(myCombo, 0, 1);
436
437   QGroupBox* aBtnBox = new QGroupBox( this );
438   aBtnBox->setColumnLayout( 0, Qt::Vertical );
439   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
440   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
441   aBtnLayout->setAlignment( Qt::AlignTop );
442   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
443   
444   QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
445   connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
446   aBtnLayout->addWidget( aOKBtn );
447
448   aBtnLayout->addStretch();
449
450   QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
451   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
452   aBtnLayout->addWidget( aCancelBtn );
453
454   aMainLayout->addMultiCellWidget(aBtnBox, 1, 1, 0, 1);
455 }