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