Salome HOME
Fix for bug PAL8110 : PAL-SUPRV-004: "Supervisor Warning" message box misses.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_CanvasPort.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE
4 //
5 //  File   : SUPERVGUI_CanvasPort.cxx
6 //  Author : Natalia KOPNOVA
7 //  Module : SUPERV
8
9 using namespace std;
10 #include "SUPERVGUI_CanvasPort.h"
11 #include "SUPERVGUI_CanvasNode.h"
12 #include "SUPERVGUI_CanvasLink.h"
13 #include "SUPERVGUI_CanvasNodePrs.h"
14 #include "SUPERVGUI_Clipboard.h"
15 #include "SUPERVGUI_Main.h"
16 #include "SUPERVGUI.h"
17 #include "SUPERVGUI_BrowseNodeDlg.h"
18
19 #include "SalomeApp_Study.h"
20
21 SUPERVGUI_CanvasPort::SUPERVGUI_CanvasPort(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
22     QObject(theParent),
23     myMain(theMain),
24     myPrs(0),
25     isIgnore(false)
26 {
27   Trace("SUPERVGUI_CanvasPort::SUPERVGUI_CanvasPort");
28   myPort = SUPERV::Port::_duplicate(thePort);
29
30   // setName(myPort->Name());
31   setName(myMain->getCanvas()->getPortName(thePort));
32 }
33
34 SUPERVGUI_CanvasPort::~SUPERVGUI_CanvasPort()
35 {
36   Trace("SUPERVGUI_CanvasPort::~SUPERVGUI_CanvasPort");
37   if (myPrs) delete myPrs;
38
39   isIgnore = true;
40   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
41   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
42     delete *it;
43   }
44 }
45
46 SUPERVGUI_CanvasPortPrs* SUPERVGUI_CanvasPort::getPrs() const
47 {
48   if (myPrs == 0) ((SUPERVGUI_CanvasPort*)this)->myPrs = createPrs();
49   return myPrs;
50 }
51
52 SUPERVGUI_CanvasPortPrs* SUPERVGUI_CanvasPort::createPrs() const
53 {
54   return new SUPERVGUI_CanvasPortPrs(myMain->getCanvas(), (SUPERVGUI_CanvasPort*)this);
55 }
56
57 QPopupMenu* SUPERVGUI_CanvasPort::getPopupMenu(QWidget* theParent) 
58 {
59   QPopupMenu* popup = new QPopupMenu(theParent);
60   if ( myMain->isEditable() && !myMain->getDataflow()->IsExecuting() ) {
61     int anItem = popup->insertItem(tr("MSG_SKETCH_LINK"), this, SLOT(sketchLink()));
62     if (myMain->getDataflow()->IsExecuting())
63       popup->setItemEnabled(anItem, false);
64     else
65       popup->setItemEnabled(anItem, !myPort->IsInput() || !myPort->IsLinked()
66                             || myPort->Kind() == SUPERV::EndSwitchParameter);
67     popup->insertSeparator();
68   }
69   if (myMain->isEditable() && !myMain->getDataflow()->IsExecuting()
70       &&
71       ((myPort->IsEndSwitch() && myPort->IsInput()) 
72        ||
73        (myPort->IsInLine() && myPort->Node()->Kind() != SUPERV::EndLoopNode
74         && 
75         !(myPort->Node()->Kind() == SUPERV::LoopNode && !myPort->IsInput())))) {
76     popup->insertItem(tr("ITM_DEL_PORT"), this, SLOT(remove()));    
77   }
78   // Copy Port functionality
79   if (myMain->isEditable() && !myPort->Node()->IsFactory()
80                            && !myPort->Node()->IsComputing()
81                            && !myPort->Node()->IsMacro())
82     popup->insertItem(tr("ITM_COPY_PORT"), this, SLOT(copy()));
83
84   /*int anItem = */popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
85 //   if (getEngine()->IsLinked())
86 //     popup->setItemEnabled(anItem, getEngine()->State() == SUPERV_Ready);
87 //   else 
88 //     popup->setItemEnabled(anItem, getEngine()->HasInput());
89
90   return popup;
91 }
92
93 QPoint SUPERVGUI_CanvasPort::getConnectionPoint() const
94 {
95   QPoint p = getPrs()->getConnectionPoint();
96
97   if (!getPrs()->isVisible()) {
98     if (parent() && parent()->inherits("SUPERVGUI_CanvasNode")) {
99       SUPERVGUI_CanvasNodePrs* aNode = ((SUPERVGUI_CanvasNode*) parent())->getPrs();
100       if (myPort->IsInput())
101         p = aNode->getInConnectionPoint();
102       else
103         p = aNode->getOutConnectionPoint();
104     }
105   }
106   return p;
107 }
108
109 void SUPERVGUI_CanvasPort::update() 
110 {
111   // ignore update if node itself is destroyed (critical for Start/End control nodes)
112   if (!((SUPERVGUI_CanvasNode*) parent())->isDestroyed())
113     getPrs()->update(true);
114 }
115
116 void SUPERVGUI_CanvasPort::sync() 
117 {
118   getPrs()->update();
119 }
120
121 void SUPERVGUI_CanvasPort::sketchLink() {
122   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
123   
124   myMain->getCanvasView()->startSketch(this);
125 }
126
127 void SUPERVGUI_CanvasPort::remove() {
128   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
129
130   //set myCopyPort from Main object to empty if engine of this port is deleted
131   //check if myCopyPort and this port engine is equal
132   SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
133   if ( aCB->isCopyPort() )
134     if ( QString(myPort->Node()->Name()) == QString(aCB->getCopyPort()->Node()->Name()) &&
135          QString(myPort->Name()) == QString(aCB->getCopyPort()->Name()) &&
136          myPort->IsInput() == aCB->getCopyPort()->IsInput() )
137         aCB->setCopyPort( 0 );
138
139   Trace("SUPERVGUI_CanvasPort::remove");
140   myPort->destroy();
141   delete this;
142 }
143
144 void SUPERVGUI_CanvasPort::moveBy(int dx, int dy) 
145 {
146   getPrs()->moveBy(dx, dy);
147
148   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
149   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
150     (*it)->moveByPort(this, dx, dy);
151   }
152 }
153
154 void SUPERVGUI_CanvasPort::addLink(SUPERVGUI_CanvasLink* theLink)
155 {
156   myLinks.append(theLink);
157   update();
158 }
159
160 void SUPERVGUI_CanvasPort::removeLink(SUPERVGUI_CanvasLink* theLink)
161 {
162   if (!isIgnore) {
163     myLinks.remove(theLink);
164     update();
165   }
166 }
167
168 void SUPERVGUI_CanvasPort::updateLinks() 
169 {
170   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
171   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
172     (*it)->moveByPort(this);
173   }
174 }
175
176 void SUPERVGUI_CanvasPort::browse() 
177 {
178   QString aMes(getEngine()->IsInput()? tr("MSG_IPORT_VAL") : tr("MSG_OPORT_VAL"));
179   aMes += getEngine()->ToString();
180   QMessageBox::information(SUIT_Session::session()->activeApplication()->desktop(), tr("MSG_INFO"), aMes);
181 }
182
183 void SUPERVGUI_CanvasPort::copy()
184 {
185   SUPERVGUI_Clipboard* aCB = SUPERVGUI_Clipboard::getClipboard();
186   aCB->setCopyPort(SUPERV::Port::_duplicate(getEngine()));
187 }
188
189 //***********************************************************
190 // Input Port
191 //***********************************************************
192 SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
193   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
194 {
195   Trace("SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn");
196   myDlg = 0;
197 }
198
199 SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn()
200 {
201   Trace("SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn");
202 }
203
204 QPopupMenu* SUPERVGUI_CanvasPortIn::getPopupMenu(QWidget* theParent) 
205 {
206   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
207   bool editable = getEngine()->IsInput() && !getEngine()->IsLinked() && !getMain()->getDataflow()->IsExecuting();
208
209   if (!getEngine()->IsGate() && editable)
210     popup->insertItem(tr("MSG_SETVALUE"), this, SLOT(setInput()));
211
212   return popup;
213 }
214
215 void SUPERVGUI_CanvasPortIn::setValue(const char* theValue) 
216 {
217   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
218   if (aSupMod && getEngine()->Input(aSupMod->getEngine()->StringValue(theValue)))
219     update(); // sync();
220   else
221     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
222 }
223
224 void SUPERVGUI_CanvasPortIn::setInput() 
225 {
226   // asv 28.01.05 : set "Editing" flag only on "OK" pressed in BrowseDlg
227   //getMain()->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
228   if (!myDlg) {
229     myDlg = new SUPERVGUI_GetValueDlg(this);
230     myDlg->installEventFilter(this);
231   }
232   if (!myDlg->isVisible())
233     myDlg->show();
234   else {
235     myDlg->raise();
236     myDlg->setActiveWindow();
237     myDlg->setFocus();
238   }
239 }
240
241 bool SUPERVGUI_CanvasPortIn::eventFilter(QObject* o, QEvent* e)
242 {
243   if (o == myDlg && e->type() == QEvent::Close)
244     myDlg = 0;
245   return SUPERVGUI_CanvasPort::eventFilter(o, e);
246 }
247
248
249 //***********************************************************
250 // Output Port
251 //***********************************************************
252 SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
253   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
254 {
255   Trace("SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut");
256   myInStudy = false;
257 }
258
259 SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut()
260 {
261   Trace("SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut");
262 }
263
264 QPopupMenu* SUPERVGUI_CanvasPortOut::getPopupMenu(QWidget* theParent) 
265 {
266   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
267
268   if (!getEngine()->IsGate()) {
269     popup->insertItem(myInStudy?tr("MSG_NOT_INSTUDY"):tr("MSG_PUT_INSTUDY"), 
270                       this, SLOT(toStudy()));
271   }
272
273   return popup;
274 }
275
276 void SUPERVGUI_CanvasPortOut::sync() 
277 {
278   /* asv : 26.01.05 : Bug PAL7164 : sometimes CanvasPortOut::sync() is called twice (or maybe even more)
279            by mistake.  It happens because of incorrect Qt events, or other reason, but it is not a
280            stable feature (bug). Adding an object in the study in sync() is therefore called more than once
281            which is a BUG.  I decided to move call to putDataStudy() method to Event handling function.
282            When a node successfully finishes execution - check the ports and put out-value to study,
283            if the corresponding flag is set.
284   bool ok = getEngine()->State() == SUPERV_Ready;
285   if (ok && myInStudy) {
286     myInStudy = getMain()->putDataStudy(getEngine(), STUDY_PORT_OUT);
287   }
288   */
289   SUPERVGUI_CanvasPort::update();
290 }
291
292 void SUPERVGUI_CanvasPortOut::toStudy() 
293 {
294   Trace("SUPERVGUI_CanvasPortOut::toStudy");
295
296   // asv 08.02.05 : added && !myInStudy - fix for PAL8105
297   if ( (( SalomeApp_Study* )(getMain()->getStudy()))->studyDS()->GetProperties()->IsLocked() && !myInStudy ) {
298     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WRN_WARNING"), 
299                          tr("WRN_STUDY_LOCKED"));
300     return;
301   }
302
303   myInStudy = !myInStudy;
304   sync();
305   getMain()->getCanvas()->update();
306
307   if ( myInStudy ) // put values to study (supervision, supervision->dataflow, supervision->dataflow->runXXX, etc.
308     if ( getMain()->putDataStudy( getEngine(), STUDY_PORT_OUT ) ) {
309       // mkr : PAL8110 : re-register dataflow in object browser with 
310       //       changing its key to IOR name (from xml-file name, for example)
311       SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
312       if ( !aSupMod ) {
313         MESSAGE("NULL Supervision module!");
314         return;
315       }
316       aSupMod->unregisterGraph(getMain());
317       aSupMod->registerGraph(getMain()->getDataflow()->getIOR(), getMain());
318     }
319 }
320
321
322
323 //***********************************************************
324 // Stream Input Port
325 //***********************************************************
326 SUPERVGUI_CanvasStreamPortIn::SUPERVGUI_CanvasStreamPortIn(QObject* theParent, SUPERVGUI_Main* theMain, 
327                                                            SUPERV::StreamPort_ptr thePort):
328   SUPERVGUI_CanvasPortIn(theParent, theMain, thePort)
329 {
330   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
331 }
332
333 QPopupMenu* SUPERVGUI_CanvasStreamPortIn::getPopupMenu(QWidget* theParent) 
334 {
335   QPopupMenu* popup = SUPERVGUI_CanvasPortIn::getPopupMenu(theParent);
336   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
337   return popup;
338 }
339
340 void SUPERVGUI_CanvasStreamPortIn::setParams()
341 {
342   SUPERVGUI_StreamInDlg* aDlg = new SUPERVGUI_StreamInDlg(this);
343   aDlg->exec();
344   delete aDlg;
345 }
346
347
348 //***********************************************************
349 // Stream Output Port
350 //***********************************************************
351 SUPERVGUI_CanvasStreamPortOut::SUPERVGUI_CanvasStreamPortOut(QObject* theParent, SUPERVGUI_Main* theMain, 
352                                                              SUPERV::StreamPort_ptr thePort):
353   SUPERVGUI_CanvasPortOut(theParent, theMain, thePort)
354 {
355   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
356 }
357
358
359 QPopupMenu* SUPERVGUI_CanvasStreamPortOut::getPopupMenu(QWidget* theParent) 
360 {
361   QPopupMenu* popup = SUPERVGUI_CanvasPortOut::getPopupMenu(theParent);
362   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
363   return popup;
364 }
365
366 void SUPERVGUI_CanvasStreamPortOut::setParams()
367 {
368   SUPERVGUI_StreamOutDlg* aDlg = new SUPERVGUI_StreamOutDlg(this);
369   aDlg->exec();
370   delete aDlg;
371 }