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