Salome HOME
NRI : First integration.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Main.cxx
1 using namespace std;
2 //  File      : SUPERVGUI_Main.cxx
3 //  Created   : 24 / 10 / 2001
4 //  Author    : Francis KLOSS
5 //  Project   : SALOME
6 //  Module    : SUPERVGUI
7 //  Copyright : CEA
8
9 #include "QAD_Splitter.h"
10 #include "QAD_LeftFrame.h"
11 #include "QAD_ObjectBrowser.h"
12 #include "QAD_ObjectBrowserItem.h"
13 #include "QAD_PyEditor.h"
14 #include "QAD_Message.h"
15 #include "QAD_FileDlg.h"
16 #include "QAD_Application.h"
17 #include "SUPERVGUI_Def.h"
18 #include "QAD_RightFrame.h"
19 #include "SUPERVGraph_ViewFrame.h"
20 #include <qlayout.h>
21 #include <qfile.h>
22 #include "SUPERVGUI_Main.h"
23 #include "SUPERVGUI.h"
24 #include "SUPERVGUI_ComputeNode.h"
25 #include "SUPERVGUI_ControlNode.h"
26 #include "NOTIFICATION.hxx"
27 #include "SUPERVGUI_Notification.h"
28 #include "SALOMEGUI_ImportOperation.h"
29 #include "SUPERVGUI_Information.h"
30
31
32 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, bool fromIOR)
33      : SUPERVGraph_View(theParent), 
34        myLogged( false ),
35        myFiltered( false ),
36        myLogFileName( QString::null ),
37        myLogFile( NULL ),
38        myWarning( false ),
39        myStep( false ),
40        myTrace( false ),
41        myVerbose( false )
42 {
43   Trace("SUPERVGUI_Main::SUPERVGUI_Main (new)");
44   theParent->setViewWidget(this); 
45   if (fromIOR) {
46     //SUPERVGUI_Main* am = Supervision.getMain();
47     QAD_ObjectBrowser* ob = ((QAD_StudyFrame*)(theDesktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
48     //        if (am == 0) {
49     //  ob = ((QAD_StudyFrame*)(theDesktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
50     //} else {
51     //ob = am->objectBrowser;
52     //}; 
53     QAD_ObjectBrowserItem* item = (QAD_ObjectBrowserItem*)(ob->getListView()->currentItem());
54     SALOMEDS::SObject_var obj = theDesktop->getActiveStudy()->getStudyDocument()->FindObjectID(item->getEntry().latin1());
55     SALOMEDS::GenericAttribute_var anAttr;
56     if (obj->FindAttribute(anAttr, "AttributeIOR")) {
57       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
58       Standard_CString ior = anIOR->Value();
59       dataflow = (*Supervision.getEngine())->getGraph(ior);
60       if (SUPERV_isNull(dataflow)) {
61         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
62         close();
63       } else {
64         init(theDesktop);
65       }
66     } else {
67       QMessageBox::warning(0, tr("ERROR"), tr("MSG_NOACCESS_BY_IOR"));
68       close();
69     }
70   } else {
71     dataflow = (*Supervision.getEngine())->Graph(MAIN_NEW);
72     if (SUPERV_isNull(dataflow)) {
73       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_CREATE_DF"));
74       close();
75     } else {
76       init(theDesktop);
77     }
78   }
79 }
80
81 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, bool isModify, const char* f)
82      : SUPERVGraph_View(theParent),
83        myLogged( false ),
84        myFiltered( false ),
85        myLogFileName( QString::null ),
86        myLogFile( NULL ),
87        myWarning( false ),
88        myStep( false ),
89        myTrace( false ),
90        myVerbose( false )
91 {
92   Trace("SUPERVGUI_Main::SUPERVGUI_Main (file)")
93     theParent->setViewWidget(this);  
94   if (isModify) {
95     dataflow = (*Supervision.getEngine())->Graph(f);
96   } else {
97     dataflow = (*Supervision.getEngine())->GraphE(f);
98   }
99   if (SUPERV_isNull(dataflow)) {
100     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
101     close();
102   } else {
103     init(theDesktop);
104   }
105 }
106
107 SUPERVGUI_Main::SUPERVGUI_Main(SUPERVGraph_ViewFrame* theParent, QAD_Desktop* theDesktop, SUPERV_Graph cp)
108      : SUPERVGraph_View(theParent),
109        myLogged( false ),
110        myFiltered( false ),
111        myLogFileName( QString::null ),
112        myLogFile( NULL ),
113        myWarning( false ),
114        myStep( false ),
115        myTrace( false ),
116        myVerbose( false )
117 {
118   Trace("SUPERVGUI_Main::SUPERVGUI_Main (copy)");
119   theParent->setViewWidget(this);  
120   //  dataflow = cp->Copy();
121   dataflow = cp;
122   if (SUPERV_isNull(dataflow)) {
123     QMessageBox::warning(0, tr("ERROR"), tr("MSG_CANT_COPY"));
124     close();
125   } else {
126     init(theDesktop);
127   }
128 }
129
130 void SUPERVGUI_Main::init(QAD_Desktop* theDesktop) {
131   Trace("SUPERVGUI_Main::init");
132   if (theDesktop) myNService = theDesktop->getNameService();
133   myHashCode = "New";
134   myCopyNum = 0;
135   choosing  = false;
136   myIsLocked = false;
137
138   myIsKilled = false;
139   //myIsRunned = false;
140   myCurrentView = GRAPH;
141   myIsFromStudy = false;
142   study     = theDesktop->getActiveStudy();
143   timer = new QTimer(this);
144   connect(timer, SIGNAL(timeout()), this, SLOT(execute()));
145
146     SALOMEDS::Study_var studyDoc = study->getStudyDocument();
147     SALOMEDS::StudyBuilder_var builder = studyDoc->NewBuilder();
148     SALOMEDS::SComponent_var father = studyDoc->FindComponent(STUDY_SUPERVISION);
149     SALOMEDS::GenericAttribute_var anAttr;
150     SALOMEDS::AttributeName_var    aName;
151     SALOMEDS::AttributePixMap_var  aPixmap;
152     if (father->_is_nil()) {
153         QAD_Operation* op = new SALOMEGUI_ImportOperation( study );
154         op->start();
155         father = builder->NewComponent(STUDY_SUPERVISION);
156         anAttr = builder->FindOrCreateAttribute(father, "AttributeName");
157         aName = SALOMEDS::AttributeName::_narrow(anAttr);
158         //aName->SetValue("Supervision");
159         aName->SetValue( QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
160         
161         anAttr = builder->FindOrCreateAttribute(father, "AttributePixMap");
162         aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
163         aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
164
165         builder->DefineComponentInstance(father, *Supervision.getEngine());
166         op->finish();
167     };
168
169     objectBrowser = study->getActiveStudyFrame()->getLeftFrame()->getObjectBrowser();
170     
171
172     graph = new SUPERVGUI_Graph(this);
173     array = new SUPERVGUI_Array(this);
174
175     message = study->getActiveStudyFrame()->getRightFrame()->getMessage();
176     notification = new NOTIFICATION_Consumer();
177
178     QBoxLayout * layout = new QVBoxLayout(this);
179     layout->setMargin(0);
180     layout->setSpacing(0);
181     layout->addWidget(graph);
182     layout->addWidget(array);
183
184     sync();
185     show();
186     if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
187       myLogFile = fopen( myLogFileName.latin1(), "a" );
188       if (  myLogFile == NULL )
189         myLogged = false;
190     }
191 }
192
193 SUPERVGUI_Main::~SUPERVGUI_Main() {
194   Trace("SUPERVGUI_Main::~SUPERVGUI_Main");
195   if ( myLogFile != NULL) {
196     fclose( myLogFile );
197   }
198   graph->removeLinks();
199   delete notification; // kloss : nota bene : quand un datalow est detruit : verifier que les canaux de notification sont aussi detruit
200   if (!SUPERV_isNull(dataflow)) {
201     if (dataflow->IsExecuting()) {
202       if (QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), 
203                                tr("MSG_DF_RUNNING"), 
204                                tr("MSG_DF_EXECUTION"), 
205                                tr("MSG_DF_KILL")) == 1) {
206         dataflow->Kill();
207       }
208     }
209   }
210 }
211
212 void SUPERVGUI_Main::StartTimer(const char* m) {
213     Trace("SUPERVGUI_Main::StartTimer")
214     if (!timer->isActive()) {
215         timer->start(5);
216         message->setMessage(m);
217     };
218 }
219
220 void SUPERVGUI_Main::StopTimer(const char* m) {
221     Trace("SUPERVGUI_Main::StopTimer")
222     if (timer->isActive()) {
223         timer->stop();
224         syncNotification();
225         message->setMessage(m);
226         sync();
227     };
228 }
229
230 void SUPERVGUI_Main::filterNotification() {
231   Trace("SUPERVGUI_Main::filterNotification");
232   SUPERVGUI_Notification* dlg = new SUPERVGUI_Notification(this);
233   dlg->setFiltered( myFiltered );
234   dlg->setLogged( myLogged, myLogFileName );
235   dlg->setWarning( myWarning );
236   dlg->setStep( myStep );
237   dlg->setTrace( myTrace );
238   dlg->setVerbose( myVerbose );
239   if ( dlg->exec() == QDialog::Accepted ) {
240     myLogged      = dlg->getLogged();
241     myLogFileName = dlg->getLogFile();
242     myFiltered    = dlg->getFiltered();
243     myWarning     = dlg->getWarning();
244     myStep        = dlg->getStep();
245     myTrace       = dlg->getTrace();
246     myVerbose     = dlg->getVerbose();
247     delete dlg;
248     if ( myLogFile != NULL) {
249       fclose( myLogFile );
250     }
251     myLogFile = NULL;
252     if ( myLogged && !myLogFileName.isEmpty() && QFile::exists( myLogFileName ) ) {
253       myLogFile = fopen( myLogFileName.latin1(), "a" );
254       if (  myLogFile == NULL ) {
255         myLogged = false;
256         QMessageBox::warning( QAD_Application::getDesktop(), tr("ERROR"), tr( "ERR_CANT_OPEN_LOG_FILE" ) );
257       }
258     }
259   }
260 }
261
262 void SUPERVGUI_Main::syncAsync() {
263     Trace("SUPERVGUI_Main::syncAsync")
264     QTimer::singleShot(1, this, SLOT(sync()));
265 }
266
267
268 /**
269  * Called by timer when dataflow is executing
270  */
271 void SUPERVGUI_Main::execute() {
272   SUPERV_CNode aNode;
273   SUPERV::GraphEvent aEvent;
274   SUPERV::GraphState aState;
275   dataflow->EventNoW(aNode, aEvent, aState);
276   if ((aEvent == SUPERV::NoEvent) && (aState == SUPERV::NoState)) return;
277
278   if (!SUPERV_isNull(aNode)) {
279     SUPERVGUI_Node* aNodePrs; 
280     if (myCurrentView == TABLE) {
281       aNodePrs = (SUPERVGUI_Node*) array->child(aNode->Name(), "SUPERVGUI_Node");
282     } else {
283       aNodePrs = (SUPERVGUI_Node*) graph->child(aNode->Name(), "SUPERVGUI_Node");
284     }
285     if (aNodePrs)
286       aNodePrs->sync();
287   }
288   switch (dataflow->State()) {
289   case SUPERV_Editing : 
290     StopTimer(dataflow->IsReadOnly()? tr("MSG_GRAPH_READONLY"): tr("MSG_GRAPH_EDITING"));
291     break;
292   
293   case SUPERV_Suspend : 
294     StopTimer(tr("MSG_GRAPH_SUSPENDED"));
295     break;
296  
297   case SUPERV_Done : 
298     StopTimer(tr("MSG_GRAPH_FINISHED"));
299     break;
300   
301   case SUPERV_Error : 
302     StopTimer(tr("MSG_GRAPH_ABORTED"));
303     break;
304     
305   case SUPERV_Kill : 
306     StopTimer(tr("MSG_GRAPH_KILLED"));
307     break;
308     
309   }
310 }
311
312
313
314 void SUPERVGUI_Main::sync() {
315     Trace("SUPERVGUI_Main::sync")
316     if ((SUPERV_isNull(dataflow))) return;
317     QString t = tr("GRAPH_TITLE");
318     
319     t += dataflow->Name();
320     setCaption(t);
321
322     study->updateObjBrowser();
323     if (myCurrentView == TABLE) {
324         array->sync();
325     } else {
326         graph->sync();
327     }
328 }
329
330
331 void SUPERVGUI_Main::showTable() {
332   if (myCurrentView == TABLE) return;
333
334   if (array->create()) {
335     myCurrentView = TABLE;
336     graph->hide();
337   }
338   sync();
339 }
340
341
342 void SUPERVGUI_Main::showFullGraph() {
343   if (myCurrentView == TABLE) {
344     array->destroy();
345     graph->show();    
346   }
347   myCurrentView = GRAPH;
348   graph->sync();
349   graph->setFullView();
350 }
351
352
353 void SUPERVGUI_Main::showContolFlow() {
354   if (myCurrentView == TABLE) {
355     array->destroy();
356     graph->show();    
357   }
358   myCurrentView = CONTROLFLOW;
359   graph->sync();
360   graph->setControlView();
361 }
362
363
364 bool SUPERVGUI_Main::exportDataflow(QString theFile) {
365   Trace("SUPERVGUI_Main::exportDataflow");
366   if ((SUPERV_isNull(dataflow))) return false;
367
368   if (!theFile.isEmpty()) {
369     if (!dataflow->Export(theFile.latin1())) {
370       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING"));
371       return false;
372     }
373   }
374   return true;
375 }
376
377 void SUPERVGUI_Main::insertFile() {
378     Trace("SUPERVGUI_Main::insertFile")
379     if ((SUPERV_isNull(dataflow))) return;
380
381     QString f = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
382                                          "",
383                                          "*.xml",
384                                          tr("MSG_GRAPH_INSERT"),
385                                          true);
386     if (!f.isEmpty()) {
387         if (dataflow->Import(f.latin1())) {
388           if (myCurrentView == TABLE) {
389             array->destroy();
390             array->create();
391           }
392           sync();
393         } else {
394             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_FILE").arg(f));
395         };
396     };
397 }
398
399 void SUPERVGUI_Main::copy() {
400   Trace("SUPERVGUI_Main::copy");
401   if (dataflow->ThreadsMax() == 0) {
402     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOTHING_COPY"));
403     return;
404   }
405   QAD_StudyFrame* aStudyFrame = Supervision.createGraph();
406   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
407     (aStudyFrame->getRightFrame()->getViewFrame());
408   if(aViewFrame){
409     SUPERV_Graph aNewGraph = dataflow->Copy();
410     QString aNewName(tr("MSG_COPY_PREFIX").arg(++myCopyNum));
411     aNewName += dataflow->Name();
412     aNewGraph->SetName(aNewName);
413     SUPERVGUI_Main* m = new SUPERVGUI_Main(aViewFrame, 
414                                            Supervision.getDesktop(), 
415                                            aNewGraph);
416     study->showFrame(aStudyFrame);
417   }
418 }
419
420 void SUPERVGUI_Main::run() {
421   Trace("SUPERVGUI_Main::run")
422     if ((SUPERV_isNull(dataflow))) return;
423   
424   if (dataflow->IsEditing()) {
425     if (!dataflow->IsValid()) {
426       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
427     } else if (!dataflow->IsExecutable()) {
428       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
429     } else {
430       myRunTime = QDateTime::currentDateTime();
431       if (myIsKilled) {
432       //if (myIsRunned) {
433         if (!dataflow->ReRun()) {
434           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
435         } else {
436           StartTimer(tr("MSG_GRAPH_STARTED"));
437           syncNotification();
438           sync();
439         }
440       } else {
441         if (!dataflow->Run()) {
442           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
443         } else {
444           StartTimer(tr("MSG_GRAPH_STARTED"));
445           syncNotification();
446           //myIsRunned = true;
447           sync();
448         }
449       }
450     }
451   } else {
452     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
453   }
454 }
455
456
457
458 void SUPERVGUI_Main::startExecute() {
459   Trace("SUPERVGUI_Main::startExecute")
460     if ((SUPERV_isNull(dataflow))) return;
461   
462   if (dataflow->IsEditing()) {
463     if (!dataflow->IsValid()) {
464       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTVALID"));
465     } else if (!dataflow->IsExecutable()) {
466       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTEXECUTABLE"));
467     } else {
468       myRunTime = QDateTime::currentDateTime();
469       if (myIsKilled) {
470       //if (myIsRunned) {
471         if (!dataflow->ReStart()) {
472           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
473         } else {
474           StartTimer(tr("MSG_GRAPH_STARTED"));
475           syncNotification();
476         }
477       }
478       else {
479         if (!dataflow->Start()) {
480           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_BADEXECUTE"));
481         } else {
482           StartTimer(tr("MSG_GRAPH_STARTED"));
483           syncNotification();
484         }
485       }
486     }
487   } else {
488     QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_RUNNING"));
489   }
490 }
491
492
493 void SUPERVGUI_Main::kill() {
494     Trace("SUPERVGUI_Main::kill")
495     if ((SUPERV_isNull(dataflow))) return;
496
497     if (dataflow->IsEditing()) {
498       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
499     } else if (dataflow->Kill()) {
500       myIsKilled = true;
501       sync();
502     } else {
503       QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANTKILL_DF"));
504     }
505 }
506
507 void SUPERVGUI_Main::suspendResume() {
508     Trace("SUPERVGUI_Main::suspendResume")
509     if ((SUPERV_isNull(dataflow))) return;
510
511     if (dataflow->IsEditing()) {
512         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_DF_NOTRUNNING"));
513     } else if (dataflow->State() == SUPERV_Suspend) {
514       if (dataflow->Resume()) {
515         message->setMessage(tr("MSG_DF_RESUMED"));
516         sync();
517       } else {
518         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESUME"));
519       }
520     } else {
521       if (dataflow->Suspend()) {
522         sync();
523       } else {
524         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_SUSPEND"));
525       }
526     }
527 }
528
529 void SUPERVGUI_Main::stopRestart() { // kloss : a reviser et a connecter dans le popup du dataflow (pas de creation de bouton)
530     Trace("SUPERVGUI_Main::stopRestart")
531     if ((SUPERV_isNull(dataflow))) return;
532
533     if (dataflow->IsEditing()) {
534         QMessageBox::warning(0, tr("ERROR"),  tr("MSG_DF_NOTRUNNING"));
535     } else if (dataflow->State() == SUPERV_Stop) {
536         if (dataflow->ReStart()) {
537             message->setMessage(tr("MSG_DF_RESTARTED"));
538             sync();
539         } else {
540             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_RESTART"));
541         };
542     } else {
543         if (dataflow->Stop()) {
544             sync();
545         } else {
546             QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_STOP"));
547         };
548     };
549 }
550
551 void SUPERVGUI_Main::addNode() {
552   Trace("SUPERVGUI_Main::addNode");
553   if (SUPERV_isNull(dataflow)) return;
554
555   if (dataflow->IsExecuting()) {
556     if (QMessageBox::warning(QAD_Application::getDesktop(), 
557                              tr("WARNING"), tr("MSG_GRAPH_ISRUN"),
558                              QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
559       return;        
560     } else {
561       kill();
562     }
563   }
564   Supervision.getBrowser()->choose();
565 }
566
567
568
569 /**
570  * Add Computation node
571  */
572 void SUPERVGUI_Main::addComputeNode(SUPERV_CNode theNode) {
573   //cout<<"### X="<<theNode->X()<<"  Y="<<theNode->Y()<<endl;
574   switch (myCurrentView) {
575   case GRAPH:
576     {
577       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
578       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
579       graph->addChild(aNode, theNode->X(), theNode->Y());
580       aNode->sync();
581     }
582     break;
583   case CONTROLFLOW: 
584     {
585       SUPERVGUI_Node* aNode = new SUPERVGUI_ComputeNode(graph->viewport(), this, theNode);
586       aNode->hideAll();
587       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
588       graph->addChild(aNode, theNode->X(), theNode->Y());
589       aNode->sync();
590     }
591     break;
592   case TABLE:
593     array->destroy();
594     array->create();
595     break;
596   }
597 }
598
599 /**
600  * Add GOTO node
601  */
602 void SUPERVGUI_Main::addGOTONode(SUPERV_CNode theNode) {
603   switch (myCurrentView) {
604   case GRAPH:
605     {
606       SUPERVGUI_Node* aNode = new SUPERVGUI_GotoNode(graph->viewport(), this, theNode);
607       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
608       graph->addChild(aNode, theNode->X(), theNode->Y());
609       aNode->sync();
610     }
611     break;
612   case CONTROLFLOW: 
613     {
614       SUPERVGUI_Node* aNode = new SUPERVGUI_GotoNode(graph->viewport(), this, theNode);
615       aNode->hideAll();
616       graph->ResizeGraph(aNode, theNode->X(), theNode->Y());
617       graph->addChild(aNode, theNode->X(), theNode->Y());
618       aNode->sync();
619     }
620     break;
621   case TABLE:
622     array->destroy();
623     array->create();
624     break;
625   }
626 }
627
628 /**
629  * Add Control node
630  */
631 void SUPERVGUI_Main::addControlNode(SUPERV_CNode theStartNode, SUPERV_CNode theEndNode, bool Update) {
632   switch (myCurrentView) {
633   case GRAPH:
634     {
635       SUPERVGUI_StartControlNode* aStartPrs = 
636         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
637       SUPERVGUI_EndControlNode* aEndPrs = 
638         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
639
640       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());            
641       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
642       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
643       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
644       if (Update) {
645         aStartPrs->updateLinksPrs();
646         aEndPrs->updateLinksPrs();
647       }
648       aStartPrs->sync();
649       aEndPrs->sync();
650       graph->repaintContents();
651     }
652     break;
653   case CONTROLFLOW: 
654     {
655       SUPERVGUI_StartControlNode* aStartPrs = 
656         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
657       SUPERVGUI_EndControlNode* aEndPrs = 
658         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
659       
660       aStartPrs->hideAll();
661       aEndPrs->hideAll();
662       
663       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());
664       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
665       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
666       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
667       if (Update) {
668         aStartPrs->updateLinksPrs();
669         aEndPrs->updateLinksPrs();
670       }
671       aStartPrs->sync();
672       aEndPrs->sync();
673       graph->repaintContents();
674     }
675     break;
676   case TABLE:
677     array->destroy();
678     array->create();
679     break;
680   }
681 }
682
683
684
685 SUPERVGUI_Graph* SUPERVGUI_Main::getGraph() {
686     Trace("SUPERVGUI_Main::getGraph")
687     return(graph);
688 }
689
690 SUPERVGUI_Array* SUPERVGUI_Main::getArray() {
691     Trace("SUPERVGUI_Main::getArray")
692     return(array);
693 }
694
695 SUPERV_Graph SUPERVGUI_Main::getDataflow() {
696     Trace("SUPERVGUI_Main::getDataflow")
697     return(dataflow);
698 }
699
700 QAD_Message* SUPERVGUI_Main::getMessage() {
701     Trace("SUPERVGUI_Main::getMessage")
702     return(message);
703 }
704
705 QAD_Study* SUPERVGUI_Main::getStudy() {
706     Trace("SUPERVGUI_Main::getStudy")
707     return(study);
708 }
709
710 bool SUPERVGUI_Main::isArrayShown() {
711     Trace("SUPERVGUI_Main::isArrayShown")
712     return(myCurrentView == TABLE);
713 }
714
715 void SUPERVGUI_Main::showPopup(QPopupMenu* p, QMouseEvent* e) {
716   Trace("SUPERVGUI_Main::showPopup");
717   // To check is Supervision active?
718   if (myIsLocked) return;
719
720   if (QAD_Application::getDesktop()->getActiveComponent().compare(STUDY_SUPERVISION) !=0) return;
721
722   checkIsInStudy();
723   if (e->button() == RightButton) {
724     p->exec(e->globalPos());
725   }
726 }
727
728
729
730 void SUPERVGUI_Main::changeInformation() {
731   SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly());
732   if (aDlg->exec() )
733     sync();
734   delete aDlg;
735   /*    Trace("SUPERVGUI_Main::changeInformation")
736     if (Supervision.information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly())) {
737       sync();
738       }*/
739 }
740
741 // returns false, if can't add dataflow into the study
742 bool SUPERVGUI_Main::addStudy() {
743   Trace("SUPERVGUI_Main::addStudy");
744   if (myIsFromStudy) return false;
745   if ((SUPERV_isNull(dataflow))) return false;
746   
747   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
748   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
749   SALOMEDS::GenericAttribute_var anAttr;
750   SALOMEDS::AttributeName_var    aName;
751   SALOMEDS::AttributeIOR_var     anIORAttr;
752   SALOMEDS::AttributePixMap_var  aPixmap;
753   bool                           aLocked = aStudy->GetProperties()->IsLocked();
754   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
755   
756   // searching dataflow
757   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
758   if (aSO->_is_nil()) { // create new dataflow SObject
759     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
760     if (aComponent->_is_nil()) { // is supervision component not found, then create it
761       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
762       anOperation->start();
763       if (aLocked) aStudy->GetProperties()->SetLocked(false);
764       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
765       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
766       aName = SALOMEDS::AttributeName::_narrow(anAttr);
767       aName->SetValue(STUDY_SUPERVISION);
768         
769       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
770       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
771       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
772       aBuilder->DefineComponentInstance(aComponent, *Supervision.getEngine());
773       if (aLocked) aStudy->GetProperties()->SetLocked(true);
774       anOperation->finish();
775     }
776     op->start();
777     aSO = aBuilder->NewObject(aComponent);
778     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
779     aName = SALOMEDS::AttributeName::_narrow(anAttr);
780     aName->SetValue(dataflow->Name());
781     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
782     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
783     anIORAttr->SetValue(dataflow->getIOR());
784     op->finish();
785     if (aLocked) return false;
786   }
787
788   sync();
789   Supervision.unregisterGraph(this);
790   Supervision.registerGraph(dataflow->getIOR(), this);
791   myIsFromStudy = true;
792   return true;
793 }
794
795
796 void SUPERVGUI_Main::chooseData(QListViewItem* item) {
797     Trace("SUPERVGUI_Main::chooseData")
798     if (choosing) {
799         QString id = ((QAD_ObjectBrowserItem*)item)->getEntry();
800         if (!id.isEmpty()) {
801             SALOMEDS::SObject_var object = study->getStudyDocument()->FindObjectID(id.latin1());
802             SALOMEDS::GenericAttribute_var anAttr;
803             SALOMEDS::AttributeIOR_var     anIOR;
804             Standard_CString      ior    = "";
805             if (object->FindAttribute(anAttr, "AttributeIOR")) {
806               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
807               ior = anIOR->Value();
808               portIn->setValue(ior);
809
810               // stop selection
811               choosing = false;
812               setCursor(Supervision.getCursor());
813               objectBrowser->setCursor(Supervision.getCursor());
814               Supervision.putInfo("");
815             }
816         }
817     }
818 }
819
820 void SUPERVGUI_Main::setData(SUPERVGUI_PortIn* p) {
821     Trace("SUPERVGUI_Main::setData")
822     portIn   = p;
823     choosing = true;
824     setCursor(forbiddenCursor);
825     objectBrowser->setCursor(pointingHandCursor);
826     Supervision.putInfo(tr("MSG_CHOOSE_DATA"));
827 }
828
829 SALOMEDS::SObject_var SearchOrCreateSOWithName(const SALOMEDS::Study_var theStudy,
830                                                const SALOMEDS::SObject_var theSO,
831                                                const char* theName,
832                                                QAD_Operation* theOperation,
833                                                bool* theStarted) {
834   SALOMEDS::SObject_var aResult;
835   SALOMEDS::AttributeName_var aName;
836   SALOMEDS::GenericAttribute_var anAttr;
837   if (!*theStarted) { // optimisation
838     SALOMEDS::ChildIterator_var anIterator = theStudy->NewChildIterator(theSO);
839     for (; anIterator->More(); anIterator->Next()) {
840       if (anIterator->Value()->FindAttribute(anAttr, "AttributeName")) {
841         aName = SALOMEDS::AttributeName::_narrow(anAttr);
842         if (strcmp(aName->Value(), theName) == 0) {
843           aResult = anIterator->Value();
844           break;
845         }
846       }
847     }
848   }
849   if (!aResult->_is_nil()) return aResult;
850   // add new SObject
851   if (!*theStarted) {
852     *theStarted = true;
853     theOperation->start();
854   }
855   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
856   aResult = aBuilder->NewObject(theSO);
857   anAttr = aBuilder->FindOrCreateAttribute(aResult, "AttributeName");
858   aName = SALOMEDS::AttributeName::_narrow(anAttr);
859   aName->SetValue(theName);
860   return aResult;
861 }
862
863 bool SUPERVGUI_Main::putDataStudy(SUPERV_Port p, const char* inout) {
864   Trace("SUPERVGUI_Main::putDataStudy");
865
866   static bool isIn = false;
867   if (isIn) return true; else isIn = true;
868
869   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
870   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
871   SALOMEDS::GenericAttribute_var anAttr;
872   SALOMEDS::AttributeName_var    aName;
873   SALOMEDS::AttributeIOR_var     anIORAttr;
874   SALOMEDS::AttributePixMap_var  aPixmap;
875   bool                           aTransaction = false;
876   bool                           aLocked = aStudy->GetProperties()->IsLocked();
877   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
878   
879   // searching dataflow
880   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
881   if (aSO->_is_nil()) { // create new dataflow SObject
882     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
883     if (aComponent->_is_nil()) { // is supervision component not found, then create it
884       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
885       anOperation->start();
886       if (aLocked) aStudy->GetProperties()->SetLocked(false);
887       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
888       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
889       aName = SALOMEDS::AttributeName::_narrow(anAttr);
890       aName->SetValue(STUDY_SUPERVISION);
891         
892       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
893       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
894       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
895       aBuilder->DefineComponentInstance(aComponent, *Supervision.getEngine());
896       if (aLocked) aStudy->GetProperties()->SetLocked(true);
897       anOperation->finish();
898     }
899     aTransaction = true;
900     op->start();
901     aSO = aBuilder->NewObject(aComponent);
902     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
903     aName = SALOMEDS::AttributeName::_narrow(anAttr);
904     aName->SetValue(dataflow->Name());
905     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
906     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
907     anIORAttr->SetValue(dataflow->getIOR());
908   }
909   aSO = SearchOrCreateSOWithName(aStudy, aSO,  // get run time SO
910                                  QString("Run ") + myRunTime.toString() , op, &aTransaction);
911   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Node()->Name(), op, &aTransaction); // get node SO
912   aSO = SearchOrCreateSOWithName(aStudy, aSO, inout, op, &aTransaction); // get in/out SO
913   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Name(), op, &aTransaction); // get port SO
914
915   if (aLocked) {
916     if (aTransaction) op->finish();
917     isIn = false;
918     return false;
919   }
920
921   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
922   anIORAttr  = SALOMEDS::AttributeIOR::_narrow(anAttr);
923   if (!aTransaction && strcmp(anIORAttr->Value(), p->ToString()) == 0) {
924     isIn = false;
925     return true;
926   }
927   // set object value to the study: if object is external, then put it with
928   //                                 help of the specific component - owner
929   if (p->IsIOR()) {
930     // get according component driver for result object
931     SALOME_LifeCycleCORBA aLCC(myNService);
932     SUPERV_FNode aFNode = SUPERV::FNode::_narrow(p->Node());
933     if (!aFNode->_is_nil()) {
934       Engines::Component_var aComponent = aLCC.FindOrLoad_Component(aFNode->GetContainer(),
935                                                                     aFNode->GetComponentName());
936       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aComponent);
937       if (!CORBA::is_nil(aDriver)) { // if driver was found, publish object
938         CORBA::Object_ptr anObject = new CORBA::Object();
939         CORBA::Any* anAny = p->ToAny();
940         (*anAny) >>= anObject;
941         
942         if (aDriver->CanPublishInStudy(anObject)) {
943           SALOMEDS::SObject_var aTmpSO;
944           if (!aTransaction) {
945             aTmpSO = aSO;
946             aTransaction = true;
947             op->start();
948           }
949           aTmpSO = aDriver->PublishInStudy(aStudy, aTmpSO, anObject, "");
950           aBuilder->Addreference(aSO, aTmpSO);
951         } else { // can't publish object: abort transaction
952           if (aTransaction) op->abort();
953           isIn = false;
954           return false;
955         }
956       }
957     }
958   } else {
959     if (!aTransaction) {
960       aTransaction = true;
961       op->start();
962     }
963     anIORAttr->SetValue(p->ToString()); // ior attribute already set for the prevoius condition
964   }
965   
966   if (aTransaction) op->finish();
967   study->updateObjBrowser();
968   isIn = false;
969   return true;
970 }
971
972
973 void SUPERVGUI_Main::ActivatePanning()
974 {
975   if (graph->isVisible()) {
976     graph->ActivatePanning();
977   } else if (array->isVisible()) {
978     array->ActivatePanning();
979   }
980 }
981
982
983 void SUPERVGUI_Main::ResetView()
984 {
985   if (graph->isVisible()) {
986     graph->ResetView();
987   } else if (array->isVisible()) {
988     array->ResetView();
989   }
990 }
991
992
993 void SUPERVGUI_Main::setAsFromStudy(bool theToStudy) {
994   myIsFromStudy = theToStudy;
995   graph->setAsFromStudy(theToStudy);
996   array->setAsFromStudy(theToStudy);
997 }
998
999 void SUPERVGUI_Main::checkIsInStudy() {
1000   if (!myIsFromStudy) return;
1001
1002   SALOMEDS::Study_var aStudyDoc = study->getStudyDocument();
1003   SALOMEDS::SComponent_var aFatherLbl = aStudyDoc->FindComponent(STUDY_SUPERVISION);
1004   SALOMEDS::ChildIterator_var aChildIterator = aStudyDoc->NewChildIterator(aFatherLbl);
1005   SALOMEDS::SObject_var aDataflowLbl;
1006   SALOMEDS::GenericAttribute_var anAttr;
1007
1008   for (; aChildIterator->More(); aChildIterator->Next()) {
1009     aDataflowLbl = aChildIterator->Value();
1010     if (!aDataflowLbl->FindAttribute(anAttr, "AttributeIOR"))
1011       continue;
1012
1013     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1014     if (strcmp(anIOR->Value(), dataflow->getIOR()) == 0) return;
1015   }
1016   // This Graph considered as in study but in study it is not exists
1017   Supervision.unregisterGraph(this);
1018   setAsFromStudy(false);
1019 }
1020
1021 void SUPERVGUI_Main::syncNotification() {
1022   char* graph;
1023   char* node;
1024   char* type;
1025   char* message;
1026   char* sender;
1027   long  counter;
1028   char* date;
1029   long  stamp;
1030   
1031   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1032     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1033       QString mess("");
1034       mess += "NOTIF: "; mess += graph;
1035       mess += " / "    ; mess += node;
1036       mess += " / "    ; mess += type;
1037       mess += " / "    ; mess += message;
1038       getMessage()->setMessage(mess.latin1());
1039     };
1040   };
1041 }
1042   
1043 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1044                                 char* sender, long counter, char* date, long stamp) {
1045   Trace("SUPERVGUI_Main::isFiltered");
1046   bool b = false;
1047   if (strcmp(getDataflow()->Name(), graph) == 0) {
1048     SUPERVGUI_Node* n;
1049     QObjectList* nodes = queryList("SUPERVGUI_Node");
1050     QObjectListIt i(*nodes);
1051     while ((n=(SUPERVGUI_Node*)i.current()) != 0) {
1052       ++i;
1053       if (strcmp(n->name(), node) == 0) {
1054         if (strcmp(type, NOTIF_WARNING) == 0) {
1055           b = n->isWarning();
1056         } else if (strcmp(type, NOTIF_STEP) == 0) {
1057           b = n->isStep();
1058         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1059           b = n->isTrace();
1060         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1061           b = n->isVerbose();
1062         };
1063         break;
1064       };
1065     };
1066     delete nodes;
1067     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1068       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 );
1069       fflush( myLogFile );
1070     };
1071   };
1072   return( b );
1073 }
1074
1075
1076
1077 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1078   array->setPaletteBackgroundColor(color);
1079   graph->setPaletteBackgroundColor(color);
1080   
1081   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1082 }
1083