Salome HOME
Removed exportDataflow() function. It was called from SUPERVGUI class only, so the...
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Main.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_Main.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29 #include "QAD_Splitter.h"
30 #include "QAD_LeftFrame.h"
31 #include "QAD_ObjectBrowser.h"
32 #include "QAD_ObjectBrowserItem.h"
33 #include "QAD_PyEditor.h"
34 #include "QAD_Message.h"
35 #include "QAD_FileDlg.h"
36 #include "QAD_Application.h"
37 #include "SUPERVGUI_Def.h"
38 #include "QAD_RightFrame.h"
39 #include "SALOME_Event.hxx"
40 #include "SUPERVGraph_ViewFrame.h"
41 #include <qlayout.h>
42 #include <qfile.h>
43 #include "SUPERVGUI_Main.h"
44 #include "SUPERVGUI.h"
45 #include "SUPERVGUI_ComputeNode.h"
46 #include "SUPERVGUI_ControlNode.h"
47 #include "NOTIFICATION.hxx"
48 #include "SUPERVGUI_Notification.h"
49 #include "SALOMEGUI_ImportOperation.h"
50 #include "SUPERVGUI_Information.h"
51 #include "SUPERVGUI_CanvasControlNode.h"
52 #include <qvalidator.h>
53
54 /*
55 asv 20.10.04: removed 2 SUPERVGUI_Main constructors.  there is only ONE way 
56 to create a Main object now: with a non-null DataFlow as a 3d parameter
57 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, bool fromIOR)
58      : SUPERVGraph_View(theParent), 
59        myLogged( false ),
60        myFiltered( false ),
61        myLogFileName( QString::null ),
62        myLogFile( NULL ),
63        myWarning( false ),
64        myStep( false ),
65        myTrace( false ),
66        myVerbose( false )
67 {
68   Trace("SUPERVGUI_Main::SUPERVGUI_Main (new)");
69   theParent->setViewWidget(this); 
70   if (fromIOR) {
71     //SUPERVGUI_Main* am = Supervision.getMain();
72     QAD_ObjectBrowser* ob = ((QAD_StudyFrame*)(theDesktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
73     //        if (am == 0) {
74     //  ob = ((QAD_StudyFrame*)(theDesktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
75     //} else {
76     //ob = am->objectBrowser;
77     //}; 
78     QAD_ObjectBrowserItem* item = (QAD_ObjectBrowserItem*)(ob->getListView()->currentItem());
79     SALOMEDS::SObject_var obj = theDesktop->getActiveStudy()->getStudyDocument()->FindObjectID(item->getEntry().latin1());
80     SALOMEDS::GenericAttribute_var anAttr;
81     if (obj->FindAttribute(anAttr, "AttributeIOR")) {
82       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
83       Standard_CString ior = anIOR->Value();
84       dataflow = Supervision.getEngine()->getStreamGraph(ior);
85       if (SUPERV_isNull(dataflow)) {
86         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
87         close();
88       } else {
89         init(theDesktop);
90       }
91     } else {
92       QMessageBox::warning(0, tr("ERROR"), tr("MSG_NOACCESS_BY_IOR"));
93       close();
94     }
95   } else {
96     dataflow = Supervision.getEngine()->StreamGraph(MAIN_NEW);
97     if (SUPERV_isNull(dataflow)) {
98       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_DF"));
99       close();
100     } else {
101       init(theDesktop);
102     }
103   }
104 }
105
106 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, bool isModify, const char* f)
107      : SUPERVGraph_View(theParent),
108        myLogged( false ),
109        myFiltered( false ),
110        myLogFileName( QString::null ),
111        myLogFile( NULL ),
112        myWarning( false ),
113        myStep( false ),
114        myTrace( false ),
115        myVerbose( false )
116 {
117   Trace("SUPERVGUI_Main::SUPERVGUI_Main (file)")
118     theParent->setViewWidget(this);  
119   if (isModify) {
120     dataflow = Supervision.getEngine()->StreamGraph(f);
121   } else {
122     dataflow = Supervision.getEngine()->StreamGraphE(f);
123   }
124   if (SUPERV_isNull(dataflow)) {
125     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
126     close();
127   } else {
128     init(theDesktop);
129   }
130 }
131 */
132
133 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, SUPERV_Graph theDataFlow )
134      : SUPERVGraph_View(theParent),
135        myLogged( false ),
136        myFiltered( false ),
137        myLogFileName( QString::null ),
138        myLogFile( NULL ),
139        myWarning( false ),
140        myStep( false ),
141        myTrace( false ),
142        myVerbose( false )
143 {
144   Trace("SUPERVGUI_Main::SUPERVGUI_Main (copy)");
145   theParent->setViewWidget(this);  
146   dataflow = theDataFlow;
147   if (SUPERV_isNull(dataflow)) {
148     QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_COPY"));
149     close();
150   } else {
151     init(theDesktop);
152   }
153 }
154
155 void SUPERVGUI_Main::init(QAD_Desktop* theDesktop) {
156   Trace("SUPERVGUI_Main::init");
157   if (theDesktop) myNService = theDesktop->getNameService();
158   myHashCode = "New";
159   myCopyNum = 0;
160   choosing  = false;
161   myIsLocked = false;
162
163   myThread = new SUPERVGUI_Thread();
164   myThread->setMain(this);
165
166   myIsKilled = false;
167   myCurrentView = CANVAS;
168   myIsFromStudy = false;
169   myLastGraph = 0;
170   study     = theDesktop->getActiveStudy();
171   
172   SALOMEDS::Study_var studyDoc = study->getStudyDocument();
173   bool aLocked = studyDoc->GetProperties()->IsLocked();
174   SALOMEDS::StudyBuilder_var builder = studyDoc->NewBuilder();
175   SALOMEDS::SComponent_var father = studyDoc->FindComponent(STUDY_SUPERVISION);
176   SALOMEDS::GenericAttribute_var anAttr;
177   SALOMEDS::AttributeName_var    aName;
178   SALOMEDS::AttributePixMap_var  aPixmap;
179   if (father->_is_nil()) {
180     QAD_Operation* op = new SALOMEGUI_ImportOperation( study );
181     op->start();
182     if (aLocked) studyDoc->GetProperties()->SetLocked(false);
183     father = builder->NewComponent(STUDY_SUPERVISION);
184     anAttr = builder->FindOrCreateAttribute(father, "AttributeName");
185     aName = SALOMEDS::AttributeName::_narrow(anAttr);
186     aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
187     
188     anAttr = builder->FindOrCreateAttribute(father, "AttributePixMap");
189     aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
190     aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
191     
192     builder->DefineComponentInstance(father, Supervision.getEngine());
193     if (aLocked) studyDoc->GetProperties()->SetLocked(true);
194     op->finish();
195   };
196
197   objectBrowser = study->getActiveStudyFrame()->getLeftFrame()->getObjectBrowser();
198     
199
200   graph = new SUPERVGUI_Graph(this);
201   graph->hide();
202   array = new SUPERVGUI_Array(this);
203
204   myCanvas = new SUPERVGUI_Canvas(this);
205   myCanvasView = new SUPERVGUI_CanvasView(myCanvas, this);
206   
207   message = study->getActiveStudyFrame()->getRightFrame()->getMessage();
208   notification = new NOTIFICATION_Consumer();
209
210   QBoxLayout * layout = new QVBoxLayout(this);
211   layout->setMargin(0);
212   layout->setSpacing(0);
213   layout->addWidget(graph);
214   layout->addWidget(array);
215   layout->addWidget(myCanvasView);
216
217   if (myCurrentView == CANVAS || myCurrentView == CONTROLFLOW) {
218     myCanvas->merge();
219   }
220   sync();
221   show();
222   if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
223     myLogFile = fopen( myLogFileName.latin1(), "a" );
224     if (  myLogFile == NULL )
225       myLogged = false;
226   }
227   myTimer = new QTimer( this );
228   connect( myTimer, SIGNAL(timeout()), this, SLOT(checkExecution()) );
229 }
230
231 SUPERVGUI_Main::~SUPERVGUI_Main() {
232   Trace("SUPERVGUI_Main::~SUPERVGUI_Main");
233
234   // close all opened SubGraphs 
235   QMap<QString, QAD_StudyFrame*>::iterator it;
236   for (it = mySubGraphs.begin(); it != mySubGraphs.end(); ++it) {
237     it.data()->removeEventFilter(this);
238     it.data()->disconnect();
239     it.data()->close();
240
241     QAD_Study* aStudy = it.data()->getStudy();
242     aStudy->removeStudyFrame(it.data());
243   }
244   mySubGraphs.clear();
245   mySubGraphMap.clear();
246   /*
247   QAD_StudyFrame* aSubGraph;
248   for (aSubGraph = mySubGraphs.first(); aSubGraph; aSubGraph = mySubGraphs.next()) {
249     aSubGraph->removeEventFilter(this);
250     aSubGraph->close();
251   }
252   */
253
254   if ( myLogFile != NULL) {
255     fclose( myLogFile );
256   }
257   graph->removeLinks();
258   //delete myCanvas;
259   //  delete notification; // kloss : nota bene : quand un datalow est detruit : verifier que les canaux de notification sont aussi detruit
260   notification->_remove_ref();  // kloss : nota bene : quand un datalow est detruit : verifier que les canaux de notification sont aussi detruit
261
262   delete myCanvas;
263 }
264
265 void SUPERVGUI_Main::filterNotification() {
266   Trace("SUPERVGUI_Main::filterNotification");
267   SUPERVGUI_Notification* dlg = new SUPERVGUI_Notification(this);
268   dlg->setFiltered( myFiltered );
269   dlg->setLogged( myLogged, myLogFileName );
270   dlg->setWarning( myWarning );
271   dlg->setStep( myStep );
272   dlg->setTrace( myTrace );
273   dlg->setVerbose( myVerbose );
274   if ( dlg->exec() == QDialog::Accepted ) {
275     myLogged      = dlg->getLogged();
276     myLogFileName = dlg->getLogFile();
277     myFiltered    = dlg->getFiltered();
278     myWarning     = dlg->getWarning();
279     myStep        = dlg->getStep();
280     myTrace       = dlg->getTrace();
281     myVerbose     = dlg->getVerbose();
282     delete dlg;
283     if ( myLogFile != NULL) {
284       fclose( myLogFile );
285     }
286     myLogFile = NULL;
287     if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
288       myLogFile = fopen( myLogFileName.latin1(), "a" );
289       if (  myLogFile == NULL ) {
290         myLogged = false;
291         QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr( "ERR_CANT_OPEN_LOG_FILE" ) );
292       }
293     }
294   }
295 }
296
297 void SUPERVGUI_Main::changeDSGraphParameters() {
298   SUPERVGUI_DSGraphParameters* aDlg = new SUPERVGUI_DSGraphParameters(dataflow, dataflow->IsReadOnly());
299   if (aDlg->exec() )
300     sync();
301   delete aDlg;
302 }
303
304 void SUPERVGUI_Main::syncAsync() {
305     Trace("SUPERVGUI_Main::syncAsync")
306     QTimer::singleShot(1, this, SLOT(sync()));
307 }
308
309
310 /**
311  * Called by thread when dataflow is executing
312  */
313 void SUPERVGUI_Main::execute(char *  theNodeName, SUPERV::GraphState theNodeState) {
314   if (myCurrentView == CANVAS || myCurrentView == CONTROLFLOW) {
315     SUPERVGUI_CanvasNode* aNode = (SUPERVGUI_CanvasNode*) myCanvas->child(theNodeName, "SUPERVGUI_CanvasNode");
316     if (aNode) aNode->sync();
317   }
318   else {
319     SUPERVGUI_Node* aNodePrs; 
320     SUPERVGUI_GraphNode* aGraphNodePrs;
321     if (myCurrentView == TABLE) {
322       aNodePrs = (SUPERVGUI_Node*) array->child(theNodeName, "SUPERVGUI_Node");
323       aGraphNodePrs = (SUPERVGUI_GraphNode*) array->child(theNodeName, "SUPERVGUI_GraphNode");
324     } else {
325       aNodePrs = (SUPERVGUI_Node*) graph->child(theNodeName, "SUPERVGUI_Node");
326       aGraphNodePrs = (SUPERVGUI_GraphNode*) graph->child(theNodeName, "SUPERVGUI_GraphNode");
327     }
328     if (aGraphNodePrs) {
329       aGraphNodePrs->sync();
330     }
331     else if (aNodePrs) {
332       aNodePrs->syncOnEvent(theNodeState);
333     }
334   }
335 }
336
337
338 void SUPERVGUI_Main::sync() {
339     Trace("SUPERVGUI_Main::sync")
340     if ((SUPERV_isNull(dataflow))) return;
341     QString t = tr("GRAPH_TITLE");
342     
343     t += dataflow->Name();
344     setCaption(t);
345
346     study->updateObjBrowser();
347     if (myCurrentView == TABLE) {
348       array->sync();
349     } else if (myCurrentView == GRAPH) {
350       graph->sync();
351     } else {
352       myCanvas->sync();
353       myCanvas->update();
354     }
355 }
356
357
358 void SUPERVGUI_Main::showTable() {
359   if (myCurrentView == TABLE) return;
360
361   if (array->create()) {
362     if (myCurrentView == GRAPH)
363       graph->hide();
364     else
365       myCanvasView->hide();
366     myCurrentView = TABLE;
367   }
368   sync();
369 }
370
371
372 void SUPERVGUI_Main::showFullGraph() {
373   if (myCurrentView == TABLE) {
374     array->destroy();
375     graph->show();    
376   }
377   else if (myCurrentView != GRAPH) { // (myCurrentView == CANVAS) {
378     myCanvasView->hide();
379     graph->show();
380   }
381   myCurrentView = GRAPH;
382   graph->sync();
383   graph->setFullView();
384 }
385
386
387 void SUPERVGUI_Main::showContolFlow() {
388   bool merge = false;
389   if (myCurrentView == TABLE) {
390     array->destroy();
391     merge = true;
392   }
393   else if (myCurrentView == GRAPH) {
394     graph->hide();
395     merge = true;
396   }
397   myCurrentView = CONTROLFLOW;
398   myCanvas->setControlView();
399   if (merge) {
400     myCanvas->merge();
401     myCanvasView->show();
402   }
403 }
404
405 void SUPERVGUI_Main::showCanvas() {
406   if (myCurrentView == CANVAS) return;
407
408   bool merge = false;
409   if (myCurrentView == TABLE) {
410     array->destroy();
411     merge = true;
412   }
413   else if (myCurrentView == GRAPH) {
414     graph->hide();
415     merge = true;
416   }
417   myCurrentView = CANVAS;
418   myCanvas->setFullView();
419   if (merge) {
420     myCanvas->merge();
421     myCanvasView->show();
422   }
423 }
424
425 void SUPERVGUI_Main::insertFile() {
426     Trace("SUPERVGUI_Main::insertFile")
427     if ((SUPERV_isNull(dataflow))) return;
428
429     QString f = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
430                                          "",
431                                          "*.xml",
432                                          tr("MSG_GRAPH_INSERT"),
433                                          true);
434     if (!f.isEmpty()) {
435         if (dataflow->Import(f.latin1())) {
436           if (myCurrentView == TABLE) {
437             array->destroy();
438             array->create();
439           }
440           else if (myCurrentView != GRAPH) { // (myCurrentView == CANVAS) {
441             myCanvas->merge();
442           }
443           sync();
444         } else {
445             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
446         };
447     };
448 }
449
450 void SUPERVGUI_Main::copy() {
451   Trace("SUPERVGUI_Main::copy");
452   if (dataflow->ThreadsMax() == 0) {
453     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOTHING_COPY"));
454     return;
455   }
456   QAD_StudyFrame* aStudyFrame = Supervision.createGraph();
457   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
458     (aStudyFrame->getRightFrame()->getViewFrame());
459   if(aViewFrame){
460     SUPERV_Graph aNewGraph; //  = dataflow->StreamCopy();
461     if (dataflow->IsStreamGraph()) {
462       SUPERV_StreamGraph aSGraph = dataflow->ToStreamGraph();
463       if (SUPERV_isNull(aSGraph)) {
464         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_CANT_COPY"));
465         return;
466       }
467       aNewGraph = aSGraph->StreamCopy();
468     }
469     else {
470       aNewGraph = dataflow->Copy();
471     }
472     QString aNewName(tr("MSG_COPY_PREFIX").arg(++myCopyNum));
473     aNewName += dataflow->Name();
474     aNewGraph->SetName(aNewName);
475     /*SUPERVGUI_Main* m = */new SUPERVGUI_Main(aViewFrame, 
476                                            Supervision.getDesktop(), 
477                                            aNewGraph);
478     study->showFrame(aStudyFrame);
479   }
480 }
481
482 void SUPERVGUI_Main::openSubGraph(SUPERV_CNode theNode, bool correct)
483 {
484   if (theNode->IsMacro()) {
485     // get SubGraph from MacroNode
486     SUPERV_Graph aMacro = SUPERV::Graph::_narrow(theNode);
487     SUPERV_Graph aGraph;
488     if (aMacro->IsStreamMacro())
489       aGraph = aMacro->StreamObjRef();
490     else
491       aGraph = aMacro->FlowObjRef();
492
493     // display SubGraph
494     if (SUPERV_isNull(aGraph)) {
495       QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOACCESS"));
496       return;
497     }
498     else {
499       QString aGraphName = aGraph->Name();
500       QAD_StudyFrame* aStudyFrame;
501       if (mySubGraphs.contains(aGraphName)) {
502         aStudyFrame = mySubGraphs[aGraphName];
503         aStudyFrame->setActiveWindow();
504         aStudyFrame->setFocus();
505       }
506       else {
507         aStudyFrame = Supervision.createGraph();
508         if (aStudyFrame) {
509           SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
510             (aStudyFrame->getRightFrame()->getViewFrame());
511           if(aViewFrame) {
512             /*SUPERVGUI_Main* m = */new SUPERVGUI_Main(aViewFrame, 
513                                                    Supervision.getDesktop(), 
514                                                    aGraph);
515             //    connect(aStudyFrame, SIGNAL(sfStudyFrameClosing(QAD_StudyFrame*)), 
516             //            this,  SLOT(onSubGraphClosed(QAD_StudyFrame*)));
517             connect(aStudyFrame, SIGNAL(sfStudyFrameActivated(QAD_StudyFrame*)), 
518                     this,  SLOT(onSubGraphActivated(QAD_StudyFrame*)));
519             aStudyFrame->installEventFilter(this);
520
521             mySubGraphs.insert(aGraphName, aStudyFrame);
522             mySubGraphMap.insert(aGraphName, theNode->Name());
523
524           }
525         }
526       }
527       study->showFrame(aStudyFrame);
528       if (!correct) myLastGraph = aStudyFrame;
529     }
530   }
531 }
532
533 bool SUPERVGUI_Main::eventFilter( QObject* o, QEvent* e)
534 {
535   // workaround to get close event
536   if (o->inherits("QAD_StudyFrame") && e->type() == QEvent::Close) {
537     QAD_StudyFrame* aFrame = (QAD_StudyFrame*) o;
538     onSubGraphClosed(aFrame);
539   }
540   return SUPERVGraph_View::eventFilter(o, e);
541 }
542
543 // workaround to fix PAL6255 -> opened SubGraph is not on top
544 void SUPERVGUI_Main::onSubGraphActivated(QAD_StudyFrame* theStudyFrame)
545 {
546   if (myLastGraph) {
547     QAD_StudyFrame* aFrame = myLastGraph;
548     myLastGraph = 0;
549     aFrame->setActiveWindow();
550     aFrame->setFocus();
551   }
552 }
553
554 void SUPERVGUI_Main::onSubGraphClosed(QAD_StudyFrame* theStudyFrame)
555 {
556   QAD_ViewFrame* viewFrame = theStudyFrame->getRightFrame()->getViewFrame();
557   if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(viewFrame)){
558     theStudyFrame->removeEventFilter(this);
559     disconnect(theStudyFrame, 0, this, 0);
560
561     SUPERVGraph_View* view = supervFrame->getViewWidget();
562     SUPERVGUI_Main* aGraph = dynamic_cast<SUPERVGUI_Main*>(view);
563     if (aGraph) {
564       QString aGraphName = aGraph->getDataflow()->Name();
565       QMap<QString, QString>::iterator it = mySubGraphMap.find(aGraphName);
566       if (it != mySubGraphMap.end()) {
567         QString aNodeName = it.data();
568         SUPERVGUI_CanvasNode* aNode = (SUPERVGUI_CanvasNode*) 
569           myCanvas->child(aNodeName, "SUPERVGUI_CanvasNode");
570         if (aNode) {
571           aNode->merge();
572           myCanvas->update();
573         }
574         mySubGraphMap.remove(it);
575       }
576       mySubGraphs.remove(aGraphName);
577     }
578   }
579 }
580
581 void SUPERVGUI_Main::run() {
582   Trace("SUPERVGUI_Main::run")
583     if ((SUPERV_isNull(dataflow))) return;
584   
585   if (dataflow->IsEditing()) {
586     if (!dataflow->IsValid()) {
587       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
588     } else if (!dataflow->IsExecutable()) {
589       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
590     } else if (graph->isAnyLinkCreating() || myCanvasView->isAnyLinkCreating()) {
591       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
592     } else {
593       myRunTime = QDateTime::currentDateTime();
594       if (myIsKilled) {
595       //if (myIsRunned) {
596         if (!dataflow->ReRun()) {
597           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
598           if (dataflow->State() == SUPERV::ErrorState) {
599             kill();
600           }
601         } else {
602           myThread->startThread(tr("MSG_GRAPH_STARTED"));
603           sync();
604         }
605       } else {
606         if (!dataflow->Run()) {
607           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
608           if (dataflow->State() == SUPERV::ErrorState) {
609             kill();
610           }
611         } else {
612           myThread->startThread(tr("MSG_GRAPH_STARTED"));
613           sync();
614         }
615       }
616     }
617   } else {
618     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
619   }
620 }
621
622
623
624 void SUPERVGUI_Main::startExecute() {
625   Trace("SUPERVGUI_Main::startExecute")
626   if ((SUPERV_isNull(dataflow))) return;
627   
628   if (dataflow->IsEditing()) {
629     if (!dataflow->IsValid()) {
630       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
631     } else if (!dataflow->IsExecutable()) {
632       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
633     } else if (graph->isAnyLinkCreating() || myCanvasView->isAnyLinkCreating()) {
634       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
635     } else {
636       myRunTime = QDateTime::currentDateTime();
637       if (myIsKilled) {
638       //if (myIsRunned) {
639         if (!dataflow->ReStart()) {
640           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
641         } else {
642           myThread->startThread(tr("MSG_GRAPH_STARTED"));
643         }
644       }
645       else {
646         if (!dataflow->Start()) {
647           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
648         } else {
649           myThread->startThread(tr("MSG_GRAPH_STARTED"));
650         }
651       }
652     }
653   } else {
654     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
655   }
656 }
657
658
659 void SUPERVGUI_Main::kill() {
660     Trace("SUPERVGUI_Main::kill")
661     if ((SUPERV_isNull(dataflow))) return;
662
663     if (dataflow->IsEditing()) {
664       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
665     } else if (dataflow->Kill()) {
666       myIsKilled = true;
667       myThread->stopThread(tr("MSG_GRAPH_KILLED"));
668       sync();
669     } else {
670       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANTKILL_DF"));
671     }
672 }
673
674 void SUPERVGUI_Main::suspendResume() {
675     Trace("SUPERVGUI_Main::suspendResume")
676     if ((SUPERV_isNull(dataflow))) return;
677
678     if (dataflow->IsEditing()) {
679         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
680     } else if (dataflow->State() == SUPERV_Suspend) {
681       if (dataflow->Resume()) {
682         myThread->startThread(tr("MSG_DF_RESUMED"));
683       } else {
684         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESUME"));
685       }
686     } else {
687       if (dataflow->Suspend()) {
688         sync();
689         myThread->stopThread(tr("MSG_GRAPH_SUSPENDED"));
690       } else {
691         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SUSPEND"));
692       }
693     }
694 }
695
696 void SUPERVGUI_Main::stopRestart() { // kloss : a reviser et a connecter dans le popup du dataflow (pas de creation de bouton)
697     Trace("SUPERVGUI_Main::stopRestart")
698     if ((SUPERV_isNull(dataflow))) return;
699
700     if (dataflow->IsEditing()) {
701         QMessageBox::warning(0, tr("ERROR"),  tr("MSG_DF_NOTRUNNING"));
702     } else if (dataflow->State() == SUPERV_Stop) {
703         if (dataflow->ReStart()) {
704             message->setMessage(tr("MSG_DF_RESTARTED"));
705             sync();
706         } else {
707             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESTART"));
708         };
709     } else {
710         if (dataflow->Stop()) {
711             sync();
712         } else {
713             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_STOP"));
714         };
715     };
716 }
717
718 void SUPERVGUI_Main::addNode() {
719   Trace("SUPERVGUI_Main::addNode");
720   if (SUPERV_isNull(dataflow)) return;
721
722   if (dataflow->IsExecuting()) {
723     if (QMessageBox::warning(QAD_Application::getDesktop(), 
724                              tr("WARNING"), tr("MSG_GRAPH_ISRUN"),
725                              QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
726       return;        
727     } else {
728       kill();
729     }
730   }
731   Supervision.getBrowser()->choose();
732 }
733
734 /**
735  * Add Computation node
736  */
737 void SUPERVGUI_Main::addComputeNode(SUPERV_CNode theNode) {
738   //cout<<"### X="<<theNode->X()<<"  Y="<<theNode->Y()<<endl;
739   switch (myCurrentView) {
740   case GRAPH:
741     {
742       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
743       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
744       graph->addChild(aNode, theNode->X(), theNode->Y());
745       aNode->sync();
746     }
747     break;
748   case TABLE:
749     array->destroy();
750     array->create();
751     break;
752   case CANVAS:
753   case CONTROLFLOW: 
754     {
755       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasComputeNode(myCanvas, this, theNode);
756       aNode->move(theNode->X(), theNode->Y());
757       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
758       aNode->show();
759       myCanvas->update();
760     }
761     break;
762   }
763 }
764
765 /**
766  * Add GOTO node
767  */
768 void SUPERVGUI_Main::addGOTONode(SUPERV_CNode theNode) {
769   switch (myCurrentView) {
770   case GRAPH:
771     {
772       SUPERVGUI_Node* aNode = new SUPERVGUI_GotoNode(graph->viewport(), this, theNode);
773       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
774       graph->addChild(aNode, theNode->X(), theNode->Y());
775       aNode->sync();
776     }
777     break;
778   case TABLE:
779     array->destroy();
780     array->create();
781     break;
782   case CANVAS:
783   case CONTROLFLOW: 
784     {
785       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasGotoNode(myCanvas, this, theNode);
786       aNode->move(theNode->X(), theNode->Y());
787       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
788       aNode->show();
789       myCanvas->update();
790     }
791     break;
792   }
793 }
794
795 /**
796  * Add Control node
797  */
798 void SUPERVGUI_Main::addControlNode(SUPERV_CNode theStartNode, SUPERV_CNode theEndNode, bool Update) {
799   switch (myCurrentView) {
800   case GRAPH:
801     {
802       SUPERVGUI_StartControlNode* aStartPrs = 
803         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
804       SUPERVGUI_EndControlNode* aEndPrs = 
805         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
806
807       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());            
808       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
809       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
810       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
811       if (Update) {
812         aStartPrs->updateLinksPrs();
813         aEndPrs->updateLinksPrs();
814       }
815       aStartPrs->sync();
816       aEndPrs->sync();
817       graph->repaintContents();
818     }
819     break;
820   case TABLE:
821     array->destroy();
822     array->create();
823     break;
824   case CANVAS:
825   case CONTROLFLOW: 
826     {
827       SUPERVGUI_CanvasStartNode* aStartNode = new SUPERVGUI_CanvasStartNode(myCanvas, this, theStartNode);
828       aStartNode->move(theStartNode->X(), theStartNode->Y());
829       if (myCurrentView == CONTROLFLOW) aStartNode->hideAll();
830
831       SUPERVGUI_CanvasEndNode* aEndNode = new SUPERVGUI_CanvasEndNode(myCanvas, this, theEndNode, aStartNode);
832       aEndNode->move(theEndNode->X(), theEndNode->Y());
833       if (myCurrentView == CONTROLFLOW) aEndNode->hideAll();
834
835       aStartNode->show();
836       aEndNode->show();
837       myCanvas->update();
838     }
839     break;
840   }
841 }
842
843
844 /**
845  * Add Macro node
846  */
847 void SUPERVGUI_Main::addMacroNode(SUPERV_CNode theNode) {
848   //cout<<"### X="<<theNode->X()<<"  Y="<<theNode->Y()<<endl;
849   switch (myCurrentView) {
850   case GRAPH:
851     {
852       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
853       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
854       graph->addChild(aNode, theNode->X(), theNode->Y());
855       aNode->sync();
856     }
857     break;
858   case TABLE:
859     array->destroy();
860     array->create();
861     break;
862   case CANVAS:
863   case CONTROLFLOW: 
864     {
865       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasMacroNode(myCanvas, this, theNode);
866       aNode->move(theNode->X(), theNode->Y());
867       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
868       aNode->show();
869       myCanvas->update();
870     }
871     break;
872   }
873 }
874
875
876 SUPERVGUI_Graph* SUPERVGUI_Main::getGraph() {
877     Trace("SUPERVGUI_Main::getGraph")
878     return(graph);
879 }
880
881 SUPERVGUI_Array* SUPERVGUI_Main::getArray() {
882     Trace("SUPERVGUI_Main::getArray")
883     return(array);
884 }
885
886 SUPERVGUI_Canvas* SUPERVGUI_Main::getCanvas() {
887     Trace("SUPERVGUI_Main::getCanvas")
888     return(myCanvas);
889 }
890
891 SUPERVGUI_CanvasView* SUPERVGUI_Main::getCanvasView() {
892     Trace("SUPERVGUI_Main::getCanvasView")
893       return(myCanvasView);
894 }
895
896 SUPERV_Graph SUPERVGUI_Main::getDataflow() {
897     Trace("SUPERVGUI_Main::getDataflow")
898     return(dataflow);
899 }
900
901 QAD_Message* SUPERVGUI_Main::getMessage() {
902     Trace("SUPERVGUI_Main::getMessage")
903     return(message);
904 }
905
906 QAD_Study* SUPERVGUI_Main::getStudy() {
907     Trace("SUPERVGUI_Main::getStudy")
908     return(study);
909 }
910
911 bool SUPERVGUI_Main::isArrayShown() {
912     Trace("SUPERVGUI_Main::isArrayShown")
913     return(myCurrentView == TABLE);
914 }
915
916 void SUPERVGUI_Main::showPopup(QPopupMenu* p, QMouseEvent* e) {
917   Trace("SUPERVGUI_Main::showPopup");
918   // To check is Supervision active?
919   if (myIsLocked) return;
920   //if (dataflow->IsExecuting()) return;
921
922   //if (QAD_Application::getDesktop()->getActiveComponent().compare(STUDY_SUPERVISION) !=0) return;
923   if (QAD_Application::getDesktop()->getActiveComponent().compare(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) ) !=0) return;
924
925   checkIsInStudy();
926   if (e->button() == RightButton) {
927     p->exec(e->globalPos());
928   }
929 }
930
931
932
933 void SUPERVGUI_Main::changeInformation() {
934   SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly());
935   if (aDlg->exec() )
936     sync();
937   delete aDlg;
938   /*    Trace("SUPERVGUI_Main::changeInformation")
939     if (Supervision.information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly())) {
940       sync();
941       }*/
942 }
943
944 // returns false, if can't add dataflow into the study
945 bool SUPERVGUI_Main::addStudy() {
946   Trace("SUPERVGUI_Main::addStudy");
947   if (myIsFromStudy) return false;
948   if ((SUPERV_isNull(dataflow))) return false;
949   
950   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
951   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
952   SALOMEDS::GenericAttribute_var anAttr;
953   SALOMEDS::AttributeName_var    aName;
954   SALOMEDS::AttributeIOR_var     anIORAttr;
955   SALOMEDS::AttributePixMap_var  aPixmap;
956   bool                           aLocked = aStudy->GetProperties()->IsLocked();
957   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
958   
959   // searching dataflow
960   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
961   if (aSO->_is_nil()) { // create new dataflow SObject
962     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
963     if (aComponent->_is_nil()) { // is supervision component not found, then create it
964       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
965       anOperation->start();
966       if (aLocked) aStudy->GetProperties()->SetLocked(false);
967       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
968       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
969       aName = SALOMEDS::AttributeName::_narrow(anAttr);
970       //aName->SetValue(STUDY_SUPERVISION);
971       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
972       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
973       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
974       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
975       aBuilder->DefineComponentInstance(aComponent, Supervision.getEngine());
976       if (aLocked) aStudy->GetProperties()->SetLocked(true);
977       anOperation->finish();
978     }
979     op->start();
980     aSO = aBuilder->NewObject(aComponent);
981     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
982     aName = SALOMEDS::AttributeName::_narrow(anAttr);
983     aName->SetValue(dataflow->Name());
984     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
985     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
986     anIORAttr->SetValue(dataflow->getIOR());
987     op->finish();
988     if (aLocked) return false;
989   }
990
991   sync();
992   Supervision.unregisterGraph(this);
993   Supervision.registerGraph(dataflow->getIOR(), this);
994   myIsFromStudy = true;
995   return true;
996 }
997
998
999 void SUPERVGUI_Main::chooseData(QListViewItem* item) {
1000     Trace("SUPERVGUI_Main::chooseData")
1001     if (choosing) {
1002         QString id = ((QAD_ObjectBrowserItem*)item)->getEntry();
1003         if (!id.isEmpty()) {
1004             SALOMEDS::SObject_var object = study->getStudyDocument()->FindObjectID(id.latin1());
1005             SALOMEDS::GenericAttribute_var anAttr;
1006             SALOMEDS::AttributeIOR_var     anIOR;
1007             Standard_CString      ior    = "";
1008             if (object->FindAttribute(anAttr, "AttributeIOR")) {
1009               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1010               ior = anIOR->Value();
1011               portIn->setValue(ior);
1012
1013               // stop selection
1014               choosing = false;
1015               setCursor(Supervision.getCursor());
1016               objectBrowser->setCursor(Supervision.getCursor());
1017               Supervision.putInfo("");
1018             }
1019         }
1020     }
1021 }
1022
1023 void SUPERVGUI_Main::setData(SUPERVGUI_PortIn* p) {
1024     Trace("SUPERVGUI_Main::setData")
1025     portIn   = p;
1026     choosing = true;
1027     setCursor(forbiddenCursor);
1028     objectBrowser->setCursor(pointingHandCursor);
1029     Supervision.putInfo(tr("MSG_CHOOSE_DATA"));
1030 }
1031
1032 SALOMEDS::SObject_var SearchOrCreateSOWithName(const SALOMEDS::Study_var theStudy,
1033                                                const SALOMEDS::SObject_var theSO,
1034                                                const char* theName,
1035                                                //QAD_Operation* theOperation,
1036                                                bool* theStarted) {
1037   SALOMEDS::SObject_var aResult;
1038   SALOMEDS::AttributeName_var aName;
1039   SALOMEDS::GenericAttribute_var anAttr;
1040   if (!*theStarted) { // optimisation
1041     SALOMEDS::ChildIterator_var anIterator = theStudy->NewChildIterator(theSO);
1042     for (; anIterator->More(); anIterator->Next()) {
1043       if (anIterator->Value()->FindAttribute(anAttr, "AttributeName")) {
1044         aName = SALOMEDS::AttributeName::_narrow(anAttr);
1045         if (strcmp(aName->Value(), theName) == 0) {
1046           aResult = anIterator->Value();
1047           break;
1048         }
1049       }
1050     }
1051   }
1052   if (!aResult->_is_nil()) return aResult;
1053   // add new SObject
1054   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
1055   if (!*theStarted) {
1056     *theStarted = true;
1057     //theOperation->start();
1058     aBuilder->NewCommand();
1059   }
1060   aResult = aBuilder->NewObject(theSO);
1061   anAttr = aBuilder->FindOrCreateAttribute(aResult, "AttributeName");
1062   aName = SALOMEDS::AttributeName::_narrow(anAttr);
1063   aName->SetValue(theName);
1064   return aResult;
1065 }
1066
1067 bool SUPERVGUI_Main::putDataStudy(SUPERV_Port p, const char* inout) {
1068   Trace("SUPERVGUI_Main::putDataStudy");
1069
1070   static bool isIn = false;
1071   if (isIn) return true; else isIn = true;
1072
1073   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
1074   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
1075   SALOMEDS::GenericAttribute_var anAttr;
1076   SALOMEDS::AttributeName_var    aName;
1077   SALOMEDS::AttributeIOR_var     anIORAttr;
1078   SALOMEDS::AttributePixMap_var  aPixmap;
1079   bool                           aTransaction = false;
1080   bool                           aLocked = aStudy->GetProperties()->IsLocked();
1081   //  QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
1082   
1083   // searching dataflow
1084   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
1085   if (aSO->_is_nil()) { // create new dataflow SObject
1086     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
1087     if (aComponent->_is_nil()) { // is supervision component not found, then create it
1088       //QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
1089       //anOperation->start();
1090       aBuilder->NewCommand();
1091       if (aLocked) aStudy->GetProperties()->SetLocked(false);
1092       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
1093       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
1094       aName = SALOMEDS::AttributeName::_narrow(anAttr);
1095       //aName->SetValue(STUDY_SUPERVISION);
1096       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
1097         
1098       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
1099       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
1100       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
1101       aBuilder->DefineComponentInstance(aComponent, Supervision.getEngine());
1102       if (aLocked) aStudy->GetProperties()->SetLocked(true);
1103       //      anOperation->finish();
1104       aBuilder->CommitCommand();
1105     }
1106     aTransaction = true;
1107     //op->start();
1108     aBuilder->NewCommand();
1109     aSO = aBuilder->NewObject(aComponent);
1110     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
1111     aName = SALOMEDS::AttributeName::_narrow(anAttr);
1112     aName->SetValue(dataflow->Name());
1113     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
1114     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
1115     anIORAttr->SetValue(dataflow->getIOR());
1116   }
1117   //QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
1118   aSO = SearchOrCreateSOWithName(aStudy, aSO,  // get run time SO
1119                                  QString("Run ") + myRunTime.toString() /*, anOperation*/, &aTransaction);
1120   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Node()->Name()/*, anOperation*/, &aTransaction); // get node SO
1121   aSO = SearchOrCreateSOWithName(aStudy, aSO, inout/*, anOperation*/, &aTransaction); // get in/out SO
1122   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Name()/*, anOperation*/, &aTransaction); // get port SO
1123
1124   if (aLocked) {
1125     if (aTransaction) aBuilder->CommitCommand();
1126       //op->finish();
1127     isIn = false;
1128     return false;
1129   }
1130
1131   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
1132   anIORAttr  = SALOMEDS::AttributeIOR::_narrow(anAttr);
1133   if (!aTransaction && strcmp(anIORAttr->Value(), p->ToString()) == 0) {
1134     isIn = false;
1135     return true;
1136   }
1137   // set object value to the study: if object is external, then put it with
1138   //                                 help of the specific component - owner
1139   if (p->IsIOR()) {
1140     // get according component driver for result object
1141     SALOME_LifeCycleCORBA aLCC(myNService);
1142     SUPERV_FNode aFNode = SUPERV::FNode::_narrow(p->Node());
1143     if (!aFNode->_is_nil()) {
1144       Engines::Component_var aComponent = aLCC.FindOrLoad_Component(aFNode->GetContainer(),
1145                                                                     aFNode->GetComponentName());
1146       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aComponent);
1147       if (!CORBA::is_nil(aDriver)) { // if driver was found, publish object
1148         CORBA::Object_ptr anObject = new CORBA::Object();
1149         CORBA::Any* anAny = p->ToAny();
1150         (*anAny) >>= anObject;
1151         
1152         if (aDriver->CanPublishInStudy(anObject)) {
1153           SALOMEDS::SObject_var aTmpSO;
1154           if (!aTransaction) {
1155             aTmpSO = aSO;
1156             aTransaction = true;
1157             //op->start();
1158             aBuilder->NewCommand();
1159           }
1160           aTmpSO = aDriver->PublishInStudy(aStudy, aTmpSO, anObject, "");
1161           aBuilder->Addreference(aSO, aTmpSO);
1162         } else { // can't publish object: abort transaction
1163           if (aTransaction) aBuilder->AbortCommand();
1164           //op->abort();
1165           isIn = false;
1166           return false;
1167         }
1168       } else { // component has no drivel, but could store IORs (like Calculator)
1169         SALOMEDS::SObject_var anIORSO = aStudy->FindObjectIOR(p->ToString());
1170         if (!CORBA::is_nil(anIORSO)) aBuilder->Addreference(aSO, anIORSO);
1171       }
1172     }
1173   } else {
1174     if (!aTransaction) {
1175       aTransaction = true;
1176       //      op->start();
1177       aBuilder->NewCommand();
1178     }
1179     anIORAttr->SetValue(p->ToString()); // ior attribute already set for the prevoius condition
1180   }
1181   
1182   if (aTransaction) 
1183     //op->finish();
1184     aBuilder->CommitCommand();
1185   if (!myThread->running())
1186     study->updateObjBrowser();
1187   isIn = false;
1188   return true;
1189 }
1190
1191
1192 void SUPERVGUI_Main::ActivatePanning()
1193 {
1194   if (graph->isVisible()) {
1195     graph->ActivatePanning();
1196   } else if (array->isVisible()) {
1197     array->ActivatePanning();
1198   } else if (myCanvasView->isVisible()) {
1199     myCanvasView->ActivatePanning();
1200   }
1201 }
1202
1203
1204 void SUPERVGUI_Main::ResetView()
1205 {
1206   if (graph->isVisible()) {
1207     graph->ResetView();
1208   } else if (array->isVisible()) {
1209     array->ResetView();
1210   } else if (myCanvasView->isVisible()) {
1211     myCanvasView->ResetView();
1212   }
1213 }
1214
1215
1216 void SUPERVGUI_Main::setAsFromStudy(bool theToStudy) {
1217   myIsFromStudy = theToStudy;
1218   graph->setAsFromStudy(theToStudy);
1219   array->setAsFromStudy(theToStudy);
1220   myCanvas->setAsFromStudy(theToStudy);
1221 }
1222
1223 void SUPERVGUI_Main::checkIsInStudy() {
1224   if (!myIsFromStudy) return;
1225
1226   SALOMEDS::Study_var aStudyDoc = study->getStudyDocument();
1227   SALOMEDS::SComponent_var aFatherLbl = aStudyDoc->FindComponent(STUDY_SUPERVISION);
1228   SALOMEDS::ChildIterator_var aChildIterator = aStudyDoc->NewChildIterator(aFatherLbl);
1229   SALOMEDS::SObject_var aDataflowLbl;
1230   SALOMEDS::GenericAttribute_var anAttr;
1231
1232   for (; aChildIterator->More(); aChildIterator->Next()) {
1233     aDataflowLbl = aChildIterator->Value();
1234     if (!aDataflowLbl->FindAttribute(anAttr, "AttributeIOR"))
1235       continue;
1236
1237     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1238     if (strcmp(anIOR->Value(), dataflow->getIOR()) == 0) return;
1239   }
1240   // This Graph considered as in study but in study it is not exists
1241   Supervision.unregisterGraph(this);
1242   setAsFromStudy(false);
1243 }
1244
1245 void SUPERVGUI_Main::syncNotification() {
1246   char* graph;
1247   char* node;
1248   char* type;
1249   char* message;
1250   char* sender;
1251   long  counter;
1252   char* date;
1253   long  stamp;
1254   
1255   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1256     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1257       QString mess("");
1258       mess += "NOTIF: "; mess += graph;
1259       mess += " / "    ; mess += node;
1260       mess += " / "    ; mess += type;
1261       mess += " / "    ; mess += message;
1262       getMessage()->setMessage(mess.latin1());
1263     };
1264   };
1265 }
1266   
1267 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1268                                 char* sender, long counter, char* date, long stamp) {
1269   Trace("SUPERVGUI_Main::isFiltered");
1270   bool b = false;
1271   if (strcmp(getDataflow()->Name(), graph) == 0) {
1272     SUPERVGUI_Node* n;
1273     QObjectList* nodes = queryList("SUPERVGUI_Node");
1274     QObjectListIt i(*nodes);
1275     while ((n=(SUPERVGUI_Node*)i.current()) != 0) {
1276       ++i;
1277       if (strcmp(n->name(), node) == 0) {
1278         if (strcmp(type, NOTIF_WARNING) == 0) {
1279           b = n->isWarning();
1280         } else if (strcmp(type, NOTIF_STEP) == 0) {
1281           b = n->isStep();
1282         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1283           b = n->isTrace();
1284         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1285           b = n->isVerbose();
1286         };
1287         break;
1288       };
1289     };
1290     delete nodes;
1291     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1292       fprintf( myLogFile, "NOTIF %ld\t%s\t%s\t%ld\t%s\t%s\t%s\t%s\n", stamp, date, sender, counter, graph, node, type, message );
1293       fflush( myLogFile );
1294     };
1295   };
1296   return( b );
1297 }
1298
1299 void SUPERVGUI_Main::closeEvent(QCloseEvent* e) {
1300   e->accept();
1301 }
1302
1303
1304 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1305
1306   array->setPaletteBackgroundColor(color);
1307   graph->setPaletteBackgroundColor(color);
1308   myCanvas->setBackgroundColor(color);
1309   myCanvasView->setPaletteBackgroundColor(color.light());
1310   
1311   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1312 }
1313
1314 QPtrList< char * > SUPERVGUI_Main::getEventNodes() {
1315   return myEventNodes;
1316 }
1317
1318 void SUPERVGUI_Main::setEventNodes(QPtrList< char * > theEventNodes) {
1319   myEventNodes = theEventNodes;
1320 }
1321
1322 QPtrList< SUPERV::GraphState > SUPERVGUI_Main::getStates() {
1323   return myStates;
1324 }
1325
1326 void SUPERVGUI_Main::setStates(QPtrList< SUPERV::GraphState > theStates) {
1327   myStates = theStates;
1328 }
1329
1330 int SUPERVGUI_Main::getNodesNumber() {
1331   //create a list of nodes of the graph
1332   SUPERV_Nodes nodes = getDataflow()->Nodes();
1333   int RetVal = nodes->CNodes.length() + nodes->FNodes.length() +
1334                nodes->INodes.length() + nodes->GNodes.length() +
1335                nodes->LNodes.length() + nodes->SNodes.length();
1336   return RetVal;
1337 }
1338
1339 SUPERVGUI_Thread* SUPERVGUI_Main::getMyThread() {
1340   return myThread;
1341 }
1342
1343 void SUPERVGUI_Main::startTimer() {
1344   myTimer->start(500);
1345 }
1346
1347 void SUPERVGUI_Main::executionFinished() {
1348   getStudy()->updateObjBrowser();
1349   myCanvas->update();
1350 }
1351
1352 void SUPERVGUI_Main::checkExecution() {
1353   if (myThread->finished()) {
1354     myTimer->stop();
1355     executionFinished();
1356   }
1357 }
1358
1359 /******************************* SUPERVGUI_Thread class ****************************************/
1360 SUPERVGUI_Thread::SUPERVGUI_Thread()
1361      :QThread()
1362 {
1363   myIsActive = false;
1364 }
1365
1366 SUPERVGUI_Thread::~SUPERVGUI_Thread()
1367 {
1368   //it is a virtual destructor and needs to be determine here
1369 }
1370
1371 void SUPERVGUI_Thread::startThread(const char* m)
1372 {
1373   if (!myIsActive) {
1374     myIsActive = true;
1375     //QThread::start();
1376     this->start();
1377     myMain->getMessage()->setMessage(m); 
1378     myMain->sync();
1379   }
1380 }
1381
1382 void SUPERVGUI_Thread::stopThread(const char* m)
1383 {
1384   myMain->getMessage()->setMessage(m);
1385 }
1386
1387 void SUPERVGUI_Thread::setMain(SUPERVGUI_Main* theMain)
1388 {
1389   myMain = theMain;
1390 }
1391
1392 void SUPERVGUI_Thread::KillThread(bool theValue)
1393 {
1394   myMutex.lock();
1395   myIsActive = !(theValue);
1396   myMutex.unlock();
1397 }
1398
1399 typedef TVoidMemFun2ArgEvent<SUPERVGUI_Main, char*, SUPERV::GraphState> TNodeSyncEvent;
1400
1401 void SUPERVGUI_Thread::run()
1402 {
1403   SUPERV_CNode aNode = NULL;
1404   SUPERV::GraphEvent aEvent = SUPERV::UndefinedEvent ;
1405   SUPERV::GraphState aState = SUPERV::UndefinedState ;
1406
1407   SUPERV_CNode aPrevNode = NULL;
1408   SUPERV::GraphEvent aPrevEvent = SUPERV::UndefinedEvent ;
1409   SUPERV::GraphState aPrevState = SUPERV::UndefinedState ;
1410
1411   char * aName;
1412   char * aPrevName;
1413
1414   QPtrList< char * > anEventNodes;
1415   QPtrList< SUPERV::GraphState > aStates;
1416
1417   myMain->startTimer();
1418   while(myIsActive) {
1419     myMain->getDataflow()->Event(aNode, aEvent, aState);
1420     
1421     if (aEvent == SUPERV::UndefinedEvent && aState == SUPERV::UndefinedState
1422         ||
1423         aEvent == SUPERV::NoEvent && aState == SUPERV::NoState
1424         ||
1425         aEvent == SUPERV::KillEvent && aState == SUPERV::KillState) {
1426
1427       if (myMain->getEventNodes().count()) {
1428         myMain->removeEventNodes();
1429       }    
1430       if (myMain->getStates().count()) {
1431         myMain->removeStates();
1432       }    
1433       myIsActive = false;
1434     }
1435     else {    
1436       if ( aNode != NULL && !CORBA::is_nil( aNode ) ) {
1437         aName = aNode->Name();
1438       }
1439
1440       if ( aPrevNode == NULL || CORBA::is_nil( aPrevNode ) ) {  //first initialize aPrev... variables
1441         anEventNodes = myMain->getEventNodes();
1442         anEventNodes.append( &aName ) ;
1443         myMain->setEventNodes(anEventNodes);
1444         
1445         aStates = myMain->getStates();
1446         aStates.append( &aState ) ;
1447         myMain->setStates(aStates);
1448       }
1449       else {
1450         if ( aEvent == aPrevEvent && aState == aPrevState) {
1451           QString aNameStr = aName;
1452           QString aPrevNameStr = aPrevName;
1453           if ( aNameStr != aPrevNameStr ) {
1454             anEventNodes = myMain->getEventNodes();
1455             anEventNodes.append( &aName ) ;
1456             myMain->setEventNodes(anEventNodes);
1457             
1458             aStates = myMain->getStates();
1459             aStates.append( &aState ) ;
1460             myMain->setStates(aStates);
1461           }
1462         }
1463         else {
1464           anEventNodes = myMain->getEventNodes();
1465           anEventNodes.append( &aName ) ;
1466           myMain->setEventNodes(anEventNodes);
1467           
1468           aStates = myMain->getStates();
1469           aStates.append( &aState ) ;
1470           myMain->setStates(aStates);
1471         }
1472       }
1473     }      
1474     if (!myIsActive) {
1475       switch (myMain->getDataflow()->State()) {
1476       case SUPERV_Editing :     
1477         stopThread(myMain->getDataflow()->IsReadOnly()? tr("MSG_GRAPH_READONLY"): tr("MSG_GRAPH_EDITING"));
1478         break;
1479         
1480       case SUPERV_Suspend : 
1481         stopThread(tr("MSG_GRAPH_SUSPENDED"));
1482         break;
1483  
1484       case SUPERV_Done : 
1485         stopThread(tr("MSG_GRAPH_FINISHED"));
1486         break;
1487         
1488       case SUPERV_Error :
1489         stopThread(tr("MSG_GRAPH_ABORTED"));
1490         break;
1491         
1492       case SUPERV_Kill:
1493         stopThread(tr("MSG_GRAPH_KILLED"));
1494         break;
1495       }
1496
1497       break;
1498     }
1499     if ( myMain->getEventNodes().count() ) {    
1500       //if list not empty call execute() -> sync()
1501       char * aNodeName = *(myMain->getEventNodes().getFirst());
1502       SUPERV::GraphState aNodeState = *(myMain->getStates().getFirst());
1503
1504       // It is PROHIBITED to deal with widgets in a secondary thread, so event posting is used here
1505       ProcessVoidEvent( new TNodeSyncEvent( myMain, &SUPERVGUI_Main::execute, aNodeName, aNodeState ) );
1506
1507       myMain->removeFirstEN();
1508       myMain->removeFirstS();
1509     }
1510
1511     aPrevNode = aNode;
1512     aPrevEvent = aEvent;
1513     aPrevState = aState;
1514
1515     if ( aPrevNode == NULL || CORBA::is_nil( aPrevNode ) ) 
1516       aPrevName = "";
1517     else 
1518       aPrevName = aPrevNode->Name();
1519     
1520     //usleep(10);
1521     //msleep(5);
1522   }
1523   // VSR: 04/12/03 ---> update object browser ufter finishing
1524 //   qApp->lock();
1525 //   myMain->getStudy()->updateObjBrowser();
1526 //   qApp->unlock();
1527   // VSR: 04/12/03 <---    
1528
1529   QThread::exit();
1530 }
1531
1532 /******************************* SUPERVGUI_DSGraphParameters class ****************************************/
1533 /*!
1534   Constructor
1535 */
1536 SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters(SUPERV_Graph theGraph, bool isReadOnly)
1537      : QDialog( QAD_Application::getDesktop(), "", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ) 
1538 {
1539   Trace("SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters");
1540   setCaption( tr( "TLT_DSGRAPHPARAMS" ) );
1541   setSizeGripEnabled( true );
1542   myGraph = theGraph;
1543
1544   QGridLayout* TopLayout = new QGridLayout( this );
1545   TopLayout->setSpacing( 6 );
1546   TopLayout->setMargin( 11 );
1547     
1548   QGroupBox* TopGroup = new QGroupBox( this, "TopGroup" );
1549   TopGroup->setColumnLayout(0, Qt::Vertical );
1550   TopGroup->layout()->setSpacing( 0 );
1551   TopGroup->layout()->setMargin( 0 );
1552   QGridLayout* TopGroupLayout = new QGridLayout( TopGroup->layout() );
1553   TopGroupLayout->setAlignment( Qt::AlignTop );
1554   TopGroupLayout->setSpacing( 6 );
1555   TopGroupLayout->setMargin( 11 );
1556
1557   // DeltaTime
1558   QLabel* DeltaTimeL = new QLabel( tr( "DELTATIME_LBL" ), TopGroup );  
1559   TopGroupLayout->addWidget( DeltaTimeL, 0, 0 );
1560   
1561   myDeltaTime = new QAD_SpinBoxDbl( TopGroup, 0.0, 1.0, 0.1);
1562   myDeltaTime->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1563   TopGroupLayout->addWidget( myDeltaTime, 0, 1 );
1564
1565   // TimeOut
1566   QLabel* TimeOutL = new QLabel( tr( "TIMEOUT_LBL" ), TopGroup); 
1567   TopGroupLayout->addWidget( TimeOutL, 1, 0 );
1568
1569   myTimeOut = new QLineEdit( TopGroup );
1570   myTimeOut->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1571   myTimeOut->setValidator( new QIntValidator(this) );
1572   myTimeOut->setMinimumSize( 100, 0 );
1573   myTimeOut->setReadOnly( isReadOnly );
1574   TopGroupLayout->addWidget( myTimeOut, 1, 1 );
1575
1576   // DataStreamTrace
1577   QLabel* DataStreamTraceL = new QLabel( tr( "DATASTREAMTRACE_LBL" ), TopGroup); 
1578   TopGroupLayout->addWidget( DataStreamTraceL, 2, 0 );
1579
1580   myDataStreamTrace = new QComboBox( TopGroup );
1581   myDataStreamTrace->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1582   myDataStreamTrace->insertItem("WithoutTrace");
1583   myDataStreamTrace->insertItem("SummaryTrace");
1584   myDataStreamTrace->insertItem("DetailedTrace");
1585   TopGroupLayout->addWidget( myDataStreamTrace, 2, 1 );
1586
1587   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
1588   GroupButtons->setColumnLayout(0, Qt::Vertical );
1589   GroupButtons->layout()->setSpacing( 0 );
1590   GroupButtons->layout()->setMargin( 0 );
1591   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
1592   GroupButtonsLayout->setAlignment( Qt::AlignTop );
1593   GroupButtonsLayout->setSpacing( 6 );
1594   GroupButtonsLayout->setMargin( 11 );
1595   
1596   QPushButton* okB     = new QPushButton( tr( "BUT_OK" ),     GroupButtons );
1597   QPushButton* cancelB = new QPushButton( tr( "BUT_CANCEL" ), GroupButtons );
1598
1599   GroupButtonsLayout->addWidget( okB, 0, 0 );
1600   GroupButtonsLayout->addItem  ( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
1601   GroupButtonsLayout->addWidget( cancelB, 0, 2 );
1602
1603   TopLayout->addWidget( TopGroup,     0, 0 );
1604   TopLayout->addWidget( GroupButtons, 1, 0 );
1605
1606   connect( okB,     SIGNAL( clicked() ), this, SLOT( accept() ) );
1607   connect( cancelB, SIGNAL( clicked() ), this, SLOT( reject() ) );
1608
1609   setData();
1610 }
1611
1612 /*!
1613   Destructor
1614 */
1615 SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters() {
1616   Trace("SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters");
1617 }
1618
1619 /*!
1620   Sets data function
1621 */
1622 void SUPERVGUI_DSGraphParameters::setData() {
1623   double aDeltaTime;
1624   long aTimeOut;
1625   SUPERV::KindOfDataStreamTrace aDataStreamTrace;
1626
1627 //  myGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1628   if (myGraph->IsStreamGraph()) {
1629     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1630     if (!SUPERV_isNull(aSGraph)) 
1631       aSGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1632   }
1633
1634   myDeltaTime->setValue(aDeltaTime);
1635   myTimeOut->setText(QString("%1").arg(aTimeOut));
1636   myDataStreamTrace->setCurrentItem((int)aDataStreamTrace);
1637 }
1638
1639 /*!
1640   <OK> button slot
1641 */
1642 void SUPERVGUI_DSGraphParameters::accept() {
1643 //   myGraph->SetStreamParams( myTimeOut->text().toLong(),
1644 //                          (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1645 //                          myDeltaTime->value());
1646   if (myGraph->IsStreamGraph()) {
1647     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1648     if (!SUPERV_isNull(aSGraph)) 
1649       aSGraph->SetStreamParams( myTimeOut->text().toLong(),
1650                                 (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1651                                 myDeltaTime->value());
1652   }
1653   QDialog::accept();
1654 }