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