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