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