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