Salome HOME
Restore IsLoading (CVS problem)
[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()) {
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() 
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 {
116   myMain->getCanvasView()->startSketch(this);
117 }
118
119 void SUPERVGUI_CanvasPort::remove() {
120   Trace("SUPERVGUI_CanvasPort::remove");
121   myPort->destroy();
122   delete this;
123 }
124
125 void SUPERVGUI_CanvasPort::moveBy(int dx, int dy) 
126 {
127   getPrs()->moveBy(dx, dy);
128
129   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
130   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
131     (*it)->moveByPort(this, dx, dy);
132   }
133 }
134
135 void SUPERVGUI_CanvasPort::addLink(SUPERVGUI_CanvasLink* theLink)
136 {
137   myLinks.append(theLink);
138   update();
139 }
140
141 void SUPERVGUI_CanvasPort::removeLink(SUPERVGUI_CanvasLink* theLink)
142 {
143   if (!isIgnore) {
144     myLinks.remove(theLink);
145     update();
146   }
147 }
148
149 void SUPERVGUI_CanvasPort::updateLinks() 
150 {
151   QValueList<SUPERVGUI_CanvasLink*>::Iterator it;
152   for (it = myLinks.begin(); it != myLinks.end(); ++it) {
153     (*it)->moveByPort(this);
154   }
155 }
156
157 void SUPERVGUI_CanvasPort::browse() 
158 {
159   QString aMes(getEngine()->IsInput()? tr("MSG_IPORT_VAL") : tr("MSG_OPORT_VAL"));
160   aMes += getEngine()->ToString();
161   QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), aMes);
162 }
163
164
165 //***********************************************************
166 // Input Port
167 //***********************************************************
168 SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
169   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
170 {
171   Trace("SUPERVGUI_CanvasPortIn::SUPERVGUI_CanvasPortIn");
172   myDlg = 0;
173 }
174
175 SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn()
176 {
177   Trace("SUPERVGUI_CanvasPortIn::~SUPERVGUI_CanvasPortIn");
178 }
179
180 QPopupMenu* SUPERVGUI_CanvasPortIn::getPopupMenu(QWidget* theParent) 
181 {
182   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
183   bool editable = getEngine()->IsInput() && (!getEngine()->IsLinked());
184
185   if (!getEngine()->IsGate() && editable)
186     popup->insertItem(tr("MSG_SETVALUE"), this, SLOT(setInput()));
187
188   return popup;
189 }
190
191 void SUPERVGUI_CanvasPortIn::setValue(const char* theValue) 
192 {
193   if (getEngine()->Input(Supervision.getEngine()->StringValue(theValue)))
194     update(); // sync();
195   else
196     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
197 }
198
199 void SUPERVGUI_CanvasPortIn::setInput() 
200 {
201   if (!myDlg) {
202     myDlg = new SUPERVGUI_GetValueDlg(this);
203     myDlg->installEventFilter(this);
204   }
205   if (!myDlg->isVisible())
206     myDlg->show();
207   else {
208     myDlg->raise();
209     myDlg->setActiveWindow();
210     myDlg->setFocus();
211   }
212   
213 }
214
215 bool SUPERVGUI_CanvasPortIn::eventFilter(QObject* o, QEvent* e)
216 {
217   if (o == myDlg && e->type() == QEvent::Close)
218     myDlg = 0;
219   return SUPERVGUI_CanvasPort::eventFilter(o, e);
220 }
221
222
223 //***********************************************************
224 // Output Port
225 //***********************************************************
226 SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut(QObject* theParent, SUPERVGUI_Main* theMain, SUPERV::Port_ptr thePort):
227   SUPERVGUI_CanvasPort(theParent, theMain, thePort)
228 {
229   Trace("SUPERVGUI_CanvasPortOut::SUPERVGUI_CanvasPortOut");
230   myInStudy = false;
231 }
232
233 SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut()
234 {
235   Trace("SUPERVGUI_CanvasPortOut::~SUPERVGUI_CanvasPortOut");
236 }
237
238 QPopupMenu* SUPERVGUI_CanvasPortOut::getPopupMenu(QWidget* theParent) 
239 {
240   QPopupMenu* popup = SUPERVGUI_CanvasPort::getPopupMenu(theParent);
241
242   if (!getEngine()->IsGate()) {
243     popup->insertItem(myInStudy?tr("MSG_NOT_INSTUDY"):tr("MSG_PUT_INSTUDY"), 
244                       this, SLOT(toStudy()));
245   }
246
247   return popup;
248 }
249
250 void SUPERVGUI_CanvasPortOut::sync() 
251 {
252   bool ok = getEngine()->State() == SUPERV_Ready;
253   if (ok && myInStudy) {
254     myInStudy = getMain()->putDataStudy(getEngine(), STUDY_PORT_OUT);
255   }
256   SUPERVGUI_CanvasPort::update();
257 }
258
259 void SUPERVGUI_CanvasPortOut::toStudy() 
260 {
261   Trace("SUPERVGUI_CanvasPortOut::toStudy");
262
263   if (getMain()->getStudy()->getStudyDocument()->GetProperties()->IsLocked()) {
264     QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), 
265                          tr("WRN_STUDY_LOCKED"));
266     return;
267   }
268
269   if (!getMain()->isFromStudy()) {
270     if (getMain()->addStudy())
271       getMain()->setAsFromStudy(true);
272   }
273   myInStudy = !myInStudy;
274   sync();
275   getMain()->getCanvas()->update();
276 }
277
278
279
280 //***********************************************************
281 // Stream Input Port
282 //***********************************************************
283 SUPERVGUI_CanvasStreamPortIn::SUPERVGUI_CanvasStreamPortIn(QObject* theParent, SUPERVGUI_Main* theMain, 
284                                                            SUPERV::StreamPort_ptr thePort):
285   SUPERVGUI_CanvasPortIn(theParent, theMain, thePort)
286 {
287   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
288 }
289
290 QPopupMenu* SUPERVGUI_CanvasStreamPortIn::getPopupMenu(QWidget* theParent) 
291 {
292   QPopupMenu* popup = SUPERVGUI_CanvasPortIn::getPopupMenu(theParent);
293   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
294   return popup;
295 }
296
297 void SUPERVGUI_CanvasStreamPortIn::setParams()
298 {
299   SUPERVGUI_StreamInDlg* aDlg = new SUPERVGUI_StreamInDlg(this);
300   aDlg->exec();
301   delete aDlg;
302 }
303
304
305 //***********************************************************
306 // Stream Output Port
307 //***********************************************************
308 SUPERVGUI_CanvasStreamPortOut::SUPERVGUI_CanvasStreamPortOut(QObject* theParent, SUPERVGUI_Main* theMain, 
309                                                              SUPERV::StreamPort_ptr thePort):
310   SUPERVGUI_CanvasPortOut(theParent, theMain, thePort)
311 {
312   myStreamPort = SUPERV::StreamPort::_duplicate(thePort);
313 }
314
315
316 QPopupMenu* SUPERVGUI_CanvasStreamPortOut::getPopupMenu(QWidget* theParent) 
317 {
318   QPopupMenu* popup = SUPERVGUI_CanvasPortOut::getPopupMenu(theParent);
319   popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
320   return popup;
321 }
322
323 void SUPERVGUI_CanvasStreamPortOut::setParams()
324 {
325   SUPERVGUI_StreamOutDlg* aDlg = new SUPERVGUI_StreamOutDlg(this);
326   aDlg->exec();
327   delete aDlg;
328 }