]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_Port.cxx
Salome HOME
NRI : Merge from V1_2.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Port.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SUPERVGUI_Port.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29 #include "SUPERVGUI_Port.h"
30 #include "SUPERVGUI_Def.h"
31 #include "SUPERVGUI_Main.h"
32 #include "SUPERVGUI_Link.h"
33 #include "SUPERVGUI.h"
34 #include "SUPERVGUI_BrowseNodeDlg.h"
35 #include "QAD_LeftFrame.h"
36 #include "QAD_ObjectBrowser.h"
37 #include <qpoint.h>
38 #include "SUPERVGUI_GraphNode.h"
39
40
41 // Port en general
42 // ---------------
43
44 SUPERVGUI_Port::SUPERVGUI_Port(QWidget* parent, SUPERVGUI_Main* m, SUPERV_Port p):
45     SUPERVGUI_Label(parent, PORT_WIDTH, PORT_HEIGHT, "", QLabel::AlignLeft | QLabel::AlignVCenter),
46     main(m),
47     port(p)
48 {
49   Trace("SUPERVGUI_Port::SUPERVGUI_Port");
50   myName = port->Name();
51   QString aName(port->Name());
52   aName += (port->IsInput())? "Input":"Output";
53   setName(aName);
54
55   if (myName.compare(OUTVOID) == 0)
56     setAlignment(QLabel::AlignRight | QLabel::AlignVCenter);
57
58   if (port->IsParam() || port->IsInLine())
59     setText(myName + "=" +  port->ToString());
60   else
61     setText(myName);
62     
63   setLineWidth(1);
64   setMidLineWidth(2);
65   setFrameShape(QLabel::Box);
66   setFrameShadow(QLabel::Plain);
67
68   popup = new QPopupMenu(this);
69   if (main->isEditable()) {
70     mySketchItem = popup->insertItem(tr("MSG_SKETCH_LINK"), this, SLOT(sketchLink()));
71     popup->insertSeparator();
72   }
73   if (port->IsInLine()) {
74     myDelItem = popup->insertItem(tr("ITM_DEL_PORT"), this, SLOT(deletePort()));    
75   }
76   connect(this, SIGNAL(MousePress(QMouseEvent*)), this, SLOT(showPopup(QMouseEvent*)));  
77   connect(this, SIGNAL(MouseRelease(QMouseEvent*)), this, SLOT(endSketch()));
78 }
79
80 SUPERVGUI_Port::~SUPERVGUI_Port() {
81     Trace("SUPERVGUI_Port::~SUPERVGUI_Port")
82 }
83
84 SUPERV_Port SUPERVGUI_Port::getPort() {
85     Trace("SUPERVGUI_Port::getPort")
86     return(port);
87 }
88
89 void SUPERVGUI_Port::showPopup(QMouseEvent* e) {
90   Trace("SUPERVGUI_Port::showPopup");
91   if (e->button() != RightButton) return;
92   if (main->getDataflow()->IsExecuting()) 
93     popup->setItemEnabled(mySketchItem, false);
94   else  
95     if (!port->IsInput() //output port
96         || 
97         port->IsInput() && (!port->IsLinked() //not linked input port
98                             ||
99                             port->Kind() == SUPERV::EndSwitchParameter)) { //input port of EndSwitch node
100       popup->setItemEnabled(mySketchItem, true);
101     }
102       
103   main->showPopup(popup, e);
104 }
105
106
107 QPoint SUPERVGUI_Port::getConnectPnt() {
108   return QPoint(0,0);
109 }
110
111
112 void SUPERVGUI_Port::sync() {
113   if (port->IsParam() || port->IsInLine())
114     setText(myName + "=" +  port->ToString());
115   else
116     setText(myName);
117
118 }
119
120
121 void SUPERVGUI_Port::sketchLink() {
122   main->getGraph()->sketchBegin(this);
123   if (port->IsInput()) 
124     setLineWidth(1);
125 }
126
127 void SUPERVGUI_Port::endSketch() {
128   main->getGraph()->sketchEnd(this);
129   if (port->IsInput() && port->IsLinked()) 
130     setLineWidth(1);
131 }
132
133
134 void SUPERVGUI_Port::deletePort() {
135   SUPERVGUI_GraphNode*  aNode = (SUPERVGUI_GraphNode* )main->getGraph()->child(port->Node()->Name(), "SUPERVGUI_GraphNode");
136   if (aNode) {
137     aNode->deletePort(this);
138   }
139 }
140
141
142
143
144 //***********************************************************
145 // Port d'entree
146 // -------------
147
148 SUPERVGUI_PortIn::SUPERVGUI_PortIn(QWidget* parent, SUPERVGUI_Main* m, SUPERV_Port p)
149   : SUPERVGUI_Port(parent, m, p), myDlg( 0 )
150 {
151   Trace("SUPERVGUI_PortIn::SUPERVGUI_PortIn");
152   myLinkPrs = 0;
153   //    link->move(0, height()/2-POINT_SIZE_HALF);
154
155   bool editing = port->IsInput() && ( !port->IsLinked() ); //main->isEditable();
156 //  bool linked  = port->IsLinked();
157 //  bool b = (!linked) && editing;
158   if ((!port->IsGate())  && editing ) {
159     psc = popup->insertItem(tr("MSG_SETVALUE"),      this, SLOT(setInput()));
160   }
161   browser = popup->insertItem(tr("MSG_BROWSE"),         this, SLOT(browse()));
162 }
163
164 SUPERVGUI_PortIn::~SUPERVGUI_PortIn() {
165   Trace("SUPERVGUI_PortIn::~SUPERVGUI_PortIn");
166 }
167
168
169 bool SUPERVGUI_PortIn::eventFilter( QObject* o, QEvent* e )
170 {
171   if ( o == myDlg && e->type() == QEvent::Close )
172     myDlg = 0;
173   return SUPERVGUI_Port::eventFilter( o, e );
174 }
175
176
177 void SUPERVGUI_PortIn::deleteLinks() {
178   if (myLinkPrs) {
179     disconnect(myLinkPrs, 0, this, 0);
180     main->getGraph()->deleteLink(myLinkPrs);
181     myLinkPrs = 0;
182   }
183 }
184
185
186 void SUPERVGUI_PortIn::sync() {
187   SUPERVGUI_Port::sync();
188   if (port->IsLinked()) {
189     popup->setItemEnabled(browser, port->State() == SUPERV_Ready);
190   } else {
191     if (port->HasInput()) {
192       popup->setItemEnabled(browser, true);
193       setLineWidth(2);
194     } else {
195       popup->setItemEnabled(browser, false);
196       setLineWidth(1);
197     }
198   }
199 }
200
201 void SUPERVGUI_PortIn::setValue(const char* d) {
202     Trace("SUPERVGUI_PortIn::setValue")
203     if (port->Input((*Supervision.getEngine())->StringValue(d))) {
204         sync();
205     } else {
206         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
207     };
208 }
209
210 void SUPERVGUI_PortIn::setInput() {
211     Trace("SUPERVGUI_PortIn::setInput");
212     if ( !myDlg ) {
213       myDlg = new SUPERVGUI_GetValueDlg(this);
214       myDlg->installEventFilter( this );
215     }
216     if ( !myDlg->isVisible() )
217       myDlg->show();
218     else {
219       myDlg->raise();
220       myDlg->setActiveWindow();
221       myDlg->setFocus();
222     }
223 }
224
225 void SUPERVGUI_PortIn::browse() {
226     Trace("SUPERVGUI_PortIn::browse")
227     QString mess(tr("MSG_IPORT_VAL"));
228     mess += port->ToString();
229     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
230 }
231
232
233
234 /**
235  * Returns coordinates of connection point in content coordinates
236  */
237 QPoint SUPERVGUI_PortIn::getConnectPnt() {
238   // Parent widget is internal node frame
239   // parent of parent is node it self
240   QWidget* aNode = parentWidget()->parentWidget();
241   QPoint aPnt(aNode->pos()); // position of the node
242
243   if (parentWidget()->isVisible()) {
244     aPnt += parentWidget()->pos();
245     return QPoint(aPnt.x() + pos().x(), 
246                   aPnt.y() + (pos().y() + height()/2));
247   }
248   return QPoint(aPnt.x(), 
249                 aPnt.y() + (aNode->height()/2));
250 }
251
252 void SUPERVGUI_PortIn::setLinkPrs(SUPERVGUI_Link* theLink) {
253   myLinkPrs = theLink;
254   connect(myLinkPrs, SIGNAL(linkDeleted(SUPERVGUI_Link*)),
255           this, SLOT(onDeleteLink(SUPERVGUI_Link*)));
256   popup->setItemEnabled(mySketchItem, false);
257 }
258
259 void SUPERVGUI_PortIn::onDeleteLink(SUPERVGUI_Link*) {
260   disconnect(myLinkPrs, 0, this, 0);
261   myLinkPrs = 0;
262   popup->setItemEnabled(psc, true);
263   if (port->HasInput())
264     setLineWidth(2);
265   if (main->isEditable()) {
266     popup->setItemEnabled(mySketchItem, true);
267   } else {
268     popup->setItemEnabled(mySketchItem, false);
269   }
270 }
271
272
273
274 //***********************************************************
275 // Port de sortie
276 // --------------
277
278 SUPERVGUI_PortOut::SUPERVGUI_PortOut(QWidget* parent, SUPERVGUI_Main* m, SUPERV_Port p):
279     SUPERVGUI_Port(parent, m, p),
280     study(false)
281 {
282     Trace("SUPERVGUI_PortOut::SUPERVGUI_PortOut")
283       //    link->move(width()-POINT_SIZE, height()/2-POINT_SIZE_HALF);
284     myLinksList.setAutoDelete(false);
285     
286     if (!port->IsGate()) {
287       pls = popup->insertItem(tr("MSG_PUT_INSTUDY"), this, SLOT(toStudy()));
288     }
289     browser = popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
290
291     if (!main->getDataflow()->IsReadOnly()) {
292       popup->setItemEnabled(mySketchItem, true);
293     } else {
294       popup->setItemEnabled(mySketchItem, false);
295     }
296 }
297
298 SUPERVGUI_PortOut::~SUPERVGUI_PortOut() {
299   Trace("SUPERVGUI_PortOut::~SUPERVGUI_PortOut");
300 }
301
302 void SUPERVGUI_PortOut::sync() {
303     Trace("SUPERVGUI_PortOut::sync")
304     SUPERVGUI_Port::sync();
305
306     bool ok = (port->State() == SUPERV_Ready);
307     popup->setItemEnabled(browser, ok);
308     if (study && ok) {
309       study = main->putDataStudy(port, STUDY_PORT_OUT);
310     }
311
312     if (study) {
313         setLineWidth(2);
314         popup->changeItem(pls, tr("MSG_NOT_INSTUDY"));
315     } else {
316         setLineWidth(1);
317         popup->changeItem(pls, tr("MSG_PUT_INSTUDY"));
318     }
319
320 }
321
322 void SUPERVGUI_PortOut::updateStudy() {
323   Trace("SUPERVGUI_PortOut::updateStudy")
324   SUPERVGUI_Node*  aNode = (SUPERVGUI_Node* )main->getGraph()->child(port->Node()->Name(), "SUPERVGUI_Node");
325   if (aNode)
326     ((SUPERVGUI_PortOut*)aNode->child(port->Name(), "SUPERVGUI_PortOut"))->study = study;
327   else
328     {
329       aNode = (SUPERVGUI_Node* )main->getArray()->child(port->Node()->Name(), "SUPERVGUI_Node");
330       if (aNode)
331         ((SUPERVGUI_PortOut*)aNode->child(port->Name(), "SUPERVGUI_PortOut"))->study = study;
332     }
333 }
334
335 void SUPERVGUI_PortOut::toStudy() {
336     Trace("SUPERVGUI_PortOut::toStudy")
337       if (!main->isFromStudy()) {
338         if (main->addStudy()) main->setAsFromStudy(true);
339       }
340     if (!main->getStudy()->getStudyDocument()->GetProperties()->IsLocked()) study = !study;
341     sync();
342 }
343
344 void SUPERVGUI_PortOut::browse() {
345     Trace("SUPERVGUI_PortOut::browse")
346     QString mess(tr("MSG_OPORT_VAL"));
347     mess += port->ToString();
348     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
349 }
350
351
352 /**
353  * Returns coordinates of connection point in scroll view content coordinates
354  */
355 QPoint SUPERVGUI_PortOut::getConnectPnt() {
356   // Parent widget is internal node frame
357   // parent of parent is node it self
358   QWidget* aNode = parentWidget()->parentWidget();
359   QPoint aPnt(aNode->pos()); // position of the node
360   if (parentWidget()->isVisible()) {
361     aPnt += parentWidget()->pos();
362     return QPoint(aPnt.x() + pos().x() + width(), 
363                   aPnt.y() + (pos().y() + height()/2));
364   }
365   return QPoint(aPnt.x() + aNode-> width(),
366                 aPnt.y() + (aNode->height()/2));
367 }
368
369
370 void SUPERVGUI_PortOut::addLinkPrs(SUPERVGUI_Link* theLink) {
371   myLinksList.append(theLink);
372   connect(theLink, SIGNAL(linkDeleted(SUPERVGUI_Link*)),
373           this, SLOT(onDeleteLink(SUPERVGUI_Link*)));
374 }
375
376
377 void SUPERVGUI_PortOut::onDeleteLink(SUPERVGUI_Link* theLink) {
378   disconnect(theLink, 0, this, 0);
379   myLinksList.remove(theLink);
380 }
381
382
383 void SUPERVGUI_PortOut::deleteLinks() {
384   if (myLinksList.count() > 0) {
385     SUPERVGUI_Link* aLink;
386     while (aLink = myLinksList.last()) {
387       disconnect(aLink, 0, this, 0); 
388       myLinksList.remove(aLink);
389       main->getGraph()->deleteLink(aLink);
390     }
391   }
392 }
393
394
395
396 //***********************************************************
397 // Input Port of EndSwitch Node
398 // ----------------------------
399 SUPERVGUI_PortInESNode::SUPERVGUI_PortInESNode(QWidget* parent, SUPERVGUI_Main* m, SUPERV_Port p)
400   : SUPERVGUI_Port(parent, m, p), myDlg( 0 )
401 {
402   Trace("SUPERVGUI_PortInESNode::SUPERVGUI_PortInESNode");
403   myLinksList.setAutoDelete(false);
404
405   bool editing = port->IsInput() && ( !port->IsLinked() ); //main->isEditable();
406   if ((!port->IsGate())  && editing ) {
407     psc = popup->insertItem(tr("MSG_SETVALUE"),      this, SLOT(setInput()));
408   }
409   browser = popup->insertItem(tr("MSG_BROWSE"),         this, SLOT(browse()));
410 }
411
412 SUPERVGUI_PortInESNode::~SUPERVGUI_PortInESNode() {
413   Trace("SUPERVGUI_PortInESNode::~SUPERVGUI_PortInESNode");
414 }
415
416 bool SUPERVGUI_PortInESNode::eventFilter( QObject* o, QEvent* e ) {
417   if ( o == myDlg && e->type() == QEvent::Close )
418     myDlg = 0;
419   return SUPERVGUI_Port::eventFilter( o, e );
420 }
421
422 void SUPERVGUI_PortInESNode::sync() {
423   SUPERVGUI_Port::sync();
424   if (port->IsLinked()) {
425     popup->setItemEnabled(browser, port->State() == SUPERV_Ready);
426   } else {
427     if (port->HasInput()) {
428       popup->setItemEnabled(browser, true);
429       setLineWidth(2);
430     } else {
431       popup->setItemEnabled(browser, false);
432       setLineWidth(1);
433     }
434   }
435 }
436
437 void SUPERVGUI_PortInESNode::setValue(const char* d) {
438     Trace("SUPERVGUI_PortIn::setValue")
439     if (port->Input((*Supervision.getEngine())->StringValue(d))) {
440         sync();
441     } else {
442         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
443     };
444 }
445
446 /**
447  * Returns coordinates of connection point in content coordinates
448  */
449 QPoint SUPERVGUI_PortInESNode::getConnectPnt() {
450   QWidget* aNode = parentWidget()->parentWidget();
451   QPoint aPnt(aNode->pos());
452
453   if (parentWidget()->isVisible()) {
454     aPnt += parentWidget()->pos();
455     return QPoint(aPnt.x() + pos().x(), 
456                   aPnt.y() + (pos().y() + height()/2));
457   }
458   return QPoint(aPnt.x(), 
459                 aPnt.y() + (aNode->height()/2));
460 }
461
462 void SUPERVGUI_PortInESNode::setLinkPrs(SUPERVGUI_Link* theLink) {
463   myLinksList.append(theLink);
464   connect(theLink, SIGNAL(linkDeleted(SUPERVGUI_Link*)),
465           this, SLOT(onDeleteLink(SUPERVGUI_Link*)));
466 }
467
468 void SUPERVGUI_PortInESNode::deleteLinks() {
469   if (myLinksList.count() > 0) {
470     SUPERVGUI_Link* aLink;
471     while (aLink = myLinksList.last()) {
472       disconnect(aLink, 0, this, 0); 
473       myLinksList.remove(aLink);
474       main->getGraph()->deleteLink(aLink);
475     }
476   }
477 }
478
479 void SUPERVGUI_PortInESNode::setInput() {
480     Trace("SUPERVGUI_PortInESNode::setInput");
481     if ( !myDlg ) {
482       myDlg = new SUPERVGUI_GetValueDlg(this);
483       myDlg->installEventFilter( this );
484     }
485     if ( !myDlg->isVisible() )
486       myDlg->show();
487     else {
488       myDlg->raise();
489       myDlg->setActiveWindow();
490       myDlg->setFocus();
491     }
492 }
493
494 void SUPERVGUI_PortInESNode::browse() {
495     Trace("SUPERVGUI_PortInESNode::browse")
496     QString mess(tr("MSG_IPORT_VAL"));
497     mess += port->ToString();
498     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
499 }
500
501
502 void SUPERVGUI_PortInESNode::onDeleteLink(SUPERVGUI_Link* theLink) {
503   disconnect(theLink, 0, this, 0);
504   myLinksList.remove(theLink);
505   
506   if (myLinksList.count() == 0) {
507     popup->setItemEnabled(psc, true);
508   } else {
509     popup->setItemEnabled(psc, false);
510   }
511   
512   if (port->HasInput())
513     setLineWidth(2);    
514   
515   if (main->isEditable()) {
516     popup->setItemEnabled(mySketchItem, true);
517   } else {
518     popup->setItemEnabled(mySketchItem, false);
519   }
520 }
521