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