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