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