Salome HOME
Fix for PAL7823 (checking for if study is locked before doing New Dataflow command).
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI.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.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29 #include "SUPERVGUI.h"
30
31 #include "QAD_Resource.h"
32 #include "QAD_FileDlg.h"
33 #include "QAD_Application.h"
34 #include "QAD_StudyFrame.h"
35 #include "QAD_RightFrame.h"
36 #include "QAD_LeftFrame.h"
37 #include "QAD_ViewFrame.h"
38 #include "QAD_ObjectBrowser.h"
39 #include "QAD_ObjectBrowserItem.h"
40 #include "QAD_MessageBox.h"
41
42 #include "SALOME_Selection.h"
43 #include "SALOME_ListIteratorOfListIO.hxx"
44 #include "SALOME_InteractiveObject.hxx"
45 #include "SALOMEGUI_ImportOperation.h"
46 #include "SALOMEGUI_NameDlg.h"            
47 #include "Utils_ORB_INIT.hxx"
48
49 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
50
51
52 int SUPERVGUI::factory = 0;
53 SUPERVGUI Supervision("");
54
55
56 SUPERVGUI::SUPERVGUI(const QString& theName, QObject* theParent)
57      : SALOMEGUI( theName, theParent ),
58        desktop(0),
59        study(0),
60        browser(0)
61   //info(0)
62 {
63     Trace("SUPERVGUI::SUPERVGUI")
64     if (factory==0) {
65         factory = 1;
66         MESSAGE("SUPERVGUI Info : factory build");
67     } else {
68         MESSAGE("SUPERVGUI Error : another call to factory constructor");
69     };
70 }
71
72 SUPERVGUI::~SUPERVGUI() {
73     Trace("SUPERVGUI::~SUPERVGUI")
74     if (factory==1) {
75         factory = 0;
76         //if (!engine->_is_nil()) CORBA::release(engine);         
77         //info->close();
78         if (browser) {
79           if (browser->isVisible())
80             browser->close();
81           delete browser;
82         }
83         MESSAGE("SUPERVGUI Info : factory destroy");
84     } else {
85         MESSAGE("SUPERVGUI Error : another call to factory destructor");
86     };
87 }
88
89 void SUPERVGUI::init(QAD_Desktop* parent) {
90     Trace("SUPERVGUI::init")
91     if (desktop==0) {
92         desktop = parent;
93         study = desktop->getActiveStudy();
94         SALOME_NamingService* namingService = desktop->getNameService();
95         loadEngine(namingService);
96         browser = new SUPERVGUI_Service(namingService);
97         //info    = new SUPERVGUI_Information();
98         cursor  = desktop->cursor();
99         connect(desktop->getMainFrame(), SIGNAL(windowActivated(QWidget*)), this, SLOT(setMain(QWidget*)));
100
101         int anId = 300;
102         parent->menuBar()->changeItem(anId,parent->getComponentUserName("SUPERV"));
103         
104     };
105 }
106
107
108 QAD_StudyFrame* SUPERVGUI::createGraph() {
109   QAD_StudyFrame* aStudyFrame = study->newWindow3d(study->getNextStudyFrameName(),
110                                                    VIEW_GRAPHSUPERV, false);
111   if (aStudyFrame) {
112     connect(study, 
113             SIGNAL(supervStudyFrameClosing(QAD_ViewFrame*)), 
114             &Supervision, 
115             SLOT(onGraphClosed(QAD_ViewFrame*)));
116   }
117   return aStudyFrame;
118 }
119
120
121
122 void SUPERVGUI::loadEngine(SALOME_NamingService* namingService) {
123     Trace("SUPERVGUI::loadEngine")
124     SALOME_LifeCycleCORBA LCC(namingService);
125     Engines::Component_var objComponent = LCC.FindOrLoad_Component("SuperVisionContainer", "SUPERV");
126     if (CORBA::is_nil(objComponent)) {
127         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_LOAD_SUPERV"));
128         return;
129     };
130
131     SUPERV::SuperG_var aSuperVisionComponent;
132     aSuperVisionComponent = SUPERV::SuperG::_narrow(objComponent);
133     if (CORBA::is_nil(aSuperVisionComponent)) {
134         QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_CANT_NARROW_SUPERV"));
135         return;
136     };
137
138     //    engine  = new SUPERV::SuperG_var;
139     engine = aSuperVisionComponent;
140 }
141 /*
142 bool SUPERVGUI::information(SUPERV_CNode node, bool isReadOnly) {
143     Trace("SUPERVGUI::information")
144     return(info->run(node, isReadOnly));
145 }
146 */
147 void SUPERVGUI::putInfo(const char* mess) {
148     Trace("SUPERVGUI::putInfo")
149     desktop->putInfo(mess);
150 }
151
152 QAD_Desktop* SUPERVGUI::getDesktop() {
153     Trace("SUPERVGUI::getDesktop")
154     return(desktop);
155 }
156
157 SUPERV_Engine SUPERVGUI::getEngine() {
158     Trace("SUPERVGUI::getEngine")
159     return(engine);
160 }
161
162 SUPERVGUI_Service* SUPERVGUI::getBrowser() {
163     Trace("SUPERVGUI::getBrowser")
164     return(browser);
165 }
166
167 QCursor SUPERVGUI::getCursor() {
168     Trace("SUPERVGUI::getCursor")
169     return(cursor);
170 }
171
172 SUPERVGUI_Main* SUPERVGUI::getMain() {
173     Trace("SUPERVGUI::getMain")
174     return main;
175 }
176
177
178 void SUPERVGUI::registerGraph(QString theName, SUPERVGUI_Main* theGraph){
179   theGraph->setHashCode(theName);
180   myGraphList.append(theGraph);
181 }
182
183
184 void SUPERVGUI::unregisterGraph(SUPERVGUI_Main* theGraph) {
185   SUPERVGUI_Main* aMain;
186   for (aMain = myGraphList.first(); aMain; aMain = myGraphList.next()) {
187     if (aMain == theGraph)
188       myGraphList.remove(aMain);
189   }
190 }
191
192
193 bool SUPERVGUI::isContains(QAD_Study* theStudy, const QString theKey) {
194   SUPERVGUI_Main* aMain;
195   for (aMain = myGraphList.first(); aMain; aMain = myGraphList.next()) {
196     if ((aMain->getStudy() == theStudy) && (aMain->getHashCode() == theKey))
197       return true;
198   }
199   return false;
200 }
201
202
203 void SUPERVGUI::onGraphClosed(QAD_ViewFrame* theViewFrame) {
204   if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(theViewFrame)){
205     disconnect(study, 0 , this, 0);
206     SUPERVGraph_View* view = supervFrame->getViewWidget();
207     SUPERVGUI_Main* aGraph = dynamic_cast<SUPERVGUI_Main*>(view);
208     if (aGraph) {
209       unregisterGraph(aGraph);
210       SUPERV_Graph aDataFlow = aGraph->getDataflow();
211       if ( !SUPERV_isNull( aDataFlow ) ) {
212         if ( aDataFlow->IsExecuting() ) {
213           const int aMsgResult = QMessageBox::warning(QAD_Application::getDesktop(),
214                                                       tr("WARNING"), 
215                                                       tr("MSG_DF_RUNNING"), 
216                                                       tr("MSG_DF_EXECUTION"), 
217                                                       tr("MSG_DF_KILL"));
218           if ( aMsgResult == 1 ) {
219             // KILL EXECUTION
220             if ( aDataFlow->Kill() ) {
221               // Kill() sends KillEvent and KillState to SUPERVGUI_Thread
222               // while sets myIsActive flag to false when it receives such event/state
223               // after myIsActive is false it calls QThread::exit() to terminate.
224
225               // why while() { qApp->processEvents() } ?
226               // because: SUPERVGUI_Thread::run() receives events, and calls myMain->execute()
227               // method using SALOME_Event paradigm, ProcessVoidEvent() function - 
228               // it puts this event to the main application event loop, in which we are being now.
229               // So if we block main GUI application thread (by calling aGraph->getMyThread()->wait() here)
230               // then we will have a deadlock of 2 threads waiting for one another 
231               while (aGraph->getMyThread()->running()) 
232                 qApp->processEvents();
233             }
234           }
235           else { // BACKGROUND EXECUTION
236             emit KillMainThread(true); // set SUPERVGUI_Thread::myIsActive to false
237             while ( aGraph->getMyThread()->running() )
238               qApp->processEvents();
239           }
240         }
241         else { // EXECUTION IS FINISHED, JUST DESTROY THE SUPERVGUI_Thread object and return.
242           emit KillMainThread(true);
243           while ( aGraph->getMyThread()->running() ) 
244             qApp->processEvents();
245         }
246       }
247     }
248   }
249 }
250
251 void SUPERVGUI::setMain(QWidget* w) {
252   main = 0;
253   QAD_StudyFrame* aStudyFrame = dynamic_cast<QAD_StudyFrame*>(w);
254   if (!aStudyFrame) return;
255
256   study = aStudyFrame->getStudy();
257   if (!study) return;
258
259   QAD_ViewFrame* viewFrame = aStudyFrame->getRightFrame()->getViewFrame();
260   if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(viewFrame)){
261     SUPERVGraph_View* view = supervFrame->getViewWidget();
262     if (view) {
263       main = dynamic_cast<SUPERVGUI_Main*>(view);
264       connect(&Supervision, SIGNAL(KillMainThread(bool)), main->getMyThread(), SLOT(KillThread(bool)));
265     }
266   }
267   Trace("SUPERVGUI::setMain - main: "<<main);
268 }
269
270 void SUPERVGUI::displayDataflow() {
271     Trace("SUPERVGUI::displayDataflow");
272     QAD_ObjectBrowser* aBrowser = ((QAD_StudyFrame*)(desktop->getMainFrame()->activeWindow()))->getLeftFrame()->getObjectBrowser();
273     SUPERV_Graph aDataFlow;
274     QString aIORName;
275
276     QAD_ObjectBrowserItem* item = (QAD_ObjectBrowserItem*)(aBrowser->getListView()->currentItem());
277     SALOMEDS::SObject_var aObj = desktop->getActiveStudy()->getStudyDocument()->FindObjectID(item->getEntry().latin1());
278     SALOMEDS::GenericAttribute_var anAttr;
279     if (aObj->FindAttribute(anAttr, "AttributeIOR")) {
280       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
281       Standard_CString ior = anIOR->Value();
282       aIORName = ior;
283       if (isContains(study, aIORName)) {
284         if (QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), 
285                                  tr("MSG_GRAPH_DISPLAYED").arg(""), 
286                                  QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
287           return;
288       }
289       //aDataFlow = engine->getStreamGraph(ior);
290       aDataFlow = engine->getGraph(ior);
291       if (SUPERV_isNull(aDataFlow)) {
292         QMessageBox::warning(0, tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
293         return;
294       }
295       /* ASV: 20.10.04: fix for 6896
296       if (aDataFlow->IsStreamGraph()) {
297         SUPERV_StreamGraph aDataFlowStream = aDataFlow->ToStreamGraph();
298         if (SUPERV_isNull(aDataFlowStream)) {
299           QMessageBox::warning(0, tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
300           return;
301         }
302         aDataFlow = aDataFlowStream;
303       }
304       */
305     }
306     QAD_StudyFrame* aStudyFrame = createGraph();
307     SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
308       (aStudyFrame->getRightFrame()->getViewFrame());
309     if(aViewFrame) {
310       main = new SUPERVGUI_Main(aViewFrame, desktop, aDataFlow);
311       main->setAsFromStudy(true);
312       registerGraph(aIORName, main);
313       study->showFrame(aStudyFrame);
314     }
315 }
316
317
318 void SUPERVGUI::renameDataflow() {
319   SALOME_Selection* Sel = SALOME_Selection::Selection( study->getSelection() );
320   SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
321   for ( ; It.More(); It.Next() ) {
322     Handle(SALOME_InteractiveObject) IObject = It.Value();
323     
324     SALOMEDS::Study_var aStudy = study->getStudyDocument();
325     SALOMEDS::SObject_var obj = aStudy->FindObjectID( IObject->getEntry() );
326     SALOMEDS::GenericAttribute_var anAttr;
327     SALOMEDS::AttributeName_var    aName;
328     if ( !obj->_is_nil() ) {
329       if (obj->FindAttribute(anAttr, "AttributeName")) {
330         aName = SALOMEDS::AttributeName::_narrow(anAttr);
331         QString nm = QString( aName->Value() );
332         nm = SALOMEGUI_NameDlg::getName( QAD_Application::getDesktop(), nm );
333         if ( !nm.isEmpty() ) {
334           // sak : 24.11.04 : fix for PAL6898 : if rename fails (study locked), 
335           // a message box is displayed, and cursor is "wait cursor".  We think that "wait cursor"
336           // is not neccessary here, because the rename operation is fast.
337           //QApplication::setOverrideCursor( Qt::waitCursor );
338           study->renameIObject( IObject, nm );
339           //QApplication::restoreOverrideCursor();
340         }
341       }
342     }
343   }
344 }
345
346 void SUPERVGUI::exportDataflow() {
347     Trace("SUPERVGUI::exportDataflow")
348     if ( main==0 ) {
349       QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
350     } 
351     else if ( SUPERV_isNull( main->getDataflow() ) ) { // should not normally happen..
352       QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
353     } 
354     else {
355       QString f = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
356                                            "",
357                                            "*.xml",
358                                            tr("TTL_EXPORT_DATAFLOW"),
359                                            false);
360       if ( !f.isEmpty() ) {
361         
362         // asv : bug [VSR Bugs and Improvements in Supervisor] 1.8 : when exporting a file, 
363         // a backup copy of an existing file must be created (in case Export fails..)
364         QString aBackupFile = SUPERVGUI::createBackupFile( f );
365
366         if ( main->getDataflow()->Export(f.latin1()) ) {
367           unregisterGraph(main);
368           registerGraph(f, main);
369
370           // remove a backup file if export was successfull
371           if ( !aBackupFile.isNull() && !aBackupFile.isEmpty() )
372             QFile::remove( aBackupFile );
373         }
374         else {
375           QMessageBox::warning(QAD_Application::getDesktop(), tr("ERROR"), tr("MSG_BAD_WRITING").arg(aBackupFile));
376         }
377       }
378     }
379 }
380
381 /** 
382  *If a file with theOriginalFileName exists, then the function tries to create 
383  * its backup copy of it (the_name + ".bakX", X = 0,1,2,..,99).  Returns backup file's name
384  * If a file with theOriginalFileName does not exist, or creation fails - Return empty string.
385  */
386 QString SUPERVGUI::createBackupFile( const QString& theOriginalFileName ) {
387   QString theBackupFileName("");
388   if ( QFile::exists( theOriginalFileName ) ) {
389     // 1. construct a backup file name
390     int i = 0;
391     do {
392       theBackupFileName = theOriginalFileName;
393       theBackupFileName += ".bak";
394       theBackupFileName += QString::number(i++);
395     } 
396     while ( QFile::exists( theBackupFileName ) && i < 100 ); // max 99 backup files
397     // if *.bak99 exists -- it will be overwritten 
398     
399     // 2. copy the original file to theBackupFileName
400     QString cmd( "cp \"");
401     cmd += theOriginalFileName;
402     cmd += "\" \"";
403     cmd += theBackupFileName;
404     cmd += "\"";
405     bool res = system( cmd.latin1() );
406     if ( res ) 
407       theBackupFileName = QString("");
408   }
409   return theBackupFileName;
410 }
411
412 void SUPERVGUI::newDataflow() {
413   createDataflow( New );
414 }
415
416 void SUPERVGUI::importDataflow() {
417   createDataflow( Import );
418 }
419
420 void SUPERVGUI::modifyDataflow() {
421   createDataflow( Modify );
422 }
423
424 bool SUPERVGUI::createDataflow( const NEW_DF_MODE mode ) {
425   Trace("SUPERVGUI::createDataflow, mode = " << mode );
426
427   // asv : 27.01.05 : fix for PAL7823 : if the study is locked - warn the user and return.
428   SALOMEDS::Study_var aStudy = study->getStudyDocument();
429   if ( aStudy->GetProperties()->IsLocked() ) {
430     QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"));
431     return false;
432   }
433
434   SUPERV::Graph_var aGraph;
435
436   QString f; // file name.  declared here because may be used below in error msg construction..
437   if ( mode == Import || mode == Modify ) { // 0. import or modify existing dataflow - select XML file
438     QString title = QString( "TLT_CREATE_DF_%1" ).arg( mode );
439     f = QAD_FileDlg::getFileName( QAD_Application::getDesktop(), "", "*.xml", tr( title ), true );
440     if ( f.isEmpty() ) // pressed Cancel in file select dialog
441       return false;
442     
443     // check if study already contains a graph imported from the same file
444     if ( isContains( study, f ) ) {
445       if ( QMessageBox::warning( QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_GRAPH_DISPLAYED").arg(f), 
446                                  QMessageBox::Yes, QMessageBox::No) == QMessageBox::No )
447         return false;
448     }
449     
450     // 1. create a graph
451     aGraph = ( mode == Import ) ? engine->StreamGraphE( f ) : engine->StreamGraph( f );        
452   }
453   else
454     aGraph = engine->StreamGraph( MAIN_NEW );
455
456   if ( CORBA::is_nil( aGraph ) ) {
457     QString msg( mode == New ? QString("MSG_CANT_CREATE_DF") : QString("MSG_BAD_FILE").arg(f) );
458     QAD_MessageBox::warn1( QAD_Application::getDesktop(), tr("ERROR"), tr(msg), tr("BUT_OK") );
459     return false;
460   }
461
462   // 2. create a ViewFrame object
463   QAD_StudyFrame* aStudyFrame = createGraph();
464   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>
465     (aStudyFrame->getRightFrame()->getViewFrame());
466   if ( aViewFrame ) {
467
468     // 3. bind Graph and ViewFrame == create SUPERVGUI_Main object
469     main = new SUPERVGUI_Main(aViewFrame, desktop, aGraph);
470     if ( mode == New )
471       main->addNode();
472     else
473       registerGraph( f, main );
474     study->showFrame(aStudyFrame);
475   }
476
477   return true;
478 }
479
480 void SUPERVGUI::reloadDataflow() {
481     Trace("SUPERVGUI::reloadDataflow")
482     if (main==0) {
483         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RELOAD"));
484     } else {
485         main->sync();
486     };
487 }
488
489 void SUPERVGUI::runDataflow() {
490     Trace("SUPERVGUI::runDataflow")
491     if (main==0) {
492         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RUN"));
493     } else {
494         main->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
495         main->run();
496     };
497 }
498
499 void SUPERVGUI::stepByStep() {
500     Trace("SUPERVGUI::stepByStep")
501     if (main==0) {
502         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RUN"));
503     } else {
504         main->Editing(); // PAL6170: GUI->Engine: setting "Editing" flag 
505         main->startExecute();
506     }
507 }
508
509 void SUPERVGUI::killDataflow() {
510     Trace("SUPERVGUI::killDataflow")
511     if (main==0) {
512         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_KILL"));
513     } else {
514         main->kill();
515     };
516 }
517
518 void SUPERVGUI::suspendResumeDataflow() {
519     Trace("SUPERVGUI::suspendResumeDataflow")
520     if (main==0) {
521         QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_SUSPEND"));
522     } else {
523         main->suspendResume();
524     };
525 }
526
527 void SUPERVGUI::showComponents() {
528   Trace("SUPERVGUI::showComponents");
529   if (main==0) {
530     QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_ADD"));
531   } else {
532     if (main->isEditable())
533       main->addNode();
534     else
535       QMessageBox::warning(QAD_Application::getDesktop(), tr("WARNING"), tr("MSG_NOTEDITABLE"));
536   }
537 }
538
539
540 bool SUPERVGUI::CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString& theContext, const QString& theParent, const QString& theObject) {
541   Trace("SUPERVGUI::customPopup");
542   Supervision.init(parent);
543   if(popup->count() > 3) {
544     // Temporary code: is to be deleted soon
545     popup->removeItem(QAD_Display_Popup_ID);
546     popup->removeItem(QAD_DisplayOnly_Popup_ID);
547     popup->removeItem(QAD_Erase_Popup_ID);
548     int id = popup->idAt(popup->count() - 1);
549     if (id < 0 && id != -1) popup->removeItem(id); // separator
550     // Temporary code end
551   }
552     SALOME_Selection* Sel = SALOME_Selection::Selection(study->getSelection() );
553     if ((Sel==NULL) || (Sel->IObjectCount() == 0)) return false;
554
555     if (Sel->IObjectCount() == 1) {
556       Handle(SALOME_InteractiveObject) anIObj = Sel->firstIObject();
557
558       // insert SUPERV-specific popup items here
559
560       bool isDataflow, ownObj;
561       whatIsSelected(anIObj, ownObj, isDataflow);
562       
563       if (isDataflow) {
564         popup->insertItem(tr( "MSG_RENAME" ), this, SLOT(renameDataflow()));
565         popup->insertItem("Display", this, SLOT(displayDataflow()));
566         popup->insertSeparator();
567       }
568       if (ownObj)
569         popup->insertItem(tr("MSG_DELETE"), this, SLOT(deleteObject()));
570     }
571     if (Sel->IObjectCount() > 1) {
572       int aIObjCount = 0;
573       SALOME_ListIteratorOfListIO It_forCheckOwner(Sel->StoredIObjects());
574       for(;It_forCheckOwner.More();It_forCheckOwner.Next()) {
575         Handle(SALOME_InteractiveObject) anIObj = It_forCheckOwner.Value();
576         bool aIsOwner, aIsDataflow;
577         whatIsSelected(anIObj, aIsOwner, aIsDataflow);
578         
579         if (aIsOwner) aIObjCount++;
580       }
581       if (aIObjCount == Sel->IObjectCount())  //all selected objects belong to Supervision
582         popup->insertItem(tr("MSG_DELETE"), this, SLOT(deleteObject()));
583     }
584   return(true);
585 }
586
587  
588
589 /** Returns:
590     theIsOwner = true if Selected object belongs to Suipervision.
591     theIsDataflow = true if Selected object is Dataflow
592 */
593 void SUPERVGUI::whatIsSelected(const Handle(SALOME_InteractiveObject)& theObj, bool& theIsOwner, bool& theIsDataflow) {
594   theIsOwner = false;
595   theIsDataflow = false;
596
597   if (theObj.IsNull()) return;
598   
599   SALOMEDS::Study_var aStudy = study->getStudyDocument();
600   SALOMEDS::SObject_var  obj = aStudy->FindObjectID( theObj->getEntry() );
601
602   if ( !obj->_is_nil() ) {
603     SALOMEDS::SComponent_var comp = obj->GetFatherComponent();
604     if ( !comp->_is_nil() ) {
605       SALOMEDS::GenericAttribute_var anAttr;
606       SALOMEDS::AttributeName_var    aName;
607       if (comp->FindAttribute(anAttr, "AttributeName")) {
608         aName = SALOMEDS::AttributeName::_narrow(anAttr);
609         QString compName(aName->Value());
610         //if (compName.compare(STUDY_SUPERVISION) == 0) {
611         if (compName.compare(QAD_Application::getDesktop()->getComponentUserName("SUPERV")) == 0) {
612           SALOMEDS::GenericAttribute_var anAttr;
613           if (obj->FindAttribute(anAttr, "AttributeIOR")) {
614             SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
615             //SUPERV_Graph aDataFlow = engine->getStreamGraph(anIOR->Value());
616             SUPERV_Graph aDataFlow = engine->getGraph(anIOR->Value());
617             if (!SUPERV_isNull(aDataFlow)) {
618               if (aDataFlow->IsStreamGraph()) {
619                 if (!SUPERV_isNull(aDataFlow->ToStreamGraph()))
620                   theIsDataflow = true;
621               }
622               else
623                 theIsDataflow = true;
624             }
625           }
626           CORBA::String_var anObjectID = obj->GetID();
627           CORBA::String_var aComponentID = comp->GetID();
628           if (strcmp(anObjectID, aComponentID)) theIsOwner = true;          
629         }
630       }
631     }
632   }
633 }
634
635 void SUPERVGUI::deleteObject() {
636   SALOME_Selection* Sel = SALOME_Selection::Selection(study->getSelection() );
637   if ((Sel==NULL) || (Sel->IObjectCount() == 0)) return;
638    
639   // sak : 24.11.04 : fix for PAL6899 : if the study is locked - warn the user and return.
640   SALOMEDS::Study_var aStudy = study->getStudyDocument();
641   if ( aStudy->GetProperties()->IsLocked() ) {
642     QMessageBox::warning(QAD_Application::getDesktop(), tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"));
643     return;
644   }
645
646   // sak : 24.11.04 : fix for PAL6901 : Incorrect deletion of the objects : pressing ESC button
647   // emitted "YES" button click, now it is corrected, and treated as "NO" button click.
648   if (QMessageBox::warning(QAD_Application::getDesktop(),
649                            tr("WARNING"),
650                            tr("MSG_ASK_DELETE"),
651                            QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes)
652     return;
653    
654   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
655   for(;It.More();It.Next()) {
656     Handle(SALOME_InteractiveObject) anIObj = It.Value();
657     bool aIsOwner, aIsDataflow;
658     whatIsSelected(anIObj, aIsOwner, aIsDataflow);
659     
660     SALOMEDS::SObject_var  aObj = aStudy->FindObjectID( anIObj->getEntry() );
661     if (!aObj->_is_nil()) {
662       if (aIsDataflow) {
663         SALOMEDS::GenericAttribute_var anAttr;
664         if (aObj->FindAttribute(anAttr, "AttributeIOR")) {
665           SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
666           QString ior = anIOR->Value();
667             
668           SUPERVGUI_Main* aMain;
669           for (aMain = myGraphList.first(); aMain; aMain = myGraphList.next()) {
670             if ((aMain->getStudy() == study) && (aMain->getHashCode() == ior)) {
671               aMain->setAsFromStudy(false);
672               break;
673             }
674           }
675         }
676       }
677       QAD_Operation* op = new SALOMEGUI_ImportOperation( study );
678       SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
679       op->start();
680       aBuilder->RemoveObjectWithChildren(aObj);
681       op->finish();
682     }
683   }
684   Sel->ClearIObjects() ; 
685 }
686
687 bool SUPERVGUI::OnGUIEvent(int command, QAD_Desktop* parent) {
688   Supervision.init(parent);
689   switch (command) {
690   case 301: 
691     Supervision.importDataflow();
692     return(false);
693     
694   case 302: 
695     Supervision.exportDataflow();
696     return(false);
697     
698   case 303: 
699     Supervision.newDataflow();
700     return(false);
701     
702   case 304: 
703     Supervision.modifyDataflow();
704     return(false);
705     
706   case 305:
707     Supervision.reloadDataflow();
708     return(false);
709     
710   case 306:
711     Supervision.runDataflow();
712     return(false);
713     
714   case 307: 
715     Supervision.killDataflow();
716     return(false);
717     
718   case 308:
719     Supervision.suspendResumeDataflow();
720     return(false);
721     
722   case 309: 
723     Supervision.showComponents();
724     return(false);
725     
726   case 310: 
727     Supervision.stepByStep();
728     return(false);
729     
730   default: 
731     QMessageBox::warning(QAD_Application::getDesktop(), "Supervision Error", "Unknown Command From Salome");
732     return(false);
733   }
734 }
735
736 bool SUPERVGUI::SetSettings(QAD_Desktop* parent) {
737   Trace("SUPERVGUI::SetSettings");
738   Supervision.init(parent);
739   return(true);
740 }
741
742 void SUPERVGUI::DefinePopup(QString& theContext, QString& theParent, QString& theObject) {
743   theObject  = "";
744   theContext = "";
745 }
746
747 bool SUPERVGUI::ActiveStudyChanged(QAD_Desktop* parent) {
748   Trace("SUPERVGUI::activeStudyChanged");
749   study = desktop->getActiveStudy();
750   return true;
751 }
752
753 void SUPERVGUI::SupportedViewType (int* buffer, int bufferSize)
754 {
755   if (!buffer || !bufferSize) return;
756   buffer[0] = (int)VIEW_GRAPHSUPERV;
757 }
758
759 extern "C"
760 {
761   Standard_EXPORT SALOMEGUI* GetComponentGUI() {
762     return &Supervision;
763   }
764 }