Salome HOME
DCQ:prepare 2.0.0
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Service.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SUPERVGUI_Service.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29 #include "SUPERVGUI_Service.h"
30 #include "SUPERVGUI_Main.h"
31 #include "SUPERVGUI.h"
32 #include "QAD_Tools.h"
33 #include "QAD_FileDlg.h"
34
35 #include "SALOME_NamingService.hxx"
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
37 #include <qlayout.h>
38 #include <qtextstream.h>
39
40
41 static const char * ComponentIcon[] = {
42 "20 20 2 1",
43 "       c None",
44 ".      c #000000",
45 "                    ",
46 "                    ",
47 "                    ",
48 " .................. ",
49 " .                . ",
50 " .                . ",
51 " .                . ",
52 " .                . ",
53 " .                . ",
54 " .                . ",
55 " .                . ",
56 " .                . ",
57 " .................. ",
58 "    .     .     .   ",
59 "    .     .     .   ",
60 "   ...   ...   ...  ",
61 "  .. .. .. .. .. .. ",
62 "  .   . .   . .   . ",
63 "  .. .. .. .. .. .. ",
64 "   ...   ...   ...  "};
65
66
67 static const char * InterfaceIcon[] = {
68 "20 20 2 1",
69 "       c None",
70 ".      c #000000",
71 "                    ",
72 "                    ",
73 "                    ",
74 "                    ",
75 "                    ",
76 "            ..      ",
77 "          ......    ",
78 "         ..    ..   ",
79 "         .      .   ",
80 "..........      ..  ",
81 "         .      .   ",
82 "         ..    ..   ",
83 "          ......    ",
84 "            ..      ",
85 "                    ",
86 "                    ",
87 "                    ",
88 "                    ",
89 "                    ",
90 "                    "};
91
92
93
94
95 SUPERVGUI_Service::SUPERVGUI_Service(SALOME_NamingService* ns):
96     QDialog(QAD_Application::getDesktop(), 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
97     naming(ns), myMFile(0)
98 {
99   setSizeGripEnabled( true );
100   setCaption(tr("TIT_SERVICES"));
101
102   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
103
104   myTabPane = new QTabWidget(this);
105   connect(myTabPane, SIGNAL(currentChanged(QWidget *)), this, SLOT(tabChanged(QWidget *)));
106
107   // Create Tab for Corba services
108   QWidget* aCorbaPane = new QWidget(myTabPane);
109   QVBoxLayout* aBaseLayoutV= new QVBoxLayout(aCorbaPane, 0, 4);
110   aBaseLayoutV->setMargin(5);
111   aBaseLayoutV->setSpacing(10);
112
113   QHBoxLayout* aBaseLayout = new QHBoxLayout(aCorbaPane); //!!
114  
115   components = new QListView(aCorbaPane);
116   components->addColumn(tr("COL_COMPONENTS"));
117   components->addColumn(tr("COL_PORTTYPE"));
118   components->addColumn(tr("COL_PORTWAY"));
119   components->setColumnAlignment(1, AlignLeft);
120   components->setColumnAlignment(2, AlignLeft);
121   components->setColumnAlignment(3, AlignLeft);
122   components->setSelectionMode(QListView::Extended);
123   components->setRootIsDecorated(true);
124 //  aBaseLayout->addWidget(components);
125   aBaseLayoutV->addWidget(components); //!!
126
127   //QHGroupBox* aAddBox = new QHGroupBox(tr("TIT_ADDNODE"), aCorbaPane); //!!
128   //aAddBox->setInsideSpacing(20); //!!
129
130 //NRI   QPushButton* aComputeCBtn = new QPushButton(tr("TIT_ADDCNODE"), aCorbaPane); //!!
131 //NRI   connect(aComputeCBtn, SIGNAL(clicked()), this, SLOT(addComputeNode())); //!!
132 //NRI   aComputeCBtn->setDefault(false); 
133
134   QPushButton* aComputeBtn = new QPushButton(tr("TIT_ADDFNODE"), aCorbaPane);
135   connect(aComputeBtn, SIGNAL(clicked()), this, SLOT(addFactoryNode()));
136   aComputeBtn->setDefault(true); 
137
138   aBaseLayout->addWidget(aComputeBtn);
139   //NRI  aBaseLayout->addWidget(aComputeCBtn); //!!
140
141   aBaseLayoutV->insertLayout(-1, aBaseLayout);
142   myTabPane->addTab(aCorbaPane, tr("MODULES_PANE"));
143
144
145   // Create Tab for Python services
146   QWidget* aPythonPane = new QWidget(myTabPane);
147   QVBoxLayout* aPythonLayout = new QVBoxLayout(aPythonPane, 0, 4);
148   aPythonLayout->setMargin(5);
149   aPythonLayout->setSpacing(10);
150
151   // Type pane
152   QHGroupBox* aAddBox2 = new QHGroupBox(tr("TIT_ADDNODE"), aPythonPane);
153   aAddBox2->setInsideSpacing(20);
154
155   QLabel* aTypeLbl = new QLabel(tr("LBL_NODETYPE"), aAddBox2);
156
157   myTypeCombo = new QComboBox(aAddBox2);
158   myTypeCombo->insertItem("Computation");
159   myTypeCombo->insertItem("Switch");
160   myTypeCombo->insertItem("Loop");
161   myTypeCombo->insertItem("GoTo");
162   //myTypeCombo->insertItem("Label");
163   connect(myTypeCombo, SIGNAL(activated(int)), this, SLOT(typeNodeSelected(int)));
164
165   aPythonLayout->addWidget(aAddBox2);
166
167   // Edit pane
168   myStackWidget = new QWidgetStack(aPythonPane);
169
170   // other pane
171   myScriptPane = new SUPERVGUI_PythonEditPane(myStackWidget);
172   myOtherId = myStackWidget->addWidget(myScriptPane);
173
174   // loop pane
175   QTabWidget* aLoopTabPane = new QTabWidget(myStackWidget);
176   myInitPane = new SUPERVGUI_PythonEditPane(myStackWidget); 
177   aLoopTabPane->addTab(myInitPane, "Init");
178
179   myMorePane = new SUPERVGUI_PythonEditPane(myStackWidget);
180   aLoopTabPane->addTab(myMorePane, "More");
181
182   myNextPane = new SUPERVGUI_PythonEditPane(myStackWidget);
183   aLoopTabPane->addTab(myNextPane, "Next");
184   myLoopId = myStackWidget->addWidget(aLoopTabPane);
185
186   myStackWidget->raiseWidget(myOtherId);
187
188   aPythonLayout->addWidget(myStackWidget, 1);
189
190   // Create button
191   QPushButton* aCreateBtn = new QPushButton(tr("TIT_ADDNODE"), aPythonPane);
192   connect(aCreateBtn, SIGNAL(clicked()), this, SLOT(addInlineNode()));
193   aPythonLayout->addWidget(aCreateBtn);
194
195   myTabPane->addTab(aPythonPane, tr("INLINE_PANE"));
196
197   // Create Tab for Macro node only if environmental variable ENABLE_MACRO_NODE is set
198         if ( getenv( "ENABLE_MACRO_NODE" ) != NULL )
199         {
200         QWidget* aMacroPane = new QWidget(myTabPane);
201         QVBoxLayout* aMacroLayout = new QVBoxLayout(aMacroPane, 0, 4);
202         aMacroLayout->setMargin(5);
203         aMacroLayout->setSpacing(10);
204
205         QHBoxLayout* aLoadLayout = new QHBoxLayout(aMacroPane); //!!
206         aLoadLayout->addStretch();
207
208         QPushButton* aLoadBtn = new QPushButton(tr("BUT_LOAD"), aMacroPane);
209         connect(aLoadBtn, SIGNAL(clicked()), this, SLOT(loadGraph()));
210
211         aLoadLayout->addWidget(aLoadBtn);
212
213         aMacroLayout->addLayout(aLoadLayout);
214  
215         myMacroPane = new QListView(aMacroPane);
216         myMacroPane->addColumn(tr("COL_COMPONENTS"));
217         myMacroPane->addColumn(tr("COL_PORTTYPE"));
218         myMacroPane->addColumn(tr("COL_PORTWAY"));
219         myMacroPane->setColumnAlignment(1, AlignLeft);
220         myMacroPane->setColumnAlignment(2, AlignLeft);
221         myMacroPane->setColumnAlignment(3, AlignLeft);
222         myMacroPane->setSelectionMode(QListView::Extended);
223         myMacroPane->setRootIsDecorated(true);
224         aMacroLayout->addWidget(myMacroPane); //!!
225
226         QPushButton* aAddBtn = new QPushButton(tr("TIT_ADDFNODE"), aMacroPane);
227         connect(aAddBtn, SIGNAL(clicked()), this, SLOT(addMacroNode()));
228         aAddBtn->setDefault(true); 
229
230         aMacroLayout->addWidget(aAddBtn);
231
232         myTabPane->addTab(aMacroPane, tr("MACRO_PANE"));
233         }
234
235   aMainLayout->addWidget(myTabPane);
236
237   // Close button
238   QHBox* aBtnBox = new QHBox(this);
239   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnBox->layout()); 
240   aBtnLayout->addStretch();
241
242   QPushButton* aCloseBtn = new QPushButton(tr("BUT_CLOSE"), aBtnBox);
243   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(reject()));
244   
245   aMainLayout->addWidget(aBtnBox);
246
247   initialise();
248   myX = 0;
249   myY = 0;
250   myIsInline = false;
251 }
252
253
254 char* getDataStreamParameterName(int aType)
255 {
256   switch(aType) {
257   case 1:
258     return "integer";
259   case 2:
260     return "float";
261   case 3:
262     return "double";
263   case 4:
264     return "string";
265   case 6:
266     return "boolean";
267   default:
268     return "unknown";
269   }
270 }
271
272 void SUPERVGUI_Service::initialise() {
273   CORBA::Object_ptr obj  = naming->Resolve("/Kernel/ModulCatalog");
274   SALOME_ModuleCatalog::ModuleCatalog_var *aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
275   *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
276   if (CORBA::is_nil(*aModuleCatalog)) {
277     setCaption("Error in Connexion to omniNames with '/Kernel/ModulCatalog'");
278     return;
279   }
280   
281   QAD_ResourceMgr* aResMgr = QAD_Desktop::createResourceManager();
282
283   SALOME_ModuleCatalog::ListOfComponents_var lComponents = (*aModuleCatalog)->GetComponentList();
284   long nbComp = lComponents->length();
285   for (int i=0; i<nbComp; i++) {
286     SALOME_ModuleCatalog::Acomponent_ptr C = (*aModuleCatalog)->GetComponent((char *)lComponents[i]);
287     QListViewItem* myComponentItem = new QListViewItem(components, (char*)C->componentusername());
288     myComponentItem->setSelectable(false);
289     QString aIconName = C->component_icone();
290     if (!aIconName.isEmpty()) {
291       QString resDir = aResMgr->findFile(aIconName, C->componentname()) ;
292       if (resDir) {
293         resDir = QAD_Tools::addSlash(resDir);
294         QPixmap aIcone(resDir + aIconName);
295         QIconSet aIconSet(aIcone);
296         myComponentItem->setPixmap(0, aIconSet.pixmap(QIconSet::Small, QIconSet::Normal));
297       } else {
298         myComponentItem->setPixmap(0, ComponentIcon);
299       }
300     } else {
301       myComponentItem->setPixmap(0, ComponentIcon);
302     }
303     SALOME_ModuleCatalog::ListOfInterfaces* lInterfaces = C->GetInterfaceList();
304     long nbInterf = lInterfaces->length();
305     for (int j=0; j<nbInterf; j++) {
306       SALOME_ModuleCatalog::DefinitionInterface* Interface = C->GetInterface((char*)(*lInterfaces)[j]);
307       QListViewItem* myInterfaceItem = new QListViewItem(myComponentItem, (char*)Interface->interfacename);
308       myInterfaceItem->setSelectable(false);
309       myInterfaceItem->setPixmap(0, InterfaceIcon);
310       
311       long nbServices = Interface->interfaceservicelist.length();
312       for (int k=0; k<nbServices; k++) {
313         SALOME_ModuleCatalog::Service* Service = &(Interface->interfaceservicelist[k]);
314         QListViewItem* myServiceItem = new QListViewItem(myInterfaceItem, (char*)Service->ServiceName);
315         myServiceItem->setSelectable(true);
316         components->ensureItemVisible(myServiceItem);
317         
318         long nbPortsOut = Service->ServiceoutParameter.length();
319         for (int m=0; m<nbPortsOut; m++) {
320           SALOME_ModuleCatalog::ServicesParameter* PortOut = &(Service->ServiceoutParameter[m]);
321           QListViewItem* myPortOutItem = 
322             new QListViewItem(myServiceItem, (char*)PortOut->Parametername, (char*)PortOut->Parametertype, "Out");
323           myPortOutItem->setSelectable(false);
324         }
325
326         long nbStreamPortsOut = Service->ServiceoutDataStreamParameter.length();
327         for (int m=0; m<nbStreamPortsOut; m++) {
328           SALOME_ModuleCatalog::ServicesDataStreamParameter* PortOut = &(Service->ServiceoutDataStreamParameter[m]);
329           QListViewItem* myPortOutItem = 
330             new QListViewItem(myServiceItem, (char*)PortOut->Parametername, 
331                               getDataStreamParameterName(PortOut->Parametertype), "DataStream Out");
332           myPortOutItem->setSelectable(false);
333         }
334         
335         long nbPortsIn = Service->ServiceinParameter.length();
336         for (int l=0; l<nbPortsIn; l++) {
337           SALOME_ModuleCatalog::ServicesParameter* PortIn = &(Service->ServiceinParameter[l]);
338           QListViewItem* myPortInItem = 
339             new QListViewItem(myServiceItem, (char*)PortIn->Parametername, (char*)PortIn->Parametertype, "In");
340           myPortInItem->setSelectable(false);
341         }
342
343         long nbStreamPortsIn = Service->ServiceinDataStreamParameter.length();
344         for (int l=0; l<nbStreamPortsIn; l++) {
345           SALOME_ModuleCatalog::ServicesDataStreamParameter* PortIn = &(Service->ServiceinDataStreamParameter[l]);
346           QListViewItem* myPortInItem = 
347             new QListViewItem(myServiceItem, (char*)PortIn->Parametername, 
348                               getDataStreamParameterName(PortIn->Parametertype), "DataStream In");
349           myPortInItem->setSelectable(false);
350         }
351       }
352     }
353   }
354 }
355
356
357
358 SUPERVGUI_Service::~SUPERVGUI_Service() {
359     Trace("SUPERVGUI_Service::~SUPERVGUI_Service")
360       if (myMFile) delete myMFile;
361 }
362
363 void SUPERVGUI_Service::addComputeNode() {
364   QAD_Desktop* aDesktop = QAD_Application::getDesktop();
365   SUPERVGUI_Main* aMain = Supervision.getMain();
366   if (aMain==0) {
367     QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
368   } else if (!aMain->isEditable()) {
369     QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));      
370   } else {
371     CORBA::Object_ptr obj  = naming->Resolve("/Kernel/ModulCatalog");
372     SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
373     *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
374     if (CORBA::is_nil(*aModuleCatalog)) {
375       QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
376     } else {
377       QListViewItem*        item;
378       bool                  b = false;
379       QListViewItemIterator i(components);
380       for (; i.current(); ++i) {
381         item = i.current();
382         if (item->isSelected()) {
383           const char* service   = item->text(0).latin1();
384           const char* interface = item->parent()->text(0).latin1();
385           const char* component = item->parent()->parent()->text(0).latin1();
386           SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(aDesktop->getComponentName(component));
387           if (myComponent==NULL) {
388             QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
389           } else {
390             const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
391             b  = true;
392             
393             SUPERV_CNode node = aMain->getDataflow()->CNode(*myService);
394             if (CORBA::is_nil(node)) {
395               QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));    
396               return;
397             }
398             node->Coords(myX, myY);
399             myX += NODE_DX;
400             myY += NODE_DY;
401             aMain->addComputeNode(SUPERV::CNode::_narrow(node));
402           }
403         }
404       }
405       if (!b) {
406         QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NONODE_TOADD"));
407       }
408     }
409   }
410 }
411
412 void SUPERVGUI_Service::addFactoryNode() {
413   QAD_Desktop* aDesktop = QAD_Application::getDesktop();
414   SUPERVGUI_Main* aMain = Supervision.getMain();
415   if (aMain==0) {
416     QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
417   } else if (!aMain->isEditable()) {
418     QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));      
419   } else {
420     CORBA::Object_ptr obj  = naming->Resolve("/Kernel/ModulCatalog");
421     SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
422     *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
423     if (CORBA::is_nil(*aModuleCatalog)) {
424       QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
425     } else {
426       QListViewItem*        item;
427       bool                  b = false;
428       QListViewItemIterator i(components);
429       for (; i.current(); ++i) {
430         item = i.current();
431         if (item->isSelected()) {
432           const char* service   = item->text(0).latin1();
433           const char* interface = item->parent()->text(0).latin1();
434           const char* component = aDesktop->getComponentName(item->parent()->parent()->text(0).latin1());
435           SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(component);
436           if (myComponent==NULL) {
437             QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
438           } else {
439             const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
440             b  = true;
441             
442             MESSAGE ( " myService->TypeOfNode == " << myService->TypeOfNode ) 
443
444             if ( myService->TypeOfNode == 0 ) { // ComputeNode
445               SUPERV_CNode node = aMain->getDataflow()->CNode(*myService);
446               if (CORBA::is_nil(node)) {
447                 QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));          
448                 return;
449               }
450               node->Coords(myX, myY);
451               myX += NODE_DX;
452               myY += NODE_DY;
453               aMain->addComputeNode(SUPERV::CNode::_narrow(node));
454             } else { // Factory Node
455               SUPERV_FNode node = aMain->getDataflow()->FNode(component, interface, *myService);
456               if (CORBA::is_nil(node)) {
457                 QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));          
458                 return;
459               }
460               node->Coords(myX, myY);
461               myX += NODE_DX;
462               myY += NODE_DY;
463               aMain->addComputeNode(SUPERV::CNode::_narrow(node));
464               
465             }
466
467 //          SUPERV_FNode node = aMain->getDataflow()->FNode(component, interface, *myService);
468 //          if (CORBA::is_nil(node)) {
469 //            QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));   
470 //            return;
471 //          }
472 //          node->Coords(myX, myY);
473 //          myX += NODE_DX;
474 //          myY += NODE_DY;
475 //          aMain->addComputeNode(SUPERV::CNode::_narrow(node));
476           }
477         }
478       }
479       if (!b) {
480         QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NONODE_TOADD"));
481       }
482     }
483   }
484 }
485
486
487 void SUPERVGUI_Service::addInlineNode() {
488   SUPERVGUI_Main* aMain = Supervision.getMain();
489   if (aMain==0) {
490     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
491   } else if (!aMain->isEditable()) {
492     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));         
493   } else {
494     int aSel = myTypeCombo->currentItem();
495     switch (aSel) {
496     case 0: // Computation
497       if (myScriptPane->isDefined()) {  
498         SUPERV_INode aNode = aMain->getDataflow()->INode(myScriptPane->getFuncName().latin1(), 
499                                                          (myScriptPane->getFunction()).in());
500         if (CORBA::is_nil(aNode)) {
501           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
502           return;
503         }
504         aNode->Coords(myX, myY);
505         myX += NODE_DX;
506         myY += NODE_DY;
507         aMain->addComputeNode(SUPERV::CNode::_narrow(aNode));
508       }
509       break;
510       
511     case 1: // Switch
512       if (myScriptPane->isDefined()) {
513         SUPERV_INode aEndNode;
514         SUPERV_SNode aStartNode = aMain->getDataflow()->SNode(myScriptPane->getFuncName().latin1(),
515                                                               (myScriptPane->getFunction()).in(),
516                                                               aEndNode);
517         if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
518           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
519           return;
520         }
521         aStartNode->Coords(myX, myY);
522         aEndNode->Coords(myX + LABEL_WIDTH*2, myY);
523         myX += NODE_DX;
524         myY += NODE_DY;
525         aMain->addControlNode(SUPERV::CNode::_narrow(aStartNode), SUPERV::CNode::_narrow(aEndNode), true);
526       }
527       break;
528       
529     case 2: // Loop
530       {
531         SUPERV_INode aEndNode;
532         SUPERV_LNode aStartNode = aMain->getDataflow()->LNode(myInitPane->getFuncName().latin1(), (myInitPane->getFunction()).in(),
533                                                               myMorePane->getFuncName().latin1(), (myMorePane->getFunction()).in(),
534                                                               myNextPane->getFuncName().latin1(), (myNextPane->getFunction()).in(),
535                                                               aEndNode);
536         if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
537           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
538           return;
539         }
540         aStartNode->Coords(myX, myY);
541         aEndNode->Coords(myX + LABEL_WIDTH*2, myY);
542         myX += NODE_DX;
543         myY += NODE_DY;
544         aMain->addControlNode(SUPERV::CNode::_narrow(aStartNode), SUPERV::CNode::_narrow(aEndNode), true);
545       }
546       break;
547       
548     case 3: // GoTo
549       {
550         SUPERV_GNode aGotoNode;
551         if (myScriptPane->isDefined()) 
552           aGotoNode = aMain->getDataflow()->GNode(myScriptPane->getFuncName().latin1(), 
553                                                   (myScriptPane->getFunction()).in(), "");
554         else
555           aGotoNode = aMain->getDataflow()->GNode("GoTo", (myScriptPane->getFunction()).in(), "");
556         if (CORBA::is_nil(aGotoNode)) {
557           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
558           return;
559         }
560         aGotoNode->Coords(myX, myY);
561         myX += NODE_DX;
562         myY += NODE_DY;
563         aMain->addGOTONode(SUPERV::GNode::_narrow(aGotoNode));
564       }
565       break;
566     }
567   }
568 }
569
570 void SUPERVGUI_Service::addMacroNode() {
571   SUPERVGUI_Main* aMain = Supervision.getMain();
572   if (aMain==0) {
573     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
574   } else if (!aMain->isEditable()) {
575     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));         
576   } else {
577     if (myMFile) {
578       SUPERV_Graph aNode;
579       if (aMain->getDataflow()->IsStreamGraph()) {
580         SUPERV_StreamGraph aSGraph = aMain->getDataflow()->ToStreamGraph();
581         if (!SUPERV_isNull(aSGraph)) 
582           aNode = aSGraph->StreamMNode(myMFile->name().latin1());
583         // TMP: while stream macro nodes doesn't impemented 
584         if (CORBA::is_nil(aNode)) {
585           aNode = aSGraph->MNode(myMFile->name().latin1());
586         }
587       }
588       else 
589         aNode = aMain->getDataflow()->MNode(myMFile->name().latin1());
590       if (CORBA::is_nil(aNode)) {
591         QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));         
592         return;
593       }
594       aNode->Coords(myX, myY);
595       myX += NODE_DX;
596       myY += NODE_DY;
597       aMain->addMacroNode(SUPERV::CNode::_narrow(aNode));
598     }
599     else {
600       QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NONODE_TOADD"));
601     }
602   }
603 }
604
605 void SUPERVGUI_Service::loadGraph() {
606         if ( getenv( "ENABLE_MACRO_NODE" ) == NULL )
607         // error! if ENABLE_MACRO_NODE is set - we should NOT get here by any means..
608         {
609                 //cout << "Error: ENABLE_MACRO_NODE is not set, but loadGraph() was called!" << endl;
610                 return;
611         }
612
613   QString aFileName = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
614                                                "",
615                                                "*.xml",
616                                                tr("MSG_GRAPH_INSERT"),
617                                                true);
618   if (aFileName.isEmpty()) return;
619
620   myMacroPane->clear();
621
622   myMFile = new QFile(aFileName);
623   if (!myMFile->open(IO_ReadOnly)) {
624     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), 
625                          tr("MSG_CANT_LOADSCRIPT"));
626     delete myMFile; myMFile = 0;
627     return;
628   }
629
630   QTextStream* aStream = new QTextStream(myMFile);
631   if (aStream->atEnd()) {
632     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), 
633                          tr("MSG_EMTY_FILE"));
634     delete aStream;
635     myMFile->close();
636     delete myMFile; myMFile = 0;
637     return;
638   }
639
640   // read graph info
641   // temporary display only file name
642   QFileInfo anInfo(*myMFile);
643   QListViewItem* anItem = new QListViewItem(myMacroPane, anInfo.baseName());
644   anItem->setSelectable(true);
645 }
646
647 void SUPERVGUI_Service::typeNodeSelected(int theRow) {
648   if (theRow == 2)
649     myStackWidget->raiseWidget(myLoopId);
650   else
651     myStackWidget->raiseWidget(myOtherId);
652 }
653
654
655
656 void SUPERVGUI_Service::choose() {
657     Trace("SUPERVGUI_Service::choose")
658     show();
659     raise();
660 }
661     
662
663 void SUPERVGUI_Service::showEvent(QShowEvent* theEvent) {
664   SUPERVGUI_Main* aMain = Supervision.getMain();
665   if (aMain && (!aMain->isArrayShown())) {
666     aMain->getGraph()->viewportToContents(0, 0, myX, myY);
667   }
668   QDialog::showEvent(theEvent);
669 }
670
671
672 void SUPERVGUI_Service::tabChanged(QWidget* theWidget) {
673   myIsInline = (myTabPane->currentPageIndex() == 1);
674 }
675
676
677
678 //*****************************************************
679 //  Pane for Python script editing
680 //*****************************************************
681 SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane(QWidget* theParent) 
682   : QFrame(theParent),
683     myIStream(0)
684 {
685   QGridLayout* aEditLayout = new QGridLayout(this, 2, 4);
686
687   // First row
688   QPushButton* aLoadBtn = new QPushButton(tr("BUT_LOAD"), this);
689   connect(aLoadBtn, SIGNAL(clicked()), this, SLOT(loadFile()));
690   
691   aEditLayout->addWidget(aLoadBtn, 0, 2);
692
693   myNextBtn = new QPushButton(tr("BUT_NEXT"), this);
694   myNextBtn->setEnabled(false);
695   connect(myNextBtn, SIGNAL(clicked()), this, SLOT(readFunction()));
696   
697   aEditLayout->addWidget(myNextBtn, 0, 3);
698   
699   //Second row
700   myText = new QTextEdit(this);
701   myText->setWordWrap(QTextEdit::FixedColumnWidth);
702   myText->setWrapColumnOrWidth(80);
703
704   aEditLayout->addMultiCellWidget(myText, 1, 1, 0, 3);
705 }
706    
707
708 /**
709  * Load existing Python script
710  */
711 void SUPERVGUI_PythonEditPane::loadFile() {
712   QString aFileName = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
713                                                "",
714                                                "*.py",
715                                                tr("TIT_LOADSCRIPT"),
716                                                true);
717   if (aFileName.isEmpty()) return;
718
719   myFile = new QFile(aFileName);
720   if (!myFile->open(IO_ReadOnly)) {
721     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), 
722                          tr("MSG_CANT_LOADSCRIPT"));
723     return;
724   }
725   myIStream = new QTextStream(myFile);
726
727   if (myIStream->atEnd()) {
728     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), 
729                          tr("MSG_EMTY_FILE"));
730     myNextBtn->setEnabled(false);
731     delete myIStream;
732     myIStream = 0;
733     myFile->close();
734     delete myFile;
735     return;
736   }
737   myNextBtn->setEnabled(true);
738   readFunction();
739 }
740   
741
742 /**
743  * Finds and Reads a function from current position in the opened file
744  */
745 void SUPERVGUI_PythonEditPane::readFunction() {
746   if (!myIStream) return;
747
748   while (!myIStream->atEnd()) {
749     QString aLine = myIStream->readLine();
750     if (aLine.isNull()) return;
751
752     // find first function
753     int aDefPos = aLine.find("def");
754     if (aDefPos == 0) { // only not a class members
755       myText->clear();
756       // find name
757       /*int aStart = aLine.find(" ", aDefPos);
758       int aEnd = aLine.find("(", aStart);
759       QString aName = aLine.mid(aStart, (aEnd-aStart));
760       aName = aName.stripWhiteSpace();
761       myNameEdt->setText(aName);*/
762       
763       // find input params
764       /*aStart = aEnd+1;
765       aEnd = aLine.find(")", aStart);
766       QString aParams = aLine.mid(aStart, (aEnd-aStart));
767       aParams = aParams.stripWhiteSpace();
768       myParamEdt->setText(aParams);*/
769
770       myText->append(aLine);
771       // read body
772       QString aBodyLine = myIStream->readLine();
773       while ((!aBodyLine.isNull()) && aBodyLine[0].isSpace()) {
774         myText->append(aBodyLine);
775         aBodyLine = myIStream->readLine();
776       }
777       return;
778     }
779   }
780   QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), 
781                          tr("MSG_NOMORE_FUNCTIONS"));
782   delete myIStream;
783   myIStream = 0;
784   myNextBtn->setEnabled(false);
785   myFile->close();
786   delete myFile;
787 }
788
789
790 QString SUPERVGUI_PythonEditPane::getFuncName() {
791   QString aName("");
792   for (int i=0; i < myText->paragraphs(); i++) {
793     QString aLine = myText->text(i);
794     int aDefPos = aLine.find("def");
795     if (aDefPos == 0) {
796       int aStart = aLine.find(" ", aDefPos);
797       int aEnd = aLine.find("(", aStart);
798       QString aName = aLine.mid(aStart, (aEnd-aStart));
799       return aName.stripWhiteSpace();
800     }
801   }
802   return aName;
803 }
804
805
806 SUPERV_Strings SUPERVGUI_PythonEditPane::getFunction() {
807   SUPERV_Strings aStrings = new SUPERV::ListOfStrings();
808   aStrings->length(myText->paragraphs());
809   for (int i=0; i < myText->paragraphs(); i++) {
810     aStrings[i] = CORBA::string_dup(myText->text(i).latin1());
811   }
812   return aStrings._retn();
813 }
814
815
816 void SUPERVGUI_PythonEditPane::setFunction(SUPERV_Strings theStr) {
817   int aLen = theStr->length();
818   for (int i=0; i < aLen; i++)
819     myText->append(QString(theStr[i]));
820 }
821
822
823
824 /*!
825  * Edit Python dialog
826  */
827 SUPERVGUI_EditPythonDlg::SUPERVGUI_EditPythonDlg(bool isLoop)
828   :QDialog(QAD_Application::getDesktop(), 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
829 {
830   setSizeGripEnabled( true );
831   setCaption(tr("TIT_FUNC_PYTHON"));
832   resize( 500, 250 );
833   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
834   if (isLoop) {
835     QTabWidget* aLoopTabPane = new QTabWidget(this);
836     myInitPane = new SUPERVGUI_PythonEditPane(this); 
837     aLoopTabPane->addTab(myInitPane, "Init");
838     
839     myMorePane = new SUPERVGUI_PythonEditPane(this);
840     aLoopTabPane->addTab(myMorePane, "More");
841     
842     myNextPane = new SUPERVGUI_PythonEditPane(this);
843     aLoopTabPane->addTab(myNextPane, "Next");
844
845     aMainLayout->addWidget(aLoopTabPane);    
846   } else {
847     myEditPane = new SUPERVGUI_PythonEditPane(this);
848     aMainLayout->addWidget(myEditPane);
849   }
850   QGroupBox* aBtnBox = new QGroupBox( this );
851   aBtnBox->setColumnLayout( 0, Qt::Vertical );
852   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
853   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
854   aBtnLayout->setAlignment( Qt::AlignTop );
855   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
856   
857   QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
858   connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
859   aBtnLayout->addWidget( aOKBtn );
860
861   aBtnLayout->addStretch();
862
863   QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
864   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
865   aBtnLayout->addWidget( aCancelBtn );
866
867   aMainLayout->addWidget(aBtnBox);
868 }