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