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