Salome HOME
SMH: 3.0.0 preparation = merged version (POLYWORK + RTVDEBUG01) + adopation for new GUI
[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
30 #include "SALOMEDSClient.hxx"
31 #include "SALOMEDS_Study.hxx"
32 #include "SALOMEDS_SObject.hxx"
33 #include <boost/shared_ptr.hpp>
34 using namespace boost;
35
36 #include "NOTIFICATION.hxx"
37 #include "SALOME_Event.hxx"
38 #include "SUPERVGraph_ViewFrame.h"
39
40 #include "LogWindow.h"
41 #include "OB_Browser.h"
42 #include "OB_ListItem.h"
43 #include "SalomeApp_Application.h"
44 #include "SalomeApp_ImportOperation.h"
45 #include "SalomeApp_Study.h"
46 #include "SUIT_FileDlg.h"
47 #include "SUIT_MessageBox.h"
48 #include "SUIT_Operation.h"
49 #include "SUIT_Session.h"
50 #include "SUIT_Study.h"
51 #include "QtxDblSpinBox.h"
52
53 #include "SUPERVGUI.h"
54 #include "SUPERVGUI_Def.h"
55 #include "SUPERVGUI_Main.h"
56 #include "SUPERVGUI_Notification.h"
57 #include "SUPERVGUI_Information.h"
58 #include "SUPERVGUI_CanvasControlNode.h"
59 #include "SUPERVGUI_CanvasPort.h"
60
61 #include <qvalidator.h>
62 #include <qlabel.h>
63 #include <qlayout.h>
64 #include <qfile.h>
65 #include <qlineedit.h>
66 #include <qcombobox.h>
67
68 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, SUIT_Desktop* theDesktop, SUPERV_Graph theDataFlow )
69      : SUPERVGraph_View(theParent),
70        myLogged( false ),
71        myFiltered( false ),
72        myLogFileName( QString::null ),
73        myLogFile( NULL ),
74        myWarning( false ),
75        myStep( false ),
76        myTrace( false ),
77        myVerbose( false )
78      //myExecuted( false )
79 {
80   Trace("SUPERVGUI_Main::SUPERVGUI_Main (copy)");
81   theParent->setViewWidget(this);  
82   dataflow = theDataFlow;
83   if (SUPERV_isNull(dataflow)) {
84     QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_COPY"));
85     close();
86   } else {
87     init(theDesktop);
88   }
89 }
90
91 void SUPERVGUI_Main::init(SUIT_Desktop* theDesktop) {
92   Trace("SUPERVGUI_Main::init");
93   SUIT_Application* anActApp = SUIT_Session::session()->activeApplication();
94
95   if (theDesktop) 
96     myNService = dynamic_cast<SalomeApp_Application*>( anActApp )->namingService();
97   myHashCode = "New";
98   myCopyNum = 0;
99   choosing  = false;
100   myIsLocked = false;
101
102   myThread = new SUPERVGUI_Thread();
103   myThread->setMain(this);
104
105   myCurrentView = CANVAS;
106   myIsFromStudy = false;
107   myLastGraph = 0;
108   study = anActApp->activeStudy();
109
110   //mkr: Supervision module already connected to the 
111   //active study. So, we have to commented this code!
112   /*_PTR(Study) studyDoc = (( SalomeApp_Study* )study)->studyDS();
113   bool aLocked = studyDoc->GetProperties()->IsLocked();
114   _PTR(StudyBuilder) builder ( studyDoc->NewBuilder() );
115   _PTR(SComponent) father = studyDoc->FindComponent(STUDY_SUPERVISION);
116   if ( !father ) {
117     SUIT_Operation* op = new SalomeApp_ImportOperation( study->application() );
118     op->start();
119     if (aLocked) studyDoc->GetProperties()->SetLocked(false);
120     father = builder->NewComponent(STUDY_SUPERVISION);
121     _PTR(GenericAttribute) anAttr = builder->FindOrCreateAttribute(father, "AttributeName");
122     _PTR(AttributeName) aName ( anAttr );
123     aName->SetValue( dynamic_cast<CAM_Application*>( study->application() )->moduleTitle( "SUPERV" ).latin1() );
124     
125     anAttr = builder->FindOrCreateAttribute(father, "AttributePixMap");
126     _PTR(AttributePixMap) aPixmap ( anAttr );
127     aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
128     
129     SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
130     if ( !aSupMod ) {
131       MESSAGE("NULL Supervision module!");
132       return;
133     }
134
135     builder->DefineComponentInstance(father, SalomeApp_Application::orb()->object_to_string(aSupMod->getEngine()));
136                                              //dynamic_cast<SALOMEDS_Study*>( studyDoc )->ConvertObjectToIOR(aSupMod->getEngine()));
137     if (aLocked) studyDoc->GetProperties()->SetLocked(true);
138     op->commit();
139   };
140   */
141
142   objectBrowser = dynamic_cast<SalomeApp_Application*>( study->application() )->objectBrowser();
143     
144   myArray = new SUPERVGUI_CanvasArray(this);
145   myArrayView = new SUPERVGUI_ArrayView(myArray, this);
146
147   myCanvas = new SUPERVGUI_Canvas(this);
148   myCanvasView = new SUPERVGUI_CanvasView(myCanvas, this);
149   
150   message = dynamic_cast<SalomeApp_Application*>( study->application() )->logWindow();
151   notification = new NOTIFICATION_Consumer();
152
153   QBoxLayout * layout = new QVBoxLayout(this);
154   layout->setMargin(0);
155   layout->setSpacing(0);
156   layout->addWidget(myCanvasView);
157   layout->addWidget(myArrayView);
158
159   if (myCurrentView == CANVAS || myCurrentView == CONTROLFLOW) {
160     myCanvas->merge();
161   }
162
163   sync();
164   show();
165   if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
166     myLogFile = fopen( myLogFileName.latin1(), "a" );
167     if (  myLogFile == NULL )
168       myLogged = false;
169   }
170   myTimer = new QTimer( this );
171   connect( myTimer, SIGNAL(timeout()), this, SLOT(checkExecution()) );
172 }
173
174 SUPERVGUI_Main::~SUPERVGUI_Main() {
175   Trace("SUPERVGUI_Main::~SUPERVGUI_Main");
176
177   // close all opened SubGraphs 
178   QMap<QString, SUIT_ViewWindow*/*QAD_StudyFrame**/>::iterator it;
179   for (it = mySubGraphs.begin(); it != mySubGraphs.end(); ++it) {
180     it.data()->removeEventFilter(this);
181     it.data()->disconnect();
182     //it.data()->close();
183
184     //QAD_Study* aStudy = it.data()->getStudy();
185     //aStudy->removeStudyFrame(it.data());
186     //remove view from internal view manager's list of views
187     //it.data()->getViewManager()->removeView(it.data());
188     SUIT_ViewManager* aVM = it.data()->getViewManager();
189     STD_Application* anApp = dynamic_cast<STD_Application*>( SUIT_Session::session()->activeApplication() );
190     if ( anApp ) anApp->removeViewManager(aVM);
191     if ( aVM ) delete aVM;
192     
193   }
194   mySubGraphs.clear();
195   mySubGraphMap.clear();
196   /*
197   QAD_StudyFrame* aSubGraph;
198   for (aSubGraph = mySubGraphs.first(); aSubGraph; aSubGraph = mySubGraphs.next()) {
199     aSubGraph->removeEventFilter(this);
200     aSubGraph->close();
201   }
202   */
203
204   if ( myLogFile != NULL) {
205     fclose( myLogFile );
206   }
207   
208   //  delete notification; // kloss : nota bene : quand un datalow est detruit : verifier que les canaux de notification sont aussi detruit
209   notification->_remove_ref();  // kloss : nota bene : quand un datalow est detruit : verifier que les canaux de notification sont aussi detruit
210
211   delete myCanvas;
212   delete myArray;
213 }
214
215 void SUPERVGUI_Main::filterNotification() {
216   Trace("SUPERVGUI_Main::filterNotification");
217   SUPERVGUI_Notification* dlg = new SUPERVGUI_Notification(this);
218   dlg->setFiltered( myFiltered );
219   dlg->setLogged( myLogged, myLogFileName );
220   dlg->setWarning( myWarning );
221   dlg->setStep( myStep );
222   dlg->setTrace( myTrace );
223   dlg->setVerbose( myVerbose );
224   if ( dlg->exec() == QDialog::Accepted ) {
225     myLogged      = dlg->getLogged();
226     myLogFileName = dlg->getLogFile();
227     myFiltered    = dlg->getFiltered();
228     myWarning     = dlg->getWarning();
229     myStep        = dlg->getStep();
230     myTrace       = dlg->getTrace();
231     myVerbose     = dlg->getVerbose();
232     delete dlg;
233     if ( myLogFile != NULL) {
234       fclose( myLogFile );
235     }
236     myLogFile = NULL;
237     if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
238       myLogFile = fopen( myLogFileName.latin1(), "a" );
239       if (  myLogFile == NULL ) {
240         myLogged = false;
241         QMessageBox::warning( SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr( "ERR_CANT_OPEN_LOG_FILE" ) );
242       }
243     }
244   }
245 }
246
247 void SUPERVGUI_Main::syncAsync() {
248   Trace("SUPERVGUI_Main::syncAsync");
249   QTimer::singleShot(1, this, SLOT(sync()));
250 }
251
252
253 /**
254  * Called by thread when dataflow is executing
255  */
256 void SUPERVGUI_Main::execute( char *  theNodeName, SUPERV::GraphState theNodeState ) {
257   if (myCurrentView == CANVAS || myCurrentView == CONTROLFLOW) {
258     SUPERVGUI_CanvasNode* aNode = (SUPERVGUI_CanvasNode*) myCanvas->child(theNodeName, "SUPERVGUI_CanvasNode");
259     if ( aNode )
260       aNode->sync();
261   }
262   else if (myCurrentView == CANVASTABLE) {
263     SUPERVGUI_CanvasCellNode* aNode = (SUPERVGUI_CanvasCellNode*) myArray->child(theNodeName, "SUPERVGUI_CanvasCellNode");
264     if (aNode) 
265       aNode->sync();
266   }
267
268   // asv : 26.01.05 : Bug PAL7164 : puting out-value to study if the "put_to_Study" flag is set on a 
269   // CanvasPort the functionality was moved from SUPERVGUI_CanvasPortOut::sync() method please, see 
270   // comment in that method for details.
271   if ( theNodeState == SUPERV::DoneState ) {
272     SUPERVGUI_CanvasNode* aNode = (SUPERVGUI_CanvasNode*) myCanvas->child(theNodeName, "SUPERVGUI_CanvasNode");
273     if ( aNode ) {
274       //cout << " *** theNode " << theNodeName << " received DONE_STATE *** " << endl;
275       SUPERVGUI_CanvasPortOut* aPortOut;
276       QObjectList* aPortList = aNode->queryList("SUPERVGUI_CanvasPortOut");
277       QObjectListIt aPortIt(*aPortList);
278       while ((aPortOut=(SUPERVGUI_CanvasPortOut*)aPortIt.current()) != 0) {
279         ++aPortIt;
280         if ( aPortOut->isInStudy() && aPortOut->getEngine()->State() == SUPERV::ReadyState ) {
281           //cout << " *** " << theNodeName << "[" << aPortOut->name() << "]--> goes into study *** " << endl;
282           putDataStudy( aPortOut->getEngine(), STUDY_PORT_OUT );
283         }
284       }
285       delete aPortList;      
286     } 
287     //else //normal case if Node is not found is when node is a graph!  it can receive DoneState as well!  
288     //  MESSAGE( "ERROR in SUPERVGUI_Main::execute() : CanvasNode \"" << theNodeName << "\" NOT FOUND in myCanvas" );
289   }
290 }
291
292
293 void SUPERVGUI_Main::sync() {
294   Trace("SUPERVGUI_Main::sync");
295   if ((SUPERV_isNull(dataflow))) 
296     return;
297   QString t = tr("GRAPH_TITLE");
298   
299   t += dataflow->Name();
300   setCaption(t);
301
302   //study->updateObjBrowser();
303   //mkr: we have to update object browser only if there are some ports datas in the study
304   //or if dataflow is in the study itself. It is done in the 
305   //1) putDataStudy(...) method (it called when we put in study data of port
306   //                             or if port is in study and execution is finished)
307   //2) addDataflowToStudy() method (it is called as a slot when we add in study dataflow itself).
308   //So, we commented updateObjBrowser function here!
309   //SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
310   //if ( aSupMod ) aSupMod->updateObjBrowser();
311   //else MESSAGE("NULL Supervision module!");
312
313   if (myCurrentView == CANVASTABLE) {
314     myArray->sync();
315     myArray->update();
316   } else {
317     myCanvas->sync();
318     myCanvas->update();
319   }
320 }
321
322 void SUPERVGUI_Main::showCanvasTable() {
323   if (myCurrentView == CANVASTABLE) 
324     return;
325
326   if (myArray->create()) {
327     myCanvasView->hide();
328     myArrayView->show();
329     myCurrentView = CANVASTABLE;
330   }
331   sync();
332 }
333
334 void SUPERVGUI_Main::showContolFlow() {
335   bool merge = false;
336   if (myCurrentView == CANVASTABLE) {
337     myArrayView->hide();
338     myArray->destroy();
339     merge = true;
340   }
341   myCurrentView = CONTROLFLOW;
342   myCanvas->setControlView();
343   if (merge) {
344     myCanvas->merge();
345     myCanvasView->show();
346   }
347 }
348
349 void SUPERVGUI_Main::showCanvas() {
350   if (myCurrentView == CANVAS) 
351     return;
352
353   bool merge = false;
354   if (myCurrentView == CANVASTABLE) {
355     myArrayView->hide();
356     myArray->destroy();
357     merge = true;
358   }
359   myCurrentView = CANVAS;
360   myCanvas->setFullView();
361   if (merge) {
362     myCanvas->merge();
363     myCanvasView->show();
364   }
365 }
366
367 void SUPERVGUI_Main::insertFile() {
368   Trace("SUPERVGUI_Main::insertFile");
369
370   if ( !ReadyToModify() ) // null dataflow or executing, ..
371     return;
372
373   QString f = SUIT_FileDlg::getFileName(SUIT_Session::session()->activeApplication()->desktop(),
374                                         "",
375                                         "*.xml",
376                                         tr("MSG_GRAPH_INSERT"),
377                                         true);
378   if ( !f.isEmpty() ) { // not Cancel, but "OK" was pressed with valid file name
379       
380     Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, why here? -> see 7960
381
382     if (dataflow->Import(f.latin1())) {
383       if (myCurrentView == CANVASTABLE) {
384         myArray->destroy();
385         myArray->create();
386       }
387       else { // (myCurrentView == CANVAS || myCurrentView == CONTROLFLOW) {
388         myCanvas->merge();
389       }
390       sync();
391     } else {
392       QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
393     }
394   }
395 }
396
397 void SUPERVGUI_Main::copy() {
398   Trace("SUPERVGUI_Main::copy");
399   if (dataflow->ThreadsMax() == 0) {
400     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_NOTHING_COPY"));
401     return;
402   }
403   
404   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
405   if ( !aSupMod ) {
406     MESSAGE("NULL Supervision module!");
407     return;
408   }
409
410   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>( aSupMod->createGraph() );
411   if( aViewFrame ) {
412     SUPERV_Graph aNewGraph; //  = dataflow->StreamCopy();
413     if ( dataflow->IsStreamGraph() ) {
414       SUPERV_StreamGraph aSGraph = dataflow->ToStreamGraph();
415       if ( SUPERV_isNull(aSGraph) ) {
416         QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_CANT_COPY"));
417         return;
418       }
419       aNewGraph = aSGraph->StreamCopy();
420     }
421     else {
422       aNewGraph = dataflow->Copy();
423     }
424     QString aNewName(tr("MSG_COPY_PREFIX").arg(++myCopyNum));
425     aNewName += dataflow->Name();
426     aNewGraph->SetName(aNewName);
427     /*SUPERVGUI_Main* m = */new SUPERVGUI_Main(aViewFrame, 
428                                                aSupMod->application()->desktop(),//getDesktop(), 
429                                                aNewGraph);
430     aViewFrame->show();
431   }
432 }
433
434 void SUPERVGUI_Main::openSubGraph(SUPERV_CNode theNode, bool correct)
435 {
436   if (theNode->IsMacro()) {
437     // get SubGraph from MacroNode
438     SUPERV_Graph aMacro = SUPERV::Graph::_narrow(theNode);
439     SUPERV_Graph aGraph;
440     if (aMacro->IsStreamMacro())
441       aGraph = aMacro->StreamObjRef();
442     else
443       aGraph = aMacro->FlowObjRef();
444
445     // display SubGraph
446     if (SUPERV_isNull(aGraph)) {
447       QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("WARNING"), tr("MSG_NOACCESS"));
448       return;
449     }
450     else {
451       QString aGraphName = aGraph->Name();
452       SUIT_ViewWindow* aStudyFrame;
453       if (mySubGraphs.contains(aGraphName)) {
454         aStudyFrame = mySubGraphs[aGraphName];
455         aStudyFrame->setActiveWindow();
456         aStudyFrame->setFocus();
457       }
458       else {
459         SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
460         if ( !aSupMod ) {
461           MESSAGE("NULL Supervision module!");
462           return;
463         }
464         
465         aStudyFrame = aSupMod->createGraph();
466         if ( aStudyFrame ) {
467           SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>( aStudyFrame );
468           if( aViewFrame ) {
469             /*SUPERVGUI_Main* m = */new SUPERVGUI_Main(aViewFrame, 
470                                                        aSupMod->application()->desktop(),//getDesktop(), 
471                                                        aGraph);
472             //    connect(aStudyFrame, SIGNAL(sfStudyFrameClosing(QAD_StudyFrame*)), 
473             //            this,  SLOT(onSubGraphClosed(QAD_StudyFrame*)));
474             connect(aSupMod->application()->desktop(), SIGNAL(windowActivated( SUIT_ViewWindow* )), 
475                     this,  SLOT(onSubGraphActivated( SUIT_ViewWindow* )));
476             aStudyFrame->installEventFilter(this);
477
478             mySubGraphs.insert(aGraphName, aStudyFrame);
479             mySubGraphMap.insert(aGraphName, theNode->Name());
480
481           }
482         }
483       }
484       aStudyFrame->show();
485       if (!correct) myLastGraph = aStudyFrame;
486     }
487   }
488 }
489
490 bool SUPERVGUI_Main::eventFilter( QObject* o, QEvent* e)
491 {
492   // workaround to get close event
493   if (o->inherits("SUIT_ViewWindow"/*"QAD_StudyFrame"*/) && e->type() == QEvent::Close) {
494     SUIT_ViewWindow* aFrame = (SUIT_ViewWindow*) o;
495     onSubGraphClosed(aFrame);
496   }
497   return SUPERVGraph_View::eventFilter(o, e);
498 }
499
500 // workaround to fix PAL6255 -> opened SubGraph is not on top
501 void SUPERVGUI_Main::onSubGraphActivated(SUIT_ViewWindow* theStudyFrame)
502 {
503   if (myLastGraph) {
504     SUIT_ViewWindow* aFrame = myLastGraph;
505     myLastGraph = 0;
506     aFrame->setActiveWindow();
507     aFrame->setFocus();
508   }
509 }
510
511 void SUPERVGUI_Main::onSubGraphClosed(SUIT_ViewWindow* theStudyFrame)
512 {
513   if ( SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>( theStudyFrame ) ) {
514     theStudyFrame->removeEventFilter(this);
515     disconnect(theStudyFrame, 0, this, 0);
516
517     SUPERVGraph_View* view = supervFrame->getViewWidget();
518     SUPERVGUI_Main* aGraph = dynamic_cast<SUPERVGUI_Main*>(view);
519     if (aGraph) {
520       QString aGraphName = aGraph->getDataflow()->Name();
521       QMap<QString, QString>::iterator it = mySubGraphMap.find(aGraphName);
522       if (it != mySubGraphMap.end()) {
523         QString aNodeName = it.data();
524         SUPERVGUI_CanvasNode* aNode = (SUPERVGUI_CanvasNode*) 
525           myCanvas->child(aNodeName, "SUPERVGUI_CanvasNode");
526         if (aNode) {
527           aNode->merge();
528           myCanvas->update();
529         }
530         mySubGraphMap.remove(it);
531       }
532       mySubGraphs.remove(aGraphName);
533     }
534   }
535 }
536
537 void SUPERVGUI_Main::run( const bool andSuspend ) {
538   Trace("SUPERVGUI_Main::run");
539   if ( SUPERV_isNull(dataflow) ) 
540     return;
541   
542   if ( dataflow->IsEditing() ) { // not currently Executing
543
544     // asv 31.01.05 : fix for PAL7854, Editing moved from SUPERVGUI.cxx runDataflow(), stepByStep() to here..
545     Editing(); // remove old executor, update GUI (all nodes to "No Status")
546
547     if ( !dataflow->IsValid() ) {
548       QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
549     } 
550     else if (!dataflow->IsExecutable()) {
551       QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
552     } 
553     else if (myCanvasView->isAnyLinkCreating()) {
554       QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CRL_NOTCOMPLETE"));
555     } 
556     else {
557       myRunTime = QDateTime::currentDateTime();
558       const bool result = andSuspend ? dataflow->Start() : dataflow->Run();
559       if ( !result ) {
560         QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
561         if ( dataflow->State() == SUPERV::ErrorState ) {
562           kill();
563         }
564       } 
565       else {
566         //myExecuted = true; // set to true on first execution. for correct publishing in Study
567         myThread->startThread(tr("MSG_GRAPH_STARTED"));
568       }
569     }
570   } 
571   else {
572     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
573   }
574 }
575
576
577 void SUPERVGUI_Main::kill() {
578   Trace("SUPERVGUI_Main::kill");
579   if ((SUPERV_isNull(dataflow))) 
580     return;
581
582   if (dataflow->IsEditing()) {
583     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
584   } 
585   else if (dataflow->Kill()) {
586     getMessage()->putMessage( tr("MSG_GRAPH_KILLED") );
587     sync();
588   } 
589   else {
590     QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANTKILL_DF"));
591   }
592 }
593
594 void SUPERVGUI_Main::suspendResume() {
595     Trace("SUPERVGUI_Main::suspendResume");
596     if ((SUPERV_isNull(dataflow))) return;
597
598     if (dataflow->IsEditing()) {
599         QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
600     } else if (dataflow->State() == SUPERV_Suspend) {
601       if (dataflow->Resume()) {
602         myThread->startThread(tr("MSG_DF_RESUMED"));
603       } else {
604         QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_RESUME"));
605       }
606     } else {
607       if (dataflow->Suspend()) {
608         sync();
609         getMessage()->putMessage( tr("MSG_GRAPH_SUSPENDED") );
610       } else {
611         QMessageBox::warning(SUIT_Session::session()->activeApplication()->desktop(), tr("ERROR"), tr("MSG_CANT_SUSPEND"));
612       }
613     }
614 }
615 /* asv : 15.12.04 : commented out stopRestart() in Main and CanvasNode because it's not called from anywhere,
616    the comment from kloss below may be explaining it, but it's in French and I do not understand it..
617    It also calls deprecated method of Engine: ReStart(). 
618 void SUPERVGUI_Main::stopRestart() { // kloss : a reviser et a connecter dans le popup du dataflow (pas de creation de bouton)
619     Trace("SUPERVGUI_Main::stopRestart");
620     if ((SUPERV_isNull(dataflow))) return;
621
622     if (dataflow->IsEditing()) {
623         QMessageBox::warning(0, tr("ERROR"),  tr("MSG_DF_NOTRUNNING"));
624     } else if (dataflow->State() == SUPERV_Stop) {
625         if (dataflow->ReStart()) {
626             message->setMessage(tr("MSG_DF_RESTARTED"));
627             sync();
628         } else {
629             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESTART"));
630         };
631     } else {
632         if (dataflow->Stop()) {
633             sync();
634         } else {
635             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_STOP"));
636         };
637     };
638 }
639 */
640 void SUPERVGUI_Main::addNode() {
641   Trace("SUPERVGUI_Main::addNode");
642   
643   if ( !ReadyToModify() ) // null dataflow or executing, ..
644     return;
645   
646   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
647   if ( !aSupMod ) {
648     MESSAGE("NULL Supervision module!");
649     return;
650   }
651
652   //Editing(); // PAL6170: GUI->Engine: setting "Editing" flag, commented: PAL7960
653   aSupMod->getBrowser()->choose();
654 }
655
656 /**
657  * Add Computation node
658  */
659 void SUPERVGUI_Main::addComputeNode(SUPERV_CNode theNode) {
660   switch (myCurrentView) {
661   case CANVASTABLE:
662     myArray->destroy();
663     myArray->create();
664     break;  
665   case CANVAS:
666   case CONTROLFLOW: 
667     {
668       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasComputeNode(myCanvas, this, theNode);
669       aNode->move(theNode->X(), theNode->Y());
670       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
671       aNode->show();
672       myCanvas->update();
673     }
674     break;
675   }
676 }
677
678 /**
679  * Add GOTO node
680  */
681 void SUPERVGUI_Main::addGOTONode(SUPERV_CNode theNode) {
682   switch (myCurrentView) {
683   case CANVASTABLE:
684     myArray->destroy();
685     myArray->create();
686     break;
687   case CANVAS:
688   case CONTROLFLOW: 
689     {
690       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasGotoNode(myCanvas, this, theNode);
691       aNode->move(theNode->X(), theNode->Y());
692       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
693       aNode->show();
694       myCanvas->update();
695     }
696     break;
697   }
698 }
699
700 /**
701  * Add Control node
702  */
703 void SUPERVGUI_Main::addControlNode(SUPERV_CNode theStartNode, SUPERV_CNode theEndNode, bool Update) {
704   switch (myCurrentView) {
705   case CANVASTABLE:
706     myArray->destroy();
707     myArray->create();
708     break;
709   case CANVAS:
710   case CONTROLFLOW: 
711     {
712       SUPERVGUI_CanvasStartNode* aStartNode = new SUPERVGUI_CanvasStartNode(myCanvas, this, theStartNode);
713       aStartNode->move(theStartNode->X(), theStartNode->Y());
714       if (myCurrentView == CONTROLFLOW) aStartNode->hideAll();
715
716       SUPERVGUI_CanvasEndNode* aEndNode = new SUPERVGUI_CanvasEndNode(myCanvas, this, theEndNode, aStartNode);
717       aEndNode->move(theEndNode->X(), theEndNode->Y());
718       if (myCurrentView == CONTROLFLOW) aEndNode->hideAll();
719
720       aStartNode->show();
721       aEndNode->show();
722       myCanvas->update();
723     }
724     break;
725   }
726 }
727
728
729 /**
730  * Add Macro node
731  */
732 void SUPERVGUI_Main::addMacroNode(SUPERV_CNode theNode) {
733   switch (myCurrentView) {
734   case CANVASTABLE:
735     myArray->destroy();
736     myArray->create();
737     break;
738   case CANVAS:
739   case CONTROLFLOW: 
740     {
741       SUPERVGUI_CanvasNode* aNode = new SUPERVGUI_CanvasMacroNode(myCanvas, this, theNode);
742       aNode->move(theNode->X(), theNode->Y());
743       if (myCurrentView == CONTROLFLOW) aNode->hideAll();
744       aNode->show();
745       myCanvas->update();
746     }
747     break;
748   }
749 }
750
751 SUPERVGUI_CanvasArray*  SUPERVGUI_Main::getCanvasArray() {
752   Trace("SUPERVGUI_Main::getCanvasArray");
753   return(myArray);
754 }
755
756 SUPERVGUI_ArrayView* SUPERVGUI_Main::getArrayView() {
757   Trace("SUPERVGUI_Main::getArrayView");
758   return(myArrayView);
759 }
760
761 SUPERVGUI_Canvas* SUPERVGUI_Main::getCanvas() {
762   Trace("SUPERVGUI_Main::getCanvas");
763   return(myCanvas);
764 }
765
766 SUPERVGUI_CanvasView* SUPERVGUI_Main::getCanvasView() {
767     Trace("SUPERVGUI_Main::getCanvasView");
768     return(myCanvasView);
769 }
770
771 SUPERV_Graph SUPERVGUI_Main::getDataflow() {
772   Trace("SUPERVGUI_Main::getDataflow");
773   return(dataflow);
774 }
775
776 LogWindow* SUPERVGUI_Main::getMessage() {
777   Trace("SUPERVGUI_Main::getMessage");
778   return(message);
779 }
780
781 SUIT_Study* SUPERVGUI_Main::getStudy() {
782   Trace("SUPERVGUI_Main::getStudy");
783   return(study);
784 }
785
786 bool SUPERVGUI_Main::isArrayShown() {
787   Trace("SUPERVGUI_Main::isArrayShown");
788   return(myCurrentView == CANVASTABLE);  
789 }
790
791 void SUPERVGUI_Main::showPopup(QPopupMenu* p, QMouseEvent* e) {
792   Trace("SUPERVGUI_Main::showPopup");
793   // To check is Supervision active?
794   if (myIsLocked) return;
795   //if (dataflow->IsExecuting()) return;
796
797   CAM_Application* anApp = ( CAM_Application* )(SUIT_Session::session()->activeApplication());
798   if ( anApp->activeModule()->moduleName().compare( anApp->moduleTitle( "SUPERV" ) ) !=0 ) return;
799
800   if (e->button() == RightButton) {
801     p->exec(e->globalPos());
802   }
803 }
804
805 void SUPERVGUI_Main::changeInformation() {
806   SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly());
807   if (aDlg->exec() )
808     sync();
809   delete aDlg;
810 }
811
812 void SUPERVGUI_Main::chooseData(QListViewItem* item) {
813     Trace("SUPERVGUI_Main::chooseData");
814     if (choosing) {
815       QString id = ((OB_ListItem*)item)->text(2);// <=> get entry
816       if (!id.isEmpty()) {
817         _PTR(SObject) object ( (( SalomeApp_Study* )study)->studyDS()->FindObjectID(id.latin1()) );
818         _PTR(GenericAttribute) anAttr;
819         if (object->FindAttribute(anAttr, "AttributeIOR")) {
820           _PTR(AttributeIOR) anIOR ( anAttr );
821           
822           // asv : 13.12.04 : commented out use of portIn field, but it seems that it 
823           // should be replaced with some analogious code... selection of IOR was done -
824           // put its value into port.
825           //portIn->setValue(ior);
826           
827           SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
828           if ( !aSupMod ) {
829             MESSAGE("NULL Supervision module!");
830             return;
831           }
832
833           // stop selection
834           choosing = false;
835           setCursor(aSupMod->getCursor());
836           objectBrowser->setCursor(aSupMod->getCursor());
837           aSupMod->putInfo("");
838         }
839       }
840     }
841 }
842
843 _PTR(SObject) SearchOrCreateSOWithName( _PTR(Study) const theStudy,
844                                         _PTR(StudyBuilder) const theBuilder,
845                                         _PTR(SObject) const theSO,
846                                         const char* theName,
847                                         bool* theDoneSomething ) {
848   _PTR(SObject) aResult;
849   _PTR(AttributeName) aName;
850   _PTR(GenericAttribute) anAttr;
851   _PTR(ChildIterator) anIterator ( theStudy->NewChildIterator(theSO) );
852   for (; anIterator->More(); anIterator->Next()) {
853     if (anIterator->Value()->FindAttribute(anAttr, "AttributeName")) {
854       aName = anAttr;
855       if (strcmp(aName->Value().c_str(), theName) == 0) {
856         aResult = anIterator->Value();
857         break;
858       }
859     }
860   }
861
862   // if aResule was found then theDoneSomething=false and we return
863   *theDoneSomething = !aResult ? 1 : 0;//->_is_nil();
864   if ( !*theDoneSomething )
865     return aResult;
866
867   // add new SObject
868   aResult = theBuilder->NewObject( theSO );
869   anAttr = theBuilder->FindOrCreateAttribute(aResult, "AttributeName");
870   aName = anAttr;
871   aName->SetValue(theName);
872   return aResult;
873 }
874     
875 /**
876  * Return true if dataflow is already in the study
877  */
878 bool SUPERVGUI_Main::isDataflowInStudy() const {
879   _PTR(Study) aStudy = (( SalomeApp_Study* )study)->studyDS();
880   _PTR(SObject) aSO ( aStudy->FindObjectIOR( dataflow->getIOR() ) );
881   return ( aSO ? true : false );
882 }
883
884 /**
885  * Create a "Supervision" object in the Study and a "dataflow" object under it
886  * aDoneSomething will be true if "Supervision" or "dataflow" object were created (not found).
887  */
888 _PTR(SObject) createDataflowSObj( SUIT_Study* study, 
889                                   SUPERV::Graph_var dataflow, 
890                                   _PTR(StudyBuilder) aBuilder,
891                                   bool& aDoneSomething ) {
892   _PTR(Study)            aStudy = (( SalomeApp_Study* )study)->studyDS();
893   _PTR(GenericAttribute) anAttr;
894   _PTR(AttributeName)    aName;
895
896   // Find or create "Supervisor -> aNewDataflow_1" SObjects in the study
897   _PTR(SObject) aSO = aStudy->FindObjectIOR( dataflow->getIOR() );
898   if ( !aSO ) { // dataflow SObject not found in the study
899     aDoneSomething = true; 
900     _PTR(SComponent) aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
901     if ( aComponent ) { // if component found, create name and icon for it to display it in object browser
902       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
903       aName = anAttr;
904       aName->SetValue( (( CAM_Application* )(study->application()))->moduleTitle( "SUPERV" ).latin1() );
905         
906       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
907       _PTR(AttributePixMap) aPixmap ( anAttr );
908       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
909
910       SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
911       if ( aSupMod )
912         aBuilder->DefineComponentInstance(aComponent, SalomeApp_Application::orb()->object_to_string(aSupMod->getEngine()));
913                                                     //dynamic_cast<SALOMEDS_Study*>( aStudy )->ConvertObjectToIOR(aSupMod->getEngine()));
914       else MESSAGE("NULL Supervision module!");
915       
916     }
917     // create dataflow SObject ("aNewDataflow_1") 
918     aSO = aBuilder->NewObject(aComponent);
919     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
920     aName = anAttr;
921     aName->SetValue(dataflow->Name());
922     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
923     _PTR(AttributeIOR) anIORAttr ( anAttr );
924     anIORAttr->SetValue(dataflow->getIOR());
925   }
926
927   return aSO;
928 }
929
930 void SUPERVGUI_Main::addDataflowToStudy() {
931   bool isCreated;
932   _PTR(StudyBuilder) aBuilder ( (( SalomeApp_Study* )study)->studyDS()->NewBuilder() );
933   aBuilder->NewCommand();
934   _PTR(SObject) aDF ( createDataflowSObj( study, dataflow, aBuilder, isCreated ) );
935   if ( aDF ) {
936     aBuilder->CommitCommand();
937     // what is this register/unregister?? don't know..
938     SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
939     if ( !aSupMod ) {
940       MESSAGE("NULL Supervision module!");
941       return;
942     }
943     aSupMod->unregisterGraph(this);
944     aSupMod->registerGraph(dataflow->getIOR(), this);
945
946     if ( !myThread->running() )
947       aSupMod->updateObjBrowser();
948   }
949   else {
950     MESSAGE( "ERROR: failed to find or create dataflow SObject" );
951     aBuilder->AbortCommand();
952   }
953 }
954
955 bool SUPERVGUI_Main::putDataStudy( SUPERV_Port p, const char* inout ) {
956   Trace("SUPERVGUI_Main::putDataStudy");
957
958   // static variable to ensure that only one instance (thread) is executing this function 
959   static bool isIn = false;
960   if (isIn)   return true; 
961   else        isIn = true;
962
963   _PTR(Study)        aStudy = (( SalomeApp_Study* )study)->studyDS();
964   const bool         aLocked = aStudy->GetProperties()->IsLocked();
965   _PTR(StudyBuilder) aBuilder ( aStudy->NewBuilder() );
966   bool                           aDoneSomething = false;
967
968   // asv 23.11.04 : fix for PAL6852 if the study is locked -- then we can't put anything in it.
969   if ( aLocked ) {
970     MESSAGE( "The study is locked and can not be modified!" );
971     isIn = false;
972     return false;
973   }
974
975   // check if the port and its node are good
976   if ( CORBA::is_nil( p ) || CORBA::is_nil( p->Node() ) ) {
977     MESSAGE( "putInStudy ERROR: port or node are NULL!" );
978     isIn = false;
979     return false;
980   }
981
982   // open new command.  Commit or Abort it depending on aDoneSomething variable or error
983   aBuilder->NewCommand();
984   
985   // Find or create "Supervisor -> aNewDataflow_1" SObjects in the study
986   _PTR(SObject) aSO = createDataflowSObj( study, dataflow, aBuilder, aDoneSomething );
987
988   if ( !aSO ) {
989     MESSAGE( "ERROR: putDataStudy() could not find or create dataflow SObject" ); 
990     aBuilder->AbortCommand();
991     isIn = false;
992     return false;
993   }
994
995   // Create "Run -> Time and date, etc." SObjects
996   if  ( p->State() == SUPERV::ReadyState ) { // if port contains valid computed value
997
998     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, QString("Run ") + myRunTime.toString(), &aDoneSomething ); // get run time SO
999     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, p->Node()->Name(), &aDoneSomething ); // get node SO
1000     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, inout, &aDoneSomething ); // get in/out SO
1001     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, p->Name(), &aDoneSomething ); // get port SO
1002
1003     // create IOR attribute for port SObject (usually with "return" name)
1004     _PTR(GenericAttribute) anAttr ( aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR") );
1005     _PTR(AttributeIOR) anIORAttr  ( anAttr );
1006
1007     // if we have not created anything (all SObject already existed) and we have the same IORAttribute
1008     // on the needed SObject -> then don't do anything! it's already there!
1009     if ( !aDoneSomething && strcmp(anIORAttr->Value().c_str(), p->ToString()) == 0 ) {
1010       aBuilder->AbortCommand();
1011       isIn = false;
1012       return true;
1013     }
1014
1015     aDoneSomething = true; // going to set some value to anIORAttr any way from this point..
1016
1017     // set object value to the study: if object is external, then put it with
1018     //  help of the specific component - owner
1019     if ( p->IsIOR() ) {
1020       // get according component driver for result object
1021       SALOME_LifeCycleCORBA aLCC( myNService );
1022       SUPERV_FNode aFNode = SUPERV::FNode::_narrow( p->Node() );
1023       if ( !aFNode->_is_nil() ) {
1024         Engines::Component_var aComponent = aLCC.FindOrLoad_Component( aFNode->GetContainer(), aFNode->GetComponentName() );
1025         SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow( aComponent );
1026         if ( !CORBA::is_nil( aDriver ) ) { // if driver was found, publish object
1027           CORBA::Object_ptr anObject = new CORBA::Object();
1028           CORBA::Any* anAny = p->ToAny();
1029           (*anAny) >>= anObject;
1030         
1031           if ( aDriver->CanPublishInStudy( anObject ) ) {
1032             SALOMEDS::SObject_var aTmpSO;// = aSO;
1033             SALOMEDS_Study* aSStudy = dynamic_cast<SALOMEDS_Study*>( aStudy.get() );
1034             if ( !aSStudy ) return false;
1035             aTmpSO = aDriver->PublishInStudy( aSStudy->GetStudy(), aTmpSO, anObject, "" );
1036             aBuilder->Addreference(aSO, _PTR(SObject)(new SALOMEDS_SObject( aTmpSO )) );
1037           } 
1038           else { // can't publish object: abort transaction
1039             MESSAGE( "CanPublishInStudy() returned FALSE.  ok, AbortCommand.." );
1040             aBuilder->AbortCommand();
1041             isIn = false;
1042             return false;
1043           }
1044         }
1045         else { // component has no driver, but could store IORs (like Calculator)
1046           _PTR(SObject) anIORSO ( aStudy->FindObjectIOR( p->ToString() ) );
1047           if ( anIORSO )
1048             aBuilder->Addreference(aSO, anIORSO);
1049           else { // Hm... the object (==port value) was not found, so we don't publish it.
1050             MESSAGE( "The object (==port value) was not found, so we don't publish it" );
1051             aBuilder->AbortCommand();
1052             isIn = false;
1053             return false;
1054           }
1055         }
1056       }
1057       else { // FNode is NULL -> bad
1058         MESSAGE( "FNode is NULL.  Not good at all.  Aborting command." );
1059         aBuilder->AbortCommand();
1060         isIn = false;
1061         return false;
1062       }
1063     } 
1064     else {
1065       anIORAttr->SetValue( p->ToString() ); // ior attribute already set for the prevoius condition
1066     }
1067   }
1068
1069   if ( aDoneSomething )
1070     aBuilder->CommitCommand();
1071   else
1072     aBuilder->AbortCommand();
1073   
1074   if ( !myThread->running() ) {
1075     SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
1076     if ( aSupMod ) 
1077       aSupMod->updateObjBrowser();
1078     else MESSAGE("NULL Supervision module!");
1079   }
1080
1081   isIn = false;
1082   return true;
1083 }
1084
1085
1086 void SUPERVGUI_Main::ActivatePanning()
1087 {
1088   if (myCanvasView->isVisible()) {
1089     myCanvasView->ActivatePanning();
1090   } else if (myArrayView->isVisible()) {
1091     myArrayView->ActivatePanning();
1092   }
1093 }
1094
1095
1096 void SUPERVGUI_Main::ResetView()
1097 {
1098   if (myCanvasView->isVisible()) {
1099     myCanvasView->ResetView();
1100   } else if (myArrayView->isVisible()) {
1101     myArrayView->ResetView();
1102   }
1103 }
1104
1105 void SUPERVGUI_Main::syncNotification() {
1106   char* graph;
1107   char* node;
1108   char* type;
1109   char* message;
1110   char* sender;
1111   long  counter;
1112   char* date;
1113   long  stamp;
1114   
1115   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1116     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1117       QString mess("");
1118       mess += "NOTIF: "; mess += graph;
1119       mess += " / "    ; mess += node;
1120       mess += " / "    ; mess += type;
1121       mess += " / "    ; mess += message;
1122       getMessage()->putMessage(mess.latin1());
1123     };
1124   };
1125 }
1126   
1127 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1128                                 char* sender, long counter, char* date, long stamp) {
1129   Trace("SUPERVGUI_Main::isFiltered");
1130   bool b = false;
1131   if (strcmp(getDataflow()->Name(), graph) == 0) {
1132     SUPERVGUI_CanvasNode* n;
1133     QObjectList* nodes = queryList("SUPERVGUI_CanvasNode");
1134     QObjectListIt i(*nodes);
1135     while ((n=(SUPERVGUI_CanvasNode*)i.current()) != 0) {
1136       ++i;
1137       if (strcmp(n->name(), node) == 0) {
1138         if (strcmp(type, NOTIF_WARNING) == 0) {
1139           b = n->isWarning();
1140         } else if (strcmp(type, NOTIF_STEP) == 0) {
1141           b = n->isStep();
1142         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1143           b = n->isTrace();
1144         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1145           b = n->isVerbose();
1146         };
1147         break;
1148       };
1149     };
1150     delete nodes;
1151     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1152       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 );
1153       fflush( myLogFile );
1154     };
1155   };
1156   return( b );
1157 }
1158
1159 void SUPERVGUI_Main::closeEvent(QCloseEvent* e) {
1160   e->accept();
1161 }
1162
1163
1164 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1165
1166   myCanvas->setBackgroundColor(color);
1167   myCanvasView->setPaletteBackgroundColor(color.light());
1168   myArray->setBackgroundColor(color);
1169   myArrayView->setPaletteBackgroundColor(color.light());
1170   
1171   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1172 }
1173
1174 QPtrList< char * > SUPERVGUI_Main::getEventNodes() {
1175   return myEventNodes;
1176 }
1177
1178 void SUPERVGUI_Main::setEventNodes(QPtrList< char * > theEventNodes) {
1179   myEventNodes = theEventNodes;
1180 }
1181
1182 QPtrList< SUPERV::GraphState > SUPERVGUI_Main::getStates() {
1183   return myStates;
1184 }
1185
1186 void SUPERVGUI_Main::setStates(QPtrList< SUPERV::GraphState > theStates) {
1187   myStates = theStates;
1188 }
1189
1190 int SUPERVGUI_Main::getNodesNumber() {
1191   //create a list of nodes of the graph
1192   SUPERV_Nodes nodes = getDataflow()->Nodes();
1193   int RetVal = nodes->CNodes.length() + nodes->FNodes.length() +
1194                nodes->INodes.length() + nodes->GNodes.length() +
1195                nodes->LNodes.length() + nodes->SNodes.length();
1196   return RetVal;
1197 }
1198
1199 SUPERVGUI_Thread* SUPERVGUI_Main::getMyThread() {
1200   return myThread;
1201 }
1202
1203 void SUPERVGUI_Main::startTimer() {
1204   myTimer->start(500);
1205 }
1206
1207 void SUPERVGUI_Main::executionFinished() {
1208   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
1209   if ( aSupMod ) 
1210     aSupMod->updateObjBrowser();
1211   else MESSAGE("NULL Supervision module!");
1212   
1213   myCanvas->update();
1214   myArray->update();
1215 }
1216
1217 void SUPERVGUI_Main::checkExecution() {
1218   if (myThread->finished()) {
1219     myTimer->stop();
1220     executionFinished();
1221   }
1222 }
1223
1224 /**
1225  * Editing() is to be called by any operation in GUI before modification of a datamodel
1226  * (add/remove ports or nodes, etc.).  It is used to resolve inconsistancies between 2 data models
1227  * in Engine: Editor and Executor.  During and after execution, the values of ports and statuses of nodes
1228  * are taken from Executor data model.  But when user starts editing the graph - these changes must
1229  * be applied to Editor data model.  This function destroys Executor data model and moves to Editor.   
1230  */ 
1231 void SUPERVGUI_Main::Editing() {
1232   if ( !SUPERV_isNull( dataflow ) )
1233     dataflow->Editing();
1234   
1235   // updata GUI, Nodes' statuses and Ports' values could change.
1236   sync();
1237 }
1238
1239 void SUPERVGUI_Main::removeArrayChild(SUPERV::CNode_ptr theNode)
1240 {
1241   // mkr: since the deletion of the node allow only in CANVAS view,
1242   // it is necessary to remove the CanvasArray's children, which
1243   // have the same CNode engine as deleting node. This CNode is given
1244   // as argument
1245   if (myArray) {
1246     const QObjectList* aChList = myArray->children();
1247     if ( aChList ) { // asv 27.01.05 : fix for 7817
1248       QObjectListIt aItChList(*aChList);
1249       SUPERVGUI_CanvasNode* anObjNode;
1250       while ((anObjNode = (SUPERVGUI_CanvasNode*)aItChList.current()) != 0) {
1251         ++aItChList;
1252         if ((QString(anObjNode->getEngine()->Name())).compare(QString(theNode->Name())) == 0) {
1253           myArray->removeChild(anObjNode);
1254           delete anObjNode;
1255         }
1256       }
1257     } // end of if ( ChList )
1258   }
1259 }
1260
1261 /**
1262  * ReadyToModify() must be called before any modification
1263  * operation - asks to kill execution of dataflow.  If returns false - 
1264  * modification (==Editing() ) is not allowed.
1265  */
1266 bool SUPERVGUI_Main::ReadyToModify() {
1267   if ( CORBA::is_nil( dataflow ) )
1268     return false;
1269   if ( dataflow->IsExecuting() ) {
1270     bool quit = SUIT_MessageBox::warn2( SUIT_Session::session()->activeApplication()->desktop(), // 0=Yes, 1=No
1271       tr("WARNING"), tr("MSG_GRAPH_ISRUN"), tr( "BUT_YES" ), tr( "BUT_CANCEL" ), 0, 1, 0 );
1272     if ( quit ) // user selected NOT to kill dataflow and NOT to add new node
1273       return false;
1274     else  if ( dataflow->IsExecuting() ) // user selected to kill the dataflow and add new node after that
1275       kill();   // checking again for IsExecuting to be sure that it was not finished while MB was up
1276   }
1277   return true;
1278 }
1279
1280 /******************************* SUPERVGUI_Thread class ****************************************/
1281 SUPERVGUI_Thread::SUPERVGUI_Thread()
1282      :QThread()
1283 {
1284   myIsActive = false;
1285 }
1286
1287 SUPERVGUI_Thread::~SUPERVGUI_Thread()
1288 {
1289   //it is a virtual destructor and needs to be determine here
1290 }
1291
1292 void SUPERVGUI_Thread::startThread(const char* m)
1293 {
1294   if (!myIsActive) {
1295     myIsActive = true;
1296     
1297     myMain->getMessage()->putMessage(m); 
1298     myMain->sync();
1299
1300     start();
1301   }
1302 }
1303
1304 void SUPERVGUI_Thread::setMain( SUPERVGUI_Main* theMain )
1305 {
1306   myMain = theMain;
1307 }
1308
1309 void SUPERVGUI_Thread::KillThread( bool theValue )
1310 {
1311   myMutex.lock();
1312   myIsActive = !(theValue);
1313   myMutex.unlock();
1314 }
1315
1316 typedef TVoidMemFun2ArgEvent<SUPERVGUI_Main, char*, SUPERV::GraphState> TNodeSyncEvent;
1317
1318 void SUPERVGUI_Thread::run()
1319 {
1320   myMain->startTimer();
1321
1322   // GUI cycle to handle events coming for Engine
1323   while ( myIsActive ) {
1324
1325     SUPERV_CNode aNode = NULL;
1326     SUPERV::GraphEvent aEvent = SUPERV::UndefinedEvent ;
1327     SUPERV::GraphState aState = SUPERV::UndefinedState ;
1328
1329     // blocking function of Engine.  Return from there only after anEvent happens on node aNode
1330     myMain->getDataflow()->Event(aNode, aEvent, aState);
1331     
1332     // "kill" or undefined event came
1333     if (( aEvent == SUPERV::UndefinedEvent && aState == SUPERV::UndefinedState ) ||
1334         ( aEvent == SUPERV::NoEvent && aState == SUPERV::NoState ) ||
1335         ( aEvent == SUPERV::KillEvent && aState == SUPERV::KillState )) {
1336
1337       myIsActive = false;
1338     }
1339     else { // a "normal" execution event came
1340       char* aName = NULL;
1341       if ( aNode != NULL && !CORBA::is_nil( aNode ) ) 
1342         aName = aNode->Name();      
1343
1344       // this function is asynchronious.  The call does NOT wait when SUPERVGUI_Main::execute finishes
1345       // handling the event.  So: SUPERVGUI_Main::execute must be fast, in order we don't get here again
1346       // on the next loop iteration, BEFORE  previous SUPERVGUI_Main::execute finished.
1347       ProcessVoidEvent( new TNodeSyncEvent( myMain, &SUPERVGUI_Main::execute, aName, aState ) );
1348     }    
1349   
1350     // execution is finished.  just set a "finished" message(s)
1351     if ( !myIsActive ) {
1352       switch ( myMain->getDataflow()->State() ) {
1353       case SUPERV_Editing :     
1354         myMain->getMessage()->putMessage( myMain->getDataflow()->IsReadOnly()? 
1355                                          tr("MSG_GRAPH_READONLY"): tr("MSG_GRAPH_EDITING") );
1356         break;  
1357       case SUPERV_Suspend : 
1358         myMain->getMessage()->putMessage( tr("MSG_GRAPH_SUSPENDED") );
1359         break; 
1360       case SUPERV_Done : 
1361         myMain->getMessage()->putMessage( tr("MSG_GRAPH_FINISHED") );
1362         break;  
1363       case SUPERV_Error :
1364         myMain->getMessage()->putMessage( tr("MSG_GRAPH_ABORTED") );
1365         break;  
1366       case SUPERV_Kill:
1367         myMain->getMessage()->putMessage( tr("MSG_GRAPH_KILLED") );
1368         break;
1369       } // end of switch
1370
1371       // asv 03.02.05 : fix for PAL6859, not very good, but works..
1372       myMain->sync();
1373     } // end of if !myIsActive
1374   } // end of while( myIsActive )
1375
1376   QThread::exit();
1377 }
1378
1379 /******************************* SUPERVGUI_DSGraphParameters class ****************************************/
1380 /*!
1381   Constructor
1382 */
1383 SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters(SUPERV_Graph theGraph, bool isReadOnly)
1384      : QDialog( SUIT_Session::session()->activeApplication()->desktop(), "", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ) 
1385 {
1386   Trace("SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters");
1387   setCaption( tr( "TLT_DSGRAPHPARAMS" ) );
1388   setSizeGripEnabled( true );
1389   myGraph = theGraph;
1390
1391   QGridLayout* TopLayout = new QGridLayout( this );
1392   TopLayout->setSpacing( 6 );
1393   TopLayout->setMargin( 11 );
1394     
1395   QGroupBox* TopGroup = new QGroupBox( this, "TopGroup" );
1396   TopGroup->setColumnLayout(0, Qt::Vertical );
1397   TopGroup->layout()->setSpacing( 0 );
1398   TopGroup->layout()->setMargin( 0 );
1399   QGridLayout* TopGroupLayout = new QGridLayout( TopGroup->layout() );
1400   TopGroupLayout->setAlignment( Qt::AlignTop );
1401   TopGroupLayout->setSpacing( 6 );
1402   TopGroupLayout->setMargin( 11 );
1403
1404   // DeltaTime
1405   QLabel* DeltaTimeL = new QLabel( tr( "DELTATIME_LBL" ), TopGroup );  
1406   TopGroupLayout->addWidget( DeltaTimeL, 0, 0 );
1407   
1408   myDeltaTime = new QtxDblSpinBox( 0.0, 1.0, 0.1, TopGroup );
1409   myDeltaTime->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1410   TopGroupLayout->addWidget( myDeltaTime, 0, 1 );
1411
1412   // TimeOut
1413   QLabel* TimeOutL = new QLabel( tr( "TIMEOUT_LBL" ), TopGroup); 
1414   TopGroupLayout->addWidget( TimeOutL, 1, 0 );
1415
1416   myTimeOut = new QLineEdit( TopGroup );
1417   myTimeOut->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1418   myTimeOut->setValidator( new QIntValidator(this) );
1419   myTimeOut->setMinimumSize( 100, 0 );
1420   myTimeOut->setReadOnly( isReadOnly );
1421   TopGroupLayout->addWidget( myTimeOut, 1, 1 );
1422
1423   // DataStreamTrace
1424   QLabel* DataStreamTraceL = new QLabel( tr( "DATASTREAMTRACE_LBL" ), TopGroup); 
1425   TopGroupLayout->addWidget( DataStreamTraceL, 2, 0 );
1426
1427   myDataStreamTrace = new QComboBox( TopGroup );
1428   myDataStreamTrace->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1429   myDataStreamTrace->insertItem("WithoutTrace");
1430   myDataStreamTrace->insertItem("SummaryTrace");
1431   myDataStreamTrace->insertItem("DetailedTrace");
1432   TopGroupLayout->addWidget( myDataStreamTrace, 2, 1 );
1433
1434   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
1435   GroupButtons->setColumnLayout(0, Qt::Vertical );
1436   GroupButtons->layout()->setSpacing( 0 );
1437   GroupButtons->layout()->setMargin( 0 );
1438   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
1439   GroupButtonsLayout->setAlignment( Qt::AlignTop );
1440   GroupButtonsLayout->setSpacing( 6 );
1441   GroupButtonsLayout->setMargin( 11 );
1442   
1443   QPushButton* okB     = new QPushButton( tr( "BUT_OK" ),     GroupButtons );
1444   QPushButton* cancelB = new QPushButton( tr( "BUT_CANCEL" ), GroupButtons );
1445
1446   GroupButtonsLayout->addWidget( okB, 0, 0 );
1447   GroupButtonsLayout->addItem  ( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
1448   GroupButtonsLayout->addWidget( cancelB, 0, 2 );
1449
1450   TopLayout->addWidget( TopGroup,     0, 0 );
1451   TopLayout->addWidget( GroupButtons, 1, 0 );
1452
1453   connect( okB,     SIGNAL( clicked() ), this, SLOT( accept() ) );
1454   connect( cancelB, SIGNAL( clicked() ), this, SLOT( reject() ) );
1455
1456   setData();
1457 }
1458
1459 /*!
1460   Destructor
1461 */
1462 SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters() {
1463   Trace("SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters");
1464 }
1465
1466 /*!
1467   Sets data function
1468 */
1469 void SUPERVGUI_DSGraphParameters::setData() {
1470   double aDeltaTime;
1471   long aTimeOut;
1472   SUPERV::KindOfDataStreamTrace aDataStreamTrace;
1473
1474 //  myGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1475   if (myGraph->IsStreamGraph()) {
1476     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1477     if (!SUPERV_isNull(aSGraph)) 
1478       aSGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1479   }
1480
1481   myDeltaTime->setValue(aDeltaTime);
1482   myTimeOut->setText(QString("%1").arg(aTimeOut));
1483   myDataStreamTrace->setCurrentItem((int)aDataStreamTrace);
1484 }
1485
1486 /*!
1487   <OK> button slot
1488 */
1489 void SUPERVGUI_DSGraphParameters::accept() {
1490 //   myGraph->SetStreamParams( myTimeOut->text().toLong(),
1491 //                          (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1492 //                          myDeltaTime->value());
1493   if (myGraph->IsStreamGraph()) {
1494     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1495     if (!SUPERV_isNull(aSGraph)) 
1496       aSGraph->SetStreamParams( myTimeOut->text().toLong(),
1497                                 (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1498                                 myDeltaTime->value());
1499   }
1500   QDialog::accept();
1501 }