]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_CanvasControlNode.cxx
Salome HOME
The following bug was fixed:
[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 "QAD_FileDlg.h"
19 #include "QAD_Application.h"
20
21 #include <qlayout.h>
22
23 //=====================================================================
24 // Compute node
25 //=====================================================================
26 SUPERVGUI_CanvasComputeNode::SUPERVGUI_CanvasComputeNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
27   SUPERVGUI_CanvasNode(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(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
54   SUPERVGUI_CanvasNode(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(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   getEngine()->destroy();
111
112   if (myCoupled) 
113     delete myCoupled;
114
115   delete this;
116
117   aCanvas->update();
118 }
119
120 void SUPERVGUI_CanvasStartNode::onDestroyed(QObject* theObject)
121 {
122   if (!isIgnore) {
123     SUPERVGUI_CanvasNode::onDestroyed(theObject);
124     if (getEngine()->IsLoop()) merge();
125     if (myCoupled) myCoupled->merge();
126   }
127 }
128
129 void SUPERVGUI_CanvasStartNode::addInputPort()
130 {
131   SUPERVGUI_CanvasNode::addInputPort();
132   if (getEngine()->IsLoop()) merge();
133   if (myCoupled) myCoupled->merge();
134 }
135
136 void SUPERVGUI_CanvasStartNode::addOutputPort()
137 {
138   SUPERVGUI_CanvasNode::addOutputPort();
139   if (myCoupled) myCoupled->merge();
140 }
141  
142 void SUPERVGUI_CanvasStartNode::pastePort()
143 {
144   SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
145   SUPERVGUI_CanvasNode::pastePort();
146   if ( aCB->getCopyPort()->IsInput() && getEngine()->IsLoop() ) 
147     merge();
148   if ( myCoupled )
149     myCoupled->merge();
150 }
151
152 //=====================================================================
153 // End control node
154 //=====================================================================
155 SUPERVGUI_CanvasEndNode::SUPERVGUI_CanvasEndNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode,
156                                                  SUPERVGUI_CanvasStartNode* theStart):
157   SUPERVGUI_CanvasNode(theParent, theMain, theNode), 
158   myCoupled(theStart)
159 {
160   Trace("SUPERVGUI_CanvasEndNode::SUPERVGUI_CanvasEndNode");
161   myCoupled->setCoupled(this);
162 }
163
164 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasEndNode::createPrs() const
165 {
166   return new SUPERVGUI_CanvasEndNodePrs(getMain()->getCanvas(), 
167                                         (SUPERVGUI_CanvasEndNode*)this);
168 }
169
170 QPopupMenu* SUPERVGUI_CanvasEndNode::getPopupMenu(QWidget* theParent) 
171 {
172   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
173
174   popup->insertSeparator();
175   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
176   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
177
178   return popup;
179 }
180
181 void SUPERVGUI_CanvasEndNode::addInputPort()
182 {
183   SUPERVGUI_CanvasNode::addInputPort();
184   if (getEngine()->IsEndSwitch()) merge();
185 }
186
187
188 //=====================================================================
189 // Goto control node
190 //=====================================================================
191 SUPERVGUI_CanvasGotoNode::SUPERVGUI_CanvasGotoNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
192   SUPERVGUI_CanvasNode(theParent, theMain, theNode)
193 {
194   Trace("SUPERVGUI_CanvasGotoNode::SUPERVGUI_CanvasGotoNode");
195 }
196
197 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasGotoNode::createPrs() const
198 {
199   return new SUPERVGUI_CanvasGotoNodePrs(getMain()->getCanvas(), 
200                                          (SUPERVGUI_CanvasGotoNode*)this);
201 }
202
203 QPopupMenu* SUPERVGUI_CanvasGotoNode::getPopupMenu(QWidget* theParent) 
204 {
205   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
206
207   popup->insertSeparator();
208   popup->insertItem("Link to Node...", this, SLOT(linkToNode()));
209
210   popup->insertSeparator();
211   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
212   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
213
214   return popup;
215 }
216
217 void SUPERVGUI_CanvasGotoNode::linkToNode() {
218   SUPERVGUI_SelectInlineDlg* aDlg = new SUPERVGUI_SelectInlineDlg(getMain());
219   if (aDlg->exec()) {
220     QString aNodeName = aDlg->getName();
221     if (!aNodeName.isEmpty()) { //implement additional check from GUI side for bug PAL7007
222       getGotoNode()->SetCoupled(aNodeName.latin1());
223       getMain()->getCanvas()->sync();
224     }
225   }
226   delete aDlg;
227 }
228
229 //=====================================================================
230 // Macro node
231 //=====================================================================
232 SUPERVGUI_CanvasMacroNode::SUPERVGUI_CanvasMacroNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
233   SUPERVGUI_CanvasComputeNode(theParent, theMain, theNode)
234 {
235   Trace("SUPERVGUI_CanvasMacroNode::SUPERVGUI_CanvasMacroNode");
236 }
237
238 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasMacroNode::createPrs() const
239 {
240   return new SUPERVGUI_CanvasMacroNodePrs(getMain()->getCanvas(), 
241                                           (SUPERVGUI_CanvasMacroNode*)this);
242 }
243
244 QPopupMenu* SUPERVGUI_CanvasMacroNode::getPopupMenu(QWidget* theParent) 
245 {
246   QPopupMenu* popup = SUPERVGUI_CanvasComputeNode::getPopupMenu(theParent);
247
248   popup->insertSeparator(0);
249   popup->insertItem(tr("MSG_EXPORT"), this, SLOT(exportDataflow()), 0, -1, 0);
250   popup->insertItem("Open SubGraph", this, SLOT(openSubGraph()), 0, -1, 0);
251
252   return popup;
253 }
254
255 void SUPERVGUI_CanvasMacroNode::openSubGraph()
256 {
257   getMain()->openSubGraph(getEngine());
258 }
259
260 void SUPERVGUI_CanvasMacroNode::exportDataflow()
261 {
262   SUPERV_Graph aGraph;
263   if (getEngine()->IsMacro()) {
264     SUPERV_Graph aMacro = getMacroNode();
265     if (aMacro->IsStreamMacro())
266       aGraph = aMacro->StreamObjRef();
267     else
268       aGraph = aMacro->FlowObjRef();
269   }
270   if (SUPERV_isNull(aGraph)) {
271     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
272     return;
273   }
274   else {
275     QString aFileName = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
276                                                  "",
277                                                  "*.xml",
278                                                  tr("TTL_EXPORT_DATAFLOW"),
279                                                  false);
280     if ( !aFileName.isEmpty() ) {
281       // asv : bug [VSR Bugs and Improvements in Supervisor] 1.8 : when exporting a file, 
282       // a backup copy of an existing file must be created (in case Export fails..)
283       QString aBackupFile = SUPERVGUI::createBackupFile( aFileName );
284
285       if (!aGraph->Export(aFileName.latin1())) {
286         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING").arg(aBackupFile));
287       }
288       // remove a backup file if export was successfull
289       else if ( !aBackupFile.isNull() && !aBackupFile.isEmpty() ) {
290         QFile::remove( aBackupFile );
291       }
292     }
293   }
294 }
295
296 //=====================================================================
297 // Cell node: node for table view
298 //=====================================================================
299 SUPERVGUI_CanvasCellNode::SUPERVGUI_CanvasCellNode(QObject* theParent, 
300                                                    SUPERVGUI_Main* theMain, 
301                                                    SUPERV_CNode theNode):
302   SUPERVGUI_CanvasNode(theParent, theMain, theNode, true)
303 {
304   Trace("SUPERVGUI_CanvasCellNode::SUPERVGUI_CanvasCellNode");
305   
306   myIsControl = false;
307   myIsStart = false;
308
309   //check for control nodes
310   if (getEngine()->IsLoop() || getEngine()->IsSwitch()) {
311     myIsControl = true;  
312     myIsStart = true;
313   }
314   if (getEngine()->IsEndLoop() || getEngine()->IsEndSwitch())
315     myIsControl = true;
316 }
317
318 QPopupMenu* SUPERVGUI_CanvasCellNode::getPopupMenu(QWidget* theParent)
319 {
320   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
321   popup->setItemEnabled(myDeleteItem, false);
322   return popup;
323 }
324
325 void SUPERVGUI_CanvasCellNode::setPairCell(SUPERVGUI_CanvasCellNode* thePairCell) {
326   if (myIsControl) { //only for ControlNode
327     myPairCell = thePairCell;
328   }
329   else 
330     myPairCell = 0;
331 }
332
333 SUPERVGUI_CanvasCellNode* SUPERVGUI_CanvasCellNode::getPairCell() {
334   return myPairCell;
335 }
336
337 void SUPERVGUI_CanvasCellNode::sync() {
338   MESSAGE("===> SUPERVGUI_CanvasCellNode::sync() " << getEngine()->Name() << ", state " << getEngine()->State());
339
340   const bool isExecuting = getMain()->getDataflow()->IsExecuting();
341   //if getEngine() is a MacroNode then set it state to state of its subgraph
342   if ( getEngine()->IsMacro() && isExecuting ) {
343     // get SubGraph from MacroNode
344     SUPERV_Graph aMacro = SUPERV::Graph::_narrow(getEngine());
345     if (!SUPERV_isNull(aMacro)) {
346       SUPERV_Graph aGraph;
347       if (aMacro->IsStreamMacro())
348         aGraph = aMacro->StreamObjRef();
349       else
350         aGraph = aMacro->FlowObjRef();
351       if (!SUPERV_isNull(aGraph)) {
352         if (aGraph->State() != SUPERV::UndefinedState && aGraph->State() != SUPERV::NoState)
353           getPrs()->setState(aGraph->State());
354         else 
355           getPrs()->setState(getEngine()->State());
356       }
357     }
358   }
359   else {
360     getPrs()->setState(getEngine()->State());
361   }
362 }
363
364 bool SUPERVGUI_CanvasCellNode::setNodeName(QString aName) 
365 {
366   bool result = SUPERVGUI_CanvasNode::setNodeName(aName);
367   if (result && myPairCell) {
368     result = myPairCell->setNodeName(QString(tr("ENDNODE_PREFIX"))+aName);
369   }
370   return result;
371 }
372
373 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasCellNode::createPrs() const
374 {
375   MESSAGE(" -> SUPERVGUI_CanvasCellNode::createPrs()");
376   SUPERVGUI_CanvasNodePrs* aPrs = 
377     new SUPERVGUI_CanvasCellNodePrs(getMain()->getCanvasArray(), 
378                                     (SUPERVGUI_CanvasCellNode*)this);
379   MESSAGE(" -> SUPERVGUI_CanvasCellNode::createPrs() 11");
380   return aPrs;
381 }
382
383 SUPERVGUI_CanvasCellEndNode::SUPERVGUI_CanvasCellEndNode( QObject* theParent, 
384                                                           SUPERVGUI_Main* theMain, 
385                                                           SUPERV_CNode theNode, 
386                                                           SUPERVGUI_CanvasCellNode* theStart):
387   SUPERVGUI_CanvasCellNode(theParent, theMain, theNode)
388 {
389   //set start cell for end cell as pair 
390   myPairCell = theStart; 
391   //set end cell for start cell as pair
392   myPairCell->setPairCell(dynamic_cast<SUPERVGUI_CanvasCellNode*>(this));
393 }
394
395 bool SUPERVGUI_CanvasCellEndNode::setNodeName(QString theName)
396 {
397   return SUPERVGUI_CanvasNode::setNodeName(theName);
398 }
399
400 //-----------------------------------------------------------
401 //*************** Select Inline node dialog******************
402 // Taken from SUPERVGUI_ControlNode.cxx without change
403 //-----------------------------------------------------------
404
405 SUPERVGUI_SelectInlineDlg::SUPERVGUI_SelectInlineDlg(SUPERVGUI_Main* theMain)  
406   :QDialog(theMain, 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
407 {
408   setSizeGripEnabled( true );
409   setCaption(tr("TIT_FUNC_PYTHON"));
410   QGridLayout* aMainLayout = new QGridLayout(this, 2, 2, 7, 4);
411
412   QLabel* aLabel = new QLabel("Connect to", this );
413   aMainLayout->addWidget(aLabel, 0, 0);
414
415   myCombo = new QComboBox(this);
416   myCombo->clear(); //for the additional check from GUI side for bug PAL7007
417   SUPERV_Nodes aNodesList = theMain->getDataflow()->Nodes();  
418   int i;
419   for (i = 0; i < aNodesList->INodes.length(); i++) {
420     myCombo->insertItem(QString(aNodesList->INodes[i]->Name()));
421   }
422   for (i = 0; i < aNodesList->LNodes.length(); i++) {
423     myCombo->insertItem(QString(aNodesList->LNodes[i]->Name()));
424   }
425   for (i = 0; i < aNodesList->SNodes.length(); i++) {
426     myCombo->insertItem(QString(aNodesList->SNodes[i]->Name()));
427   }
428   aMainLayout->addWidget(myCombo, 0, 1);
429
430   QGroupBox* aBtnBox = new QGroupBox( this );
431   aBtnBox->setColumnLayout( 0, Qt::Vertical );
432   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
433   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
434   aBtnLayout->setAlignment( Qt::AlignTop );
435   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
436   
437   QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
438   connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
439   aBtnLayout->addWidget( aOKBtn );
440
441   aBtnLayout->addStretch();
442
443   QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
444   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
445   aBtnLayout->addWidget( aCancelBtn );
446
447   aMainLayout->addMultiCellWidget(aBtnBox, 1, 1, 0, 1);
448 }