1 // SUPERV SUPERVGUI : GUI for Supervisor component
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SUPERVGUI_ComputeNode.cxx
8 // Author : Vitaly SMETANNIKOV
12 #include "SUPERVGUI_GraphNode.h"
13 #include "SUPERVGUI_Graph.h"
14 #include "SUPERVGUI_Main.h"
17 SUPERVGUI_GraphNode::SUPERVGUI_GraphNode(QWidget* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode)
18 :SUPERVGUI_Node(theParent, theMain, theNode),
22 //disconnect(myTitle, 0, this, 0);
23 myTitle = new SUPERVGUI_Label(this, LABEL_WIDTH, LABEL_HEIGHT, name(), QLabel::AlignLeft);
25 connect(myTitle, SIGNAL(MousePress(QMouseEvent*)), this, SLOT(mouseTitlePress(QMouseEvent*)));
26 connect(myTitle, SIGNAL(MouseMove (QMouseEvent*)), this, SLOT(mouseTitleMove(QMouseEvent*)));
27 connect(myTitle, SIGNAL(MouseRelease(QMouseEvent*)), this, SLOT(mouseTitleRelease(QMouseEvent*)));
29 myPortsBox = new QVBox(0);
30 myPortsBox->setSpacing(3);
31 myValuesBox = new QWidget(myPortsBox);
32 myPortLayout = new QGridLayout(myValuesBox, 0, 2, 0, 1);
34 SUPERV_StreamPorts aStreamPorts = myNode->StreamPorts();
35 int aStreamNb = aStreamPorts->length();
37 myStreamBox = new QFrame(myPortsBox);
38 myStreamBox->setPaletteForegroundColor(red.dark());
39 myStreamBox->setPaletteBackgroundColor(paletteBackgroundColor());
40 myStreamBox->setFrameStyle( QFrame::Box | QFrame::Plain );
41 myStreamBox->setLineWidth(1);
42 myStreamLayout = new QGridLayout(myStreamBox, 0, 2, 2, 3);
47 myGatesBox = new QFrame(myPortsBox);
48 myGatesBox->setFrameStyle( QFrame::Panel | QFrame::Raised );
49 myGatesBox->setLineWidth(2);
50 // myGatesBox->setPaletteBackgroundColor(backgroundColor().dark(105));
51 myGatesBox->setPaletteBackgroundColor(green.light(170));
52 myGatesLayout = new QGridLayout(myGatesBox, 0, 2);
53 myGatesLayout->setMargin(3);
55 myTimer = new QTimer(this);
56 connect(myTimer, SIGNAL(timeout()), this, SLOT(movingNode(/*QMouseEvent* e*/)));
68 SUPERV_Ports ports = myNode->Ports();
69 int n = ports->length();
71 for (int i=0; i<n; i++) {
72 if (ports[i]->IsInput()) {
73 if (getComputingNode()->IsEndSwitch()) {
74 if (ports[i]->IsGate())
75 myGatesLayout->addWidget(new SUPERVGUI_PortInESNode(myGatesBox, myMain, ports[i].in()),
78 myPortLayout->addWidget(new SUPERVGUI_PortInESNode(myValuesBox, myMain, ports[i].in()),
83 if (ports[i]->IsGate())
84 myGatesLayout->addWidget(new SUPERVGUI_PortIn(myGatesBox, myMain, ports[i].in()),
87 myPortLayout->addWidget(new SUPERVGUI_PortIn(myValuesBox, myMain, ports[i].in()),
93 if (ports[i]->IsGate())
94 myGatesLayout->addWidget(new SUPERVGUI_PortOut(myGatesBox, myMain, ports[i].in()),
95 0, 1, Qt::AlignRight);
97 myPortLayout->addWidget(new SUPERVGUI_PortOut(myValuesBox, myMain, ports[i].in()),
98 myPOcount, 1, Qt::AlignRight);
105 for (int i=0; i < aStreamNb; i++) {
106 if (aStreamPorts[i]->IsInput()) {
107 myStreamLayout->addWidget(new SUPERVGUI_StreamPortIn(myStreamBox, myMain, aStreamPorts[i].in()),
111 myStreamLayout->addWidget(new SUPERVGUI_StreamPortOut(myStreamBox, myMain, aStreamPorts[i].in()),
112 myStrPOcount, 1, Qt::AlignRight);
120 SUPERVGUI_GraphNode::~SUPERVGUI_GraphNode() {
123 void SUPERVGUI_GraphNode::mouseTitlePress(QMouseEvent* e) {
124 if (!myMain->getGraph()->isAnyLinkCreating()) {
125 if (e->button() == RightButton) {
130 SUPERVGUI_Graph* sv = myMain->getGraph();
131 MouseX = sv->childX(this) - e->globalX();
132 MouseY = sv->childY(this) - e->globalY();
137 void SUPERVGUI_GraphNode::mouseTitleMove(QMouseEvent* e) {
138 Trace("SUPERVGUI_Node::moveAgain")
139 SUPERVGUI_Graph* sv = myMain->getGraph();
141 if ((MouseX == MouseNo) && (MouseY == MouseNo)) {
142 MouseX = sv->childX(this) - e->globalX();
143 MouseY = sv->childY(this) - e->globalY();
144 MouseLeft = (e->state() == RightButton? 0: 1);
147 if (MouseLeft == 1) {
149 int x = e->globalX()+MouseX;
150 int y = e->globalY()+MouseY;
154 if (x > myMain->getGraph()->horizontalScrollBar()->value()
155 + myMain->getGraph()->viewport()->width() - width()/2
157 y > myMain->getGraph()->verticalScrollBar()->value()
158 + myMain->getGraph()->viewport()->height() - height()/2
160 x < myMain->getGraph()->horizontalScrollBar()->value()
162 y < myMain->getGraph()->verticalScrollBar()->value()){
163 if (!myTimer->isActive()) {
166 myDifX = x - myNode->X();
167 myDifY = y - myNode->Y();
168 myFirstDifX = myDifX;
169 myFirstDifY = myDifY;
175 if (x - myNode->X() > 15 && abs(myDifX) < abs(myDifY)) {
176 if (x > myMain->getGraph()->horizontalScrollBar()->value()
177 + myMain->getGraph()->viewport()->width() - width()/2)
178 myDifX = 5; // or another positive number
180 //not out of the right boundary
181 sv->moveChild(this, x, myNode->Y()); //new X and old Y
182 myNode->Coords(x, myNode->Y());
187 if (x - myNode->X() < -15 && abs(myDifX) < abs(myDifY)) {
188 if(x < myMain->getGraph()->horizontalScrollBar()->value())
189 myDifX = -5; //or another negative number
191 //not out of the left boundary
192 sv->moveChild(this, x, myNode->Y()); //new X and old Y
193 myNode->Coords(x, myNode->Y());
198 if (y - myNode->Y() > 15 && abs(myDifX) > abs(myDifY)) {
199 if (y > myMain->getGraph()->verticalScrollBar()->value()
200 + myMain->getGraph()->viewport()->height() - height()/2)
201 myDifY = 5; //or another positive number
203 //not out of the lower boundary
204 sv->moveChild(this, myNode->X(), y); //old X and new Y
205 myNode->Coords(myNode->X(), y);
210 if (y - myNode->Y() < -15 && abs(myDifX) > abs(myDifY)) {
211 if (y < myMain->getGraph()->verticalScrollBar()->value())
212 myDifY = -5; //or another negative number
214 //not out of the upper boundary
215 sv->moveChild(this, myNode->X(), y); //old X and new Y
216 myNode->Coords(myNode->X(), y);
223 if ((myDifX*(x - myNode->X()) < 0 || myDifY*(y - myNode->Y()) < 0)) {
227 //for mouse moving to boundaries from outside space (chack then node must be move)
228 int xp1, yp1, xp2, yp2;
229 xp1 = myMain->getGraph()->horizontalScrollBar()->value();
230 xp2 = myMain->getGraph()->horizontalScrollBar()->value() +
231 myMain->getGraph()->viewport()->width();
232 yp1 = myMain->getGraph()->verticalScrollBar()->value();
233 yp2 = myMain->getGraph()->verticalScrollBar()->value() +
234 myMain->getGraph()->viewport()->height();
236 && x <= xp1+myMain->getGraph()->viewport()->visibleRect().width()
238 && y <= yp1+myMain->getGraph()->viewport()->visibleRect().height()) {
239 sv->moveChild(this, x, y);
240 myNode->Coords(x, y);
247 void SUPERVGUI_GraphNode::mouseTitleRelease(QMouseEvent* e) {
252 void SUPERVGUI_GraphNode::movingNode() {
253 SUPERVGUI_Graph* sv = myMain->getGraph();
255 myX = myX<0 ? 0: myX;
256 myY = myY<0 ? 0: myY;
267 myMain->getGraph()->ResizeGraph(this, myX, myY);
268 myMain->getGraph()->scrollBy( myX - myNode->X(), myY - myNode->Y() );
269 sv->moveChild(this, myX, myY);
270 myNode->Coords(myX, myY);
273 void SUPERVGUI_GraphNode::StartTimer() {
274 Trace("SUPERVGUI_Main::StartTimer")
275 if (!myTimer->isActive()) {
280 void SUPERVGUI_GraphNode::StopTimer() {
281 Trace("SUPERVGUI_Main::StopTimer")
282 if (myTimer->isActive()) {
287 void SUPERVGUI_GraphNode::deleteLinks() {
288 QObjectList* aList = queryList("SUPERVGUI_Port");
289 SUPERVGUI_Port* aPort;
290 QObjectListIt aIt(*aList);
291 while ((aPort=(SUPERVGUI_Port*)aIt.current()) != 0) {
293 aPort->deleteLinks();
299 void SUPERVGUI_GraphNode::sync() {
300 SUPERVGUI_Node::sync();
303 QObjectList* ihmList = queryList("SUPERVGUI_Port");
304 QObjectListIt i(*ihmList);
305 while ((pi=(SUPERVGUI_Port*)i.current()) != 0) {
311 //check: is creation of link is complete
312 if (myMain->getGraph()->isAnyLinkCreating()) {
313 //not creation complete
314 myPopup->setItemEnabled(myDeleteItem, false);
318 myPopup->setItemEnabled(myDeleteItem, true);
324 * Creates presentation of links to In ports
325 * If toCheckExisting = false the links will be created without checking
326 * of their existing in Graph
327 * If toCheckExisting = true the existing of links will be checked before creation
329 void SUPERVGUI_GraphNode::updateLinksPrs(bool toCheckExisting) {
331 SUPERVGUI_Graph* aGraph= myMain->getGraph();
333 SUPERVGUI_PortInESNode* piES;
334 QObjectList* ihmListES = queryList("SUPERVGUI_PortInESNode");
335 QObjectListIt iES(*ihmListES);
337 while ((piES=(SUPERVGUI_PortInESNode*)iES.current()) != 0) {
339 if (piES->getPort()->IsLinked()) {
340 SUPERV_Links aLinks = piES->getPort()->Links();
341 for (int j=0; j<aLinks->length(); j++) {
342 if (toCheckExisting) {
343 if (aGraph->isLinkPrsExists(aLinks[j]))
346 aGraph->createLinkPrs(aLinks[j]);
352 SUPERVGUI_PortIn* pi;
353 QObjectList* ihmList = queryList("SUPERVGUI_PortIn");
354 QObjectListIt i(*ihmList);
355 while ((pi=(SUPERVGUI_PortIn*)i.current()) != 0) {
357 if (pi->getPort()->IsLinked()) {
358 SUPERV_Link aLink = pi->getPort()->Link();
359 if (toCheckExisting) {
360 if (aGraph->isLinkPrsExists(aLink))
363 aGraph->createLinkPrs(aLink);
373 void SUPERVGUI_GraphNode::setNodeName(QString theName) {
374 SUPERVGUI_Node::setNodeName(theName);
375 myTitle->setText(name());
379 void SUPERVGUI_GraphNode::deletePort(SUPERVGUI_Port* thePort) {
380 if (thePort->isA("SUPERVGUI_PortIn") || thePort->isA("SUPERVGUI_PortInESNode")) {
386 thePort->deleteLinks();
388 thePort->getPort()->destroy();
390 thePort->close(true);
397 void SUPERVGUI_GraphNode::addInputPort() {
398 SUPERV_Port aPort = createInPort();
399 if (aPort == NULL) return;
401 if (getNodeType() == SUPERV::EndSwitchNode) {
402 SUPERVGUI_PortInESNode* aPortPrs = new SUPERVGUI_PortInESNode(myValuesBox, myMain, aPort.in());
403 myPortLayout->addWidget(aPortPrs, myPIcount, 0);
405 if (myPortsBox->isVisible()) {
410 SUPERVGUI_PortIn* aPortPrs = new SUPERVGUI_PortIn(myValuesBox, myMain, aPort.in());
411 myPortLayout->addWidget(aPortPrs, myPIcount, 0);
413 if (myPortsBox->isVisible()) {
424 void SUPERVGUI_GraphNode::addOutputPort() {
425 SUPERV_Port aPort = createOutPort();
426 if (aPort == NULL) return;
428 SUPERVGUI_PortOut* aPortPrs = new SUPERVGUI_PortOut(myValuesBox, myMain, aPort.in());
429 myPortLayout->addWidget(aPortPrs, myPOcount, 1, Qt::AlignRight);
431 if (myPortsBox->isVisible()) {
439 void SUPERVGUI_GraphNode::updatePorts() {
440 bool isAdded = false;
441 SUPERV_Ports aPorts = getEngine()->Ports();
442 int n = aPorts->length();
443 SUPERVGUI_Port* aPortPrs;
444 bool aIsVisible = myPortsBox->isVisible();
445 for (int i=0; i < n; i++) {
446 QString aName(aPorts[i]->Name());
447 aName += (aPorts[i]->IsInput())? "Input":"Output";
448 aPortPrs = (SUPERVGUI_Port*) child(aName, "SUPERVGUI_Port");
449 if (aPortPrs == NULL) {
450 if (aPorts[i]->IsInput()) {
451 SUPERVGUI_PortIn* aPortIn;
452 if (aPorts[i]->IsGate()) {
453 aPortIn = new SUPERVGUI_PortIn(myGatesBox, myMain, aPorts[i].in());
454 myGatesLayout->addWidget(aPortIn, myPIcount, 0);
456 aPortIn = new SUPERVGUI_PortIn(myValuesBox, myMain, aPorts[i].in());
457 myPortLayout->addWidget(aPortIn, myPIcount, 0);
460 if (aIsVisible) aPortIn->show();
463 SUPERVGUI_PortOut* aPortOut;
464 if (aPorts[i]->IsGate()) {
465 aPortOut = new SUPERVGUI_PortOut(myGatesBox, myMain, aPorts[i].in());
466 myGatesLayout->addWidget(aPortOut, myPOcount, 1, Qt::AlignRight);
468 aPortOut = new SUPERVGUI_PortOut(myValuesBox, myMain, aPorts[i].in());
469 myPortLayout->addWidget(aPortOut, myPOcount, 1, Qt::AlignRight);
472 if (aIsVisible) aPortOut->show();
478 QObjectList* aShownPortsList = queryList("SUPERVGUI_Port");
479 QObjectListIt aLI(*aShownPortsList);
480 SUPERVGUI_Port* aVisPort;
483 while ((aVisPort=(SUPERVGUI_Port*)aLI.current()) != 0) {
485 QString aNameVisible(aVisPort->getPort()->Name());
487 bool aIsExists = false;
488 for (int i=0; i < n; i++) {
489 QString aName(aPorts[i]->Name());
490 if (aName == aNameVisible)
494 if (!aIsExists) { //we have a visible object, which has no engine entity
495 aVisPort->close(true);
502 * Returns coordinates of connection point in content coordinates
504 QPoint SUPERVGUI_GraphNode::getInConnectPnt() {
505 return QPoint(pos().x(),
506 pos().y() + (height()/2));
508 QPoint SUPERVGUI_GraphNode::getOutConnectPnt() {
509 return QPoint(pos().x() + width(),
510 pos().y() + (height()/2));