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 myGatesBox = new QFrame(myPortsBox);
35 myGatesBox->setFrameStyle( QFrame::Panel | QFrame::Raised );
36 myGatesBox->setLineWidth(2);
37 // myGatesBox->setPaletteBackgroundColor(backgroundColor().dark(105));
38 myGatesBox->setPaletteBackgroundColor(green.light(170));
39 myGatesLayout = new QGridLayout(myGatesBox, 0, 2);
40 myGatesLayout->setMargin(3);
42 myTimer = new QTimer(this);
43 connect(myTimer, SIGNAL(timeout()), this, SLOT(movingNode(/*QMouseEvent* e*/)));
55 SUPERV_Ports ports = myNode->Ports();
56 int n = ports->length();
58 for (int i=0; i<n; i++) {
59 if (ports[i]->IsInput()) {
60 if (getComputingNode()->IsEndSwitch()) {
61 if (ports[i]->IsGate())
62 myGatesLayout->addWidget(new SUPERVGUI_PortInESNode(myGatesBox, myMain, ports[i]),
65 myPortLayout->addWidget(new SUPERVGUI_PortInESNode(myValuesBox, myMain, ports[i]),
70 if (ports[i]->IsGate())
71 myGatesLayout->addWidget(new SUPERVGUI_PortIn(myGatesBox, myMain, ports[i]),
74 myPortLayout->addWidget(new SUPERVGUI_PortIn(myValuesBox, myMain, ports[i]),
80 if (ports[i]->IsGate())
81 myGatesLayout->addWidget(new SUPERVGUI_PortOut(myGatesBox, myMain, ports[i]),
82 0, 1, Qt::AlignRight);
84 myPortLayout->addWidget(new SUPERVGUI_PortOut(myValuesBox, myMain, ports[i]),
85 myPOcount, 1, Qt::AlignRight);
95 SUPERVGUI_GraphNode::~SUPERVGUI_GraphNode() {
98 void SUPERVGUI_GraphNode::mouseTitlePress(QMouseEvent* e) {
99 if (!myMain->getGraph()->isAnyLinkCreating()) {
100 if (e->button() == RightButton) {
105 SUPERVGUI_Graph* sv = myMain->getGraph();
106 MouseX = sv->childX(this) - e->globalX();
107 MouseY = sv->childY(this) - e->globalY();
112 void SUPERVGUI_GraphNode::mouseTitleMove(QMouseEvent* e) {
113 Trace("SUPERVGUI_Node::moveAgain")
114 SUPERVGUI_Graph* sv = myMain->getGraph();
116 if ((MouseX == MouseNo) && (MouseY == MouseNo)) {
117 MouseX = sv->childX(this) - e->globalX();
118 MouseY = sv->childY(this) - e->globalY();
119 MouseLeft = (e->state() == RightButton? 0: 1);
122 if (MouseLeft == 1) {
124 int x = e->globalX()+MouseX;
125 int y = e->globalY()+MouseY;
129 if (x > myMain->getGraph()->horizontalScrollBar()->value()
130 + myMain->getGraph()->viewport()->width() - width()/2
132 y > myMain->getGraph()->verticalScrollBar()->value()
133 + myMain->getGraph()->viewport()->height() - height()/2
135 x < myMain->getGraph()->horizontalScrollBar()->value()
137 y < myMain->getGraph()->verticalScrollBar()->value()){
138 if (!myTimer->isActive()) {
141 myDifX = x - myNode->X();
142 myDifY = y - myNode->Y();
143 myFirstDifX = myDifX;
144 myFirstDifY = myDifY;
150 if (x - myNode->X() > 15 && abs(myDifX) < abs(myDifY)) {
151 if (x > myMain->getGraph()->horizontalScrollBar()->value()
152 + myMain->getGraph()->viewport()->width() - width()/2)
153 myDifX = 5; // or another positive number
155 //not out of the right boundary
156 sv->moveChild(this, x, myNode->Y()); //new X and old Y
157 myNode->Coords(x, myNode->Y());
162 if (x - myNode->X() < -15 && abs(myDifX) < abs(myDifY)) {
163 if(x < myMain->getGraph()->horizontalScrollBar()->value())
164 myDifX = -5; //or another negative number
166 //not out of the left boundary
167 sv->moveChild(this, x, myNode->Y()); //new X and old Y
168 myNode->Coords(x, myNode->Y());
173 if (y - myNode->Y() > 15 && abs(myDifX) > abs(myDifY)) {
174 if (y > myMain->getGraph()->verticalScrollBar()->value()
175 + myMain->getGraph()->viewport()->height() - height()/2)
176 myDifY = 5; //or another positive number
178 //not out of the lower boundary
179 sv->moveChild(this, myNode->X(), y); //old X and new Y
180 myNode->Coords(myNode->X(), y);
185 if (y - myNode->Y() < -15 && abs(myDifX) > abs(myDifY)) {
186 if (y < myMain->getGraph()->verticalScrollBar()->value())
187 myDifY = -5; //or another negative number
189 //not out of the upper boundary
190 sv->moveChild(this, myNode->X(), y); //old X and new Y
191 myNode->Coords(myNode->X(), y);
198 if ((myDifX*(x - myNode->X()) < 0 || myDifY*(y - myNode->Y()) < 0)) {
202 //for mouse moving to boundaries from outside space (chack then node must be move)
203 int xp1, yp1, xp2, yp2;
204 xp1 = myMain->getGraph()->horizontalScrollBar()->value();
205 xp2 = myMain->getGraph()->horizontalScrollBar()->value() +
206 myMain->getGraph()->viewport()->width();
207 yp1 = myMain->getGraph()->verticalScrollBar()->value();
208 yp2 = myMain->getGraph()->verticalScrollBar()->value() +
209 myMain->getGraph()->viewport()->height();
211 && x <= xp1+myMain->getGraph()->viewport()->visibleRect().width()
213 && y <= yp1+myMain->getGraph()->viewport()->visibleRect().height()) {
214 sv->moveChild(this, x, y);
215 myNode->Coords(x, y);
222 void SUPERVGUI_GraphNode::mouseTitleRelease(QMouseEvent* e) {
227 void SUPERVGUI_GraphNode::movingNode() {
228 SUPERVGUI_Graph* sv = myMain->getGraph();
230 myX = myX<0 ? 0: myX;
231 myY = myY<0 ? 0: myY;
242 myMain->getGraph()->ResizeGraph(this, myX, myY);
243 myMain->getGraph()->scrollBy( myX - myNode->X(), myY - myNode->Y() );
244 sv->moveChild(this, myX, myY);
245 myNode->Coords(myX, myY);
248 void SUPERVGUI_GraphNode::StartTimer() {
249 Trace("SUPERVGUI_Main::StartTimer")
250 if (!myTimer->isActive()) {
255 void SUPERVGUI_GraphNode::StopTimer() {
256 Trace("SUPERVGUI_Main::StopTimer")
257 if (myTimer->isActive()) {
262 void SUPERVGUI_GraphNode::deleteLinks() {
263 QObjectList* aList = queryList("SUPERVGUI_Port");
264 SUPERVGUI_Port* aPort;
265 QObjectListIt aIt(*aList);
266 while ((aPort=(SUPERVGUI_Port*)aIt.current()) != 0) {
268 aPort->deleteLinks();
274 void SUPERVGUI_GraphNode::sync() {
275 SUPERVGUI_Node::sync();
278 QObjectList* ihmList = queryList("SUPERVGUI_Port");
279 QObjectListIt i(*ihmList);
280 while ((pi=(SUPERVGUI_Port*)i.current()) != 0) {
286 //check: is creation of link is complete
287 if (myMain->getGraph()->isAnyLinkCreating()) {
288 //not creation complete
289 myPopup->setItemEnabled(myDeleteItem, false);
293 myPopup->setItemEnabled(myDeleteItem, true);
299 * Creates presentation of links to In ports
300 * If toCheckExisting = false the links will be created without checking
301 * of their existing in Graph
302 * If toCheckExisting = true the existing of links will be checked before creation
304 void SUPERVGUI_GraphNode::updateLinksPrs(bool toCheckExisting) {
306 SUPERVGUI_Graph* aGraph= myMain->getGraph();
308 SUPERVGUI_PortInESNode* piES;
309 QObjectList* ihmListES = queryList("SUPERVGUI_PortInESNode");
310 QObjectListIt iES(*ihmListES);
312 while ((piES=(SUPERVGUI_PortInESNode*)iES.current()) != 0) {
314 if (piES->getPort()->IsLinked()) {
315 SUPERV_Links aLinks = piES->getPort()->Links();
316 for (int j=0; j<aLinks->length(); j++) {
317 if (toCheckExisting) {
318 if (aGraph->isLinkPrsExists(aLinks[j]))
321 aGraph->createLinkPrs(aLinks[j]);
327 SUPERVGUI_PortIn* pi;
328 QObjectList* ihmList = queryList("SUPERVGUI_PortIn");
329 QObjectListIt i(*ihmList);
330 while ((pi=(SUPERVGUI_PortIn*)i.current()) != 0) {
332 if (pi->getPort()->IsLinked()) {
333 SUPERV_Link aLink = pi->getPort()->Link();
334 if (toCheckExisting) {
335 if (aGraph->isLinkPrsExists(aLink))
338 aGraph->createLinkPrs(aLink);
348 void SUPERVGUI_GraphNode::setNodeName(QString theName) {
349 SUPERVGUI_Node::setNodeName(theName);
350 myTitle->setText(name());
354 void SUPERVGUI_GraphNode::deletePort(SUPERVGUI_Port* thePort) {
355 if (thePort->isA("SUPERVGUI_PortIn") || thePort->isA("SUPERVGUI_PortInESNode")) {
361 thePort->deleteLinks();
363 thePort->getPort()->destroy();
365 thePort->close(true);
372 void SUPERVGUI_GraphNode::addInputPort() {
373 SUPERV_Port aPort = createInPort();
374 if (aPort == NULL) return;
376 if (getNodeType() == SUPERV::EndSwitchNode) {
377 SUPERVGUI_PortInESNode* aPortPrs = new SUPERVGUI_PortInESNode(myValuesBox, myMain, aPort);
378 myPortLayout->addWidget(aPortPrs, myPIcount, 0);
380 if (myPortsBox->isVisible()) {
385 SUPERVGUI_PortIn* aPortPrs = new SUPERVGUI_PortIn(myValuesBox, myMain, aPort);
386 myPortLayout->addWidget(aPortPrs, myPIcount, 0);
388 if (myPortsBox->isVisible()) {
399 void SUPERVGUI_GraphNode::addOutputPort() {
400 SUPERV_Port aPort = createOutPort();
401 if (aPort == NULL) return;
403 SUPERVGUI_PortOut* aPortPrs = new SUPERVGUI_PortOut(myValuesBox, myMain, aPort);
404 myPortLayout->addWidget(aPortPrs, myPOcount, 1, Qt::AlignRight);
406 if (myPortsBox->isVisible()) {
414 void SUPERVGUI_GraphNode::updatePorts() {
415 bool isAdded = false;
416 SUPERV_Ports aPorts = getEngine()->Ports();
417 int n = aPorts->length();
418 SUPERVGUI_Port* aPortPrs;
419 bool aIsVisible = myPortsBox->isVisible();
420 for (int i=0; i < n; i++) {
421 QString aName(aPorts[i]->Name());
422 aName += (aPorts[i]->IsInput())? "Input":"Output";
423 aPortPrs = (SUPERVGUI_Port*) child(aName, "SUPERVGUI_Port");
424 if (aPortPrs == NULL) {
425 if (aPorts[i]->IsInput()) {
426 SUPERVGUI_PortIn* aPortIn;
427 if (aPorts[i]->IsGate()) {
428 aPortIn = new SUPERVGUI_PortIn(myGatesBox, myMain, aPorts[i]);
429 myGatesLayout->addWidget(aPortIn, myPIcount, 0);
431 aPortIn = new SUPERVGUI_PortIn(myValuesBox, myMain, aPorts[i]);
432 myPortLayout->addWidget(aPortIn, myPIcount, 0);
435 if (aIsVisible) aPortIn->show();
438 SUPERVGUI_PortOut* aPortOut;
439 if (aPorts[i]->IsGate()) {
440 aPortOut = new SUPERVGUI_PortOut(myGatesBox, myMain, aPorts[i]);
441 myGatesLayout->addWidget(aPortOut, myPOcount, 1, Qt::AlignRight);
443 aPortOut = new SUPERVGUI_PortOut(myValuesBox, myMain, aPorts[i]);
444 myPortLayout->addWidget(aPortOut, myPOcount, 1, Qt::AlignRight);
447 if (aIsVisible) aPortOut->show();
453 QObjectList* aShownPortsList = queryList("SUPERVGUI_Port");
454 QObjectListIt aLI(*aShownPortsList);
455 SUPERVGUI_Port* aVisPort;
458 while ((aVisPort=(SUPERVGUI_Port*)aLI.current()) != 0) {
460 QString aNameVisible(aVisPort->getPort()->Name());
462 bool aIsExists = false;
463 for (int i=0; i < n; i++) {
464 QString aName(aPorts[i]->Name());
465 if (aName == aNameVisible)
469 if (!aIsExists) { //we have a visible object, which has no engine entity
470 aVisPort->close(true);
477 * Returns coordinates of connection point in content coordinates
479 QPoint SUPERVGUI_GraphNode::getInConnectPnt() {
480 return QPoint(pos().x(),
481 pos().y() + (height()/2));
483 QPoint SUPERVGUI_GraphNode::getOutConnectPnt() {
484 return QPoint(pos().x() + width(),
485 pos().y() + (height()/2));