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