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