Salome HOME
Merge with version on tag OCC-V2_1_0d
[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
426 bool SUPERVGUI_Main::exportDataflow(QString theFile) {
427   Trace("SUPERVGUI_Main::exportDataflow");
428   if ((SUPERV_isNull(dataflow))) return false;
429
430   if (!theFile.isEmpty()) {
431     if (!dataflow->Export(theFile.latin1())) {
432       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING"));
433       return false;
434     }
435   }
436   return true;
437 }
438
439 void SUPERVGUI_Main::insertFile() {
440     Trace("SUPERVGUI_Main::insertFile")
441     if ((SUPERV_isNull(dataflow))) return;
442
443     QString f = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
444                                          "",
445                                          "*.xml",
446                                          tr("MSG_GRAPH_INSERT"),
447                                          true);
448     if (!f.isEmpty()) {
449         if (dataflow->Import(f.latin1())) {
450           if (myCurrentView == TABLE) {
451             array->destroy();
452             array->create();
453           }
454           else if (myCurrentView != GRAPH) { // (myCurrentView == CANVAS) {
455             myCanvas->merge();
456           }
457           sync();
458         } else {
459             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
460         };
461     };
462 }
463
464 void SUPERVGUI_Main::copy() {
465   Trace("SUPERVGUI_Main::copy");
466   if (dataflow->ThreadsMax() == 0) {
467     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOTHING_COPY"));
468     return;
469   }
470   QAD_StudyFrame* aStudyFrame = Supervision.createGraph();
471   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
472     (aStudyFrame->getRightFrame()->getViewFrame());
473   if(aViewFrame){
474     SUPERV_Graph aNewGraph; //  = dataflow->StreamCopy();
475     if (dataflow->IsStreamGraph()) {
476       SUPERV_StreamGraph aSGraph = dataflow->ToStreamGraph();
477       if (SUPERV_isNull(aSGraph)) {
478         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_CANT_COPY"));
479         return;
480       }
481       aNewGraph = aSGraph->StreamCopy();
482     }
483     else {
484       aNewGraph = dataflow->Copy();
485     }
486     QString aNewName(tr("MSG_COPY_PREFIX").arg(++myCopyNum));
487     aNewName += dataflow->Name();
488     aNewGraph->SetName(aNewName);
489     /*SUPERVGUI_Main* m = */new SUPERVGUI_Main(aViewFrame, 
490                                            Supervision.getDesktop(), 
491                                            aNewGraph);
492     study->showFrame(aStudyFrame);
493   }
494 }
495
496 void SUPERVGUI_Main::openSubGraph(SUPERV_CNode theNode, bool correct)
497 {
498   if (theNode->IsMacro()) {
499     // get SubGraph from MacroNode
500     SUPERV_Graph aMacro = SUPERV::Graph::_narrow(theNode);
501     SUPERV_Graph aGraph;
502     if (aMacro->IsStreamMacro())
503       aGraph = aMacro->StreamObjRef();
504     else
505       aGraph = aMacro->FlowObjRef();
506
507     // display SubGraph
508     if (SUPERV_isNull(aGraph)) {
509       QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOACCESS"));
510       return;
511     }
512     else {
513       QString aGraphName = aGraph->Name();
514       QAD_StudyFrame* aStudyFrame;
515       if (mySubGraphs.contains(aGraphName)) {
516         aStudyFrame = mySubGraphs[aGraphName];
517         aStudyFrame->setActiveWindow();
518         aStudyFrame->setFocus();
519       }
520       else {
521         aStudyFrame = Supervision.createGraph();
522         if (aStudyFrame) {
523           SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
524             (aStudyFrame->getRightFrame()->getViewFrame());
525           if(aViewFrame) {
526             /*SUPERVGUI_Main* m = */new SUPERVGUI_Main(aViewFrame, 
527                                                    Supervision.getDesktop(), 
528                                                    aGraph);
529             //    connect(aStudyFrame, SIGNAL(sfStudyFrameClosing(QAD_StudyFrame*)), 
530             //            this,  SLOT(onSubGraphClosed(QAD_StudyFrame*)));
531             connect(aStudyFrame, SIGNAL(sfStudyFrameActivated(QAD_StudyFrame*)), 
532                     this,  SLOT(onSubGraphActivated(QAD_StudyFrame*)));
533             aStudyFrame->installEventFilter(this);
534
535             mySubGraphs.insert(aGraphName, aStudyFrame);
536             mySubGraphMap.insert(aGraphName, theNode->Name());
537
538           }
539         }
540       }
541       study->showFrame(aStudyFrame);
542       if (!correct) myLastGraph = aStudyFrame;
543     }
544   }
545 }
546
547 bool SUPERVGUI_Main::eventFilter( QObject* o, QEvent* e)
548 {
549   // workaround to get close event
550   if (o->inherits("QAD_StudyFrame") && e->type() == QEvent::Close) {
551     QAD_StudyFrame* aFrame = (QAD_StudyFrame*) o;
552     onSubGraphClosed(aFrame);
553   }
554   return SUPERVGraph_View::eventFilter(o, e);
555 }
556
557 // workaround to fix PAL6255 -> opened SubGraph is not on top
558 void SUPERVGUI_Main::onSubGraphActivated(QAD_StudyFrame* theStudyFrame)
559 {
560   if (myLastGraph) {
561     QAD_StudyFrame* aFrame = myLastGraph;
562     myLastGraph = 0;
563     aFrame->setActiveWindow();
564     aFrame->setFocus();
565   }
566 }
567
568 void SUPERVGUI_Main::onSubGraphClosed(QAD_StudyFrame* theStudyFrame)
569 {
570   QAD_ViewFrame* viewFrame = theStudyFrame->getRightFrame()->getViewFrame();
571   if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(viewFrame)){
572     theStudyFrame->removeEventFilter(this);
573     disconnect(theStudyFrame, 0, this, 0);
574
575     SUPERVGraph_View* view = supervFrame->getViewWidget();
576     SUPERVGUI_Main* aGraph = dynamic_cast<SUPERVGUI_Main*>(view);
577     if (aGraph) {
578       QString aGraphName = aGraph->getDataflow()->Name();
579       QMap<QString, QString>::iterator it = mySubGraphMap.find(aGraphName);
580       if (it != mySubGraphMap.end()) {
581         QString aNodeName = it.data();
582         SUPERVGUI_CanvasNode* aNode = (SUPERVGUI_CanvasNode*) 
583           myCanvas->child(aNodeName, "SUPERVGUI_CanvasNode");
584         if (aNode) {
585           aNode->merge();
586           myCanvas->update();
587         }
588         mySubGraphMap.remove(it);
589       }
590       mySubGraphs.remove(aGraphName);
591     }
592   }
593 }
594
595 void SUPERVGUI_Main::run() {
596   Trace("SUPERVGUI_Main::run")
597     if ((SUPERV_isNull(dataflow))) return;
598   
599   if (dataflow->IsEditing()) {
600     if (!dataflow->IsValid()) {
601       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
602     } else if (!dataflow->IsExecutable()) {
603       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
604     } else if (graph->isAnyLinkCreating() || myCanvasView->isAnyLinkCreating()) {
605       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
606     } else {
607       myRunTime = QDateTime::currentDateTime();
608       if (myIsKilled) {
609       //if (myIsRunned) {
610         if (!dataflow->ReRun()) {
611           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
612           if (dataflow->State() == SUPERV::ErrorState) {
613             kill();
614           }
615         } else {
616           myThread->startThread(tr("MSG_GRAPH_STARTED"));
617           sync();
618         }
619       } else {
620         if (!dataflow->Run()) {
621           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
622           if (dataflow->State() == SUPERV::ErrorState) {
623             kill();
624           }
625         } else {
626           myThread->startThread(tr("MSG_GRAPH_STARTED"));
627           sync();
628         }
629       }
630     }
631   } else {
632     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
633   }
634 }
635
636
637
638 void SUPERVGUI_Main::startExecute() {
639   Trace("SUPERVGUI_Main::startExecute")
640   if ((SUPERV_isNull(dataflow))) return;
641   
642   if (dataflow->IsEditing()) {
643     if (!dataflow->IsValid()) {
644       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
645     } else if (!dataflow->IsExecutable()) {
646       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
647     } else if (graph->isAnyLinkCreating() || myCanvasView->isAnyLinkCreating()) {
648       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
649     } else {
650       myRunTime = QDateTime::currentDateTime();
651       if (myIsKilled) {
652       //if (myIsRunned) {
653         if (!dataflow->ReStart()) {
654           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
655         } else {
656           myThread->startThread(tr("MSG_GRAPH_STARTED"));
657         }
658       }
659       else {
660         if (!dataflow->Start()) {
661           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
662         } else {
663           myThread->startThread(tr("MSG_GRAPH_STARTED"));
664         }
665       }
666     }
667   } else {
668     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
669   }
670 }
671
672
673 void SUPERVGUI_Main::kill() {
674     Trace("SUPERVGUI_Main::kill")
675     if ((SUPERV_isNull(dataflow))) return;
676
677     if (dataflow->IsEditing()) {
678       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
679     } else if (dataflow->Kill()) {
680       myIsKilled = true;
681       myThread->stopThread(tr("MSG_GRAPH_KILLED"));
682       sync();
683     } else {
684       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANTKILL_DF"));
685     }
686 }
687
688 void SUPERVGUI_Main::suspendResume() {
689     Trace("SUPERVGUI_Main::suspendResume")
690     if ((SUPERV_isNull(dataflow))) return;
691
692     if (dataflow->IsEditing()) {
693         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
694     } else if (dataflow->State() == SUPERV_Suspend) {
695       if (dataflow->Resume()) {
696         myThread->startThread(tr("MSG_DF_RESUMED"));
697       } else {
698         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESUME"));
699       }
700     } else {
701       if (dataflow->Suspend()) {
702         sync();
703         myThread->stopThread(tr("MSG_GRAPH_SUSPENDED"));
704       } else {
705         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SUSPEND"));
706       }
707     }
708 }
709
710 void SUPERVGUI_Main::stopRestart() { // kloss : a reviser et a connecter dans le popup du dataflow (pas de creation de bouton)
711     Trace("SUPERVGUI_Main::stopRestart")
712     if ((SUPERV_isNull(dataflow))) return;
713
714     if (dataflow->IsEditing()) {
715         QMessageBox::warning(0, tr("ERROR"),  tr("MSG_DF_NOTRUNNING"));
716     } else if (dataflow->State() == SUPERV_Stop) {
717         if (dataflow->ReStart()) {
718             message->setMessage(tr("MSG_DF_RESTARTED"));
719             sync();
720         } else {
721             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESTART"));
722         };
723     } else {
724         if (dataflow->Stop()) {
725             sync();
726         } else {
727             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_STOP"));
728         };
729     };
730 }
731
732 void SUPERVGUI_Main::addNode() {
733   Trace("SUPERVGUI_Main::addNode");
734   if (SUPERV_isNull(dataflow)) return;
735
736   if (dataflow->IsExecuting()) {
737     if (QMessageBox::warning(QAD_Application::getDesktop(), 
738                              tr("WARNING"), tr("MSG_GRAPH_ISRUN"),
739                              QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
740       return;        
741     } else {
742       kill();
743     }
744   }
745   Supervision.getBrowser()->choose();
746 }
747
748 /**
749  * Add Computation node
750  */
751 void SUPERVGUI_Main::addComputeNode(SUPERV_CNode theNode) {
752   //cout<<"### X="<<theNode->X()<<"  Y="<<theNode->Y()<<endl;
753   switch (myCurrentView) {
754   case GRAPH:
755     {
756       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
757       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
758       graph->addChild(aNode, theNode->X(), theNode->Y());
759       aNode->sync();
760     }
761     break;
762   case TABLE:
763     array->destroy();
764     array->create();
765     break;
766   case CANVAS:
767   case CONTROLFLOW: 
768     {
769       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasComputeNode(myCanvas, this, theNode);
770       aNode->move(theNode->X(), theNode->Y());
771       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
772       aNode->show();
773       myCanvas->update();
774     }
775     break;
776   }
777 }
778
779 /**
780  * Add GOTO node
781  */
782 void SUPERVGUI_Main::addGOTONode(SUPERV_CNode theNode) {
783   switch (myCurrentView) {
784   case GRAPH:
785     {
786       SUPERVGUI_Node* aNode = new SUPERVGUI_GotoNode(graph->viewport(), this, theNode);
787       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
788       graph->addChild(aNode, theNode->X(), theNode->Y());
789       aNode->sync();
790     }
791     break;
792   case TABLE:
793     array->destroy();
794     array->create();
795     break;
796   case CANVAS:
797   case CONTROLFLOW: 
798     {
799       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasGotoNode(myCanvas, this, theNode);
800       aNode->move(theNode->X(), theNode->Y());
801       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
802       aNode->show();
803       myCanvas->update();
804     }
805     break;
806   }
807 }
808
809 /**
810  * Add Control node
811  */
812 void SUPERVGUI_Main::addControlNode(SUPERV_CNode theStartNode, SUPERV_CNode theEndNode, bool Update) {
813   switch (myCurrentView) {
814   case GRAPH:
815     {
816       SUPERVGUI_StartControlNode* aStartPrs = 
817         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
818       SUPERVGUI_EndControlNode* aEndPrs = 
819         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
820
821       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());            
822       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
823       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
824       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
825       if (Update) {
826         aStartPrs->updateLinksPrs();
827         aEndPrs->updateLinksPrs();
828       }
829       aStartPrs->sync();
830       aEndPrs->sync();
831       graph->repaintContents();
832     }
833     break;
834   case TABLE:
835     array->destroy();
836     array->create();
837     break;
838   case CANVAS:
839   case CONTROLFLOW: 
840     {
841       SUPERVGUI_CanvasStartNode* aStartNode = new SUPERVGUI_CanvasStartNode(myCanvas, this, theStartNode);
842       aStartNode->move(theStartNode->X(), theStartNode->Y());
843       if (myCurrentView == CONTROLFLOW) aStartNode->hideAll();
844
845       SUPERVGUI_CanvasEndNode* aEndNode = new SUPERVGUI_CanvasEndNode(myCanvas, this, theEndNode, aStartNode);
846       aEndNode->move(theEndNode->X(), theEndNode->Y());
847       if (myCurrentView == CONTROLFLOW) aEndNode->hideAll();
848
849       aStartNode->show();
850       aEndNode->show();
851       myCanvas->update();
852     }
853     break;
854   }
855 }
856
857
858 /**
859  * Add Macro node
860  */
861 void SUPERVGUI_Main::addMacroNode(SUPERV_CNode theNode) {
862   //cout<<"### X="<<theNode->X()<<"  Y="<<theNode->Y()<<endl;
863   switch (myCurrentView) {
864   case GRAPH:
865     {
866       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
867       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
868       graph->addChild(aNode, theNode->X(), theNode->Y());
869       aNode->sync();
870     }
871     break;
872   case TABLE:
873     array->destroy();
874     array->create();
875     break;
876   case CANVAS:
877   case CONTROLFLOW: 
878     {
879       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasMacroNode(myCanvas, this, theNode);
880       aNode->move(theNode->X(), theNode->Y());
881       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
882       aNode->show();
883       myCanvas->update();
884     }
885     break;
886   }
887 }
888
889
890 SUPERVGUI_Graph* SUPERVGUI_Main::getGraph() {
891     Trace("SUPERVGUI_Main::getGraph")
892     return(graph);
893 }
894
895 SUPERVGUI_Array* SUPERVGUI_Main::getArray() {
896     Trace("SUPERVGUI_Main::getArray")
897     return(array);
898 }
899
900 SUPERVGUI_Canvas* SUPERVGUI_Main::getCanvas() {
901     Trace("SUPERVGUI_Main::getCanvas")
902     return(myCanvas);
903 }
904
905 SUPERVGUI_CanvasView* SUPERVGUI_Main::getCanvasView() {
906     Trace("SUPERVGUI_Main::getCanvasView")
907       return(myCanvasView);
908 }
909
910 SUPERV_Graph SUPERVGUI_Main::getDataflow() {
911     Trace("SUPERVGUI_Main::getDataflow")
912     return(dataflow);
913 }
914
915 QAD_Message* SUPERVGUI_Main::getMessage() {
916     Trace("SUPERVGUI_Main::getMessage")
917     return(message);
918 }
919
920 QAD_Study* SUPERVGUI_Main::getStudy() {
921     Trace("SUPERVGUI_Main::getStudy")
922     return(study);
923 }
924
925 bool SUPERVGUI_Main::isArrayShown() {
926     Trace("SUPERVGUI_Main::isArrayShown")
927     return(myCurrentView == TABLE);
928 }
929
930 void SUPERVGUI_Main::showPopup(QPopupMenu* p, QMouseEvent* e) {
931   Trace("SUPERVGUI_Main::showPopup");
932   // To check is Supervision active?
933   if (myIsLocked) return;
934   //if (dataflow->IsExecuting()) return;
935
936   //if (QAD_Application::getDesktop()->getActiveComponent().compare(STUDY_SUPERVISION) !=0) return;
937   if (QAD_Application::getDesktop()->getActiveComponent().compare(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) ) !=0) return;
938
939   checkIsInStudy();
940   if (e->button() == RightButton) {
941     p->exec(e->globalPos());
942   }
943 }
944
945
946
947 void SUPERVGUI_Main::changeInformation() {
948   SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly());
949   if (aDlg->exec() )
950     sync();
951   delete aDlg;
952   /*    Trace("SUPERVGUI_Main::changeInformation")
953     if (Supervision.information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly())) {
954       sync();
955       }*/
956 }
957
958 // returns false, if can't add dataflow into the study
959 bool SUPERVGUI_Main::addStudy() {
960   Trace("SUPERVGUI_Main::addStudy");
961   if (myIsFromStudy) return false;
962   if ((SUPERV_isNull(dataflow))) return false;
963   
964   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
965   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
966   SALOMEDS::GenericAttribute_var anAttr;
967   SALOMEDS::AttributeName_var    aName;
968   SALOMEDS::AttributeIOR_var     anIORAttr;
969   SALOMEDS::AttributePixMap_var  aPixmap;
970   bool                           aLocked = aStudy->GetProperties()->IsLocked();
971   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
972   
973   // searching dataflow
974   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
975   if (aSO->_is_nil()) { // create new dataflow SObject
976     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
977     if (aComponent->_is_nil()) { // is supervision component not found, then create it
978       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
979       anOperation->start();
980       if (aLocked) aStudy->GetProperties()->SetLocked(false);
981       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
982       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
983       aName = SALOMEDS::AttributeName::_narrow(anAttr);
984       //aName->SetValue(STUDY_SUPERVISION);
985       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
986       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
987       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
988       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
989       aBuilder->DefineComponentInstance(aComponent, Supervision.getEngine());
990       if (aLocked) aStudy->GetProperties()->SetLocked(true);
991       anOperation->finish();
992     }
993     op->start();
994     aSO = aBuilder->NewObject(aComponent);
995     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
996     aName = SALOMEDS::AttributeName::_narrow(anAttr);
997     aName->SetValue(dataflow->Name());
998     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
999     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
1000     anIORAttr->SetValue(dataflow->getIOR());
1001     op->finish();
1002     if (aLocked) return false;
1003   }
1004
1005   sync();
1006   Supervision.unregisterGraph(this);
1007   Supervision.registerGraph(dataflow->getIOR(), this);
1008   myIsFromStudy = true;
1009   return true;
1010 }
1011
1012
1013 void SUPERVGUI_Main::chooseData(QListViewItem* item) {
1014     Trace("SUPERVGUI_Main::chooseData")
1015     if (choosing) {
1016         QString id = ((QAD_ObjectBrowserItem*)item)->getEntry();
1017         if (!id.isEmpty()) {
1018             SALOMEDS::SObject_var object = study->getStudyDocument()->FindObjectID(id.latin1());
1019             SALOMEDS::GenericAttribute_var anAttr;
1020             SALOMEDS::AttributeIOR_var     anIOR;
1021             Standard_CString      ior    = "";
1022             if (object->FindAttribute(anAttr, "AttributeIOR")) {
1023               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1024               ior = anIOR->Value();
1025               portIn->setValue(ior);
1026
1027               // stop selection
1028               choosing = false;
1029               setCursor(Supervision.getCursor());
1030               objectBrowser->setCursor(Supervision.getCursor());
1031               Supervision.putInfo("");
1032             }
1033         }
1034     }
1035 }
1036
1037 void SUPERVGUI_Main::setData(SUPERVGUI_PortIn* p) {
1038     Trace("SUPERVGUI_Main::setData")
1039     portIn   = p;
1040     choosing = true;
1041     setCursor(forbiddenCursor);
1042     objectBrowser->setCursor(pointingHandCursor);
1043     Supervision.putInfo(tr("MSG_CHOOSE_DATA"));
1044 }
1045
1046 SALOMEDS::SObject_var SearchOrCreateSOWithName(const SALOMEDS::Study_var theStudy,
1047                                                const SALOMEDS::SObject_var theSO,
1048                                                const char* theName,
1049                                                //QAD_Operation* theOperation,
1050                                                bool* theStarted) {
1051   SALOMEDS::SObject_var aResult;
1052   SALOMEDS::AttributeName_var aName;
1053   SALOMEDS::GenericAttribute_var anAttr;
1054   if (!*theStarted) { // optimisation
1055     SALOMEDS::ChildIterator_var anIterator = theStudy->NewChildIterator(theSO);
1056     for (; anIterator->More(); anIterator->Next()) {
1057       if (anIterator->Value()->FindAttribute(anAttr, "AttributeName")) {
1058         aName = SALOMEDS::AttributeName::_narrow(anAttr);
1059         if (strcmp(aName->Value(), theName) == 0) {
1060           aResult = anIterator->Value();
1061           break;
1062         }
1063       }
1064     }
1065   }
1066   if (!aResult->_is_nil()) return aResult;
1067   // add new SObject
1068   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
1069   if (!*theStarted) {
1070     *theStarted = true;
1071     //theOperation->start();
1072     aBuilder->NewCommand();
1073   }
1074   aResult = aBuilder->NewObject(theSO);
1075   anAttr = aBuilder->FindOrCreateAttribute(aResult, "AttributeName");
1076   aName = SALOMEDS::AttributeName::_narrow(anAttr);
1077   aName->SetValue(theName);
1078   return aResult;
1079 }
1080
1081 bool SUPERVGUI_Main::putDataStudy(SUPERV_Port p, const char* inout) {
1082   Trace("SUPERVGUI_Main::putDataStudy");
1083
1084   static bool isIn = false;
1085   if (isIn) return true; else isIn = true;
1086
1087   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
1088   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
1089   SALOMEDS::GenericAttribute_var anAttr;
1090   SALOMEDS::AttributeName_var    aName;
1091   SALOMEDS::AttributeIOR_var     anIORAttr;
1092   SALOMEDS::AttributePixMap_var  aPixmap;
1093   bool                           aTransaction = false;
1094   bool                           aLocked = aStudy->GetProperties()->IsLocked();
1095   //  QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
1096   
1097   // searching dataflow
1098   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
1099   if (aSO->_is_nil()) { // create new dataflow SObject
1100     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
1101     if (aComponent->_is_nil()) { // is supervision component not found, then create it
1102       //QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
1103       //anOperation->start();
1104       aBuilder->NewCommand();
1105       if (aLocked) aStudy->GetProperties()->SetLocked(false);
1106       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
1107       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
1108       aName = SALOMEDS::AttributeName::_narrow(anAttr);
1109       //aName->SetValue(STUDY_SUPERVISION);
1110       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
1111         
1112       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
1113       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
1114       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
1115       aBuilder->DefineComponentInstance(aComponent, Supervision.getEngine());
1116       if (aLocked) aStudy->GetProperties()->SetLocked(true);
1117       //      anOperation->finish();
1118       aBuilder->CommitCommand();
1119     }
1120     aTransaction = true;
1121     //op->start();
1122     aBuilder->NewCommand();
1123     aSO = aBuilder->NewObject(aComponent);
1124     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
1125     aName = SALOMEDS::AttributeName::_narrow(anAttr);
1126     aName->SetValue(dataflow->Name());
1127     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
1128     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
1129     anIORAttr->SetValue(dataflow->getIOR());
1130   }
1131   //QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
1132   aSO = SearchOrCreateSOWithName(aStudy, aSO,  // get run time SO
1133                                  QString("Run ") + myRunTime.toString() /*, anOperation*/, &aTransaction);
1134   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Node()->Name()/*, anOperation*/, &aTransaction); // get node SO
1135   aSO = SearchOrCreateSOWithName(aStudy, aSO, inout/*, anOperation*/, &aTransaction); // get in/out SO
1136   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Name()/*, anOperation*/, &aTransaction); // get port SO
1137
1138   if (aLocked) {
1139     if (aTransaction) aBuilder->CommitCommand();
1140       //op->finish();
1141     isIn = false;
1142     return false;
1143   }
1144
1145   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
1146   anIORAttr  = SALOMEDS::AttributeIOR::_narrow(anAttr);
1147   if (!aTransaction && strcmp(anIORAttr->Value(), p->ToString()) == 0) {
1148     isIn = false;
1149     return true;
1150   }
1151   // set object value to the study: if object is external, then put it with
1152   //                                 help of the specific component - owner
1153   if (p->IsIOR()) {
1154     // get according component driver for result object
1155     SALOME_LifeCycleCORBA aLCC(myNService);
1156     SUPERV_FNode aFNode = SUPERV::FNode::_narrow(p->Node());
1157     if (!aFNode->_is_nil()) {
1158       Engines::Component_var aComponent = aLCC.FindOrLoad_Component(aFNode->GetContainer(),
1159                                                                     aFNode->GetComponentName());
1160       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aComponent);
1161       if (!CORBA::is_nil(aDriver)) { // if driver was found, publish object
1162         CORBA::Object_ptr anObject = new CORBA::Object();
1163         CORBA::Any* anAny = p->ToAny();
1164         (*anAny) >>= anObject;
1165         
1166         if (aDriver->CanPublishInStudy(anObject)) {
1167           SALOMEDS::SObject_var aTmpSO;
1168           if (!aTransaction) {
1169             aTmpSO = aSO;
1170             aTransaction = true;
1171             //op->start();
1172             aBuilder->NewCommand();
1173           }
1174           aTmpSO = aDriver->PublishInStudy(aStudy, aTmpSO, anObject, "");
1175           aBuilder->Addreference(aSO, aTmpSO);
1176         } else { // can't publish object: abort transaction
1177           if (aTransaction) aBuilder->AbortCommand();
1178           //op->abort();
1179           isIn = false;
1180           return false;
1181         }
1182       } else { // component has no drivel, but could store IORs (like Calculator)
1183         SALOMEDS::SObject_var anIORSO = aStudy->FindObjectIOR(p->ToString());
1184         if (!CORBA::is_nil(anIORSO)) aBuilder->Addreference(aSO, anIORSO);
1185       }
1186     }
1187   } else {
1188     if (!aTransaction) {
1189       aTransaction = true;
1190       //      op->start();
1191       aBuilder->NewCommand();
1192     }
1193     anIORAttr->SetValue(p->ToString()); // ior attribute already set for the prevoius condition
1194   }
1195   
1196   if (aTransaction) 
1197     //op->finish();
1198     aBuilder->CommitCommand();
1199   if (!myThread->running())
1200     study->updateObjBrowser();
1201   isIn = false;
1202   return true;
1203 }
1204
1205
1206 void SUPERVGUI_Main::ActivatePanning()
1207 {
1208   if (graph->isVisible()) {
1209     graph->ActivatePanning();
1210   } else if (array->isVisible()) {
1211     array->ActivatePanning();
1212   } else if (myCanvasView->isVisible()) {
1213     myCanvasView->ActivatePanning();
1214   }
1215 }
1216
1217
1218 void SUPERVGUI_Main::ResetView()
1219 {
1220   if (graph->isVisible()) {
1221     graph->ResetView();
1222   } else if (array->isVisible()) {
1223     array->ResetView();
1224   } else if (myCanvasView->isVisible()) {
1225     myCanvasView->ResetView();
1226   }
1227 }
1228
1229
1230 void SUPERVGUI_Main::setAsFromStudy(bool theToStudy) {
1231   myIsFromStudy = theToStudy;
1232   graph->setAsFromStudy(theToStudy);
1233   array->setAsFromStudy(theToStudy);
1234   myCanvas->setAsFromStudy(theToStudy);
1235 }
1236
1237 void SUPERVGUI_Main::checkIsInStudy() {
1238   if (!myIsFromStudy) return;
1239
1240   SALOMEDS::Study_var aStudyDoc = study->getStudyDocument();
1241   SALOMEDS::SComponent_var aFatherLbl = aStudyDoc->FindComponent(STUDY_SUPERVISION);
1242   SALOMEDS::ChildIterator_var aChildIterator = aStudyDoc->NewChildIterator(aFatherLbl);
1243   SALOMEDS::SObject_var aDataflowLbl;
1244   SALOMEDS::GenericAttribute_var anAttr;
1245
1246   for (; aChildIterator->More(); aChildIterator->Next()) {
1247     aDataflowLbl = aChildIterator->Value();
1248     if (!aDataflowLbl->FindAttribute(anAttr, "AttributeIOR"))
1249       continue;
1250
1251     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1252     if (strcmp(anIOR->Value(), dataflow->getIOR()) == 0) return;
1253   }
1254   // This Graph considered as in study but in study it is not exists
1255   Supervision.unregisterGraph(this);
1256   setAsFromStudy(false);
1257 }
1258
1259 void SUPERVGUI_Main::syncNotification() {
1260   char* graph;
1261   char* node;
1262   char* type;
1263   char* message;
1264   char* sender;
1265   long  counter;
1266   char* date;
1267   long  stamp;
1268   
1269   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1270     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1271       QString mess("");
1272       mess += "NOTIF: "; mess += graph;
1273       mess += " / "    ; mess += node;
1274       mess += " / "    ; mess += type;
1275       mess += " / "    ; mess += message;
1276       getMessage()->setMessage(mess.latin1());
1277     };
1278   };
1279 }
1280   
1281 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1282                                 char* sender, long counter, char* date, long stamp) {
1283   Trace("SUPERVGUI_Main::isFiltered");
1284   bool b = false;
1285   if (strcmp(getDataflow()->Name(), graph) == 0) {
1286     SUPERVGUI_Node* n;
1287     QObjectList* nodes = queryList("SUPERVGUI_Node");
1288     QObjectListIt i(*nodes);
1289     while ((n=(SUPERVGUI_Node*)i.current()) != 0) {
1290       ++i;
1291       if (strcmp(n->name(), node) == 0) {
1292         if (strcmp(type, NOTIF_WARNING) == 0) {
1293           b = n->isWarning();
1294         } else if (strcmp(type, NOTIF_STEP) == 0) {
1295           b = n->isStep();
1296         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1297           b = n->isTrace();
1298         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1299           b = n->isVerbose();
1300         };
1301         break;
1302       };
1303     };
1304     delete nodes;
1305     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1306       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 );
1307       fflush( myLogFile );
1308     };
1309   };
1310   return( b );
1311 }
1312
1313 void SUPERVGUI_Main::closeEvent(QCloseEvent* e) {
1314   e->accept();
1315 }
1316
1317
1318 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1319
1320   array->setPaletteBackgroundColor(color);
1321   graph->setPaletteBackgroundColor(color);
1322   myCanvas->setBackgroundColor(color);
1323   myCanvasView->setPaletteBackgroundColor(color.light());
1324   
1325   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1326 }
1327
1328 QPtrList< char * > SUPERVGUI_Main::getEventNodes() {
1329   return myEventNodes;
1330 }
1331
1332 void SUPERVGUI_Main::setEventNodes(QPtrList< char * > theEventNodes) {
1333   myEventNodes = theEventNodes;
1334 }
1335
1336 QPtrList< SUPERV::GraphState > SUPERVGUI_Main::getStates() {
1337   return myStates;
1338 }
1339
1340 void SUPERVGUI_Main::setStates(QPtrList< SUPERV::GraphState > theStates) {
1341   myStates = theStates;
1342 }
1343
1344 int SUPERVGUI_Main::getNodesNumber() {
1345   //create a list of nodes of the graph
1346   SUPERV_Nodes nodes = getDataflow()->Nodes();
1347   int RetVal = nodes->CNodes.length() + nodes->FNodes.length() +
1348                nodes->INodes.length() + nodes->GNodes.length() +
1349                nodes->LNodes.length() + nodes->SNodes.length();
1350   return RetVal;
1351 }
1352
1353 SUPERVGUI_Thread* SUPERVGUI_Main::getMyThread() {
1354   return myThread;
1355 }
1356
1357 void SUPERVGUI_Main::startTimer() {
1358   myTimer->start(500);
1359 }
1360
1361 void SUPERVGUI_Main::executionFinished() {
1362   getStudy()->updateObjBrowser();
1363   myCanvas->update();
1364 }
1365
1366 void SUPERVGUI_Main::checkExecution() {
1367   if (myThread->finished()) {
1368     myTimer->stop();
1369     executionFinished();
1370   }
1371 }
1372
1373 /******************************* SUPERVGUI_Thread class ****************************************/
1374 SUPERVGUI_Thread::SUPERVGUI_Thread()
1375      :QThread()
1376 {
1377   myIsActive = false;
1378 }
1379
1380 SUPERVGUI_Thread::~SUPERVGUI_Thread()
1381 {
1382   //it is a virtual destructor and needs to be determine here
1383 }
1384
1385 void SUPERVGUI_Thread::startThread(const char* m)
1386 {
1387   if (!myIsActive) {
1388     myIsActive = true;
1389     //QThread::start();
1390     this->start();
1391     myMain->getMessage()->setMessage(m); 
1392     myMain->sync();
1393   }
1394 }
1395
1396 void SUPERVGUI_Thread::stopThread(const char* m)
1397 {
1398   myMain->getMessage()->setMessage(m);
1399 }
1400
1401 void SUPERVGUI_Thread::setMain(SUPERVGUI_Main* theMain)
1402 {
1403   myMain = theMain;
1404 }
1405
1406 void SUPERVGUI_Thread::KillThread(bool theValue)
1407 {
1408   myMutex.lock();
1409   myIsActive = !(theValue);
1410   myMutex.unlock();
1411 }
1412
1413 typedef TVoidMemFun2ArgEvent<SUPERVGUI_Main, char*, SUPERV::GraphState> TNodeSyncEvent;
1414
1415 void SUPERVGUI_Thread::run()
1416 {
1417   SUPERV_CNode aNode = NULL;
1418   SUPERV::GraphEvent aEvent = SUPERV::UndefinedEvent ;
1419   SUPERV::GraphState aState = SUPERV::UndefinedState ;
1420
1421   SUPERV_CNode aPrevNode = NULL;
1422   SUPERV::GraphEvent aPrevEvent = SUPERV::UndefinedEvent ;
1423   SUPERV::GraphState aPrevState = SUPERV::UndefinedState ;
1424
1425   char * aName;
1426   char * aPrevName;
1427
1428   QPtrList< char * > anEventNodes;
1429   QPtrList< SUPERV::GraphState > aStates;
1430
1431   myMain->startTimer();
1432   while(myIsActive) {
1433     myMain->getDataflow()->Event(aNode, aEvent, aState);
1434     
1435     if (aEvent == SUPERV::UndefinedEvent && aState == SUPERV::UndefinedState
1436         ||
1437         aEvent == SUPERV::NoEvent && aState == SUPERV::NoState
1438         ||
1439         aEvent == SUPERV::KillEvent && aState == SUPERV::KillState) {
1440
1441       if (myMain->getEventNodes().count()) {
1442         myMain->removeEventNodes();
1443       }    
1444       if (myMain->getStates().count()) {
1445         myMain->removeStates();
1446       }    
1447       myIsActive = false;
1448     }
1449     else {    
1450       if ( aNode != NULL && !CORBA::is_nil( aNode ) ) {
1451         aName = aNode->Name();
1452       }
1453
1454       if ( aPrevNode == NULL || CORBA::is_nil( aPrevNode ) ) {  //first initialize aPrev... variables
1455         anEventNodes = myMain->getEventNodes();
1456         anEventNodes.append( &aName ) ;
1457         myMain->setEventNodes(anEventNodes);
1458         
1459         aStates = myMain->getStates();
1460         aStates.append( &aState ) ;
1461         myMain->setStates(aStates);
1462       }
1463       else {
1464         if ( aEvent == aPrevEvent && aState == aPrevState) {
1465           QString aNameStr = aName;
1466           QString aPrevNameStr = aPrevName;
1467           if ( aNameStr != aPrevNameStr ) {
1468             anEventNodes = myMain->getEventNodes();
1469             anEventNodes.append( &aName ) ;
1470             myMain->setEventNodes(anEventNodes);
1471             
1472             aStates = myMain->getStates();
1473             aStates.append( &aState ) ;
1474             myMain->setStates(aStates);
1475           }
1476         }
1477         else {
1478           anEventNodes = myMain->getEventNodes();
1479           anEventNodes.append( &aName ) ;
1480           myMain->setEventNodes(anEventNodes);
1481           
1482           aStates = myMain->getStates();
1483           aStates.append( &aState ) ;
1484           myMain->setStates(aStates);
1485         }
1486       }
1487     }      
1488     if (!myIsActive) {
1489       switch (myMain->getDataflow()->State()) {
1490       case SUPERV_Editing :     
1491         stopThread(myMain->getDataflow()->IsReadOnly()? tr("MSG_GRAPH_READONLY"): tr("MSG_GRAPH_EDITING"));
1492         break;
1493         
1494       case SUPERV_Suspend : 
1495         stopThread(tr("MSG_GRAPH_SUSPENDED"));
1496         break;
1497  
1498       case SUPERV_Done : 
1499         stopThread(tr("MSG_GRAPH_FINISHED"));
1500         break;
1501         
1502       case SUPERV_Error :
1503         stopThread(tr("MSG_GRAPH_ABORTED"));
1504         break;
1505         
1506       case SUPERV_Kill:
1507         stopThread(tr("MSG_GRAPH_KILLED"));
1508         break;
1509       }
1510
1511       break;
1512     }
1513     if ( myMain->getEventNodes().count() ) {    
1514       //if list not empty call execute() -> sync()
1515       char * aNodeName = *(myMain->getEventNodes().getFirst());
1516       SUPERV::GraphState aNodeState = *(myMain->getStates().getFirst());
1517
1518       // It is PROHIBITED to deal with widgets in a secondary thread, so event posting is used here
1519       ProcessVoidEvent( new TNodeSyncEvent( myMain, &SUPERVGUI_Main::execute, aNodeName, aNodeState ) );
1520
1521       myMain->removeFirstEN();
1522       myMain->removeFirstS();
1523     }
1524
1525     aPrevNode = aNode;
1526     aPrevEvent = aEvent;
1527     aPrevState = aState;
1528
1529     if ( aPrevNode == NULL || CORBA::is_nil( aPrevNode ) ) 
1530       aPrevName = "";
1531     else 
1532       aPrevName = aPrevNode->Name();
1533     
1534     //usleep(10);
1535     //msleep(5);
1536   }
1537   // VSR: 04/12/03 ---> update object browser ufter finishing
1538 //   qApp->lock();
1539 //   myMain->getStudy()->updateObjBrowser();
1540 //   qApp->unlock();
1541   // VSR: 04/12/03 <---    
1542
1543   QThread::exit();
1544 }
1545
1546 /******************************* SUPERVGUI_DSGraphParameters class ****************************************/
1547 /*!
1548   Constructor
1549 */
1550 SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters(SUPERV_Graph theGraph, bool isReadOnly)
1551      : QDialog( QAD_Application::getDesktop(), "", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ) 
1552 {
1553   Trace("SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters");
1554   setCaption( tr( "TLT_DSGRAPHPARAMS" ) );
1555   setSizeGripEnabled( true );
1556   myGraph = theGraph;
1557
1558   QGridLayout* TopLayout = new QGridLayout( this );
1559   TopLayout->setSpacing( 6 );
1560   TopLayout->setMargin( 11 );
1561     
1562   QGroupBox* TopGroup = new QGroupBox( this, "TopGroup" );
1563   TopGroup->setColumnLayout(0, Qt::Vertical );
1564   TopGroup->layout()->setSpacing( 0 );
1565   TopGroup->layout()->setMargin( 0 );
1566   QGridLayout* TopGroupLayout = new QGridLayout( TopGroup->layout() );
1567   TopGroupLayout->setAlignment( Qt::AlignTop );
1568   TopGroupLayout->setSpacing( 6 );
1569   TopGroupLayout->setMargin( 11 );
1570
1571   // DeltaTime
1572   QLabel* DeltaTimeL = new QLabel( tr( "DELTATIME_LBL" ), TopGroup );  
1573   TopGroupLayout->addWidget( DeltaTimeL, 0, 0 );
1574   
1575   myDeltaTime = new QAD_SpinBoxDbl( TopGroup, 0.0, 1.0, 0.1);
1576   myDeltaTime->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1577   TopGroupLayout->addWidget( myDeltaTime, 0, 1 );
1578
1579   // TimeOut
1580   QLabel* TimeOutL = new QLabel( tr( "TIMEOUT_LBL" ), TopGroup); 
1581   TopGroupLayout->addWidget( TimeOutL, 1, 0 );
1582
1583   myTimeOut = new QLineEdit( TopGroup );
1584   myTimeOut->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1585   myTimeOut->setValidator( new QIntValidator(this) );
1586   myTimeOut->setMinimumSize( 100, 0 );
1587   myTimeOut->setReadOnly( isReadOnly );
1588   TopGroupLayout->addWidget( myTimeOut, 1, 1 );
1589
1590   // DataStreamTrace
1591   QLabel* DataStreamTraceL = new QLabel( tr( "DATASTREAMTRACE_LBL" ), TopGroup); 
1592   TopGroupLayout->addWidget( DataStreamTraceL, 2, 0 );
1593
1594   myDataStreamTrace = new QComboBox( TopGroup );
1595   myDataStreamTrace->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1596   myDataStreamTrace->insertItem("WithoutTrace");
1597   myDataStreamTrace->insertItem("SummaryTrace");
1598   myDataStreamTrace->insertItem("DetailedTrace");
1599   TopGroupLayout->addWidget( myDataStreamTrace, 2, 1 );
1600
1601   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
1602   GroupButtons->setColumnLayout(0, Qt::Vertical );
1603   GroupButtons->layout()->setSpacing( 0 );
1604   GroupButtons->layout()->setMargin( 0 );
1605   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
1606   GroupButtonsLayout->setAlignment( Qt::AlignTop );
1607   GroupButtonsLayout->setSpacing( 6 );
1608   GroupButtonsLayout->setMargin( 11 );
1609   
1610   QPushButton* okB     = new QPushButton( tr( "BUT_OK" ),     GroupButtons );
1611   QPushButton* cancelB = new QPushButton( tr( "BUT_CANCEL" ), GroupButtons );
1612
1613   GroupButtonsLayout->addWidget( okB, 0, 0 );
1614   GroupButtonsLayout->addItem  ( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
1615   GroupButtonsLayout->addWidget( cancelB, 0, 2 );
1616
1617   TopLayout->addWidget( TopGroup,     0, 0 );
1618   TopLayout->addWidget( GroupButtons, 1, 0 );
1619
1620   connect( okB,     SIGNAL( clicked() ), this, SLOT( accept() ) );
1621   connect( cancelB, SIGNAL( clicked() ), this, SLOT( reject() ) );
1622
1623   setData();
1624 }
1625
1626 /*!
1627   Destructor
1628 */
1629 SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters() {
1630   Trace("SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters");
1631 }
1632
1633 /*!
1634   Sets data function
1635 */
1636 void SUPERVGUI_DSGraphParameters::setData() {
1637   double aDeltaTime;
1638   long aTimeOut;
1639   SUPERV::KindOfDataStreamTrace aDataStreamTrace;
1640
1641 //  myGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1642   if (myGraph->IsStreamGraph()) {
1643     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1644     if (!SUPERV_isNull(aSGraph)) 
1645       aSGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1646   }
1647
1648   myDeltaTime->setValue(aDeltaTime);
1649   myTimeOut->setText(QString("%1").arg(aTimeOut));
1650   myDataStreamTrace->setCurrentItem((int)aDataStreamTrace);
1651 }
1652
1653 /*!
1654   <OK> button slot
1655 */
1656 void SUPERVGUI_DSGraphParameters::accept() {
1657 //   myGraph->SetStreamParams( myTimeOut->text().toLong(),
1658 //                          (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1659 //                          myDeltaTime->value());
1660   if (myGraph->IsStreamGraph()) {
1661     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1662     if (!SUPERV_isNull(aSGraph)) 
1663       aSGraph->SetStreamParams( myTimeOut->text().toLong(),
1664                                 (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1665                                 myDeltaTime->value());
1666   }
1667   QDialog::accept();
1668 }