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