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