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