Salome HOME
Modify the version number of SALOME: 2.2.0.
[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_ptr p):
45     SUPERVGUI_Label(parent, PORT_WIDTH, PORT_HEIGHT, "", QLabel::AlignLeft | QLabel::AlignVCenter),
46     main(m)
47 {
48   Trace("SUPERVGUI_Port::SUPERVGUI_Port");
49   port = SUPERV::Port::_duplicate(p);
50   myName = port->Name();
51   QString aName(port->Name());
52   aName += (port->IsInput())? "Input":"Output";
53   setName(aName); 
54
55   if (!port->IsInput() && port->IsGate())
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   //for Loop Node: enable "Delete" popup item only for the input port 
74   if ( ( port->IsInLine() 
75          && 
76          ( port->Node()->Kind()!=SUPERV::EndLoopNode )
77          &&
78          !( port->Node()->Kind()==SUPERV::LoopNode && !port->IsInput() )
79        )
80        ||
81        ( port->IsEndSwitch() && port->IsInput())) {
82     myDelItem = popup->insertItem(tr("ITM_DEL_PORT"), this, SLOT(deletePort()));    
83   }
84   browser = popup->insertItem(tr("MSG_BROWSE"),this, SLOT(browse()));
85
86   connect(this, SIGNAL(MousePress(QMouseEvent*)), this, SLOT(showPopup(QMouseEvent*)));  
87   connect(this, SIGNAL(MouseRelease(QMouseEvent*)), this, SLOT(endSketch()));
88 }
89
90 SUPERVGUI_Port::~SUPERVGUI_Port() {
91     Trace("SUPERVGUI_Port::~SUPERVGUI_Port")
92 }
93
94 SUPERV_Port SUPERVGUI_Port::getPort() {
95     Trace("SUPERVGUI_Port::getPort")
96     return(port);
97 }
98
99 void SUPERVGUI_Port::showPopup(QMouseEvent* e) {
100   Trace("SUPERVGUI_Port::showPopup");
101   if (e->button() != RightButton) return;
102   if (main->getDataflow()->IsExecuting()) 
103     popup->setItemEnabled(mySketchItem, false);
104   /*  else  
105     if (!port->IsInput() //output port
106         || 
107         port->IsInput() && (!port->IsLinked() //not linked input port
108                             ||
109                             port->Kind() == SUPERV::EndSwitchParameter)) { //input port of EndSwitch node
110       popup->setItemEnabled(mySketchItem, true);
111     }
112   */      
113   main->showPopup(popup, e);
114 }
115
116
117 void SUPERVGUI_Port::browse() {
118     Trace("SUPERVGUI_Port::browse")
119     QString mess = (port->IsInput())? (tr("MSG_IPORT_VAL")) : (tr("MSG_OPORT_VAL"));
120     mess += port->ToString();
121     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
122 }
123
124
125 QPoint SUPERVGUI_Port::getConnectPnt() {
126   return QPoint(0,0);
127 }
128
129
130 void SUPERVGUI_Port::sync() {
131   if (port->IsParam() || port->IsInLine())
132     setText(myName + "=" +  port->ToString());
133   else
134     setText(myName);
135
136 }
137
138
139 void SUPERVGUI_Port::sketchLink() {
140   main->getGraph()->sketchBegin(this);
141   if (port->IsInput()) 
142     setLineWidth(1);
143 }
144
145 void SUPERVGUI_Port::endSketch() {
146   main->getGraph()->sketchEnd(this);
147   if (port->IsInput() && port->IsLinked()) 
148     setLineWidth(1);
149 }
150
151
152 void SUPERVGUI_Port::deletePort() {
153   SUPERVGUI_GraphNode*  aNode = (SUPERVGUI_GraphNode* )main->getGraph()->child(port->Node()->Name(), "SUPERVGUI_GraphNode");
154   if (aNode) {
155     aNode->deletePort(this);
156   }
157 }
158
159
160
161
162 //***********************************************************
163 // Port d'entree
164 // -------------
165
166 SUPERVGUI_PortIn::SUPERVGUI_PortIn(QWidget* parent, SUPERVGUI_Main* m, SUPERV::Port_ptr p)
167   : SUPERVGUI_Port(parent, m, p), myDlg( 0 )
168 {
169   Trace("SUPERVGUI_PortIn::SUPERVGUI_PortIn");
170   myLinkPrs = 0;
171   //    link->move(0, height()/2-POINT_SIZE_HALF);
172
173   bool editing = port->IsInput() && ( !port->IsLinked() ); //main->isEditable();
174 //  bool linked  = port->IsLinked();
175 //  bool b = (!linked) && editing;
176   if ((!port->IsGate())  && editing ) {
177     psc = popup->insertItem(tr("MSG_SETVALUE"),      this, SLOT(setInput()));
178   }
179   //  browser = popup->insertItem(tr("MSG_BROWSE"),         this, SLOT(browse()));
180 }
181
182 SUPERVGUI_PortIn::~SUPERVGUI_PortIn() {
183   Trace("SUPERVGUI_PortIn::~SUPERVGUI_PortIn");
184 }
185
186
187 bool SUPERVGUI_PortIn::eventFilter( QObject* o, QEvent* e )
188 {
189   if ( o == myDlg && e->type() == QEvent::Close )
190     myDlg = 0;
191   return SUPERVGUI_Port::eventFilter( o, e );
192 }
193
194 void SUPERVGUI_PortIn::showPopup(QMouseEvent* e){
195   if (e->button() != RightButton) return;
196   if (main->getDataflow()->IsExecuting()) 
197     popup->setItemEnabled(mySketchItem, false);
198   else {
199     popup->setItemEnabled(mySketchItem, !port->IsLinked());
200     popup->setItemEnabled(psc, !port->IsLinked());
201   }
202   main->showPopup(popup, e);
203 }
204
205 void SUPERVGUI_PortIn::deleteLinks() {
206   if (myLinkPrs) {
207     disconnect(myLinkPrs, 0, this, 0);
208     main->getGraph()->deleteLink(myLinkPrs);
209     myLinkPrs = 0;
210   }
211 }
212
213
214 void SUPERVGUI_PortIn::sync() {
215   SUPERVGUI_Port::sync();
216   //  if (port->IsLinked()) {
217   //  popup->setItemEnabled(browser, port->State() == SUPERV_Ready);
218   //} else {
219   if (!port->IsLinked()) {
220     if (port->HasInput()) {
221       //  popup->setItemEnabled(browser, true);
222       setLineWidth(2);
223     } else {
224       //popup->setItemEnabled(browser, false);
225       setLineWidth(1);
226     }
227   }
228 }
229
230 void SUPERVGUI_PortIn::setValue(const char* d) {
231     Trace("SUPERVGUI_PortIn::setValue")
232     if (port->Input(Supervision.getEngine()->StringValue(d))) {
233         sync();
234     } else {
235         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
236     };
237 }
238
239 void SUPERVGUI_PortIn::setInput() {
240     Trace("SUPERVGUI_PortIn::setInput");
241     if ( !myDlg ) {
242       myDlg = new SUPERVGUI_GetValueDlg(this);
243       myDlg->installEventFilter( this );
244     }
245     if ( !myDlg->isVisible() )
246       myDlg->show();
247     else {
248       myDlg->raise();
249       myDlg->setActiveWindow();
250       myDlg->setFocus();
251     }
252 }
253
254 /*void SUPERVGUI_PortIn::browse() {
255     Trace("SUPERVGUI_PortIn::browse")
256     QString mess(tr("MSG_IPORT_VAL"));
257     mess += port->ToString();
258     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
259     }*/
260
261
262
263 /**
264  * Returns coordinates of connection point in content coordinates
265  */
266 QPoint SUPERVGUI_PortIn::getConnectPnt() {
267   // Parent widget is internal node frame
268   // parent of parent is node it self
269   QWidget* aNode = parentWidget()->parentWidget()->parentWidget();
270   QPoint aPnt(aNode->pos()); // position of the node
271
272   if (parentWidget()->isVisible()) {
273     aPnt += parentWidget()->parentWidget()->pos();
274     aPnt += parentWidget()->pos();
275     return QPoint(aPnt.x() + pos().x(), 
276                   aPnt.y() + (pos().y() + height()/2));
277   }
278   return QPoint(aPnt.x(), 
279                 aPnt.y() + (aNode->height()/2));
280 }
281
282 void SUPERVGUI_PortIn::setLinkPrs(SUPERVGUI_Link* theLink) {
283   myLinkPrs = theLink;
284   connect(myLinkPrs, SIGNAL(linkDeleted(SUPERVGUI_Link*)),
285           this, SLOT(onDeleteLink(SUPERVGUI_Link*)));
286   popup->setItemEnabled(mySketchItem, false);
287 }
288
289 void SUPERVGUI_PortIn::onDeleteLink(SUPERVGUI_Link*) {
290   disconnect(myLinkPrs, 0, this, 0);
291   myLinkPrs = 0;
292   popup->setItemEnabled(psc, true);
293   if (port->HasInput())
294     setLineWidth(2);
295   if (main->isEditable()) {
296     popup->setItemEnabled(mySketchItem, true);
297   } else {
298     popup->setItemEnabled(mySketchItem, false);
299   }
300 }
301
302
303
304 //***********************************************************
305 // Port de sortie
306 // --------------
307
308 SUPERVGUI_PortOut::SUPERVGUI_PortOut(QWidget* parent, SUPERVGUI_Main* m, SUPERV::Port_ptr p):
309     SUPERVGUI_Port(parent, m, p),
310     study(false)
311 {
312     Trace("SUPERVGUI_PortOut::SUPERVGUI_PortOut")
313       //    link->move(width()-POINT_SIZE, height()/2-POINT_SIZE_HALF);
314     myLinksList.setAutoDelete(false);
315     
316     if (!port->IsGate()) {
317       pls = popup->insertItem(tr("MSG_PUT_INSTUDY"), this, SLOT(toStudy()));
318     }
319     //browser = popup->insertItem(tr("MSG_BROWSE"), this, SLOT(browse()));
320
321     if (!main->getDataflow()->IsReadOnly()) {
322       popup->setItemEnabled(mySketchItem, true);
323     } else {
324       popup->setItemEnabled(mySketchItem, false);
325     }
326 }
327
328 SUPERVGUI_PortOut::~SUPERVGUI_PortOut() {
329   Trace("SUPERVGUI_PortOut::~SUPERVGUI_PortOut");
330 }
331
332 void SUPERVGUI_PortOut::sync() {
333     Trace("SUPERVGUI_PortOut::sync")
334     SUPERVGUI_Port::sync();
335
336     bool ok = (port->State() == SUPERV_Ready);
337     //    popup->setItemEnabled(browser, ok);
338     if (study && ok) {
339       study = main->putDataStudy(port, STUDY_PORT_OUT);
340     }
341
342     if (study) {
343         setLineWidth(2);
344         popup->changeItem(pls, tr("MSG_NOT_INSTUDY"));
345     } else {
346         setLineWidth(1);
347         popup->changeItem(pls, tr("MSG_PUT_INSTUDY"));
348     }
349
350 }
351
352 void SUPERVGUI_PortOut::updateStudy() {
353   Trace("SUPERVGUI_PortOut::updateStudy")
354   SUPERVGUI_Node*  aNode = (SUPERVGUI_Node* )main->getGraph()->child(port->Node()->Name(), "SUPERVGUI_Node");
355   if (aNode)
356     ((SUPERVGUI_PortOut*)aNode->child(port->Name(), "SUPERVGUI_PortOut"))->study = study;
357   else
358     {
359       aNode = (SUPERVGUI_Node* )main->getArray()->child(port->Node()->Name(), "SUPERVGUI_Node");
360       if (aNode)
361         ((SUPERVGUI_PortOut*)aNode->child(port->Name(), "SUPERVGUI_PortOut"))->study = study;
362     }
363 }
364
365 void SUPERVGUI_PortOut::toStudy() {
366     Trace("SUPERVGUI_PortOut::toStudy")
367
368     if (main->getStudy()->getStudyDocument()->GetProperties()->IsLocked()) {
369       QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"));
370       return;
371     }
372
373     if (!main->isFromStudy()) {
374       if (main->addStudy()) main->setAsFromStudy(true);
375     }
376     study = !study;
377     sync();
378 }
379
380 /*void SUPERVGUI_PortOut::browse() {
381     Trace("SUPERVGUI_PortOut::browse")
382     QString mess(tr("MSG_OPORT_VAL"));
383     mess += port->ToString();
384     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
385     }*/
386
387
388 /**
389  * Returns coordinates of connection point in scroll view content coordinates
390  */
391 QPoint SUPERVGUI_PortOut::getConnectPnt() {
392   // Parent widget is internal node frame
393   // parent of parent is node it self
394   QWidget* aNode = parentWidget()->parentWidget()->parentWidget();
395   QPoint aPnt(aNode->pos()); // position of the node
396   if (parentWidget()->isVisible()) {
397     aPnt += parentWidget()->parentWidget()->pos();
398     aPnt += parentWidget()->pos();
399     return QPoint(aPnt.x() + pos().x() + width(), 
400                   aPnt.y() + (pos().y() + height()/2));
401   }
402   return QPoint(aPnt.x() + aNode-> width(),
403                 aPnt.y() + (aNode->height()/2));
404 }
405
406
407 void SUPERVGUI_PortOut::addLinkPrs(SUPERVGUI_Link* theLink) {
408   myLinksList.append(theLink);
409   connect(theLink, SIGNAL(linkDeleted(SUPERVGUI_Link*)),
410           this, SLOT(onDeleteLink(SUPERVGUI_Link*)));
411 }
412
413
414 void SUPERVGUI_PortOut::onDeleteLink(SUPERVGUI_Link* theLink) {
415   disconnect(theLink, 0, this, 0);
416   myLinksList.remove(theLink);
417 }
418
419
420 void SUPERVGUI_PortOut::deleteLinks() {
421   if (myLinksList.count() > 0) {
422     while (SUPERVGUI_Link* aLink = myLinksList.last()) {
423       disconnect(aLink, 0, this, 0); 
424       myLinksList.remove(aLink);
425       main->getGraph()->deleteLink(aLink);
426     }
427   }
428 }
429
430
431
432 // ----------------------------
433 // Input Port of EndSwitch Node
434 // ----------------------------
435 SUPERVGUI_PortInESNode::SUPERVGUI_PortInESNode(QWidget* parent, SUPERVGUI_Main* m, SUPERV::Port_ptr p)
436   : SUPERVGUI_Port(parent, m, p), myDlg( 0 )
437 {
438   Trace("SUPERVGUI_PortInESNode::SUPERVGUI_PortInESNode");
439   myLinksList.setAutoDelete(false);
440
441   bool editing = port->IsInput() && ( !port->IsLinked() ); //main->isEditable();
442   if ((!port->IsGate())  && editing ) {
443     psc = popup->insertItem(tr("MSG_SETVALUE"),this, SLOT(setInput()));
444   }
445   //  browser = popup->insertItem(tr("MSG_BROWSE"),this, SLOT(browse()));
446 }
447
448 SUPERVGUI_PortInESNode::~SUPERVGUI_PortInESNode() {
449   Trace("SUPERVGUI_PortInESNode::~SUPERVGUI_PortInESNode");
450 }
451
452 bool SUPERVGUI_PortInESNode::eventFilter( QObject* o, QEvent* e ) {
453   if ( o == myDlg && e->type() == QEvent::Close )
454     myDlg = 0;
455   return SUPERVGUI_Port::eventFilter( o, e );
456 }
457
458 void SUPERVGUI_PortInESNode::sync() {
459   SUPERVGUI_Port::sync();
460   /*  if (port->IsLinked()) {
461     popup->setItemEnabled(browser, port->State() == SUPERV_Ready);
462     } else {*/
463   if (!port->IsLinked()) {
464     if (port->HasInput()) {
465       //      popup->setItemEnabled(browser, true);
466       setLineWidth(2);
467     } else {
468       //      popup->setItemEnabled(browser, false);
469       setLineWidth(1);
470     }
471   }
472 }
473
474 void SUPERVGUI_PortInESNode::setValue(const char* d) {
475     Trace("SUPERVGUI_PortIn::setValue")
476     if (port->Input(Supervision.getEngine()->StringValue(d))) {
477         sync();
478     } else {
479         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SETVAL"));
480     };
481 }
482
483 /**
484  * Returns coordinates of connection point in content coordinates
485  */
486 QPoint SUPERVGUI_PortInESNode::getConnectPnt() {
487   QWidget* aNode = parentWidget()->parentWidget()->parentWidget();
488   QPoint aPnt(aNode->pos());
489
490   if (parentWidget()->isVisible()) {
491     aPnt += parentWidget()->parentWidget()->pos();
492     aPnt += parentWidget()->pos();
493     return QPoint(aPnt.x() + pos().x(), 
494                   aPnt.y() + (pos().y() + height()/2));
495   }
496   return QPoint(aPnt.x(), 
497                 aPnt.y() + (aNode->height()/2));
498 }
499
500 void SUPERVGUI_PortInESNode::setLinkPrs(SUPERVGUI_Link* theLink) {
501   myLinksList.append(theLink);
502   connect(theLink, SIGNAL(linkDeleted(SUPERVGUI_Link*)),
503           this, SLOT(onDeleteLink(SUPERVGUI_Link*)));
504 }
505
506 void SUPERVGUI_PortInESNode::deleteLinks() {
507   if (myLinksList.count() > 0) {
508     while (SUPERVGUI_Link* aLink = myLinksList.last()) {
509       disconnect(aLink, 0, this, 0); 
510       myLinksList.remove(aLink);
511       main->getGraph()->deleteLink(aLink);
512     }
513   }
514 }
515
516 void SUPERVGUI_PortInESNode::setInput() {
517     Trace("SUPERVGUI_PortInESNode::setInput");
518     if ( !myDlg ) {
519       myDlg = new SUPERVGUI_GetValueDlg(this);
520       myDlg->installEventFilter( this );
521     }
522     if ( !myDlg->isVisible() )
523       myDlg->show();
524     else {
525       myDlg->raise();
526       myDlg->setActiveWindow();
527       myDlg->setFocus();
528     }
529 }
530
531 /*void SUPERVGUI_PortInESNode::browse() {
532     Trace("SUPERVGUI_PortInESNode::browse")
533     QString mess(tr("MSG_IPORT_VAL"));
534     mess += port->ToString();
535     QMessageBox::information(QAD_Application::getDesktop(), tr("MSG_INFO"), mess);
536     }*/
537
538
539 void SUPERVGUI_PortInESNode::onDeleteLink(SUPERVGUI_Link* theLink) {
540   disconnect(theLink, 0, this, 0);
541   myLinksList.remove(theLink);
542   
543   if (myLinksList.count() == 0) {
544     popup->setItemEnabled(psc, true);
545   } else {
546     popup->setItemEnabled(psc, false);
547   }
548   
549   if (port->HasInput())
550     setLineWidth(2);    
551   
552   if (main->isEditable()) {
553     popup->setItemEnabled(mySketchItem, true);
554   } else {
555     popup->setItemEnabled(mySketchItem, false);
556   }
557 }
558
559
560
561 // ----------------------------
562 // Stream Ports
563 // ----------------------------
564
565 SUPERVGUI_StreamPortIn::SUPERVGUI_StreamPortIn(QWidget* parent, SUPERVGUI_Main* m, SUPERV::StreamPort_ptr p):
566   SUPERVGUI_PortIn(parent, m, p)
567 {
568   myPort = SUPERV::StreamPort::_duplicate(p);
569   myParamItem = popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
570 }
571
572 SUPERVGUI_StreamPortIn::~SUPERVGUI_StreamPortIn()
573 {
574 }
575
576
577 void SUPERVGUI_StreamPortIn::setParams()
578 {
579   SUPERVGUI_StreamInDlg* aDlg = new SUPERVGUI_StreamInDlg(this);
580   aDlg->exec();
581   delete aDlg;
582 }
583
584
585 void SUPERVGUI_StreamPortIn::sync() 
586 {
587   SUPERVGUI_PortIn::sync();
588   setText(myName + "=" +  myPort->ToString());
589 }
590
591
592 //------------------------------------------------------------------------------------------------
593
594 SUPERVGUI_StreamPortOut::SUPERVGUI_StreamPortOut(QWidget* parent, SUPERVGUI_Main* m, SUPERV::StreamPort_ptr p):
595   SUPERVGUI_PortOut(parent, m, p)
596 {
597   myPort = SUPERV::StreamPort::_duplicate(p);
598   myParamItem = popup->insertItem(tr("MSG_STREAM_PARAM"),this, SLOT(setParams()));
599 }
600
601 SUPERVGUI_StreamPortOut::~SUPERVGUI_StreamPortOut()
602 {
603 }
604
605 void SUPERVGUI_StreamPortOut::setParams()
606 {
607   SUPERVGUI_StreamOutDlg* aDlg = new SUPERVGUI_StreamOutDlg(this);
608   aDlg->exec();
609   delete aDlg;
610 }
611
612
613 void SUPERVGUI_StreamPortOut::sync() 
614 {
615   SUPERVGUI_PortOut::sync();
616   setText(myName + "=" +  myPort->ToString());
617 }
618
619
620 //------------------------------------------------------------------------------------------------