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