Salome HOME
Preferences Editor.
[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   for ( ModuleListIterator it = modules(); it.current(); ++it )
487   {    
488     CAM_DataModel* camDM = it.current()->dataModel();
489     if ( camDM && camDM->inherits( "SalomeApp_DataModel" ) )
490       ((SalomeApp_DataModel*)camDM)->update();
491   }
492 }
493
494 void SalomeApp_Application::setActiveStudy( SUIT_Study* study )
495 {
496   CAM_Application::setActiveStudy( study );
497     
498   activateWindows();
499 }
500
501 //=======================================================================
502 // name    : createNewStudy
503 // Purpose : Create new study
504 //=======================================================================
505 SUIT_Study* SalomeApp_Application::createNewStudy() 
506
507   SalomeApp_Study* aStudy = new SalomeApp_Study( this ); 
508   
509   // Set up processing of major study-related events
510   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
511   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
512   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
513   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
514
515   return aStudy; 
516 }
517
518 //=======================================================================
519 // name    : createNewStudy
520 // Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu
521 //=======================================================================
522 void SalomeApp_Application::updateCommandsStatus()
523 {
524   CAM_Application::updateCommandsStatus();
525
526   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
527   {
528     QAction* a = action( id );
529     if ( a )
530       a->setEnabled( activeStudy() );
531   }
532
533   // Load script menu
534   QAction* a = action( LoadScriptId );
535   if ( a )
536     a->setEnabled( activeStudy() );
537   
538   a = action( PropertiesId );
539   if( a )
540     a->setEnabled( activeStudy() );
541 }
542
543 //=======================================================================
544 // name    : onHelpAbout
545 // Purpose : SLOT. Display "About" message box
546 //=======================================================================
547 void SalomeApp_Application::onHelpAbout()
548 {
549   SalomeApp_AboutDlg* dlg = new SalomeApp_AboutDlg( applicationName(), applicationVersion(), desktop() );
550   dlg->exec();
551   delete dlg;
552 }
553
554 QWidget* SalomeApp_Application::window( const int flag, const int studyId ) const
555 {
556   QWidget* wid = 0;
557
558   int sId = studyId;
559   if ( sId < 0 )
560   {
561     if ( !activeStudy() )
562       return 0;
563     else
564       sId = activeStudy()->id();
565   }
566
567   if ( myWindows.contains( flag ) )
568     wid = myWindows[flag]->widget( sId );
569
570   return wid;
571 }
572
573 void SalomeApp_Application::addWindow( QWidget* wid, const int flag, const int studyId )
574 {
575   if ( !wid )
576     return;
577
578   int sId = studyId;
579   if ( sId < 0 )
580   {
581     if ( !activeStudy() )
582       return;
583     else
584       sId = activeStudy()->id();
585   }
586
587   if ( !myWindows.contains( flag ) )
588   {
589     QMap<int, int> winMap;
590     currentWindows( winMap );
591
592     myWindows.insert( flag, new SalomeApp_WidgetContainer( flag, desktop() ) );
593     if ( winMap.contains( flag ) )
594       desktop()->moveDockWindow( myWindows[flag], (Dock)winMap[flag] );
595
596     myWindows[flag]->setResizeEnabled( true );
597     myWindows[flag]->setCloseMode( QDockWindow::Always );
598     myWindows[flag]->setName( QString( "dock_window_%1" ).arg( flag ) );
599   }
600
601   myWindows[flag]->insert( sId, wid );
602
603   setWindowShown( flag, !myWindows[flag]->isEmpty() );
604 }
605
606 void SalomeApp_Application::removeWindow( const int flag, const int studyId )
607 {
608   if ( !myWindows.contains( flag ) )
609     return;
610
611   int sId = studyId;
612   if ( sId < 0 )
613   {
614     if ( !activeStudy() )
615       return;
616     else
617       sId = activeStudy()->id();
618   }
619
620   QWidget* wid = myWindows[flag]->widget( sId );
621   myWindows[flag]->remove( sId );
622   delete wid;
623
624   setWindowShown( flag, !myWindows[flag]->isEmpty() );
625 }
626
627 QWidget* SalomeApp_Application::getWindow( const int flag, const int studyId )
628 {
629   QWidget* wid = window( flag, studyId );
630   if ( !wid )
631     addWindow( wid = createWindow( flag ), flag, studyId );
632
633   return wid;
634 }
635
636 bool SalomeApp_Application::isWindowVisible( const int type ) const
637 {
638   bool res = false;
639   if ( myWindows.contains( type ) )
640   {
641     SUIT_Desktop* desk = ((SalomeApp_Application*)this)->desktop();
642     res = desk && desk->appropriate( myWindows[type] );
643   }
644   return res;
645 }
646
647 void SalomeApp_Application::setWindowShown( const int type, const bool on )
648 {
649   if ( !desktop() || !myWindows.contains( type ) )
650     return;
651
652   QDockWindow* dw = myWindows[type];
653   desktop()->setAppropriate( dw, on );
654   on ? dw->show() : dw->hide();
655 }
656
657 OB_Browser* SalomeApp_Application::objectBrowser()
658 {
659   OB_Browser* ob = 0;
660   QWidget* wid = getWindow( WT_ObjectBrowser );
661   if ( wid->inherits( "OB_Browser" ) )
662     ob = (OB_Browser*)wid;
663   return ob;
664 }
665
666 LogWindow* SalomeApp_Application::logWindow()
667 {
668   LogWindow* lw = 0;
669   QWidget* wid = getWindow( WT_LogWindow );
670   if ( wid->inherits( "LogWindow" ) )
671     lw = (LogWindow*)wid;
672   return lw;
673 }
674
675 PythonConsole* SalomeApp_Application::pythonConsole()
676 {
677   PythonConsole* console = 0;
678   QWidget* wid = getWindow( WT_PyConsole );
679   if ( wid->inherits( "PythonConsole" ) )
680     console = (PythonConsole*)wid;
681   return console;
682 }
683
684 QtxResourceEdit* SalomeApp_Application::resourceEdit() const
685 {
686   return 0;
687   /*
688   QtxResourceEdit* edit = 0;
689   if ( !myPrefDlg )
690   {
691     SalomeApp_Application* that = (SalomeApp_Application*)this;
692     that->myPrefDlg = new SalomeApp_PreferencesDlg( that->desktop() );
693
694     edit = myPrefDlg->resourceEdit();
695
696     QStringList modList;
697     modules( modList, false );
698     for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
699     {
700       int id = edit->addItem( *it );
701       edit->setProperty( id, "info", tr( "PREFERENCES_NOT_LOADED" ).arg( *it ) );
702     }
703   }
704   else
705     edit = myPrefDlg->resourceEdit();
706   */
707 }
708
709 SUIT_ViewManager* SalomeApp_Application::getViewManager( const QString& vmType, const bool create )
710 {
711   SUIT_ViewManager* aVM = viewManager( vmType );
712   SUIT_ViewManager* anActiveVM = CAM_Application::activeViewManager();
713
714   if ( anActiveVM && anActiveVM->getType() == vmType )
715     aVM = anActiveVM;
716
717   if ( aVM && create )
718   {
719     if ( !aVM->getActiveView() )
720       aVM->createView();
721     else
722       aVM->getActiveView()->setFocus();
723   }
724   else if ( create )
725     aVM = createViewManager( vmType );
726
727   return aVM;
728 }
729
730 SUIT_ViewManager* SalomeApp_Application::createViewManager( const QString& vmType )
731 {
732   SUIT_ViewManager* viewMgr = 0;
733   if ( vmType == GLViewer_Viewer::Type() )
734   {
735     viewMgr = new GLViewer_ViewManager( activeStudy(), desktop() );
736     new SalomeApp_GLSelector( (GLViewer_Viewer2d*)viewMgr->getViewModel(), mySelMgr );
737   }
738   else if ( vmType == Plot2d_Viewer::Type() )
739   {
740     viewMgr = new Plot2d_ViewManager( activeStudy(), desktop() );
741     viewMgr->setViewModel( new SPlot2d_Viewer() );// custom view model, which extends SALOME_View interface
742   }
743   else if ( vmType == OCCViewer_Viewer::Type() )
744   {
745     viewMgr = new OCCViewer_ViewManager( activeStudy(), desktop() );
746     viewMgr->setViewModel( new SOCC_Viewer() );// custom view model, which extends SALOME_View interface
747     new SalomeApp_OCCSelector( (OCCViewer_Viewer*)viewMgr->getViewModel(), mySelMgr );
748   }
749   else if ( vmType == SVTK_Viewer::Type() )
750   {
751     viewMgr = new SVTK_ViewManager( activeStudy(), desktop() );
752     new SalomeApp_VTKSelector((SVTK_Viewer*)viewMgr->getViewModel(),mySelMgr);
753   }
754
755   if ( !viewMgr )
756     return 0;
757
758   addViewManager( viewMgr );
759   SUIT_ViewWindow* viewWin = viewMgr->createViewWindow();
760
761   if ( viewWin && desktop() )
762     viewWin->resize( (int)( desktop()->width() * 0.6 ), (int)( desktop()->height() * 0.6 ) );
763
764   connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
765            this, SLOT( onCloseView( SUIT_ViewManager* ) ) );
766
767   return viewMgr;
768 }
769
770 void SalomeApp_Application::onCloseView( SUIT_ViewManager* theVM )
771 {
772   removeViewManager( theVM );
773 }
774
775 void SalomeApp_Application::onStudyCreated( SUIT_Study* theStudy )
776 {
777   SUIT_DataObject* aRoot = 0;
778   if ( theStudy && theStudy->root() )
779   {
780     aRoot = theStudy->root();
781     //aRoot->setName( tr( "DATA_MODELS" ) );
782   }
783   if ( objectBrowser() != 0 )
784     objectBrowser()->setRootObject( aRoot );
785
786   activateModule( defaultModule() );
787
788   activateWindows();
789 }
790
791 void SalomeApp_Application::onStudyOpened( SUIT_Study* theStudy )
792 {
793   SUIT_DataObject* aRoot = 0;
794   if ( theStudy && theStudy->root() )
795   {
796     aRoot = theStudy->root();
797     //aRoot->dump();
798   }
799   if ( objectBrowser() != 0 ) {
800     objectBrowser()->setRootObject( aRoot );
801   }
802
803   activateModule( defaultModule() );
804
805   activateWindows();
806
807   emit studyOpened();
808 }
809
810 void SalomeApp_Application::onStudySaved( SUIT_Study* )
811 {
812   emit studySaved();
813 }
814
815 void SalomeApp_Application::onStudyClosed( SUIT_Study* )
816 {
817   emit studyClosed();
818
819   activateModule( "" );
820
821   saveWindowsGeometry();
822 }
823
824 void SalomeApp_Application::onLoadScript( )
825 {
826   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
827   if ( !appStudy ) return;
828   _PTR(Study) aStudy = appStudy->studyDS();
829   
830   if ( aStudy->GetProperties()->IsLocked() ) {
831     SUIT_MessageBox::warn1 ( desktop(),
832                              QObject::tr("WRN_WARNING"), 
833                              QObject::tr("WRN_STUDY_LOCKED"),
834                              QObject::tr("BUT_OK") );
835     return;
836   }
837   
838   QStringList filtersList;
839   filtersList.append(tr("PYTHON_FILES_FILTER"));
840   filtersList.append(tr("ALL_FILES_FILTER"));
841   
842   QString aFile = SUIT_FileDlg::getFileName(desktop(), "", filtersList, tr("TOT_DESK_FILE_LOAD_SCRIPT"), true, true );
843   if(!aFile.isEmpty()) {
844     QString command = QString("execfile(\"%1\")").arg(aFile);
845     
846     PythonConsole* pyConsole = pythonConsole();
847     
848     if(pyConsole)
849       pyConsole->exec(command);
850   }
851 }
852
853 QString SalomeApp_Application::getFileFilter() const
854 {
855   return "(*.hdf)";
856 }
857
858 void SalomeApp_Application::beforeCloseDoc( SUIT_Study* s )
859 {
860   CAM_Application::beforeCloseDoc( s );
861
862   for ( WindowMap::ConstIterator itr = myWindows.begin(); s && itr != myWindows.end(); ++itr )
863     removeWindow( itr.key(), s->id() );
864 }
865
866 void SalomeApp_Application::updateActions()
867 {
868   updateCommandsStatus();
869 }
870
871 QWidget* SalomeApp_Application::createWindow( const int flag )
872 {
873   QWidget* wid = 0;
874
875   if ( flag == WT_ObjectBrowser )
876   {
877     OB_Browser* ob = new OB_Browser( desktop() );
878     ob->setAutoUpdate( true );
879     ob->setAutoOpenLevel( 1 );
880     ob->setCaption( tr( "OBJECT_BROWSER" ) );
881     ob->resize( OBJECT_BROWSER_WIDTH, ob->height() );
882     ob->setFilter( new SalomeApp_OBFilter( selectionMgr() ) );
883
884     ob->setNameTitle( tr( "OBJ_BROWSER_NAME" ) );
885     for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
886       ob->addColumn( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), i );
887
888     // Create OBSelector
889     new SalomeApp_OBSelector( ob, mySelMgr );
890
891     wid = ob;
892
893     connect( ob, SIGNAL( aboutRefresh() ), this, SLOT( onAboutRefresh() ) );
894     ob->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
895   }
896   else if ( flag == WT_PyConsole )
897   {
898     PythonConsole* pyCons = new PythonConsole( desktop(), new SalomeApp_PyInterp() );
899     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
900     wid = pyCons;
901
902     //    pyCons->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
903   }
904   else if ( flag == WT_LogWindow )
905   {
906     LogWindow* logWin = new LogWindow( desktop() );
907     logWin->setCaption( tr( "LOG_WINDOW" ) );
908     wid = logWin;
909
910     logWin->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
911   }
912
913   return wid;
914 }
915
916 void SalomeApp_Application::defaultWindows( QMap<int, int>& aMap ) const
917 {
918   aMap.insert( WT_ObjectBrowser, Qt::DockLeft );
919   aMap.insert( WT_PyConsole, Qt::DockBottom );
920   aMap.insert( WT_LogWindow, Qt::DockBottom );
921 }
922
923 void SalomeApp_Application::defaultViewManagers( QStringList& ) const
924 {
925 }
926
927 void SalomeApp_Application::afterCloseDoc()
928 {
929   updateWindows();
930
931   CAM_Application::afterCloseDoc();
932 }
933
934 CORBA::ORB_var SalomeApp_Application::orb()
935 {
936   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
937   static CORBA::ORB_var _orb = init( qApp->argc(), qApp->argv() );
938   return _orb;
939 }
940
941 SALOMEDSClient_StudyManager* SalomeApp_Application::studyMgr()
942 {
943   static SALOMEDSClient_StudyManager* _sm = new SALOMEDS_StudyManager();
944   return _sm;
945 }
946
947 SALOME_NamingService* SalomeApp_Application::namingService()
948 {
949   static SALOME_NamingService* _ns = new SALOME_NamingService( orb() );
950   return _ns;
951 }
952
953 SALOME_LifeCycleCORBA* SalomeApp_Application::lcc()
954 {
955   static SALOME_LifeCycleCORBA* _lcc = new SALOME_LifeCycleCORBA( namingService() );
956   return _lcc;
957 }
958
959 QString SalomeApp_Application::defaultEngineIOR()
960 {
961   // Look for a default module engine (needed for CORBAless modules to use SALOMEDS persistence)
962   QString anIOR( "" );
963   CORBA::Object_ptr anEngine = namingService()->Resolve( "/SalomeAppEngine" );
964   if ( !CORBA::is_nil( anEngine ) )
965     anIOR = orb()->object_to_string( anEngine );
966   return anIOR;
967 }
968
969 void SalomeApp_Application::moduleIconNames( QMap<QString, QString>& iconMap ) const
970 {
971   iconMap.clear();
972
973   SUIT_ResourceMgr* resMgr = resourceMgr();
974   if ( !resMgr )
975     return;
976
977   QStringList modList;
978   modules( modList, false );
979
980   for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
981   {
982     QString modName = *it;
983     QString modIntr = moduleName( modName );
984     QString modIcon = resMgr->stringValue( modIntr, "icon", QString::null );
985
986     if ( modIcon.isEmpty() )
987       continue;
988
989     if ( SUIT_Tools::extension( modIcon ).isEmpty() )
990       modIcon += QString( ".png" );
991
992     iconMap.insert( modName, modIcon );
993   }
994 }
995
996 void SalomeApp_Application::updateModuleActions()
997 {
998   QString modName;
999   if ( activeModule() )
1000     modName = activeModule()->moduleName();
1001
1002   if ( myActions.contains( modName ) )
1003     myActions[modName]->setOn( true );
1004 }
1005
1006 void SalomeApp_Application::currentWindows( QMap<int, int>& winMap ) const
1007 {
1008   winMap.clear();
1009   if ( !activeStudy() )
1010     return;
1011
1012   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1013     ((SalomeApp_Module*)activeModule())->windows( winMap );
1014   else
1015     defaultWindows( winMap );
1016 }
1017
1018 void SalomeApp_Application::currentViewManagers( QStringList& lst ) const
1019 {
1020   lst.clear();
1021   if ( !activeStudy() )
1022     return;
1023
1024   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1025     ((SalomeApp_Module*)activeModule())->viewManagers( lst );
1026   else
1027     defaultViewManagers( lst );
1028 }
1029
1030 void SalomeApp_Application::updateWindows()
1031 {
1032   QMap<int, int> winMap;
1033   currentWindows( winMap );
1034
1035   for ( QMap<int, int>::ConstIterator it = winMap.begin(); it != winMap.end(); ++it )
1036     getWindow( it.key() );
1037
1038   loadWindowsGeometry();
1039
1040   for ( WindowMap::ConstIterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1041     setWindowShown( itr.key(), !itr.data()->isEmpty() && winMap.contains( itr.key() ) );
1042 }
1043
1044 void SalomeApp_Application::updateViewManagers()
1045 {
1046   QStringList lst;
1047   currentViewManagers( lst );
1048
1049   for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
1050     getViewManager( *it, true );
1051 }
1052
1053 void SalomeApp_Application::loadWindowsGeometry()
1054 {
1055   QtxDockAction* dockMgr = 0;
1056
1057   QAction* a = action( ViewWindowsId );
1058   if ( a && a->inherits( "QtxDockAction" ) )
1059     dockMgr = (QtxDockAction*)a;
1060
1061   if ( !dockMgr )
1062     return;
1063
1064   QString modName;
1065   if ( activeModule() )
1066     modName = moduleLibrary( activeModule()->moduleName(), false );
1067   
1068   QString section = QString( "windows_geometry" );
1069   if ( !modName.isEmpty() )
1070     section += QString( "." ) + modName;
1071
1072   dockMgr->loadGeometry( resourceMgr(), section, false );
1073   dockMgr->restoreGeometry();
1074 }
1075
1076 void SalomeApp_Application::saveWindowsGeometry()
1077 {
1078   QtxDockAction* dockMgr = 0;
1079
1080   QAction* a = action( ViewWindowsId );
1081   if ( a && a->inherits( "QtxDockAction" ) )
1082     dockMgr = (QtxDockAction*)a;
1083
1084   if ( !dockMgr )
1085     return;
1086
1087   QString modName;
1088   if ( activeModule() )
1089     modName = moduleLibrary( activeModule()->moduleName(), false );
1090   
1091   QString section = QString( "windows_geometry" );
1092   if ( !modName.isEmpty() )
1093     section += QString( "." ) + modName;
1094
1095   dockMgr->storeGeometry();
1096   dockMgr->saveGeometry( resourceMgr(), section, false );
1097 }
1098
1099 void SalomeApp_Application::activateWindows()
1100 {
1101   if ( activeStudy() )
1102   {
1103     for ( WindowMap::Iterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1104       itr.data()->activate( activeStudy()->id() );
1105   }
1106 }
1107
1108 void SalomeApp_Application::onProperties()
1109 {
1110   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
1111   if( !study )
1112     return;
1113
1114   _PTR(StudyBuilder) SB = study->studyDS()->NewBuilder();
1115   SB->NewCommand();
1116
1117   SalomeApp_StudyPropertiesDlg aDlg( desktop() );
1118   int res = aDlg.exec();
1119   if( res==QDialog::Accepted && aDlg.isChanged() )
1120     SB->CommitCommand();
1121   else
1122     SB->AbortCommand();
1123
1124   //study->updateCaptions();
1125 }
1126
1127 QString SalomeApp_Application::getFileName( bool open, const QString& initial, const QString& filters, 
1128                                             const QString& caption, QWidget* parent )
1129 {
1130   if ( !parent )
1131     parent = desktop();
1132   QStringList fls = QStringList::split( ";", filters, false );
1133   return SUIT_FileDlg::getFileName( parent, initial, fls, caption, open, true );
1134 }