Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Canvas.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  SUPERV SUPERVGUI : GUI for Supervisor component
21 //
22 //  File   : SUPERVGUI_Ganvas.cxx
23 //  Author : Natalia KOPNOVA
24 //  Module : SUPERV
25
26 #include "SUPERVGUI.h"
27 #include "SUPERVGUI_Def.h"
28 #include "SUPERVGUI_Main.h"
29 #include "SUPERVGUI_CanvasNode.h"
30 #include "SUPERVGUI_CanvasPort.h"
31 #include "SUPERVGUI_CanvasLink.h"
32
33 #include "SUIT_ResourceMgr.h"
34 #include "SUIT_Session.h"
35
36 //#define CHECKTIME
37
38 #ifdef CHECKTIME
39 #include <sys/timeb.h>
40 #endif
41
42 #define UPDATECONTROLNODES(NodesName)  \
43     n = nodes->NodesName.length(); \
44     for (int i=0; i<n; i++) { \
45       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[i]); \
46       SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->NodesName[i]->Coupled()); \
47       ihmNode = getNode(aCNode); \
48       if (ihmNode == NULL) { \
49         myMain->addControlNode(aCNode, aCNodeEnd, false); \
50       } else { \
51         ihmNode->move(aCNode->X(), aCNode->Y()); \
52         ihmList->removeRef(ihmNode); \
53         ihmNode->merge(); \
54         ihmNode = getNode(aCNodeEnd); \
55         if (ihmNode) { \
56           ihmList->removeRef(ihmNode); \
57           ihmNode->merge(); \
58         } \
59       } \
60     }
61
62 #define UPDATENODES(NodesName, FuncName)   \
63     n = nodes->NodesName.length(); \
64     for (int i=0; i<n; i++) { \
65       SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->NodesName[i]); \
66       ihmNode = getNode(aCNode); \
67       if (ihmNode == NULL) { \
68         myMain->FuncName(aCNode); \
69       } else { \
70         ihmNode->move(aCNode->X(), aCNode->Y()); \
71         ihmList->removeRef(ihmNode); \
72         ihmNode->merge(); \
73       } \
74     }
75
76 #define UPDATELINK(Link)   \
77     ihmLink = getLink(Link.in()); \
78     if (ihmLink == NULL) { \
79       ihmLink = new SUPERVGUI_CanvasLink(this, myMain, Link.in()); \
80       ihmLink->show(); \
81     } \
82     else { \
83       ihmList->removeRef(ihmLink); \
84       ihmLink->merge(); \
85     }
86
87 /*
88 #define UPDATESTREAMLINK(Link)   \
89     ihmLink = getLink(Link.in()); \
90     if (ihmLink == NULL) { \
91       ihmLink = new SUPERVGUI_CanvasStreamLink(this, myMain, Link.in()); \
92       ihmLink->show(); \
93     } \
94     else { \
95       ihmList->removeRef(ihmLink); \
96       ihmLink->merge(); \
97     }
98 */
99
100
101 SUPERVGUI_Canvas::SUPERVGUI_Canvas( SUPERVGUI_Main* m, SUIT_ResourceMgr* mgr ):
102     QCanvas(),
103     myMain(m)
104 {
105   Trace("SUPERVGUI_Canvas::SUPERVGUI_Canvas");
106   myIsControlView = false;
107   // resize(GRAPH_WIDTH, GRAPH_HEIGHT);
108   resize(1050, 750);
109   setDoubleBuffering(true);
110
111   // mkr : IPAL10825 -->
112   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
113   if ( !aSupMod ) {
114     MESSAGE("NULL Supervision module!");
115     return;
116   }
117   QColor aColor =  aSupMod->getIVFBackgroundColor();
118   if ( !aColor.isValid() )
119     aColor = mgr->colorValue( "SUPERVGraph", "Background", DEF_MAIN_COLOR );
120   // <--
121      
122   //aColor = QColor(SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorRed" ), 
123   //              SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorGreen" ), 
124   //              SUIT_Session::session()->resourceMgr()->integerValue( "SUPERVGraph", "BackgroundColorBlue" ) );
125   setBackgroundColor(aColor);
126 }
127
128
129 SUPERVGUI_Canvas::~SUPERVGUI_Canvas() {
130   // asv : 17.01.05 : why delete its own children (CanvasNode-s)?
131   // they must be destroyed automatically.
132   QObjectList* aNodeList = queryList("SUPERVGUI_CanvasNode");
133   QObjectListIt aIt(*aNodeList);
134   QObject* anObj;
135   while ( (anObj = aIt.current()) != 0 ) {
136     ++aIt;
137     aNodeList->removeRef(anObj);
138     delete anObj;
139   }
140   // asv : list returned by queryList() must be removed
141   delete aNodeList;
142 }
143
144 void SUPERVGUI_Canvas::addView(QCanvasView* theView)
145 {
146   QCanvas::addView(theView);
147   theView->setPaletteBackgroundColor(backgroundColor().light(120));
148 }
149
150 void SUPERVGUI_Canvas::setFullView() 
151 {
152   myIsControlView = false;
153   updateNodes(true);
154   updateLinks();
155   update();
156 }
157
158 void SUPERVGUI_Canvas::setControlView() 
159 {
160   myIsControlView = true;
161   updateNodes(false);
162   updateLinks();
163   update();
164 }
165
166 void SUPERVGUI_Canvas::updateNodes(bool withPorts) 
167 {
168   QObjectList* aNodeList = queryList("SUPERVGUI_CanvasNode");
169   QObjectListIt aIt(*aNodeList);
170   SUPERVGUI_CanvasNode* aNode;
171   while ((aNode=(SUPERVGUI_CanvasNode*)aIt.current()) != 0) {
172     ++aIt;
173     if (withPorts)
174       aNode->showAll();
175     else
176       aNode->hideAll();
177   }
178   delete aNodeList;
179 }
180
181 void SUPERVGUI_Canvas::updateLinks() 
182 {
183   QObjectList* aLinkList = queryList("SUPERVGUI_CanvasLink");
184   QObjectListIt aIt(*aLinkList);
185   SUPERVGUI_CanvasLink* aLink;
186   while ((aLink=(SUPERVGUI_CanvasLink*)aIt.current()) != 0) {
187     ++aIt;
188     aLink->merge();
189   }
190   delete aLinkList;
191 }
192
193 void SUPERVGUI_Canvas::sync() {
194   if (SUPERV_isNull(myMain->getDataflow())) return;
195       
196   SUPERVGUI_CanvasNode* ihmNode;
197   QObjectList* ihmList = queryList("SUPERVGUI_CanvasNode");
198   QObjectListIt it(*ihmList);
199   while ((ihmNode=(SUPERVGUI_CanvasNode*)it.current()) != 0) {
200     ++it;
201     ihmNode->sync();
202   }
203   delete ihmList;
204 }
205
206 /** 
207  * Synchronizes Graph presentation with internal graph structure
208  */
209 void SUPERVGUI_Canvas::merge() {
210   if (SUPERV_isNull(myMain->getDataflow())) return;
211
212   SUPERVGUI_CanvasNode* ihmNode;
213   QObjectList* ihmList = queryList("SUPERVGUI_CanvasNode");
214   SUPERV_Nodes nodes = myMain->getDataflow()->Nodes();
215
216   MESSAGE("CNodes="<<nodes->CNodes.length());
217   MESSAGE("FNodes="<<nodes->FNodes.length());
218   MESSAGE("INodes="<<nodes->INodes.length());
219   MESSAGE("GNodes="<<nodes->GNodes.length());
220   MESSAGE("LNodes="<<nodes->LNodes.length());
221   MESSAGE("SNodes="<<nodes->SNodes.length());
222   MESSAGE("Graphs="<<nodes->Graphs.length());
223
224   int n;
225   UPDATENODES(CNodes, addComputeNode);
226   UPDATENODES(FNodes, addComputeNode);
227   UPDATENODES(INodes, addComputeNode);
228   UPDATENODES(Graphs, addMacroNode);
229   UPDATENODES(GNodes, addGOTONode);
230
231   UPDATECONTROLNODES(LNodes);    
232   UPDATECONTROLNODES(SNodes);
233   /*
234   n = nodes->LNodes.length();
235   for (int i=0; i<n; i++) {
236     SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->LNodes[i]);
237     SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->LNodes[i]->Coupled());
238     ihmNode = getNode(aCNode);
239     SUPERVGUI_CanvasNode* ihmNodeEnd = getNode(aCNodeEnd);
240     if (ihmNode == NULL) {
241       myMain->addControlNode(aCNode, aCNodeEnd, false);
242     } else {
243       if (ihmNodeEnd) {
244         ihmNode->move(aCNode->X(), aCNode->Y());
245         ihmList->removeRef(ihmNode);
246         ihmNode->merge();
247         ihmNode = getNode(aCNodeEnd);
248         ihmList->removeRef(ihmNode);
249         ihmNode->merge();
250       }
251     }
252   }
253   n = nodes->SNodes.length();
254   for (int i=0; i<n; i++) {
255     SUPERV_CNode aCNode = SUPERV::CNode::_narrow(nodes->SNodes[i]);
256     SUPERV_CNode aCNodeEnd = SUPERV::CNode::_narrow(nodes->SNodes[i]->Coupled());
257     ihmNode = getNode(aCNode);
258     if (ihmNode == NULL) {
259       myMain->addControlNode(aCNode, aCNodeEnd, false);
260     } else {
261       ihmNode->move(aCNode->X(), aCNode->Y());
262       ihmList->removeRef(ihmNode);
263       ihmNode->merge();
264       ihmNode = getNode(aCNodeEnd);
265       ihmList->removeRef(ihmNode);
266       ihmNode->merge();
267     }
268   }
269   //*/
270
271   QObjectListIt it(*ihmList);
272   while ((ihmNode=(SUPERVGUI_CanvasNode*)it.current()) != 0) {
273     ++it;
274     ihmList->removeRef(ihmNode);
275     delete ihmNode;
276   }
277   delete ihmList;
278
279   // update links
280   SUPERVGUI_CanvasLink* ihmLink;
281   ihmList = queryList("SUPERVGUI_CanvasLink");
282   /*
283   don't work... that's a pity!
284   */
285   //SUPERV_Links links = myMain->getDataflow()->Links();
286   SUPERV_Links links = myMain->getDataflow()->GLinks();
287   for (int i = 0; i < links->length(); i++) {
288     UPDATELINK(links[i]);
289   }
290
291   /*
292   SUPERV_StreamLinks slinks = myMain->getDataflow()->StreamLinks();
293   for (int i = 0; i < slinks->length(); i++) {
294     UPDATELINK(slinks[i]);
295   }
296   */
297   /*
298   if (myMain->getDataflow()->IsStreamGraph()) {
299     SUPERV_StreamGraph aSGraph = myMain->getDataflow()->ToStreamGraph();
300     if (!SUPERV_isNull(aSGraph)) {
301       SUPERV_StreamLinks slinks = aSGraph->StreamLinks(); // <<- doesn't work!!!
302       for (int i = 0; i < slinks->length(); i++) {
303         UPDATELINK(slinks[i]);
304       }
305     }
306   }
307   */
308
309   // update stream links by old method until StreamLinks() fuction works
310   SUPERVGUI_CanvasPort* aPort;
311   QObjectList* aPortList = queryList("SUPERVGUI_CanvasPort");
312   QObjectListIt aPortIt(*aPortList);
313   while ((aPort=(SUPERVGUI_CanvasPort*)aPortIt.current()) != 0) {
314     ++aPortIt;
315     if (aPort->getEngine()->IsInput() && aPort->getEngine()->IsLinked() 
316         && aPort->getEngine()->Kind() == SUPERV::DataStreamParameter) {
317       if (aPort->getEngine()->Node()->Kind() == SUPERV::EndSwitchNode) {
318         SUPERV_Links aLinks = aPort->getEngine()->Links();
319         for (int i = 0; i < aLinks->length(); i++) {
320           UPDATELINK(aLinks[i]);
321         }
322       }
323 //    else if (aPort->isStream()) {
324 //      SUPERV_StreamLink aStreamLink = ((SUPERVGUI_CanvasStreamPortIn*)aPort)->getStreamEngine()->StreamLink();
325 //      UPDATESTREAMLINK(aStreamLink);
326 //    }
327       else {
328         SUPERV_Link aLink = aPort->getEngine()->Link();
329         UPDATELINK(aLink);
330       }
331     }
332   }
333   delete aPortList;
334
335   it = QObjectListIt(*ihmList);
336   while ((ihmLink=(SUPERVGUI_CanvasLink*)it.current()) != 0) {
337     ++it;
338     ihmList->removeRef(ihmLink);
339     delete ihmLink;
340   }
341   delete ihmList;
342 }
343
344 SUPERVGUI_CanvasNode* SUPERVGUI_Canvas::getNode(SUPERV::CNode_ptr theNode) const
345 {
346   return (SUPERVGUI_CanvasNode*) 
347     ((SUPERVGUI_Canvas*)this)->child(theNode->Name(), "SUPERVGUI_CanvasNode");
348 }
349
350 SUPERVGUI_CanvasPort* SUPERVGUI_Canvas::getPort(SUPERV::Port_ptr thePort) const
351 {
352   SUPERVGUI_CanvasNode* aNode = getNode(thePort->Node());
353   if (aNode == NULL) return NULL;
354
355   return (SUPERVGUI_CanvasPort*) 
356     aNode->child(getPortName(thePort), "SUPERVGUI_CanvasPort");
357 }
358
359 SUPERVGUI_CanvasLink* SUPERVGUI_Canvas::getLink(SUPERV::Link_ptr theLink) const
360 {
361   return (SUPERVGUI_CanvasLink*) 
362     ((SUPERVGUI_Canvas*)this)->child(getLinkName(theLink), "SUPERVGUI_CanvasLink");
363 }
364
365 QString SUPERVGUI_Canvas::getPortName(SUPERV::Port_ptr thePort) const
366 {
367   QString aName(thePort->Name());
368   if (thePort->IsInput()) aName = aName + ".In";
369   else aName = aName + ".Out";
370   return aName;
371 }
372
373 QString SUPERVGUI_Canvas::getLinkName(SUPERV::Link_ptr theLink) const
374 {
375   SUPERV_Port aInPort = theLink->InPort();
376   SUPERV_Port aOutPort = theLink->OutPort();
377   QString aName = 
378     QString(aOutPort->Node()->Name()) + "." +
379     QString(aOutPort->Name()) + "-" +
380     QString(aInPort->Node()->Name()) + "." +
381     QString(aInPort->Name());
382   return aName;
383 }