]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_Main.cxx
Salome HOME
Stream Graph parameters dialog box
[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
52
53 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, bool fromIOR)
54      : SUPERVGraph_View(theParent), 
55        myLogged( false ),
56        myFiltered( false ),
57        myLogFileName( QString::null ),
58        myLogFile( NULL ),
59        myWarning( false ),
60        myStep( false ),
61        myTrace( false ),
62        myVerbose( false )
63 {
64   Trace("SUPERVGUI_Main::SUPERVGUI_Main (new)");
65   theParent->setViewWidget(this); 
66   if (fromIOR) {
67     //SUPERVGUI_Main* am = Supervision.getMain();
68     QAD_ObjectBrowser* ob = ((QAD_StudyFrame*)(theDesktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
69     //        if (am == 0) {
70     //  ob = ((QAD_StudyFrame*)(theDesktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
71     //} else {
72     //ob = am->objectBrowser;
73     //}; 
74     QAD_ObjectBrowserItem* item = (QAD_ObjectBrowserItem*)(ob->getListView()->currentItem());
75     SALOMEDS::SObject_var obj = theDesktop->getActiveStudy()->getStudyDocument()->FindObjectID(item->getEntry().latin1());
76     SALOMEDS::GenericAttribute_var anAttr;
77     if (obj->FindAttribute(anAttr, "AttributeIOR")) {
78       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
79       Standard_CString ior = anIOR->Value();
80       dataflow = Supervision.getEngine()->getStreamGraph(ior);
81       if (SUPERV_isNull(dataflow)) {
82         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
83         close();
84       } else {
85         init(theDesktop);
86       }
87     } else {
88       QMessageBox::warning(0, tr("ERROR"), tr("MSG_NOACCESS_BY_IOR"));
89       close();
90     }
91   } else {
92     dataflow = Supervision.getEngine()->StreamGraph(MAIN_NEW);
93     if (SUPERV_isNull(dataflow)) {
94       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_DF"));
95       close();
96     } else {
97       init(theDesktop);
98     }
99   }
100 }
101
102 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, bool isModify, const char* f)
103      : SUPERVGraph_View(theParent),
104        myLogged( false ),
105        myFiltered( false ),
106        myLogFileName( QString::null ),
107        myLogFile( NULL ),
108        myWarning( false ),
109        myStep( false ),
110        myTrace( false ),
111        myVerbose( false )
112 {
113   Trace("SUPERVGUI_Main::SUPERVGUI_Main (file)")
114     theParent->setViewWidget(this);  
115   if (isModify) {
116     dataflow = Supervision.getEngine()->StreamGraph(f);
117   } else {
118     dataflow = Supervision.getEngine()->StreamGraphE(f);
119   }
120   if (SUPERV_isNull(dataflow)) {
121     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
122     close();
123   } else {
124     init(theDesktop);
125   }
126 }
127
128 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, SUPERV_Graph cp)
129      : SUPERVGraph_View(theParent),
130        myLogged( false ),
131        myFiltered( false ),
132        myLogFileName( QString::null ),
133        myLogFile( NULL ),
134        myWarning( false ),
135        myStep( false ),
136        myTrace( false ),
137        myVerbose( false )
138 {
139   Trace("SUPERVGUI_Main::SUPERVGUI_Main (copy)");
140   theParent->setViewWidget(this);  
141   //  dataflow = cp->Copy();
142   dataflow = cp;
143   if (SUPERV_isNull(dataflow)) {
144     QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_COPY"));
145     close();
146   } else {
147     init(theDesktop);
148   }
149 }
150
151 void SUPERVGUI_Main::init(QAD_Desktop* theDesktop) {
152   Trace("SUPERVGUI_Main::init");
153   if (theDesktop) myNService = theDesktop->getNameService();
154   myHashCode = "New";
155   myCopyNum = 0;
156   choosing  = false;
157   myIsLocked = false;
158
159   myThread = new SUPERVGUI_Thread();
160   myThread->setMain(this);
161   connect(this, SIGNAL(KillMyThread(bool)), myThread, SLOT(KillThread(bool)));
162
163   myIsKilled = false;
164   //myIsRunned = false;
165   myCurrentView = GRAPH;
166   myIsFromStudy = false;
167   study     = theDesktop->getActiveStudy();
168   
169   SALOMEDS::Study_var studyDoc = study->getStudyDocument();
170   bool aLocked = studyDoc->GetProperties()->IsLocked();
171   SALOMEDS::StudyBuilder_var builder = studyDoc->NewBuilder();
172   SALOMEDS::SComponent_var father = studyDoc->FindComponent(STUDY_SUPERVISION);
173   SALOMEDS::GenericAttribute_var anAttr;
174   SALOMEDS::AttributeName_var    aName;
175   SALOMEDS::AttributePixMap_var  aPixmap;
176   if (father->_is_nil()) {
177     QAD_Operation* op = new SALOMEGUI_ImportOperation( study );
178     op->start();
179     if (aLocked) studyDoc->GetProperties()->SetLocked(false);
180     father = builder->NewComponent(STUDY_SUPERVISION);
181     anAttr = builder->FindOrCreateAttribute(father, "AttributeName");
182     aName = SALOMEDS::AttributeName::_narrow(anAttr);
183     //aName->SetValue("Supervision");
184     aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
185     
186     anAttr = builder->FindOrCreateAttribute(father, "AttributePixMap");
187     aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
188     aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
189     
190     builder->DefineComponentInstance(father, Supervision.getEngine());
191     if (aLocked) studyDoc->GetProperties()->SetLocked(true);
192     op->finish();
193   };
194
195   objectBrowser = study->getActiveStudyFrame()->getLeftFrame()->getObjectBrowser();
196     
197
198   graph = new SUPERVGUI_Graph(this);
199   array = new SUPERVGUI_Array(this);
200   
201   message = study->getActiveStudyFrame()->getRightFrame()->getMessage();
202   notification = new NOTIFICATION_Consumer();
203
204   QBoxLayout * layout = new QVBoxLayout(this);
205   layout->setMargin(0);
206   layout->setSpacing(0);
207   layout->addWidget(graph);
208   layout->addWidget(array);
209
210   sync();
211   show();
212   if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
213     myLogFile = fopen( myLogFileName.latin1(), "a" );
214     if (  myLogFile == NULL )
215       myLogged = false;
216   }
217   myTimer = new QTimer( this );
218   connect( myTimer, SIGNAL(timeout()), this, SLOT(checkExecution()) );
219 }
220
221 SUPERVGUI_Main::~SUPERVGUI_Main() {
222   Trace("SUPERVGUI_Main::~SUPERVGUI_Main");
223   if ( myLogFile != NULL) {
224     fclose( myLogFile );
225   }
226   graph->removeLinks();
227   delete notification; // kloss : nota bene : quand un datalow est detruit : verifier que les canaux de notification sont aussi detruit
228   if (!SUPERV_isNull(dataflow)) {
229     if (dataflow->IsExecuting()) {
230       if (QMessageBox::warning(QAD_Application::getDesktop(),
231                                tr("WARNING"), 
232                                tr("MSG_DF_RUNNING"), 
233                                tr("MSG_DF_EXECUTION"), 
234                                tr("MSG_DF_KILL")) == 1) {
235         if (dataflow->Kill()) {
236           if (myThread->running()) 
237             myThread->wait();
238         }
239       }
240       else {
241         emit KillMyThread(true);
242         if (myThread->running()) 
243           myThread->wait();
244       }
245     }
246     else {
247       emit KillMyThread(true);
248       if (myThread->running()) 
249         myThread->wait();
250     }
251   }
252 }
253
254 void SUPERVGUI_Main::filterNotification() {
255   Trace("SUPERVGUI_Main::filterNotification");
256   SUPERVGUI_Notification* dlg = new SUPERVGUI_Notification(this);
257   dlg->setFiltered( myFiltered );
258   dlg->setLogged( myLogged, myLogFileName );
259   dlg->setWarning( myWarning );
260   dlg->setStep( myStep );
261   dlg->setTrace( myTrace );
262   dlg->setVerbose( myVerbose );
263   if ( dlg->exec() == QDialog::Accepted ) {
264     myLogged      = dlg->getLogged();
265     myLogFileName = dlg->getLogFile();
266     myFiltered    = dlg->getFiltered();
267     myWarning     = dlg->getWarning();
268     myStep        = dlg->getStep();
269     myTrace       = dlg->getTrace();
270     myVerbose     = dlg->getVerbose();
271     delete dlg;
272     if ( myLogFile != NULL) {
273       fclose( myLogFile );
274     }
275     myLogFile = NULL;
276     if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
277       myLogFile = fopen( myLogFileName.latin1(), "a" );
278       if (  myLogFile == NULL ) {
279         myLogged = false;
280         QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr( "ERR_CANT_OPEN_LOG_FILE" ) );
281       }
282     }
283   }
284 }
285
286 void SUPERVGUI_Main::changeDSGraphParameters() {
287   SUPERVGUI_DSGraphParameters* aDlg = new SUPERVGUI_DSGraphParameters(dataflow, dataflow->IsReadOnly());
288   if (aDlg->exec() )
289     sync();
290   delete aDlg;
291 }
292
293 void SUPERVGUI_Main::syncAsync() {
294     Trace("SUPERVGUI_Main::syncAsync")
295     QTimer::singleShot(1, this, SLOT(sync()));
296 }
297
298
299 /**
300  * Called by thread when dataflow is executing
301  */
302 void SUPERVGUI_Main::execute(char *  theNodeName, SUPERV::GraphState theNodeState) {
303   
304   SUPERVGUI_Node* aNodePrs; 
305   SUPERVGUI_GraphNode* aGraphNodePrs;
306   if (myCurrentView == TABLE) {
307     aNodePrs = (SUPERVGUI_Node*) array->child(theNodeName, "SUPERVGUI_Node");
308     aGraphNodePrs = (SUPERVGUI_GraphNode*) array->child(theNodeName, "SUPERVGUI_GraphNode");
309   } else {
310     aNodePrs = (SUPERVGUI_Node*) graph->child(theNodeName, "SUPERVGUI_Node");
311     aGraphNodePrs = (SUPERVGUI_GraphNode*) graph->child(theNodeName, "SUPERVGUI_GraphNode");
312   }
313   if (aGraphNodePrs) {
314     aGraphNodePrs->sync();
315   }
316   else if (aNodePrs) {
317     //aNodePrs->sync();
318     aNodePrs->syncOnEvent(theNodeState);
319   }
320 }
321
322
323
324 void SUPERVGUI_Main::sync() {
325     Trace("SUPERVGUI_Main::sync")
326     if ((SUPERV_isNull(dataflow))) return;
327     QString t = tr("GRAPH_TITLE");
328     
329     t += dataflow->Name();
330     setCaption(t);
331
332     study->updateObjBrowser();
333     if (myCurrentView == TABLE) {
334         array->sync();
335     } else {
336         graph->sync();
337     }
338 }
339
340
341 void SUPERVGUI_Main::showTable() {
342   if (myCurrentView == TABLE) return;
343
344   if (array->create()) {
345     myCurrentView = TABLE;
346     graph->hide();
347   }
348   sync();
349 }
350
351
352 void SUPERVGUI_Main::showFullGraph() {
353   if (myCurrentView == TABLE) {
354     array->destroy();
355     graph->show();    
356   }
357   myCurrentView = GRAPH;
358   graph->sync();
359   graph->setFullView();
360 }
361
362
363 void SUPERVGUI_Main::showContolFlow() {
364   if (myCurrentView == TABLE) {
365     array->destroy();
366     graph->show();    
367   }
368   myCurrentView = CONTROLFLOW;
369   graph->sync();
370   graph->setControlView();
371 }
372
373
374 bool SUPERVGUI_Main::exportDataflow(QString theFile) {
375   Trace("SUPERVGUI_Main::exportDataflow");
376   if ((SUPERV_isNull(dataflow))) return false;
377
378   if (!theFile.isEmpty()) {
379     if (!dataflow->Export(theFile.latin1())) {
380       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING"));
381       return false;
382     }
383   }
384   return true;
385 }
386
387 void SUPERVGUI_Main::insertFile() {
388     Trace("SUPERVGUI_Main::insertFile")
389     if ((SUPERV_isNull(dataflow))) return;
390
391     QString f = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
392                                          "",
393                                          "*.xml",
394                                          tr("MSG_GRAPH_INSERT"),
395                                          true);
396     if (!f.isEmpty()) {
397         if (dataflow->Import(f.latin1())) {
398           if (myCurrentView == TABLE) {
399             array->destroy();
400             array->create();
401           }
402           sync();
403         } else {
404             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
405         };
406     };
407 }
408
409 void SUPERVGUI_Main::copy() {
410   Trace("SUPERVGUI_Main::copy");
411   if (dataflow->ThreadsMax() == 0) {
412     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOTHING_COPY"));
413     return;
414   }
415   QAD_StudyFrame* aStudyFrame = Supervision.createGraph();
416   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
417     (aStudyFrame->getRightFrame()->getViewFrame());
418   if(aViewFrame){
419     SUPERV_Graph aNewGraph = dataflow->StreamCopy();
420     QString aNewName(tr("MSG_COPY_PREFIX").arg(++myCopyNum));
421     aNewName += dataflow->Name();
422     aNewGraph->SetName(aNewName);
423     SUPERVGUI_Main* m = new SUPERVGUI_Main(aViewFrame, 
424                                            Supervision.getDesktop(), 
425                                            aNewGraph);
426     study->showFrame(aStudyFrame);
427   }
428 }
429
430 void SUPERVGUI_Main::run() {
431   Trace("SUPERVGUI_Main::run")
432     if ((SUPERV_isNull(dataflow))) return;
433   
434   if (dataflow->IsEditing()) {
435     if (!dataflow->IsValid()) {
436       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
437     } else if (!dataflow->IsExecutable()) {
438       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
439     } else if (graph->isAnyLinkCreating()) {
440       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
441     } else {
442       myRunTime = QDateTime::currentDateTime();
443       if (myIsKilled) {
444       //if (myIsRunned) {
445         if (!dataflow->ReRun()) {
446           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
447         } else {
448           myThread->startThread(tr("MSG_GRAPH_STARTED"));
449           //syncNotification();
450           sync();
451         }
452       } else {
453         if (!dataflow->Run()) {
454           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
455         } else {
456           myThread->startThread(tr("MSG_GRAPH_STARTED"));
457           //syncNotification();
458           //myIsRunned = true;
459           sync();
460         }
461       }
462     }
463   } else {
464     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
465   }
466 }
467
468
469
470 void SUPERVGUI_Main::startExecute() {
471   Trace("SUPERVGUI_Main::startExecute")
472   if ((SUPERV_isNull(dataflow))) return;
473   
474   if (dataflow->IsEditing()) {
475     if (!dataflow->IsValid()) {
476       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
477     } else if (!dataflow->IsExecutable()) {
478       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
479     } else if (graph->isAnyLinkCreating()) {
480       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
481     } else {
482       myRunTime = QDateTime::currentDateTime();
483       if (myIsKilled) {
484       //if (myIsRunned) {
485         if (!dataflow->ReStart()) {
486           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
487         } else {
488           myThread->startThread(tr("MSG_GRAPH_STARTED"));
489           //syncNotification();
490         }
491       }
492       else {
493         if (!dataflow->Start()) {
494           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
495         } else {
496           myThread->startThread(tr("MSG_GRAPH_STARTED"));
497           //syncNotification();
498         }
499       }
500     }
501   } else {
502     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
503   }
504 }
505
506
507 void SUPERVGUI_Main::kill() {
508     Trace("SUPERVGUI_Main::kill")
509     if ((SUPERV_isNull(dataflow))) return;
510
511     if (dataflow->IsEditing()) {
512       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
513     } else if (dataflow->Kill()) {
514       myIsKilled = true;
515       myThread->stopThread(tr("MSG_GRAPH_KILLED"));
516       sync();
517     } else {
518       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANTKILL_DF"));
519     }
520 }
521
522 void SUPERVGUI_Main::suspendResume() {
523     Trace("SUPERVGUI_Main::suspendResume")
524     if ((SUPERV_isNull(dataflow))) return;
525
526     if (dataflow->IsEditing()) {
527         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
528     } else if (dataflow->State() == SUPERV_Suspend) {
529       if (dataflow->Resume()) {
530         myThread->startThread(tr("MSG_DF_RESUMED"));
531       } else {
532         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESUME"));
533       }
534     } else {
535       if (dataflow->Suspend()) {
536         sync();
537         myThread->stopThread(tr("MSG_GRAPH_SUSPENDED"));
538       } else {
539         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SUSPEND"));
540       }
541     }
542 }
543
544 void SUPERVGUI_Main::stopRestart() { // kloss : a reviser et a connecter dans le popup du dataflow (pas de creation de bouton)
545     Trace("SUPERVGUI_Main::stopRestart")
546     if ((SUPERV_isNull(dataflow))) return;
547
548     if (dataflow->IsEditing()) {
549         QMessageBox::warning(0, tr("ERROR"),  tr("MSG_DF_NOTRUNNING"));
550     } else if (dataflow->State() == SUPERV_Stop) {
551         if (dataflow->ReStart()) {
552             message->setMessage(tr("MSG_DF_RESTARTED"));
553             sync();
554         } else {
555             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESTART"));
556         };
557     } else {
558         if (dataflow->Stop()) {
559             sync();
560         } else {
561             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_STOP"));
562         };
563     };
564 }
565
566 void SUPERVGUI_Main::addNode() {
567   Trace("SUPERVGUI_Main::addNode");
568   if (SUPERV_isNull(dataflow)) return;
569
570   if (dataflow->IsExecuting()) {
571     if (QMessageBox::warning(QAD_Application::getDesktop(), 
572                              tr("WARNING"), tr("MSG_GRAPH_ISRUN"),
573                              QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
574       return;        
575     } else {
576       kill();
577     }
578   }
579   Supervision.getBrowser()->choose();
580 }
581
582
583
584 /**
585  * Add Computation node
586  */
587 void SUPERVGUI_Main::addComputeNode(SUPERV_CNode theNode) {
588   //cout<<"### X="<<theNode->X()<<"  Y="<<theNode->Y()<<endl;
589   switch (myCurrentView) {
590   case GRAPH:
591     {
592       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
593       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
594       graph->addChild(aNode, theNode->X(), theNode->Y());
595       aNode->sync();
596     }
597     break;
598   case CONTROLFLOW: 
599     {
600       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
601       aNode->hideAll();
602       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
603       graph->addChild(aNode, theNode->X(), theNode->Y());
604       aNode->sync();
605     }
606     break;
607   case TABLE:
608     array->destroy();
609     array->create();
610     break;
611   }
612 }
613
614 /**
615  * Add GOTO node
616  */
617 void SUPERVGUI_Main::addGOTONode(SUPERV_CNode theNode) {
618   switch (myCurrentView) {
619   case GRAPH:
620     {
621       SUPERVGUI_Node* aNode = new SUPERVGUI_GotoNode(graph->viewport(), this, theNode);
622       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
623       graph->addChild(aNode, theNode->X(), theNode->Y());
624       aNode->sync();
625     }
626     break;
627   case CONTROLFLOW: 
628     {
629       SUPERVGUI_Node* aNode = new SUPERVGUI_GotoNode(graph->viewport(), this, theNode);
630       aNode->hideAll();
631       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
632       graph->addChild(aNode, theNode->X(), theNode->Y());
633       aNode->sync();
634     }
635     break;
636   case TABLE:
637     array->destroy();
638     array->create();
639     break;
640   }
641 }
642
643 /**
644  * Add Control node
645  */
646 void SUPERVGUI_Main::addControlNode(SUPERV_CNode theStartNode, SUPERV_CNode theEndNode, bool Update) {
647   switch (myCurrentView) {
648   case GRAPH:
649     {
650       SUPERVGUI_StartControlNode* aStartPrs = 
651         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
652       SUPERVGUI_EndControlNode* aEndPrs = 
653         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
654
655       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());            
656       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
657       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
658       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
659       if (Update) {
660         aStartPrs->updateLinksPrs();
661         aEndPrs->updateLinksPrs();
662       }
663       aStartPrs->sync();
664       aEndPrs->sync();
665       graph->repaintContents();
666     }
667     break;
668   case CONTROLFLOW: 
669     {
670       SUPERVGUI_StartControlNode* aStartPrs = 
671         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
672       SUPERVGUI_EndControlNode* aEndPrs = 
673         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
674       
675       aStartPrs->hideAll();
676       aEndPrs->hideAll();
677       
678       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());
679       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
680       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
681       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
682       if (Update) {
683         aStartPrs->updateLinksPrs();
684         aEndPrs->updateLinksPrs();
685       }
686       aStartPrs->sync();
687       aEndPrs->sync();
688       graph->repaintContents();
689     }
690     break;
691   case TABLE:
692     array->destroy();
693     array->create();
694     break;
695   }
696 }
697
698
699
700 SUPERVGUI_Graph* SUPERVGUI_Main::getGraph() {
701     Trace("SUPERVGUI_Main::getGraph")
702     return(graph);
703 }
704
705 SUPERVGUI_Array* SUPERVGUI_Main::getArray() {
706     Trace("SUPERVGUI_Main::getArray")
707     return(array);
708 }
709
710 SUPERV_Graph SUPERVGUI_Main::getDataflow() {
711     Trace("SUPERVGUI_Main::getDataflow")
712     return(dataflow);
713 }
714
715 QAD_Message* SUPERVGUI_Main::getMessage() {
716     Trace("SUPERVGUI_Main::getMessage")
717     return(message);
718 }
719
720 QAD_Study* SUPERVGUI_Main::getStudy() {
721     Trace("SUPERVGUI_Main::getStudy")
722     return(study);
723 }
724
725 bool SUPERVGUI_Main::isArrayShown() {
726     Trace("SUPERVGUI_Main::isArrayShown")
727     return(myCurrentView == TABLE);
728 }
729
730 void SUPERVGUI_Main::showPopup(QPopupMenu* p, QMouseEvent* e) {
731   Trace("SUPERVGUI_Main::showPopup");
732   // To check is Supervision active?
733   if (myIsLocked) return;
734   if (dataflow->IsExecuting()) return;
735
736   //if (QAD_Application::getDesktop()->getActiveComponent().compare(STUDY_SUPERVISION) !=0) return;
737   if (QAD_Application::getDesktop()->getActiveComponent().compare(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) ) !=0) return;
738
739   checkIsInStudy();
740   if (e->button() == RightButton) {
741     p->exec(e->globalPos());
742   }
743 }
744
745
746
747 void SUPERVGUI_Main::changeInformation() {
748   SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly());
749   if (aDlg->exec() )
750     sync();
751   delete aDlg;
752   /*    Trace("SUPERVGUI_Main::changeInformation")
753     if (Supervision.information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly())) {
754       sync();
755       }*/
756 }
757
758 // returns false, if can't add dataflow into the study
759 bool SUPERVGUI_Main::addStudy() {
760   Trace("SUPERVGUI_Main::addStudy");
761   if (myIsFromStudy) return false;
762   if ((SUPERV_isNull(dataflow))) return false;
763   
764   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
765   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
766   SALOMEDS::GenericAttribute_var anAttr;
767   SALOMEDS::AttributeName_var    aName;
768   SALOMEDS::AttributeIOR_var     anIORAttr;
769   SALOMEDS::AttributePixMap_var  aPixmap;
770   bool                           aLocked = aStudy->GetProperties()->IsLocked();
771   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
772   
773   // searching dataflow
774   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
775   if (aSO->_is_nil()) { // create new dataflow SObject
776     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
777     if (aComponent->_is_nil()) { // is supervision component not found, then create it
778       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
779       anOperation->start();
780       if (aLocked) aStudy->GetProperties()->SetLocked(false);
781       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
782       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
783       aName = SALOMEDS::AttributeName::_narrow(anAttr);
784       //aName->SetValue(STUDY_SUPERVISION);
785       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
786       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
787       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
788       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
789       aBuilder->DefineComponentInstance(aComponent, Supervision.getEngine());
790       if (aLocked) aStudy->GetProperties()->SetLocked(true);
791       anOperation->finish();
792     }
793     op->start();
794     aSO = aBuilder->NewObject(aComponent);
795     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
796     aName = SALOMEDS::AttributeName::_narrow(anAttr);
797     aName->SetValue(dataflow->Name());
798     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
799     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
800     anIORAttr->SetValue(dataflow->getIOR());
801     op->finish();
802     if (aLocked) return false;
803   }
804
805   sync();
806   Supervision.unregisterGraph(this);
807   Supervision.registerGraph(dataflow->getIOR(), this);
808   myIsFromStudy = true;
809   return true;
810 }
811
812
813 void SUPERVGUI_Main::chooseData(QListViewItem* item) {
814     Trace("SUPERVGUI_Main::chooseData")
815     if (choosing) {
816         QString id = ((QAD_ObjectBrowserItem*)item)->getEntry();
817         if (!id.isEmpty()) {
818             SALOMEDS::SObject_var object = study->getStudyDocument()->FindObjectID(id.latin1());
819             SALOMEDS::GenericAttribute_var anAttr;
820             SALOMEDS::AttributeIOR_var     anIOR;
821             Standard_CString      ior    = "";
822             if (object->FindAttribute(anAttr, "AttributeIOR")) {
823               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
824               ior = anIOR->Value();
825               portIn->setValue(ior);
826
827               // stop selection
828               choosing = false;
829               setCursor(Supervision.getCursor());
830               objectBrowser->setCursor(Supervision.getCursor());
831               Supervision.putInfo("");
832             }
833         }
834     }
835 }
836
837 void SUPERVGUI_Main::setData(SUPERVGUI_PortIn* p) {
838     Trace("SUPERVGUI_Main::setData")
839     portIn   = p;
840     choosing = true;
841     setCursor(forbiddenCursor);
842     objectBrowser->setCursor(pointingHandCursor);
843     Supervision.putInfo(tr("MSG_CHOOSE_DATA"));
844 }
845
846 SALOMEDS::SObject_var SearchOrCreateSOWithName(const SALOMEDS::Study_var theStudy,
847                                                const SALOMEDS::SObject_var theSO,
848                                                const char* theName,
849                                                //QAD_Operation* theOperation,
850                                                bool* theStarted) {
851   SALOMEDS::SObject_var aResult;
852   SALOMEDS::AttributeName_var aName;
853   SALOMEDS::GenericAttribute_var anAttr;
854   if (!*theStarted) { // optimisation
855     SALOMEDS::ChildIterator_var anIterator = theStudy->NewChildIterator(theSO);
856     for (; anIterator->More(); anIterator->Next()) {
857       if (anIterator->Value()->FindAttribute(anAttr, "AttributeName")) {
858         aName = SALOMEDS::AttributeName::_narrow(anAttr);
859         if (strcmp(aName->Value(), theName) == 0) {
860           aResult = anIterator->Value();
861           break;
862         }
863       }
864     }
865   }
866   if (!aResult->_is_nil()) return aResult;
867   // add new SObject
868   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
869   if (!*theStarted) {
870     *theStarted = true;
871     //theOperation->start();
872     aBuilder->NewCommand();
873   }
874   aResult = aBuilder->NewObject(theSO);
875   anAttr = aBuilder->FindOrCreateAttribute(aResult, "AttributeName");
876   aName = SALOMEDS::AttributeName::_narrow(anAttr);
877   aName->SetValue(theName);
878   return aResult;
879 }
880
881 bool SUPERVGUI_Main::putDataStudy(SUPERV_Port p, const char* inout) {
882   Trace("SUPERVGUI_Main::putDataStudy");
883
884   static bool isIn = false;
885   if (isIn) return true; else isIn = true;
886
887   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
888   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
889   SALOMEDS::GenericAttribute_var anAttr;
890   SALOMEDS::AttributeName_var    aName;
891   SALOMEDS::AttributeIOR_var     anIORAttr;
892   SALOMEDS::AttributePixMap_var  aPixmap;
893   bool                           aTransaction = false;
894   bool                           aLocked = aStudy->GetProperties()->IsLocked();
895   //  QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
896   
897   // searching dataflow
898   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
899   if (aSO->_is_nil()) { // create new dataflow SObject
900     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
901     if (aComponent->_is_nil()) { // is supervision component not found, then create it
902       //QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
903       //anOperation->start();
904       aBuilder->NewCommand();
905       if (aLocked) aStudy->GetProperties()->SetLocked(false);
906       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
907       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
908       aName = SALOMEDS::AttributeName::_narrow(anAttr);
909       //aName->SetValue(STUDY_SUPERVISION);
910       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
911         
912       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
913       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
914       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
915       aBuilder->DefineComponentInstance(aComponent, Supervision.getEngine());
916       if (aLocked) aStudy->GetProperties()->SetLocked(true);
917       //      anOperation->finish();
918       aBuilder->CommitCommand();
919     }
920     aTransaction = true;
921     //op->start();
922     aBuilder->NewCommand();
923     aSO = aBuilder->NewObject(aComponent);
924     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
925     aName = SALOMEDS::AttributeName::_narrow(anAttr);
926     aName->SetValue(dataflow->Name());
927     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
928     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
929     anIORAttr->SetValue(dataflow->getIOR());
930   }
931   //QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
932   aSO = SearchOrCreateSOWithName(aStudy, aSO,  // get run time SO
933                                  QString("Run ") + myRunTime.toString() /*, anOperation*/, &aTransaction);
934   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Node()->Name()/*, anOperation*/, &aTransaction); // get node SO
935   aSO = SearchOrCreateSOWithName(aStudy, aSO, inout/*, anOperation*/, &aTransaction); // get in/out SO
936   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Name()/*, anOperation*/, &aTransaction); // get port SO
937
938   if (aLocked) {
939     if (aTransaction) aBuilder->CommitCommand();
940       //op->finish();
941     isIn = false;
942     return false;
943   }
944
945   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
946   anIORAttr  = SALOMEDS::AttributeIOR::_narrow(anAttr);
947   if (!aTransaction && strcmp(anIORAttr->Value(), p->ToString()) == 0) {
948     isIn = false;
949     return true;
950   }
951   // set object value to the study: if object is external, then put it with
952   //                                 help of the specific component - owner
953   if (p->IsIOR()) {
954     // get according component driver for result object
955     SALOME_LifeCycleCORBA aLCC(myNService);
956     SUPERV_FNode aFNode = SUPERV::FNode::_narrow(p->Node());
957     if (!aFNode->_is_nil()) {
958       Engines::Component_var aComponent = aLCC.FindOrLoad_Component(aFNode->GetContainer(),
959                                                                     aFNode->GetComponentName());
960       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aComponent);
961       if (!CORBA::is_nil(aDriver)) { // if driver was found, publish object
962         CORBA::Object_ptr anObject = new CORBA::Object();
963         CORBA::Any* anAny = p->ToAny();
964         (*anAny) >>= anObject;
965         
966         if (aDriver->CanPublishInStudy(anObject)) {
967           SALOMEDS::SObject_var aTmpSO;
968           if (!aTransaction) {
969             aTmpSO = aSO;
970             aTransaction = true;
971             //op->start();
972             aBuilder->NewCommand();
973           }
974           aTmpSO = aDriver->PublishInStudy(aStudy, aTmpSO, anObject, "");
975           aBuilder->Addreference(aSO, aTmpSO);
976         } else { // can't publish object: abort transaction
977           if (aTransaction) aBuilder->AbortCommand();
978           //op->abort();
979           isIn = false;
980           return false;
981         }
982       } else { // component has no drivel, but could store IORs (like Calculator)
983         SALOMEDS::SObject_var anIORSO = aStudy->FindObjectIOR(p->ToString());
984         if (!CORBA::is_nil(anIORSO)) aBuilder->Addreference(aSO, anIORSO);
985       }
986     }
987   } else {
988     if (!aTransaction) {
989       aTransaction = true;
990       //      op->start();
991       aBuilder->NewCommand();
992     }
993     anIORAttr->SetValue(p->ToString()); // ior attribute already set for the prevoius condition
994   }
995   
996   if (aTransaction) 
997     //op->finish();
998     aBuilder->CommitCommand();
999   if (!myThread->running())
1000     study->updateObjBrowser();
1001   isIn = false;
1002   return true;
1003 }
1004
1005
1006 void SUPERVGUI_Main::ActivatePanning()
1007 {
1008   if (graph->isVisible()) {
1009     graph->ActivatePanning();
1010   } else if (array->isVisible()) {
1011     array->ActivatePanning();
1012   }
1013 }
1014
1015
1016 void SUPERVGUI_Main::ResetView()
1017 {
1018   if (graph->isVisible()) {
1019     graph->ResetView();
1020   } else if (array->isVisible()) {
1021     array->ResetView();
1022   }
1023 }
1024
1025
1026 void SUPERVGUI_Main::setAsFromStudy(bool theToStudy) {
1027   myIsFromStudy = theToStudy;
1028   graph->setAsFromStudy(theToStudy);
1029   array->setAsFromStudy(theToStudy);
1030 }
1031
1032 void SUPERVGUI_Main::checkIsInStudy() {
1033   if (!myIsFromStudy) return;
1034
1035   SALOMEDS::Study_var aStudyDoc = study->getStudyDocument();
1036   SALOMEDS::SComponent_var aFatherLbl = aStudyDoc->FindComponent(STUDY_SUPERVISION);
1037   SALOMEDS::ChildIterator_var aChildIterator = aStudyDoc->NewChildIterator(aFatherLbl);
1038   SALOMEDS::SObject_var aDataflowLbl;
1039   SALOMEDS::GenericAttribute_var anAttr;
1040
1041   for (; aChildIterator->More(); aChildIterator->Next()) {
1042     aDataflowLbl = aChildIterator->Value();
1043     if (!aDataflowLbl->FindAttribute(anAttr, "AttributeIOR"))
1044       continue;
1045
1046     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1047     if (strcmp(anIOR->Value(), dataflow->getIOR()) == 0) return;
1048   }
1049   // This Graph considered as in study but in study it is not exists
1050   Supervision.unregisterGraph(this);
1051   setAsFromStudy(false);
1052 }
1053
1054 void SUPERVGUI_Main::syncNotification() {
1055   char* graph;
1056   char* node;
1057   char* type;
1058   char* message;
1059   char* sender;
1060   long  counter;
1061   char* date;
1062   long  stamp;
1063   
1064   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1065     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1066       QString mess("");
1067       mess += "NOTIF: "; mess += graph;
1068       mess += " / "    ; mess += node;
1069       mess += " / "    ; mess += type;
1070       mess += " / "    ; mess += message;
1071       getMessage()->setMessage(mess.latin1());
1072     };
1073   };
1074 }
1075   
1076 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1077                                 char* sender, long counter, char* date, long stamp) {
1078   Trace("SUPERVGUI_Main::isFiltered");
1079   bool b = false;
1080   if (strcmp(getDataflow()->Name(), graph) == 0) {
1081     SUPERVGUI_Node* n;
1082     QObjectList* nodes = queryList("SUPERVGUI_Node");
1083     QObjectListIt i(*nodes);
1084     while ((n=(SUPERVGUI_Node*)i.current()) != 0) {
1085       ++i;
1086       if (strcmp(n->name(), node) == 0) {
1087         if (strcmp(type, NOTIF_WARNING) == 0) {
1088           b = n->isWarning();
1089         } else if (strcmp(type, NOTIF_STEP) == 0) {
1090           b = n->isStep();
1091         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1092           b = n->isTrace();
1093         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1094           b = n->isVerbose();
1095         };
1096         break;
1097       };
1098     };
1099     delete nodes;
1100     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1101       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 );
1102       fflush( myLogFile );
1103     };
1104   };
1105   return( b );
1106 }
1107
1108
1109
1110 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1111   array->setPaletteBackgroundColor(color);
1112   graph->setPaletteBackgroundColor(color);
1113   
1114   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1115 }
1116
1117 QPtrList< char * > SUPERVGUI_Main::getEventNodes() {
1118   return myEventNodes;
1119 }
1120
1121 void SUPERVGUI_Main::setEventNodes(QPtrList< char * > theEventNodes) {
1122   myEventNodes = theEventNodes;
1123 }
1124
1125 QPtrList< SUPERV::GraphState > SUPERVGUI_Main::getStates() {
1126   return myStates;
1127 }
1128
1129 void SUPERVGUI_Main::setStates(QPtrList< SUPERV::GraphState > theStates) {
1130   myStates = theStates;
1131 }
1132
1133 int SUPERVGUI_Main::getNodesNumber() {
1134   //create a list of nodes of the graph
1135   SUPERV_Nodes nodes = getDataflow()->Nodes();
1136   int RetVal = nodes->CNodes.length() + nodes->FNodes.length() +
1137                nodes->INodes.length() + nodes->GNodes.length() +
1138                nodes->LNodes.length() + nodes->SNodes.length();
1139   return RetVal;
1140 }
1141
1142 SUPERVGUI_Thread* SUPERVGUI_Main::getMyThread() {
1143   return myThread;
1144 }
1145
1146 void SUPERVGUI_Main::startTimer() {
1147   myTimer->start(500);
1148 }
1149
1150 void SUPERVGUI_Main::executionFinished() {
1151   getStudy()->updateObjBrowser();
1152 }
1153
1154 void SUPERVGUI_Main::checkExecution() {
1155   if (myThread->finished()) {
1156     myTimer->stop();
1157     executionFinished();
1158   }
1159 }
1160
1161 /******************************* SUPERVGUI_Thread class ****************************************/
1162 SUPERVGUI_Thread::SUPERVGUI_Thread()
1163      :QThread()
1164 {
1165   myIsActive = false;
1166 }
1167
1168 SUPERVGUI_Thread::~SUPERVGUI_Thread()
1169 {
1170   //it is a virtual destructor and needs to be determine here
1171 }
1172
1173 void SUPERVGUI_Thread::startThread(const char* m)
1174 {
1175   if (!myIsActive) {
1176     myIsActive = true;
1177     QThread::start();
1178     myMain->getMessage()->setMessage(m); 
1179     myMain->sync();
1180   }
1181 }
1182
1183 void SUPERVGUI_Thread::stopThread(const char* m)
1184 {
1185   myMain->getMessage()->setMessage(m);
1186 }
1187
1188 void SUPERVGUI_Thread::setMain(SUPERVGUI_Main* theMain)
1189 {
1190   myMain = theMain;
1191 }
1192
1193 void SUPERVGUI_Thread::KillThread(bool theValue)
1194 {
1195   myMutex.lock();
1196   myIsActive = !(theValue);
1197   myMutex.unlock();
1198 }
1199
1200 typedef TVoidMemFun2ArgEvent<SUPERVGUI_Main, char*, SUPERV::GraphState> TNodeSyncEvent;
1201
1202 void SUPERVGUI_Thread::run()
1203 {
1204   SUPERV_CNode aNode = NULL;
1205   SUPERV::GraphEvent aEvent = SUPERV::UndefinedEvent ;
1206   SUPERV::GraphState aState = SUPERV::UndefinedState ;
1207
1208   SUPERV_CNode aPrevNode = NULL;
1209   SUPERV::GraphEvent aPrevEvent = SUPERV::UndefinedEvent ;
1210   SUPERV::GraphState aPrevState = SUPERV::UndefinedState ;
1211
1212   char * aName;
1213   char * aPrevName;
1214
1215   QPtrList< char * > anEventNodes;
1216   QPtrList< SUPERV::GraphState > aStates;
1217
1218   myMain->startTimer();
1219   while(myIsActive) {
1220     myMain->getDataflow()->Event(aNode, aEvent, aState);
1221       
1222     if (aEvent == SUPERV::UndefinedEvent && aState == SUPERV::UndefinedState
1223         ||
1224         aEvent == SUPERV::NoEvent && aState == SUPERV::NoState
1225         ||
1226         aEvent == SUPERV::KillEvent && aState == SUPERV::KillState) {
1227       if (myMain->getEventNodes().count()) {
1228         myMain->removeEventNodes();
1229       }
1230       if (myMain->getStates().count()) {
1231         myMain->removeStates();
1232       }
1233       myIsActive = false;
1234     }
1235     else {
1236       if ( aNode != NULL) {
1237         aName = aNode->Name();
1238       }
1239
1240       if ( aPrevNode == NULL ) {  //first initialize aPrev... variables
1241         anEventNodes = myMain->getEventNodes();
1242         anEventNodes.append( &aName ) ;
1243         myMain->setEventNodes(anEventNodes);
1244         
1245         aStates = myMain->getStates();
1246         aStates.append( &aState ) ;
1247         myMain->setStates(aStates);
1248       }
1249       else {
1250         if ( aEvent == aPrevEvent && aState == aPrevState) {
1251           QString aNameStr = aName;
1252           QString aPrevNameStr = aPrevName;
1253           if ( aNameStr != aPrevNameStr ) {
1254             anEventNodes = myMain->getEventNodes();
1255             anEventNodes.append( &aName ) ;
1256             myMain->setEventNodes(anEventNodes);
1257             
1258             aStates = myMain->getStates();
1259             aStates.append( &aState ) ;
1260             myMain->setStates(aStates);
1261           }
1262         }
1263         else {
1264           anEventNodes = myMain->getEventNodes();
1265           anEventNodes.append( &aName ) ;
1266           myMain->setEventNodes(anEventNodes);
1267           
1268           aStates = myMain->getStates();
1269           aStates.append( &aState ) ;
1270           myMain->setStates(aStates);
1271         }
1272       }
1273     }
1274     if (!myIsActive) {
1275       switch (myMain->getDataflow()->State()) {
1276       case SUPERV_Editing : 
1277         stopThread(myMain->getDataflow()->IsReadOnly()? tr("MSG_GRAPH_READONLY"): tr("MSG_GRAPH_EDITING"));
1278         break;
1279         
1280       case SUPERV_Suspend : 
1281         stopThread(tr("MSG_GRAPH_SUSPENDED"));
1282         break;
1283  
1284       case SUPERV_Done : 
1285         stopThread(tr("MSG_GRAPH_FINISHED"));
1286         break;
1287         
1288       case SUPERV_Error : 
1289         stopThread(tr("MSG_GRAPH_ABORTED"));
1290         break;
1291         
1292       case SUPERV_Kill:
1293         stopThread(tr("MSG_GRAPH_KILLED"));
1294         break;
1295       }
1296
1297       break;
1298     }
1299
1300     if (myMain->getEventNodes().count()) {
1301       //if list not empty call execute() -> sync()
1302       char * aNodeName = *(myMain->getEventNodes().getFirst());
1303       SUPERV::GraphState aNodeState = *(myMain->getStates().getFirst());
1304
1305       // It is PROHIBITED to deal with widgets in a secondary thread, so event posting is used here
1306       ProcessVoidEvent( new TNodeSyncEvent( myMain, &SUPERVGUI_Main::execute, aNodeName, aNodeState ) );
1307
1308       myMain->removeFirstEN();
1309       myMain->removeFirstS();
1310     }
1311
1312     aPrevNode = aNode;
1313     aPrevEvent = aEvent;
1314     aPrevState = aState;
1315     aPrevName = aPrevNode->Name();
1316     
1317     //usleep(10);
1318     //msleep(5);
1319   }
1320   // VSR: 04/12/03 ---> update object browser ufter finishing
1321 //   qApp->lock();
1322 //   myMain->getStudy()->updateObjBrowser();
1323 //   qApp->unlock();
1324   // VSR: 04/12/03 <---
1325   QThread::exit();
1326 }
1327