]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_CanvasPort.cxx
Salome HOME
Point 2.8 of "SUPERVISOR: Current status - 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
77   int anItem = popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
78 //   if (getEngine()->IsLinked())
79 //     popup->setItemEnabled(anItem, getEngine()->State() == SUPERV_Ready);
80 //   else 
81 //     popup->setItemEnabled(anItem, getEngine()->HasInput());
82
83   return popup;
84 }
85
86 QPoint SUPERVGUI_CanvasPort::getConnectionPoint() const
87 {
88   QPoint p = getPrs()->getConnectionPoint();
89
90   if (!getPrs()->isVisible()) {
91     if (parent() && parent()->inherits("SUPERVGUI_CanvasNode")) {
92       SUPERVGUI_CanvasNodePrs* aNode = ((SUPERVGUI_CanvasNode*) parent())->getPrs();
93       if (myPort->IsInput())
94         p = aNode->getInConnectionPoint();
95       else
96         p = aNode->getOutConnectionPoint();
97     }
98   }
99   return p;
100 }
101
102 void SUPERVGUI_CanvasPort::update() 
103 {
104   // ignore update if node itself is destroyed (critical for Start/End control nodes)
105   if (!((SUPERVGUI_CanvasNode*) parent())->isDestroyed())
106     getPrs()->update(true);
107 }
108
109 void SUPERVGUI_CanvasPort::sync() 
110 {
111   getPrs()->update();
112 }
113
114 void SUPERVGUI_CanvasPort::sketchLink() {
115   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
116   
117   myMain->getCanvasView()->startSketch(this);
118 }
119
120 void SUPERVGUI_CanvasPort::remove() {
121   myMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
122
123   Trace("SUPERVGUI_CanvasPort::remove");
124   myPort->destroy();
125   delete this;
126 }
127
128 void SUPERVGUI_CanvasPort::moveBy(int dx, int dy) 
129 {
130   getPrs()->moveBy(dx, dy);
131
132   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
133   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
134     (*it)->moveByPort(this, dx, dy);
135   }
136 }
137
138 void SUPERVGUI_CanvasPort::addLink(SUPERVGUI_CanvasLink* theLink)
139 {
140   myLinks.append(theLink);
141   update();
142 }
143
144 void SUPERVGUI_CanvasPort::removeLink(SUPERVGUI_CanvasLink* theLink)
145 {
146   if (!isIgnore) {
147     myLinks.remove(theLink);
148     update();
149   }
150 }
151
152 void SUPERVGUI_CanvasPort::updateLinks() 
153 {
154   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
155   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
156     (*it)->moveByPort(this);
157   }
158 }
159
160 void SUPERVGUI_CanvasPort::browse() 
161 {
162   QString aMes(getEngine()->IsInput()? tr("MSG_IPORT_VAL") : tr("MSG_OPORT_VAL"));
163   aMes += getEngine()->ToString();
164   QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), aMes);
165 }
166
167
168 //***********************************************************
169 // Input Port
170 //***********************************************************
171 SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
172   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
173 {
174   Trace("SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn");
175   myDlg = 0;
176 }
177
178 SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn()
179 {
180   Trace("SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn");
181 }
182
183 QPopupMenu* SUPERVGUI_CanvasPortIn::getPopupMenu(QWidget* theParent) 
184 {
185   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
186   bool editable = getEngine()->IsInput() && !getEngine()->IsLinked() && !getMain()->getDataflow()->IsExecuting();
187
188   if (!getEngine()->IsGate() && editable)
189     popup->insertItem(tr("MSG_SETVALUE"), this, SLOT(setInput()));
190
191   return popup;
192 }
193
194 void SUPERVGUI_CanvasPortIn::setValue(const char* theValue) 
195 {
196   if (getEngine()->Input(Supervision.getEngine()->StringValue(theValue)))
197     update(); // sync();
198   else
199     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
200 }
201
202 void SUPERVGUI_CanvasPortIn::setInput() 
203 {
204   getMain()->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
205   if (!myDlg) {
206     myDlg = new SUPERVGUI_GetValueDlg(this);
207     myDlg->installEventFilter(this);
208   }
209   if (!myDlg->isVisible())
210     myDlg->show();
211   else {
212     myDlg->raise();
213     myDlg->setActiveWindow();
214     myDlg->setFocus();
215   }
216 }
217
218 bool SUPERVGUI_CanvasPortIn::eventFilter(QObject* o, QEvent* e)
219 {
220   if (o == myDlg && e->type() == QEvent::Close)
221     myDlg = 0;
222   return SUPERVGUI_CanvasPort::eventFilter(o, e);
223 }
224
225
226 //***********************************************************
227 // Output Port
228 //***********************************************************
229 SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
230   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
231 {
232   Trace("SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut");
233   myInStudy = false;
234 }
235
236 SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut()
237 {
238   Trace("SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut");
239 }
240
241 QPopupMenu* SUPERVGUI_CanvasPortOut::getPopupMenu(QWidget* theParent) 
242 {
243   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
244
245   if (!getEngine()->IsGate()) {
246     popup->insertItem(myInStudy?tr("MSG_NOT_INSTUDY"):tr("MSG_PUT_INSTUDY"), 
247                       this, SLOT(toStudy()));
248   }
249
250   return popup;
251 }
252
253 void SUPERVGUI_CanvasPortOut::sync() 
254 {
255   bool ok = getEngine()->State() == SUPERV_Ready;
256   if (ok && myInStudy) {
257     myInStudy = getMain()->putDataStudy(getEngine(), STUDY_PORT_OUT);
258   }
259   SUPERVGUI_CanvasPort::update();
260 }
261
262 void SUPERVGUI_CanvasPortOut::toStudy() 
263 {
264   Trace("SUPERVGUI_CanvasPortOut::toStudy");
265
266   if (getMain()->getStudy()->getStudyDocument()->GetProperties()->IsLocked()) {
267     QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), 
268                          tr("WRN_STUDY_LOCKED"));
269     return;
270   }
271
272   if (!getMain()->isFromStudy()) {
273     if (getMain()->addStudy())
274       getMain()->setAsFromStudy(true);
275   }
276   myInStudy = !myInStudy;
277   sync();
278   getMain()->getCanvas()->update();
279 }
280
281
282
283 //***********************************************************
284 // Stream Input Port
285 //***********************************************************
286 SUPERVGUI_CanvasStreamPortIn::SUPERVGUI_CanvasStreamPortIn(QObject* theParent, SUPERVGUI_Main* theMain, 
287                                                            SUPERV::StreamPort_ptr thePort):
288   SUPERVGUI_CanvasPortIn(theParent, theMain, thePort)
289 {
290   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
291 }
292
293 QPopupMenu* SUPERVGUI_CanvasStreamPortIn::getPopupMenu(QWidget* theParent) 
294 {
295   QPopupMenu* popup = SUPERVGUI_CanvasPortIn::getPopupMenu(theParent);
296   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
297   return popup;
298 }
299
300 void SUPERVGUI_CanvasStreamPortIn::setParams()
301 {
302   SUPERVGUI_StreamInDlg* aDlg = new SUPERVGUI_StreamInDlg(this);
303   aDlg->exec();
304   delete aDlg;
305 }
306
307
308 //***********************************************************
309 // Stream Output Port
310 //***********************************************************
311 SUPERVGUI_CanvasStreamPortOut::SUPERVGUI_CanvasStreamPortOut(QObject* theParent, SUPERVGUI_Main* theMain, 
312                                                              SUPERV::StreamPort_ptr thePort):
313   SUPERVGUI_CanvasPortOut(theParent, theMain, thePort)
314 {
315   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
316 }
317
318
319 QPopupMenu* SUPERVGUI_CanvasStreamPortOut::getPopupMenu(QWidget* theParent) 
320 {
321   QPopupMenu* popup = SUPERVGUI_CanvasPortOut::getPopupMenu(theParent);
322   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
323   return popup;
324 }
325
326 void SUPERVGUI_CanvasStreamPortOut::setParams()
327 {
328   SUPERVGUI_StreamOutDlg* aDlg = new SUPERVGUI_StreamOutDlg(this);
329   aDlg->exec();
330   delete aDlg;
331 }