]> SALOME platform Git repositories - modules/superv.git/blob - src/SUPERVGUI/SUPERVGUI.cxx
Salome HOME
Fix for bug IPAL9214 : 3.0.0(/dn06/../current1506): dataflow object has no "Rename...
[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 #include "SUPERVGUI_DataModel.h"
31 #include "SUPERVGUI_Selection.h"
32
33 #include "SALOMEDSClient.hxx"
34 #include <boost/shared_ptr.hpp>
35 using namespace boost;
36
37 #include "CAM_Application.h"
38 #include "OB_Browser.h"
39 #include "OB_ListItem.h"
40 #include "SalomeApp_Application.h"
41 #include "SalomeApp_ImportOperation.h"
42 #include "SalomeApp_NameDlg.h"
43 #include "SalomeApp_SelectionMgr.h"
44 #include "SalomeApp_Study.h"
45 #include "SUIT_Desktop.h"
46 #include "SUIT_FileDlg.h"
47 #include "SUIT_MessageBox.h"
48 #include "SUIT_Operation.h"
49 #include "SUIT_ResourceMgr.h"
50 #include "SUIT_Session.h"
51 #include "SUIT_Study.h"
52
53 #include "SUPERVGraph_ViewManager.h"
54
55 #include "SALOME_ListIO.hxx"
56 #include "SALOME_ListIteratorOfListIO.hxx"
57 #include "SALOME_InteractiveObject.hxx"
58 #include "Utils_ORB_INIT.hxx"
59
60 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
61
62 #include <qmenubar.h>
63
64 int SUPERVGUI::factory = 0;
65
66 extern "C" {
67   Standard_EXPORT CAM_Module* createModule() {
68     return new SUPERVGUI();
69   }
70 }
71
72
73 SUPERVGUI::SUPERVGUI()
74      : SalomeApp_Module( "SUPERV" ),
75        study(0),
76        browser(0)
77   //info(0)
78 {
79     Trace("SUPERVGUI::SUPERVGUI")
80     if (factory==0) {
81         factory = 1;
82         MESSAGE("SUPERVGUI Info : factory build");
83     } else {
84         MESSAGE("SUPERVGUI Error : another call to factory constructor");
85     };
86 }
87
88 SUPERVGUI::~SUPERVGUI() {
89     Trace("SUPERVGUI::~SUPERVGUI")
90     if (factory==1) {
91         factory = 0;
92         //if (!engine->_is_nil()) CORBA::release(engine);         
93         //info->close();
94         if (browser) {
95           if (browser->isVisible())
96             browser->close();
97           delete browser;
98         }
99         MESSAGE("SUPERVGUI Info : factory destroy");
100     } else {
101         MESSAGE("SUPERVGUI Error : another call to factory destructor");
102     };
103 }
104
105 SUPERVGUI* SUPERVGUI::Supervision() {
106   CAM_Application* anApp = dynamic_cast<CAM_Application*>( SUIT_Session::session()->activeApplication() );
107   if ( anApp ) {
108     SUPERVGUI* aSupervModule = dynamic_cast<SUPERVGUI*>( anApp->activeModule() );
109     return aSupervModule;
110   }
111   return NULL;
112 }
113
114 void SUPERVGUI::createSupervAction( const int id, const QString& po_id, const QString& icon_id, const int key, const bool toggle  )
115 {
116   QIconSet icon;
117   QWidget* parent = application()->desktop();
118   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
119   QPixmap pix;
120   if ( icon_id.length() ) 
121     pix = resMgr->loadPixmap( "SUPERV", tr( icon_id ) );
122   else
123     pix = resMgr->loadPixmap( "SUPERV", tr( QString( "ICO_" )+po_id ) );
124   if ( !pix.isNull() )
125     icon = QIconSet( pix );
126
127   QString tooltip    = tr( QString( "TOP_" )+po_id ),
128           menu       = tr( QString( "MEN_" )+po_id ),
129           status_bar = tr( QString( "STB_" )+po_id );
130
131   createAction( id, tooltip, icon, menu, status_bar, key, parent, toggle, this, SLOT( OnGUIEvent() )  );
132 }
133
134 void SUPERVGUI::initialize( CAM_Application* app )
135 {
136   SalomeApp_Module::initialize( app );
137
138   SALOME_NamingService* namingService = (( SalomeApp_Application* )application())->namingService();
139   loadEngine(namingService);
140
141   browser = new SUPERVGUI_Service(namingService);
142   //info    = new SUPERVGUI_Information();
143   cursor  = application()->desktop()->cursor();
144
145   fillInterfaceNameMap();
146
147   // ----- create actions --------------
148   createSupervAction( 301, "IMPORT" );
149   createSupervAction( 302, "EXPORT" );
150
151   createSupervAction( 303, "NEW" );
152   createSupervAction( 304, "MODIFY" );
153
154   createSupervAction( 305, "RELOAD" );
155   createSupervAction( 306, "RUN" );
156   createSupervAction( 307, "KILL" );
157   createSupervAction( 308, "SUSPEND/RESUME" );
158   createSupervAction( 309, "ADDNODE" );
159   createSupervAction( 310, "STEPBYSTEP" );
160
161   createSupervAction( 311, "POP_RENAME" );
162   createSupervAction( 312, "POP_DISPLAY" );
163   createSupervAction( 313, "POP_DELETE" );
164
165   // ----- create menu -----------------
166   int fileId = createMenu( tr( "MEN_FILE" ), -1, -1 );
167   createMenu( 301, fileId, 10 );
168   createMenu( 302, fileId, 10 );
169   createMenu( separator(), fileId, 10 );
170
171   int editId = createMenu( tr( "MEN_EDIT" ), -1, -1 );
172   createMenu( separator(), fileId, 7 );
173   createMenu( 303, editId, -1 );
174   createMenu( 304, editId, -1 );
175
176   int supervId = createMenu( tr( "MEN_SUPERV" ), -1, -1, 10/*, 300*/ );
177   createMenu( 305, supervId, -1 );
178   createMenu( 309, supervId, -1 );
179   createMenu( separator(), fileId, 3 );
180   createMenu( 306, supervId, -1 );
181   createMenu( 310, supervId, -1 );
182   createMenu( 307, supervId, -1 );
183   createMenu( 308, supervId, -1 );     
184
185   // ----- create toolbars -------------
186   int supervisorId = createTool( tr( "TOOL_SUPERVISOR" ) );
187   createTool( 301, supervisorId );
188   createTool( 302, supervisorId );
189   createTool( 303, supervisorId );
190   createTool( 304, supervisorId );
191   createTool( 305, supervisorId );
192   createTool( 309, supervisorId );
193
194   int executionId = createTool( tr( "TOOL_EXECUTION" ) );
195   createTool( 306, executionId );
196   createTool( 310, executionId );
197   createTool( 307, executionId );
198   createTool( 308, executionId );
199
200   // ----- create popup for object browser items -------------
201   QtxPopupMgr* mgr = popupMgr();
202   mgr->insert( action(  311 ), -1, -1 ); // rename dataflow
203   mgr->setRule( action( 311 ), "$type in {'Dataflow'} and selcount=1", true );
204   mgr->insert( action(  312 ), -1, -1 ); // display dataflow
205   mgr->setRule( action( 312 ), "$type in {'Dataflow'} and selcount=1", true );
206   mgr->insert( separator(), -1, -1 ); // -----------
207   mgr->insert( action(  313 ), -1, -1 ); // delete object
208   mgr->setRule( action( 313 ), "$client in {'ObjectBrowser'} and $type in {'Dataflow' 'SupervisorObject'} and selcount>0", true );
209 }
210
211 bool SUPERVGUI::activateModule( SUIT_Study* theStudy )
212 {
213   if ( CORBA::is_nil( engine ) )
214     return false;
215
216   if ( !SalomeApp_Module::activateModule( theStudy ) )
217     return false;
218
219
220
221   //---------------------------------------
222   setMenuShown( true );
223   setToolShown( true );
224
225   study = application()->activeStudy();
226
227   connect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ), 
228            this, SLOT( setMain( SUIT_ViewWindow* ) ) );
229
230   return true;
231 }
232
233 QString SUPERVGUI::engineIOR() const 
234 {
235  if ( !CORBA::is_nil( engine ) )
236     return QString( getApp()->orb()->object_to_string( engine));
237   return QString( "" );
238 }
239
240 bool SUPERVGUI::deactivateModule( SUIT_Study* theStudy )
241 {
242   setMenuShown( false );
243   setToolShown( false );
244
245   disconnect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ), 
246               this, SLOT( setMain( SUIT_ViewWindow* ) ) );
247   //---------------------------------------
248
249   return SalomeApp_Module::deactivateModule( theStudy );
250 }
251
252 CAM_DataModel* SUPERVGUI::createDataModel()
253 {
254   return new SUPERVGUI_DataModel( this );
255 }
256
257 SalomeApp_Selection* SUPERVGUI::createSelection() const
258 {
259   return new SUPERVGUI_Selection();
260 }
261
262 SUIT_ViewWindow* SUPERVGUI::createGraph() {
263   SUPERVGraph_ViewManager* aVM = new SUPERVGraph_ViewManager( study, application()->desktop(), new SUPERVGraph_Viewer() );
264   SUIT_ViewWindow* aVW = aVM->createViewWindow();
265   if ( aVW ) {
266     connect( aVM, 
267              SIGNAL( deleteView( SUIT_ViewWindow* ) ),
268              this,
269              SLOT( onGraphClosed( SUIT_ViewWindow* ) ) );
270   }
271   return aVW;
272 }
273
274
275
276 void SUPERVGUI::loadEngine(SALOME_NamingService* namingService) {
277     Trace("SUPERVGUI::loadEngine")
278     SALOME_LifeCycleCORBA LCC(namingService);
279     Engines::Component_var objComponent = LCC.FindOrLoad_Component("SuperVisionContainer", "SUPERV");
280     if (CORBA::is_nil(objComponent)) {
281         QMessageBox::warning(application()->desktop(), tr("ERROR"), tr("MSG_CANT_LOAD_SUPERV"));
282         return;
283     };
284
285     SUPERV::SuperG_var aSuperVisionComponent;
286     aSuperVisionComponent = SUPERV::SuperG::_narrow(objComponent);
287     if (CORBA::is_nil(aSuperVisionComponent)) {
288         QMessageBox::warning(application()->desktop(), tr("ERROR"), tr("MSG_CANT_NARROW_SUPERV"));
289         return;
290     };
291
292     //    engine  = new SUPERV::SuperG_var;
293     engine = aSuperVisionComponent;
294 }
295
296 /*
297 bool SUPERVGUI::information(SUPERV_CNode node, bool isReadOnly) {
298     Trace("SUPERVGUI::information")
299     return(info->run(node, isReadOnly));
300 }
301 */
302 void SUPERVGUI::putInfo(const char* mess) {
303     Trace("SUPERVGUI::putInfo")
304     application()->putInfo(mess);
305 }
306
307 SUPERV_Engine SUPERVGUI::getEngine() {
308     Trace("SUPERVGUI::getEngine")
309     return(engine);
310 }
311
312 SUPERVGUI_Service* SUPERVGUI::getBrowser() {
313     Trace("SUPERVGUI::getBrowser")
314     return(browser);
315 }
316
317 QCursor SUPERVGUI::getCursor() {
318     Trace("SUPERVGUI::getCursor")
319     return(cursor);
320 }
321
322 SUPERVGUI_Main* SUPERVGUI::getMain() {
323     Trace("SUPERVGUI::getMain")
324     return main;
325 }
326
327
328 void SUPERVGUI::registerGraph(QString theName, SUPERVGUI_Main* theGraph){
329   theGraph->setHashCode(theName);
330   myGraphList.append(theGraph);
331 }
332
333
334 void SUPERVGUI::unregisterGraph(SUPERVGUI_Main* theGraph) {
335   SUPERVGUI_Main* aMain;
336   for (aMain = myGraphList.first(); aMain; aMain = myGraphList.next()) {
337     if (aMain == theGraph)
338       myGraphList.remove(aMain);
339   }
340 }
341
342
343 bool SUPERVGUI::isContains(SUIT_Study* theStudy, const QString theKey) {
344   SUPERVGUI_Main* aMain;
345   for (aMain = myGraphList.first(); aMain; aMain = myGraphList.next()) {
346     if ((aMain->getStudy() == theStudy) && (aMain->getHashCode() == theKey))
347       return true;
348   }
349   return false;
350 }
351
352
353 void SUPERVGUI::onGraphClosed(SUIT_ViewWindow* theViewWindow) {
354   if( SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(theViewWindow) ) {
355     disconnect(supervFrame->getViewManager(), 0 , this, 0);
356     SUPERVGraph_View* view = supervFrame->getViewWidget();
357     SUPERVGUI_Main* aGraph = dynamic_cast<SUPERVGUI_Main*>(view);
358     if ( aGraph ) {
359       unregisterGraph(aGraph);
360       SUPERV_Graph aDataFlow = aGraph->getDataflow();
361       if ( !SUPERV_isNull( aDataFlow ) ) {
362         if ( aDataFlow->IsExecuting() ) {
363           const int aMsgResult = QMessageBox::warning(application()->desktop(),
364                                                       tr("WARNING"), 
365                                                       tr("MSG_DF_RUNNING"), 
366                                                       tr("MSG_DF_EXECUTION"), 
367                                                       tr("MSG_DF_KILL"));
368           if ( aMsgResult == 1 ) {
369             // KILL EXECUTION
370             if ( aDataFlow->Kill() ) {
371               // Kill() sends KillEvent and KillState to SUPERVGUI_Thread
372               // while sets myIsActive flag to false when it receives such event/state
373               // after myIsActive is false it calls QThread::exit() to terminate.
374
375               // why while() { qApp->processEvents() } ?
376               // because: SUPERVGUI_Thread::run() receives events, and calls myMain->execute()
377               // method using SALOME_Event paradigm, ProcessVoidEvent() function - 
378               // it puts this event to the main application event loop, in which we are being now.
379               // So if we block main GUI application thread (by calling aGraph->getMyThread()->wait() here)
380               // then we will have a deadlock of 2 threads waiting for one another 
381               while (aGraph->getMyThread()->running()) 
382                 qApp->processEvents();
383             }
384           }
385           else { // BACKGROUND EXECUTION
386             emit KillMainThread(true); // set SUPERVGUI_Thread::myIsActive to false
387             while ( aGraph->getMyThread()->running() )
388               qApp->processEvents();
389           }
390         }
391         else { // EXECUTION IS FINISHED, JUST DESTROY THE SUPERVGUI_Thread object and return.
392           emit KillMainThread(true);
393           while ( aGraph->getMyThread()->running() ) 
394             qApp->processEvents();
395         }
396       }
397     }
398   }
399 }
400
401 void SUPERVGUI::setMain( SUIT_ViewWindow* w) {
402   main = 0;
403   if ( !w ) return;
404
405   study = w->getViewManager()->study();
406   if (!study)
407     return;
408
409   if(SUPERVGraph_ViewFrame* supervFrame = dynamic_cast<SUPERVGraph_ViewFrame*>(w)){
410     SUPERVGraph_View* view = supervFrame->getViewWidget();
411     if (view) {
412       main = dynamic_cast<SUPERVGUI_Main*>(view);
413       connect(this, SIGNAL(KillMainThread(bool)), main->getMyThread(), SLOT(KillThread(bool)));
414     }
415   }
416   Trace("SUPERVGUI::setMain - main: "<<main);
417 }
418
419 void SUPERVGUI::displayDataflow() {
420     Trace("SUPERVGUI::displayDataflow");
421     OB_Browser* aBrowser = (( SalomeApp_Application* )application())->objectBrowser();
422     SUPERV_Graph aDataFlow;
423     QString aIORName;
424
425     OB_ListItem* item = (OB_ListItem*)(aBrowser->listView()->currentItem());
426     _PTR(SObject) aObj 
427       ( (( SalomeApp_Study* )(application()->activeStudy()))->studyDS()->FindObjectID(item->text(2).latin1()) );
428     _PTR(GenericAttribute) anAttr;
429     if (aObj->FindAttribute(anAttr, "AttributeIOR")) {
430       _PTR(AttributeIOR) anIOR ( anAttr );
431       aIORName = QString(anIOR->Value().c_str());
432       if (isContains(study, aIORName)) {
433         if (QMessageBox::warning(application()->desktop(), tr("WARNING"), 
434                                  tr("MSG_GRAPH_DISPLAYED").arg(""), 
435                                  QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
436           return;
437       }
438       //aDataFlow = engine->getStreamGraph(anIOR->Value().c_str());
439       aDataFlow = engine->getGraph(anIOR->Value().c_str());
440       if (SUPERV_isNull(aDataFlow)) {
441         QMessageBox::warning(0, tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
442         return;
443       }
444       /* ASV: 20.10.04: fix for 6896
445       if (aDataFlow->IsStreamGraph()) {
446         SUPERV_StreamGraph aDataFlowStream = aDataFlow->ToStreamGraph();
447         if (SUPERV_isNull(aDataFlowStream)) {
448           QMessageBox::warning(0, tr("ERROR"), tr("MSG_ACCESS_BAD_IOR"));
449           return;
450         }
451         aDataFlow = aDataFlowStream;
452       }
453       */
454     }
455     SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>( createGraph() );
456     if( aViewFrame ) {
457       main = new SUPERVGUI_Main( aViewFrame, application()->desktop(), aDataFlow );
458       registerGraph( aIORName, main );
459       aViewFrame->show();
460     }
461 }
462
463
464 void SUPERVGUI::renameDataflow() {
465   SALOME_ListIO aList;
466   aList.Clear();
467   (( SalomeApp_Application* )application())->selectionMgr()->selectedObjects( aList );
468
469   SALOME_ListIteratorOfListIO It( aList );
470   for ( ; It.More(); It.Next() ) {
471     Handle(SALOME_InteractiveObject) IObject = It.Value();
472     
473     _PTR(Study) aStudy = (( SalomeApp_Study* )study)->studyDS();
474     _PTR(SObject) obj ( aStudy->FindObjectID( IObject->getEntry() ) );
475     _PTR(GenericAttribute) anAttr;
476     if ( obj ) {
477       if ( obj->FindAttribute(anAttr, "AttributeName") ) {
478         _PTR(AttributeName) aName ( anAttr );
479         QString nm = QString( aName->Value().c_str() );
480         nm = SalomeApp_NameDlg::getName( application()->desktop(), nm );
481         if ( !nm.isEmpty() ) {
482           // sak : 24.11.04 : fix for PAL6898 : if rename fails (study locked), 
483           // a message box is displayed, and cursor is "wait cursor".  We think that "wait cursor"
484           // is not neccessary here, because the rename operation is fast.
485           //QApplication::setOverrideCursor( Qt::waitCursor );
486           //study->renameIObject( IObject, nm );
487
488           //--->
489           aName->SetValue( nm.latin1() );
490           //rename Interactive object 
491           IObject->setName( ( char* )nm.latin1() );
492           //<---
493
494           updateObjBrowser();
495           //QApplication::restoreOverrideCursor();
496         }
497       }
498     }
499   }
500 }
501
502 void SUPERVGUI::exportDataflow() {
503     Trace("SUPERVGUI::exportDataflow")
504     if ( main==0 ) {
505       QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
506     } 
507     else if ( SUPERV_isNull( main->getDataflow() ) ) { // should not normally happen..
508       QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_EXPORT"));
509     } 
510     else {
511       QString f = SUIT_FileDlg::getFileName(application()->desktop(),
512                                             "",
513                                             "*.xml",
514                                             tr("TTL_EXPORT_DATAFLOW"),
515                                             false);
516       if ( !f.isEmpty() ) {
517         
518         // asv : bug [VSR Bugs and Improvements in Supervisor] 1.8 : when exporting a file, 
519         // a backup copy of an existing file must be created (in case Export fails..)
520         QString aBackupFile = SUPERVGUI::createBackupFile( f );
521
522         if ( main->getDataflow()->Export(f.latin1()) ) {
523           unregisterGraph(main);
524           registerGraph(f, main);
525
526           // remove a backup file if export was successfull
527           if ( !aBackupFile.isNull() && !aBackupFile.isEmpty() )
528             QFile::remove( aBackupFile );
529         }
530         else {
531           QMessageBox::warning(application()->desktop(), tr("ERROR"), tr("MSG_BAD_WRITING").arg(aBackupFile));
532         }
533       }
534     }
535 }
536
537 /** 
538  *If a file with theOriginalFileName exists, then the function tries to create 
539  * its backup copy of it (the_name + ".bakX", X = 0,1,2,..,99).  Returns backup file's name
540  * If a file with theOriginalFileName does not exist, or creation fails - Return empty string.
541  */
542 QString SUPERVGUI::createBackupFile( const QString& theOriginalFileName ) {
543   QString theBackupFileName("");
544   if ( QFile::exists( theOriginalFileName ) ) {
545     // 1. construct a backup file name
546     int i = 0;
547     do {
548       theBackupFileName = theOriginalFileName;
549       theBackupFileName += ".bak";
550       theBackupFileName += QString::number(i++);
551     } 
552     while ( QFile::exists( theBackupFileName ) && i < 100 ); // max 99 backup files
553     // if *.bak99 exists -- it will be overwritten 
554     
555     // 2. copy the original file to theBackupFileName
556     QString cmd( "cp \"");
557     cmd += theOriginalFileName;
558     cmd += "\" \"";
559     cmd += theBackupFileName;
560     cmd += "\"";
561     bool res = system( cmd.latin1() );
562     if ( res ) 
563       theBackupFileName = QString("");
564   }
565   return theBackupFileName;
566 }
567
568 void SUPERVGUI::windows( QMap<int, int>& mappa ) const
569 {
570   mappa.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
571   mappa.insert( SalomeApp_Application::WT_PyConsole, Qt::DockBottom );
572   mappa.insert( SalomeApp_Application::WT_LogWindow, Qt::DockBottom );
573 }
574
575 void SUPERVGUI::newDataflow() {
576   createDataflow( New );
577 }
578
579 void SUPERVGUI::importDataflow() {
580   createDataflow( Import );
581 }
582
583 void SUPERVGUI::modifyDataflow() {
584   createDataflow( Modify );
585 }
586
587 bool SUPERVGUI::createDataflow( const NEW_DF_MODE mode ) {
588   Trace("SUPERVGUI::createDataflow, mode = " << mode );
589
590   // asv : 27.01.05 : fix for PAL7823 : if the study is locked - warn the user and return.
591   _PTR(Study) aStudy = (( SalomeApp_Study* )study)->studyDS();
592   if ( aStudy->GetProperties()->IsLocked() ) {
593     QMessageBox::warning(application()->desktop(), tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"));
594     return false;
595   }
596
597   SUPERV::Graph_var aGraph;
598
599   QString f; // file name.  declared here because may be used below in error msg construction..
600   if ( mode == Import || mode == Modify ) { // 0. import or modify existing dataflow - select XML file
601     QString title = QString( "TLT_CREATE_DF_%1" ).arg( mode );
602     f = SUIT_FileDlg::getFileName( application()->desktop(), "", "*.xml", tr( title ), true );
603     if ( f.isEmpty() ) // pressed Cancel in file select dialog
604       return false;
605     
606     // check if study already contains a graph imported from the same file
607     if ( isContains( study, f ) ) {
608       if ( QMessageBox::warning( application()->desktop(), tr("WARNING"), tr("MSG_GRAPH_DISPLAYED").arg(f), 
609                                  QMessageBox::Yes, QMessageBox::No) == QMessageBox::No )
610         return false;
611     }
612     
613     // 1. create a graph
614     aGraph = ( mode == Import ) ? engine->StreamGraphE( f ) : engine->StreamGraph( f );        
615   }
616   else
617     aGraph = engine->StreamGraph( MAIN_NEW );
618
619   if ( CORBA::is_nil( aGraph ) ) {
620     QString msg( mode == New ? tr("MSG_CANT_CREATE_DF") : tr("MSG_BAD_FILE").arg(f) );
621     SUIT_MessageBox::warn1( application()->desktop(), tr("ERROR"), tr(msg), tr("BUT_OK") );
622     return false;
623   }
624
625   // 2. create a ViewFrame object
626   SUPERVGraph_ViewFrame* aViewFrame = dynamic_cast<SUPERVGraph_ViewFrame*>( createGraph() );
627   if ( aViewFrame ) {
628
629     // 3. bind Graph and ViewFrame == create SUPERVGUI_Main object
630     main = new SUPERVGUI_Main( aViewFrame, application()->desktop(), aGraph );
631     if ( mode == New )
632       main->addNode();
633     else
634       registerGraph( f, main );
635     aViewFrame->show();
636   }
637
638   return true;
639 }
640
641 void SUPERVGUI::reloadDataflow() {
642   Trace("SUPERVGUI::reloadDataflow");
643   if ( main )  
644     main->sync();
645   else 
646     QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RELOAD"));
647 }
648
649 void SUPERVGUI::runDataflow() {
650   Trace("SUPERVGUI::runDataflow");
651   if ( main )
652     main->run( /*andSuspend=*/false );
653   else
654     QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RUN"));
655 }
656
657 void SUPERVGUI::stepByStep() {
658   Trace("SUPERVGUI::stepByStep");
659   if ( main )
660     main->run( /*andSuspend=*/true );
661   else
662     QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_RUN"));
663 }
664
665 void SUPERVGUI::killDataflow() {
666   Trace("SUPERVGUI::killDataflow");
667   if ( main )
668     main->kill();
669   else
670     QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_KILL"));
671 }
672
673 void SUPERVGUI::suspendResumeDataflow() {
674   Trace("SUPERVGUI::suspendResumeDataflow");
675   if ( main )
676     main->suspendResume();
677   else
678     QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_SUSPEND"));
679 }
680
681 void SUPERVGUI::showComponents() {
682   Trace("SUPERVGUI::showComponents");
683   if ( main ) {
684     if ( main->isEditable() )
685       main->addNode();
686     else
687       QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOTEDITABLE"));
688   }
689   else
690     QMessageBox::warning(application()->desktop(), tr("WARNING"), tr("MSG_NOWINDOW_TO_ADD"));
691
692 }
693
694
695 /*bool SUPERVGUI::CustomPopup(QPopupMenu* popup, const QString& theContext, const QString& theParent, const QString& theObject) {
696   Trace("SUPERVGUI::customPopup");
697   Supervision.init();
698   if(popup->count() > 3) {
699     // Temporary code: is to be deleted soon
700     popup->removeItem(QAD_Display_Popup_ID);
701     popup->removeItem(QAD_DisplayOnly_Popup_ID);
702     popup->removeItem(QAD_Erase_Popup_ID);
703     int id = popup->idAt(popup->count() - 1);
704     if (id < 0 && id != -1) popup->removeItem(id); // separator
705     // Temporary code end
706   }
707   
708   SALOME_ListIO aList;
709   aList.Clear();
710   (( SalomeApp_Application* )application())->selectionMgr()->selectedObjects( aList );
711   if ( aList.Extent() == 0 ) return false;
712   
713   if ( aList.Extent() == 1 ) {
714     Handle(SALOME_InteractiveObject) anIObj = aList.First();
715     
716     // insert SUPERV-specific popup items here
717     
718     bool isDataflow, ownObj;
719     whatIsSelected(anIObj, ownObj, isDataflow);
720     
721     if (isDataflow) {
722       popup->insertItem(tr( "MSG_RENAME" ), this, SLOT(renameDataflow()));
723       popup->insertItem("Display", this, SLOT(displayDataflow()));
724       popup->insertSeparator();
725     }
726     if (ownObj)
727       popup->insertItem(tr("MSG_DELETE"), this, SLOT(deleteObject()));
728   }
729
730   if ( aList.Extent() > 1 ) {
731     int aIObjCount = 0;
732     SALOME_ListIteratorOfListIO It_forCheckOwner( aList );
733     for(;It_forCheckOwner.More();It_forCheckOwner.Next()) {
734       Handle(SALOME_InteractiveObject) anIObj = It_forCheckOwner.Value();
735       bool aIsOwner, aIsDataflow;
736       whatIsSelected(anIObj, aIsOwner, aIsDataflow);
737       
738       if (aIsOwner) aIObjCount++;
739     }
740     if ( aIObjCount == aList.Extent() )  //all selected objects belong to Supervision
741       popup->insertItem(tr("MSG_DELETE"), this, SLOT(deleteObject()));
742   }
743
744   return(true);
745   }*/
746
747  
748
749 /** Returns:
750     theIsOwner = true if Selected object belongs to Supervision.
751     theIsDataflow = true if Selected object is Dataflow
752 */
753 void SUPERVGUI::whatIsSelected(const _PTR(SObject)& theObj, bool& theIsOwner, bool& theIsDataflow) {
754   theIsOwner = false;
755   theIsDataflow = false;
756
757   if ( theObj ) {
758     _PTR(SComponent) comp ( theObj->GetFatherComponent() );
759     if ( comp ) {
760       _PTR(GenericAttribute) anAttr;
761       if (comp->FindAttribute(anAttr, "AttributeName")) {
762         _PTR(AttributeName) aName ( anAttr );
763         QString compName(aName->Value().c_str());
764         if ( compName.compare( moduleName() ) == 0 ) {
765           _PTR(GenericAttribute) anAttr;
766           if (theObj->FindAttribute(anAttr, "AttributeIOR")) {
767             _PTR(AttributeIOR) anIOR ( anAttr );
768             SUPERV_Graph aDataFlow = engine->getGraph(anIOR->Value().c_str());
769             if (!SUPERV_isNull(aDataFlow)) {
770               if (aDataFlow->IsStreamGraph()) {
771                 if (!SUPERV_isNull(aDataFlow->ToStreamGraph()))
772                   theIsDataflow = true;
773               }
774               else
775                 theIsDataflow = true;
776             }
777           }
778           CORBA::String_var anObjectID = theObj->GetID().c_str();
779           CORBA::String_var aComponentID = comp->GetID().c_str();
780           if (strcmp(anObjectID, aComponentID))
781             theIsOwner = true;      
782         }
783       }
784     }
785   }
786 }
787
788 void SUPERVGUI::deleteObject() {
789   SALOME_ListIO aList;
790   aList.Clear();
791   (( SalomeApp_Application* )application())->selectionMgr()->selectedObjects( aList );
792   if ( aList.Extent() == 0 ) return;
793    
794   // sak : 24.11.04 : fix for PAL6899 : if the study is locked - warn the user and return.
795   _PTR(Study) aStudy = (( SalomeApp_Study* )study)->studyDS();
796   if ( aStudy->GetProperties()->IsLocked() ) {
797     QMessageBox::warning(application()->desktop(), tr("WRN_WARNING"), tr("WRN_STUDY_LOCKED"));
798     return;
799   }
800
801   // sak : 24.11.04 : fix for PAL6901 : Incorrect deletion of the objects : pressing ESC button
802   // emitted "YES" button click, now it is corrected, and treated as "NO" button click.
803   if (QMessageBox::warning(application()->desktop(),
804                            tr("WARNING"),
805                            tr("MSG_ASK_DELETE"),
806                            QMessageBox::No, QMessageBox::Yes) != QMessageBox::Yes)
807     return;
808    
809   SALOME_ListIteratorOfListIO It( aList );
810   for(;It.More();It.Next()) {
811     Handle(SALOME_InteractiveObject) anIObj = It.Value();
812     //bool aIsOwner, aIsDataflow;
813     //whatIsSelected(anIObj, aIsOwner, aIsDataflow);
814     
815     _PTR(SObject) aObj ( aStudy->FindObjectID( anIObj->getEntry() ) );
816     if ( aObj ) {
817       SUIT_Operation* op = new SalomeApp_ImportOperation( application() );
818       _PTR(StudyBuilder) aBuilder ( aStudy->NewBuilder() );
819       op->start();
820       aBuilder->RemoveObjectWithChildren( aObj );
821       op->commit();
822     }
823   }
824   (( SalomeApp_Application* )application())->selectionMgr()->clearSelected();
825   updateObjBrowser();
826 }
827
828 void SUPERVGUI::OnGUIEvent()
829 {
830   const QObject* obj = sender();
831   if ( !obj || !obj->inherits( "QAction" ) )
832     return;
833   int id = actionId((QAction*)obj);
834   if ( id != -1 )
835     OnGUIEvent( id );
836 }
837
838 bool SUPERVGUI::OnGUIEvent(int command) {
839   switch (command) {
840   case 301: 
841     importDataflow();
842     return(false);
843     
844   case 302: 
845     exportDataflow();
846     return(false);
847     
848   case 303: 
849     newDataflow();
850     return(false);
851     
852   case 304: 
853     modifyDataflow();
854     return(false);
855     
856   case 305:
857     reloadDataflow();
858     return(false);
859     
860   case 306:
861     runDataflow();
862     return(false);
863     
864   case 307: 
865     killDataflow();
866     return(false);
867     
868   case 308:
869     suspendResumeDataflow();
870     return(false);
871     
872   case 309: 
873     showComponents();
874     return(false);
875     
876   case 310: 
877     stepByStep();
878     return(false);
879
880   case 311:
881     renameDataflow();
882     return(false);
883
884   case 312:
885     displayDataflow();
886     return(false);
887
888   case 313:
889     deleteObject();
890     return(false);
891
892   default: 
893     QMessageBox::warning(application()->desktop(), "Supervision Error", "Unknown Command From Salome");
894     return(false);
895   }
896 }
897
898 /*bool SUPERVGUI::SetSettings() {
899   Trace("SUPERVGUI::SetSettings");
900   Supervision.init();
901   return(true);
902   }*/
903
904 /*void SUPERVGUI::DefinePopup(QString& theContext, QString& theParent, QString& theObject) {
905   theObject  = "";
906   theContext = "";
907   }*/
908
909 bool SUPERVGUI::ActiveStudyChanged() {
910   Trace("SUPERVGUI::activeStudyChanged");
911   study = application()->activeStudy();
912   return true;
913 }
914
915 /*void SUPERVGUI::SupportedViewType (int* buffer, int bufferSize)
916 {
917   if (!buffer || !bufferSize) return;
918   buffer[0] = (int)VIEW_GRAPHSUPERV;
919 }*/
920
921 void SUPERVGUI::fillInterfaceNameMap() {
922   //fill map: interface_name <-> component_name for all
923   //interfaces of all loaded components, it is necessary
924   //for correct adding factory nodes from AddComponent,
925   //SubComponent, MulComponent, etc. components.
926   myInterfaceNameMap.clear();
927
928   //get naming service
929   SALOME_NamingService* namingService = (( SalomeApp_Application* )application())->namingService();
930   //get module catalog
931   CORBA::Object_ptr obj  = namingService->Resolve("/Kernel/ModulCatalog");
932   SALOME_ModuleCatalog::ModuleCatalog_var* aModuleCatalog = new SALOME_ModuleCatalog::ModuleCatalog_var;
933   *aModuleCatalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj);
934   if (!CORBA::is_nil(*aModuleCatalog)) {
935     //get component list
936     SALOME_ModuleCatalog::ListOfComponents_var aCompList = (*aModuleCatalog)->GetComponentList();
937     for (int ind1 = 0; ind1 < aCompList->length(); ind1++) {
938       //get component
939       SALOME_ModuleCatalog::Acomponent_ptr aComponent = (*aModuleCatalog)->GetComponent(aCompList[ind1]);
940       if ( aComponent != NULL ) {
941         //get interface list 
942         SALOME_ModuleCatalog::ListOfInterfaces_var anIntList = aComponent->GetInterfaceList();
943         for (int ind2 = 0; ind2 < anIntList->length(); ind2++) {
944           const char* anIntName = anIntList[ind2];
945           myInterfaceNameMap.insert(QString(anIntName), QString(aComponent->componentname()));
946         }
947       }
948     }
949   }
950 }
951
952 QMap<QString, QString> SUPERVGUI::getInterfaceNameMap() const {
953   return myInterfaceNameMap;
954 }