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