Salome HOME
Fixed a bug described in VSR's "Bugs and improvements in Supervisor" 1.8: Crash while...
[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_Main.h"
13 #include "SUPERVGUI.h"
14 #include "SUPERVGUI_Canvas.h"
15 #include "SUPERVGUI_ControlNode.h" // access to SelectInlineDlg
16
17 #include "QAD_FileDlg.h"
18 #include "QAD_Application.h"
19
20 //=====================================================================
21 // Compute node
22 //=====================================================================
23 SUPERVGUI_CanvasComputeNode::SUPERVGUI_CanvasComputeNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
24   SUPERVGUI_CanvasNode(theParent, theMain, theNode)
25 {
26   Trace("SUPERVGUI_CanvasComputeNode::SUPERVGUI_CanvasComputeNode");
27 }
28
29 QPopupMenu* SUPERVGUI_CanvasComputeNode::getPopupMenu(QWidget* theParent) 
30 {
31   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
32
33   QPopupMenu* aShowPopup = new QPopupMenu(theParent);
34   int anItem;
35   anItem = aShowPopup->insertItem(tr("POP_SHOWTITLES"), this, SLOT(switchLabel()));
36   aShowPopup->setItemChecked(anItem, getPrs()->isLabelVisible());
37   anItem = aShowPopup->insertItem(tr("POP_SHOWPORTS"), this, SLOT(switchPorts()));
38   aShowPopup->setItemChecked(anItem, getPrs()->isPortVisible());
39
40   popup->insertSeparator();
41   popup->insertItem(tr("POP_SHOW"), aShowPopup);
42
43   return popup;
44 }
45
46
47 //=====================================================================
48 // Start control node
49 //=====================================================================
50 SUPERVGUI_CanvasStartNode::SUPERVGUI_CanvasStartNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
51   SUPERVGUI_CanvasNode(theParent, theMain, theNode), 
52   myCoupled(0)
53 {
54   Trace("SUPERVGUI_CanvasStartNode::SUPERVGUI_CanvasStartNode");
55 }
56
57 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasStartNode::createPrs() const
58 {
59   return new SUPERVGUI_CanvasStartNodePrs(getMain()->getCanvas(), 
60                                           (SUPERVGUI_CanvasStartNode*)this);
61 }
62
63 bool SUPERVGUI_CanvasStartNode::setNodeName(QString aName)
64 {
65   bool result = SUPERVGUI_CanvasNode::setNodeName(aName);
66   if (result && myCoupled) {
67     myCoupled->setNodeName(QString(tr("ENDNODE_PREFIX"))+aName);
68   }
69   return result;
70 }
71
72 QPopupMenu* SUPERVGUI_CanvasStartNode::getPopupMenu(QWidget* theParent) 
73 {
74   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
75
76   popup->insertSeparator();
77   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
78   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
79
80   return popup;
81 }
82
83 void SUPERVGUI_CanvasStartNode::remove()
84 {
85   SUPERVGUI_Canvas* aCanvas = getMain()->getCanvas();
86
87   setDestroyed();
88   if (myCoupled) myCoupled->setDestroyed();
89
90   getEngine()->destroy();
91
92   if (myCoupled) delete myCoupled;
93   delete this;
94
95   aCanvas->update();
96 }
97
98 void SUPERVGUI_CanvasStartNode::onDestroyed(QObject* theObject)
99 {
100   if (!isIgnore) {
101     SUPERVGUI_CanvasNode::onDestroyed(theObject);
102     if (getEngine()->IsLoop()) merge();
103     if (myCoupled) myCoupled->merge();
104   }
105 }
106
107 void SUPERVGUI_CanvasStartNode::addInputPort()
108 {
109   SUPERVGUI_CanvasNode::addInputPort();
110   if (getEngine()->IsLoop()) merge();
111   if (myCoupled) myCoupled->merge();
112 }
113
114 void SUPERVGUI_CanvasStartNode::addOutputPort()
115 {
116   SUPERVGUI_CanvasNode::addOutputPort();
117   if (myCoupled) myCoupled->merge();
118 }
119
120 //=====================================================================
121 // End control node
122 //=====================================================================
123 SUPERVGUI_CanvasEndNode::SUPERVGUI_CanvasEndNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode,
124                                                  SUPERVGUI_CanvasStartNode* theStart):
125   SUPERVGUI_CanvasNode(theParent, theMain, theNode), 
126   myCoupled(theStart)
127 {
128   Trace("SUPERVGUI_CanvasEndNode::SUPERVGUI_CanvasEndNode");
129   myCoupled->setCoupled(this);
130 }
131
132 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasEndNode::createPrs() const
133 {
134   return new SUPERVGUI_CanvasEndNodePrs(getMain()->getCanvas(), 
135                                         (SUPERVGUI_CanvasEndNode*)this);
136 }
137
138 QPopupMenu* SUPERVGUI_CanvasEndNode::getPopupMenu(QWidget* theParent) 
139 {
140   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
141
142   popup->insertSeparator();
143   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
144   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
145
146   return popup;
147 }
148
149 void SUPERVGUI_CanvasEndNode::addInputPort()
150 {
151   SUPERVGUI_CanvasNode::addInputPort();
152   if (getEngine()->IsEndSwitch()) merge();
153 }
154
155
156 //=====================================================================
157 // Goto control node
158 //=====================================================================
159 SUPERVGUI_CanvasGotoNode::SUPERVGUI_CanvasGotoNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
160   SUPERVGUI_CanvasNode(theParent, theMain, theNode)
161 {
162   Trace("SUPERVGUI_CanvasGotoNode::SUPERVGUI_CanvasGotoNode");
163 }
164
165 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasGotoNode::createPrs() const
166 {
167   return new SUPERVGUI_CanvasGotoNodePrs(getMain()->getCanvas(), 
168                                          (SUPERVGUI_CanvasGotoNode*)this);
169 }
170
171 QPopupMenu* SUPERVGUI_CanvasGotoNode::getPopupMenu(QWidget* theParent) 
172 {
173   QPopupMenu* popup = SUPERVGUI_CanvasNode::getPopupMenu(theParent);
174
175   popup->insertSeparator();
176   popup->insertItem("Link to Node...", this, SLOT(linkToNode()));
177
178   popup->insertSeparator();
179   int anItem = popup->insertItem(tr("POP_HIDEPORTS"), this, SLOT(switchPorts()));
180   popup->setItemChecked(anItem, !getPrs()->isPortVisible());
181
182   return popup;
183 }
184
185 void SUPERVGUI_CanvasGotoNode::linkToNode() {
186   SUPERVGUI_SelectInlineDlg* aDlg = new SUPERVGUI_SelectInlineDlg(getMain());
187   if (aDlg->exec()) {
188     QString aNodeName = aDlg->getName();
189     if (!aNodeName.isEmpty()) { //implement additional check from GUI side for bug PAL7007
190       getGotoNode()->SetCoupled(aNodeName.latin1());
191       getMain()->getCanvas()->sync();
192     }
193   }
194   delete aDlg;
195 }
196
197 //=====================================================================
198 // Macro node
199 //=====================================================================
200 SUPERVGUI_CanvasMacroNode::SUPERVGUI_CanvasMacroNode(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode):
201   SUPERVGUI_CanvasComputeNode(theParent, theMain, theNode)
202 {
203   Trace("SUPERVGUI_CanvasMacroNode::SUPERVGUI_CanvasMacroNode");
204 }
205
206 SUPERVGUI_CanvasNodePrs* SUPERVGUI_CanvasMacroNode::createPrs() const
207 {
208   return new SUPERVGUI_CanvasMacroNodePrs(getMain()->getCanvas(), 
209                                           (SUPERVGUI_CanvasMacroNode*)this);
210 }
211
212 QPopupMenu* SUPERVGUI_CanvasMacroNode::getPopupMenu(QWidget* theParent) 
213 {
214   QPopupMenu* popup = SUPERVGUI_CanvasComputeNode::getPopupMenu(theParent);
215
216   popup->insertSeparator(0);
217   popup->insertItem(tr("MSG_EXPORT"), this, SLOT(exportDataflow()), 0, -1, 0);
218   popup->insertItem("Open SubGraph", this, SLOT(openSubGraph()), 0, -1, 0);
219
220   return popup;
221 }
222
223 void SUPERVGUI_CanvasMacroNode::openSubGraph()
224 {
225   getMain()->openSubGraph(getEngine());
226 }
227
228 void SUPERVGUI_CanvasMacroNode::exportDataflow()
229 {
230   SUPERV_Graph aGraph;
231   if (getEngine()->IsMacro()) {
232     SUPERV_Graph aMacro = getMacroNode();
233     if (aMacro->IsStreamMacro())
234       aGraph = aMacro->StreamObjRef();
235     else
236       aGraph = aMacro->FlowObjRef();
237   }
238   if (SUPERV_isNull(aGraph)) {
239     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
240     return;
241   }
242   else {
243     QString aFileName = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
244                                                  "",
245                                                  "*.xml",
246                                                  tr("TTL_EXPORT_DATAFLOW"),
247                                                  false);
248     if ( !aFileName.isEmpty() ) {
249       // asv : bug [VSR Bugs and Improvements in Supervisor] 1.8 : when exporting a file, 
250       // a backup copy of an existing file must be created (in case Export fails..)
251       QString aBackupFile = SUPERVGUI::createBackupFile( aFileName );
252
253       if (!aGraph->Export(aFileName.latin1())) {
254         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING").arg(aBackupFile));
255       }
256       // remove a backup file if export was successfull
257       else if ( !aBackupFile.isNull() && !aBackupFile.isEmpty() ) {
258         QFile::remove( aBackupFile );
259       }
260     }
261   }
262 }