]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_CanvasPort.cxx
Salome HOME
Point 2.3 of "SUPERVISOR: Current state - bugs/improvements" :
[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_Main.h"
15 #include "SUPERVGUI.h"
16 #include "SUPERVGUI_BrowseNodeDlg.h"
17
18
19 SUPERVGUI_CanvasPort::SUPERVGUI_CanvasPort(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
20     QObject(theParent),
21     myMain(theMain),
22     myPrs(0),
23     isIgnore(false)
24 {
25   Trace("SUPERVGUI_CanvasPort::SUPERVGUI_CanvasPort");
26   myPort = SUPERV::Port::_duplicate(thePort);
27
28   // setName(myPort->Name());
29   setName(myMain->getCanvas()->getPortName(thePort));
30 }
31
32 SUPERVGUI_CanvasPort::~SUPERVGUI_CanvasPort()
33 {
34   Trace("SUPERVGUI_CanvasPort::~SUPERVGUI_CanvasPort");
35   if (myPrs) delete myPrs;
36
37   isIgnore = true;
38   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
39   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
40     delete *it;
41   }
42 }
43
44 SUPERVGUI_CanvasPortPrs* SUPERVGUI_CanvasPort::getPrs() const
45 {
46   if (myPrs == 0) ((SUPERVGUI_CanvasPort*)this)->myPrs = createPrs();
47   return myPrs;
48 }
49
50 SUPERVGUI_CanvasPortPrs* SUPERVGUI_CanvasPort::createPrs() const
51 {
52   return new SUPERVGUI_CanvasPortPrs(myMain->getCanvas(), (SUPERVGUI_CanvasPort*)this);
53 }
54
55 QPopupMenu* SUPERVGUI_CanvasPort::getPopupMenu(QWidget* theParent) 
56 {
57   QPopupMenu* popup = new QPopupMenu(theParent);
58   if ( myMain->isEditable() && !myMain->getDataflow()->IsExecuting() ) {
59     int anItem = popup->insertItem(tr("MSG_SKETCH_LINK"), this, SLOT(sketchLink()));
60     if (myMain->getDataflow()->IsExecuting())
61       popup->setItemEnabled(anItem, false);
62     else
63       popup->setItemEnabled(anItem, !myPort->IsInput() || !myPort->IsLinked()
64                             || myPort->Kind() == SUPERV::EndSwitchParameter);
65     popup->insertSeparator();
66   }
67   if (myMain->isEditable() && !myMain->getDataflow()->IsExecuting()
68       &&
69       ((myPort->IsEndSwitch() && myPort->IsInput()) 
70        ||
71        (myPort->IsInLine() && myPort->Node()->Kind() != SUPERV::EndLoopNode
72         && 
73         !(myPort->Node()->Kind() == SUPERV::LoopNode && !myPort->IsInput())))) {
74     popup->insertItem(tr("ITM_DEL_PORT"), this, SLOT(remove()));    
75   }
76   // Copy Port functionality
77   if (myMain->isEditable() && !myPort->Node()->IsFactory()
78                            && !myPort->Node()->IsComputing()
79                            && !myPort->Node()->IsMacro())
80     popup->insertItem(tr("ITM_COPY_PORT"), this, SLOT(copy()));
81
82   int anItem = popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
83 //   if (getEngine()->IsLinked())
84 //     popup->setItemEnabled(anItem, getEngine()->State() == SUPERV_Ready);
85 //   else 
86 //     popup->setItemEnabled(anItem, getEngine()->HasInput());
87
88   return popup;
89 }
90
91 QPoint SUPERVGUI_CanvasPort::getConnectionPoint() const
92 {
93   QPoint p = getPrs()->getConnectionPoint();
94
95   if (!getPrs()->isVisible()) {
96     if (parent() && parent()->inherits("SUPERVGUI_CanvasNode")) {
97       SUPERVGUI_CanvasNodePrs* aNode = ((SUPERVGUI_CanvasNode*) parent())->getPrs();
98       if (myPort->IsInput())
99         p = aNode->getInConnectionPoint();
100       else
101         p = aNode->getOutConnectionPoint();
102     }
103   }
104   return p;
105 }
106
107 void SUPERVGUI_CanvasPort::update() 
108 {
109   // ignore update if node itself is destroyed (critical for Start/End control nodes)
110   if (!((SUPERVGUI_CanvasNode*) parent())->isDestroyed())
111     getPrs()->update(true);
112 }
113
114 void SUPERVGUI_CanvasPort::sync() 
115 {
116   getPrs()->update();
117 }
118
119 void SUPERVGUI_CanvasPort::sketchLink() {
120   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
121   
122   myMain->getCanvasView()->startSketch(this);
123 }
124
125 void SUPERVGUI_CanvasPort::remove() {
126   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
127
128   //set myCopyPort from Main object to empty if engine of this port is deleted
129   //check if myCopyPort and this port engine is equal
130   if (!SUPERV_isNull(getMain()->getCopyPort()) || getMain()->getCopyPort())
131     if ((QString(myPort->Node()->Name())).compare(QString(getMain()->getCopyPort()->Node()->Name())) == 0
132         &&
133         (QString(myPort->Name())).compare(QString(getMain()->getCopyPort()->Name())) == 0
134         &&
135         myPort->IsInput() == getMain()->getCopyPort()->IsInput())
136         getMain()->setCopyPort(NULL);
137
138   Trace("SUPERVGUI_CanvasPort::remove");
139   myPort->destroy();
140   delete this;
141 }
142
143 void SUPERVGUI_CanvasPort::moveBy(int dx, int dy) 
144 {
145   getPrs()->moveBy(dx, dy);
146
147   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
148   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
149     (*it)->moveByPort(this, dx, dy);
150   }
151 }
152
153 void SUPERVGUI_CanvasPort::addLink(SUPERVGUI_CanvasLink* theLink)
154 {
155   myLinks.append(theLink);
156   update();
157 }
158
159 void SUPERVGUI_CanvasPort::removeLink(SUPERVGUI_CanvasLink* theLink)
160 {
161   if (!isIgnore) {
162     myLinks.remove(theLink);
163     update();
164   }
165 }
166
167 void SUPERVGUI_CanvasPort::updateLinks() 
168 {
169   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
170   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
171     (*it)->moveByPort(this);
172   }
173 }
174
175 void SUPERVGUI_CanvasPort::browse() 
176 {
177   QString aMes(getEngine()->IsInput()? tr("MSG_IPORT_VAL") : tr("MSG_OPORT_VAL"));
178   aMes += getEngine()->ToString();
179   QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), aMes);
180 }
181
182 void SUPERVGUI_CanvasPort::copy()
183 {
184   //MESSAGE(" -> SUPERVGUI_CanvasPort::copy()");
185   getMain()->setCopyPort(SUPERV::Port::_duplicate(getEngine()));
186 }
187
188 //***********************************************************
189 // Input Port
190 //***********************************************************
191 SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
192   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
193 {
194   Trace("SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn");
195   myDlg = 0;
196 }
197
198 SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn()
199 {
200   Trace("SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn");
201 }
202
203 QPopupMenu* SUPERVGUI_CanvasPortIn::getPopupMenu(QWidget* theParent) 
204 {
205   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
206   bool editable = getEngine()->IsInput() && !getEngine()->IsLinked() && !getMain()->getDataflow()->IsExecuting();
207
208   if (!getEngine()->IsGate() && editable)
209     popup->insertItem(tr("MSG_SETVALUE"), this, SLOT(setInput()));
210
211   return popup;
212 }
213
214 void SUPERVGUI_CanvasPortIn::setValue(const char* theValue) 
215 {
216   if (getEngine()->Input(Supervision.getEngine()->StringValue(theValue)))
217     update(); // sync();
218   else
219     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
220 }
221
222 void SUPERVGUI_CanvasPortIn::setInput() 
223 {
224   getMain()->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
225   if (!myDlg) {
226     myDlg = new SUPERVGUI_GetValueDlg(this);
227     myDlg->installEventFilter(this);
228   }
229   if (!myDlg->isVisible())
230     myDlg->show();
231   else {
232     myDlg->raise();
233     myDlg->setActiveWindow();
234     myDlg->setFocus();
235   }
236 }
237
238 bool SUPERVGUI_CanvasPortIn::eventFilter(QObject* o, QEvent* e)
239 {
240   if (o == myDlg && e->type() == QEvent::Close)
241     myDlg = 0;
242   return SUPERVGUI_CanvasPort::eventFilter(o, e);
243 }
244
245
246 //***********************************************************
247 // Output Port
248 //***********************************************************
249 SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
250   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
251 {
252   Trace("SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut");
253   myInStudy = false;
254 }
255
256 SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut()
257 {
258   Trace("SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut");
259 }
260
261 QPopupMenu* SUPERVGUI_CanvasPortOut::getPopupMenu(QWidget* theParent) 
262 {
263   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
264
265   if (!getEngine()->IsGate()) {
266     popup->insertItem(myInStudy?tr("MSG_NOT_INSTUDY"):tr("MSG_PUT_INSTUDY"), 
267                       this, SLOT(toStudy()));
268   }
269
270   return popup;
271 }
272
273 void SUPERVGUI_CanvasPortOut::sync() 
274 {
275   bool ok = getEngine()->State() == SUPERV_Ready;
276   if (ok && myInStudy) {
277     myInStudy = getMain()->putDataStudy(getEngine(), STUDY_PORT_OUT);
278   }
279   SUPERVGUI_CanvasPort::update();
280 }
281
282 void SUPERVGUI_CanvasPortOut::toStudy() 
283 {
284   Trace("SUPERVGUI_CanvasPortOut::toStudy");
285
286   if (getMain()->getStudy()->getStudyDocument()->GetProperties()->IsLocked()) {
287     QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), 
288                          tr("WRN_STUDY_LOCKED"));
289     return;
290   }
291
292   if (!getMain()->isFromStudy()) {
293     if (getMain()->addStudy())
294       getMain()->setAsFromStudy(true);
295   }
296   myInStudy = !myInStudy;
297   sync();
298   getMain()->getCanvas()->update();
299 }
300
301
302
303 //***********************************************************
304 // Stream Input Port
305 //***********************************************************
306 SUPERVGUI_CanvasStreamPortIn::SUPERVGUI_CanvasStreamPortIn(QObject* theParent, SUPERVGUI_Main* theMain, 
307                                                            SUPERV::StreamPort_ptr thePort):
308   SUPERVGUI_CanvasPortIn(theParent, theMain, thePort)
309 {
310   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
311 }
312
313 QPopupMenu* SUPERVGUI_CanvasStreamPortIn::getPopupMenu(QWidget* theParent) 
314 {
315   QPopupMenu* popup = SUPERVGUI_CanvasPortIn::getPopupMenu(theParent);
316   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
317   return popup;
318 }
319
320 void SUPERVGUI_CanvasStreamPortIn::setParams()
321 {
322   SUPERVGUI_StreamInDlg* aDlg = new SUPERVGUI_StreamInDlg(this);
323   aDlg->exec();
324   delete aDlg;
325 }
326
327
328 //***********************************************************
329 // Stream Output Port
330 //***********************************************************
331 SUPERVGUI_CanvasStreamPortOut::SUPERVGUI_CanvasStreamPortOut(QObject* theParent, SUPERVGUI_Main* theMain, 
332                                                              SUPERV::StreamPort_ptr thePort):
333   SUPERVGUI_CanvasPortOut(theParent, theMain, thePort)
334 {
335   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
336 }
337
338
339 QPopupMenu* SUPERVGUI_CanvasStreamPortOut::getPopupMenu(QWidget* theParent) 
340 {
341   QPopupMenu* popup = SUPERVGUI_CanvasPortOut::getPopupMenu(theParent);
342   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
343   return popup;
344 }
345
346 void SUPERVGUI_CanvasStreamPortOut::setParams()
347 {
348   SUPERVGUI_StreamOutDlg* aDlg = new SUPERVGUI_StreamOutDlg(this);
349   aDlg->exec();
350   delete aDlg;
351 }