Salome HOME
df83116693188f44337420d986b772910369163d
[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   
539   SUIT_Session* aSession = SUIT_Session::session();
540   QPtrList<SUIT_Application> aAppList = aSession->applications();
541   SUIT_Application* aApp = 0;
542   
543   for (unsigned int ind = 0; ind < List.size(); ind++) {
544      studyname = List[ind].c_str();
545      //Add to list only unloaded studies
546      bool isAlreadyOpen = false;
547      for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
548        {
549          aApp = it.current();
550          if(!aApp || !aApp->activeStudy()) continue;
551          if ( aApp->activeStudy()->studyName() == studyname ) isAlreadyOpen = true;
552        }
553
554      if ( !isAlreadyOpen ) aDlg.ListComponent->insertItem( studyname );
555   }
556   
557   int retVal = aDlg.exec();
558   studyname = aDlg.ListComponent->currentText();
559
560   if (retVal == QDialog::Rejected)
561     return;
562   
563   if ( studyname.isNull() || studyname.isEmpty() )
564     return;
565   
566   name = studyname;
567   name.replace( QRegExp(":"), "/" );        
568
569   if(onLoadDoc(name)) {
570      updateWindows();
571      updateViewManagers(); 
572      updateObjectBrowser(true);
573   }
574 }
575
576
577
578 bool SalomeApp_Application::onLoadDoc( const QString& aName )
579 {
580   bool res = CAM_Application::onLoadDoc( aName );
581
582   /*jfa tmp:QAction* a = action( MRUId );
583   if ( a && a->inherits( "QtxMRUAction" ) )
584   {
585     QtxMRUAction* mru = (QtxMRUAction*)a;
586     if ( res )
587       mru->insert( aName );
588     else
589       mru->remove( aName );
590   }*/
591   return res;
592 }
593
594 void SalomeApp_Application::onSelection()
595 {
596   onSelectionChanged();
597
598   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
599     ((SalomeApp_Module*)activeModule())->selectionChanged();
600 }
601
602 void SalomeApp_Application::onCopy() 
603 {
604   SALOME_ListIO list;
605   SalomeApp_SelectionMgr* mgr = selectionMgr();
606   mgr->selectedObjects(list);
607   
608   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
609   if(study == NULL) return;
610   
611   _PTR(Study) stdDS = study->studyDS();
612   if(!stdDS) return;
613
614   SALOME_ListIteratorOfListIO it( list ); 
615   if(it.More())
616     {
617       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
618       try {
619         studyMgr()->Copy(so);
620         onSelectionChanged();
621       }
622       catch(...) {
623       }
624     }
625 }
626
627 void SalomeApp_Application::onPaste() 
628 {
629   SALOME_ListIO list;
630   SalomeApp_SelectionMgr* mgr = selectionMgr();
631   mgr->selectedObjects(list);
632   
633   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
634   if(study == NULL) return;
635   
636   _PTR(Study) stdDS = study->studyDS();
637   if(!stdDS) return;
638   
639   SALOME_ListIteratorOfListIO it( list ); 
640   if(it.More())
641     {
642       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
643       try {
644         studyMgr()->Paste(so);
645         updateObjectBrowser( true );
646         updateActions(); //SRN: BugID IPAL9377, case 3
647       }
648       catch(...) {
649       }
650     }
651 }
652
653 void SalomeApp_Application::onSelectionChanged()
654 {
655    SALOME_ListIO list;
656    SalomeApp_SelectionMgr* mgr = selectionMgr();
657    mgr->selectedObjects(list);
658    
659    SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
660    if(study == NULL) return;
661    
662    _PTR(Study) stdDS = study->studyDS();
663    if(!stdDS) return;
664    
665    QAction* qaction;  
666
667    SALOME_ListIteratorOfListIO it( list ); 
668    if(it.More() && list.Extent() == 1)
669    {
670       _PTR(SObject) so = stdDS->FindObjectID(it.Value()->getEntry());
671
672       qaction = action(EditCopyId); 
673       if(studyMgr()->CanCopy(so) ) qaction->setEnabled(true);  
674       else qaction->setEnabled(false);         
675      
676       qaction = action(EditPasteId);
677       if( studyMgr()->CanPaste(so) ) qaction->setEnabled(true);
678       else qaction->setEnabled(false);
679    } 
680    else {
681      qaction = action(EditCopyId); 
682      qaction->setEnabled(false); 
683      qaction = action(EditPasteId);
684      qaction->setEnabled(false);
685    }
686 }               
687  
688 void SalomeApp_Application::onRefresh()
689 {
690   updateObjectBrowser( true );
691 }
692
693  
694 void SalomeApp_Application::onOpenWith()
695 {
696   QApplication::setOverrideCursor( Qt::waitCursor );
697   SALOME_ListIO aList;
698   SalomeApp_SelectionMgr* mgr = selectionMgr();
699   mgr->selectedObjects(aList);
700   if (aList.Extent() != 1)
701     {
702       QApplication::restoreOverrideCursor();
703       return;
704     }
705   Handle(SALOME_InteractiveObject) aIObj = aList.First();
706   QString aModuleName(aIObj->getComponentDataType());
707   QString aModuleTitle = moduleTitle(aModuleName);
708   activateModule(aModuleTitle);
709   QApplication::restoreOverrideCursor();
710 }
711
712 bool SalomeApp_Application::useStudy(const QString& theName)
713 {
714   createEmptyStudy();
715   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(activeStudy());
716   bool res = false;
717   if (aStudy)
718     res = aStudy->loadDocument( theName );
719   updateDesktopTitle();
720   updateCommandsStatus();
721   return res;
722 }
723
724 void SalomeApp_Application::setActiveStudy( SUIT_Study* study )
725 {
726   CAM_Application::setActiveStudy( study );
727
728   activateWindows();
729 }
730
731 //=======================================================================
732 // name    : createNewStudy
733 // Purpose : Create new study
734 //=======================================================================
735 SUIT_Study* SalomeApp_Application::createNewStudy()
736 {
737   SalomeApp_Study* aStudy = new SalomeApp_Study( this );
738
739   // Set up processing of major study-related events
740   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
741   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
742   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
743   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
744
745   return aStudy;
746 }
747
748 //=======================================================================
749 // name    : createNewStudy
750 // Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu
751 //=======================================================================
752 void SalomeApp_Application::updateCommandsStatus()
753 {
754   CAM_Application::updateCommandsStatus();
755
756   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
757   {
758     QAction* a = action( id );
759     if ( a )
760       a->setEnabled( activeStudy() );
761   }
762
763   // Dump study menu
764   QAction* a = action( DumpStudyId );
765   if ( a )
766     a->setEnabled( activeStudy() );
767
768   // Load script menu
769   a = action( LoadScriptId );
770   if ( a )
771     a->setEnabled( activeStudy() );
772   
773   a = action( PropertiesId );
774   if( a )
775     a->setEnabled( activeStudy() );
776     
777   a = action(EditCopyId);
778   a->setEnabled(false);       
779   a = action(EditPasteId);
780   a->setEnabled(false);      
781 }
782
783 //=======================================================================
784 // name    : onHelpAbout
785 // Purpose : SLOT. Display "About" message box
786 //=======================================================================
787 void SalomeApp_Application::onHelpAbout()
788 {
789   SalomeApp_AboutDlg* dlg = new SalomeApp_AboutDlg( applicationName(), applicationVersion(), desktop() );
790   dlg->exec();
791   delete dlg;
792 }
793
794 QWidget* SalomeApp_Application::window( const int flag, const int studyId ) const
795 {
796   QWidget* wid = 0;
797
798   int sId = studyId;
799   if ( sId < 0 )
800   {
801     if ( !activeStudy() )
802       return 0;
803     else
804       sId = activeStudy()->id();
805   }
806
807   if ( myWindows.contains( flag ) )
808     wid = myWindows[flag]->widget( sId );
809
810   return wid;
811 }
812
813 void SalomeApp_Application::addWindow( QWidget* wid, const int flag, const int studyId )
814 {
815   if ( !wid )
816     return;
817
818   int sId = studyId;
819   if ( sId < 0 )
820   {
821     if ( !activeStudy() )
822       return;
823     else
824       sId = activeStudy()->id();
825   }
826
827   if ( !myWindows.contains( flag ) )
828   {
829     QMap<int, int> winMap;
830     currentWindows( winMap );
831
832     myWindows.insert( flag, new SalomeApp_WidgetContainer( flag, desktop() ) );
833     if ( winMap.contains( flag ) )
834       desktop()->moveDockWindow( myWindows[flag], (Dock)winMap[flag] );
835
836     myWindows[flag]->setResizeEnabled( true );
837     myWindows[flag]->setCloseMode( QDockWindow::Always );
838     myWindows[flag]->setName( QString( "dock_window_%1" ).arg( flag ) );
839   }
840
841   myWindows[flag]->insert( sId, wid );
842
843   setWindowShown( flag, !myWindows[flag]->isEmpty() );
844 }
845
846 void SalomeApp_Application::removeWindow( const int flag, const int studyId )
847 {
848   if ( !myWindows.contains( flag ) )
849     return;
850
851   int sId = studyId;
852   if ( sId < 0 )
853   {
854     if ( !activeStudy() )
855       return;
856     else
857       sId = activeStudy()->id();
858   }
859
860   QWidget* wid = myWindows[flag]->widget( sId );
861   myWindows[flag]->remove( sId );
862   delete wid;
863
864   setWindowShown( flag, !myWindows[flag]->isEmpty() );
865 }
866
867 QWidget* SalomeApp_Application::getWindow( const int flag, const int studyId )
868 {
869   QWidget* wid = window( flag, studyId );
870   if ( !wid )
871     addWindow( wid = createWindow( flag ), flag, studyId );
872
873   return wid;
874 }
875
876 bool SalomeApp_Application::isWindowVisible( const int type ) const
877 {
878   bool res = false;
879   if ( myWindows.contains( type ) )
880   {
881     SUIT_Desktop* desk = ((SalomeApp_Application*)this)->desktop();
882     res = desk && desk->appropriate( myWindows[type] );
883   }
884   return res;
885 }
886
887 void SalomeApp_Application::setWindowShown( const int type, const bool on )
888 {
889   if ( !desktop() || !myWindows.contains( type ) )
890     return;
891
892   QDockWindow* dw = myWindows[type];
893   desktop()->setAppropriate( dw, on );
894   on ? dw->show() : dw->hide();
895 }
896
897 OB_Browser* SalomeApp_Application::objectBrowser()
898 {
899   OB_Browser* ob = 0;
900   QWidget* wid = getWindow( WT_ObjectBrowser );
901   if ( wid->inherits( "OB_Browser" ) )
902     ob = (OB_Browser*)wid;
903   return ob;
904 }
905
906 LogWindow* SalomeApp_Application::logWindow()
907 {
908   LogWindow* lw = 0;
909   QWidget* wid = getWindow( WT_LogWindow );
910   if ( wid->inherits( "LogWindow" ) )
911     lw = (LogWindow*)wid;
912   return lw;
913 }
914
915 PythonConsole* SalomeApp_Application::pythonConsole()
916 {
917   PythonConsole* console = 0;
918   QWidget* wid = getWindow( WT_PyConsole );
919   if ( wid->inherits( "PythonConsole" ) )
920     console = (PythonConsole*)wid;
921   return console;
922 }
923
924 SalomeApp_Preferences* SalomeApp_Application::preferences() const
925 {
926   return preferences( false );
927 }
928
929 SUIT_ViewManager* SalomeApp_Application::getViewManager( const QString& vmType, const bool create )
930 {
931   SUIT_ViewManager* aVM = viewManager( vmType );
932   SUIT_ViewManager* anActiveVM = CAM_Application::activeViewManager();
933
934   if ( anActiveVM && anActiveVM->getType() == vmType )
935     aVM = anActiveVM;
936
937   if ( aVM && create )
938   {
939     if ( !aVM->getActiveView() )
940       aVM->createView();
941     else
942       aVM->getActiveView()->setFocus();
943   }
944   else if ( create )
945     aVM = createViewManager( vmType );
946
947   return aVM;
948 }
949
950 SUIT_ViewManager* SalomeApp_Application::createViewManager( const QString& vmType )
951 {
952   SUIT_ResourceMgr* resMgr = resourceMgr();
953
954   SUIT_ViewManager* viewMgr = 0;
955   if ( vmType == GLViewer_Viewer::Type() )
956   {
957     viewMgr = new GLViewer_ViewManager( activeStudy(), desktop() );
958     new SalomeApp_GLSelector( (GLViewer_Viewer2d*)viewMgr->getViewModel(), mySelMgr );
959   }
960   else if ( vmType == Plot2d_Viewer::Type() )
961   {
962     viewMgr = new Plot2d_ViewManager( activeStudy(), desktop() );
963     viewMgr->setViewModel( new SPlot2d_Viewer() );// custom view model, which extends SALOME_View interface
964   }
965   else if ( vmType == OCCViewer_Viewer::Type() )
966   {
967     viewMgr = new OCCViewer_ViewManager( activeStudy(), desktop() );
968     SOCC_Viewer* vm = new SOCC_Viewer();
969     vm->setBackgroundColor( resMgr->colorValue( "OCCViewer", "background", vm->backgroundColor() ) );
970     vm->setTrihedronSize( resMgr->integerValue( "OCCViewer", "trihedron_size", vm->trihedronSize() ) );
971     int u( 1 ), v( 1 );
972     vm->isos( u, v );
973     u = resMgr->integerValue( "OCCViewer", "iso_number_u", u );
974     v = resMgr->integerValue( "OCCViewer", "iso_number_v", v );
975     vm->setIsos( u, v );
976     viewMgr->setViewModel( vm );// custom view model, which extends SALOME_View interface
977     new SalomeApp_OCCSelector( (OCCViewer_Viewer*)viewMgr->getViewModel(), mySelMgr );
978   }
979   else if ( vmType == SVTK_Viewer::Type() )
980   {
981     viewMgr = new SVTK_ViewManager( activeStudy(), desktop() );
982     SVTK_Viewer* vm = (SVTK_Viewer*)viewMgr->getViewModel();
983     vm->setBackgroundColor( resMgr->colorValue( "VTKViewer", "background", vm->backgroundColor() ) );
984     vm->setTrihedronSize( resMgr->integerValue( "VTKViewer", "trihedron_size", vm->trihedronSize() ) );
985     new SalomeApp_VTKSelector((SVTK_Viewer*)viewMgr->getViewModel(),mySelMgr);
986   }
987
988   if ( !viewMgr )
989     return 0;
990
991   addViewManager( viewMgr );
992   SUIT_ViewWindow* viewWin = viewMgr->createViewWindow();
993
994   if ( viewWin && desktop() )
995     viewWin->resize( (int)( desktop()->width() * 0.6 ), (int)( desktop()->height() * 0.6 ) );
996
997   connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
998            this, SLOT( onCloseView( SUIT_ViewManager* ) ) );
999
1000   return viewMgr;
1001 }
1002
1003 void SalomeApp_Application::onCloseView( SUIT_ViewManager* theVM )
1004 {
1005   removeViewManager( theVM );
1006 }
1007
1008 void SalomeApp_Application::onStudyCreated( SUIT_Study* theStudy )
1009 {
1010   SUIT_DataObject* aRoot = 0;
1011   if ( theStudy && theStudy->root() )
1012   {
1013     aRoot = theStudy->root();
1014     //aRoot->setName( tr( "DATA_MODELS" ) );
1015   }
1016   if ( objectBrowser() != 0 )
1017     objectBrowser()->setRootObject( aRoot );
1018
1019   activateModule( defaultModule() );
1020
1021   activateWindows();
1022 }
1023
1024 void SalomeApp_Application::onStudyOpened( SUIT_Study* theStudy )
1025 {
1026   SUIT_DataObject* aRoot = 0;
1027   if ( theStudy && theStudy->root() )
1028   {
1029     aRoot = theStudy->root();
1030     //aRoot->dump();
1031   }
1032   if ( objectBrowser() != 0 ) {
1033     objectBrowser()->setRootObject( aRoot );
1034   }
1035
1036   activateModule( defaultModule() );
1037
1038   activateWindows();
1039
1040   emit studyOpened();
1041 }
1042
1043 void SalomeApp_Application::onStudySaved( SUIT_Study* )
1044 {
1045   emit studySaved();
1046 }
1047
1048 void SalomeApp_Application::onStudyClosed( SUIT_Study* )
1049 {
1050   emit studyClosed();
1051
1052   activateModule( "" );
1053
1054   saveWindowsGeometry();
1055 }
1056
1057 void SalomeApp_Application::onDumpStudy( )
1058 {
1059   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
1060   if ( !appStudy ) return;
1061   _PTR(Study) aStudy = appStudy->studyDS();
1062
1063   QStringList aFilters;
1064   aFilters.append( tr( "PYTHON_FILES_FILTER" ) );
1065
1066   SalomeApp_CheckFileDlg* fd = new SalomeApp_CheckFileDlg( desktop(), false, tr("PUBLISH_IN_STUDY"), true, true);
1067   fd->setCaption( tr( "TOT_DESK_FILE_DUMP_STUDY" ) );
1068   fd->setFilters( aFilters );  
1069   fd->SetChecked(true);
1070   fd->exec();
1071   QString aFileName = fd->selectedFile();
1072   bool toPublish = fd->IsChecked();
1073   delete fd;
1074
1075   if(!aFileName.isEmpty()) {
1076     QFileInfo aFileInfo(aFileName);
1077     aStudy->DumpStudy( aFileInfo.dirPath( true ).latin1(), aFileInfo.baseName().latin1(), toPublish );
1078   }
1079 }
1080
1081 void SalomeApp_Application::onLoadScript( )
1082 {
1083   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
1084   if ( !appStudy ) return;
1085   _PTR(Study) aStudy = appStudy->studyDS();
1086
1087   if ( aStudy->GetProperties()->IsLocked() ) {
1088     SUIT_MessageBox::warn1 ( desktop(),
1089                              QObject::tr("WRN_WARNING"),
1090                              QObject::tr("WRN_STUDY_LOCKED"),
1091                              QObject::tr("BUT_OK") );
1092     return;
1093   }
1094
1095   QStringList filtersList;
1096   filtersList.append(tr("PYTHON_FILES_FILTER"));
1097   filtersList.append(tr("ALL_FILES_FILTER"));
1098
1099   QString aFile = SUIT_FileDlg::getFileName( desktop(), "", filtersList, tr( "TOT_DESK_FILE_LOAD_SCRIPT" ), true, true );
1100
1101   if ( !aFile.isEmpty() )
1102   {
1103     QString command = QString("execfile(\"%1\")").arg(aFile);
1104
1105     PythonConsole* pyConsole = pythonConsole();
1106
1107     if ( pyConsole )
1108       pyConsole->exec( command );
1109   }
1110 }
1111
1112 void SalomeApp_Application::onPreferences()
1113 {
1114   QApplication::setOverrideCursor( Qt::waitCursor );
1115
1116   SalomeApp_PreferencesDlg* prefDlg = new SalomeApp_PreferencesDlg( preferences( true ), desktop());
1117
1118   QApplication::restoreOverrideCursor();
1119
1120   if ( !prefDlg )
1121     return;
1122
1123   prefDlg->exec();
1124
1125   delete prefDlg;
1126 }
1127
1128 void SalomeApp_Application::onMRUActivated( QString aName )
1129 {
1130   onOpenDoc( aName );
1131 }
1132
1133 void SalomeApp_Application::onPreferenceChanged( QString& modName, QString& section, QString& param )
1134 {
1135   SalomeApp_Module* sMod = 0;
1136   CAM_Module* mod = module( modName );
1137   if ( mod && mod->inherits( "SalomeApp_Module" ) )
1138     sMod = (SalomeApp_Module*)mod;
1139
1140   if ( sMod )
1141     sMod->preferencesChanged( section, param );
1142   else
1143     preferencesChanged( section, param );
1144 }
1145
1146 QString SalomeApp_Application::getFileFilter() const
1147 {
1148   return "(*.hdf)";
1149 }
1150
1151 void SalomeApp_Application::beforeCloseDoc( SUIT_Study* s )
1152 {
1153   CAM_Application::beforeCloseDoc( s );
1154
1155   for ( WindowMap::ConstIterator itr = myWindows.begin(); s && itr != myWindows.end(); ++itr )
1156     removeWindow( itr.key(), s->id() );
1157 }
1158
1159 void SalomeApp_Application::updateActions()
1160 {
1161   updateCommandsStatus();
1162 }
1163
1164 QWidget* SalomeApp_Application::createWindow( const int flag )
1165 {
1166   QWidget* wid = 0;
1167
1168   SUIT_ResourceMgr* resMgr = resourceMgr();
1169
1170   if ( flag == WT_ObjectBrowser )
1171   {
1172     OB_Browser* ob = new OB_Browser( desktop() );
1173     ob->setAutoUpdate( true );
1174     ob->setAutoOpenLevel( 1 );
1175     ob->setCaption( tr( "OBJECT_BROWSER" ) );
1176     ob->resize( OBJECT_BROWSER_WIDTH, ob->height() );
1177     ob->setFilter( new SalomeApp_OBFilter( selectionMgr() ) );
1178
1179     ob->setNameTitle( tr( "OBJ_BROWSER_NAME" ) );
1180
1181     for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
1182     {
1183       ob->addColumn( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), i );
1184       ob->setColumnShown( i, resMgr->booleanValue( "ObjectBrowser",
1185                                                    QString().sprintf( "visibility_column_%d", i ), true ) );
1186     }
1187
1188     // Create OBSelector
1189     new SalomeApp_OBSelector( ob, mySelMgr );
1190
1191     wid = ob;
1192
1193     ob->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1194   }
1195   else if ( flag == WT_PyConsole )
1196   {
1197     PythonConsole* pyCons = new PythonConsole( desktop(), new SalomeApp_PyInterp() );
1198     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
1199     wid = pyCons;
1200
1201     //    pyCons->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1202   }
1203   else if ( flag == WT_LogWindow )
1204   {
1205     LogWindow* logWin = new LogWindow( desktop() );
1206     logWin->setCaption( tr( "LOG_WINDOW" ) );
1207     wid = logWin;
1208
1209     logWin->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1210   }
1211
1212   return wid;
1213 }
1214
1215 void SalomeApp_Application::defaultWindows( QMap<int, int>& aMap ) const
1216 {
1217   aMap.insert( WT_ObjectBrowser, Qt::DockLeft );
1218   aMap.insert( WT_PyConsole, Qt::DockBottom );
1219   //  aMap.insert( WT_LogWindow, Qt::DockBottom );
1220 }
1221
1222 void SalomeApp_Application::defaultViewManagers( QStringList& ) const
1223 {
1224 }
1225
1226 SalomeApp_Preferences* SalomeApp_Application::preferences( const bool crt ) const
1227 {
1228   if ( myPrefs )
1229     return myPrefs;
1230
1231   SalomeApp_Application* that = (SalomeApp_Application*)this;
1232
1233   if ( !_prefs_ && crt )
1234   {
1235     _prefs_ = new SalomeApp_Preferences( resourceMgr() );
1236     that->createPreferences( _prefs_ );
1237   }
1238
1239   that->myPrefs = _prefs_;
1240
1241   QPtrList<SUIT_Application> appList = SUIT_Session::session()->applications();
1242   for ( QPtrListIterator<SUIT_Application> appIt ( appList ); appIt.current(); ++appIt )
1243   {
1244     if ( !appIt.current()->inherits( "SalomeApp_Application" ) )
1245       continue;
1246
1247     SalomeApp_Application* app = (SalomeApp_Application*)appIt.current();
1248
1249     QStringList modNameList;
1250     app->modules( modNameList, false );
1251     for ( QStringList::const_iterator it = modNameList.begin(); it != modNameList.end(); ++it )
1252     {
1253       int id = _prefs_->addPreference( *it );
1254       _prefs_->setItemProperty( id, "info", tr( "PREFERENCES_NOT_LOADED" ).arg( *it ) );
1255     }
1256
1257     ModuleList modList;
1258     app->modules( modList );
1259     for ( ModuleListIterator itr( modList ); itr.current(); ++itr )
1260     {
1261       SalomeApp_Module* mod = 0;
1262       if ( itr.current()->inherits( "SalomeApp_Module" ) )
1263         mod = (SalomeApp_Module*)itr.current();
1264
1265       if ( mod && !_prefs_->hasModule( mod->moduleName() ) )
1266       {
1267         int modCat = _prefs_->addPreference( mod->moduleName() );
1268         _prefs_->setItemProperty( modCat, "info", QString::null );
1269         mod->createPreferences();
1270       }
1271     }
1272   }
1273
1274   connect( myPrefs, SIGNAL( preferenceChanged( QString&, QString&, QString& ) ),
1275            this, SLOT( onPreferenceChanged( QString&, QString&, QString& ) ) );
1276
1277   return myPrefs;
1278 }
1279
1280 void SalomeApp_Application::moduleAdded( CAM_Module* mod )
1281 {
1282   CAM_Application::moduleAdded( mod );
1283
1284   SalomeApp_Module* salomeMod = 0;
1285   if ( mod && mod->inherits( "SalomeApp_Module" ) )
1286     salomeMod = (SalomeApp_Module*)mod;
1287
1288   if ( myPrefs && salomeMod && !myPrefs->hasModule( salomeMod->moduleName() ) )
1289   {
1290     int modCat = myPrefs->addPreference( mod->moduleName() );
1291     myPrefs->setItemProperty( modCat, "info", QString::null );
1292     salomeMod->createPreferences();
1293   }
1294 }
1295
1296 void SalomeApp_Application::createPreferences( SalomeApp_Preferences* pref )
1297 {
1298   if ( !pref )
1299     return;
1300
1301   int salomeCat = pref->addPreference( tr( "PREF_CATEGORY_SALOME" ) );
1302
1303   int genTab = pref->addPreference( tr( "PREF_TAB_GENERAL" ), salomeCat );
1304
1305   int obGroup = pref->addPreference( tr( "PREF_GROUP_OBJBROWSER" ), genTab );
1306   for ( int i = SalomeApp_DataObject::CT_Value; i <= SalomeApp_DataObject::CT_RefEntry; i++ )
1307   {
1308     pref->addPreference( tr( QString().sprintf( "OBJ_BROWSER_COLUMN_%d", i ) ), obGroup,
1309                          SalomeApp_Preferences::Bool, "ObjectBrowser", QString().sprintf( "visibility_column_%d", i ) );
1310   }
1311   pref->setItemProperty( obGroup, "columns", 1 );
1312
1313   int viewTab = pref->addPreference( tr( "PREF_TAB_VIEWERS" ), salomeCat );
1314
1315   int occGroup = pref->addPreference( tr( "PREF_GROUP_OCCVIEWER" ), viewTab );
1316
1317   int vtkGroup = pref->addPreference( tr( "PREF_GROUP_VTKVIEWER" ), viewTab );
1318   pref->setItemProperty( occGroup, "columns", 1 );
1319   pref->setItemProperty( vtkGroup, "columns", 1 );
1320
1321   int occTS = pref->addPreference( tr( "PREF_TRIHEDRON_SIZE" ), occGroup,
1322                                    SalomeApp_Preferences::IntSpin, "OCCViewer", "trihedron_size" );
1323   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), occGroup,
1324                        SalomeApp_Preferences::Color, "OCCViewer", "background" );
1325
1326   pref->setItemProperty( occTS, "min", 1 );
1327   pref->setItemProperty( occTS, "max", 150 );
1328
1329   int isoU = pref->addPreference( tr( "PREF_ISOS_U" ), occGroup,
1330                                   SalomeApp_Preferences::IntSpin, "OCCViewer", "iso_number_u" );
1331   int isoV = pref->addPreference( tr( "PREF_ISOS_V" ), occGroup,
1332                                   SalomeApp_Preferences::IntSpin, "OCCViewer", "iso_number_v" );
1333
1334   pref->setItemProperty( isoU, "min", 0 );
1335   pref->setItemProperty( isoU, "max", 100000 );
1336
1337   pref->setItemProperty( isoV, "min", 0 );
1338   pref->setItemProperty( isoV, "max", 100000 );
1339
1340   int vtkTS = pref->addPreference( tr( "PREF_TRIHEDRON_SIZE" ), vtkGroup,
1341                                    SalomeApp_Preferences::IntSpin, "VTKViewer", "trihedron_size" );
1342   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), vtkGroup,
1343                        SalomeApp_Preferences::Color, "VTKViewer", "background" );
1344
1345   pref->setItemProperty( vtkTS, "min", 1 );
1346   pref->setItemProperty( vtkTS, "max", 150 );
1347 }
1348
1349 void SalomeApp_Application::preferencesChanged( const QString& sec, const QString& param )
1350 {
1351   SUIT_ResourceMgr* resMgr = resourceMgr();
1352   if ( !resMgr )
1353     return;
1354
1355   if ( sec == QString( "OCCViewer" ) && param == QString( "trihedron_size" ) )
1356   {
1357     int sz = resMgr->integerValue( sec, param, -1 );
1358     QPtrList<SUIT_ViewManager> lst;
1359     viewManagers( OCCViewer_Viewer::Type(), lst );
1360     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current() && sz >= 0; ++it )
1361     {
1362       SUIT_ViewModel* vm = it.current()->getViewModel();
1363       if ( !vm || !vm->inherits( "OCCViewer_Viewer" ) )
1364         continue;
1365
1366       OCCViewer_Viewer* occVM = (OCCViewer_Viewer*)vm;
1367       occVM->setTrihedronSize( sz );
1368       occVM->getAISContext()->UpdateCurrentViewer();
1369     }
1370   }
1371
1372   if ( sec == QString( "VTKViewer" ) && param == QString( "trihedron_size" ) )
1373   {
1374     int sz = resMgr->integerValue( sec, param, -1 );
1375     QPtrList<SUIT_ViewManager> lst;
1376     viewManagers( SVTK_Viewer::Type(), lst );
1377     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current() && sz >= 0; ++it )
1378     {
1379       SUIT_ViewModel* vm = it.current()->getViewModel();
1380       if ( !vm || !vm->inherits( "SVTK_Viewer" ) )
1381         continue;
1382
1383       SVTK_Viewer* vtkVM = (SVTK_Viewer*)vm;
1384       vtkVM->setTrihedronSize( sz );
1385       vtkVM->Repaint();
1386     }
1387   }
1388   
1389   if ( sec == QString( "OCCViewer" ) && ( param == QString( "iso_number_u" ) || param == QString( "iso_number_v" ) ) )
1390   {
1391     QPtrList<SUIT_ViewManager> lst;
1392     viewManagers( OCCViewer_Viewer::Type(), lst );
1393     int u = resMgr->integerValue( sec, "iso_number_u" );
1394     int v = resMgr->integerValue( sec, "iso_number_v" );
1395     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
1396       ((OCCViewer_Viewer*)it.current())->setIsos( u, v );
1397   }
1398   
1399 }
1400
1401 void SalomeApp_Application::updateDesktopTitle() {
1402   QString aTitle = applicationName();
1403   QString aVer = applicationVersion();
1404   if ( !aVer.isEmpty() )
1405     aTitle += QString( " " ) + aVer;
1406
1407   if ( activeStudy() )
1408   {
1409     QString sName = SUIT_Tools::file( activeStudy()->studyName().stripWhiteSpace(), false );
1410     if ( !sName.isEmpty() ) {
1411       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
1412       _PTR(Study) stdDS = study->studyDS();
1413       if(stdDS) {
1414         if ( stdDS->GetProperties()->IsLocked() ) {
1415           aTitle += QString( " - [%1 (%2)]").arg( sName ).arg( tr( "STUDY_LOCKED" ) );
1416         } else {
1417           aTitle += QString( " - [%1]" ).arg( sName );
1418         }
1419       }
1420     }
1421   }
1422
1423   desktop()->setCaption( aTitle );
1424 }
1425
1426 void SalomeApp_Application::afterCloseDoc()
1427 {
1428   updateWindows();
1429
1430   CAM_Application::afterCloseDoc();
1431 }
1432
1433 CORBA::ORB_var SalomeApp_Application::orb()
1434 {
1435   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
1436   static CORBA::ORB_var _orb = init( qApp->argc(), qApp->argv() );
1437   return _orb;
1438 }
1439
1440 SALOMEDSClient_StudyManager* SalomeApp_Application::studyMgr()
1441 {
1442   static SALOMEDSClient_StudyManager* _sm = new SALOMEDS_StudyManager();
1443   return _sm;
1444 }
1445
1446 SALOME_NamingService* SalomeApp_Application::namingService()
1447 {
1448   static SALOME_NamingService* _ns = new SALOME_NamingService( orb() );
1449   return _ns;
1450 }
1451
1452 SALOME_LifeCycleCORBA* SalomeApp_Application::lcc()
1453 {
1454   static SALOME_LifeCycleCORBA* _lcc = new SALOME_LifeCycleCORBA( namingService() );
1455   return _lcc;
1456 }
1457
1458 QString SalomeApp_Application::defaultEngineIOR()
1459 {
1460   // Look for a default module engine (needed for CORBAless modules to use SALOMEDS persistence)
1461   QString anIOR( "" );
1462   CORBA::Object_ptr anEngine = namingService()->Resolve( "/SalomeAppEngine" );
1463   if ( !CORBA::is_nil( anEngine ) )
1464     anIOR = orb()->object_to_string( anEngine );
1465   return anIOR;
1466 }
1467
1468 void SalomeApp_Application::moduleIconNames( QMap<QString, QString>& iconMap ) const
1469 {
1470   iconMap.clear();
1471
1472   SUIT_ResourceMgr* resMgr = resourceMgr();
1473   if ( !resMgr )
1474     return;
1475
1476   QStringList modList;
1477   modules( modList, false );
1478
1479   for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
1480   {
1481     QString modName = *it;
1482     QString modIntr = moduleName( modName );
1483     QString modIcon = resMgr->stringValue( modIntr, "icon", QString::null );
1484
1485     if ( modIcon.isEmpty() )
1486       continue;
1487
1488     if ( SUIT_Tools::extension( modIcon ).isEmpty() )
1489       modIcon += QString( ".png" );
1490
1491     iconMap.insert( modName, modIcon );
1492   }
1493 }
1494
1495 void SalomeApp_Application::updateModuleActions()
1496 {
1497   QString modName;
1498   if ( activeModule() )
1499     modName = activeModule()->moduleName();
1500
1501   if ( myActions.contains( modName ) )
1502     myActions[modName]->setOn( true );
1503 }
1504
1505 void SalomeApp_Application::currentWindows( QMap<int, int>& winMap ) const
1506 {
1507   winMap.clear();
1508   if ( !activeStudy() )
1509     return;
1510
1511   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1512     ((SalomeApp_Module*)activeModule())->windows( winMap );
1513   else
1514     defaultWindows( winMap );
1515 }
1516
1517 void SalomeApp_Application::currentViewManagers( QStringList& lst ) const
1518 {
1519   lst.clear();
1520   if ( !activeStudy() )
1521     return;
1522
1523   if ( activeModule() && activeModule()->inherits( "SalomeApp_Module" ) )
1524     ((SalomeApp_Module*)activeModule())->viewManagers( lst );
1525   else
1526     defaultViewManagers( lst );
1527 }
1528
1529 void SalomeApp_Application::updateWindows()
1530 {
1531   QMap<int, int> winMap;
1532   currentWindows( winMap );
1533
1534   for ( QMap<int, int>::ConstIterator it = winMap.begin(); it != winMap.end(); ++it )
1535     getWindow( it.key() );
1536
1537   loadWindowsGeometry();
1538
1539   for ( WindowMap::ConstIterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1540     setWindowShown( itr.key(), !itr.data()->isEmpty() && winMap.contains( itr.key() ) );
1541 }
1542
1543 void SalomeApp_Application::updateViewManagers()
1544 {
1545   QStringList lst;
1546   currentViewManagers( lst );
1547
1548   for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
1549     getViewManager( *it, true );
1550 }
1551
1552 void SalomeApp_Application::loadWindowsGeometry()
1553 {
1554   QtxDockAction* dockMgr = 0;
1555
1556   QAction* a = action( ViewWindowsId );
1557   if ( a && a->inherits( "QtxDockAction" ) )
1558     dockMgr = (QtxDockAction*)a;
1559
1560   if ( !dockMgr )
1561     return;
1562
1563   QString modName;
1564   if ( activeModule() )
1565     modName = moduleLibrary( activeModule()->moduleName(), false );
1566
1567   QString section = QString( "windows_geometry" );
1568   if ( !modName.isEmpty() )
1569     section += QString( "." ) + modName;
1570
1571   dockMgr->loadGeometry( resourceMgr(), section, false );
1572   dockMgr->restoreGeometry();
1573 }
1574
1575 void SalomeApp_Application::saveWindowsGeometry()
1576 {
1577   QtxDockAction* dockMgr = 0;
1578
1579   QAction* a = action( ViewWindowsId );
1580   if ( a && a->inherits( "QtxDockAction" ) )
1581     dockMgr = (QtxDockAction*)a;
1582
1583   if ( !dockMgr )
1584     return;
1585
1586   QString modName;
1587   if ( activeModule() )
1588     modName = moduleLibrary( activeModule()->moduleName(), false );
1589
1590   QString section = QString( "windows_geometry" );
1591   if ( !modName.isEmpty() )
1592     section += QString( "." ) + modName;
1593
1594   dockMgr->storeGeometry();
1595   dockMgr->saveGeometry( resourceMgr(), section, false );
1596 }
1597
1598 void SalomeApp_Application::activateWindows()
1599 {
1600   if ( activeStudy() )
1601   {
1602     for ( WindowMap::Iterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1603       itr.data()->activate( activeStudy()->id() );
1604   }
1605 }
1606
1607 void SalomeApp_Application::onProperties()
1608 {
1609   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( activeStudy() );
1610   if( !study )
1611     return;
1612
1613   _PTR(StudyBuilder) SB = study->studyDS()->NewBuilder();
1614   SB->NewCommand();
1615
1616   SalomeApp_StudyPropertiesDlg aDlg( desktop() );
1617   int res = aDlg.exec();
1618   if( res==QDialog::Accepted && aDlg.isChanged() )
1619     SB->CommitCommand();
1620   else
1621     SB->AbortCommand();
1622
1623   //study->updateCaptions();
1624   updateDesktopTitle();
1625 }
1626
1627 QString SalomeApp_Application::getFileName( bool open, const QString& initial, const QString& filters, 
1628                                             const QString& caption, QWidget* parent )
1629 {
1630   if ( !parent )
1631     parent = desktop();
1632   QStringList fls = QStringList::split( ";;", filters, false );
1633   return SUIT_FileDlg::getFileName( parent, initial, fls, caption, open, true );
1634 }
1635
1636 QString SalomeApp_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
1637 {
1638   if ( !parent )
1639     parent = desktop();
1640   return SUIT_FileDlg::getExistingDirectory( parent, initial, caption, true );
1641 }
1642
1643 QStringList SalomeApp_Application::getOpenFileNames( const QString& initial, const QString& filters, 
1644                                                      const QString& caption, QWidget* parent )
1645 {
1646   if ( !parent )
1647     parent = desktop();
1648   QStringList fls = QStringList::split( ";;", filters, false );
1649   return SUIT_FileDlg::getOpenFileNames( parent, initial, fls, caption, true );
1650 }
1651
1652 void SalomeApp_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
1653 {
1654   CAM_Application::contextMenuPopup( type, thePopup, title );
1655   thePopup->insertSeparator();
1656   thePopup->insertItem( tr( "MEN_REFRESH" ), this, SLOT( onRefresh() ) );
1657   
1658   // "Activate module" item should appear only if it's necessary
1659   OB_Browser* ob = objectBrowser();
1660   if ( !ob || type != ob->popupClientType() )
1661     return;
1662   SALOME_ListIO aList;
1663   SalomeApp_SelectionMgr* mgr = selectionMgr();
1664   mgr->selectedObjects(aList);
1665   if (aList.Extent() != 1)
1666     return;
1667   Handle(SALOME_InteractiveObject) aIObj = aList.First();
1668   QString aModuleName(aIObj->getComponentDataType());
1669   QString aModuleTitle = moduleTitle(aModuleName);
1670   CAM_Module* currentModule = activeModule();
1671   if (currentModule && currentModule->moduleName() == aModuleTitle)
1672     return;
1673   thePopup->insertItem( tr( "MEN_OPENWITH" ), this, SLOT( onOpenWith() ) );
1674 }
1675
1676 void SalomeApp_Application::updateObjectBrowser( const bool updateModels )
1677 {
1678   // update existing data models (already loaded SComponents)
1679   if ( updateModels ) 
1680   {
1681     for ( ModuleListIterator it = modules(); it.current(); ++it )
1682     {    
1683       CAM_DataModel* camDM = it.current()->dataModel();
1684       if ( camDM && camDM->inherits( "SalomeApp_DataModel" ) )
1685         ((SalomeApp_DataModel*)camDM)->update();
1686     }
1687   }
1688   // update "non-existing" (not loaded yet) data models
1689   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(activeStudy());
1690   if ( study ) 
1691   {
1692     _PTR(Study) stdDS = study->studyDS();
1693     if( stdDS ) 
1694     {
1695       for ( _PTR(SComponentIterator) it ( stdDS->NewComponentIterator() ); it->More(); it->Next() ) 
1696       {
1697         _PTR(SComponent) aComponent ( it->Value() ); 
1698
1699         if ( aComponent->ComponentDataType() == "Interface Applicative" )
1700           continue; // skip the magic "Interface Applicative" component
1701             
1702         SalomeApp_DataModel::BuildTree( aComponent, study->root(), study, /*skipExisitng=*/true );
1703       }
1704     }
1705   }
1706
1707   if ( objectBrowser() )
1708     objectBrowser()->updateTree();
1709 }
1710
1711
1712 //************************************************************
1713 void SalomeApp_Application::onDesktopActivated()
1714 {
1715   CAM_Application::onDesktopActivated();
1716   SalomeApp_Module* aModule = dynamic_cast<SalomeApp_Module*>(activeModule());
1717   if(aModule)
1718     aModule->studyActivated();
1719 }
1720
1721 void SalomeApp_Application::createEmptyStudy()
1722 {
1723   CAM_Application::createEmptyStudy();
1724   if ( objectBrowser() )
1725     objectBrowser()->updateTree();
1726 }
1727
1728 bool SalomeApp_Application::activateModule( CAM_Module* mod )
1729 {
1730   bool res = CAM_Application::activateModule( mod );
1731   if ( objectBrowser() )
1732     objectBrowser()->updateTree();
1733   return res;
1734 }