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