Salome HOME
3f9eb7c3067fe6ce905eddcf3e740e908b52342e
[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_Preferences.h"
21 #include "SalomeApp_PreferencesDlg.h"
22 #include "SalomeApp_StudyPropertiesDlg.h"
23 #include "SalomeApp_CheckFileDlg.h"
24
25 #include "SalomeApp_GLSelector.h"
26 #include "SalomeApp_OBSelector.h"
27 #include "SalomeApp_OCCSelector.h"
28 #include "SalomeApp_VTKSelector.h"
29 #include "SalomeApp_SelectionMgr.h"
30
31 #include <LogWindow.h>
32
33 #include <GLViewer_Viewer.h>
34 #include <GLViewer_ViewManager.h>
35
36 #include <Plot2d_ViewManager.h>
37 #include <SPlot2d_ViewModel.h>
38
39 #include <OCCViewer_ViewManager.h>
40 #include <SOCC_ViewModel.h>
41
42 #include <SVTK_ViewModel.h>
43 #include <SVTK_ViewManager.h>
44
45 #include <STD_TabDesktop.h>
46
47 #include <SUIT_Tools.h>
48 #include <SUIT_Session.h>
49
50 #include <QtxToolBar.h>
51 #include <QtxMRUAction.h>
52 #include <QtxDockAction.h>
53 #include <QtxResourceEdit.h>
54
55 #include <OB_Browser.h>
56
57 #include <PythonConsole_PyConsole.h>
58
59 #include <SUIT_FileDlg.h>
60 #include <SUIT_MessageBox.h>
61 #include <SUIT_ResourceMgr.h>
62 #include <SUIT_ActionOperation.h>
63
64 #include <Utils_ORB_INIT.hxx>
65 #include <Utils_SINGLETON.hxx>
66 #include <SALOME_ModuleCatalog_impl.hxx>
67 #include <SALOME_LifeCycleCORBA.hxx>
68
69 #include <qmap.h>
70 #include <qdir.h>
71 #include <qlabel.h>
72 #include <qimage.h>
73 #include <qaction.h>
74 #include <qmenubar.h>
75 #include <qcombobox.h>
76 #include <qmessagebox.h>
77 #include <qapplication.h>
78
79 #include "SALOMEDS_StudyManager.hxx"
80
81 #include "SALOME_ListIteratorOfListIO.hxx"
82 #include "SALOME_ListIO.hxx"
83
84 #define OBJECT_BROWSER_WIDTH 300
85
86 static const char* imageEmptyIcon[] = {
87 "20 20 1 1",
88 ".      c None",
89 "....................",
90 "....................",
91 "....................",
92 "....................",
93 "....................",
94 "....................",
95 "....................",
96 "....................",
97 "....................",
98 "....................",
99 "....................",
100 "....................",
101 "....................",
102 "....................",
103 "....................",
104 "....................",
105 "....................",
106 "....................",
107 "....................",
108 "...................."};
109
110 extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
111 {
112   return new SalomeApp_Application();
113 }
114
115 SalomeApp_Preferences* SalomeApp_Application::_prefs_ = 0;
116
117 /*
118   Class       : SalomeApp_Application
119   Description : Application containing SalomeApp module
120 */
121
122 SalomeApp_Application::SalomeApp_Application()
123 : CAM_Application( false ),
124 myPrefs( 0 )
125 {
126   STD_TabDesktop* desk = new STD_TabDesktop();
127
128   setDesktop( desk );
129
130   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
131   QPixmap aLogo = aResMgr->loadPixmap( "SalomeApp", tr( "APP_DEFAULT_ICO" ), false );
132
133   desktop()->setIcon( aLogo );
134   desktop()->setDockableMenuBar( true );
135   desktop()->setDockableStatusBar( false );
136
137   clearViewManagers();
138
139   mySelMgr = new SalomeApp_SelectionMgr( this );
140
141   connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
142            this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
143
144   connect( mySelMgr, SIGNAL( selectionChanged() ), this, SLOT( onSelection() ) );
145 }
146
147 SalomeApp_Application::~SalomeApp_Application()
148 {
149   saveWindowsGeometry();
150
151   if ( resourceMgr() )
152   {
153     if ( desktop() )
154       desktop()->saveGeometry( resourceMgr(), "desktop" );
155     resourceMgr()->save();
156   }
157
158   delete mySelMgr;
159
160   SalomeApp_EventFilter::Destroy();
161 }
162
163 void SalomeApp_Application::start()
164 {
165   if ( desktop() )
166     desktop()->loadGeometry( resourceMgr(), "desktop" );
167
168   CAM_Application::start();
169
170   QAction* a = action( ViewWindowsId );
171   if ( a && a->inherits( "QtxDockAction" ) )
172     ((QtxDockAction*)a)->setAutoPlace( true );
173
174   SalomeApp_EventFilter::Init();
175
176   updateWindows();
177   updateViewManagers();
178
179   putInfo( "" );
180 }
181
182 QString SalomeApp_Application::applicationName() const
183 {
184   return tr( "APP_NAME" );
185 }
186
187 QString SalomeApp_Application::applicationVersion() const
188 {
189   static QString _app_version;
190
191   if ( _app_version.isEmpty() )
192   {
193     QString path( ::getenv( "GUI_ROOT_DIR" ) );
194     if ( !path.isEmpty() )
195       path += QDir::separator();
196     path += QString( "bin/salome/VERSION" );
197
198     QFile vf( path );
199     if ( vf.open( IO_ReadOnly ) )
200     {
201       QString line;
202       vf.readLine( line, 1024 );
203       vf.close();
204
205       if ( !line.isEmpty() )
206       {
207         while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
208           line.remove( line.length() - 1, 1 );
209
210         int idx = line.findRev( ":" );
211         if ( idx != -1 )
212           _app_version = line.mid( idx + 1 ).stripWhiteSpace();
213       }
214     }
215   }
216
217   return _app_version;
218 }
219
220 CAM_Module* SalomeApp_Application::loadModule( const QString& name )
221 {
222   CAM_Module* mod = CAM_Application::loadModule( name );
223   if ( mod )
224   {
225     connect( this, SIGNAL( studyOpened() ), mod, SLOT( onModelOpened() ) );
226     connect( this, SIGNAL( studySaved() ),  mod, SLOT( onModelSaved() ) );
227     connect( this, SIGNAL( studyClosed() ), mod, SLOT( onModelClosed() ) );
228   }
229   return mod;
230 }
231
232 bool SalomeApp_Application::activateModule( const QString& modName )
233 {
234   QString actName;
235   CAM_Module* prevMod = activeModule();
236
237   if ( prevMod )
238     actName = prevMod->moduleName();
239
240   if ( actName == modName )
241     return true;
242
243   saveWindowsGeometry();
244
245   bool status = CAM_Application::activateModule( modName );
246
247   updateModuleActions();
248
249   if ( !status )
250     return false;
251
252   updateWindows();
253   updateViewManagers();
254
255   return true;
256 }
257
258 SalomeApp_SelectionMgr* SalomeApp_Application::selectionMgr() const
259 {
260   return mySelMgr;
261 }
262
263 void SalomeApp_Application::createActions()
264 {
265   STD_Application::createActions();
266
267   SUIT_Desktop* desk = desktop();
268   SUIT_ResourceMgr* resMgr = resourceMgr();
269   
270   // Dump study
271   createAction( DumpStudyId, tr( "TOT_DESK_FILE_DUMP_STUDY" ), QIconSet(),
272                 tr( "MEN_DESK_FILE_DUMP_STUDY" ), tr( "PRP_DESK_FILE_DUMP_STUDY" ),
273                 0, desk, false, this, SLOT( onDumpStudy() ) );
274     
275   // Load script
276   createAction( LoadScriptId, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), QIconSet(),
277                 tr( "MEN_DESK_FILE_LOAD_SCRIPT" ), tr( "PRP_DESK_FILE_LOAD_SCRIPT" ),
278                 0, desk, false, this, SLOT( onLoadScript() ) );
279
280   // Properties
281   createAction( PropertiesId, tr( "TOT_DESK_PROPERTIES" ), QIconSet(),
282                 tr( "MEN_DESK_PROPERTIES" ), tr( "PRP_DESK_PROPERTIES" ),
283                 0, desk, false, this, SLOT( onProperties() ) );
284
285   // Preferences
286   createAction( PreferencesId, tr( "TOT_DESK_PREFERENCES" ), QIconSet(),
287                 tr( "MEN_DESK_PREFERENCES" ), tr( "PRP_DESK_PREFERENCES" ),
288                 CTRL+Key_P, desk, false, this, SLOT( onPreferences() ) );
289
290   // MRU
291   QtxMRUAction* mru = new QtxMRUAction( tr( "TOT_DESK_MRU" ), tr( "MEN_DESK_MRU" ), desk );
292   connect( mru, SIGNAL( activated( QString ) ), this, SLOT( onMRUActivated( QString ) ) );
293   registerAction( MRUId, mru );
294
295   // default icon for neutral point ('SALOME' module)
296   QPixmap defIcon = resMgr->loadPixmap( "SalomeApp", tr( "APP_DEFAULT_ICO" ), false );
297   if ( defIcon.isNull() )
298     defIcon = QPixmap( imageEmptyIcon );
299
300   // default icon for any module
301   QPixmap modIcon = resMgr->loadPixmap( "SalomeApp", tr( "APP_MODULE_ICO" ), false );
302   if ( modIcon.isNull() )
303     modIcon = QPixmap( imageEmptyIcon );
304
305   QToolBar* modTBar = new QtxToolBar( true, desk );
306   modTBar->setLabel( tr( "INF_TOOLBAR_MODULES" ) );
307
308   QActionGroup* modGroup = new QActionGroup( this );
309   modGroup->setExclusive( true );
310   modGroup->setUsesDropDown( true );
311
312   QAction* a = createAction( -1, tr( "APP_NAME" ), defIcon, tr( "APP_NAME" ),
313                              tr( "PRP_APP_MODULE" ), 0, desk, true );
314   modGroup->add( a );
315   myActions.insert( QString(), a );
316
317   QMap<QString, QString> iconMap;
318   moduleIconNames( iconMap );
319
320   const int iconSize = 20;
321
322   modGroup->addTo( modTBar );
323   modTBar->addSeparator();
324
325   QStringList modList;
326   modules( modList, false );
327
328   for ( QStringList::Iterator it = modList.begin(); it != modList.end(); ++it )
329   {
330     if ( (*it).isEmpty() )
331       continue;
332
333     QString iconName;
334     if ( iconMap.contains( *it ) )
335       iconName = iconMap[*it];
336
337     QString modName = moduleName( *it );
338
339     QPixmap icon = resMgr->loadPixmap( modName, iconName, false );
340     if ( icon.isNull() )
341       icon = modIcon;
342
343     icon.convertFromImage( icon.convertToImage().smoothScale( iconSize, iconSize, QImage::ScaleMin ) );
344
345     QAction* a = createAction( -1, *it, icon, *it, tr( "PRP_MODULE" ).arg( *it ), 0, desk, true );
346     a->addTo( modTBar );
347     modGroup->add( a );
348
349     myActions.insert( *it, a );
350   }
351
352   SUIT_Tools::simplifySeparators( modTBar );
353
354   // New window
355
356   int windowMenu = createMenu( tr( "MEN_DESK_WINDOW" ), -1, 100 );
357   int newWinMenu = createMenu( tr( "MEN_DESK_NEWWINDOW" ), windowMenu, -1, 0 );
358   createMenu( separator(), windowMenu, -1, 1 );
359
360   QMap<int, int> accelMap;
361   accelMap[NewGLViewId]  = ALT+Key_G;
362   accelMap[NewPlot2dId]  = ALT+Key_P;
363   accelMap[NewOCCViewId] = ALT+Key_O;
364   accelMap[NewVTKViewId] = ALT+Key_K;
365
366   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
367   {
368     QAction* a = createAction( id, tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ), QIconSet(),
369                                tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ),
370                                tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ),
371                                accelMap.contains( id ) ? accelMap[id] : 0, desk, false, this, SLOT( onNewWindow() ) );
372     createMenu( a, newWinMenu, -1 );
373   }
374   connect( modGroup, SIGNAL( selected( QAction* ) ), this, SLOT( onModuleActivation( QAction* ) ) );
375
376   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1 );
377
378   createMenu( DumpStudyId, fileMenu, 10, -1 );
379   createMenu( separator(), fileMenu, -1, 15, -1 );
380   createMenu( LoadScriptId, fileMenu, 10, -1 );
381   createMenu( separator(), fileMenu, -1, 15, -1 );
382   createMenu( PropertiesId, fileMenu, 10, -1 );
383   createMenu( separator(), fileMenu, -1, 15, -1 );
384   createMenu( PreferencesId, fileMenu, 15, -1 );
385   createMenu( separator(), fileMenu, -1, 15, -1 );
386
387   /*
388   createMenu( separator(), fileMenu, -1, 100, -1 );
389   createMenu( MRUId, fileMenu, 100, -1 );
390   createMenu( separator(), fileMenu, -1, 100, -1 );
391   */
392 }
393
394 void SalomeApp_Application::onModuleActivation( QAction* a )
395 {
396   if ( !a )
397     return;
398
399   QString modName = a->menuText();
400   if ( modName == tr( "APP_NAME" ) )
401     modName = QString::null;
402
403   // Force user to create/open a study before module activation
404   QMap<QString, QString> iconMap;
405   moduleIconNames( iconMap );
406   QPixmap icon = resourceMgr()->loadPixmap( moduleName( modName ), iconMap[ modName ], false );
407   if ( icon.isNull() )
408     icon = resourceMgr()->loadPixmap( "SalomeApp", tr( "APP_MODULE_BIG_ICO" ), false ); // default icon for any module
409
410   bool cancelled = false;
411   while ( !modName.isEmpty() && !activeStudy() && !cancelled ){
412     SalomeApp_ModuleDlg aDlg( desktop(), modName, icon );
413     int res = aDlg.exec();
414
415     switch ( res ){
416     case 1:
417       onNewDoc();
418       break;
419     case 2:
420       onOpenDoc();
421       break;
422     case 3:
423       //onLoadStudy();
424       //break;
425     case 0:
426     default:
427       putInfo( tr("INF_CANCELLED") );
428       myActions[QString()]->setOn( true );
429       cancelled = true;
430     }
431   }
432
433   if ( !cancelled )
434     activateModule( modName );
435 }
436
437 QString SalomeApp_Application::defaultModule() const
438 {
439   QStringList aModuleNames;
440   modules( aModuleNames, false ); // obtain a complete list of module names for the current configuration
441   // If there's the one and only module --> activate it automatically
442   // TODO: Possible improvement - default module can be taken from preferences
443   return aModuleNames.count() > 1 ? "" : ( aModuleNames.count() ? aModuleNames.first() : "" );
444 }
445
446 void SalomeApp_Application::onNewWindow()
447 {
448   const QObject* obj = sender();
449   if ( !obj || !obj->inherits( "QAction" ) )
450     return;
451
452   QString type;
453   int id = actionId( (QAction*)obj );
454   switch ( id )
455   {
456   case NewGLViewId:
457     type = GLViewer_Viewer::Type();
458     break;
459   case NewPlot2dId:
460     type = Plot2d_Viewer::Type();
461     break;
462   case NewOCCViewId:
463     type = OCCViewer_Viewer::Type();
464     break;
465   case NewVTKViewId:
466     type = VTKViewer_Viewer::Type();
467     break;
468   }
469
470   if ( !type.isEmpty() )
471     createViewManager( type );
472 }
473
474 //=======================================================================
475 // name    : onNewDoc
476 // Purpose : SLOT. Create new document
477 //=======================================================================
478 void SalomeApp_Application::onNewDoc()
479 {
480   SUIT_Study* study = activeStudy();
481
482   saveWindowsGeometry();
483
484   CAM_Application::onNewDoc();
485
486   if ( !study ) // new study will be create in THIS application
487   {
488     updateWindows();
489     updateViewManagers();
490   }
491 }
492
493 //=======================================================================
494 // name    : onOpenDoc
495 // Purpose : SLOT. Open new document
496 //=======================================================================
497 void SalomeApp_Application::onOpenDoc()
498 {
499   SUIT_Study* study = activeStudy();
500   saveWindowsGeometry();
501
502   CAM_Application::onOpenDoc();
503
504   if ( !study ) // new study will be create in THIS application
505   {
506     updateWindows();
507     updateViewManagers();
508   }
509 }
510
511 bool SalomeApp_Application::onOpenDoc( const QString& aName )
512 {
513   bool res = CAM_Application::onOpenDoc( aName );
514
515   QAction* a = action( MRUId );
516   if ( a && a->inherits( "QtxMRUAction" ) )
517   {
518     QtxMRUAction* mru = (QtxMRUAction*)a;
519     if ( res )
520       mru->insert( aName );
521     else
522       mru->remove( aName );
523   }
524   return res;
525 }
526
527 void SalomeApp_Application::onSelection()
528 {
529   onSelectionChanged();
530
531   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
532     ((SalomeApp_Module*)activeModule())->selectionChanged();
533 }
534
535 void SalomeApp_Application::onCopy() 
536 {
537   SALOME_ListIO list;
538   SalomeApp_SelectionMgr* mgr = selectionMgr();
539   mgr->selectedObjects(list);
540   
541   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
542   if(study == NULL) return;
543   
544   _PTR(Study) stdDS = study->studyDS();
545   if(!stdDS) return;
546
547   SALOME_ListIteratorOfListIO it( list ); 
548   if(it.More())
549     {
550       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
551       try {
552         studyMgr()->Copy(so);
553         onSelectionChanged();
554       }
555       catch(...) {
556       }
557     }
558 }
559
560 void SalomeApp_Application::onPaste() 
561 {
562   SALOME_ListIO list;
563   SalomeApp_SelectionMgr* mgr = selectionMgr();
564   mgr->selectedObjects(list);
565   
566   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
567   if(study == NULL) return;
568   
569   _PTR(Study) stdDS = study->studyDS();
570   if(!stdDS) return;
571   
572   SALOME_ListIteratorOfListIO it( list ); 
573   if(it.More())
574     {
575       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
576       try {
577         studyMgr()->Paste(so);
578         updateObjectBrowser( true );
579       }
580       catch(...) {
581       }
582     }
583 }
584
585 void SalomeApp_Application::onSelectionChanged()
586 {
587    SALOME_ListIO list;
588    SalomeApp_SelectionMgr* mgr = selectionMgr();
589    mgr->selectedObjects(list);
590    
591    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
592    if(study == NULL) return;
593    
594    _PTR(Study) stdDS = study->studyDS();
595    if(!stdDS) return;
596    
597    QAction* qaction;  
598
599    SALOME_ListIteratorOfListIO it( list ); 
600    if(it.More() && list.Extent() == 1)
601    {
602       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
603
604       qaction = action(EditCopyId); 
605       if(studyMgr()->CanCopy(so) ) qaction->setEnabled(true);  
606       else qaction->setEnabled(false);         
607      
608       qaction = action(EditPasteId);
609       if( studyMgr()->CanPaste(so) ) qaction->setEnabled(true);
610       else qaction->setEnabled(false);
611    } 
612    else {
613      qaction = action(EditCopyId); 
614      qaction->setEnabled(false); 
615      qaction = action(EditPasteId);
616      qaction->setEnabled(false);
617    }
618 }               
619  
620 void SalomeApp_Application::onRefresh()
621 {
622   updateObjectBrowser( true );
623 }
624
625  
626 void SalomeApp_Application::onOpenWith()
627 {
628   QApplication::setOverrideCursor( Qt::waitCursor );
629   SALOME_ListIO aList;
630   SalomeApp_SelectionMgr* mgr = selectionMgr();
631   mgr->selectedObjects(aList);
632   if (aList.Extent() != 1)
633     {
634       QApplication::restoreOverrideCursor();
635       return;
636     }
637   Handle(SALOME_InteractiveObject) aIObj = aList.First();
638   QString aModuleName(aIObj->getComponentDataType());
639   QString aModuleTitle = moduleTitle(aModuleName);
640   activateModule(aModuleTitle);
641   QApplication::restoreOverrideCursor();
642 }
643
644 void SalomeApp_Application::setActiveStudy( SUIT_Study* study )
645 {
646   CAM_Application::setActiveStudy( study );
647
648   activateWindows();
649 }
650
651 //=======================================================================
652 // name    : createNewStudy
653 // Purpose : Create new study
654 //=======================================================================
655 SUIT_Study* SalomeApp_Application::createNewStudy()
656 {
657   SalomeApp_Study* aStudy = new SalomeApp_Study( this );
658
659   // Set up processing of major study-related events
660   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
661   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
662   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
663   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
664
665   return aStudy;
666 }
667
668 //=======================================================================
669 // name    : createNewStudy
670 // Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu
671 //=======================================================================
672 void SalomeApp_Application::updateCommandsStatus()
673 {
674   CAM_Application::updateCommandsStatus();
675
676   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
677   {
678     QAction* a = action( id );
679     if ( a )
680       a->setEnabled( activeStudy() );
681   }
682
683   // Dump study menu
684   QAction* a = action( DumpStudyId );
685   if ( a )
686     a->setEnabled( activeStudy() );
687
688   // Load script menu
689   a = action( LoadScriptId );
690   if ( a )
691     a->setEnabled( activeStudy() );
692   
693   a = action( PropertiesId );
694   if( a )
695     a->setEnabled( activeStudy() );
696     
697   a = action(EditCopyId);
698   a->setEnabled(false);       
699   a = action(EditPasteId);
700   a->setEnabled(false);      
701 }
702
703 //=======================================================================
704 // name    : onHelpAbout
705 // Purpose : SLOT. Display "About" message box
706 //=======================================================================
707 void SalomeApp_Application::onHelpAbout()
708 {
709   SalomeApp_AboutDlg* dlg = new SalomeApp_AboutDlg( applicationName(), applicationVersion(), desktop() );
710   dlg->exec();
711   delete dlg;
712 }
713
714 QWidget* SalomeApp_Application::window( const int flag, const int studyId ) const
715 {
716   QWidget* wid = 0;
717
718   int sId = studyId;
719   if ( sId < 0 )
720   {
721     if ( !activeStudy() )
722       return 0;
723     else
724       sId = activeStudy()->id();
725   }
726
727   if ( myWindows.contains( flag ) )
728     wid = myWindows[flag]->widget( sId );
729
730   return wid;
731 }
732
733 void SalomeApp_Application::addWindow( QWidget* wid, const int flag, const int studyId )
734 {
735   if ( !wid )
736     return;
737
738   int sId = studyId;
739   if ( sId < 0 )
740   {
741     if ( !activeStudy() )
742       return;
743     else
744       sId = activeStudy()->id();
745   }
746
747   if ( !myWindows.contains( flag ) )
748   {
749     QMap<int, int> winMap;
750     currentWindows( winMap );
751
752     myWindows.insert( flag, new SalomeApp_WidgetContainer( flag, desktop() ) );
753     if ( winMap.contains( flag ) )
754       desktop()->moveDockWindow( myWindows[flag], (Dock)winMap[flag] );
755
756     myWindows[flag]->setResizeEnabled( true );
757     myWindows[flag]->setCloseMode( QDockWindow::Always );
758     myWindows[flag]->setName( QString( "dock_window_%1" ).arg( flag ) );
759   }
760
761   myWindows[flag]->insert( sId, wid );
762
763   setWindowShown( flag, !myWindows[flag]->isEmpty() );
764 }
765
766 void SalomeApp_Application::removeWindow( const int flag, const int studyId )
767 {
768   if ( !myWindows.contains( flag ) )
769     return;
770
771   int sId = studyId;
772   if ( sId < 0 )
773   {
774     if ( !activeStudy() )
775       return;
776     else
777       sId = activeStudy()->id();
778   }
779
780   QWidget* wid = myWindows[flag]->widget( sId );
781   myWindows[flag]->remove( sId );
782   delete wid;
783
784   setWindowShown( flag, !myWindows[flag]->isEmpty() );
785 }
786
787 QWidget* SalomeApp_Application::getWindow( const int flag, const int studyId )
788 {
789   QWidget* wid = window( flag, studyId );
790   if ( !wid )
791     addWindow( wid = createWindow( flag ), flag, studyId );
792
793   return wid;
794 }
795
796 bool SalomeApp_Application::isWindowVisible( const int type ) const
797 {
798   bool res = false;
799   if ( myWindows.contains( type ) )
800   {
801     SUIT_Desktop* desk = ((SalomeApp_Application*)this)->desktop();
802     res = desk && desk->appropriate( myWindows[type] );
803   }
804   return res;
805 }
806
807 void SalomeApp_Application::setWindowShown( const int type, const bool on )
808 {
809   if ( !desktop() || !myWindows.contains( type ) )
810     return;
811
812   QDockWindow* dw = myWindows[type];
813   desktop()->setAppropriate( dw, on );
814   on ? dw->show() : dw->hide();
815 }
816
817 OB_Browser* SalomeApp_Application::objectBrowser()
818 {
819   OB_Browser* ob = 0;
820   QWidget* wid = getWindow( WT_ObjectBrowser );
821   if ( wid->inherits( "OB_Browser" ) )
822     ob = (OB_Browser*)wid;
823   return ob;
824 }
825
826 LogWindow* SalomeApp_Application::logWindow()
827 {
828   LogWindow* lw = 0;
829   QWidget* wid = getWindow( WT_LogWindow );
830   if ( wid->inherits( "LogWindow" ) )
831     lw = (LogWindow*)wid;
832   return lw;
833 }
834
835 PythonConsole* SalomeApp_Application::pythonConsole()
836 {
837   PythonConsole* console = 0;
838   QWidget* wid = getWindow( WT_PyConsole );
839   if ( wid->inherits( "PythonConsole" ) )
840     console = (PythonConsole*)wid;
841   return console;
842 }
843
844 SalomeApp_Preferences* SalomeApp_Application::preferences() const
845 {
846   return preferences( false );
847 }
848
849 SUIT_ViewManager* SalomeApp_Application::getViewManager( const QString& vmType, const bool create )
850 {
851   SUIT_ViewManager* aVM = viewManager( vmType );
852   SUIT_ViewManager* anActiveVM = CAM_Application::activeViewManager();
853
854   if ( anActiveVM && anActiveVM->getType() == vmType )
855     aVM = anActiveVM;
856
857   if ( aVM && create )
858   {
859     if ( !aVM->getActiveView() )
860       aVM->createView();
861     else
862       aVM->getActiveView()->setFocus();
863   }
864   else if ( create )
865     aVM = createViewManager( vmType );
866
867   return aVM;
868 }
869
870 SUIT_ViewManager* SalomeApp_Application::createViewManager( const QString& vmType )
871 {
872   SUIT_ResourceMgr* resMgr = resourceMgr();
873
874   SUIT_ViewManager* viewMgr = 0;
875   if ( vmType == GLViewer_Viewer::Type() )
876   {
877     viewMgr = new GLViewer_ViewManager( activeStudy(), desktop() );
878     new SalomeApp_GLSelector( (GLViewer_Viewer2d*)viewMgr->getViewModel(), mySelMgr );
879   }
880   else if ( vmType == Plot2d_Viewer::Type() )
881   {
882     viewMgr = new Plot2d_ViewManager( activeStudy(), desktop() );
883     viewMgr->setViewModel( new SPlot2d_Viewer() );// custom view model, which extends SALOME_View interface
884   }
885   else if ( vmType == OCCViewer_Viewer::Type() )
886   {
887     viewMgr = new OCCViewer_ViewManager( activeStudy(), desktop() );
888     SOCC_Viewer* vm = new SOCC_Viewer();
889     vm->setBackgroundColor( resMgr->colorValue( "OCCViewer", "background", vm->backgroundColor() ) );
890     vm->setTrihedronSize( resMgr->integerValue( "OCCViewer", "trihedron_size", vm->trihedronSize() ) );
891     int u( 1 ), v( 1 );
892     vm->isos( u, v );
893     u = resMgr->integerValue( "OCCViewer", "iso_number_u", u );
894     v = resMgr->integerValue( "OCCViewer", "iso_number_v", v );
895     //    vm->setIsos( u, v );
896     viewMgr->setViewModel( vm );// custom view model, which extends SALOME_View interface
897     new SalomeApp_OCCSelector( (OCCViewer_Viewer*)viewMgr->getViewModel(), mySelMgr );
898   }
899   else if ( vmType == SVTK_Viewer::Type() )
900   {
901     viewMgr = new SVTK_ViewManager( activeStudy(), desktop() );
902     SVTK_Viewer* vm = (SVTK_Viewer*)viewMgr->getViewModel();
903     vm->setBackgroundColor( resMgr->colorValue( "VTKViewer", "background", vm->backgroundColor() ) );
904     vm->setTrihedronSize( resMgr->integerValue( "VTKViewer", "trihedron_size", vm->trihedronSize() ) );
905     new SalomeApp_VTKSelector((SVTK_Viewer*)viewMgr->getViewModel(),mySelMgr);
906   }
907
908   if ( !viewMgr )
909     return 0;
910
911   addViewManager( viewMgr );
912   SUIT_ViewWindow* viewWin = viewMgr->createViewWindow();
913
914   if ( viewWin && desktop() )
915     viewWin->resize( (int)( desktop()->width() * 0.6 ), (int)( desktop()->height() * 0.6 ) );
916
917   connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
918            this, SLOT( onCloseView( SUIT_ViewManager* ) ) );
919
920   return viewMgr;
921 }
922
923 void SalomeApp_Application::onCloseView( SUIT_ViewManager* theVM )
924 {
925   removeViewManager( theVM );
926 }
927
928 void SalomeApp_Application::onStudyCreated( SUIT_Study* theStudy )
929 {
930   SUIT_DataObject* aRoot = 0;
931   if ( theStudy && theStudy->root() )
932   {
933     aRoot = theStudy->root();
934     //aRoot->setName( tr( "DATA_MODELS" ) );
935   }
936   if ( objectBrowser() != 0 )
937     objectBrowser()->setRootObject( aRoot );
938
939   activateModule( defaultModule() );
940
941   activateWindows();
942 }
943
944 void SalomeApp_Application::onStudyOpened( SUIT_Study* theStudy )
945 {
946   SUIT_DataObject* aRoot = 0;
947   if ( theStudy && theStudy->root() )
948   {
949     aRoot = theStudy->root();
950     //aRoot->dump();
951   }
952   if ( objectBrowser() != 0 ) {
953     objectBrowser()->setRootObject( aRoot );
954   }
955
956   activateModule( defaultModule() );
957
958   activateWindows();
959
960   emit studyOpened();
961 }
962
963 void SalomeApp_Application::onStudySaved( SUIT_Study* )
964 {
965   emit studySaved();
966 }
967
968 void SalomeApp_Application::onStudyClosed( SUIT_Study* )
969 {
970   emit studyClosed();
971
972   activateModule( "" );
973
974   saveWindowsGeometry();
975 }
976
977 void SalomeApp_Application::onDumpStudy( )
978 {
979   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
980   if ( !appStudy ) return;
981   _PTR(Study) aStudy = appStudy->studyDS();
982
983   QStringList aFilters;
984   aFilters.append( tr( "PYTHON_FILES_FILTER" ) );
985
986   SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg( desktop(), false, tr("PUBLISH_IN_STUDY"), true, true);
987   fd->setCaption( tr( "TOT_DESK_FILE_DUMP_STUDY" ) );
988   fd->setFilters( aFilters );  
989   fd->SetChecked(true);
990   fd->exec();
991   QString aFileName = fd->selectedFile();
992   bool toPublish = fd->IsChecked();
993   delete fd;
994
995   if(!aFileName.isEmpty()) {
996     QFileInfo aFileInfo(aFileName);
997     aStudy->DumpStudy( aFileInfo.dirPath( true ).latin1(), aFileInfo.baseName().latin1(), toPublish );
998   }
999 }
1000
1001 void SalomeApp_Application::onLoadScript( )
1002 {
1003   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
1004   if ( !appStudy ) return;
1005   _PTR(Study) aStudy = appStudy->studyDS();
1006
1007   if ( aStudy->GetProperties()->IsLocked() ) {
1008     SUIT_MessageBox::warn1 ( desktop(),
1009                              QObject::tr("WRN_WARNING"),
1010                              QObject::tr("WRN_STUDY_LOCKED"),
1011                              QObject::tr("BUT_OK") );
1012     return;
1013   }
1014
1015   QStringList filtersList;
1016   filtersList.append(tr("PYTHON_FILES_FILTER"));
1017   filtersList.append(tr("ALL_FILES_FILTER"));
1018
1019   QString aFile = SUIT_FileDlg::getFileName( desktop(), "", filtersList, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), true, true );
1020
1021   if ( !aFile.isEmpty() )
1022   {
1023     QString command = QString("execfile(\"%1\")").arg(aFile);
1024
1025     PythonConsole* pyConsole = pythonConsole();
1026
1027     if ( pyConsole )
1028       pyConsole->exec( command );
1029   }
1030 }
1031
1032 void SalomeApp_Application::onPreferences()
1033 {
1034   QApplication::setOverrideCursor( Qt::waitCursor );
1035
1036   SalomeApp_PreferencesDlg* prefDlg = new SalomeApp_PreferencesDlg( preferences( true ), desktop());
1037
1038   QApplication::restoreOverrideCursor();
1039
1040   if ( !prefDlg )
1041     return;
1042
1043   prefDlg->exec();
1044
1045   delete prefDlg;
1046 }
1047
1048 void SalomeApp_Application::onMRUActivated( QString aName )
1049 {
1050   onOpenDoc( aName );
1051 }
1052
1053 void SalomeApp_Application::onPreferenceChanged( QString& modName, QString& section, QString& param )
1054 {
1055   SalomeApp_Module* sMod = 0;
1056   CAM_Module* mod = module( modName );
1057   if ( mod && mod->inherits( "SalomeApp_Module" ) )
1058     sMod = (SalomeApp_Module*)mod;
1059
1060   if ( sMod )
1061     sMod->preferencesChanged( section, param );
1062   else
1063     preferencesChanged( section, param );
1064 }
1065
1066 QString SalomeApp_Application::getFileFilter() const
1067 {
1068   return "(*.hdf)";
1069 }
1070
1071 void SalomeApp_Application::beforeCloseDoc( SUIT_Study* s )
1072 {
1073   CAM_Application::beforeCloseDoc( s );
1074
1075   for ( WindowMap::ConstIterator itr = myWindows.begin(); s && itr != myWindows.end(); ++itr )
1076     removeWindow( itr.key(), s->id() );
1077 }
1078
1079 void SalomeApp_Application::updateActions()
1080 {
1081   updateCommandsStatus();
1082 }
1083
1084 QWidget* SalomeApp_Application::createWindow( const int flag )
1085 {
1086   QWidget* wid = 0;
1087
1088   SUIT_ResourceMgr* resMgr = resourceMgr();
1089
1090   if ( flag == WT_ObjectBrowser )
1091   {
1092     OB_Browser* ob = new OB_Browser( desktop() );
1093     ob->setAutoUpdate( true );
1094     ob->setAutoOpenLevel( 1 );
1095     ob->setCaption( tr( "OBJECT_BROWSER" ) );
1096     ob->resize( OBJECT_BROWSER_WIDTH, ob->height() );
1097     ob->setFilter( new SalomeApp_OBFilter( selectionMgr() ) );
1098
1099     ob->setNameTitle( tr( "OBJ_BROWSER_NAME" ) );
1100
1101     for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
1102     {
1103       ob->addColumn( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), i );
1104       ob->setColumnShown( i, resMgr->booleanValue( "ObjectBrowser",
1105                                                    QString().sprintf( "visibility_column_%d", i ), true ) );
1106     }
1107
1108     // Create OBSelector
1109     new SalomeApp_OBSelector( ob, mySelMgr );
1110
1111     wid = ob;
1112
1113     ob->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1114   }
1115   else if ( flag == WT_PyConsole )
1116   {
1117     PythonConsole* pyCons = new PythonConsole( desktop(), new SalomeApp_PyInterp() );
1118     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
1119     wid = pyCons;
1120
1121     //    pyCons->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1122   }
1123   else if ( flag == WT_LogWindow )
1124   {
1125     LogWindow* logWin = new LogWindow( desktop() );
1126     logWin->setCaption( tr( "LOG_WINDOW" ) );
1127     wid = logWin;
1128
1129     logWin->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1130   }
1131
1132   return wid;
1133 }
1134
1135 void SalomeApp_Application::defaultWindows( QMap<int, int>& aMap ) const
1136 {
1137   aMap.insert( WT_ObjectBrowser, Qt::DockLeft );
1138   aMap.insert( WT_PyConsole, Qt::DockBottom );
1139   //  aMap.insert( WT_LogWindow, Qt::DockBottom );
1140 }
1141
1142 void SalomeApp_Application::defaultViewManagers( QStringList& ) const
1143 {
1144 }
1145
1146 SalomeApp_Preferences* SalomeApp_Application::preferences( const bool crt ) const
1147 {
1148   if ( myPrefs )
1149     return myPrefs;
1150
1151   SalomeApp_Application* that = (SalomeApp_Application*)this;
1152
1153   if ( !_prefs_ && crt )
1154   {
1155     _prefs_ = new SalomeApp_Preferences( resourceMgr() );
1156     that->createPreferences( _prefs_ );
1157   }
1158
1159   that->myPrefs = _prefs_;
1160
1161   QPtrList<SUIT_Application> appList = SUIT_Session::session()->applications();
1162   for ( QPtrListIterator<SUIT_Application> appIt ( appList ); appIt.current(); ++appIt )
1163   {
1164     if ( !appIt.current()->inherits( "SalomeApp_Application" ) )
1165       continue;
1166
1167     SalomeApp_Application* app = (SalomeApp_Application*)appIt.current();
1168
1169     QStringList modNameList;
1170     app->modules( modNameList, false );
1171     for ( QStringList::const_iterator it = modNameList.begin(); it != modNameList.end(); ++it )
1172     {
1173       int id = _prefs_->addPreference( *it );
1174       _prefs_->setProperty( id, "info", tr( "PREFERENCES_NOT_LOADED" ).arg( *it ) );
1175     }
1176
1177     ModuleList modList;
1178     app->modules( modList );
1179     for ( ModuleListIterator itr( modList ); itr.current(); ++itr )
1180     {
1181       SalomeApp_Module* mod = 0;
1182       if ( itr.current()->inherits( "SalomeApp_Module" ) )
1183         mod = (SalomeApp_Module*)itr.current();
1184
1185       if ( mod && !_prefs_->hasModule( mod->moduleName() ) )
1186         mod->createPreferences();
1187     }
1188   }
1189
1190   connect( myPrefs, SIGNAL( preferenceChanged( QString&, QString&, QString& ) ),
1191            this, SLOT( onPreferenceChanged( QString&, QString&, QString& ) ) );
1192
1193   return myPrefs;
1194 }
1195
1196 void SalomeApp_Application::moduleAdded( CAM_Module* mod )
1197 {
1198   CAM_Application::moduleAdded( mod );
1199
1200   SalomeApp_Module* salomeMod = 0;
1201   if ( mod && mod->inherits( "SalomeApp_Module" ) )
1202     salomeMod = (SalomeApp_Module*)mod;
1203
1204   if ( myPrefs && salomeMod && !myPrefs->hasModule( salomeMod->moduleName() ))
1205     salomeMod->createPreferences();
1206 }
1207
1208 void SalomeApp_Application::createPreferences( SalomeApp_Preferences* pref )
1209 {
1210   if ( !pref )
1211     return;
1212
1213   int salomeCat = pref->addPreference( tr( "PREF_CATEGORY_SALOME" ) );
1214
1215   int genTab = pref->addPreference( tr( "PREF_TAB_GENERAL" ), salomeCat );
1216
1217   int obGroup = pref->addPreference( tr( "PREF_GROUP_OBJBROWSER" ), genTab );
1218   for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
1219   {
1220     pref->addPreference( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), obGroup,
1221                          SalomeApp_Preferences::Bool, "ObjectBrowser", QString().sprintf( "visibility_column_%d", i ) );
1222   }
1223   pref->setProperty( obGroup, "columns", 1 );
1224
1225   int viewTab = pref->addPreference( tr( "PREF_TAB_VIEWERS" ), salomeCat );
1226
1227   int occGroup = pref->addPreference( tr( "PREF_GROUP_OCCVIEWER" ), viewTab );
1228
1229   int vtkGroup = pref->addPreference( tr( "PREF_GROUP_VTKVIEWER" ), viewTab );
1230   pref->setProperty( occGroup, "columns", 1 );
1231   pref->setProperty( vtkGroup, "columns", 1 );
1232
1233   int occTS = pref->addPreference( tr( "PREF_TRIHEDRON_SIZE" ), occGroup,
1234                                    SalomeApp_Preferences::IntSpin, "OCCViewer", "trihedron_size" );
1235   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), occGroup,
1236                        SalomeApp_Preferences::Color, "OCCViewer", "background" );
1237
1238   pref->setProperty( occTS, "min", 1 );
1239   pref->setProperty( occTS, "max", 150 );
1240
1241   int isoU = pref->addPreference( tr( "PREF_ISOS_U" ), occGroup,
1242                                   SalomeApp_Preferences::IntSpin, "OCCViewer", "iso_number_u" );
1243   int isoV = pref->addPreference( tr( "PREF_ISOS_V" ), occGroup,
1244                                   SalomeApp_Preferences::IntSpin, "OCCViewer", "iso_number_v" );
1245
1246   pref->setProperty( isoU, "min", 0 );
1247   pref->setProperty( isoU, "max", 100000 );
1248
1249   pref->setProperty( isoV, "min", 0 );
1250   pref->setProperty( isoV, "max", 100000 );
1251
1252   int vtkTS = pref->addPreference( tr( "PREF_TRIHEDRON_SIZE" ), vtkGroup,
1253                                    SalomeApp_Preferences::IntSpin, "VTKViewer", "trihedron_size" );
1254   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), vtkGroup,
1255                        SalomeApp_Preferences::Color, "VTKViewer", "background" );
1256
1257   pref->setProperty( vtkTS, "min", 1 );
1258   pref->setProperty( vtkTS, "max", 150 );
1259 }
1260
1261 void SalomeApp_Application::preferencesChanged( const QString& sec, const QString& param )
1262 {
1263   SUIT_ResourceMgr* resMgr = resourceMgr();
1264   if ( !resMgr )
1265     return;
1266
1267   if ( sec == QString( "OCCViewer" ) && param == QString( "trihedron_size" ) )
1268   {
1269     int sz = resMgr->integerValue( sec, param, -1 );
1270     QPtrList<SUIT_ViewManager> lst;
1271     viewManagers( OCCViewer_Viewer::Type(), lst );
1272     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current() && sz >= 0; ++it )
1273     {
1274       SUIT_ViewModel* vm = it.current()->getViewModel();
1275       if ( !vm || !vm->inherits( "OCCViewer_Viewer" ) )
1276         continue;
1277
1278       OCCViewer_Viewer* occVM = (OCCViewer_Viewer*)vm;
1279       occVM->setTrihedronSize( sz );
1280       occVM->getAISContext()->UpdateCurrentViewer();
1281     }
1282   }
1283
1284   if ( sec == QString( "VTKViewer" ) && param == QString( "trihedron_size" ) )
1285   {
1286     int sz = resMgr->integerValue( sec, param, -1 );
1287     QPtrList<SUIT_ViewManager> lst;
1288     viewManagers( SVTK_Viewer::Type(), lst );
1289     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current() && sz >= 0; ++it )
1290     {
1291       SUIT_ViewModel* vm = it.current()->getViewModel();
1292       if ( !vm || !vm->inherits( "SVTK_Viewer" ) )
1293         continue;
1294
1295       SVTK_Viewer* vtkVM = (SVTK_Viewer*)vm;
1296       vtkVM->setTrihedronSize( sz );
1297       vtkVM->Repaint();
1298     }
1299   }
1300   /*
1301   if ( sec == QString( "OCCViewer" ) && ( param == QString( "iso_number_u" ) || param == QString( "iso_number_v" ) ) )
1302   {
1303     QPtrList<SUIT_ViewManager> lst;
1304     viewManagers( OCCViewer_Viewer::Type(), lst );
1305     int u = resMgr->integerValue( sec, "iso_number_u" );
1306     int v = resMgr->integerValue( sec, "iso_number_v" );
1307     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
1308       ((OCCViewer_Viewer*)it.current())->setIsos( u, v );
1309   }
1310   */
1311 }
1312
1313 void SalomeApp_Application::afterCloseDoc()
1314 {
1315   updateWindows();
1316
1317   CAM_Application::afterCloseDoc();
1318 }
1319
1320 CORBA::ORB_var SalomeApp_Application::orb()
1321 {
1322   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
1323   static CORBA::ORB_var _orb = init( qApp->argc(), qApp->argv() );
1324   return _orb;
1325 }
1326
1327 SALOMEDSClient_StudyManager* SalomeApp_Application::studyMgr()
1328 {
1329   static SALOMEDSClient_StudyManager* _sm = new SALOMEDS_StudyManager();
1330   return _sm;
1331 }
1332
1333 SALOME_NamingService* SalomeApp_Application::namingService()
1334 {
1335   static SALOME_NamingService* _ns = new SALOME_NamingService( orb() );
1336   return _ns;
1337 }
1338
1339 SALOME_LifeCycleCORBA* SalomeApp_Application::lcc()
1340 {
1341   static SALOME_LifeCycleCORBA* _lcc = new SALOME_LifeCycleCORBA( namingService() );
1342   return _lcc;
1343 }
1344
1345 QString SalomeApp_Application::defaultEngineIOR()
1346 {
1347   // Look for a default module engine (needed for CORBAless modules to use SALOMEDS persistence)
1348   QString anIOR( "" );
1349   CORBA::Object_ptr anEngine = namingService()->Resolve( "/SalomeAppEngine" );
1350   if ( !CORBA::is_nil( anEngine ) )
1351     anIOR = orb()->object_to_string( anEngine );
1352   return anIOR;
1353 }
1354
1355 void SalomeApp_Application::moduleIconNames( QMap<QString, QString>& iconMap ) const
1356 {
1357   iconMap.clear();
1358
1359   SUIT_ResourceMgr* resMgr = resourceMgr();
1360   if ( !resMgr )
1361     return;
1362
1363   QStringList modList;
1364   modules( modList, false );
1365
1366   for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
1367   {
1368     QString modName = *it;
1369     QString modIntr = moduleName( modName );
1370     QString modIcon = resMgr->stringValue( modIntr, "icon", QString::null );
1371
1372     if ( modIcon.isEmpty() )
1373       continue;
1374
1375     if ( SUIT_Tools::extension( modIcon ).isEmpty() )
1376       modIcon += QString( ".png" );
1377
1378     iconMap.insert( modName, modIcon );
1379   }
1380 }
1381
1382 void SalomeApp_Application::updateModuleActions()
1383 {
1384   QString modName;
1385   if ( activeModule() )
1386     modName = activeModule()->moduleName();
1387
1388   if ( myActions.contains( modName ) )
1389     myActions[modName]->setOn( true );
1390 }
1391
1392 void SalomeApp_Application::currentWindows( QMap<int, int>& winMap ) const
1393 {
1394   winMap.clear();
1395   if ( !activeStudy() )
1396     return;
1397
1398   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1399     ((SalomeApp_Module*)activeModule())->windows( winMap );
1400   else
1401     defaultWindows( winMap );
1402 }
1403
1404 void SalomeApp_Application::currentViewManagers( QStringList& lst ) const
1405 {
1406   lst.clear();
1407   if ( !activeStudy() )
1408     return;
1409
1410   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1411     ((SalomeApp_Module*)activeModule())->viewManagers( lst );
1412   else
1413     defaultViewManagers( lst );
1414 }
1415
1416 void SalomeApp_Application::updateWindows()
1417 {
1418   QMap<int, int> winMap;
1419   currentWindows( winMap );
1420
1421   for ( QMap<int, int>::ConstIterator it = winMap.begin(); it != winMap.end(); ++it )
1422     getWindow( it.key() );
1423
1424   loadWindowsGeometry();
1425
1426   for ( WindowMap::ConstIterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1427     setWindowShown( itr.key(), !itr.data()->isEmpty() && winMap.contains( itr.key() ) );
1428 }
1429
1430 void SalomeApp_Application::updateViewManagers()
1431 {
1432   QStringList lst;
1433   currentViewManagers( lst );
1434
1435   for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
1436     getViewManager( *it, true );
1437 }
1438
1439 void SalomeApp_Application::loadWindowsGeometry()
1440 {
1441   QtxDockAction* dockMgr = 0;
1442
1443   QAction* a = action( ViewWindowsId );
1444   if ( a && a->inherits( "QtxDockAction" ) )
1445     dockMgr = (QtxDockAction*)a;
1446
1447   if ( !dockMgr )
1448     return;
1449
1450   QString modName;
1451   if ( activeModule() )
1452     modName = moduleLibrary( activeModule()->moduleName(), false );
1453
1454   QString section = QString( "windows_geometry" );
1455   if ( !modName.isEmpty() )
1456     section += QString( "." ) + modName;
1457
1458   dockMgr->loadGeometry( resourceMgr(), section, false );
1459   dockMgr->restoreGeometry();
1460 }
1461
1462 void SalomeApp_Application::saveWindowsGeometry()
1463 {
1464   QtxDockAction* dockMgr = 0;
1465
1466   QAction* a = action( ViewWindowsId );
1467   if ( a && a->inherits( "QtxDockAction" ) )
1468     dockMgr = (QtxDockAction*)a;
1469
1470   if ( !dockMgr )
1471     return;
1472
1473   QString modName;
1474   if ( activeModule() )
1475     modName = moduleLibrary( activeModule()->moduleName(), false );
1476
1477   QString section = QString( "windows_geometry" );
1478   if ( !modName.isEmpty() )
1479     section += QString( "." ) + modName;
1480
1481   dockMgr->storeGeometry();
1482   dockMgr->saveGeometry( resourceMgr(), section, false );
1483 }
1484
1485 void SalomeApp_Application::activateWindows()
1486 {
1487   if ( activeStudy() )
1488   {
1489     for ( WindowMap::Iterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1490       itr.data()->activate( activeStudy()->id() );
1491   }
1492 }
1493
1494 void SalomeApp_Application::onProperties()
1495 {
1496   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
1497   if( !study )
1498     return;
1499
1500   _PTR(StudyBuilder) SB = study->studyDS()->NewBuilder();
1501   SB->NewCommand();
1502
1503   SalomeApp_StudyPropertiesDlg aDlg( desktop() );
1504   int res = aDlg.exec();
1505   if( res==QDialog::Accepted && aDlg.isChanged() )
1506     SB->CommitCommand();
1507   else
1508     SB->AbortCommand();
1509
1510   //study->updateCaptions();
1511 }
1512
1513 QString SalomeApp_Application::getFileName( bool open, const QString& initial, const QString& filters, 
1514                                             const QString& caption, QWidget* parent )
1515 {
1516   if ( !parent )
1517     parent = desktop();
1518   QStringList fls = QStringList::split( ";;", filters, false );
1519   return SUIT_FileDlg::getFileName( parent, initial, fls, caption, open, true );
1520 }
1521
1522 QString SalomeApp_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
1523 {
1524   if ( !parent )
1525     parent = desktop();
1526   return SUIT_FileDlg::getExistingDirectory( parent, initial, caption, true );
1527 }
1528
1529 QStringList SalomeApp_Application::getOpenFileNames( const QString& initial, const QString& filters, 
1530                                                      const QString& caption, QWidget* parent )
1531 {
1532   if ( !parent )
1533     parent = desktop();
1534   QStringList fls = QStringList::split( ";;", filters, false );
1535   return SUIT_FileDlg::getOpenFileNames( parent, initial, fls, caption, true );
1536 }
1537
1538 void SalomeApp_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
1539 {
1540   CAM_Application::contextMenuPopup( type, thePopup, title );
1541   thePopup->insertSeparator();
1542   thePopup->insertItem( tr( "MEN_REFRESH" ), this, SLOT( onRefresh() ) );
1543   
1544   // "Activate module" item should appear only if it's necessary
1545   OB_Browser* ob = objectBrowser();
1546   if ( !ob || type != ob->popupClientType() )
1547     return;
1548   SALOME_ListIO aList;
1549   SalomeApp_SelectionMgr* mgr = selectionMgr();
1550   mgr->selectedObjects(aList);
1551   if (aList.Extent() != 1)
1552     return;
1553   Handle(SALOME_InteractiveObject) aIObj = aList.First();
1554   QString aModuleName(aIObj->getComponentDataType());
1555   QString aModuleTitle = moduleTitle(aModuleName);
1556   CAM_Module* currentModule = activeModule();
1557   if (currentModule && currentModule->moduleName() == aModuleTitle)
1558     return;
1559   thePopup->insertItem( tr( "MEN_OPENWITH" ), this, SLOT( onOpenWith() ) );
1560 }
1561
1562 void SalomeApp_Application::updateObjectBrowser( const bool updateModels )
1563 {
1564   if ( updateModels ) 
1565   {
1566     for ( ModuleListIterator it = modules(); it.current(); ++it )
1567     {    
1568       CAM_DataModel* camDM = it.current()->dataModel();
1569       if ( camDM && camDM->inherits( "SalomeApp_DataModel" ) )
1570         ((SalomeApp_DataModel*)camDM)->update();
1571     }
1572   }
1573   if ( objectBrowser() )
1574     objectBrowser()->updateTree();
1575 }