Salome HOME
Modifications for correct adding factory nodes into the dataflow.
[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_Library.h"
32 #include "SUPERVGUI.h"
33
34 #include "CAM_Application.h"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_FileDlg.h"
37 #include "SUIT_Session.h"
38 #include "SUIT_Tools.h"
39
40 #include "SALOME_NamingService.hxx"
41 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
42 #include <qlabel.h>
43 #include <qlayout.h>
44 #include <qhbox.h>
45 #include <qtextstream.h>
46 #include <qregexp.h>
47
48 static const char * ComponentIcon[] = {
49 "20 20 2 1",
50 "       c None",
51 ".      c #000000",
52 "                    ",
53 "                    ",
54 "                    ",
55 " .................. ",
56 " .                . ",
57 " .                . ",
58 " .                . ",
59 " .                . ",
60 " .                . ",
61 " .                . ",
62 " .                . ",
63 " .                . ",
64 " .................. ",
65 "    .     .     .   ",
66 "    .     .     .   ",
67 "   ...   ...   ...  ",
68 "  .. .. .. .. .. .. ",
69 "  .   . .   . .   . ",
70 "  .. .. .. .. .. .. ",
71 "   ...   ...   ...  "};
72
73
74 static const char * InterfaceIcon[] = {
75 "20 20 2 1",
76 "       c None",
77 ".      c #000000",
78 "                    ",
79 "                    ",
80 "                    ",
81 "                    ",
82 "                    ",
83 "            ..      ",
84 "          ......    ",
85 "         ..    ..   ",
86 "         .      .   ",
87 "..........      ..  ",
88 "         .      .   ",
89 "         ..    ..   ",
90 "          ......    ",
91 "            ..      ",
92 "                    ",
93 "                    ",
94 "                    ",
95 "                    ",
96 "                    ",
97 "                    "};
98
99
100
101
102 SUPERVGUI_Service::SUPERVGUI_Service(SALOME_NamingService* ns):
103     QDialog(SUIT_Session::session()->activeApplication()->desktop(), 0, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
104     naming(ns), myMFile(0)
105 {
106   setSizeGripEnabled( true );
107   setCaption(tr("TIT_SERVICES"));
108
109   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
110
111   myTabPane = new QTabWidget(this);
112   connect(myTabPane, SIGNAL(currentChanged(QWidget *)), this, SLOT(tabChanged(QWidget *)));
113
114   // Create Tab for Corba services
115   QWidget* aCorbaPane = new QWidget(myTabPane);
116   QVBoxLayout* aBaseLayoutV= new QVBoxLayout(aCorbaPane, 0, 4);
117   aBaseLayoutV->setMargin(5);
118   aBaseLayoutV->setSpacing(10);
119
120   QHBoxLayout* aBaseLayout = new QHBoxLayout(aCorbaPane); //!!
121  
122   components = new QListView(aCorbaPane);
123   components->addColumn(tr("COL_COMPONENTS"));
124   components->addColumn(tr("COL_PORTTYPE"));
125   components->addColumn(tr("COL_PORTWAY"));
126   components->setColumnAlignment(1, AlignLeft);
127   components->setColumnAlignment(2, AlignLeft);
128   components->setColumnAlignment(3, AlignLeft);
129   components->setSelectionMode(QListView::Extended);
130   components->setRootIsDecorated(true);
131 //  aBaseLayout->addWidget(components);
132   aBaseLayoutV->addWidget(components); //!!
133
134   //QHGroupBox* aAddBox = new QHGroupBox(tr("TIT_ADDNODE"), aCorbaPane); //!!
135   //aAddBox->setInsideSpacing(20); //!!
136
137 //NRI   QPushButton* aComputeCBtn = new QPushButton(tr("TIT_ADDCNODE"), aCorbaPane); //!!
138 //NRI   connect(aComputeCBtn, SIGNAL(clicked()), this, SLOT(addComputeNode())); //!!
139 //NRI   aComputeCBtn->setDefault(false); 
140
141   QPushButton* aComputeBtn = new QPushButton(tr("TIT_ADDFNODE"), aCorbaPane);
142   connect(aComputeBtn, SIGNAL(clicked()), this, SLOT(addFactoryNode()));
143   aComputeBtn->setDefault(true); 
144
145   aBaseLayout->addWidget(aComputeBtn);
146   //NRI  aBaseLayout->addWidget(aComputeCBtn); //!!
147
148   aBaseLayoutV->insertLayout(-1, aBaseLayout);
149   myTabPane->addTab(aCorbaPane, tr("MODULES_PANE"));
150
151
152   // Create Tab for Python services
153   QWidget* aPythonPane = new QWidget(myTabPane);
154   QVBoxLayout* aPythonLayout = new QVBoxLayout(aPythonPane, 0, 4);
155   aPythonLayout->setMargin(5);
156   aPythonLayout->setSpacing(10);
157
158   // Type pane
159   QHGroupBox* aAddBox2 = new QHGroupBox(tr("TIT_ADDNODE"), aPythonPane);
160   aAddBox2->setInsideSpacing(20);
161
162   /*QLabel* aTypeLbl = */new QLabel(tr("LBL_NODETYPE"), aAddBox2);
163
164   myTypeCombo = new QComboBox(aAddBox2);
165   myTypeCombo->insertItem( tr( "INLINE_COMP" ) );
166   myTypeCombo->insertItem( tr( "INLINE_SWTC" ) );
167   myTypeCombo->insertItem( tr( "INLINE_LOOP" ) );
168   myTypeCombo->insertItem( tr( "INLINE_GOTO") );
169   connect(myTypeCombo, SIGNAL(activated(int)), this, SLOT(typeNodeSelected(int)));
170
171   aPythonLayout->addWidget(aAddBox2);
172
173   // Edit pane
174   myStackWidget = new QWidgetStack(aPythonPane);
175
176   // other pane
177   myScriptPane = new SUPERVGUI_PythonEditPane( myStackWidget, true, myX, myY );
178   myOtherId = myStackWidget->addWidget(myScriptPane);
179
180   // loop pane
181   QTabWidget* aLoopTabPane = new QTabWidget(myStackWidget);
182   myInitPane = new SUPERVGUI_PythonEditPane( myStackWidget, true, myX, myY ); 
183   aLoopTabPane->addTab(myInitPane, "Init");
184
185   myMorePane = new SUPERVGUI_PythonEditPane( myStackWidget, true, myX, myY );
186   aLoopTabPane->addTab(myMorePane, "More");
187
188   myNextPane = new SUPERVGUI_PythonEditPane( myStackWidget, true, myX, myY );
189   aLoopTabPane->addTab(myNextPane, "Next");
190   myLoopId = myStackWidget->addWidget(aLoopTabPane);
191
192   myStackWidget->raiseWidget(myOtherId);
193
194   aPythonLayout->addWidget(myStackWidget, 1);
195
196   // Create button
197   QPushButton* aCreateBtn = new QPushButton(tr("TIT_ADDNODE"), aPythonPane);
198   connect(aCreateBtn, SIGNAL(clicked()), this, SLOT(addInlineNode()));
199   aPythonLayout->addWidget(aCreateBtn);
200
201   myTabPane->addTab(aPythonPane, tr("INLINE_PANE"));
202
203   // Create Tab for Macro node only if environmental variable ENABLE_MACRO_NODE is set
204   if ( getenv( "ENABLE_MACRO_NODE" ) != NULL ) {
205     QWidget* aMacroPane = new QWidget(myTabPane);
206     QVBoxLayout* aMacroLayout = new QVBoxLayout(aMacroPane, 0, 4);
207     aMacroLayout->setMargin(5);
208     aMacroLayout->setSpacing(10);
209     
210     QHBoxLayout* aLoadLayout = new QHBoxLayout(aMacroPane); //!!
211     aLoadLayout->addStretch();
212     
213     QPushButton* aLoadBtn = new QPushButton(tr("BUT_LOAD"), aMacroPane);
214     connect(aLoadBtn, SIGNAL(clicked()), this, SLOT(loadGraph()));
215
216     aLoadLayout->addWidget(aLoadBtn);
217
218     aMacroLayout->addLayout(aLoadLayout);
219  
220     myMacroPane = new QListView(aMacroPane);
221     myMacroPane->addColumn(tr("COL_COMPONENTS"));
222     myMacroPane->addColumn(tr("COL_PORTTYPE"));
223     myMacroPane->addColumn(tr("COL_PORTWAY"));
224     myMacroPane->setColumnAlignment(1, AlignLeft);
225     myMacroPane->setColumnAlignment(2, AlignLeft);
226     myMacroPane->setColumnAlignment(3, AlignLeft);
227     myMacroPane->setSelectionMode(QListView::Extended);
228     myMacroPane->setRootIsDecorated(true);
229     aMacroLayout->addWidget(myMacroPane); //!!
230     
231     QPushButton* aAddBtn = new QPushButton(tr("TIT_ADDFNODE"), aMacroPane);
232     connect(aAddBtn, SIGNAL(clicked()), this, SLOT(addMacroNode()));
233     aAddBtn->setDefault(true); 
234     
235     aMacroLayout->addWidget(aAddBtn);
236
237     myTabPane->addTab(aMacroPane, tr("MACRO_PANE"));
238   }
239   
240   aMainLayout->addWidget(myTabPane);
241
242   // Close button
243   QHBox* aBtnBox = new QHBox(this);
244   QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnBox->layout()); 
245   aBtnLayout->addStretch();
246
247   QPushButton* aCloseBtn = new QPushButton(tr("BUT_CLOSE"), aBtnBox);
248   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(reject()));
249   
250   aMainLayout->addWidget(aBtnBox);
251
252   initialise();
253   myX = 0;
254   myY = 0;
255   myIsInline = false;
256 }
257
258
259 char* getDataStreamParameterName(int aType)
260 {
261   switch(aType) {
262   case 1:
263     return "integer";
264   case 2:
265     return "float";
266   case 3:
267     return "double";
268   case 4:
269     return "string";
270   case 6:
271     return "boolean";
272   default:
273     return "unknown";
274   }
275 }
276
277 void SUPERVGUI_Service::initialise() {
278   CORBA::Object_ptr obj  = naming->Resolve("/Kernel/ModulCatalog");
279   SALOME_ModuleCatalog::ModuleCatalog_var *aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
280   *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
281   if (CORBA::is_nil(*aModuleCatalog)) {
282     setCaption("Error in Connexion to omniNames with '/Kernel/ModulCatalog'");
283     return;
284   }
285   
286   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
287
288   SALOME_ModuleCatalog::ListOfComponents_var lComponents = (*aModuleCatalog)->GetComponentList();
289   long nbComp = lComponents->length();
290   for (int i=0; i<nbComp; i++) {
291     SALOME_ModuleCatalog::Acomponent_ptr C = (*aModuleCatalog)->GetComponent((char *)lComponents[i]);
292     QListViewItem* myComponentItem = new QListViewItem(components, (char*)C->componentusername());
293     myComponentItem->setSelectable(false);
294     QString aIconName = C->component_icone();
295     if (!aIconName.isEmpty()) {
296       QString resFilePath = aResMgr->path(aResMgr->resSection(), C->componentname(), aIconName) ;
297       if ( resFilePath ) {
298         QPixmap aIcone(resFilePath);
299         QIconSet aIconSet(aIcone);
300         myComponentItem->setPixmap(0, aIconSet.pixmap(QIconSet::Small, QIconSet::Normal));
301       } else {
302         myComponentItem->setPixmap(0, ComponentIcon);
303       }
304     } else {
305       myComponentItem->setPixmap(0, ComponentIcon);
306     }
307     SALOME_ModuleCatalog::ListOfInterfaces* lInterfaces = C->GetInterfaceList();
308     long nbInterf = lInterfaces->length();
309     for (int j=0; j<nbInterf; j++) {
310       SALOME_ModuleCatalog::DefinitionInterface* Interface = C->GetInterface((char*)(*lInterfaces)[j]);
311       QListViewItem* myInterfaceItem = new QListViewItem(myComponentItem, (char*)Interface->interfacename);
312       myInterfaceItem->setSelectable(false);
313       myInterfaceItem->setPixmap(0, InterfaceIcon);
314       
315       long nbServices = Interface->interfaceservicelist.length();
316       for (int k=0; k<nbServices; k++) {
317         SALOME_ModuleCatalog::Service* Service = &(Interface->interfaceservicelist[k]);
318         QListViewItem* myServiceItem = new QListViewItem(myInterfaceItem, (char*)Service->ServiceName);
319         myServiceItem->setSelectable(true);
320         components->ensureItemVisible(myServiceItem);
321         
322         long nbPortsOut = Service->ServiceoutParameter.length();
323         for (int m=0; m<nbPortsOut; m++) {
324           SALOME_ModuleCatalog::ServicesParameter* PortOut = &(Service->ServiceoutParameter[m]);
325           QListViewItem* myPortOutItem = 
326             new QListViewItem(myServiceItem, (char*)PortOut->Parametername, (char*)PortOut->Parametertype, "Out");
327           myPortOutItem->setSelectable(false);
328         }
329
330         long nbStreamPortsOut = Service->ServiceoutDataStreamParameter.length();
331         for (int m=0; m<nbStreamPortsOut; m++) {
332           SALOME_ModuleCatalog::ServicesDataStreamParameter* PortOut = &(Service->ServiceoutDataStreamParameter[m]);
333           QListViewItem* myPortOutItem = 
334             new QListViewItem(myServiceItem, (char*)PortOut->Parametername, 
335                               getDataStreamParameterName(PortOut->Parametertype), "DataStream Out");
336           myPortOutItem->setSelectable(false);
337         }
338         
339         long nbPortsIn = Service->ServiceinParameter.length();
340         for (int l=0; l<nbPortsIn; l++) {
341           SALOME_ModuleCatalog::ServicesParameter* PortIn = &(Service->ServiceinParameter[l]);
342           QListViewItem* myPortInItem = 
343             new QListViewItem(myServiceItem, (char*)PortIn->Parametername, (char*)PortIn->Parametertype, "In");
344           myPortInItem->setSelectable(false);
345         }
346
347         long nbStreamPortsIn = Service->ServiceinDataStreamParameter.length();
348         for (int l=0; l<nbStreamPortsIn; l++) {
349           SALOME_ModuleCatalog::ServicesDataStreamParameter* PortIn = &(Service->ServiceinDataStreamParameter[l]);
350           QListViewItem* myPortInItem = 
351             new QListViewItem(myServiceItem, (char*)PortIn->Parametername, 
352                               getDataStreamParameterName(PortIn->Parametertype), "DataStream In");
353           myPortInItem->setSelectable(false);
354         }
355       }
356     }
357   }
358 }
359
360
361
362 SUPERVGUI_Service::~SUPERVGUI_Service() {
363     Trace("SUPERVGUI_Service::~SUPERVGUI_Service")
364       if (myMFile) delete myMFile;
365 }
366
367 void SUPERVGUI_Service::addComputeNode() {
368   SUIT_Desktop* aDesktop = SUIT_Session::session()->activeApplication()->desktop();
369   CAM_Application* anApp = ( CAM_Application* )(SUIT_Session::session()->activeApplication());
370   if ( !anApp ) return;
371   
372   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
373   if ( !aSupMod ) {
374     MESSAGE("NULL Supervision module!");
375     return;
376   }
377
378   SUPERVGUI_Main* aMain = aSupMod->getMain();
379   if (aMain==0) {
380     QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
381   } else if (!aMain->isEditable()) {
382     QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));      
383   } else {
384     CORBA::Object_ptr obj  = naming->Resolve("/Kernel/ModulCatalog");
385     SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
386     *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
387     if (CORBA::is_nil(*aModuleCatalog)) {
388       QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
389     } else {
390       QListViewItem*        item;
391       bool                  b = false;
392       QListViewItemIterator i(components);
393       for (; i.current(); ++i) {
394         item = i.current();
395         if (item->isSelected()) {
396           const char* service   = item->text(0).latin1();
397           const char* interface = item->parent()->text(0).latin1();
398           const char* component = item->parent()->parent()->text(0).latin1();
399           SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(anApp->moduleName(component));
400           if ( myComponent == NULL ) {
401             QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
402           } 
403           else {
404             const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
405             SUPERV_CNode aNode = aMain->getDataflow()->CNode(*myService);
406             if ( CORBA::is_nil( aNode ) ) {
407               QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));    
408               return;
409             }
410
411             SUPERV::INode_var aDummyEndNode;
412             addNode( aNode, aDummyEndNode, myX, myY );
413             b = true; // at least one node was added
414           }
415         }
416       }
417       if ( !b ) {
418         QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NONODE_TOADD"));
419       }
420     }
421   }
422 }
423
424 void SUPERVGUI_Service::addFactoryNode() {
425   SUIT_Desktop* aDesktop = SUIT_Session::session()->activeApplication()->desktop();
426   CAM_Application* anApp = ( CAM_Application* )(SUIT_Session::session()->activeApplication());
427   if ( !anApp ) return;
428
429   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
430   if ( !aSupMod ) {
431     MESSAGE("NULL Supervision module!");
432     return;
433   }
434
435   SUPERVGUI_Main* aMain = aSupMod->getMain();
436   if (aMain==0) {
437     QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
438   } else if (!aMain->isEditable()) {
439     QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));      
440   } else {
441     CORBA::Object_ptr obj  = naming->Resolve("/Kernel/ModulCatalog");
442     SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
443     *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
444     if (CORBA::is_nil(*aModuleCatalog)) {
445       QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
446     } else {
447       QListViewItem*        item;
448       bool                  b = false;
449       QListViewItemIterator i(components);
450       for (; i.current(); ++i) {
451         item = i.current();
452         if (item->isSelected()) {
453           const char* service   = item->text(0).latin1();
454           const char* interface = item->parent()->text(0).latin1();
455           //const char* component = anApp->moduleName(item->parent()->parent()->text(0).latin1());
456           if ( aSupMod->getInterfaceNameMap().contains(item->parent()->text(0)) ) {
457             const char* component = aSupMod->getInterfaceNameMap().find(item->parent()->text(0)).data();
458
459             SALOME_ModuleCatalog::Acomponent_ptr myComponent = (*aModuleCatalog)->GetComponent(component);
460             if (myComponent==NULL) {
461               QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CHOOSE_SERVICE"));
462             } 
463             else {
464               const SALOME_ModuleCatalog::Service* myService = myComponent->GetService(interface, service);
465               SUPERV_CNode aNode;
466               if ( myService->TypeOfNode == 0 ) { // ComputeNode
467                 aNode = aMain->getDataflow()->CNode(*myService);
468                 if (CORBA::is_nil( aNode ) ) {
469                   QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));        
470                   return;
471                 }
472               } 
473               else { // Factory Node
474                 aNode = aMain->getDataflow()->FNode(component, interface, *myService);
475                 if ( CORBA::is_nil( aNode ) ) {
476                   QMessageBox::warning(aDesktop, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));        
477                   return;
478                 }
479               }
480               SUPERV::INode_var aDummyEndNode;
481               addNode( aNode, aDummyEndNode, myX, myY );
482               b = true;
483             }
484           }
485         }
486       }
487       if ( !b ) {
488         QMessageBox::warning(aDesktop, tr("WARNING"), tr("MSG_NONODE_TOADD"));
489       }
490     }
491   }
492 }
493
494
495 void SUPERVGUI_Service::addInlineNode() {
496   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
497   if ( !aSupMod ) {
498     MESSAGE("NULL Supervision module!");
499     return;
500   }
501
502   SUPERVGUI_Main* aMain = aSupMod->getMain();
503   if (aMain==0) {
504     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
505   } 
506   else if (!aMain->isEditable()) {
507     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
508   } 
509   else {
510     int aSel = myTypeCombo->currentItem();
511     switch (aSel) {
512     case 0: // Computation
513       { 
514         SUPERV_CNode aNode = aMain->getDataflow()->INode(myScriptPane->getFuncName().latin1(), 
515                                                          (myScriptPane->getFunction()).in());
516         if (CORBA::is_nil(aNode)) {
517           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
518           return;
519         }
520         SUPERV::INode_var aDummyEndNode;
521         addNode( aNode, aDummyEndNode, myX, myY );
522       }
523       break;
524       
525     case 1: // Switch
526       {
527         SUPERV_INode aEndNode;
528         SUPERV_CNode aStartNode = aMain->getDataflow()->SNode(myScriptPane->getFuncName().latin1(),
529                                                               (myScriptPane->getFunction()).in(),
530                                                               aEndNode);
531         if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
532           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
533           return;
534         }
535         addNode( aStartNode, aEndNode, myX, myY );
536       }
537       break;
538       
539     case 2: // Loop
540       {
541         SUPERV_INode aEndNode;
542         SUPERV_CNode aStartNode = aMain->getDataflow()->LNode(myInitPane->getFuncName().latin1(), (myInitPane->getFunction()).in(),
543                                                               myMorePane->getFuncName().latin1(), (myMorePane->getFunction()).in(),
544                                                               myNextPane->getFuncName().latin1(), (myNextPane->getFunction()).in(),
545                                                               aEndNode);
546         if (CORBA::is_nil(aStartNode) || CORBA::is_nil(aEndNode)) {
547           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
548           return;
549         }
550         addNode( aStartNode, aEndNode, myX, myY );
551       }
552       break;
553       
554     case 3: // GoTo
555       {
556         SUPERV_CNode aGotoNode;
557         if (myScriptPane->isDefined()) 
558           aGotoNode = aMain->getDataflow()->GNode(myScriptPane->getFuncName().latin1(), 
559                                                   (myScriptPane->getFunction()).in(), "");
560         else
561           aGotoNode = aMain->getDataflow()->GNode("GoTo", (myScriptPane->getFunction()).in(), "");
562         if (CORBA::is_nil(aGotoNode)) {
563           QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
564           return;
565         }
566         SUPERV::INode_var aDummyEndNode;
567         addNode( aGotoNode, aDummyEndNode, myX, myY );
568       }
569       break;
570     }
571   }
572 }
573
574 void SUPERVGUI_Service::addMacroNode() {
575   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
576   if ( !aSupMod ) {
577     MESSAGE("NULL Supervision module!");
578     return;
579   }
580
581   SUPERVGUI_Main* aMain = aSupMod->getMain();
582   if (aMain==0) {
583     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_NO_SUPERVISION_WINDOW"));
584   } 
585   else if (!aMain->isEditable()) {
586     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));       
587   } 
588   else {
589     if ( myMFile ) {
590       SUPERV_CNode aNode;
591       if (aMain->getDataflow()->IsStreamGraph()) {
592         SUPERV_StreamGraph aSGraph = aMain->getDataflow()->ToStreamGraph();
593         if (!SUPERV_isNull(aSGraph)) 
594           aNode = aSGraph->StreamMNode(myMFile->name().latin1());
595         // TMP: while stream macro nodes doesn't impemented 
596         if (CORBA::is_nil(aNode)) {
597           aNode = aSGraph->MNode(myMFile->name().latin1());
598         }
599       }
600       else 
601         aNode = aMain->getDataflow()->MNode(myMFile->name().latin1());
602       if (CORBA::is_nil(aNode)) {
603         QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_CREATE_NODE"));         
604         return;
605       }
606
607       SUPERV::INode_var aDummyEndNode;
608       addNode( aNode, aDummyEndNode, myX, myY );
609     }
610     else {
611       QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_NONODE_TOADD"));
612     }
613   }
614 }
615
616 void SUPERVGUI_Service::loadGraph() {
617         if ( getenv( "ENABLE_MACRO_NODE" ) == NULL )
618         // error! if ENABLE_MACRO_NODE is set - we should NOT get here by any means..
619         {
620                 //MESSAGE("Error: ENABLE_MACRO_NODE is not set, but loadGraph() was called!");
621                 return;
622         }
623
624   QString aFileName = SUIT_FileDlg::getFileName(SUIT_Session::session()->activeApplication()->desktop(),
625                                                 "",
626                                                 "*.xml",
627                                                 tr("MSG_GRAPH_INSERT"),
628                                                 true);
629   if (aFileName.isEmpty()) return;
630
631   myMacroPane->clear();
632
633   myMFile = new QFile(aFileName);
634   if (!myMFile->open(IO_ReadOnly)) {
635     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), 
636                          tr("MSG_CANT_LOADSCRIPT"));
637     delete myMFile; myMFile = 0;
638     return;
639   }
640
641   QTextStream* aStream = new QTextStream(myMFile);
642   if (aStream->atEnd()) {
643     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), 
644                          tr("MSG_EMTY_FILE"));
645     delete aStream;
646     myMFile->close();
647     delete myMFile; myMFile = 0;
648     return;
649   }
650
651   // read graph info
652   // temporary display only file name
653   QFileInfo anInfo(*myMFile);
654   QListViewItem* anItem = new QListViewItem(myMacroPane, anInfo.baseName());
655   anItem->setSelectable(true);
656 }
657
658 void SUPERVGUI_Service::typeNodeSelected(int theRow) {
659   if (theRow == 2)
660     myStackWidget->raiseWidget(myLoopId);
661   else
662     myStackWidget->raiseWidget(myOtherId);
663 }
664
665
666
667 void SUPERVGUI_Service::choose() {
668     Trace("SUPERVGUI_Service::choose")
669     show();
670     raise();
671 }
672     
673
674 void SUPERVGUI_Service::showEvent(QShowEvent* theEvent) {
675   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
676   if ( !aSupMod ) {
677     MESSAGE("NULL Supervision module!");
678     return;
679   }
680
681   SUPERVGUI_Main* aMain = aSupMod->getMain();
682   if (aMain && (!aMain->isArrayShown())) {
683     aMain->getArrayView()->viewportToContents(0, 0, myX, myY);
684     //aMain->getGraph()->viewportToContents(0, 0, myX, myY);
685   }
686   QDialog::showEvent(theEvent);
687 }
688
689
690 void SUPERVGUI_Service::tabChanged(QWidget* theWidget) {
691   myIsInline = (myTabPane->currentPageIndex() == 1);
692 }
693
694
695
696 //*****************************************************
697 //  Pane for Python script editing
698 //*****************************************************
699 SUPERVGUI_PythonEditPane::SUPERVGUI_PythonEditPane( QWidget* theParent, const bool isNodeCreation, int& theX, int& theY ) 
700   : myIsWithLibrary( isNodeCreation ), 
701     QFrame( theParent ),
702     myX( theX ), myY( theY )
703 {
704   QGridLayout* aEditLayout = new QGridLayout( this, 2, 8, 0, 6 );
705
706   // First row
707   if ( myIsWithLibrary ) {
708     QPushButton* aLibBtn = new QPushButton(tr("BUT_LIBRARY"), this);
709     connect(aLibBtn, SIGNAL(clicked()), this, SLOT(library()));
710     aEditLayout->addMultiCellWidget( aLibBtn, 0, 0, 1, 2 );
711   }
712
713   QPushButton* aLoadBtn = new QPushButton(tr("BUT_LOAD"), this);
714   connect(aLoadBtn, SIGNAL(clicked()), this, SLOT(loadFile()));
715   
716   aEditLayout->addMultiCellWidget( aLoadBtn, 0, 0, 3, 4 );
717
718   myFunctionsCombo = new QComboBox( this );
719   connect( myFunctionsCombo, SIGNAL( activated( int ) ), this, SLOT( readFunction( int ) ) );
720   
721   aEditLayout->addMultiCellWidget( myFunctionsCombo, 0, 0, 5, 7 );
722   
723   //Second row
724   myText = new QTextEdit(this);
725   myText->setTextFormat( Qt::PlainText ); // NOT rich text, so text() returns plain text
726   myText->setWordWrap( QTextEdit::FixedColumnWidth );
727   myText->setWrapColumnOrWidth( 80 );
728   connect( myText, SIGNAL( returnPressed() ), this, SLOT( autoIndentLine() ) );
729
730   aEditLayout->addMultiCellWidget( myText, 1, 1, 0, 7 );
731   //aEditLayout->setColStretch( 3, 1 ); // to allow myFunctionsCombo be larger when needed
732 }
733
734 /**
735  * Return a text between "def" and "("
736  * "def" must begin with position 0, which means that only global function definitions are possible
737  */ 
738 QString getFunctionName( const QString& aLine ) {
739   int aDefPos = aLine.find("def");
740   if ( aDefPos == 0 ) { // only global function definitions!
741     int aStart = aLine.find(" ", aDefPos);
742     int aEnd = aLine.find("(", aStart);
743     QString aName = aLine.mid(aStart, (aEnd-aStart));
744     return aName.stripWhiteSpace();
745   }
746   return QString();
747 }
748    
749 /**
750  * Searches for text beginning with "def" and ending with any meanful character at 
751  * beginning of line.  Starts searching with current position of given stream.
752  * Fills the myPyFunctions with strings corresponding to found fucntions.
753  */
754 void SUPERVGUI_PythonEditPane::initPyFunctions( QTextStream& theStream ) {
755
756   if ( theStream.atEnd() )
757     return;
758
759   QString aPyFunction;
760   QString aLine = theStream.readLine(); 
761
762   while ( !theStream.atEnd() ) { // not EOF
763
764     // asv : 23.11.04 : fix for PAL6870 : skip empty lines in the beginning or  between functions
765     //       find("def)==0 -> analizing only global function definitions which start with 0 indentation
766     while ( !aLine.isNull() && aLine.find( "def" ) != 0 ) 
767       aLine = theStream.readLine();
768     
769     if ( !aLine.isNull() && aLine.find("def") == 0 ) { 
770       myFunctionsCombo->insertItem( getFunctionName( aLine ) ); // aLine now == function name 
771
772       aPyFunction += aLine;
773       aPyFunction += '\n'; 
774
775       // read function body
776       aLine = theStream.readLine();
777       // asv : 23.11.04 : added "|| aLine.isEmpty()" - fix for PAL6870. readLine() returns an empty
778       //       string for "\n" string, it trails \n caracter.  but we accept such lines..
779       // asv : 22.12.04 : aLine[0].isSpace() || aLine[0]=='#' -> line must begin with space or tab
780       //       (a normal code with indentation) or comment sign.
781       while ( !aLine.isNull() && ( aLine.isEmpty() || aLine[0].isSpace() || aLine[0] == '#' ) ) {
782         aPyFunction += aLine;
783         aPyFunction += '\n'; 
784         aLine = theStream.readLine();
785       }
786
787       myPyFunctions << aPyFunction;
788       aPyFunction.setLength( 0 );
789     }
790   }
791 }
792
793 /**
794  * Load existing Python script
795  */
796 void SUPERVGUI_PythonEditPane::loadFile() {
797   QString aFileName = SUIT_FileDlg::getFileName(SUIT_Session::session()->activeApplication()->desktop(),
798                                                 "",
799                                                 "*.py",
800                                                 tr("TIT_LOADSCRIPT"),
801                                                 true);
802   if (aFileName.isEmpty()) return;
803
804   QFile aFile( aFileName );
805   if (!aFile.open(IO_ReadOnly)) {
806     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), 
807                          tr("MSG_CANT_LOADSCRIPT"));
808     return;
809   }
810
811   myPyFunctions.clear();
812   myFunctionsCombo->clear();
813   myText->clear();
814
815   QTextStream aFileReader(&aFile);
816   if ( aFileReader.atEnd() ) {
817     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_EMTY_FILE"));
818     aFile.close();
819     return;
820   }
821
822   initPyFunctions( aFileReader );
823
824   if ( myPyFunctions.size() )
825     readFunction( 0 );
826 }
827   
828 /**
829  * Finds and Reads a function from current position in the opened file
830  * asv : Comment above is old! Present model: just take an already read string = PyFunction
831  * from the list which is filled in loadFile().
832  */
833 void SUPERVGUI_PythonEditPane::readFunction( int i ) {
834   myText->clear();
835   if ( i != -1 && myPyFunctions.size() && myPyFunctions.size() > i )
836     myText->append( myPyFunctions[ i ] );
837
838   // asv : 30.11.04 : 2.7 - Inline node function editor improvement
839   // 2.7.1 - set focus to editor widget on editor window opening
840   // 2.7.2 - scroll to the beginning of function on editor window opening
841   myText->setFocus();
842   myText->ensureVisible( 0,0 );
843 }
844
845 /**
846  * Returns the text after "def" and before "(" -- first defined function name
847  */
848 QString SUPERVGUI_PythonEditPane::getFuncName() {
849   for (int i=0; i < myText->paragraphs(); i++) {
850     QString aName = getFunctionName( myText->text(i) );
851     if ( !aName.isEmpty() )
852       return aName;
853   }
854   return QString();
855 }
856
857 /**
858  * Returns the contents of the myText widget without trailing spacing 
859  */
860 SUPERV_Strings SUPERVGUI_PythonEditPane::getFunction() {
861   SUPERV_Strings aStrings = new SUPERV::ListOfStrings();
862   aStrings->length(myText->paragraphs());
863   for (int i=0; i < myText->paragraphs(); i++) {
864     QString aLine = myText->text(i);
865     // asv : 30.11.04 - why do we have to remove trailing spaces??  
866     // it's user's responsibility to enter correct Python code, we don't do anything with it.
867     // if (..) -- initial, while(..) -- my improvement, but also commented out -- needless.
868     //if (!aLine.right(1).compare(" ")) // replaced with the line below -- loop
869     //while (aLine.at(aLine.length()-1).isSpace()) // remove trailing spaces
870     //  aLine = aLine.remove(aLine.length()-1,1);
871     aStrings[i] = CORBA::string_dup(aLine.latin1());
872   }
873   return aStrings._retn();
874 }
875
876
877 void SUPERVGUI_PythonEditPane::setFunction(SUPERV_Strings theStr) {
878   myText->clear();
879   for ( int i=0, aLen = theStr->length(); i < aLen; i++ )
880     myText->append(QString(theStr[i]));
881
882   // asv : 30.11.04 : 2.7 - Inline node function editor improvement
883   // 2.7.1 - set focus to editor widget on editor window opening
884   // 2.7.2 - scroll to the beginning of function on editor window opening
885   myText->setFocus();
886   myText->ensureVisible( 0,0 );
887 }
888
889 /**
890  * Automatic indentation rule: if a previous line ended with 
891  * ':', then add N additional spaces in the current line.  If no ':' found, then 
892  * the same amount of spaces as in the previous line is added. Connected to 
893  * "returnPressed" signal of myText text edit.
894 */
895 void SUPERVGUI_PythonEditPane::autoIndentLine() {
896   const int N = 4; // width of indentation "tab"
897   if ( myText && myText->paragraphs() ) {
898
899     // get current cursor position and previous line (the one to be analized) 
900     int pos, para, i;
901     myText->getCursorPosition( &para, &pos ); // pos==0, beginning of line
902     QString line = myText->text( para-1 ); // previous paragraph line
903
904     // construct a string containing all leading space characters of previous line (tabs, etc.)
905     QString spacesStr;
906     i = -1;
907     while ( line[++i].isSpace() ) // append all isSpace() characters at beginning of line to spacesStr
908       spacesStr += line[i];
909
910     // if ':' was found -- add more spaces to spacesStr
911     line = line.stripWhiteSpace();
912     if ( line[ line.length()-1 ] == ':' ) {
913       i = 0;
914       while ( i++ < N ) 
915         spacesStr += ' ';
916     }
917
918     // ok, append spacesStr at the beginning of the current line = make indentation
919     myText->insertAt( spacesStr, para, pos );
920     myText->setCursorPosition( para, pos+spacesStr.length() );
921   }
922 }
923
924 /**
925  * Create a node by loading it from an external XML library file
926  * This slot opens a dialog box which then "lives" by itself..
927  */
928 void SUPERVGUI_PythonEditPane::library() {
929   // if CanImport() returns false, it displays an error message box, so there is no need to
930   // display such message here ("library file not found", etc.).
931   if ( SUPERVGUI_Library::getLibrary()->CanImport() ) {
932     SUPERVGUI_LibDlg* aDlg = new SUPERVGUI_LibDlg( this, myX, myY );
933     aDlg->exec();
934   }
935 }
936
937 /*!
938  * Edit Python dialog
939  */
940 SUPERVGUI_EditPythonDlg::SUPERVGUI_EditPythonDlg( bool isLoop )
941   :QDialog(SUIT_Session::session()->activeApplication()->desktop(), 0, true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu) 
942 {
943   setSizeGripEnabled( true );
944   setCaption(tr("TIT_FUNC_PYTHON"));
945   resize( 500, 250 );
946   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 7, 4);
947   int a,b; // dummies for PythonEditPane, not used, since library = false and myX, myY are not used in PythonEditPane
948   if (isLoop) {
949     QTabWidget* aLoopTabPane = new QTabWidget(this);
950     myInitPane = new SUPERVGUI_PythonEditPane( this, false, a, b ); // library == false, since no creation of a node is needed here
951     aLoopTabPane->addTab(myInitPane, "Init");
952     
953     myMorePane = new SUPERVGUI_PythonEditPane( this, false, a, b );
954     aLoopTabPane->addTab(myMorePane, "More");
955     
956     myNextPane = new SUPERVGUI_PythonEditPane( this, false, a, b );
957     aLoopTabPane->addTab(myNextPane, "Next");
958
959     aMainLayout->addWidget(aLoopTabPane);    
960   } else {
961     myEditPane = new SUPERVGUI_PythonEditPane( this, false, a, b );
962     aMainLayout->addWidget(myEditPane);
963   }
964   QGroupBox* aBtnBox = new QGroupBox( this );
965   aBtnBox->setColumnLayout( 0, Qt::Vertical );
966   aBtnBox->layout()->setSpacing( 0 ); aBtnBox->layout()->setMargin( 0 );
967   QHBoxLayout* aBtnLayout = new QHBoxLayout( aBtnBox->layout() );
968   aBtnLayout->setAlignment( Qt::AlignTop );
969   aBtnLayout->setSpacing( 6 ); aBtnLayout->setMargin( 11 );
970   
971   QPushButton* aOKBtn = new QPushButton( tr( "BUT_OK" ), aBtnBox );
972   connect( aOKBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
973   aBtnLayout->addWidget( aOKBtn );
974
975   aBtnLayout->addStretch();
976
977   QPushButton* aCancelBtn = new QPushButton( tr("BUT_CANCEL"), aBtnBox );
978   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
979   aBtnLayout->addWidget( aCancelBtn );
980
981   aMainLayout->addWidget(aBtnBox);
982 }
983
984 /**
985  * Do the following actions for newly created Engine's CNode:
986  * 1. Create a presentation for it (CanvasNode)
987  * 2. Place the CanvasNode to the current top-left corner or the current viewport
988  * 3. Increment the coordinates of the next CanvasNode (new nodes are "cascaded" when several of them are created at once)
989  * PS theEndNode is passed only for Loop and Switch nodes (EndLoop and EndSwitch)
990  */ 
991 void SUPERVGUI_Service::addNode( SUPERV::CNode_var theNode, SUPERV::INode_var theEndNode, int& theX, int& theY )  {
992   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
993   if ( !aSupMod ) {
994     MESSAGE("NULL Supervision module!");
995     return;
996   }
997   
998   SUPERVGUI_Main* aMain = aSupMod->getMain();
999
1000   if ( !CORBA::is_nil( theNode ) && aMain ) {
1001     
1002     aMain->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> PAL7960
1003
1004     int cx, cy;   //to appear a new node in the top-left corner of the current viewport
1005
1006     //2.8 point of improvements: Adding node to graph window with taking into account zoom factor
1007     QWMatrix aWM = aMain->getCanvasView()->worldMatrix();
1008     aMain->getCanvasView()->viewportToContents(theX, theY, cx, cy);
1009
1010     //2.8 point of improvements:
1011     cx = (int)((double)cx/aWM.m11());
1012     cy = (int)((double)cy/aWM.m22());
1013     theNode->Coords(cx, cy);
1014     if ( !CORBA::is_nil( theEndNode ) )
1015       theEndNode->Coords(cx + LABEL_WIDTH*2, cy);
1016     theX += (int)(NODE_DX*aWM.m11());
1017     theY += (int)(NODE_DY*aWM.m22());
1018
1019     if ( theNode->IsGOTO() )
1020       aMain->addGOTONode( theNode );
1021     else if ( theNode->IsMacro() )
1022       aMain->addMacroNode( theNode );
1023     else if ( theNode->IsLoop() || theNode->IsSwitch() )
1024       aMain->addControlNode( theNode, SUPERV::CNode::_narrow( theEndNode ), true );
1025     else
1026       aMain->addComputeNode( theNode );
1027   }
1028 }
1029