]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI_Main.cxx
Salome HOME
74595fb33779821d754c797e8a96111f08197e5e
[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) {
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       aStartPrs->updateLinksPrs();
645       aEndPrs->updateLinksPrs();
646       aStartPrs->sync();
647       aEndPrs->sync();
648       graph->repaintContents();
649     }
650     break;
651   case CONTROLFLOW: 
652     {
653       SUPERVGUI_StartControlNode* aStartPrs = 
654         new SUPERVGUI_StartControlNode(graph->viewport(), this, theStartNode);
655       SUPERVGUI_EndControlNode* aEndPrs = 
656         new SUPERVGUI_EndControlNode(graph->viewport(), this, theEndNode, aStartPrs);
657       
658       aStartPrs->hideAll();
659       aEndPrs->hideAll();
660       
661       graph->ResizeGraph(aStartPrs, theStartNode->X(), theStartNode->Y());
662       graph->addChild(aStartPrs, theStartNode->X(), theStartNode->Y());
663       graph->ResizeGraph(aEndPrs, theEndNode->X(), theEndNode->Y());
664       graph->addChild(aEndPrs, theEndNode->X(), theEndNode->Y());
665       aStartPrs->updateLinksPrs();
666       aEndPrs->updateLinksPrs();
667       aStartPrs->sync();
668       aEndPrs->sync();
669       graph->repaintContents();
670     }
671     break;
672   case TABLE:
673     array->destroy();
674     array->create();
675     break;
676   }
677 }
678
679
680
681 SUPERVGUI_Graph* SUPERVGUI_Main::getGraph() {
682     Trace("SUPERVGUI_Main::getGraph")
683     return(graph);
684 }
685
686 SUPERVGUI_Array* SUPERVGUI_Main::getArray() {
687     Trace("SUPERVGUI_Main::getArray")
688     return(array);
689 }
690
691 SUPERV_Graph SUPERVGUI_Main::getDataflow() {
692     Trace("SUPERVGUI_Main::getDataflow")
693     return(dataflow);
694 }
695
696 QAD_Message* SUPERVGUI_Main::getMessage() {
697     Trace("SUPERVGUI_Main::getMessage")
698     return(message);
699 }
700
701 QAD_Study* SUPERVGUI_Main::getStudy() {
702     Trace("SUPERVGUI_Main::getStudy")
703     return(study);
704 }
705
706 bool SUPERVGUI_Main::isArrayShown() {
707     Trace("SUPERVGUI_Main::isArrayShown")
708     return(myCurrentView == TABLE);
709 }
710
711 void SUPERVGUI_Main::showPopup(QPopupMenu* p, QMouseEvent* e) {
712   Trace("SUPERVGUI_Main::showPopup");
713   // To check is Supervision active?
714   if (myIsLocked) return;
715
716   //  if (QAD_Application::getDesktop()->getActiveComponent().compare(STUDY_SUPERVISION) !=0) return;
717   if (QAD_Application::getDesktop()->getActiveComponent().compare( QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) ) !=0) return;
718
719   checkIsInStudy();
720   if (e->button() == RightButton) {
721     p->exec(e->globalPos());
722   }
723 }
724
725
726
727 void SUPERVGUI_Main::changeInformation() {
728   SUPERVGUI_Information* aDlg = new SUPERVGUI_Information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly());
729   if (aDlg->exec() )
730     sync();
731   delete aDlg;
732   /*    Trace("SUPERVGUI_Main::changeInformation")
733     if (Supervision.information(SUPERV::CNode::_narrow(dataflow), dataflow->IsReadOnly())) {
734       sync();
735       }*/
736 }
737
738 // returns false, if can't add dataflow into the study
739 bool SUPERVGUI_Main::addStudy() {
740   Trace("SUPERVGUI_Main::addStudy");
741   if (myIsFromStudy) return false;
742   if ((SUPERV_isNull(dataflow))) return false;
743   
744   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
745   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
746   SALOMEDS::GenericAttribute_var anAttr;
747   SALOMEDS::AttributeName_var    aName;
748   SALOMEDS::AttributeIOR_var     anIORAttr;
749   SALOMEDS::AttributePixMap_var  aPixmap;
750   bool                           aLocked = aStudy->GetProperties()->IsLocked();
751   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
752   
753   // searching dataflow
754   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
755   if (aSO->_is_nil()) { // create new dataflow SObject
756     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
757     if (aComponent->_is_nil()) { // is supervision component not found, then create it
758       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
759       anOperation->start();
760       if (aLocked) aStudy->GetProperties()->SetLocked(false);
761       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
762       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
763       aName = SALOMEDS::AttributeName::_narrow(anAttr);
764       //aName->SetValue("Supervision");
765       aName->SetValue( QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
766         
767       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
768       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
769       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
770       aBuilder->DefineComponentInstance(aComponent, *Supervision.getEngine());
771       if (aLocked) aStudy->GetProperties()->SetLocked(true);
772       anOperation->finish();
773     }
774     op->start();
775     aSO = aBuilder->NewObject(aComponent);
776     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
777     aName = SALOMEDS::AttributeName::_narrow(anAttr);
778     aName->SetValue(dataflow->Name());
779     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
780     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
781     anIORAttr->SetValue(dataflow->getIOR());
782     op->finish();
783     if (aLocked) return false;
784   }
785
786   sync();
787   Supervision.unregisterGraph(this);
788   Supervision.registerGraph(dataflow->getIOR(), this);
789   myIsFromStudy = true;
790   return true;
791 }
792
793
794 void SUPERVGUI_Main::chooseData(QListViewItem* item) {
795     Trace("SUPERVGUI_Main::chooseData")
796     if (choosing) {
797         QString id = ((QAD_ObjectBrowserItem*)item)->getEntry();
798         if (!id.isEmpty()) {
799             SALOMEDS::SObject_var object = study->getStudyDocument()->FindObjectID(id.latin1());
800             SALOMEDS::GenericAttribute_var anAttr;
801             SALOMEDS::AttributeIOR_var     anIOR;
802             Standard_CString      ior    = "";
803             if (object->FindAttribute(anAttr, "AttributeIOR")) {
804               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
805               ior = anIOR->Value();
806               portIn->setValue(ior);
807
808               // stop selection
809               choosing = false;
810               setCursor(Supervision.getCursor());
811               objectBrowser->setCursor(Supervision.getCursor());
812               Supervision.putInfo("");
813             }
814         }
815     }
816 }
817
818 void SUPERVGUI_Main::setData(SUPERVGUI_PortIn* p) {
819     Trace("SUPERVGUI_Main::setData")
820     portIn   = p;
821     choosing = true;
822     setCursor(forbiddenCursor);
823     objectBrowser->setCursor(pointingHandCursor);
824     Supervision.putInfo(tr("MSG_CHOOSE_DATA"));
825 }
826
827 SALOMEDS::SObject_var SearchOrCreateSOWithName(const SALOMEDS::Study_var theStudy,
828                                                const SALOMEDS::SObject_var theSO,
829                                                const char* theName,
830                                                QAD_Operation* theOperation,
831                                                bool* theStarted) {
832   SALOMEDS::SObject_var aResult;
833   SALOMEDS::AttributeName_var aName;
834   SALOMEDS::GenericAttribute_var anAttr;
835   if (!*theStarted) { // optimisation
836     SALOMEDS::ChildIterator_var anIterator = theStudy->NewChildIterator(theSO);
837     for (; anIterator->More(); anIterator->Next()) {
838       if (anIterator->Value()->FindAttribute(anAttr, "AttributeName")) {
839         aName = SALOMEDS::AttributeName::_narrow(anAttr);
840         if (strcmp(aName->Value(), theName) == 0) {
841           aResult = anIterator->Value();
842           break;
843         }
844       }
845     }
846   }
847   if (!aResult->_is_nil()) return aResult;
848   // add new SObject
849   if (!*theStarted) {
850     *theStarted = true;
851     theOperation->start();
852   }
853   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
854   aResult = aBuilder->NewObject(theSO);
855   anAttr = aBuilder->FindOrCreateAttribute(aResult, "AttributeName");
856   aName = SALOMEDS::AttributeName::_narrow(anAttr);
857   aName->SetValue(theName);
858   return aResult;
859 }
860
861 bool SUPERVGUI_Main::putDataStudy(SUPERV_Port p, const char* inout) {
862   Trace("SUPERVGUI_Main::putDataStudy");
863
864   static bool isIn = false;
865   if (isIn) return true; else isIn = true;
866
867   SALOMEDS::Study_var            aStudy = study->getStudyDocument();
868   SALOMEDS::StudyBuilder_var     aBuilder  = aStudy->NewBuilder();
869   SALOMEDS::GenericAttribute_var anAttr;
870   SALOMEDS::AttributeName_var    aName;
871   SALOMEDS::AttributeIOR_var     anIORAttr;
872   SALOMEDS::AttributePixMap_var  aPixmap;
873   bool                           aTransaction = false;
874   bool                           aLocked = aStudy->GetProperties()->IsLocked();
875   QAD_Operation*                 op = new SALOMEGUI_ImportOperation( study );
876   
877   // searching dataflow
878   SALOMEDS::SObject_var aSO = aStudy->FindObjectIOR(dataflow->getIOR());
879   if (aSO->_is_nil()) { // create new dataflow SObject
880     SALOMEDS::SComponent_ptr aComponent = aStudy->FindComponent(STUDY_SUPERVISION);
881     if (aComponent->_is_nil()) { // is supervision component not found, then create it
882       QAD_Operation* anOperation = new SALOMEGUI_ImportOperation( study );
883       anOperation->start();
884       if (aLocked) aStudy->GetProperties()->SetLocked(false);
885       aComponent = aBuilder->NewComponent(STUDY_SUPERVISION);
886       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
887       aName = SALOMEDS::AttributeName::_narrow(anAttr);
888       aName->SetValue( QAD_Application::getDesktop()->getComponentUserName( "SUPERV" ) );
889         
890       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
891       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
892       aPixmap->SetPixMap( "ICON_OBJBROWSER_Supervision" );
893       aBuilder->DefineComponentInstance(aComponent, *Supervision.getEngine());
894       if (aLocked) aStudy->GetProperties()->SetLocked(true);
895       anOperation->finish();
896     }
897     aTransaction = true;
898     op->start();
899     aSO = aBuilder->NewObject(aComponent);
900     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
901     aName = SALOMEDS::AttributeName::_narrow(anAttr);
902     aName->SetValue(dataflow->Name());
903     anAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
904     anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
905     anIORAttr->SetValue(dataflow->getIOR());
906   }
907   aSO = SearchOrCreateSOWithName(aStudy, aSO,  // get run time SO
908                                  QString("Run ") + myRunTime.toString() , op, &aTransaction);
909   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Node()->Name(), op, &aTransaction); // get node SO
910   aSO = SearchOrCreateSOWithName(aStudy, aSO, inout, op, &aTransaction); // get in/out SO
911   aSO = SearchOrCreateSOWithName(aStudy, aSO, p->Name(), op, &aTransaction); // get port SO
912
913   if (aLocked) {
914     if (aTransaction) op->finish();
915     isIn = false;
916     return false;
917   }
918
919   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
920   anIORAttr  = SALOMEDS::AttributeIOR::_narrow(anAttr);
921   if (!aTransaction && strcmp(anIORAttr->Value(), p->ToString()) == 0) {
922     isIn = false;
923     return true;
924   }
925   // set object value to the study: if object is external, then put it with
926   //                                 help of the specific component - owner
927   if (p->IsIOR()) {
928     // get according component driver for result object
929     SALOME_LifeCycleCORBA aLCC(myNService);
930     SUPERV_FNode aFNode = SUPERV::FNode::_narrow(p->Node());
931     if (!aFNode->_is_nil()) {
932       Engines::Component_var aComponent = aLCC.FindOrLoad_Component(aFNode->GetContainer(),
933                                                                     aFNode->GetComponentName());
934       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aComponent);
935       if (!CORBA::is_nil(aDriver)) { // if driver was found, publish object
936         CORBA::Object_ptr anObject = new CORBA::Object();
937         CORBA::Any* anAny = p->ToAny();
938         (*anAny) >>= anObject;
939         
940         if (aDriver->CanPublishInStudy(anObject)) {
941           SALOMEDS::SObject_var aTmpSO;
942           if (!aTransaction) {
943             aTmpSO = aSO;
944             aTransaction = true;
945             op->start();
946           }
947           aTmpSO = aDriver->PublishInStudy(aStudy, aTmpSO, anObject, "");
948           aBuilder->Addreference(aSO, aTmpSO);
949         } else { // can't publish object: abort transaction
950           if (aTransaction) op->abort();
951           isIn = false;
952           return false;
953         }
954       }
955     }
956   } else {
957     if (!aTransaction) {
958       aTransaction = true;
959       op->start();
960     }
961     anIORAttr->SetValue(p->ToString()); // ior attribute already set for the prevoius condition
962   }
963   
964   if (aTransaction) op->finish();
965   study->updateObjBrowser();
966   isIn = false;
967   return true;
968 }
969
970
971 void SUPERVGUI_Main::ActivatePanning()
972 {
973   if (graph->isVisible()) {
974     graph->ActivatePanning();
975   } else if (array->isVisible()) {
976     array->ActivatePanning();
977   }
978 }
979
980
981 void SUPERVGUI_Main::ResetView()
982 {
983   if (graph->isVisible()) {
984     graph->ResetView();
985   } else if (array->isVisible()) {
986     array->ResetView();
987   }
988 }
989
990
991 void SUPERVGUI_Main::setAsFromStudy(bool theToStudy) {
992   myIsFromStudy = theToStudy;
993   graph->setAsFromStudy(theToStudy);
994   array->setAsFromStudy(theToStudy);
995 }
996
997 void SUPERVGUI_Main::checkIsInStudy() {
998   if (!myIsFromStudy) return;
999
1000   SALOMEDS::Study_var aStudyDoc = study->getStudyDocument();
1001   SALOMEDS::SComponent_var aFatherLbl = aStudyDoc->FindComponent(STUDY_SUPERVISION);
1002   SALOMEDS::ChildIterator_var aChildIterator = aStudyDoc->NewChildIterator(aFatherLbl);
1003   SALOMEDS::SObject_var aDataflowLbl;
1004   SALOMEDS::GenericAttribute_var anAttr;
1005
1006   for (; aChildIterator->More(); aChildIterator->Next()) {
1007     aDataflowLbl = aChildIterator->Value();
1008     if (!aDataflowLbl->FindAttribute(anAttr, "AttributeIOR"))
1009       continue;
1010
1011     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1012     if (strcmp(anIOR->Value(), dataflow->getIOR()) == 0) return;
1013   }
1014   // This Graph considered as in study but in study it is not exists
1015   Supervision.unregisterGraph(this);
1016   setAsFromStudy(false);
1017 }
1018
1019 void SUPERVGUI_Main::syncNotification() {
1020   char* graph;
1021   char* node;
1022   char* type;
1023   char* message;
1024   char* sender;
1025   long  counter;
1026   char* date;
1027   long  stamp;
1028   
1029   while (notification->Receive(&graph, &node, &type, &message, &sender, &counter, &date, &stamp)) {
1030     if (isFiltered(graph, node, type, message, sender, counter, date, stamp)) {
1031       QString mess("");
1032       mess += "NOTIF: "; mess += graph;
1033       mess += " / "    ; mess += node;
1034       mess += " / "    ; mess += type;
1035       mess += " / "    ; mess += message;
1036       getMessage()->setMessage(mess.latin1());
1037     };
1038   };
1039 }
1040   
1041 bool SUPERVGUI_Main::isFiltered(char* graph,  char* node,   char* type, char* message, 
1042                                 char* sender, long counter, char* date, long stamp) {
1043   Trace("SUPERVGUI_Main::isFiltered");
1044   bool b = false;
1045   if (strcmp(getDataflow()->Name(), graph) == 0) {
1046     SUPERVGUI_Node* n;
1047     QObjectList* nodes = queryList("SUPERVGUI_Node");
1048     QObjectListIt i(*nodes);
1049     while ((n=(SUPERVGUI_Node*)i.current()) != 0) {
1050       ++i;
1051       if (strcmp(n->name(), node) == 0) {
1052         if (strcmp(type, NOTIF_WARNING) == 0) {
1053           b = n->isWarning();
1054         } else if (strcmp(type, NOTIF_STEP) == 0) {
1055           b = n->isStep();
1056         } else if (strcmp(type, NOTIF_TRACE) == 0) {
1057           b = n->isTrace();
1058         } else if (strcmp(type, NOTIF_VERBOSE) == 0) {
1059           b = n->isVerbose();
1060         };
1061         break;
1062       };
1063     };
1064     delete nodes;
1065     if ( myLogged && myLogFile && ( ( !myFiltered ) || b ) ) {
1066       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 );
1067       fflush( myLogFile );
1068     };
1069   };
1070   return( b );
1071 }
1072
1073
1074
1075 void SUPERVGUI_Main::setPaletteBackgroundColor(const QColor& color) { 
1076   array->setPaletteBackgroundColor(color);
1077   graph->setPaletteBackgroundColor(color);
1078   
1079   SUPERVGraph_View::setPaletteBackgroundColor(color); 
1080 }
1081