Salome HOME
Fix for IPAL9140: 3.0.0 (09 Jun): "Supervision" module has to be renamed into "Superv...
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
1 // File:      SalomeApp_Application.cxx
2 // Created:   10/22/2004 3:23:45 PM
3 // Author:    Sergey LITONIN
4 // Copyright (C) CEA 2004
5
6 #include "SalomeApp_PyInterp.h" // WARNING! This include must be the first!
7
8 #include "SalomeApp_Application.h"
9
10 #include "SalomeApp_Study.h"
11 #include "SalomeApp_Module.h"
12 #include "SalomeApp_OBFilter.h"
13 #include "SalomeApp_DataModel.h"
14 #include "SalomeApp_DataObject.h"
15 #include "SalomeApp_EventFilter.h"
16 #include "SalomeApp_WidgetContainer.h"
17
18 #include "SalomeApp_AboutDlg.h"
19 #include "SalomeApp_ModuleDlg.h"
20 #include "SalomeApp_PreferencesDlg.h"
21
22 #include "SalomeApp_GLSelector.h"
23 #include "SalomeApp_OBSelector.h"
24 #include "SalomeApp_OCCSelector.h"
25 #include "SalomeApp_VTKSelector.h"
26 #include "SalomeApp_SelectionMgr.h"
27
28 #include <LogWindow.h>
29
30 #include <GLViewer_Viewer.h>
31 #include <GLViewer_ViewManager.h>
32
33 #include <Plot2d_ViewManager.h>
34 #include <SPlot2d_ViewModel.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <SOCC_ViewModel.h>
38
39 #include <SVTK_ViewModel.h>
40 #include <SVTK_ViewManager.h>
41
42 #include <STD_TabDesktop.h>
43
44 #include <SUIT_Tools.h>
45 #include <SUIT_Session.h>
46
47 #include <QtxToolBar.h>
48 #include <QtxDockAction.h>
49
50 #include <OB_Browser.h>
51
52 #include <PythonConsole_PyConsole.h>
53
54 #include <SUIT_FileDlg.h>
55 #include <SUIT_MessageBox.h>
56 #include <SUIT_ResourceMgr.h>
57 #include <SUIT_ActionOperation.h>
58
59 #include <Utils_ORB_INIT.hxx>
60 #include <Utils_SINGLETON.hxx>
61 #include <SALOME_ModuleCatalog_impl.hxx>
62 #include <SALOME_LifeCycleCORBA.hxx>
63
64 #include <qmap.h>
65 #include <qdir.h>
66 #include <qlabel.h>
67 #include <qimage.h>
68 #include <qaction.h>
69 #include <qmenubar.h>
70 #include <qcombobox.h>
71 #include <qmessagebox.h>
72 #include <qapplication.h>
73
74 #include "SALOMEDS_StudyManager.hxx"
75
76 #define OBJECT_BROWSER_WIDTH 300
77
78 static const char* imageEmptyIcon[] = {
79 "20 20 1 1",
80 ".      c None",
81 "....................",
82 "....................",
83 "....................",
84 "....................",
85 "....................",
86 "....................",
87 "....................",
88 "....................",
89 "....................",
90 "....................",
91 "....................",
92 "....................",
93 "....................",
94 "....................",
95 "....................",
96 "....................",
97 "....................",
98 "....................",
99 "....................",
100 "...................."};
101
102 extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
103 {
104   return new SalomeApp_Application();
105 }
106
107 /*
108   Class       : SalomeApp_Application
109   Description : Application containing SalomeApp module
110 */
111
112 SalomeApp_Application::SalomeApp_Application()
113 : CAM_Application( false ),
114 myPrefDlg( 0 )
115 {
116   STD_TabDesktop* desk = new STD_TabDesktop();
117
118   setDesktop( desk );
119
120   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
121   QPixmap aLogo = aResMgr->loadPixmap( "SalomeApp", tr( "APP_DEFAULT_ICO" ) );
122
123   desktop()->setIcon( aLogo );
124   desktop()->setDockableMenuBar( true );
125   desktop()->setDockableStatusBar( false );
126
127   clearViewManagers();
128
129   mySelMgr = new SalomeApp_SelectionMgr( this );
130
131   connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
132            this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
133
134   connect( mySelMgr, SIGNAL( selectionChanged() ), this, SLOT( onSelection() ) );
135 }
136
137 SalomeApp_Application::~SalomeApp_Application()
138 {
139   saveWindowsGeometry();
140
141   if ( resourceMgr() )
142   {
143     if ( desktop() )
144       desktop()->saveGeometry( resourceMgr(), "desktop" );
145     resourceMgr()->save();
146   }
147
148   delete mySelMgr;
149
150   SalomeApp_EventFilter::Destroy();
151 }
152
153 void SalomeApp_Application::start()
154 {
155   if ( desktop() )
156     desktop()->loadGeometry( resourceMgr(), "desktop" );
157
158   CAM_Application::start();
159
160   QAction* a = action( ViewWindowsId );
161   if ( a && a->inherits( "QtxDockAction" ) )
162     ((QtxDockAction*)a)->setAutoPlace( true );
163
164   SalomeApp_EventFilter::Init();
165
166   updateWindows();
167   updateViewManagers();
168
169   putInfo( "" );
170 }
171
172 QString SalomeApp_Application::applicationName() const 
173
174   return tr( "APP_NAME" );
175 }
176
177 QString SalomeApp_Application::applicationVersion() const
178 {
179   static QString _app_version;
180
181   if ( _app_version.isEmpty() )
182   {
183     QString path( ::getenv( "GUI_ROOT_DIR" ) );
184     if ( !path.isEmpty() )
185       path += QDir::separator();
186     path += QString( "bin/salome/VERSION" );
187   
188     QFile vf( path );
189     if ( vf.open( IO_ReadOnly ) )
190     {
191       QString line;
192       vf.readLine( line, 1024 );
193       vf.close();
194
195       if ( !line.isEmpty() )
196       {
197         while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
198           line.remove( line.length() - 1, 1 );
199
200         int idx = line.findRev( ":" );
201         if ( idx != -1 )
202           _app_version = line.mid( idx + 1 ).stripWhiteSpace();
203       }
204     }
205   }
206
207   return _app_version;
208 }
209
210 CAM_Module* SalomeApp_Application::loadModule( const QString& name )
211 {
212   CAM_Module* mod = CAM_Application::loadModule( name );
213   if ( mod )
214   {
215     connect( this, SIGNAL( studyOpened() ), mod, SLOT( onModelOpened() ) );
216     connect( this, SIGNAL( studySaved() ),  mod, SLOT( onModelSaved() ) );
217     connect( this, SIGNAL( studyClosed() ), mod, SLOT( onModelClosed() ) );
218   }
219   return mod;
220 }
221
222 bool SalomeApp_Application::activateModule( const QString& modName )
223 {
224   QString actName;
225   CAM_Module* prevMod = activeModule();
226
227   if ( prevMod )
228     actName = prevMod->moduleName();
229
230   if ( actName == modName )
231     return true;
232
233   saveWindowsGeometry();
234
235   bool status = CAM_Application::activateModule( modName );
236
237   updateModuleActions();
238
239   if ( !status )
240     return false;
241
242   updateWindows();
243   updateViewManagers();
244
245   return true;
246 }
247
248 SalomeApp_SelectionMgr* SalomeApp_Application::selectionMgr() const
249 {
250   return mySelMgr;
251 }
252
253 void SalomeApp_Application::createActions()
254 {
255   STD_Application::createActions();
256
257   SUIT_Desktop* desk = desktop();
258   SUIT_ResourceMgr* resMgr = resourceMgr();
259   
260   // Load script
261   createAction( LoadScriptId, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), QIconSet(),
262                 tr( "MEN_DESK_FILE_LOAD_SCRIPT" ), tr( "PRP_DESK_FILE_LOAD_SCRIPT" ),
263                 0, desk, false, this, SLOT( onLoadScript() ) );
264   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1 );
265   createMenu( LoadScriptId, fileMenu, 10, -1 );
266   
267   // default icon for neutral point ('SALOME' module)
268   QPixmap defIcon = resMgr->loadPixmap( "SalomeApp", tr( "APP_DEFAULT_ICO" ) );
269   if ( defIcon.isNull() )
270     defIcon = QPixmap( imageEmptyIcon );
271
272   // default icon for any module
273   QPixmap modIcon = resMgr->loadPixmap( "SalomeApp", tr( "APP_MODULE_ICO" ) );
274   if ( modIcon.isNull() )
275     modIcon = QPixmap( imageEmptyIcon );
276
277   QToolBar* modTBar = new QtxToolBar( true, desk );
278   modTBar->setLabel( tr( "INF_TOOLBAR_MODULES" ) );
279
280   QActionGroup* modGroup = new QActionGroup( this );
281   modGroup->setExclusive( true );
282   modGroup->setUsesDropDown( true );
283
284   QAction* a = createAction( -1, tr( "APP_NAME" ), defIcon, tr( "APP_NAME" ),
285                              tr( "PRP_APP_MODULE" ), 0, desk, true );
286   modGroup->add( a );
287   myActions.insert( QString(), a );
288
289   QMap<QString, QString> iconMap;
290   moduleIconNames( iconMap );
291
292   const int iconSize = 20;
293
294   modGroup->addTo( modTBar );
295   modTBar->addSeparator();
296
297   QStringList modList;
298   modules( modList, false );
299
300   for ( QStringList::Iterator it = modList.begin(); it != modList.end(); ++it )
301   {
302     if ( (*it).isEmpty() )
303       continue;
304
305     QString iconName;
306     if ( iconMap.contains( *it ) )
307       iconName = iconMap[*it];
308
309     QString modName = moduleName( *it );
310
311     QPixmap icon = resMgr->loadPixmap( modName, iconName );
312     if ( icon.isNull() )
313       icon = modIcon;
314
315     icon.convertFromImage( icon.convertToImage().smoothScale( iconSize, iconSize, QImage::ScaleMin ) );
316
317     QAction* a = createAction( -1, *it, icon, *it, tr( "PRP_MODULE" ).arg( *it ), 0, desk, true );
318     a->addTo( modTBar );
319     modGroup->add( a );
320
321     myActions.insert( *it, a );
322   }
323
324   SUIT_Tools::simplifySeparators( modTBar );
325   
326   // New window
327
328   int windowMenu = createMenu( tr( "MEN_DESK_WINDOW" ), -1, 100 );
329   int newWinMenu = createMenu( tr( "MEN_DESK_NEWWINDOW" ), windowMenu, -1, 0 );
330   createMenu( separator(), windowMenu, -1, 1 );
331
332   QMap<int, int> accelMap;
333   accelMap[NewGLViewId]  = ALT+Key_G;
334   accelMap[NewPlot2dId]  = ALT+Key_P;
335   accelMap[NewOCCViewId] = ALT+Key_O;
336   accelMap[NewVTKViewId] = ALT+Key_K;
337
338   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
339   {
340     QAction* a = createAction( id, tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ), QIconSet(), 
341                                tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ),
342                                tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ),
343                                accelMap.contains( id ) ? accelMap[id] : 0, desk, false, this, SLOT( onNewWindow() ) );
344     createMenu( a, newWinMenu, -1 );
345   }
346
347   connect( modGroup, SIGNAL( selected( QAction* ) ), this, SLOT( onModuleActivation( QAction* ) ) );
348 }
349
350 void SalomeApp_Application::onModuleActivation( QAction* a )
351 {
352   if ( !a )
353     return;
354
355   QString modName = a->menuText();
356   if ( modName == tr( "APP_NAME" ) )
357     modName = QString::null;
358
359   // Force user to create/open a study before module activation
360   QMap<QString, QString> iconMap;
361   moduleIconNames( iconMap );
362   QPixmap icon = resourceMgr()->loadPixmap( moduleName( modName ), iconMap[ modName ] );
363
364   bool cancelled = false;
365   while ( !modName.isEmpty() && !activeStudy() && !cancelled ){
366     SalomeApp_ModuleDlg aDlg( desktop(), modName, icon );
367     int res = aDlg.exec();
368
369     switch ( res ){
370     case 1:
371       onNewDoc();
372       break;
373     case 2:
374       onOpenDoc();
375       break;
376     case 3:
377       //onLoadStudy();
378       //break;
379     case 0:
380     default:
381       putInfo( tr("INF_CANCELLED") );
382       myActions[QString()]->setOn( true );
383       cancelled = true;
384     }
385   }
386
387   if ( !cancelled )
388     activateModule( modName );
389 }
390
391 QString SalomeApp_Application::defaultModule() const
392 {
393   QStringList aModuleNames;
394   modules( aModuleNames, false ); // obtain a complete list of module names for the current configuration
395   // If there's the one and only module --> activate it automatically
396   // TODO: Possible improvement - default module can be taken from preferences
397   return aModuleNames.count() > 1 ? "" : ( aModuleNames.count() ? aModuleNames.first() : "" );
398 }
399
400 void SalomeApp_Application::onNewWindow()
401 {
402   const QObject* obj = sender();
403   if ( !obj || !obj->inherits( "QAction" ) )
404     return;
405
406   QString type;
407   int id = actionId( (QAction*)obj );
408   switch ( id )
409   {
410   case NewGLViewId:
411     type = GLViewer_Viewer::Type();
412     break;
413   case NewPlot2dId:
414     type = Plot2d_Viewer::Type();
415     break;
416   case NewOCCViewId:
417     type = OCCViewer_Viewer::Type();
418     break;
419   case NewVTKViewId:
420     type = VTKViewer_Viewer::Type();
421     break;
422   }
423
424   if ( !type.isEmpty() )
425     createViewManager( type );
426 }
427
428 //=======================================================================
429 // name    : onNewDoc
430 // Purpose : SLOT. Create new document
431 //=======================================================================
432 void SalomeApp_Application::onNewDoc()
433 {
434   SUIT_Study* study = activeStudy();
435
436   saveWindowsGeometry();
437
438   CAM_Application::onNewDoc();
439   
440   if ( !study ) // new study will be create in THIS application
441   {
442     updateWindows();
443     updateViewManagers();
444   }
445 }
446
447 //=======================================================================
448 // name    : onOpenDoc
449 // Purpose : SLOT. Open new document
450 //=======================================================================
451 void SalomeApp_Application::onOpenDoc()
452 {
453   SUIT_Study* study = activeStudy();
454   saveWindowsGeometry();
455
456   CAM_Application::onOpenDoc();
457   
458   if ( !study ) // new study will be create in THIS application
459   {
460     updateWindows();
461     updateViewManagers();
462   }
463 }
464
465 void SalomeApp_Application::onSelection()
466 {
467   onSelectionChanged();
468
469   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
470     ((SalomeApp_Module*)activeModule())->selectionChanged();
471 }
472
473 void SalomeApp_Application::onSelectionChanged()
474 {
475 }
476
477 void SalomeApp_Application::onAboutRefresh()
478 {
479   SalomeApp_DataModel* dm = 0;
480   if ( activeModule() )
481   {
482     CAM_DataModel* camDM = activeModule()->dataModel();
483     if ( camDM && camDM->inherits( "SalomeApp_DataModel" ) )
484       dm = (SalomeApp_DataModel*)camDM;
485   }
486
487   if ( dm )
488     dm->update();
489 }
490
491 void SalomeApp_Application::setActiveStudy( SUIT_Study* study )
492 {
493   CAM_Application::setActiveStudy( study );
494     
495   activateWindows();
496 }
497
498 //=======================================================================
499 // name    : createNewStudy
500 // Purpose : Create new study
501 //=======================================================================
502 SUIT_Study* SalomeApp_Application::createNewStudy() 
503
504   SalomeApp_Study* aStudy = new SalomeApp_Study( this ); 
505   
506   // Set up processing of major study-related events
507   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
508   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
509   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
510   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
511
512   return aStudy; 
513 }
514
515 //=======================================================================
516 // name    : createNewStudy
517 // Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu
518 //=======================================================================
519 void SalomeApp_Application::updateCommandsStatus()
520 {
521   CAM_Application::updateCommandsStatus();
522
523   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
524   {
525     QAction* a = action( id );
526     if ( a )
527       a->setEnabled( activeStudy() );
528   }
529
530   // Load script menu
531   QAction* a = action( LoadScriptId );
532   if ( a )
533     a->setEnabled( activeStudy() );
534 }
535
536 //=======================================================================
537 // name    : onHelpAbout
538 // Purpose : SLOT. Display "About" message box
539 //=======================================================================
540 void SalomeApp_Application::onHelpAbout()
541 {
542   SalomeApp_AboutDlg* dlg = new SalomeApp_AboutDlg( applicationName(), applicationVersion(), desktop() );
543   dlg->exec();
544   delete dlg;
545 }
546
547 QWidget* SalomeApp_Application::window( const int flag, const int studyId ) const
548 {
549   QWidget* wid = 0;
550
551   int sId = studyId;
552   if ( sId < 0 )
553   {
554     if ( !activeStudy() )
555       return 0;
556     else
557       sId = activeStudy()->id();
558   }
559
560   if ( myWindows.contains( flag ) )
561     wid = myWindows[flag]->widget( sId );
562
563   return wid;
564 }
565
566 void SalomeApp_Application::addWindow( QWidget* wid, const int flag, const int studyId )
567 {
568   if ( !wid )
569     return;
570
571   int sId = studyId;
572   if ( sId < 0 )
573   {
574     if ( !activeStudy() )
575       return;
576     else
577       sId = activeStudy()->id();
578   }
579
580   if ( !myWindows.contains( flag ) )
581   {
582     QMap<int, int> winMap;
583     currentWindows( winMap );
584
585     myWindows.insert( flag, new SalomeApp_WidgetContainer( flag, desktop() ) );
586     if ( winMap.contains( flag ) )
587       desktop()->moveDockWindow( myWindows[flag], (Dock)winMap[flag] );
588
589     myWindows[flag]->setResizeEnabled( true );
590     myWindows[flag]->setCloseMode( QDockWindow::Always );
591     myWindows[flag]->setName( QString( "dock_window_%1" ).arg( flag ) );
592   }
593
594   myWindows[flag]->insert( sId, wid );
595
596   setWindowShown( flag, !myWindows[flag]->isEmpty() );
597 }
598
599 void SalomeApp_Application::removeWindow( const int flag, const int studyId )
600 {
601   if ( !myWindows.contains( flag ) )
602     return;
603
604   int sId = studyId;
605   if ( sId < 0 )
606   {
607     if ( !activeStudy() )
608       return;
609     else
610       sId = activeStudy()->id();
611   }
612
613   QWidget* wid = myWindows[flag]->widget( sId );
614   myWindows[flag]->remove( sId );
615   delete wid;
616
617   setWindowShown( flag, !myWindows[flag]->isEmpty() );
618 }
619
620 QWidget* SalomeApp_Application::getWindow( const int flag, const int studyId )
621 {
622   QWidget* wid = window( flag, studyId );
623   if ( !wid )
624     addWindow( wid = createWindow( flag ), flag, studyId );
625
626   return wid;
627 }
628
629 bool SalomeApp_Application::isWindowVisible( const int type ) const
630 {
631   bool res = false;
632   if ( myWindows.contains( type ) )
633   {
634     SUIT_Desktop* desk = ((SalomeApp_Application*)this)->desktop();
635     res = desk && desk->appropriate( myWindows[type] );
636   }
637   return res;
638 }
639
640 void SalomeApp_Application::setWindowShown( const int type, const bool on )
641 {
642   if ( !desktop() || !myWindows.contains( type ) )
643     return;
644
645   QDockWindow* dw = myWindows[type];
646   desktop()->setAppropriate( dw, on );
647   on ? dw->show() : dw->hide();
648 }
649
650 OB_Browser* SalomeApp_Application::objectBrowser()
651 {
652   OB_Browser* ob = 0;
653   QWidget* wid = getWindow( WT_ObjectBrowser );
654   if ( wid->inherits( "OB_Browser" ) )
655     ob = (OB_Browser*)wid;
656   return ob;
657 }
658
659 LogWindow* SalomeApp_Application::logWindow()
660 {
661   LogWindow* lw = 0;
662   QWidget* wid = getWindow( WT_LogWindow );
663   if ( wid->inherits( "LogWindow" ) )
664     lw = (LogWindow*)wid;
665   return lw;
666 }
667
668 PythonConsole* SalomeApp_Application::pythonConsole()
669 {
670   PythonConsole* console = 0;
671   QWidget* wid = getWindow( WT_PyConsole );
672   if ( wid->inherits( "PythonConsole" ) )
673     console = (PythonConsole*)wid;
674   return console;
675 }
676
677 QtxResourceEdit* SalomeApp_Application::resourceEdit() const
678 {
679   return 0;
680   /*
681   QtxResourceEdit* edit = 0;
682   if ( !myPrefDlg )
683   {
684     SalomeApp_Application* that = (SalomeApp_Application*)this;
685     that->myPrefDlg = new SalomeApp_PreferencesDlg( that->desktop() );
686
687     edit = myPrefDlg->resourceEdit();
688
689     QStringList modList;
690     modules( modList, false );
691     for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
692     {
693       int id = edit->addItem( *it );
694       edit->setProperty( id, "info", tr( "PREFERENCES_NOT_LOADED" ).arg( *it ) );
695     }
696   }
697   else
698     edit = myPrefDlg->resourceEdit();
699   */
700 }
701
702 SUIT_ViewManager* SalomeApp_Application::getViewManager( const QString& vmType, const bool create )
703 {
704   SUIT_ViewManager* aVM = viewManager( vmType );
705   SUIT_ViewManager* anActiveVM = CAM_Application::activeViewManager();
706   if ( anActiveVM && anActiveVM->getType() == vmType )
707     aVM = anActiveVM;
708   else if ( aVM )
709   {
710     if ( !aVM->getActiveView() )
711       aVM->createView();
712   }
713   else if ( create )
714     aVM = createViewManager( vmType );
715   return aVM;
716 }
717
718 SUIT_ViewManager* SalomeApp_Application::createViewManager( const QString& vmType )
719 {
720   SUIT_ViewManager* viewMgr = 0;
721   if ( vmType == GLViewer_Viewer::Type() )
722   {
723     viewMgr = new GLViewer_ViewManager( activeStudy(), desktop() );
724     new SalomeApp_GLSelector( (GLViewer_Viewer2d*)viewMgr->getViewModel(), mySelMgr );
725   }
726   else if ( vmType == Plot2d_Viewer::Type() )
727   {
728     viewMgr = new Plot2d_ViewManager( activeStudy(), desktop() );
729     viewMgr->setViewModel( new SPlot2d_Viewer() );// custom view model, which extends SALOME_View interface
730   }
731   else if ( vmType == OCCViewer_Viewer::Type() )
732   {
733     viewMgr = new OCCViewer_ViewManager( activeStudy(), desktop() );
734     viewMgr->setViewModel( new SOCC_Viewer() );// custom view model, which extends SALOME_View interface
735     new SalomeApp_OCCSelector( (OCCViewer_Viewer*)viewMgr->getViewModel(), mySelMgr );
736   }
737   else if ( vmType == SVTK_Viewer::Type() )
738   {
739     viewMgr = new SVTK_ViewManager( activeStudy(), desktop() );
740     new SalomeApp_VTKSelector((SVTK_Viewer*)viewMgr->getViewModel(),mySelMgr);
741   }
742
743   if ( !viewMgr )
744     return 0;
745
746   addViewManager( viewMgr );
747   SUIT_ViewWindow* viewWin = viewMgr->createViewWindow();
748
749   if ( viewWin && desktop() )
750     viewWin->resize( (int)( desktop()->width() * 0.6 ), (int)( desktop()->height() * 0.6 ) );
751
752   connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
753            this, SLOT( onCloseView( SUIT_ViewManager* ) ) );
754
755   return viewMgr;
756 }
757
758 void SalomeApp_Application::onCloseView( SUIT_ViewManager* theVM )
759 {
760   removeViewManager( theVM );
761 }
762
763 void SalomeApp_Application::onStudyCreated( SUIT_Study* theStudy )
764 {
765   SUIT_DataObject* aRoot = 0;
766   if ( theStudy && theStudy->root() )
767   {
768     aRoot = theStudy->root();
769     //aRoot->setName( tr( "DATA_MODELS" ) );
770   }
771   if ( objectBrowser() != 0 )
772     objectBrowser()->setRootObject( aRoot );
773
774   activateModule( defaultModule() );
775
776   activateWindows();
777 }
778
779 void SalomeApp_Application::onStudyOpened( SUIT_Study* theStudy )
780 {
781   SUIT_DataObject* aRoot = 0;
782   if ( theStudy && theStudy->root() )
783   {
784     aRoot = theStudy->root();
785     //aRoot->dump();
786   }
787   if ( objectBrowser() != 0 ) {
788     objectBrowser()->setRootObject( aRoot );
789   }
790
791   activateModule( defaultModule() );
792
793   activateWindows();
794
795   emit studyOpened();
796 }
797
798 void SalomeApp_Application::onStudySaved( SUIT_Study* )
799 {
800   emit studySaved();
801 }
802
803 void SalomeApp_Application::onStudyClosed( SUIT_Study* )
804 {
805   emit studyClosed();
806
807   activateModule( "" );
808
809   saveWindowsGeometry();
810 }
811
812 void SalomeApp_Application::onLoadScript( )
813 {
814   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
815   if ( !appStudy ) return;
816   _PTR(Study) aStudy = appStudy->studyDS();
817   
818   if ( aStudy->GetProperties()->IsLocked() ) {
819     SUIT_MessageBox::warn1 ( desktop(),
820                              QObject::tr("WRN_WARNING"), 
821                              QObject::tr("WRN_STUDY_LOCKED"),
822                              QObject::tr("BUT_OK") );
823     return;
824   }
825   
826   QStringList filtersList;
827   filtersList.append(tr("PYTHON_FILES_FILTER"));
828   filtersList.append(tr("ALL_FILES_FILTER"));
829   
830   QString aFile = SUIT_FileDlg::getFileName(desktop(), "", filtersList, tr("TOT_DESK_FILE_LOAD_SCRIPT"), true, false);
831   if(!aFile.isEmpty()) {
832     QString command = QString("execfile(\"%1\")").arg(aFile);
833     
834     PythonConsole* pyConsole = pythonConsole();
835     
836     if(pyConsole)
837       pyConsole->exec(command);
838   }
839 }
840
841 QString SalomeApp_Application::getFileFilter() const
842 {
843   return "(*.hdf)";
844 }
845
846 void SalomeApp_Application::beforeCloseDoc( SUIT_Study* s )
847 {
848   CAM_Application::beforeCloseDoc( s );
849
850   for ( WindowMap::ConstIterator itr = myWindows.begin(); s && itr != myWindows.end(); ++itr )
851     removeWindow( itr.key(), s->id() );
852 }
853
854 void SalomeApp_Application::updateActions()
855 {
856   updateCommandsStatus();
857 }
858
859 QWidget* SalomeApp_Application::createWindow( const int flag )
860 {
861   QWidget* wid = 0;
862
863   if ( flag == WT_ObjectBrowser )
864   {
865     OB_Browser* ob = new OB_Browser( desktop() );
866     ob->setAutoUpdate( true );
867     ob->setAutoOpenLevel( 1 );
868     ob->setCaption( tr( "OBJECT_BROWSER" ) );
869     ob->resize( OBJECT_BROWSER_WIDTH, ob->height() );
870     ob->setFilter( new SalomeApp_OBFilter( selectionMgr() ) );
871
872     ob->setNameTitle( tr( "OBJ_BROWSER_NAME" ) );
873     for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
874       ob->addColumn( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), i );
875
876     // Create OBSelector
877     new SalomeApp_OBSelector( ob, mySelMgr );
878
879     wid = ob;
880
881     connect( ob, SIGNAL( aboutRefresh() ), this, SLOT( onAboutRefresh() ) );
882     ob->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
883   }
884   else if ( flag == WT_PyConsole )
885   {
886     PythonConsole* pyCons = new PythonConsole( desktop(), new SalomeApp_PyInterp() );
887     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
888     wid = pyCons;
889
890     //    pyCons->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
891   }
892   else if ( flag == WT_LogWindow )
893   {
894     LogWindow* logWin = new LogWindow( desktop() );
895     logWin->setCaption( tr( "LOG_WINDOW" ) );
896     wid = logWin;
897
898     logWin->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
899   }
900
901   return wid;
902 }
903
904 void SalomeApp_Application::defaultWindows( QMap<int, int>& aMap ) const
905 {
906   aMap.insert( WT_ObjectBrowser, Qt::DockLeft );
907   aMap.insert( WT_PyConsole, Qt::DockBottom );
908   aMap.insert( WT_LogWindow, Qt::DockBottom );
909 }
910
911 void SalomeApp_Application::defaultViewManagers( QStringList& ) const
912 {
913 }
914
915 void SalomeApp_Application::afterCloseDoc()
916 {
917   updateWindows();
918
919   CAM_Application::afterCloseDoc();
920 }
921
922 CORBA::ORB_var SalomeApp_Application::orb()
923 {
924   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
925   static CORBA::ORB_var _orb = init( qApp->argc(), qApp->argv() );
926   return _orb;
927 }
928
929 SALOMEDSClient_StudyManager* SalomeApp_Application::studyMgr()
930 {
931   static SALOMEDSClient_StudyManager* _sm = new SALOMEDS_StudyManager();
932   return _sm;
933 }
934
935 SALOME_NamingService* SalomeApp_Application::namingService()
936 {
937   static SALOME_NamingService* _ns = new SALOME_NamingService( orb() );
938   return _ns;
939 }
940
941 SALOME_LifeCycleCORBA* SalomeApp_Application::lcc()
942 {
943   static SALOME_LifeCycleCORBA* _lcc = new SALOME_LifeCycleCORBA( namingService() );
944   return _lcc;
945 }
946
947 QString SalomeApp_Application::defaultEngineIOR()
948 {
949   // Look for a default module engine (needed for CORBAless modules to use SALOMEDS persistence)
950   QString anIOR( "" );
951   CORBA::Object_ptr anEngine = namingService()->Resolve( "/SalomeAppEngine" );
952   if ( !CORBA::is_nil( anEngine ) )
953     anIOR = orb()->object_to_string( anEngine );
954   return anIOR;
955 }
956
957 void SalomeApp_Application::moduleIconNames( QMap<QString, QString>& iconMap ) const
958 {
959   iconMap.clear();
960
961   SUIT_ResourceMgr* resMgr = resourceMgr();
962   if ( !resMgr )
963     return;
964
965   QStringList modList;
966   modules( modList, false );
967
968   for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
969   {
970     QString modName = *it;
971     QString modIntr = moduleName( modName );
972     QString modIcon = resMgr->stringValue( modIntr, "icon", QString::null );
973
974     if ( modIcon.isEmpty() )
975       continue;
976
977     if ( SUIT_Tools::extension( modIcon ).isEmpty() )
978       modIcon += QString( ".png" );
979
980     iconMap.insert( modName, modIcon );
981   }
982 }
983
984 void SalomeApp_Application::updateModuleActions()
985 {
986   QString modName;
987   if ( activeModule() )
988     modName = activeModule()->moduleName();
989
990   if ( myActions.contains( modName ) )
991     myActions[modName]->setOn( true );
992 }
993
994 void SalomeApp_Application::currentWindows( QMap<int, int>& winMap ) const
995 {
996   winMap.clear();
997   if ( !activeStudy() )
998     return;
999
1000   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1001     ((SalomeApp_Module*)activeModule())->windows( winMap );
1002   else
1003     defaultWindows( winMap );
1004 }
1005
1006 void SalomeApp_Application::currentViewManagers( QStringList& lst ) const
1007 {
1008   lst.clear();
1009   if ( !activeStudy() )
1010     return;
1011
1012   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1013     ((SalomeApp_Module*)activeModule())->viewManagers( lst );
1014   else
1015     defaultViewManagers( lst );
1016 }
1017
1018 void SalomeApp_Application::updateWindows()
1019 {
1020   QMap<int, int> winMap;
1021   currentWindows( winMap );
1022
1023   for ( QMap<int, int>::ConstIterator it = winMap.begin(); it != winMap.end(); ++it )
1024     getWindow( it.key() );
1025
1026   loadWindowsGeometry();
1027
1028   for ( WindowMap::ConstIterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1029     setWindowShown( itr.key(), !itr.data()->isEmpty() && winMap.contains( itr.key() ) );
1030 }
1031
1032 void SalomeApp_Application::updateViewManagers()
1033 {
1034   QStringList lst;
1035   currentViewManagers( lst );
1036
1037   for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
1038     getViewManager( *it, true );
1039 }
1040
1041 void SalomeApp_Application::loadWindowsGeometry()
1042 {
1043   QtxDockAction* dockMgr = 0;
1044
1045   QAction* a = action( ViewWindowsId );
1046   if ( a && a->inherits( "QtxDockAction" ) )
1047     dockMgr = (QtxDockAction*)a;
1048
1049   if ( !dockMgr )
1050     return;
1051
1052   QString modName;
1053   if ( activeModule() )
1054     modName = moduleLibrary( activeModule()->moduleName(), false );
1055   
1056   QString section = QString( "windows_geometry" );
1057   if ( !modName.isEmpty() )
1058     section += QString( "." ) + modName;
1059
1060   dockMgr->loadGeometry( resourceMgr(), section, false );
1061   dockMgr->restoreGeometry();
1062 }
1063
1064 void SalomeApp_Application::saveWindowsGeometry()
1065 {
1066   QtxDockAction* dockMgr = 0;
1067
1068   QAction* a = action( ViewWindowsId );
1069   if ( a && a->inherits( "QtxDockAction" ) )
1070     dockMgr = (QtxDockAction*)a;
1071
1072   if ( !dockMgr )
1073     return;
1074
1075   QString modName;
1076   if ( activeModule() )
1077     modName = moduleLibrary( activeModule()->moduleName(), false );
1078   
1079   QString section = QString( "windows_geometry" );
1080   if ( !modName.isEmpty() )
1081     section += QString( "." ) + modName;
1082
1083   dockMgr->storeGeometry();
1084   dockMgr->saveGeometry( resourceMgr(), section, false );
1085 }
1086
1087 void SalomeApp_Application::activateWindows()
1088 {
1089   if ( activeStudy() )
1090   {
1091     for ( WindowMap::Iterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1092       itr.data()->activate( activeStudy()->id() );
1093   }
1094 }