Salome HOME
Fix PAL8912 in SUIT-based version of SUPERVISOR.
[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   // static variable to ensure that only one instance (thread) is executing this function 
967   static bool isIn = false;
968   if (isIn)   return true; 
969   else        isIn = true;
970
971   _PTR(Study)        aStudy = (( SalomeApp_Study* )study)->studyDS();
972   const bool         aLocked = aStudy->GetProperties()->IsLocked();
973   _PTR(StudyBuilder) aBuilder ( aStudy->NewBuilder() );
974   bool                           aDoneSomething = false;
975
976   // asv 23.11.04 : fix for PAL6852 if the study is locked -- then we can't put anything in it.
977   if ( aLocked ) {
978     MESSAGE( "The study is locked and can not be modified!" );
979     isIn = false;
980     return false;
981   }
982
983   // check if the port and its node are good
984   if ( CORBA::is_nil( p ) || CORBA::is_nil( p->Node() ) ) {
985     MESSAGE( "putInStudy ERROR: port or node are NULL!" );
986     isIn = false;
987     return false;
988   }
989
990   // open new command.  Commit or Abort it depending on aDoneSomething variable or error
991   aBuilder->NewCommand();
992   
993   // Find or create "Supervisor -> aNewDataflow_1" SObjects in the study
994   _PTR(SObject) aSO = createDataflowSObj( study, dataflow, aBuilder, aDoneSomething );
995
996   if ( !aSO ) {
997     MESSAGE( "ERROR: putDataStudy() could not find or create dataflow SObject" ); 
998     aBuilder->AbortCommand();
999     isIn = false;
1000     return false;
1001   }
1002
1003   // Create "Run -> Time and date, etc." SObjects
1004   if  ( p->State() == SUPERV::ReadyState ) { // if port contains valid computed value
1005
1006     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, QString("Run ") + myRunTime.toString(), &aDoneSomething ); // get run time SO
1007     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, p->Node()->Name(), &aDoneSomething ); // get node SO
1008     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, inout, &aDoneSomething ); // get in/out SO
1009     aSO = SearchOrCreateSOWithName( aStudy, aBuilder, aSO, p->Name(), &aDoneSomething ); // get port SO
1010
1011     // create IOR attribute for port SObject (usually with "return" name)
1012     _PTR(GenericAttribute) anAttr ( aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR") );
1013     _PTR(AttributeIOR) anIORAttr  ( anAttr );
1014
1015     // if we have not created anything (all SObject already existed) and we have the same IORAttribute
1016     // on the needed SObject -> then don't do anything! it's already there!
1017     if ( !aDoneSomething && strcmp(anIORAttr->Value().c_str(), p->ToString()) == 0 ) {
1018       aBuilder->AbortCommand();
1019       isIn = false;
1020       return true;
1021     }
1022
1023     aDoneSomething = true; // going to set some value to anIORAttr any way from this point..
1024
1025     // set object value to the study: if object is external, then put it with
1026     //  help of the specific component - owner
1027     if ( p->IsIOR() ) {
1028       // get according component driver for result object
1029       SALOME_LifeCycleCORBA aLCC( myNService );
1030       SUPERV_FNode aFNode = SUPERV::FNode::_narrow( p->Node() );
1031       if ( !aFNode->_is_nil() ) {
1032         Engines::Component_var aComponent = aLCC.FindOrLoad_Component( aFNode->GetContainer(), aFNode->GetComponentName() );
1033         SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow( aComponent );
1034         if ( !CORBA::is_nil( aDriver ) ) { // if driver was found, publish object
1035           CORBA::Object_ptr anObject = new CORBA::Object();
1036           CORBA::Any* anAny = p->ToAny();
1037           (*anAny) >>= anObject;
1038         
1039           if ( aDriver->CanPublishInStudy( anObject ) ) {
1040             SALOMEDS::SObject_var aTmpSO;// = aSO;
1041             SALOMEDS_Study* aSStudy = dynamic_cast<SALOMEDS_Study*>( aStudy.get() );
1042             if ( !aSStudy ) return false;
1043             aTmpSO = aDriver->PublishInStudy( aSStudy->GetStudy(), aTmpSO, anObject, "" );
1044             aBuilder->Addreference(aSO, _PTR(SObject)(new SALOMEDS_SObject( aTmpSO )) );
1045           } 
1046           else { // can't publish object: abort transaction
1047             MESSAGE( "CanPublishInStudy() returned FALSE.  ok, AbortCommand.." );
1048             aBuilder->AbortCommand();
1049             isIn = false;
1050             return false;
1051           }
1052         }
1053         else { // component has no driver, but could store IORs (like Calculator)
1054           _PTR(SObject) anIORSO ( aStudy->FindObjectIOR( p->ToString() ) );
1055           if ( anIORSO )
1056             aBuilder->Addreference(aSO, anIORSO);
1057           else { // Hm... the object (==port value) was not found, so we don't publish it.
1058             MESSAGE( "The object (==port value) was not found, so we don't publish it" );
1059             aBuilder->AbortCommand();
1060             isIn = false;
1061             return false;
1062           }
1063         }
1064       }
1065       else { // FNode is NULL -> bad
1066         MESSAGE( "FNode is NULL.  Not good at all.  Aborting command." );
1067         aBuilder->AbortCommand();
1068         isIn = false;
1069         return false;
1070       }
1071     } 
1072     else {
1073       anIORAttr->SetValue( p->ToString() ); // ior attribute already set for the prevoius condition
1074     }
1075   }
1076
1077   if ( aDoneSomething )
1078     aBuilder->CommitCommand();
1079   else
1080     aBuilder->AbortCommand();
1081   
1082   if ( !myThread->running() ) {
1083     SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
1084     if ( aSupMod ) 
1085       aSupMod->updateObjBrowser();
1086     else MESSAGE("NULL Supervision module!");
1087   }
1088
1089   isIn = false;
1090   return true;
1091 }
1092
1093
1094 void SUPERVGUI_Main::ActivatePanning()
1095 {
1096   if (myCanvasView->isVisible()) {
1097     myCanvasView->ActivatePanning();
1098   } else if (myArrayView->isVisible()) {
1099     myArrayView->ActivatePanning();
1100   }
1101 }
1102
1103
1104 void SUPERVGUI_Main::ResetView()
1105 {
1106   if (myCanvasView->isVisible()) {
1107     myCanvasView->ResetView();
1108   } else if (myArrayView->isVisible()) {
1109     myArrayView->ResetView();
1110   }
1111 }
1112
1113 void SUPERVGUI_Main::syncNotification() {
1114   char* graph;
1115   char* node;
1116   char* type;
1117   char* message;
1118   char* sender;
1119   long  counter;
1120   char* date;
1121   long  stamp;
1122   
1123   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1124     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1125       QString mess("");
1126       mess += "NOTIF: "; mess += graph;
1127       mess += " / "    ; mess += node;
1128       mess += " / "    ; mess += type;
1129       mess += " / "    ; mess += message;
1130       getMessage()->putMessage(mess.latin1());
1131     };
1132   };
1133 }
1134   
1135 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1136                                 char* sender, long counter, char* date, long stamp) {
1137   Trace("SUPERVGUI_Main::isFiltered");
1138   bool b = false;
1139   if (strcmp(getDataflow()->Name(), graph) == 0) {
1140     SUPERVGUI_CanvasNode* n;
1141     QObjectList* nodes = queryList("SUPERVGUI_CanvasNode");
1142     QObjectListIt i(*nodes);
1143     while ((n=(SUPERVGUI_CanvasNode*)i.current()) != 0) {
1144       ++i;
1145       if (strcmp(n->name(), node) == 0) {
1146         if (strcmp(type, NOTIF_WARNING) == 0) {
1147           b = n->isWarning();
1148         } else if (strcmp(type, NOTIF_STEP) == 0) {
1149           b = n->isStep();
1150         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1151           b = n->isTrace();
1152         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1153           b = n->isVerbose();
1154         };
1155         break;
1156       };
1157     };
1158     delete nodes;
1159     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1160       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 );
1161       fflush( myLogFile );
1162     };
1163   };
1164   return( b );
1165 }
1166
1167 void SUPERVGUI_Main::closeEvent(QCloseEvent* e) {
1168   e->accept();
1169 }
1170
1171
1172 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1173
1174   myCanvas->setBackgroundColor(color);
1175   myCanvasView->setPaletteBackgroundColor(color.light());
1176   myArray->setBackgroundColor(color);
1177   myArrayView->setPaletteBackgroundColor(color.light());
1178   
1179   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1180 }
1181
1182 QPtrList< char * > SUPERVGUI_Main::getEventNodes() {
1183   return myEventNodes;
1184 }
1185
1186 void SUPERVGUI_Main::setEventNodes(QPtrList< char * > theEventNodes) {
1187   myEventNodes = theEventNodes;
1188 }
1189
1190 QPtrList< SUPERV::GraphState > SUPERVGUI_Main::getStates() {
1191   return myStates;
1192 }
1193
1194 void SUPERVGUI_Main::setStates(QPtrList< SUPERV::GraphState > theStates) {
1195   myStates = theStates;
1196 }
1197
1198 int SUPERVGUI_Main::getNodesNumber() {
1199   //create a list of nodes of the graph
1200   SUPERV_Nodes nodes = getDataflow()->Nodes();
1201   int RetVal = nodes->CNodes.length() + nodes->FNodes.length() +
1202                nodes->INodes.length() + nodes->GNodes.length() +
1203                nodes->LNodes.length() + nodes->SNodes.length();
1204   return RetVal;
1205 }
1206
1207 SUPERVGUI_Thread* SUPERVGUI_Main::getMyThread() {
1208   return myThread;
1209 }
1210
1211 void SUPERVGUI_Main::startTimer() {
1212   myTimer->start(500);
1213 }
1214
1215 void SUPERVGUI_Main::executionFinished() {
1216   SUPERVGUI* aSupMod = SUPERVGUI::Supervision();
1217   if ( aSupMod ) 
1218     aSupMod->updateObjBrowser();
1219   else MESSAGE("NULL Supervision module!");
1220   
1221   myCanvas->update();
1222   myArray->update();
1223 }
1224
1225 void SUPERVGUI_Main::checkExecution() {
1226   if (myThread->finished()) {
1227     myTimer->stop();
1228     executionFinished();
1229   }
1230 }
1231
1232 /**
1233  * Editing() is to be called by any operation in GUI before modification of a datamodel
1234  * (add/remove ports or nodes, etc.).  It is used to resolve inconsistancies between 2 data models
1235  * in Engine: Editor and Executor.  During and after execution, the values of ports and statuses of nodes
1236  * are taken from Executor data model.  But when user starts editing the graph - these changes must
1237  * be applied to Editor data model.  This function destroys Executor data model and moves to Editor.   
1238  */ 
1239 void SUPERVGUI_Main::Editing() {
1240   if ( !SUPERV_isNull( dataflow ) )
1241     dataflow->Editing();
1242   
1243   // updata GUI, Nodes' statuses and Ports' values could change.
1244   sync();
1245 }
1246
1247 void SUPERVGUI_Main::removeArrayChild(SUPERV::CNode_ptr theNode)
1248 {
1249   // mkr: since the deletion of the node allow only in CANVAS view,
1250   // it is necessary to remove the CanvasArray's children, which
1251   // have the same CNode engine as deleting node. This CNode is given
1252   // as argument
1253   if (myArray) {
1254     const QObjectList* aChList = myArray->children();
1255     if ( aChList ) { // asv 27.01.05 : fix for 7817
1256       QObjectListIt aItChList(*aChList);
1257       SUPERVGUI_CanvasNode* anObjNode;
1258       while ((anObjNode = (SUPERVGUI_CanvasNode*)aItChList.current()) != 0) {
1259         ++aItChList;
1260         if ((QString(anObjNode->getEngine()->Name())).compare(QString(theNode->Name())) == 0) {
1261           myArray->removeChild(anObjNode);
1262           delete anObjNode;
1263         }
1264       }
1265     } // end of if ( ChList )
1266   }
1267 }
1268
1269 /**
1270  * ReadyToModify() must be called before any modification
1271  * operation - asks to kill execution of dataflow.  If returns false - 
1272  * modification (==Editing() ) is not allowed.
1273  */
1274 bool SUPERVGUI_Main::ReadyToModify() {
1275   if ( CORBA::is_nil( dataflow ) )
1276     return false;
1277   if ( dataflow->IsExecuting() ) {
1278     bool quit = SUIT_MessageBox::warn2( SUIT_Session::session()->activeApplication()->desktop(), // 0=Yes, 1=No
1279       tr("WARNING"), tr("MSG_GRAPH_ISRUN"), tr( "BUT_YES" ), tr( "BUT_CANCEL" ), 0, 1, 0 );
1280     if ( quit ) // user selected NOT to kill dataflow and NOT to add new node
1281       return false;
1282     else  if ( dataflow->IsExecuting() ) // user selected to kill the dataflow and add new node after that
1283       kill();   // checking again for IsExecuting to be sure that it was not finished while MB was up
1284   }
1285   return true;
1286 }
1287
1288 /******************************* SUPERVGUI_Thread class ****************************************/
1289 SUPERVGUI_Thread::SUPERVGUI_Thread()
1290      :QThread()
1291 {
1292   myIsActive = false;
1293 }
1294
1295 SUPERVGUI_Thread::~SUPERVGUI_Thread()
1296 {
1297   //it is a virtual destructor and needs to be determine here
1298 }
1299
1300 void SUPERVGUI_Thread::startThread(const char* m)
1301 {
1302   if (!myIsActive) {
1303     myIsActive = true;
1304     
1305     myMain->getMessage()->putMessage(m); 
1306     myMain->sync();
1307
1308     start();
1309   }
1310 }
1311
1312 void SUPERVGUI_Thread::setMain( SUPERVGUI_Main* theMain )
1313 {
1314   myMain = theMain;
1315 }
1316
1317 void SUPERVGUI_Thread::KillThread( bool theValue )
1318 {
1319   myMutex.lock();
1320   myIsActive = !(theValue);
1321   myMutex.unlock();
1322 }
1323
1324 typedef TVoidMemFun2ArgEvent<SUPERVGUI_Main, char*, SUPERV::GraphState> TNodeSyncEvent;
1325
1326 void SUPERVGUI_Thread::run()
1327 {
1328   myMain->startTimer();
1329
1330   // GUI cycle to handle events coming for Engine
1331   while ( myIsActive ) {
1332
1333     SUPERV_CNode aNode = NULL;
1334     SUPERV::GraphEvent aEvent = SUPERV::UndefinedEvent ;
1335     SUPERV::GraphState aState = SUPERV::UndefinedState ;
1336
1337     // blocking function of Engine.  Return from there only after anEvent happens on node aNode
1338     myMain->getDataflow()->Event(aNode, aEvent, aState);
1339     
1340     // "kill" or undefined event came
1341     if (( aEvent == SUPERV::UndefinedEvent && aState == SUPERV::UndefinedState ) ||
1342         ( aEvent == SUPERV::NoEvent && aState == SUPERV::NoState ) ||
1343         ( aEvent == SUPERV::KillEvent && aState == SUPERV::KillState )) {
1344
1345       myIsActive = false;
1346     }
1347     else { // a "normal" execution event came
1348       char* aName = NULL;
1349       if ( aNode != NULL && !CORBA::is_nil( aNode ) ) 
1350         aName = aNode->Name();      
1351
1352       // this function is asynchronious.  The call does NOT wait when SUPERVGUI_Main::execute finishes
1353       // handling the event.  So: SUPERVGUI_Main::execute must be fast, in order we don't get here again
1354       // on the next loop iteration, BEFORE  previous SUPERVGUI_Main::execute finished.
1355       ProcessVoidEvent( new TNodeSyncEvent( myMain, &SUPERVGUI_Main::execute, aName, aState ) );
1356     }    
1357   
1358     // execution is finished.  just set a "finished" message(s)
1359     if ( !myIsActive ) {
1360       switch ( myMain->getDataflow()->State() ) {
1361       case SUPERV_Editing :     
1362         myMain->getMessage()->putMessage( myMain->getDataflow()->IsReadOnly()? 
1363                                          tr("MSG_GRAPH_READONLY"): tr("MSG_GRAPH_EDITING") );
1364         break;  
1365       case SUPERV_Suspend : 
1366         myMain->getMessage()->putMessage( tr("MSG_GRAPH_SUSPENDED") );
1367         break; 
1368       case SUPERV_Done : 
1369         myMain->getMessage()->putMessage( tr("MSG_GRAPH_FINISHED") );
1370         break;  
1371       case SUPERV_Error :
1372         myMain->getMessage()->putMessage( tr("MSG_GRAPH_ABORTED") );
1373         break;  
1374       case SUPERV_Kill:
1375         myMain->getMessage()->putMessage( tr("MSG_GRAPH_KILLED") );
1376         break;
1377       } // end of switch
1378
1379       // asv 03.02.05 : fix for PAL6859, not very good, but works..
1380       myMain->sync();
1381     } // end of if !myIsActive
1382   } // end of while( myIsActive )
1383
1384   QThread::exit();
1385 }
1386
1387 /******************************* SUPERVGUI_DSGraphParameters class ****************************************/
1388 /*!
1389   Constructor
1390 */
1391 SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters(SUPERV_Graph theGraph, bool isReadOnly)
1392      : QDialog( SUIT_Session::session()->activeApplication()->desktop(), "", true, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ) 
1393 {
1394   Trace("SUPERVGUI_DSGraphParameters::SUPERVGUI_DSGraphParameters");
1395   setCaption( tr( "TLT_DSGRAPHPARAMS" ) );
1396   setSizeGripEnabled( true );
1397   myGraph = theGraph;
1398
1399   QGridLayout* TopLayout = new QGridLayout( this );
1400   TopLayout->setSpacing( 6 );
1401   TopLayout->setMargin( 11 );
1402     
1403   QGroupBox* TopGroup = new QGroupBox( this, "TopGroup" );
1404   TopGroup->setColumnLayout(0, Qt::Vertical );
1405   TopGroup->layout()->setSpacing( 0 );
1406   TopGroup->layout()->setMargin( 0 );
1407   QGridLayout* TopGroupLayout = new QGridLayout( TopGroup->layout() );
1408   TopGroupLayout->setAlignment( Qt::AlignTop );
1409   TopGroupLayout->setSpacing( 6 );
1410   TopGroupLayout->setMargin( 11 );
1411
1412   // DeltaTime
1413   QLabel* DeltaTimeL = new QLabel( tr( "DELTATIME_LBL" ), TopGroup );  
1414   TopGroupLayout->addWidget( DeltaTimeL, 0, 0 );
1415   
1416   myDeltaTime = new QtxDblSpinBox( 0.0, 1.0, 0.1, TopGroup );
1417   myDeltaTime->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1418   TopGroupLayout->addWidget( myDeltaTime, 0, 1 );
1419
1420   // TimeOut
1421   QLabel* TimeOutL = new QLabel( tr( "TIMEOUT_LBL" ), TopGroup); 
1422   TopGroupLayout->addWidget( TimeOutL, 1, 0 );
1423
1424   myTimeOut = new QLineEdit( TopGroup );
1425   myTimeOut->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1426   myTimeOut->setValidator( new QIntValidator(this) );
1427   myTimeOut->setMinimumSize( 100, 0 );
1428   myTimeOut->setReadOnly( isReadOnly );
1429   TopGroupLayout->addWidget( myTimeOut, 1, 1 );
1430
1431   // DataStreamTrace
1432   QLabel* DataStreamTraceL = new QLabel( tr( "DATASTREAMTRACE_LBL" ), TopGroup); 
1433   TopGroupLayout->addWidget( DataStreamTraceL, 2, 0 );
1434
1435   myDataStreamTrace = new QComboBox( TopGroup );
1436   myDataStreamTrace->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
1437   myDataStreamTrace->insertItem("WithoutTrace");
1438   myDataStreamTrace->insertItem("SummaryTrace");
1439   myDataStreamTrace->insertItem("DetailedTrace");
1440   TopGroupLayout->addWidget( myDataStreamTrace, 2, 1 );
1441
1442   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
1443   GroupButtons->setColumnLayout(0, Qt::Vertical );
1444   GroupButtons->layout()->setSpacing( 0 );
1445   GroupButtons->layout()->setMargin( 0 );
1446   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons->layout() );
1447   GroupButtonsLayout->setAlignment( Qt::AlignTop );
1448   GroupButtonsLayout->setSpacing( 6 );
1449   GroupButtonsLayout->setMargin( 11 );
1450   
1451   QPushButton* okB     = new QPushButton( tr( "BUT_OK" ),     GroupButtons );
1452   QPushButton* cancelB = new QPushButton( tr( "BUT_CANCEL" ), GroupButtons );
1453
1454   GroupButtonsLayout->addWidget( okB, 0, 0 );
1455   GroupButtonsLayout->addItem  ( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
1456   GroupButtonsLayout->addWidget( cancelB, 0, 2 );
1457
1458   TopLayout->addWidget( TopGroup,     0, 0 );
1459   TopLayout->addWidget( GroupButtons, 1, 0 );
1460
1461   connect( okB,     SIGNAL( clicked() ), this, SLOT( accept() ) );
1462   connect( cancelB, SIGNAL( clicked() ), this, SLOT( reject() ) );
1463
1464   setData();
1465 }
1466
1467 /*!
1468   Destructor
1469 */
1470 SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters() {
1471   Trace("SUPERVGUI_DSGraphParameters::~SUPERVGUI_DSGraphParameters");
1472 }
1473
1474 /*!
1475   Sets data function
1476 */
1477 void SUPERVGUI_DSGraphParameters::setData() {
1478   double aDeltaTime;
1479   long aTimeOut;
1480   SUPERV::KindOfDataStreamTrace aDataStreamTrace;
1481
1482 //  myGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1483   if (myGraph->IsStreamGraph()) {
1484     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1485     if (!SUPERV_isNull(aSGraph)) 
1486       aSGraph->StreamParams(aTimeOut, aDataStreamTrace, aDeltaTime);
1487   }
1488
1489   myDeltaTime->setValue(aDeltaTime);
1490   myTimeOut->setText(QString("%1").arg(aTimeOut));
1491   myDataStreamTrace->setCurrentItem((int)aDataStreamTrace);
1492 }
1493
1494 /*!
1495   <OK> button slot
1496 */
1497 void SUPERVGUI_DSGraphParameters::accept() {
1498 //   myGraph->SetStreamParams( myTimeOut->text().toLong(),
1499 //                          (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1500 //                          myDeltaTime->value());
1501   if (myGraph->IsStreamGraph()) {
1502     SUPERV_StreamGraph aSGraph = myGraph->ToStreamGraph();
1503     if (!SUPERV_isNull(aSGraph)) 
1504       aSGraph->SetStreamParams( myTimeOut->text().toLong(),
1505                                 (SUPERV::KindOfDataStreamTrace) myDataStreamTrace->currentItem(),
1506                                 myDeltaTime->value());
1507   }
1508   QDialog::accept();
1509 }