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