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