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