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