Salome HOME
remove view manager from memory when it become unused
[modules/gui.git] / src / LightApp / LightApp_Application.cxx
1 // File:      LightApp_Application.cxx
2 // Created:   6/20/2005 18:39:45 PM
3 // Author:    Natalia Donis
4 // Copyright (C) CEA 2005
5
6 #include "PythonConsole_PyInterp.h" // WARNING! This include must be the first!
7
8 #include "LightApp_Application.h"
9 #include "LightApp_WidgetContainer.h"
10 #include "LightApp_Module.h"
11 #include "LightApp_DataModel.h"
12 #include "LightApp_Study.h"
13 #include "LightApp_Preferences.h"
14 #include "LightApp_PreferencesDlg.h"
15 #include "LightApp_ModuleDlg.h"
16 #include "LightApp_AboutDlg.h"
17
18 #include "LightApp_OBFilter.h"
19
20 #include "LightApp_GLSelector.h"
21 #include "LightApp_OBSelector.h"
22 #include "LightApp_OCCSelector.h"
23 #include "LightApp_VTKSelector.h"
24 #include "LightApp_SelectionMgr.h"
25
26 #include <CAM_Module.h>
27 #include <CAM_DataModel.h>
28 #include <CAM_Study.h>
29 #include <STD_TabDesktop.h>
30
31 #include <SUIT_Session.h>
32 #include <SUIT_Study.h>
33 #include <SUIT_FileDlg.h>
34 #include <SUIT_ResourceMgr.h>
35 #include <SUIT_Tools.h>
36 #include <SUIT_Accel.h>
37
38 #include <QtxMRUAction.h>
39 #include <QtxDockAction.h>
40 #include <QtxToolBar.h>
41
42 #include <LogWindow.h>
43 #include <OB_Browser.h>
44 #include <PythonConsole_PyConsole.h>
45
46 #include <GLViewer_Viewer.h>
47 #include <GLViewer_ViewManager.h>
48
49 #include <Plot2d_ViewManager.h>
50 #include <Plot2d_ViewModel.h>
51 #include <SPlot2d_ViewModel.h>
52
53 #include <OCCViewer_ViewManager.h>
54 #include <SOCC_ViewModel.h>
55
56 #include <SVTK_ViewModel.h>
57 #include <SVTK_ViewManager.h>
58 #include <VTKViewer_ViewModel.h>
59
60 #include <SUPERVGraph_ViewModel.h>
61 #include <SUPERVGraph_ViewFrame.h>
62 #include <SUPERVGraph_ViewManager.h>
63
64 #include <qdir.h>
65 #include <qimage.h>
66 #include <qstring.h>
67 #include <qwidget.h>
68 #include <qstringlist.h>
69 #include <qfile.h>
70 #include <qapplication.h>
71 #include <qmap.h>
72 #include <qstatusbar.h>
73 #include <qthread.h>
74
75 #define OBJECT_BROWSER_WIDTH 300
76 #define OBJECT_COLUMN_WIDTH 150
77
78 #define DEFAULT_BROWSER "mozilla"
79
80 #define FIRST_HELP_ID 1000000
81
82 #include "SALOME_InteractiveObject.hxx"
83 #include "SALOME_ListIO.hxx"
84
85 static const char* imageEmptyIcon[] = {
86 "20 20 1 1",
87 ".      c None",
88 "....................",
89 "....................",
90 "....................",
91 "....................",
92 "....................",
93 "....................",
94 "....................",
95 "....................",
96 "....................",
97 "....................",
98 "....................",
99 "....................",
100 "....................",
101 "....................",
102 "....................",
103 "....................",
104 "....................",
105 "....................",
106 "....................",
107 "...................."};
108
109 /*!Create new instance of LightApp_Application.*/
110 extern "C" LIGHTAPP_EXPORT SUIT_Application* createApplication()
111 {
112   return new LightApp_Application();
113 }
114
115 LightApp_Preferences* LightApp_Application::_prefs_ = 0;
116
117 /*
118   Class       : LightApp_Application
119   Description : Application containing LightApp module
120 */
121
122 /*!Constructor.*/
123 LightApp_Application::LightApp_Application()
124 : CAM_Application( false ),
125 myPrefs( 0 )
126 {
127   STD_TabDesktop* desk = new STD_TabDesktop();
128
129   setDesktop( desk );
130
131   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
132   QPixmap aLogo = aResMgr->loadPixmap( "LightApp", tr( "APP_DEFAULT_ICO" ), false );
133
134   desktop()->setIcon( aLogo );
135   desktop()->setDockableMenuBar( true );
136   desktop()->setDockableStatusBar( false );
137
138   // base logo (salome itself)
139   desktop()->addLogo( "_app_base",  aResMgr->loadPixmap( "LightApp", tr( "APP_BASE_LOGO" ), false ) );
140   // extra logo (salome-based application)
141   desktop()->addLogo( "_app_extra", aResMgr->loadPixmap( "LightApp", tr( "APP_EXTRA_LOGO" ), false ) );
142
143   clearViewManagers();
144
145   mySelMgr = new LightApp_SelectionMgr( this );
146
147   myAccel = new SUIT_Accel( desktop() );
148   myAccel->setActionKey( SUIT_Accel::PanLeft,     CTRL+Key_Left,     OCCViewer_Viewer::Type() );
149   myAccel->setActionKey( SUIT_Accel::PanRight,    CTRL+Key_Right,    OCCViewer_Viewer::Type() );
150   myAccel->setActionKey( SUIT_Accel::PanUp,       CTRL+Key_Up,       OCCViewer_Viewer::Type() );
151   myAccel->setActionKey( SUIT_Accel::PanDown,     CTRL+Key_Down,     OCCViewer_Viewer::Type() );
152   myAccel->setActionKey( SUIT_Accel::ZoomIn,      CTRL+Key_Plus,     OCCViewer_Viewer::Type() );
153   myAccel->setActionKey( SUIT_Accel::ZoomOut,     CTRL+Key_Minus,    OCCViewer_Viewer::Type() );
154   myAccel->setActionKey( SUIT_Accel::ZoomFit,     CTRL+Key_Asterisk, OCCViewer_Viewer::Type() );
155   myAccel->setActionKey( SUIT_Accel::RotateLeft,  ALT+Key_Left,      OCCViewer_Viewer::Type() );
156   myAccel->setActionKey( SUIT_Accel::RotateRight, ALT+Key_Right,     OCCViewer_Viewer::Type() );
157   myAccel->setActionKey( SUIT_Accel::RotateUp,    ALT+Key_Up,        OCCViewer_Viewer::Type() );
158   myAccel->setActionKey( SUIT_Accel::RotateDown,  ALT+Key_Down,      OCCViewer_Viewer::Type() );
159   myAccel->setActionKey( SUIT_Accel::PanLeft,     CTRL+Key_Left,     VTKViewer_Viewer::Type() );
160   myAccel->setActionKey( SUIT_Accel::PanRight,    CTRL+Key_Right,    VTKViewer_Viewer::Type() );
161   myAccel->setActionKey( SUIT_Accel::PanUp,       CTRL+Key_Up,       VTKViewer_Viewer::Type() );
162   myAccel->setActionKey( SUIT_Accel::PanDown,     CTRL+Key_Down,     VTKViewer_Viewer::Type() );
163   myAccel->setActionKey( SUIT_Accel::ZoomIn,      CTRL+Key_Plus,     VTKViewer_Viewer::Type() );
164   myAccel->setActionKey( SUIT_Accel::ZoomOut,     CTRL+Key_Minus,    VTKViewer_Viewer::Type() );
165   myAccel->setActionKey( SUIT_Accel::ZoomFit,     CTRL+Key_Asterisk, VTKViewer_Viewer::Type() );
166   myAccel->setActionKey( SUIT_Accel::RotateLeft,  ALT+Key_Left,      VTKViewer_Viewer::Type() );
167   myAccel->setActionKey( SUIT_Accel::RotateRight, ALT+Key_Right,     VTKViewer_Viewer::Type() );
168   myAccel->setActionKey( SUIT_Accel::RotateUp,    ALT+Key_Up,        VTKViewer_Viewer::Type() );
169   myAccel->setActionKey( SUIT_Accel::RotateDown,  ALT+Key_Down,      VTKViewer_Viewer::Type() );
170
171   connect( mySelMgr, SIGNAL( selectionChanged() ), this, SLOT( onSelection() ) );
172 }
173
174 /*!Destructor.
175  *\li Save window geometry.
176  *\li Save desktop geometry.
177  *\li Save resource maneger.
178  *\li Delete selection manager.
179  */
180 LightApp_Application::~LightApp_Application()
181 {
182   saveWindowsGeometry();
183
184   if ( resourceMgr() )
185   {
186     if ( desktop() )
187       desktop()->saveGeometry( resourceMgr(), "desktop" );
188     resourceMgr()->save();
189   }
190   delete mySelMgr;
191 }
192
193 /*!Start application.*/
194 void LightApp_Application::start()
195 {
196   if ( desktop() )
197     desktop()->loadGeometry( resourceMgr(), "desktop" );
198
199   CAM_Application::start();
200
201   QAction* a = action( ViewWindowsId );
202   if ( a && a->inherits( "QtxDockAction" ) )
203     ((QtxDockAction*)a)->setAutoPlace( true );
204
205   updateWindows();
206   updateViewManagers();
207
208   putInfo( "" );
209   desktop()->statusBar()->message( "" );
210 }
211
212 /*!Gets application name.*/
213 QString LightApp_Application::applicationName() const
214 {
215   return tr( "APP_NAME" );
216 }
217
218 /*!Gets application version.*/
219 QString LightApp_Application::applicationVersion() const
220 {
221   static QString _app_version;
222
223   if ( _app_version.isEmpty() )
224   {
225     QString resVersion = tr( "APP_VERSION" );
226     if ( resVersion != "APP_VERSION" )
227     {
228       _app_version = resVersion;
229     }
230     else
231     {
232       QString path( ::getenv( "GUI_ROOT_DIR" ) );
233       if ( !path.isEmpty() )
234         path += QDir::separator();
235       path += QString( "bin/salome/VERSION" );
236
237       QFile vf( path );
238       if ( vf.open( IO_ReadOnly ) )
239       {
240         QString line;
241         vf.readLine( line, 1024 );
242         vf.close();
243
244         if ( !line.isEmpty() )
245         {
246           while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
247             line.remove( line.length() - 1, 1 );
248
249           int idx = line.findRev( ":" );
250           if ( idx != -1 )
251             _app_version = line.mid( idx + 1 ).stripWhiteSpace();
252         }
253       }
254     }
255   }
256   return _app_version;  
257 }
258
259 /*!Load module by \a name.*/
260 CAM_Module* LightApp_Application::loadModule( const QString& name )
261 {
262   CAM_Module* mod = CAM_Application::loadModule( name );
263   if ( mod )
264   {
265     connect( this, SIGNAL( studyOpened() ), mod, SLOT( onModelOpened() ) );
266     connect( this, SIGNAL( studySaved() ),  mod, SLOT( onModelSaved() ) );
267     connect( this, SIGNAL( studyClosed() ), mod, SLOT( onModelClosed() ) );
268   }
269   return mod;
270 }
271
272 /*!Activate module by \a modName*/
273 bool LightApp_Application::activateModule( const QString& modName )
274 {
275   QString actName;
276   CAM_Module* prevMod = activeModule();
277
278   if ( prevMod )
279     actName = prevMod->moduleName();
280
281   if ( actName == modName )
282     return true;
283
284   putInfo( tr( "ACTIVATING_MODULE" ).arg( modName ) );  
285
286   saveWindowsGeometry();
287
288   bool status = CAM_Application::activateModule( modName );
289
290   updateModuleActions();
291
292   putInfo( "" );  
293
294   if ( !status )
295     return false;
296
297   updateWindows();
298   updateViewManagers();
299
300   return true;
301 }
302
303 bool LightApp_Application::useStudy(const QString& theName)
304 {
305   createEmptyStudy();
306   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(activeStudy());
307   bool res = false;
308   if (aStudy)
309     res = aStudy->loadDocument( theName );
310   updateDesktopTitle();
311   updateCommandsStatus();
312   return res;
313 }
314
315 /*!Gets selection manager.*/
316 LightApp_SelectionMgr* LightApp_Application::selectionMgr() const
317 {
318   return mySelMgr;
319 }
320
321 /*!Create actions:*/
322 void LightApp_Application::createActions()
323 {
324   STD_Application::createActions();
325
326   SUIT_Desktop* desk = desktop();
327   SUIT_ResourceMgr* resMgr = resourceMgr();
328
329   //! Preferences
330   createAction( PreferencesId, tr( "TOT_DESK_PREFERENCES" ), QIconSet(),
331                 tr( "MEN_DESK_PREFERENCES" ), tr( "PRP_DESK_PREFERENCES" ),
332                 CTRL+Key_P, desk, false, this, SLOT( onPreferences() ) );
333
334   //! Help for modules
335   int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
336   int helpModuleMenu = createMenu( tr( "MEN_DESK_MODULE_HELP" ), helpMenu, -1, 0 );
337   createMenu( separator(), helpMenu, -1, 1 );
338
339   QStringList aModuleList;
340   modules( aModuleList, false );
341
342   int id = LightApp_Application::UserID + FIRST_HELP_ID;
343   // help for KERNEL and GUI
344   QCString dir;
345   QAction* a;
346   if (dir = getenv("GUI_ROOT_DIR")) {
347     a = createAction( id, tr( QString("Kernel & GUI Help") ), QIconSet(),
348                       tr( QString("Kernel && GUI Help") ),
349                       tr( QString("Kernel & GUI Help") ),
350                       0, desk, false, this, SLOT( onHelpContentsModule() ) );
351     a->setName( QString("GUI") );
352     createMenu( a, helpModuleMenu, -1 );
353     id++;
354   }
355   // help for other existing modules
356   QStringList::Iterator it;
357   for ( it = aModuleList.begin(); it != aModuleList.end(); ++it )
358   {
359     if ( (*it).isEmpty() )
360       continue;
361
362     QString modName = moduleName( *it );
363     if ( modName.compare("GEOM") == 0 ) { // to be removed when documentation for other modules will be done
364       QAction* a = createAction( id, tr( moduleTitle(modName) + QString(" Help") ), QIconSet(),
365                                  tr( moduleTitle(modName) + QString(" Help") ),
366                                  tr( moduleTitle(modName) + QString(" Help") ),
367                                  0, desk, false, this, SLOT( onHelpContentsModule() ) );
368       a->setName( modName );
369       createMenu( a, helpModuleMenu, -1 );
370       id++;
371     }
372   }
373
374   //! MRU
375   QtxMRUAction* mru = new QtxMRUAction( tr( "TOT_DESK_MRU" ), tr( "MEN_DESK_MRU" ), desk );
376   connect( mru, SIGNAL( activated( QString ) ), this, SLOT( onMRUActivated( QString ) ) );
377   registerAction( MRUId, mru );
378
379   // default icon for neutral point ('SALOME' module)
380   QPixmap defIcon = resMgr->loadPixmap( "LightApp", tr( "APP_DEFAULT_ICO" ), false );
381   if ( defIcon.isNull() )
382     defIcon = QPixmap( imageEmptyIcon );
383
384   //! default icon for any module
385   QPixmap modIcon = resMgr->loadPixmap( "LightApp", tr( "APP_MODULE_ICO" ), false );
386   if ( modIcon.isNull() )
387     modIcon = QPixmap( imageEmptyIcon );
388
389   QToolBar* modTBar = new QtxToolBar( true, desk );
390   modTBar->setLabel( tr( "INF_TOOLBAR_MODULES" ) );
391
392   QActionGroup* modGroup = new QActionGroup( this );
393   modGroup->setExclusive( true );
394   modGroup->setUsesDropDown( true );
395
396   a = createAction( -1, tr( "APP_NAME" ), defIcon, tr( "APP_NAME" ),
397                     tr( "PRP_APP_MODULE" ), 0, desk, true );
398   modGroup->add( a );
399   myActions.insert( QString(), a );
400
401   QMap<QString, QString> iconMap;
402   moduleIconNames( iconMap );
403
404   const int iconSize = 20;
405
406   modGroup->addTo( modTBar );
407   modTBar->addSeparator();
408
409   QStringList modList;
410   modules( modList, false );
411
412   for ( it = modList.begin(); it != modList.end(); ++it )
413   {
414     if ( (*it).isEmpty() )
415       continue;
416
417     QString iconName;
418     if ( iconMap.contains( *it ) )
419       iconName = iconMap[*it];
420
421     QString modName = moduleName( *it );
422
423     QPixmap icon = resMgr->loadPixmap( modName, iconName, false );
424     if ( icon.isNull() )
425       icon = modIcon;
426
427     icon.convertFromImage( icon.convertToImage().smoothScale( iconSize, iconSize, QImage::ScaleMin ) );
428
429     QAction* a = createAction( -1, *it, icon, *it, tr( "PRP_MODULE" ).arg( *it ), 0, desk, true );
430     a->addTo( modTBar );
431     modGroup->add( a );
432
433     myActions.insert( *it, a );
434   }
435
436   SUIT_Tools::simplifySeparators( modTBar );
437
438   // New window
439   int windowMenu = createMenu( tr( "MEN_DESK_WINDOW" ), -1, 100 );
440   int newWinMenu = createMenu( tr( "MEN_DESK_NEWWINDOW" ), windowMenu, -1, 0 );
441   createMenu( separator(), windowMenu, -1, 1 );
442
443   QMap<int, int> accelMap;
444   accelMap[NewGLViewId]  = ALT+Key_G;
445   accelMap[NewPlot2dId]  = ALT+Key_P;
446   accelMap[NewOCCViewId] = ALT+Key_O;
447   accelMap[NewVTKViewId] = ALT+Key_K;
448
449   for ( id = NewGLViewId; id <= NewVTKViewId; id++ )
450   {
451     QAction* a = createAction( id, tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ), QIconSet(),
452                                tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ),
453                                tr( QString( "NEW_WINDOW_%1" ).arg( id - NewGLViewId ) ),
454                                accelMap.contains( id ) ? accelMap[id] : 0, desk, false, this, SLOT( onNewWindow() ) );
455     createMenu( a, newWinMenu, -1 );
456   }
457
458   connect( modGroup, SIGNAL( selected( QAction* ) ), this, SLOT( onModuleActivation( QAction* ) ) );
459
460   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1 );
461   createMenu( PreferencesId, fileMenu, 15, -1 );
462   createMenu( separator(), fileMenu, -1, 15, -1 );
463
464   /*
465   createMenu( separator(), fileMenu, -1, 100, -1 );
466   createMenu( MRUId, fileMenu, 100, -1 );
467   createMenu( separator(), fileMenu, -1, 100, -1 );
468   */
469 }
470
471 /*!On module activation action.*/
472 void LightApp_Application::onModuleActivation( QAction* a )
473 {
474   if ( !a )
475     return;
476
477   QString modName = a->menuText();
478   if ( modName == tr( "APP_NAME" ) )
479     modName = QString::null;
480
481   // Force user to create/open a study before module activation
482   QMap<QString, QString> iconMap;
483   moduleIconNames( iconMap );
484   QPixmap icon = resourceMgr()->loadPixmap( moduleName( modName ), iconMap[ modName ], false );
485   if ( icon.isNull() )
486     icon = resourceMgr()->loadPixmap( "LightApp", tr( "APP_MODULE_BIG_ICO" ), false ); // default icon for any module
487
488   bool cancelled = false;
489   while ( !modName.isEmpty() && !activeStudy() && !cancelled ){
490     LightApp_ModuleDlg aDlg( desktop(), modName, icon );
491     int res = aDlg.exec();
492
493     switch ( res ){
494     case 1:
495       onNewDoc();
496       break;
497     case 2:
498       onOpenDoc();
499       break;
500     case 3:
501       //onLoadStudy();
502       //break;
503     case 0:
504     default:
505       putInfo( tr("INF_CANCELLED") );
506       myActions[QString()]->setOn( true );
507       cancelled = true;
508     }
509   }
510
511   if ( !cancelled )
512     activateModule( modName );
513 }
514
515 /*!Default module activation.*/
516 QString LightApp_Application::defaultModule() const
517 {
518   QStringList aModuleNames;
519   modules( aModuleNames, false ); // obtain a complete list of module names for the current configuration
520   //! If there's the one and only module --> activate it automatically
521   //! TODO: Possible improvement - default module can be taken from preferences
522   return aModuleNames.count() > 1 ? "" : ( aModuleNames.count() ? aModuleNames.first() : "" );
523 }
524
525 /*!On new window slot.*/
526 void LightApp_Application::onNewWindow()
527 {
528   const QObject* obj = sender();
529   if ( !obj || !obj->inherits( "QAction" ) )
530     return;
531
532   QString type;
533   int id = actionId( (QAction*)obj );
534   switch ( id )
535   {
536   case NewGLViewId:
537     type = GLViewer_Viewer::Type();
538     break;
539   case NewPlot2dId:
540     type = Plot2d_Viewer::Type();
541     break;
542   case NewOCCViewId:
543     type = OCCViewer_Viewer::Type();
544     break;
545   case NewVTKViewId:
546     type = VTKViewer_Viewer::Type();
547     break;
548   }
549
550   if ( !type.isEmpty() )
551     createViewManager( type );
552 }
553
554 //=======================================================================
555 //  name    : onNewDoc
556 /*! Purpose : SLOT. Create new document*/
557 //=======================================================================
558 void LightApp_Application::onNewDoc()
559 {
560   SUIT_Study* study = activeStudy();
561
562   saveWindowsGeometry();
563
564   CAM_Application::onNewDoc();
565
566   if ( !study ) // new study will be create in THIS application
567   {
568     updateWindows();
569     updateViewManagers();
570   }
571 }
572
573 //=======================================================================
574 // name    : onOpenDoc
575 /*! Purpose : SLOT. Open new document*/
576 //=======================================================================
577 void LightApp_Application::onOpenDoc()
578 {
579   SUIT_Study* study = activeStudy();
580   saveWindowsGeometry();
581
582   CAM_Application::onOpenDoc();
583
584   if ( !study ) // new study will be create in THIS application
585   {
586     updateWindows();
587     updateViewManagers();
588   }
589 }
590
591 #include "SUIT_MessageBox.h"
592 /*! Purpose : SLOT. Open new document with \a aName.*/
593 bool LightApp_Application::onOpenDoc( const QString& aName )
594 {
595   bool isAlreadyOpen = false;
596
597   // Look among opened studies
598   if (activeStudy()) { // at least one study is opened
599     SUIT_Session* aSession = SUIT_Session::session();
600     QPtrList<SUIT_Application> aAppList = aSession->applications();
601     QPtrListIterator<SUIT_Application> it (aAppList);
602     SUIT_Application* aApp = 0;
603     // iterate on all applications
604     for (; (aApp = it.current()) && !isAlreadyOpen; ++it) {
605       if (aApp->activeStudy()->studyName() == aName) {
606         isAlreadyOpen = true; // Already opened, ask user what to do
607
608         // The document ... is already open.
609         // Do you want to reload it?
610         int aAnswer = SUIT_MessageBox::warn2(desktop(), tr("WRN_WARNING"),
611                                              tr("QUE_DOC_ALREADYOPEN").arg(aName),
612                                              tr("BUT_YES"), tr("BUT_NO"), 1, 2, 2);
613         if (aAnswer == 1) { // reload
614           if (activeStudy()->studyName() == aName && aAppList.count() > 1) {
615             // Opened in THIS (active) application.
616             STD_Application* app1 = (STD_Application*)aAppList.at(0);
617             STD_Application* app2 = (STD_Application*)aAppList.at(1);
618             if (!app1 || !app2) {
619               // Error
620               return false;
621             }
622             if (app1->activeStudy()->studyName() == aName) {
623               // app1 is this application, we need another one
624               app1 = app2;
625             }
626             // Close document of this application. This application will be destroyed.
627             onCloseDoc(/*ask = */false);
628             // Open the file with another application, as this one will be destroyed.
629             return app1->onOpenDoc(aName);
630           } else {
631             // Opened in another application.
632             STD_Application* app = (STD_Application*)aApp;
633             if (app)
634               app->onCloseDoc(/*ask = */false);
635           }
636         } else { // do not reload
637           // OK, the study will not be reloaded, but we call
638           // CAM_Application::onOpenDoc( aName ) all the same.
639           // It will activate a desktop of the study <aName>.
640         }
641       }
642     }
643   }
644
645   bool res = CAM_Application::onOpenDoc( aName );
646
647   QAction* a = action( MRUId );
648   if ( a && a->inherits( "QtxMRUAction" ) )
649   {
650     QtxMRUAction* mru = (QtxMRUAction*)a;
651     if ( res )
652       mru->insert( aName );
653     else
654       mru->remove( aName );
655   }
656   return res;
657 }
658
659 //=======================================================================
660 // name    : onHelpAbout
661 /*! Purpose : SLOT. Display "About" message box*/
662 //=======================================================================
663 void LightApp_Application::onHelpAbout()
664 {
665   LightApp_AboutDlg* dlg = new LightApp_AboutDlg( applicationName(), applicationVersion(), desktop() );
666   dlg->exec();
667   delete dlg;
668 }
669
670 /*!SLOT. Load document with \a aName.*/
671 bool LightApp_Application::onLoadDoc( const QString& aName )
672 {
673   bool res = CAM_Application::onLoadDoc( aName );
674
675   /*jfa tmp:QAction* a = action( MRUId );
676   if ( a && a->inherits( "QtxMRUAction" ) )
677   {
678     QtxMRUAction* mru = (QtxMRUAction*)a;
679     if ( res )
680       mru->insert( aName );
681     else
682       mru->remove( aName );
683   }*/
684   return res;
685 }
686
687 /*!Private SLOT. Selection.*/
688 void LightApp_Application::onSelection()
689 {
690   onSelectionChanged();
691
692   if ( activeModule() && activeModule()->inherits( "LightApp_Module" ) )
693     ((LightApp_Module*)activeModule())->selectionChanged();
694 }
695
696 /*!Set active study.
697  *\param study - SUIT_Study.
698  */
699 void LightApp_Application::setActiveStudy( SUIT_Study* study )
700 {
701   CAM_Application::setActiveStudy( study );
702
703   activateWindows();
704 }
705
706 //=======================================================================
707 // name    : updateCommandsStatus
708 /*! Purpose : Enable/Disable menu items and toolbar buttons. Rebuild menu*/
709 //=======================================================================
710 void LightApp_Application::updateCommandsStatus()
711 {
712   CAM_Application::updateCommandsStatus();
713
714   for ( int id = NewGLViewId; id <= NewVTKViewId; id++ )
715   {
716     QAction* a = action( id );
717     if ( a )
718       a->setEnabled( activeStudy() );
719   }
720 }
721
722 // Helps to execute command
723 class RunBrowser: public QThread {
724 public:
725
726   RunBrowser(QString theApp, QString theParams, QString theHelpFile, QString theContext=NULL):
727     myApp(theApp), myParams(theParams), myHelpFile("file:" + theHelpFile + theContext), myStatus(0) {};
728
729   virtual void run()
730   {
731     QString aCommand;
732
733     if ( !myApp.isEmpty())
734       {
735         aCommand.sprintf("%s %s %s",myApp.latin1(),myParams.latin1(),myHelpFile.latin1());
736         myStatus = system(aCommand);
737         if(myStatus != 0)
738           {
739             QCustomEvent* ce2000 = new QCustomEvent (2000);
740             postEvent (qApp, ce2000);
741           }
742       }
743
744     if( myStatus != 0 || myApp.isEmpty())
745       {
746         myParams = "";
747         aCommand.sprintf("%s %s %s", QString(DEFAULT_BROWSER).latin1(),myParams.latin1(), myHelpFile.latin1());
748         myStatus = system(aCommand);
749         if(myStatus != 0)
750           {
751             QCustomEvent* ce2001 = new QCustomEvent (2001);
752             postEvent (qApp, ce2001);
753           }
754       }
755   }
756
757 private:
758   QString myApp;
759   QString myParams;
760   QString myHelpFile;
761   int myStatus;
762
763 };
764
765 //=======================================================================
766 // name    : onHelpContentsModule
767 /*! Purpose : SLOT. Display help contents for choosen module*/
768 //=======================================================================
769 void LightApp_Application::onHelpContentsModule()
770 {
771   const QAction* obj = (QAction*) sender();
772
773   QString aComponentName = obj->name();
774   QString aFileName = aComponentName + "_index.html";
775
776   QCString dir;
777   QString root;
778   QString homeDir;
779   if (dir = getenv( aComponentName + "_ROOT_DIR")) {
780     root = Qtx::addSlash( Qtx::addSlash(dir) +  Qtx::addSlash("doc") +  Qtx::addSlash("salome") );
781     if ( QFileInfo( root + aFileName ).exists() ) {
782       homeDir = root;
783     } else {
784       SUIT_MessageBox::warn1( desktop(), tr("WRN_WARNING"),
785                               QString( "%1"+ aFileName + " doesn't exist." ).arg(root), tr ("BUT_OK") );
786       return;
787     }
788   }
789
790   QString helpFile = QFileInfo( homeDir + aFileName ).absFilePath();
791   SUIT_ResourceMgr* resMgr = resourceMgr();
792   QString anApp = resMgr->stringValue("ExternalBrowser", "application");
793   QString aParams = resMgr->stringValue("ExternalBrowser", "parameters");
794
795   RunBrowser* rs = new RunBrowser(anApp, aParams, helpFile);
796   rs->start();
797 }
798
799 /*!Sets enable or disable some actions on selection changed.*/
800 void LightApp_Application::onSelectionChanged()
801 {
802 }
803
804 /*!Return window.
805  *\param flag - key for window
806  *\param studyId - study id
807  * Flag used how identificator of window in windows list.
808  */
809 QWidget* LightApp_Application::window( const int flag, const int studyId ) const
810 {
811   QWidget* wid = 0;
812
813  int sId = studyId;
814   if ( sId < 0 )
815   {
816     if ( !activeStudy() )
817       return 0;
818     else
819       sId = activeStudy()->id();
820   }
821
822   if ( myWindows.contains( flag ) )
823     wid = myWindows[flag]->widget( sId );
824
825   return wid;
826 }
827
828 /*!Adds window to application.
829  *\param wid - QWidget
830  *\param flag - key wor window
831  *\param studyId - study id
832  * Flag used how identificator of window in windows list.
833  */
834 void LightApp_Application::addWindow( QWidget* wid, const int flag, const int studyId )
835 {
836   if ( !wid )
837     return;
838
839   int sId = studyId;
840   if ( sId < 0 )
841   {
842     if ( !activeStudy() )
843       return;
844     else
845       sId = activeStudy()->id();
846   }
847
848   if ( !myWindows.contains( flag ) )
849   {
850     QMap<int, int> winMap;
851     currentWindows( winMap );
852
853     LightApp_WidgetContainer* newWC = new LightApp_WidgetContainer( flag, desktop() );
854     connect( newWC, SIGNAL(  destroyed ( QObject* ) ), this, SLOT( onWCDestroyed( QObject* ) ) );
855     myWindows.insert( flag, newWC );
856     if ( winMap.contains( flag ) )
857       desktop()->moveDockWindow( myWindows[flag], (Dock)winMap[flag] );
858
859     myWindows[flag]->setResizeEnabled( true );
860     myWindows[flag]->setCloseMode( QDockWindow::Always );
861     myWindows[flag]->setName( QString( "dock_window_%1" ).arg( flag ) );
862   }
863
864   QFont f;
865   if( wid->inherits( "PythonConsole" ) )
866     f = ( ( PythonConsole* )wid )->font();
867   else
868     f = wid->font();
869
870   myWindows[flag]->insert( sId, wid );
871   wid->setFont(f);
872
873   setWindowShown( flag, !myWindows[flag]->isEmpty() );
874 }
875
876 /*!Remove window from application.
877  *\param flag - key wor window
878  *\param studyId - study id
879  * Flag used how identificator of window in windows list.
880  */
881 void LightApp_Application::removeWindow( const int flag, const int studyId )
882 {
883   if ( !myWindows.contains( flag ) )
884     return;
885
886   int sId = studyId;
887   if ( sId < 0 )
888   {
889     if ( !activeStudy() )
890       return;
891     else
892       sId = activeStudy()->id();
893   }
894
895   QWidget* wid = myWindows[flag]->widget( sId );
896   myWindows[flag]->remove( sId );
897   delete wid;
898
899   setWindowShown( flag, !myWindows[flag]->isEmpty() );
900 }
901
902 /*!Gets window.
903  *\param flag - key wor window
904  *\param studyId - study id
905  * Flag used how identificator of window in windows list.
906  */
907 QWidget* LightApp_Application::getWindow( const int flag, const int studyId )
908 {
909   QWidget* wid = window( flag, studyId );
910   if ( !wid )
911     addWindow( wid = createWindow( flag ), flag, studyId );
912
913   return wid;
914 }
915
916 /*!Check is window visible?(with identificator \a type)*/
917 bool LightApp_Application::isWindowVisible( const int type ) const
918 {
919   bool res = false;
920   if ( myWindows.contains( type ) )
921   {
922     SUIT_Desktop* desk = ((LightApp_Application*)this)->desktop();
923     res = desk && desk->appropriate( myWindows[type] );
924   }
925   return res;
926 }
927
928 /*!Sets window show or hide.
929  *\param type - window identificator.
930  *\param on   - true/false (window show/hide)
931  */
932 void LightApp_Application::setWindowShown( const int type, const bool on )
933 {
934   if ( !desktop() || !myWindows.contains( type ) )
935     return;
936
937   QDockWindow* dw = myWindows[type];
938   desktop()->setAppropriate( dw, on );
939   on ? dw->show() : dw->hide();
940 }
941
942 /*!Gets "ObjectBrowser".*/
943 OB_Browser* LightApp_Application::objectBrowser()
944 {
945   OB_Browser* ob = 0;
946   QWidget* wid = getWindow( WT_ObjectBrowser );
947   if ( wid->inherits( "OB_Browser" ) )
948     ob = (OB_Browser*)wid;
949   return ob;
950 }
951
952 /*!Gets "LogWindow".*/
953 LogWindow* LightApp_Application::logWindow()
954 {
955   LogWindow* lw = 0;
956   QWidget* wid = getWindow( WT_LogWindow );
957   if ( wid->inherits( "LogWindow" ) )
958     lw = (LogWindow*)wid;
959   return lw;
960 }
961
962 /*!Get "PythonConsole"*/
963 PythonConsole* LightApp_Application::pythonConsole()
964 {
965   PythonConsole* console = 0;
966   QWidget* wid = getWindow( WT_PyConsole );
967   if ( wid->inherits( "PythonConsole" ) )
968     console = (PythonConsole*)wid;
969   return console;
970 }
971
972 /*!Update obect browser*/
973 void LightApp_Application::updateObjectBrowser( const bool updateModels )
974 {
975   // update existing data models
976   if ( updateModels ) 
977   {
978     LightApp_Study* study = dynamic_cast<LightApp_Study*>(activeStudy());
979     if ( study ) {
980       CAM_Study::ModelList dm_list;
981       study->dataModels( dm_list );
982       for ( CAM_Study::ModelListIterator it( dm_list ); it.current(); ++it ) {
983         CAM_DataModel* camDM = it.current();
984         if ( camDM && camDM->inherits( "LightApp_DataModel" ) )
985           ((LightApp_DataModel*)camDM)->update();
986       }
987     }
988   }
989   if ( objectBrowser() )
990   {
991     objectBrowser()->updateGeometry();
992     objectBrowser()->updateTree();
993   }
994 }
995
996 /*!Gets preferences.*/
997 LightApp_Preferences* LightApp_Application::preferences() const
998 {
999   return preferences( false );
1000 }
1001
1002 /*!Gets view manager*/
1003 SUIT_ViewManager* LightApp_Application::getViewManager( const QString& vmType, const bool create )
1004 {
1005   SUIT_ViewManager* aVM = viewManager( vmType );
1006   SUIT_ViewManager* anActiveVM = CAM_Application::activeViewManager();
1007
1008   if ( anActiveVM && anActiveVM->getType() == vmType )
1009     aVM = anActiveVM;
1010
1011   if ( aVM && create )
1012   {
1013     if ( !aVM->getActiveView() )
1014       aVM->createView();
1015     else
1016       aVM->getActiveView()->setFocus();
1017   }
1018   else if ( create )
1019     aVM = createViewManager( vmType );
1020
1021   return aVM;
1022 }
1023
1024 /*!Create view manager.*/
1025 SUIT_ViewManager* LightApp_Application::createViewManager( const QString& vmType )
1026 {
1027   SUIT_ResourceMgr* resMgr = resourceMgr();
1028
1029   SUIT_ViewManager* viewMgr = 0;
1030   if( vmType == GLViewer_Viewer::Type() )
1031   {
1032     viewMgr = new GLViewer_ViewManager( activeStudy(), desktop() );
1033     new LightApp_GLSelector( (GLViewer_Viewer2d*)viewMgr->getViewModel(), mySelMgr );
1034   }
1035   else if( vmType == Plot2d_Viewer::Type() )
1036   {
1037     viewMgr = new Plot2d_ViewManager( activeStudy(), desktop() );
1038     SPlot2d_Viewer* vm = new SPlot2d_Viewer();
1039     viewMgr->setViewModel( vm  );// custom view model, which extends SALOME_View interface 
1040     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( viewMgr->getActiveView() );
1041     if( wnd )
1042     {
1043       Plot2d_ViewFrame* frame = wnd->getViewFrame();
1044       frame->setBackgroundColor( resMgr->colorValue( "Plot2d", "Background", frame->backgroundColor() ) );
1045     }
1046   }
1047   else if( vmType == SUPERVGraph_Viewer::Type() )
1048   {
1049     viewMgr = new SUPERVGraph_ViewManager( activeStudy(), desktop() );
1050     SUPERVGraph_Viewer* vm = new SUPERVGraph_Viewer();
1051     SUPERVGraph_ViewFrame* view = dynamic_cast<SUPERVGraph_ViewFrame*>( vm->getViewManager()->getActiveView() );
1052     if( view )
1053       view->setBackgroundColor( resMgr->colorValue( "SUPERVGraph", "Background", view->backgroundColor() ) );
1054   }
1055   else if( vmType == OCCViewer_Viewer::Type() )
1056   {
1057     viewMgr = new OCCViewer_ViewManager( activeStudy(), desktop() );
1058     SOCC_Viewer* vm = new SOCC_Viewer();
1059     vm->setBackgroundColor( resMgr->colorValue( "OCCViewer", "background", vm->backgroundColor() ) );
1060     vm->setTrihedronSize( resMgr->integerValue( "OCCViewer", "trihedron_size", vm->trihedronSize() ) );
1061     int u( 1 ), v( 1 );
1062     vm->isos( u, v );
1063     u = resMgr->integerValue( "OCCViewer", "iso_number_u", u );
1064     v = resMgr->integerValue( "OCCViewer", "iso_number_v", v );
1065     vm->setIsos( u, v );
1066     viewMgr->setViewModel( vm );// custom view model, which extends SALOME_View interface
1067     new LightApp_OCCSelector( (OCCViewer_Viewer*)viewMgr->getViewModel(), mySelMgr );
1068   }
1069   else if ( vmType == SVTK_Viewer::Type() )
1070   {
1071     viewMgr = new SVTK_ViewManager( activeStudy(), desktop() );
1072     SVTK_Viewer* vm = dynamic_cast<SVTK_Viewer*>( viewMgr->getViewModel() );
1073     if( vm )
1074     {
1075       vm->setBackgroundColor( resMgr->colorValue( "VTKViewer", "background", vm->backgroundColor() ) );
1076       vm->setTrihedronSize( resMgr->integerValue( "VTKViewer", "trihedron_size", vm->trihedronSize() ) );
1077       new LightApp_VTKSelector( vm, mySelMgr );
1078     }
1079   }
1080
1081   if ( !viewMgr )
1082     return 0;
1083
1084   addViewManager( viewMgr );
1085   SUIT_ViewWindow* viewWin = viewMgr->createViewWindow();
1086
1087   if ( viewWin && desktop() )
1088     viewWin->resize( (int)( desktop()->width() * 0.6 ), (int)( desktop()->height() * 0.6 ) );
1089
1090   connect( viewMgr, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1091            this, SLOT( onCloseView( SUIT_ViewManager* ) ) );
1092
1093   return viewMgr;
1094 }
1095
1096 //=======================================================================
1097 // name    : onCloseView
1098 /*! Purpose : SLOT. Remove view manager from application*/
1099 //=======================================================================
1100 void LightApp_Application::onCloseView( SUIT_ViewManager* theVM )
1101 {
1102   removeViewManager( theVM );
1103 }
1104
1105 /*!Protected SLOT. On study created.*/
1106 void LightApp_Application::onStudyCreated( SUIT_Study* theStudy )
1107 {
1108   SUIT_DataObject* aRoot = 0;
1109   if ( theStudy && theStudy->root() )
1110   {
1111     aRoot = theStudy->root();
1112     //aRoot->setName( tr( "DATA_MODELS" ) );
1113   }
1114   if ( objectBrowser() != 0 )
1115     objectBrowser()->setRootObject( aRoot );
1116
1117   activateModule( defaultModule() );
1118
1119   activateWindows();
1120 }
1121
1122 /*!Protected SLOT. On study opened.*/
1123 void LightApp_Application::onStudyOpened( SUIT_Study* theStudy )
1124 {
1125   SUIT_DataObject* aRoot = 0;
1126   if ( theStudy && theStudy->root() )
1127   {
1128     aRoot = theStudy->root();
1129     //aRoot->dump();
1130   }
1131   if ( objectBrowser() != 0 ) {
1132     objectBrowser()->setRootObject( aRoot );
1133   }
1134
1135   activateModule( defaultModule() );
1136
1137   activateWindows();
1138
1139   emit studyOpened();
1140 }
1141
1142 /*!Protected SLOT. On study saved.*/
1143 void LightApp_Application::onStudySaved( SUIT_Study* )
1144 {
1145   emit studySaved();
1146 }
1147
1148 /*!Protected SLOT. On study closed.*/
1149 void LightApp_Application::onStudyClosed( SUIT_Study* )
1150 {
1151   emit studyClosed();
1152
1153   activateModule( "" );
1154
1155   // Bug 10396: remove all selectors
1156   //delete mySelMgr;
1157   //mySelMgr = new LightApp_SelectionMgr( this );
1158   mySelMgr->clearSelected();
1159
1160   saveWindowsGeometry();
1161 }
1162
1163 /*!Protected SLOT.On desktop activated.*/
1164 void LightApp_Application::onDesktopActivated()
1165 {
1166   CAM_Application::onDesktopActivated();
1167   LightApp_Module* aModule = dynamic_cast<LightApp_Module*>(activeModule());
1168   if(aModule)
1169     aModule->studyActivated();
1170 }
1171
1172 /*!Gets file filter.
1173  *\retval QString "(*.bin)"
1174  */
1175 QString LightApp_Application::getFileFilter() const
1176 {
1177   return "(*.bin)";
1178 }
1179
1180 /*! Gets file name*/
1181 QString LightApp_Application::getFileName( bool open, const QString& initial, const QString& filters, 
1182                                            const QString& caption, QWidget* parent )
1183 {
1184   if ( !parent )
1185     parent = desktop();
1186   QStringList fls = QStringList::split( ";;", filters, false );
1187   return SUIT_FileDlg::getFileName( parent, initial, fls, caption, open, true );
1188 }
1189
1190 /*! Gets directory*/
1191 QString LightApp_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
1192 {
1193   if ( !parent )
1194     parent = desktop();
1195   return SUIT_FileDlg::getExistingDirectory( parent, initial, caption, true );
1196 }
1197
1198 /*! Get open file names*/
1199 QStringList LightApp_Application::getOpenFileNames( const QString& initial, const QString& filters, 
1200                                                     const QString& caption, QWidget* parent )
1201 {
1202   if ( !parent )
1203     parent = desktop();
1204   QStringList fls = QStringList::split( ";;", filters, false );
1205   return SUIT_FileDlg::getOpenFileNames( parent, initial, fls, caption, true );
1206 }
1207
1208 /*!Private SLOT. Update object browser.*/
1209 void LightApp_Application::onRefresh()
1210 {
1211   updateObjectBrowser( true );
1212 }
1213
1214 /*!Private SLOT. On preferences.*/
1215 void LightApp_Application::onPreferences()
1216 {
1217   QApplication::setOverrideCursor( Qt::waitCursor );
1218
1219   LightApp_PreferencesDlg* prefDlg = new LightApp_PreferencesDlg( preferences( true ), desktop());
1220
1221   QApplication::restoreOverrideCursor();
1222
1223   if ( !prefDlg )
1224     return;
1225
1226   prefDlg->exec();
1227
1228   delete prefDlg;
1229 }
1230
1231 /*!Protected SLOT. On preferences changed.*/
1232 void LightApp_Application::onPreferenceChanged( QString& modName, QString& section, QString& param )
1233 {
1234   LightApp_Module* sMod = 0;
1235   CAM_Module* mod = module( modName );
1236   if ( mod && mod->inherits( "LightApp_Module" ) )
1237     sMod = (LightApp_Module*)mod;
1238
1239   if ( sMod )
1240     sMod->preferencesChanged( section, param );
1241   else
1242     preferencesChanged( section, param );
1243 }
1244
1245 /*!Private SLOT. On open document with name \a aName.*/
1246 void LightApp_Application::onMRUActivated( QString aName )
1247 {
1248   onOpenDoc( aName );
1249 }
1250
1251 /*!Remove all windows from study.*/
1252 void LightApp_Application::beforeCloseDoc( SUIT_Study* s )
1253 {
1254   CAM_Application::beforeCloseDoc( s );
1255
1256   for ( WindowMap::ConstIterator itr = myWindows.begin(); s && itr != myWindows.end(); ++itr )
1257     removeWindow( itr.key(), s->id() );
1258 }
1259
1260 /*!Update actions.*/
1261 void LightApp_Application::updateActions()
1262 {
1263   updateCommandsStatus();
1264 }
1265
1266 //=======================================================================
1267 // name    : createNewStudy
1268 /*! Purpose : Create new study*/
1269 //=======================================================================
1270 SUIT_Study* LightApp_Application::createNewStudy()
1271 {
1272   LightApp_Study* aStudy = new LightApp_Study( this );
1273
1274   // Set up processing of major study-related events
1275   connect( aStudy, SIGNAL( created( SUIT_Study* ) ), this, SLOT( onStudyCreated( SUIT_Study* ) ) );
1276   connect( aStudy, SIGNAL( opened ( SUIT_Study* ) ), this, SLOT( onStudyOpened ( SUIT_Study* ) ) );
1277   connect( aStudy, SIGNAL( saved  ( SUIT_Study* ) ), this, SLOT( onStudySaved  ( SUIT_Study* ) ) );
1278   connect( aStudy, SIGNAL( closed ( SUIT_Study* ) ), this, SLOT( onStudyClosed ( SUIT_Study* ) ) );
1279
1280   return aStudy;
1281 }
1282
1283 /*!Create window.*/
1284 QWidget* LightApp_Application::createWindow( const int flag )
1285 {
1286   QWidget* wid = 0;
1287   if ( flag == WT_ObjectBrowser )
1288   {
1289     OB_Browser* ob = new OB_Browser( desktop() );
1290     ob->setAutoUpdate( true );
1291     ob->setAutoOpenLevel( 1 );
1292     ob->setCaption( tr( "OBJECT_BROWSER" ) );
1293     ob->listView()->setColumnWidth( 0, OBJECT_COLUMN_WIDTH );
1294     ob->resize( OBJECT_BROWSER_WIDTH, ob->height() );
1295     ob->setFilter( new LightApp_OBFilter( selectionMgr() ) );
1296
1297     ob->setNameTitle( tr( "OBJ_BROWSER_NAME" ) );
1298
1299     // Create OBSelector
1300     new LightApp_OBSelector( ob, mySelMgr );
1301
1302     wid = ob;
1303
1304     ob->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1305   }
1306   else  if ( flag == WT_PyConsole )
1307   {
1308     PythonConsole* pyCons = new PythonConsole( desktop() );
1309     pyCons->setCaption( tr( "PYTHON_CONSOLE" ) );
1310     wid = pyCons;
1311     //    pyCons->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1312   }
1313   else if ( flag == WT_LogWindow )
1314   {
1315     LogWindow* logWin = new LogWindow( desktop() );
1316     logWin->setCaption( tr( "LOG_WINDOW" ) );
1317     wid = logWin;
1318     logWin->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
1319   }
1320   return wid;
1321 }
1322
1323 /*!Default windows(Object Browser, Python Console).
1324  * Adds to map \a aMap.
1325  */
1326 void LightApp_Application::defaultWindows( QMap<int, int>& aMap ) const
1327 {  
1328   aMap.insert( WT_ObjectBrowser, Qt::DockLeft );
1329   aMap.insert( WT_PyConsole, Qt::DockBottom );
1330   //  aMap.insert( WT_LogWindow, Qt::DockBottom );
1331 }
1332
1333 /*!Default view manager.*/
1334 void LightApp_Application::defaultViewManagers( QStringList& ) const
1335 {
1336   /*!Do nothing.*/
1337 }
1338
1339 /*!Gets preferences.
1340  * Create preferences, if \a crt = true.
1341  */
1342 LightApp_Preferences* LightApp_Application::preferences( const bool crt ) const
1343 {
1344   if ( myPrefs )
1345     return myPrefs;
1346
1347   LightApp_Application* that = (LightApp_Application*)this;
1348
1349   if ( !_prefs_ && crt )
1350   {
1351     _prefs_ = new LightApp_Preferences( resourceMgr() );
1352     that->createPreferences( _prefs_ );
1353   }
1354
1355   that->myPrefs = _prefs_;
1356
1357   QPtrList<SUIT_Application> appList = SUIT_Session::session()->applications();
1358   for ( QPtrListIterator<SUIT_Application> appIt ( appList ); appIt.current(); ++appIt )
1359   {
1360     if ( !appIt.current()->inherits( "LightApp_Application" ) )
1361       continue;
1362
1363     LightApp_Application* app = (LightApp_Application*)appIt.current();
1364
1365     QStringList modNameList;
1366     app->modules( modNameList, false );
1367     for ( QStringList::const_iterator it = modNameList.begin(); it != modNameList.end(); ++it )
1368     {
1369       int id = _prefs_->addPreference( *it );
1370       _prefs_->setItemProperty( id, "info", tr( "PREFERENCES_NOT_LOADED" ).arg( *it ) );
1371     }
1372
1373     ModuleList modList;
1374     app->modules( modList );
1375     for ( ModuleListIterator itr( modList ); itr.current(); ++itr )
1376     {
1377       LightApp_Module* mod = 0;
1378       if ( itr.current()->inherits( "LightApp_Module" ) )
1379         mod = (LightApp_Module*)itr.current();
1380
1381       if ( mod && !_prefs_->hasModule( mod->moduleName() ) )
1382       {
1383         int modCat = _prefs_->addPreference( mod->moduleName() );
1384         _prefs_->setItemProperty( modCat, "info", QString::null );
1385         mod->createPreferences();
1386       }
1387     }
1388   }
1389
1390   connect( myPrefs, SIGNAL( preferenceChanged( QString&, QString&, QString& ) ),
1391            this, SLOT( onPreferenceChanged( QString&, QString&, QString& ) ) );
1392
1393   return myPrefs;
1394 }
1395
1396 /*!Add new module to application.*/
1397 void LightApp_Application::moduleAdded( CAM_Module* mod )
1398 {
1399   CAM_Application::moduleAdded( mod );
1400
1401   LightApp_Module* lightMod = 0;
1402   if ( mod && mod->inherits( "LightApp_Module" ) )
1403     lightMod = (LightApp_Module*)mod;
1404
1405   if ( myPrefs && lightMod && !myPrefs->hasModule( lightMod->moduleName() ))
1406   {
1407     int modCat = myPrefs->addPreference( mod->moduleName() );
1408     myPrefs->setItemProperty( modCat, "info", QString::null );
1409     lightMod->createPreferences();
1410   }
1411 }
1412
1413 /*!Create preferences.*/
1414 void LightApp_Application::createPreferences( LightApp_Preferences* pref )
1415 {
1416   if ( !pref )
1417     return;
1418
1419   int salomeCat = pref->addPreference( tr( "PREF_CATEGORY_SALOME" ) );
1420
1421   int genTab = pref->addPreference( tr( "PREF_TAB_GENERAL" ), salomeCat );
1422   int studyGroup = pref->addPreference( tr( "PREF_GROUP_STUDY" ), genTab );
1423   pref->setItemProperty( studyGroup, "columns", 1 );
1424
1425   pref->addPreference( tr( "PREF_MULTI_FILE" ), studyGroup, LightApp_Preferences::Bool, "Study", "multi_file" );
1426   pref->addPreference( tr( "PREF_ASCII_FILE" ), studyGroup, LightApp_Preferences::Bool, "Study", "ascii_file" );
1427   int undoPref = pref->addPreference( tr( "PREF_UNDO_LEVEL" ), studyGroup, LightApp_Preferences::IntSpin, "Study", "undo_level" );
1428   pref->setItemProperty( undoPref, "min", 1 );
1429   pref->setItemProperty( undoPref, "max", 100 );
1430
1431   int extgroup = pref->addPreference( tr( "PREF_GROUP_EXT_BROWSER" ), genTab );
1432   pref->setItemProperty( extgroup, "columns", 1 );
1433   int apppref = pref->addPreference( tr( "PREF_APP" ), extgroup, LightApp_Preferences::File, "ExternalBrowser", "application" );
1434   pref->setItemProperty( apppref, "existing", true );
1435   pref->setItemProperty( apppref, "flags", QFileInfo::ExeUser );
1436
1437   pref->addPreference( tr( "PREF_PARAM" ), extgroup, LightApp_Preferences::String, "ExternalBrowser", "parameters" );
1438
1439   int pythonConsoleGroup = pref->addPreference( tr( "PREF_GROUP_PY_CONSOLE" ), genTab );
1440   pref->setItemProperty( pythonConsoleGroup, "columns", 1 );
1441   pref->addPreference( tr( "PREF_FONT" ), pythonConsoleGroup, LightApp_Preferences::Font, "PyConsole", "font" );
1442
1443   int viewTab = pref->addPreference( tr( "PREF_TAB_VIEWERS" ), salomeCat );
1444
1445   int occGroup = pref->addPreference( tr( "PREF_GROUP_OCCVIEWER" ), viewTab );
1446
1447   int vtkGroup = pref->addPreference( tr( "PREF_GROUP_VTKVIEWER" ), viewTab );
1448
1449   int plot2dGroup = pref->addPreference( tr( "PREF_GROUP_PLOT2DVIEWER" ), viewTab );
1450
1451   int supervGroup = pref->addPreference( tr( "PREF_GROUP_SUPERV" ), viewTab );
1452
1453   pref->setItemProperty( occGroup, "columns", 1 );
1454   pref->setItemProperty( vtkGroup, "columns", 1 );
1455   pref->setItemProperty( plot2dGroup, "columns", 1 );
1456
1457   int occTS = pref->addPreference( tr( "PREF_TRIHEDRON_SIZE" ), occGroup,
1458                                    LightApp_Preferences::IntSpin, "OCCViewer", "trihedron_size" );
1459   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), occGroup,
1460                        LightApp_Preferences::Color, "OCCViewer", "background" );
1461
1462   pref->setItemProperty( occTS, "min", 1 );
1463   pref->setItemProperty( occTS, "max", 150 );
1464
1465   int isoU = pref->addPreference( tr( "PREF_ISOS_U" ), occGroup,
1466                                   LightApp_Preferences::IntSpin, "OCCViewer", "iso_number_u" );
1467   int isoV = pref->addPreference( tr( "PREF_ISOS_V" ), occGroup,
1468                                   LightApp_Preferences::IntSpin, "OCCViewer", "iso_number_v" );
1469
1470   pref->setItemProperty( isoU, "min", 0 );
1471   pref->setItemProperty( isoU, "max", 100000 );
1472
1473   pref->setItemProperty( isoV, "min", 0 );
1474   pref->setItemProperty( isoV, "max", 100000 );
1475
1476   int vtkTS = pref->addPreference( tr( "PREF_TRIHEDRON_SIZE" ), vtkGroup,
1477                                    LightApp_Preferences::IntSpin, "VTKViewer", "trihedron_size" );
1478   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), vtkGroup,
1479                        LightApp_Preferences::Color, "VTKViewer", "background" );
1480
1481   pref->setItemProperty( vtkTS, "min", 1 );
1482   pref->setItemProperty( vtkTS, "max", 150 );
1483
1484   pref->addPreference( tr( "PREF_SHOW_LEGEND" ), plot2dGroup,
1485                        LightApp_Preferences::Bool, "Plot2d", "ShowLegend" );
1486
1487   int legendPosition = pref->addPreference( tr( "PREF_LEGEND_POSITION" ), plot2dGroup,
1488                                             LightApp_Preferences::Selector, "Plot2d", "LegendPos" );
1489   QStringList aLegendPosList;
1490   aLegendPosList.append( tr("PREF_LEFT") );
1491   aLegendPosList.append( tr("PREF_RIGHT") );
1492   aLegendPosList.append( tr("PREF_TOP") );
1493   aLegendPosList.append( tr("PREF_BOTTOM") );
1494
1495   QValueList<QVariant> anIndexesList;
1496   anIndexesList.append(0);
1497   anIndexesList.append(1);
1498   anIndexesList.append(2);
1499   anIndexesList.append(3);
1500
1501   pref->setItemProperty( legendPosition, "strings", aLegendPosList );
1502   pref->setItemProperty( legendPosition, "indexes", anIndexesList );
1503
1504   int curveType = pref->addPreference( tr( "PREF_CURVE_TYPE" ), plot2dGroup,
1505                                        LightApp_Preferences::Selector, "Plot2d", "CurveType" );
1506   QStringList aCurveTypesList;
1507   aCurveTypesList.append( tr("PREF_POINTS") );
1508   aCurveTypesList.append( tr("PREF_LINES") );
1509   aCurveTypesList.append( tr("PREF_SPLINE") );
1510
1511   anIndexesList.clear();
1512   anIndexesList.append(0);
1513   anIndexesList.append(1);
1514   anIndexesList.append(2);
1515   
1516   pref->setItemProperty( curveType, "strings", aCurveTypesList );
1517   pref->setItemProperty( curveType, "indexes", anIndexesList );
1518
1519   int markerSize = pref->addPreference( tr( "PREF_MARKER_SIZE" ), plot2dGroup,
1520                                         LightApp_Preferences::IntSpin, "Plot2d", "MarkerSize" );
1521
1522   pref->setItemProperty( markerSize, "min", 0 );
1523   pref->setItemProperty( markerSize, "max", 100 );
1524   
1525   QStringList aScaleModesList;
1526   aScaleModesList.append( tr("PREF_LINEAR") );
1527   aScaleModesList.append( tr("PREF_LOGARITHMIC") );
1528   
1529   anIndexesList.clear();
1530   anIndexesList.append(0);
1531   anIndexesList.append(1);
1532
1533   int horScale = pref->addPreference( tr( "PREF_HOR_AXIS_SCALE" ), plot2dGroup,
1534                                       LightApp_Preferences::Selector, "Plot2d", "HorScaleMode" );
1535
1536   pref->setItemProperty( horScale, "strings", aScaleModesList );
1537   pref->setItemProperty( horScale, "indexes", anIndexesList );
1538
1539   int verScale = pref->addPreference( tr( "PREF_VERT_AXIS_SCALE" ), plot2dGroup,
1540                                       LightApp_Preferences::Selector, "Plot2d", "VerScaleMode" );
1541
1542   pref->setItemProperty( verScale, "strings", aScaleModesList );
1543   pref->setItemProperty( verScale, "indexes", anIndexesList );
1544
1545   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), plot2dGroup,
1546                        LightApp_Preferences::Color, "Plot2d", "Background" );
1547
1548   int dirTab = pref->addPreference( tr( "PREF_TAB_DIRECTORIES" ), salomeCat );
1549   int dirGroup = pref->addPreference( tr( "PREF_GROUP_DIRECTORIES" ), dirTab );
1550   pref->setItemProperty( dirGroup, "columns", 1 );
1551   pref->addPreference( tr( "" ), dirGroup,
1552                        LightApp_Preferences::DirList, "FileDlg", "QuickDirList" );
1553
1554   pref->addPreference( tr( "PREF_VIEWER_BACKGROUND" ), supervGroup,
1555                        LightApp_Preferences::Color, "SUPERVGraph", "Background" );
1556   pref->addPreference( tr( "PREF_SUPERV_TITLE_COLOR" ), supervGroup,
1557                        LightApp_Preferences::Color, "SUPERVGraph", "Title" );
1558 //  pref->addPreference( tr( "PREF_SUPERV_CTRL_COLOR" ), supervGroup,
1559 //                     LightApp_Preferences::Color, "SUPERVGraph", "Ctrl" );
1560 }
1561
1562 /*!Changed preferences */
1563 void LightApp_Application::preferencesChanged( const QString& sec, const QString& param )
1564 {
1565   SUIT_ResourceMgr* resMgr = resourceMgr();
1566   if ( !resMgr )
1567     return;
1568
1569   if ( sec == QString( "OCCViewer" ) && param == QString( "trihedron_size" ) )
1570   {
1571     int sz = resMgr->integerValue( sec, param, -1 );
1572     QPtrList<SUIT_ViewManager> lst;
1573     viewManagers( OCCViewer_Viewer::Type(), lst );
1574     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current() && sz >= 0; ++it )
1575     {
1576       SUIT_ViewModel* vm = it.current()->getViewModel();
1577       if ( !vm || !vm->inherits( "OCCViewer_Viewer" ) )
1578         continue;
1579
1580       OCCViewer_Viewer* occVM = (OCCViewer_Viewer*)vm;
1581       occVM->setTrihedronSize( sz );
1582       occVM->getAISContext()->UpdateCurrentViewer();
1583     }
1584   }
1585
1586   if ( sec == QString( "VTKViewer" ) && param == QString( "trihedron_size" ) )
1587   {
1588     int sz = resMgr->integerValue( sec, param, -1 );
1589     QPtrList<SUIT_ViewManager> lst;
1590     viewManagers( SVTK_Viewer::Type(), lst );
1591     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current() && sz >= 0; ++it )
1592     {
1593       SUIT_ViewModel* vm = it.current()->getViewModel();
1594       if ( !vm || !vm->inherits( "SVTK_Viewer" ) )
1595         continue;
1596
1597       SVTK_Viewer* vtkVM = dynamic_cast<SVTK_Viewer*>( vm );
1598       if( vtkVM )
1599       {
1600         vtkVM->setTrihedronSize( sz );
1601         vtkVM->Repaint();
1602       }
1603     }
1604   }
1605
1606   if ( sec == QString( "OCCViewer" ) && ( param == QString( "iso_number_u" ) || param == QString( "iso_number_v" ) ) )
1607   {
1608     QPtrList<SUIT_ViewManager> lst;
1609     viewManagers( OCCViewer_Viewer::Type(), lst );
1610     int u = resMgr->integerValue( sec, "iso_number_u" );
1611     int v = resMgr->integerValue( sec, "iso_number_v" );
1612     for ( QPtrListIterator<SUIT_ViewManager> it( lst ); it.current(); ++it )
1613       ((OCCViewer_Viewer*)it.current())->setIsos( u, v );
1614   }
1615
1616   if( sec=="ObjectBrowser" )
1617   {
1618     if( param=="auto_size" )
1619     {
1620       OB_Browser* ob = objectBrowser();
1621       if( !ob )
1622         return;
1623
1624       bool autoSize = resMgr->booleanValue( "ObjectBrowser", "auto_size", false );
1625       ob->setWidthMode( autoSize ? QListView::Maximum : QListView::Manual );
1626
1627       updateObjectBrowser( false );
1628     }
1629   }
1630
1631   if( sec=="PyConsole" )
1632   {
1633     if( param=="font" )
1634       if( pythonConsole() )
1635         pythonConsole()->setFont( resMgr->fontValue( "PyConsole", "font" ) );
1636   }
1637 }
1638
1639 /*!Update desktop title.*/
1640 void LightApp_Application::updateDesktopTitle() {
1641   QString aTitle = applicationName();
1642   QString aVer = applicationVersion();
1643   if ( !aVer.isEmpty() )
1644     aTitle += QString( " " ) + aVer;
1645
1646   desktop()->setCaption( aTitle );
1647 }
1648
1649 /*!Update windows after close document.*/
1650 void LightApp_Application::afterCloseDoc()
1651 {
1652   updateWindows();
1653
1654   CAM_Application::afterCloseDoc();
1655 }
1656
1657 /*!Update module action.*/
1658 void LightApp_Application::updateModuleActions()
1659 {
1660   QString modName;
1661   if ( activeModule() )
1662     modName = activeModule()->moduleName();
1663
1664   if ( myActions.contains( modName ) )
1665     myActions[modName]->setOn( true );
1666 }
1667
1668 /*!Gets current windows.
1669  *\param winMap - output current windows map.
1670  */
1671 void LightApp_Application::currentWindows( QMap<int, int>& winMap ) const
1672 {
1673   winMap.clear();
1674   if ( !activeStudy() )
1675     return;
1676
1677   if ( activeModule() && activeModule()->inherits( "LightApp_Module" ) )
1678     ((LightApp_Module*)activeModule())->windows( winMap );
1679   else
1680     defaultWindows( winMap );
1681 }
1682
1683 /*!Gets current view managers.
1684  *\param lst - output current view managers list.
1685  */
1686 void LightApp_Application::currentViewManagers( QStringList& lst ) const
1687 {
1688   lst.clear();
1689   if ( !activeStudy() )
1690     return;
1691
1692   if ( activeModule() && activeModule()->inherits( "LightApp_Module" ) )
1693     ((LightApp_Module*)activeModule())->viewManagers( lst );
1694   else
1695     defaultViewManagers( lst );
1696 }
1697
1698 /*!Update windows.*/
1699 void LightApp_Application::updateWindows()
1700 {
1701   QMap<int, int> winMap;
1702   currentWindows( winMap );
1703
1704   for ( QMap<int, int>::ConstIterator it = winMap.begin(); it != winMap.end(); ++it )
1705     getWindow( it.key() );
1706
1707   loadWindowsGeometry();
1708
1709   for ( WindowMap::ConstIterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1710     setWindowShown( itr.key(), !itr.data()->isEmpty() && winMap.contains( itr.key() ) );
1711 }
1712
1713 /*!Update view managers.*/
1714 void LightApp_Application::updateViewManagers()
1715 {
1716   QStringList lst;
1717   currentViewManagers( lst );
1718
1719   for ( QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it )
1720     getViewManager( *it, true );
1721 }
1722
1723 /*!Load windows geometry.*/
1724 void LightApp_Application::loadWindowsGeometry()
1725 {
1726   QtxDockAction* dockMgr = 0;
1727
1728   QAction* a = action( ViewWindowsId );
1729   if ( a && a->inherits( "QtxDockAction" ) )
1730     dockMgr = (QtxDockAction*)a;
1731
1732   if ( !dockMgr )
1733     return;
1734
1735   QString modName;
1736   if ( activeModule() )
1737     modName = activeModule()->name("");
1738
1739   QString section = QString( "windows_geometry" );
1740   if ( !modName.isEmpty() )
1741     section += QString( "." ) + modName;
1742
1743   dockMgr->loadGeometry( resourceMgr(), section, false );
1744   dockMgr->restoreGeometry();
1745 }
1746
1747 /*!Save windows geometry.*/
1748 void LightApp_Application::saveWindowsGeometry()
1749 {
1750   QtxDockAction* dockMgr = 0;
1751
1752   QAction* a = action( ViewWindowsId );
1753   if ( a && a->inherits( "QtxDockAction" ) )
1754     dockMgr = (QtxDockAction*)a;
1755
1756   if ( !dockMgr )
1757     return;
1758
1759   QString modName;
1760   if ( activeModule() )
1761     modName = activeModule()->name("");
1762
1763   QString section = QString( "windows_geometry" );
1764   if ( !modName.isEmpty() )
1765     section += QString( "." ) + modName;
1766
1767   dockMgr->storeGeometry();
1768   dockMgr->saveGeometry( resourceMgr(), section, false );
1769 }
1770
1771 /*!Activate windows.*/
1772 void LightApp_Application::activateWindows()
1773 {
1774   if ( activeStudy() )
1775   {
1776     for ( WindowMap::Iterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1777       itr.data()->activate( activeStudy()->id() );
1778   }
1779 }
1780
1781 /*!Adds icon names for modules.*/
1782 void LightApp_Application::moduleIconNames( QMap<QString, QString>& iconMap ) const
1783 {
1784   iconMap.clear();
1785
1786   SUIT_ResourceMgr* resMgr = resourceMgr();
1787   if ( !resMgr )
1788     return;
1789
1790   QStringList modList;
1791   modules( modList, false );
1792
1793   for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
1794   {
1795     QString modName = *it;
1796     QString modIntr = moduleName( modName );
1797     QString modIcon = resMgr->stringValue( modIntr, "icon", QString::null );
1798
1799     if ( modIcon.isEmpty() )
1800       continue;
1801
1802     if ( SUIT_Tools::extension( modIcon ).isEmpty() )
1803       modIcon += QString( ".png" );
1804
1805     iconMap.insert( modName, modIcon );
1806   }
1807 }
1808
1809 /*!Insert items in popup, which necessary for current application*/
1810 void LightApp_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
1811 {
1812   CAM_Application::contextMenuPopup( type, thePopup, title );
1813
1814   OB_Browser* ob = objectBrowser();
1815   if ( !ob || type != ob->popupClientType() )
1816     return;
1817
1818   thePopup->insertSeparator();
1819   thePopup->insertItem( tr( "MEN_REFRESH" ), this, SLOT( onRefresh() ) );
1820 }
1821
1822 /*!Create empty study.*/
1823 void LightApp_Application::createEmptyStudy()
1824 {
1825   CAM_Application::createEmptyStudy();
1826   if ( objectBrowser() )
1827     objectBrowser()->updateTree();
1828 }
1829
1830 /*!Activate module \a mod.*/
1831 bool LightApp_Application::activateModule( CAM_Module* mod )
1832 {
1833   bool res = CAM_Application::activateModule( mod );
1834   if ( objectBrowser() )
1835     objectBrowser()->updateTree();
1836   return res;
1837 }
1838
1839 /*!return keyborad accelerators manager object */
1840 SUIT_Accel* LightApp_Application::accel() const
1841 {
1842   return myAccel;
1843 }
1844
1845 /*! remove dead widget container from map */
1846 void LightApp_Application::onWCDestroyed( QObject* ob )
1847 {
1848   // remove destroyed widget container from windows map
1849   for ( WindowMap::ConstIterator itr = myWindows.begin(); itr != myWindows.end(); ++itr )
1850   {
1851     if ( itr.data() != ob )
1852       continue;
1853
1854     int key = itr.key();
1855     myWindows.remove( key );
1856     break;
1857   }
1858 }
1859
1860 /*! redefined to remove view manager from memory */
1861 void LightApp_Application::removeViewManager( SUIT_ViewManager* vm )
1862 {
1863   disconnect( vm, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
1864            this, SLOT( onCloseView( SUIT_ViewManager* ) ) );
1865   STD_Application::removeViewManager( vm );
1866   delete vm;
1867 }