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