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