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