]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_GraphNode.cxx
Salome HOME
NRI : Correction 1.1a version.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_GraphNode.cxx
1 using namespace std;
2 //  File      : SUPERVGUI_ComputeNode.cxx
3 //  Created   : 09 / 01 / 2003
4 //  Author    : Vitaly SMETANNIKOV
5 //  Project   : SALOME 
6 //  Module    : SUPERVGUI
7 //  Copyright : Open CASCADE 
8
9 #include "SUPERVGUI_GraphNode.h"
10 #include "SUPERVGUI_Graph.h"
11 #include "SUPERVGUI_Main.h"
12
13
14 SUPERVGUI_GraphNode::SUPERVGUI_GraphNode(QWidget* theParent, SUPERVGUI_Main* theMain, SUPERV_CNode theNode)
15   :SUPERVGUI_Node(theParent, theMain, theNode),
16    MouseX(MouseNo),
17    MouseY(MouseNo)
18 {
19   //disconnect(myTitle, 0, this, 0);
20   myTitle = new SUPERVGUI_Label(this, LABEL_WIDTH, LABEL_HEIGHT, name(), QLabel::AlignLeft);
21   myTitle->hide();
22   connect(myTitle, SIGNAL(MousePress(QMouseEvent*)), this, SLOT(mouseTitlePress(QMouseEvent*)));
23   connect(myTitle, SIGNAL(MouseMove (QMouseEvent*)), this, SLOT(mouseTitleMove(QMouseEvent*)));
24   connect(myTitle, SIGNAL(MouseRelease(QMouseEvent*)), this, SLOT(mouseTitleRelease(QMouseEvent*)));
25
26   myPortsBox = new QFrame(0);
27   myPortLayout = new QGridLayout(myPortsBox, 0, 2, 0, 1);
28   
29   myTimer = new QTimer(this);
30   connect(myTimer, SIGNAL(timeout()), this, SLOT(movingNode(/*QMouseEvent* e*/)));
31   myStopFlag = true;
32
33   myX = -1;
34   myY = -1;
35   myDifX = 0;
36   myDifY = 0;
37   myFirstDifX = 0;
38   myFirstDifY = 0;
39
40   myPIcount = 0;
41   myPOcount = 0;
42   SUPERV_Ports       ports = myNode->Ports();
43   int                n     = ports->length();
44
45   for (int i=0; i<n; i++) {
46     if (ports[i]->IsInput()) {
47       myPortLayout->addWidget(new SUPERVGUI_PortIn(myPortsBox, myMain, ports[i]), 
48                              myPIcount, 0);
49       myPIcount++;
50     } else {
51       myPortLayout->addWidget(new SUPERVGUI_PortOut(myPortsBox, myMain, ports[i]), 
52                              myPOcount, 1, Qt::AlignRight);
53       myPOcount++;
54     }
55   }
56 }
57
58
59
60 SUPERVGUI_GraphNode::~SUPERVGUI_GraphNode() {
61 }
62
63 void SUPERVGUI_GraphNode::mouseTitlePress(QMouseEvent* e) {
64   if (e->button() == RightButton) {
65     MouseLeft       = 0;
66     showPopup(e);
67   } else {
68     MouseLeft       = 1;
69     SUPERVGUI_Graph* sv = myMain->getGraph();
70     MouseX          = sv->childX(this) - e->globalX();
71     MouseY          = sv->childY(this) - e->globalY();
72   }
73 }
74
75 void SUPERVGUI_GraphNode::mouseTitleMove(QMouseEvent* e) {
76     Trace("SUPERVGUI_Node::moveAgain")
77     SUPERVGUI_Graph* sv = myMain->getGraph();
78
79     if ((MouseX == MouseNo) && (MouseY == MouseNo)) {
80         MouseX    = sv->childX(this) - e->globalX();
81         MouseY    = sv->childY(this) - e->globalY();
82         MouseLeft = (e->state() == RightButton? 0: 1);
83     };
84
85     if (MouseLeft == 1) {
86
87       int x = e->globalX()+MouseX;
88       int y = e->globalY()+MouseY;
89       x = x<0? 0: x;
90       y = y<0? 0: y;
91
92         if (x > myMain->getGraph()->horizontalScrollBar()->value()
93             + myMain->getGraph()->viewport()->width() - width()/2
94             ||
95             y > myMain->getGraph()->verticalScrollBar()->value()
96             + myMain->getGraph()->viewport()->height() - height()/2 
97             ||
98             x < myMain->getGraph()->horizontalScrollBar()->value()
99             ||
100             y < myMain->getGraph()->verticalScrollBar()->value()){
101           if (!myTimer->isActive()) {
102             myX = x;
103             myY = y;
104             myDifX = x - myNode->X();
105             myDifY = y - myNode->Y();
106             myFirstDifX = myDifX;
107             myFirstDifY = myDifY;
108           }
109
110           StartTimer();
111           myStopFlag = false;
112
113           if (x - myNode->X() > 15 && abs(myDifX) < abs(myDifY)) {
114             if (x > myMain->getGraph()->horizontalScrollBar()->value()
115                 + myMain->getGraph()->viewport()->width() - width()/2)
116               myDifX = 5; // or another positive number
117             else {
118               //not out of the right boundary
119               sv->moveChild(this, x, myNode->Y()); //new X and old Y
120               myNode->Coords(x, myNode->Y());
121               myX = x;
122             }
123           }
124
125           if (x - myNode->X() < -15 && abs(myDifX) < abs(myDifY)) {
126             if(x < myMain->getGraph()->horizontalScrollBar()->value())
127               myDifX = -5; //or another negative number
128             else {
129               //not out of the left boundary
130               sv->moveChild(this, x, myNode->Y()); //new X and old Y
131               myNode->Coords(x, myNode->Y());
132               myX = x;
133             }
134           }
135
136           if (y - myNode->Y() > 15 && abs(myDifX) > abs(myDifY)) {
137             if (y > myMain->getGraph()->verticalScrollBar()->value() 
138                 + myMain->getGraph()->viewport()->height() - height()/2)
139               myDifY = 5; //or another positive number
140             else {
141               //not out of the lower boundary
142               sv->moveChild(this, myNode->X(), y); //old X and new Y
143               myNode->Coords(myNode->X(), y);
144               myY = y;
145             }
146           }
147
148           if (y - myNode->Y() < -15 && abs(myDifX) > abs(myDifY)) {
149             if (y < myMain->getGraph()->verticalScrollBar()->value())
150               myDifY = -5; //or another negative number
151             else {
152               //not out of the upper boundary
153               sv->moveChild(this, myNode->X(), y); //old X and new Y
154               myNode->Coords(myNode->X(), y);
155               myY = y;
156             }
157           }
158
159         } 
160         else { 
161           if ((myDifX*(x - myNode->X()) < 0 || myDifY*(y - myNode->Y()) < 0)) {
162             StopTimer();
163             myStopFlag = true;
164           }
165           //for mouse moving to boundaries from outside space (chack then node must be move)
166           int xp1, yp1, xp2, yp2;
167           xp1 = myMain->getGraph()->horizontalScrollBar()->value();
168           xp2 = myMain->getGraph()->horizontalScrollBar()->value() +
169             myMain->getGraph()->viewport()->width();
170           yp1 = myMain->getGraph()->verticalScrollBar()->value();
171           yp2 = myMain->getGraph()->verticalScrollBar()->value() +
172             myMain->getGraph()->viewport()->height();
173           if (x >= xp1
174               && x <= xp1+myMain->getGraph()->viewport()->visibleRect().width() 
175               && y >= yp1 
176               && y <= yp1+myMain->getGraph()->viewport()->visibleRect().height()) {
177             sv->moveChild(this, x, y);
178             myNode->Coords(x, y);
179           }
180           
181         }
182     }
183 }
184
185 void SUPERVGUI_GraphNode::mouseTitleRelease(QMouseEvent* e) {
186   StopTimer();
187   myStopFlag = true;
188 }
189
190 void SUPERVGUI_GraphNode::movingNode() {
191   SUPERVGUI_Graph* sv = myMain->getGraph();
192
193   myX = myX<0 ? 0: myX;
194   myY = myY<0 ? 0: myY;
195
196   if (myDifX > 0)
197     myX = myX + 5;
198   if (myDifX < 0)
199     myX = myX - 5;
200   if (myDifY > 0)
201     myY = myY + 5;
202   if (myDifY < 0)
203     myY = myY - 5;
204
205   myMain->getGraph()->ResizeGraph(this, myX, myY);
206   myMain->getGraph()->scrollBy( myX - myNode->X(), myY - myNode->Y() );
207   sv->moveChild(this, myX, myY);
208   myNode->Coords(myX, myY);
209 }
210
211 void SUPERVGUI_GraphNode::StartTimer() {
212     Trace("SUPERVGUI_Main::StartTimer")
213     if (!myTimer->isActive()) {
214       myTimer->start(170);
215     };
216 }
217
218 void SUPERVGUI_GraphNode::StopTimer() {
219     Trace("SUPERVGUI_Main::StopTimer")
220     if (myTimer->isActive()) {
221       myTimer->stop();
222     };
223 }
224
225 void SUPERVGUI_GraphNode::deleteLinks() {
226   QObjectList* aList = queryList("SUPERVGUI_Port");
227   SUPERVGUI_Port* aPort;
228   QObjectListIt aIt(*aList);
229   while ((aPort=(SUPERVGUI_Port*)aIt.current()) != 0) {
230     ++aIt;
231     aPort->deleteLinks();
232   }
233   delete aList;
234 }
235
236
237 void SUPERVGUI_GraphNode::sync() {
238   SUPERVGUI_Node::sync();
239
240   SUPERVGUI_Port* pi;
241   QObjectList* ihmList = queryList("SUPERVGUI_Port");
242   QObjectListIt i(*ihmList);
243   while ((pi=(SUPERVGUI_Port*)i.current()) != 0) {
244     ++i;
245     pi->sync();
246   }
247   delete ihmList;
248 }
249
250
251 /**
252  * Creates presentation of links to In ports
253  * If toCheckExisting = false the links will be created without checking
254  * of their existing in Graph
255  * If toCheckExisting = true the existing of links will be checked before creation
256  */
257 void SUPERVGUI_GraphNode::updateLinksPrs(bool toCheckExisting) {
258   SUPERVGUI_Graph* aGraph= myMain->getGraph();
259   SUPERVGUI_PortIn* pi;
260   QObjectList* ihmList = queryList("SUPERVGUI_PortIn");
261   QObjectListIt i(*ihmList);
262   while ((pi=(SUPERVGUI_PortIn*)i.current()) != 0) {
263     ++i;
264     if (pi->getPort()->IsLinked()) {
265       SUPERV_Link aLink = pi->getPort()->Link();
266       if (toCheckExisting) {
267         if (aGraph->isLinkPrsExists(aLink))
268           continue;
269       }
270       aGraph->createLinkPrs(aLink);
271     }
272   }
273   delete ihmList;
274
275 }
276
277
278 void SUPERVGUI_GraphNode::setNodeName(QString theName) {
279   SUPERVGUI_Node::setNodeName(theName);
280   myTitle->setText(name());
281 }
282
283
284 void SUPERVGUI_GraphNode::deletePort(SUPERVGUI_Port* thePort) {
285   if (thePort->isA("SUPERVGUI_PortIn") || thePort->isA("SUPERVGUI_PortInESNode")) {
286     myPIcount--;
287   }
288   else {
289     myPOcount--;
290   }
291   thePort->deleteLinks();
292   
293   thePort->getPort()->destroy();
294   thePort->close(true);
295   updatePorts();
296   updateShape();
297 }
298
299
300 void SUPERVGUI_GraphNode::addInputPort() {
301   SUPERV_Port aPort = createInPort();
302   if (aPort == NULL) return;
303
304   if (getNodeType() == SUPERV::EndSwitchNode) {
305     SUPERVGUI_PortInESNode* aPortPrs = new SUPERVGUI_PortInESNode(myPortsBox, myMain, aPort);    
306     myPortLayout->addWidget(aPortPrs, myPIcount, 0);
307
308     if (myPortsBox->isVisible()) {
309       aPortPrs->show();
310     }
311   }
312   else {
313     SUPERVGUI_PortIn* aPortPrs = new SUPERVGUI_PortIn(myPortsBox, myMain, aPort);
314     myPortLayout->addWidget(aPortPrs, myPIcount, 0);
315
316     if (myPortsBox->isVisible()) {
317       aPortPrs->show();
318     }
319   }
320
321   myPIcount++;
322   updatePorts();
323   updateShape();
324 }
325
326
327 void SUPERVGUI_GraphNode::addOutputPort() {
328   SUPERV_Port aPort = createOutPort();
329   if (aPort == NULL) return;
330
331   SUPERVGUI_PortOut* aPortPrs = new SUPERVGUI_PortOut(myPortsBox, myMain, aPort);
332   myPortLayout->addWidget(aPortPrs, myPOcount, 1, Qt::AlignRight);
333   myPOcount++;
334   if (myPortsBox->isVisible()) {
335     aPortPrs->show();
336   }
337   updatePorts();
338   updateShape();
339 }
340
341
342 void SUPERVGUI_GraphNode::updatePorts() {
343   bool isAdded = false;
344   SUPERV_Ports aPorts = getEngine()->Ports();
345   int n = aPorts->length();
346   SUPERVGUI_Port* aPortPrs;
347   bool aIsVisible = myPortsBox->isVisible();
348   for (int i=0; i < n; i++) {
349     QString aName(aPorts[i]->Name());
350     aName += (aPorts[i]->IsInput())? "Input":"Output";
351     aPortPrs = (SUPERVGUI_Port*) child(aName, "SUPERVGUI_Port");
352     if (aPortPrs == NULL) {
353       if (aPorts[i]->IsInput()) {
354         SUPERVGUI_PortIn* aPortIn = new SUPERVGUI_PortIn(myPortsBox, myMain, aPorts[i]);
355         myPortLayout->addWidget(aPortIn, myPIcount, 0);
356         if (aIsVisible) aPortIn->show();
357         myPIcount++;
358         isAdded = true;
359       } else {
360         SUPERVGUI_PortOut* aPortOut = new SUPERVGUI_PortOut(myPortsBox, myMain, aPorts[i]);
361         myPortLayout->addWidget(aPortOut, myPOcount, 1, Qt::AlignRight);
362         if (aIsVisible) aPortOut->show();
363         myPOcount++;
364         isAdded = true;
365       }
366     }
367   }
368   
369   QObjectList* aShownPortsList = queryList("SUPERVGUI_Port");
370   QObjectListIt aLI(*aShownPortsList);
371   SUPERVGUI_Port* aVisPort;
372
373   while ((aVisPort=(SUPERVGUI_Port*)aLI.current()) != 0) {  
374     ++aLI;
375     QString aNameVisible(aVisPort->getPort()->Name());
376     
377     bool aIsExists = false;
378     for (int i=0; i < n; i++) {
379       QString aName(aPorts[i]->Name());
380       if (aName == aNameVisible)
381           aIsExists = true;
382     }
383   
384     if (!aIsExists) { //we have a visible object, which has no engine entity
385       aVisPort->close(true);
386     }
387   }
388 }