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