Salome HOME
1) Fix a bug of Selection object in PyQt modules - create the only instance of Select...
[modules/gui.git] / src / SALOME_PYQT / SalomePyQt / SalomePyQt.cxx
1 //=============================================================================
2 // File      : SalomePyQt.cxx
3 // Created   : 25/04/05
4 // Author    : Vadim SANDLER
5 // Project   : SALOME
6 // Copyright : 2003-2005 CEA/DEN, EDF R&D
7 // $Header   : $
8 //=============================================================================
9
10 #include "SALOME_PYQT_Module.h" // this include must be first!!!
11 #include "SalomePyQt.h"
12
13 #include <qapplication.h>
14 #include <qmenubar.h>
15 #include <qwidget.h>
16 #include <qpopupmenu.h>
17 #include <qstringlist.h>
18
19 #include "SALOME_Event.hxx"
20
21 #include "SUIT_Session.h"
22 #include "SUIT_Desktop.h"
23 #include "SUIT_ResourceMgr.h"
24 #include "SUIT_Tools.h"
25 #include "STD_MDIDesktop.h"
26 #include "SalomeApp_Application.h"
27 #include "SalomeApp_Study.h"
28 #include "SalomeApp_SelectionMgr.h"
29 #include "OB_Browser.h"
30 #include "QtxAction.h"
31
32 using namespace std;
33
34 //====================================================================================
35 // static functions
36 //====================================================================================
37 /*!
38   getApplication()
39   Returns active application object [ static ]
40 */
41 static SalomeApp_Application* getApplication() {
42   if ( SUIT_Session::session() )
43     return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
44   return NULL;
45 }
46
47 /*!
48   getActiveStudy()
49   Gets active study or 0 if there is no study opened [ static ]
50 */
51 static SalomeApp_Study* getActiveStudy()
52 {
53   if ( getApplication() )
54     return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
55   return 0;
56 }
57
58 //====================================================================================
59 // SALOME_Selection class.
60 //====================================================================================
61 static QMap<SalomeApp_Application*, SALOME_Selection*> SelMap;
62
63 /*!
64   SALOME_Selection::GetSelection
65   Creates or finds the selection object (one per study).
66 */
67 SALOME_Selection* SALOME_Selection::GetSelection( SalomeApp_Application* app )
68 {
69   SALOME_Selection* sel = 0;
70   if ( app && SelMap.find( app ) != SelMap.end() )
71     sel = SelMap[ app ];
72   else 
73     sel = SelMap[ app ] = new SALOME_Selection( app );
74   return sel;
75 }
76
77 /*!
78   SALOME_Selection::SALOME_Selection
79   Selection constructor.
80 */
81 SALOME_Selection::SALOME_Selection( QObject* p ) : QObject( p ), mySelMgr( 0 )
82 {
83   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( p );
84   if ( app ) {
85     mySelMgr = app->selectionMgr();
86     connect( mySelMgr, SIGNAL( selectionChanged() ), this, SIGNAL( currentSelectionChanged() ) );
87     connect( mySelMgr, SIGNAL( destroyed() ),        this, SLOT  ( onSelMgrDestroyed() ) );
88   }
89 }
90 /*!
91   SALOME_Selection::~SALOME_Selection
92   Selection destructor. Removes selection object from the map.
93 */
94 SALOME_Selection::~SALOME_Selection()
95 {
96   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( parent() );
97   if ( app && SelMap.find( app ) != SelMap.end() )
98     SelMap.remove( app );
99 }
100
101 /*!
102   SALOME_Selection::onSelMgrDestroyed
103   Watches for the selection manager destroying when study is closed.
104 */
105 void SALOME_Selection::onSelMgrDestroyed()
106 {
107   mySelMgr = 0;
108 }
109
110 /*!
111   SALOME_Selection::Clear
112   Clears the selection.
113 */
114 void SALOME_Selection::Clear()
115 {
116   class TEvent: public SALOME_Event {
117     SalomeApp_SelectionMgr* mySelMgr;
118   public:
119     TEvent( SalomeApp_SelectionMgr* selMgr ) 
120       : mySelMgr( selMgr ) {}
121     virtual void Execute() {
122       if ( mySelMgr )
123         mySelMgr->clearSelected();
124     }
125   };
126   ProcessVoidEvent( new TEvent( mySelMgr ) );
127 }
128
129 /*!
130   SALOME_Selection::ClearIObjects
131   Clears the selection.
132 */
133 void SALOME_Selection::ClearIObjects()
134 {
135   Clear();
136 }
137
138 /*!
139   SALOME_Selection::ClearFilters
140   Removes all selection filters.
141 */
142 void SALOME_Selection::ClearFilters()
143 {
144   class TEvent: public SALOME_Event {
145     SalomeApp_SelectionMgr* mySelMgr;
146   public:
147     TEvent( SalomeApp_SelectionMgr* selMgr ) 
148       : mySelMgr( selMgr ) {}
149     virtual void Execute() {
150       if ( mySelMgr )
151         mySelMgr->clearFilters();
152     }
153   };
154   ProcessVoidEvent( new TEvent( mySelMgr ) );
155 }
156
157 //====================================================================================
158 // SalomePyQt class
159 //====================================================================================
160
161 /*!
162   SalomePyQt::getDesktop
163   Gets desktop. Returns 0 in error.
164 */
165 class TGetDesktopEvent: public SALOME_Event {
166 public:
167   typedef QWidget* TResult;
168   TResult myResult;
169   TGetDesktopEvent() : myResult( 0 ) {}
170   virtual void Execute() {
171     if ( getApplication() )
172       myResult = (QWidget*)( getApplication()->desktop() );
173   }
174 };
175 QWidget* SalomePyQt::getDesktop()
176 {
177   return ProcessEvent( new TGetDesktopEvent() );
178 }
179
180 /*!
181   SalomePyQt::getMainFrame
182   Gets workspace widget. Returns 0 in error.
183 */
184 class TGetMainFrameEvent: public SALOME_Event {
185 public:
186   typedef QWidget* TResult;
187   TResult myResult;
188   TGetMainFrameEvent() : myResult( 0 ) {}
189   virtual void Execute() {
190     if ( getApplication() ) {
191       SUIT_Desktop* aDesktop = getApplication()->desktop();
192       myResult = (QWidget*)( aDesktop->centralWidget() );
193     }
194   }
195 };
196 QWidget* SalomePyQt::getMainFrame()
197 {
198   return ProcessEvent( new TGetMainFrameEvent() );
199 }
200
201 /*!
202   SalomePyQt::getMainMenuBar
203   Gets main menu. Returns 0 in error.
204 */
205 class TGetMainMenuBarEvent: public SALOME_Event {
206 public:
207   typedef QMenuBar* TResult;
208   TResult myResult;
209   TGetMainMenuBarEvent() : myResult( 0 ) {}
210   virtual void Execute() {
211     if ( SalomeApp_Application* anApp = getApplication() ) {
212       myResult = anApp->desktop()->menuBar();
213     }
214   }
215 };
216 QMenuBar* SalomePyQt::getMainMenuBar() 
217 {
218   return ProcessEvent( new TGetMainMenuBarEvent() );
219 }
220
221 /*!
222   SalomePyQt::getPopupMenu
223   Gets an main menu's child popup menu by its id
224 */
225 class TGetPopupMenuEvent: public SALOME_Event {
226 public:
227   typedef QPopupMenu* TResult;
228   TResult  myResult;
229   MenuName myMenuName;
230   TGetPopupMenuEvent( const MenuName menu ) : myResult( 0 ), myMenuName( menu ) {}
231   virtual void Execute() {
232     if ( SalomeApp_Application* anApp = getApplication() ) {
233       QMenuBar* menuBar = anApp->desktop()->menuBar();
234       if ( menuBar ) {
235         QString menu;
236         switch( myMenuName) {
237         case File:
238           menu = QObject::tr( "MEN_DESK_FILE" );        break;
239         case View:
240           menu = QObject::tr( "MEN_DESK_VIEW" );        break;
241         case Edit:
242           menu = QObject::tr( "MEN_DESK_EDIT" );        break;
243         case Preferences:
244           menu = QObject::tr( "MEN_DESK_PREFERENCES" ); break;
245         case Tools:
246           menu = QObject::tr( "MEN_DESK_TOOLS" );       break;
247         case Window:
248           menu = QObject::tr( "MEN_DESK_WINDOW" );      break;
249         case Help:
250           menu = QObject::tr( "MEN_DESK_HELP" );        break;
251         }
252         for ( int i = 0; i < menuBar->count() && !myResult; i++ ) {
253           QMenuItem* item = menuBar->findItem( menuBar->idAt( i ) );
254           if ( item && item->text() == menu && item->popup() )
255             myResult = item->popup();
256         }
257       }
258     }
259   }
260 };
261 QPopupMenu* SalomePyQt::getPopupMenu( const MenuName menu )
262 {
263   return ProcessEvent( new TGetPopupMenuEvent( menu ) );
264 }
265
266 /*!
267   SalomePyQt::getStudyId
268   Returns active study's ID or 0 if there is no active study.
269 */
270 class TGetStudyIdEvent: public SALOME_Event {
271 public:
272   typedef int TResult;
273   TResult myResult;
274   TGetStudyIdEvent() : myResult( 0 ) {}
275   virtual void Execute() {
276     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
277       myResult = aStudy->studyDS()->StudyId();
278     }
279   }
280 };
281 int SalomePyQt::getStudyId()
282 {
283   return ProcessEvent( new TGetStudyIdEvent() );
284 }
285
286 /*!
287   SalomePyQt::getSelection
288   Creates a Selection object (to provide a compatibility with previous SALOME GUI).
289 */
290 class TGetSelectionEvent: public SALOME_Event {
291 public:
292   typedef SALOME_Selection* TResult;
293   TResult myResult;
294   TGetSelectionEvent() : myResult( 0 ) {}
295   virtual void Execute() {
296     myResult = SALOME_Selection::GetSelection( getApplication() );
297   }
298 };
299 SALOME_Selection* SalomePyQt::getSelection()
300 {
301   return ProcessEvent( new TGetSelectionEvent() );
302 }
303
304 /*!
305   SalomePyQt::putInfo
306   Puts an information message to the desktop's status bar
307   (with optional delay parameter given in seconds)
308 */
309 class TPutInfoEvent: public SALOME_Event {
310   QString myMsg;
311   int     mySecs;
312 public:
313   TPutInfoEvent( const QString& msg, const int sec = 0 ) : myMsg( msg ), mySecs( sec ) {}
314   virtual void Execute() {
315     if ( SalomeApp_Application* anApp = getApplication() ) {
316       anApp->putInfo( myMsg, mySecs * 1000 );
317     }
318   }
319 };
320 void SalomePyQt::putInfo( const QString& msg, const int sec )
321 {
322   ProcessVoidEvent( new TPutInfoEvent( msg, sec ) );
323 }
324
325 /*!
326   SalomePyQt::getActiveComponent
327   Returns an active component name or empty string if there is no active component
328 */
329 class TGetActiveComponentEvent: public SALOME_Event {
330 public:
331   typedef QString TResult;
332   TResult myResult;
333   TGetActiveComponentEvent() {}
334   virtual void Execute() {
335     if ( SalomeApp_Application* anApp = getApplication() ) {
336       if ( CAM_Module* mod = anApp->activeModule() ) {
337         myResult = mod->name("");
338       }
339     }
340   }
341 };
342 const QString SalomePyQt::getActiveComponent()
343 {
344   return ProcessEvent( new TGetActiveComponentEvent() );
345 }
346
347 /*!
348   SalomePyQt::updateObjBrowser
349   Updates an Object Browser of a given study.
350   If <studyId> <= 0 the active study's object browser is updated.
351   <updateSelection> parameter is obsolete parameter and currently not used. To be removed lately.
352 */
353 void SalomePyQt::updateObjBrowser( const int studyId, bool updateSelection )
354 {  
355   class TEvent: public SALOME_Event {
356     int  myStudyId;
357     bool myUpdateSelection;
358   public:
359     TEvent( const int studyId, bool updateSelection ) 
360       : myStudyId( studyId ), myUpdateSelection( updateSelection ) {}
361     virtual void Execute() {
362       if ( SUIT_Session::session() ) {
363         if ( getActiveStudy() && myStudyId <= 0 )
364           myStudyId = getActiveStudy()->id();
365         if ( myStudyId > 0 ) {
366           QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
367           QPtrListIterator<SUIT_Application> it( apps );
368           for( ; it.current(); ++it ) {
369             SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( it.current() );
370             if ( anApp && anApp->activeStudy() && anApp->activeStudy()->id() == myStudyId )
371               anApp->updateObjectBrowser();
372           }
373         }
374       }
375     }
376   };
377   ProcessVoidEvent( new TEvent( studyId, updateSelection ) );
378 }
379
380 const char* DEFAULT_SECTION = "SalomePyQt";
381
382 /*!
383   SalomePyQt::addStringSetting
384   Adds a string setting to the application preferences
385   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
386   This function is obsolete. Use addSetting() instead.
387 */
388 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
389 {
390   class TEvent: public SALOME_Event {
391     QString myName;
392     QString myValue;
393     bool    myAutoValue;
394   public:
395     TEvent( const QString& name, const QString& value, bool autoValue ) 
396       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
397     virtual void Execute() {
398       if ( SUIT_Session::session() ) {
399         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
400         QStringList sl = QStringList::split( ":", myName );
401         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
402         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
403         if ( !_sec.isEmpty() && !_nam.isEmpty() )
404           resMgr->setValue( _sec, _nam, myValue );
405       }
406     }
407   };
408   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
409 }
410
411 /*!
412   SalomePyQt::addIntSetting
413   Adds an integer setting to the application preferences
414   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
415   This function is obsolete. Use addSetting() instead.
416 */
417 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
418 {
419   class TEvent: public SALOME_Event {
420     QString myName;
421     int     myValue;
422     bool    myAutoValue;
423   public:
424     TEvent( const QString& name, const int value, bool autoValue ) 
425       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
426     virtual void Execute() {
427       if ( SUIT_Session::session() ) {
428         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
429         QStringList sl = QStringList::split( ":", myName );
430         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
431         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
432         if ( !_sec.isEmpty() && !_nam.isEmpty() )
433           resMgr->setValue( _sec, _nam, myValue );
434       }
435     }
436   };
437   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
438 }
439
440 /*!
441   SalomePyQt::addDoubleSetting
442   Adds an double setting to the application preferences
443   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
444   This function is obsolete. Use addSetting() instead.
445 */
446 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
447 {
448   class TEvent: public SALOME_Event {
449     QString myName;
450     double  myValue;
451     bool    myAutoValue;
452   public:
453     TEvent( const QString& name, const double value, bool autoValue ) 
454       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
455     virtual void Execute() {
456       if ( SUIT_Session::session() ) {
457         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
458         QStringList sl = QStringList::split( ":", myName );
459         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
460         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
461         if ( !_sec.isEmpty() && !_nam.isEmpty() )
462           resMgr->setValue( _sec, _nam, myValue );
463       }
464     }
465   };
466   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
467 }
468
469 /*!
470   SalomePyQt::removeSettings
471   Removes a setting from the application preferences
472   This function is obsolete. Use removeSetting() instead.
473 */
474 void SalomePyQt::removeSettings( const QString& name )
475 {
476   class TEvent: public SALOME_Event {
477     QString myName;
478   public:
479     TEvent( const QString& name ) : myName( name ) {}
480     virtual void Execute() {
481       if ( SUIT_Session::session() ) {
482         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
483         QStringList sl = QStringList::split( ":", myName );
484         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
485         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
486         if ( !_sec.isEmpty() && !_nam.isEmpty() )
487           resMgr->remove( _sec, _nam );
488       }
489     }
490   };
491   ProcessVoidEvent( new TEvent( name ) );
492 }
493
494 /*!
495   SalomePyQt::getSetting
496   Gets a setting value (as string)
497   This function is obsolete. Use stringSetting(), integerSetting(), 
498   boolSetting(), stringSetting() or colorSetting() instead.
499 */
500 class TGetSettingEvent: public SALOME_Event {
501 public:
502   typedef QString TResult;
503   TResult myResult;
504   QString myName;
505   TGetSettingEvent( const QString& name ) : myName( name ) {}
506   virtual void Execute() {
507     if ( SUIT_Session::session() ) {
508       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
509       QStringList sl = QStringList::split( ":", myName );
510       QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
511       QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
512       myResult = ( !_sec.isEmpty() && !_nam.isEmpty() ) ? resMgr->stringValue( _sec, _nam, "" ) : QString( "" );
513     }
514   }
515 };
516 QString SalomePyQt::getSetting( const QString& name )
517 {
518   return ProcessEvent( new TGetSettingEvent( name ) );
519 }
520
521 /*!
522   SalomePyQt::addSetting
523   Adds a double setting to the application preferences
524 */
525 void SalomePyQt::addSetting( const QString& section, const QString& name, const double value )
526 {
527   class TEvent: public SALOME_Event {
528     QString mySection;
529     QString myName;
530     double  myValue;
531   public:
532     TEvent( const QString& section, const QString& name, double value ) 
533       : mySection( section ), myName( name ), myValue( value ) {}
534     virtual void Execute() {
535       if ( SUIT_Session::session() ) {
536         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
537         if ( !mySection.isEmpty() && !myName.isEmpty() )
538           resMgr->setValue( mySection, myName, myValue );
539       }
540     }
541   };
542   ProcessVoidEvent( new TEvent( section, name, value ) );
543 }
544
545 /*!
546   SalomePyQt::addSetting
547   Adds an integer setting to the application preferences
548 */
549 void SalomePyQt::addSetting( const QString& section, const QString& name, const int value )
550 {
551   class TEvent: public SALOME_Event {
552     QString mySection;
553     QString myName;
554     int     myValue;
555   public:
556     TEvent( const QString& section, const QString& name, int value ) 
557       : mySection( section ), myName( name ), myValue( value ) {}
558     virtual void Execute() {
559       if ( SUIT_Session::session() ) {
560         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
561         if ( !mySection.isEmpty() && !myName.isEmpty() )
562           resMgr->setValue( mySection, myName, myValue );
563       }
564     }
565   };
566   ProcessVoidEvent( new TEvent( section, name, value ) );
567 }
568
569 /*!
570   SalomePyQt::addSetting
571   Adds a string setting to the application preferences
572 */
573 void SalomePyQt::addSetting( const QString& section, const QString& name, const QString& value )
574 {
575   class TEvent: public SALOME_Event {
576     QString mySection;
577     QString myName;
578     QString myValue;
579   public:
580     TEvent( const QString& section, const QString& name, const QString& value ) 
581       : mySection( section ), myName( name ), myValue( value ) {}
582     virtual void Execute() {
583       if ( SUIT_Session::session() ) {
584         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
585         if ( !mySection.isEmpty() && !myName.isEmpty() )
586           resMgr->setValue( mySection, myName, myValue );
587       }
588     }
589   };
590   ProcessVoidEvent( new TEvent( section, name, value ) );
591 }
592
593 /*!
594   SalomePyQt::addSetting
595   Adds a color setting to the application preferences
596 */
597 void SalomePyQt::addSetting( const QString& section, const QString& name, const QColor& value )
598 {
599   class TEvent: public SALOME_Event {
600     QString mySection;
601     QString myName;
602     QColor  myValue;
603   public:
604     TEvent( const QString& section, const QString& name, const QColor& value ) 
605       : mySection( section ), myName( name ), myValue( value ) {}
606     virtual void Execute() {
607       if ( SUIT_Session::session() ) {
608         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
609         if ( !mySection.isEmpty() && !myName.isEmpty() )
610           resMgr->setValue( mySection, myName, myValue );
611       }
612     }
613   };
614   ProcessVoidEvent( new TEvent( section, name, value ) );
615 }
616
617 /*!
618   SalomePyQt::integerSetting
619   Gets an integer setting from the application preferences
620 */
621 class TGetIntSettingEvent: public SALOME_Event {
622 public:
623   typedef int TResult;
624   TResult myResult;
625   QString mySection;
626   QString myName;
627   TResult myDefault;
628   TGetIntSettingEvent( const QString& section, const QString& name, const int def ) 
629     : mySection( section ), myName( name ), myDefault( def ) {}
630   virtual void Execute() {
631     if ( SUIT_Session::session() ) {
632       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
633       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->integerValue( mySection, myName, myDefault ) : myDefault;
634     }
635   }
636 };
637 int SalomePyQt::integerSetting( const QString& section, const QString& name, const int def )
638 {
639   return ProcessEvent( new TGetIntSettingEvent( section, name, def ) );
640 }
641
642 /*!
643   SalomePyQt::doubleSetting
644   Gets a double setting from the application preferences
645 */
646 class TGetDblSettingEvent: public SALOME_Event {
647 public:
648   typedef double TResult;
649   TResult myResult;
650   QString mySection;
651   QString myName;
652   TResult myDefault;
653   TGetDblSettingEvent( const QString& section, const QString& name, const double def ) 
654     : mySection( section ), myName( name ), myDefault( def ) {}
655   virtual void Execute() {
656     if ( SUIT_Session::session() ) {
657       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
658       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->doubleValue( mySection, myName, myDefault ) : myDefault;
659     }
660   }
661 };
662 double SalomePyQt::doubleSetting( const QString& section, const QString& name, const int def )
663 {
664   return ProcessEvent( new TGetDblSettingEvent( section, name, def ) );
665 }
666
667 /*!
668   SalomePyQt::boolSetting
669   Gets a boolean setting from the application preferences
670 */
671 class TGetBoolSettingEvent: public SALOME_Event {
672 public:
673   typedef bool TResult;
674   TResult myResult;
675   QString mySection;
676   QString myName;
677   TResult myDefault;
678   TGetBoolSettingEvent( const QString& section, const QString& name, const bool def ) 
679     : mySection( section ), myName( name ), myDefault( def ) {}
680   virtual void Execute() {
681     if ( SUIT_Session::session() ) {
682       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
683       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->booleanValue( mySection, myName, myDefault ) : myDefault;
684     }
685   }
686 };
687 bool SalomePyQt::boolSetting( const QString& section, const QString& name, const bool def )
688 {
689   return ProcessEvent( new TGetBoolSettingEvent( section, name, def ) );
690 }
691
692 /*!
693   SalomePyQt::stringSetting
694   Gets a string setting from the application preferences
695 */
696 class TGetStrSettingEvent: public SALOME_Event {
697 public:
698   typedef QString TResult;
699   TResult myResult;
700   QString mySection;
701   QString myName;
702   TResult myDefault;
703   TGetStrSettingEvent( const QString& section, const QString& name, const QString& def ) 
704     : mySection( section ), myName( name ), myDefault( def ) {}
705   virtual void Execute() {
706     if ( SUIT_Session::session() ) {
707       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
708       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault ) : myDefault;
709     }
710   }
711 };
712 QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def )
713 {
714   return ProcessEvent( new TGetStrSettingEvent( section, name, def ) );
715 }
716
717 /*!
718   SalomePyQt::colorSetting
719   Gets a color setting from the application preferences
720 */
721 class TGetColorSettingEvent: public SALOME_Event {
722 public:
723   typedef QColor TResult;
724   TResult myResult;
725   QString mySection;
726   QString myName;
727   TResult myDefault;
728   TGetColorSettingEvent( const QString& section, const QString& name, const QColor& def ) 
729     : mySection( section ), myName( name ), myDefault( def ) {}
730   virtual void Execute() {
731     if ( SUIT_Session::session() ) {
732       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
733       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->colorValue( mySection, myName, myDefault ) : myDefault;
734     }
735   }
736 };
737 QColor SalomePyQt::colorSetting ( const QString& section, const QString& name, const QColor& def )
738 {
739   return ProcessEvent( new TGetColorSettingEvent( section, name, def ) );
740 }
741
742 /*!
743   SalomePyQt::removeSetting
744   Removes a setting from the application preferences
745 */
746 void SalomePyQt::removeSetting( const QString& section, const QString& name )
747 {
748   class TEvent: public SALOME_Event {
749     QString mySection;
750     QString myName;
751   public:
752     TEvent( const QString& section, const QString& name ) : mySection( section ), myName( name ) {}
753     virtual void Execute() {
754       if ( SUIT_Session::session() ) {
755         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
756         if ( !mySection.isEmpty() && !myName.isEmpty() )
757           resMgr->remove( mySection, myName );
758       }
759     }
760   };
761   ProcessVoidEvent( new TEvent( section, name ) );
762 }
763
764 /*!
765   SalomePyQt::getFileName
766   Displays 'Open/Save file' dialog box and returns a user's choice (file name)
767 */
768 class TGetFileNameEvent: public SALOME_Event {
769 public:
770   typedef QString TResult;
771   TResult     myResult;
772   QWidget*    myParent;
773   QString     myInitial;
774   QStringList myFilters;
775   QString     myCaption;
776   bool        myOpen;
777   TGetFileNameEvent( QWidget*           parent, 
778                      const QString&     initial, 
779                      const QStringList& filters, 
780                      const QString&     caption,
781                      bool               open ) 
782     : myParent ( parent ), 
783       myInitial( initial ), 
784       myFilters( filters ), 
785       myCaption( caption ), 
786       myOpen ( open ) {}
787   virtual void Execute() {
788     if ( SalomeApp_Application* anApp = getApplication() ) {
789       myResult = anApp->getFileName( myOpen, myInitial, myFilters.join(";;"), myCaption, myParent );
790     }
791   }
792 };
793 QString SalomePyQt::getFileName( QWidget*           parent, 
794                                  const QString&     initial, 
795                                  const QStringList& filters, 
796                                  const QString&     caption,
797                                  bool               open )
798 {
799   return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
800 }
801
802 /*!
803   SalomePyQt::getOpenFileNames
804   Displays 'Open files' dialog box and returns a user's choice (a list of file names)
805 */
806 class TGetOpenFileNamesEvent: public SALOME_Event {
807 public:
808   typedef QStringList TResult;
809   TResult     myResult;
810   QWidget*    myParent;
811   QString     myInitial;
812   QStringList myFilters;
813   QString     myCaption;
814   TGetOpenFileNamesEvent( QWidget*           parent, 
815                           const QString&     initial, 
816                           const QStringList& filters, 
817                           const QString&     caption ) 
818     : myParent ( parent ), 
819       myInitial( initial ), 
820       myFilters( filters ), 
821       myCaption( caption ) {}
822   virtual void Execute() {
823     if ( SalomeApp_Application* anApp = getApplication() ) {
824       myResult = anApp->getOpenFileNames( myInitial, myFilters.join(";;"), myCaption, myParent );
825     }
826   }
827 };
828 QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
829                                           const QString&     initial, 
830                                           const QStringList& filters, 
831                                           const QString&     caption )
832 {
833   return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
834 }
835
836 /*!
837   SalomePyQt::getExistingDirectory
838   Displays 'Get Directory' dialog box and returns a user's choice (a directory name)
839 */
840 class TGetExistingDirectoryEvent: public SALOME_Event {
841 public:
842   typedef QString TResult;
843   TResult     myResult;
844   QWidget*    myParent;
845   QString     myInitial;
846   QString     myCaption;
847   TGetExistingDirectoryEvent( QWidget*           parent, 
848                               const QString&     initial, 
849                               const QString&     caption ) 
850     : myParent ( parent ), 
851       myInitial( initial ), 
852       myCaption( caption ) {}
853   virtual void Execute() {
854     if ( SalomeApp_Application* anApp = getApplication() ) {
855       myResult = anApp->getDirectory( myInitial, myCaption, myParent );
856     }
857   }
858 };
859 QString SalomePyQt::getExistingDirectory( QWidget*       parent,
860                                           const QString& initial,
861                                           const QString& caption )
862 {
863   return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
864 }
865
866 /*!
867   SalomePyQt::helpContext
868   Opens external browser to display 'context help' information
869   current implementation does nothing.
870 */
871 void SalomePyQt::helpContext( const QString& source, const QString& context ) {
872   class TEvent: public SALOME_Event {
873     QString mySource;
874     QString myContext;
875   public:
876     TEvent( const QString& source, const QString& context ) 
877       : mySource( source ), myContext( context ) {}
878     virtual void Execute() {
879       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
880         // VSR: TODO
881         // anApp->helpContext( mySource, myContext );
882       }
883     }
884   };
885   ProcessVoidEvent( new TEvent( source, context ) );
886 }
887
888 /*!
889   SalomePyQt::dumpView
890   Dumps the contents of the currently active view to the image file 
891   in the given format (JPEG, PNG, BMP are supported)
892 */
893 class TDumpViewEvent: public SALOME_Event {
894 public:
895   typedef bool TResult;
896   TResult myResult;
897   QString myFileName;
898   TDumpViewEvent( const QString& filename ) 
899     : myResult ( false ), myFileName( filename ) {}
900   virtual void Execute() {
901     if ( SalomeApp_Application* anApp = getApplication() ) {
902       SUIT_ViewManager* vm = anApp->activeViewManager();
903       if ( vm ) { 
904         SUIT_ViewWindow* vw = vm->getActiveView();
905         if ( vw ) {
906           QImage im = vw->dumpView();
907           if ( !im.isNull() && !myFileName.isEmpty() ) {
908             QString fmt = SUIT_Tools::extension( myFileName ).upper();
909             if ( fmt.isEmpty() ) fmt = QString( "BMP" ); // default format
910             if ( fmt == "JPG" )  fmt = "JPEG";
911             myResult = im.save( myFileName, fmt.latin1() );
912           }
913         }
914       }
915     }
916   }
917 };
918 bool SalomePyQt::dumpView( const QString& filename )
919 {
920   return ProcessEvent( new TDumpViewEvent( filename ) );
921 }
922
923 /*!
924   SalomePyQt::createTool
925   These methods allow operating with the toolbars:
926   - create a new toolbar or get the existing one (the toolbar name is passed as parameter);
927     this method returns an id of the toolbar;
928   - add action with given id (must be created previously) and optional index to the existing toolbar
929     (toobar is identified either by its id or by its name)
930     these methods return an id of the action.
931   If error occurs, the -1 value is returned.
932 */
933 class CrTool
934 {
935 public:
936   CrTool( const QString& tBar ) 
937     : myCase( 0 ), myTbName( tBar ) {}
938   CrTool( const int id, const int tBar, const int idx ) 
939     : myCase( 1 ), myId( id ), myTbId( tBar ), myIndex( idx ) {}
940   CrTool( const int id, const QString& tBar, const int idx )
941     : myCase( 2 ), myId( id ), myTbName( tBar ), myIndex( idx ) {}
942   CrTool( QtxAction* action, const int tbId, const int id, const int idx )
943     : myCase( 3 ), myAction( action ), myTbId( tbId ), myId( id ), myIndex( idx ) {}
944   CrTool( QtxAction* action, const QString& tBar, const int id, const int idx )
945     : myCase( 4 ), myAction( action ), myTbName( tBar ), myId( id ), myIndex( idx ) {}
946
947   int execute( SALOME_PYQT_Module* module ) const
948   {
949     if ( module ) {
950       switch ( myCase ) {
951       case 0:
952         return module->createTool( myTbName );
953       case 1:
954         return module->createTool( myId, myTbId, myIndex );
955       case 2:
956         return module->createTool( myId, myTbName, myIndex );
957       case 3:
958         return module->createTool( myAction, myTbId, myId, myIndex );
959       case 4:
960         return module->createTool( myAction, myTbName, myId, myIndex );
961       }
962     }
963     return -1;
964   }
965 private:
966    int        myCase;
967    QString    myTbName;
968    int        myTbId;
969    QtxAction* myAction;
970    int        myId;
971    int        myIndex;
972 };
973 class TCreateToolEvent: public SALOME_Event {
974 public:
975   typedef int TResult;
976   TResult myResult;
977   const CrTool& myCrTool;
978   TCreateToolEvent( const CrTool& crTool ) 
979     : myResult( -1 ), myCrTool( crTool ) {}
980   virtual void Execute() {
981     if ( SalomeApp_Application* anApp = getApplication() ) {
982       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
983       if ( !module )
984         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
985       myResult = myCrTool.execute( module );
986     }
987   }
988 };
989 // create new toolbar or get existing by name 
990 int SalomePyQt::createTool( const QString& tBar )
991 {
992   return ProcessEvent( new TCreateToolEvent( CrTool( tBar ) ) );
993 }
994 // add action with id and index to the existing tollbar
995 int SalomePyQt::createTool( const int id, const int tBar, const int idx )
996 {
997   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
998 }
999 // add action with id and index to the existing tollbar
1000 int SalomePyQt::createTool( const int id, const QString& tBar, const int idx )
1001 {
1002   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
1003 }
1004 // add action with id and index to the existing tollbar
1005 int SalomePyQt::createTool( QtxAction* a, const int tBar, const int id, const int idx )
1006 {
1007   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
1008 }
1009 // add action with id and index to the existing tollbar
1010 int SalomePyQt::createTool( QtxAction* a, const QString& tBar, const int id, const int idx )
1011 {
1012   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
1013 }
1014
1015 /*!
1016   SalomePyQt::createMenu
1017   These methods allow operating with the main menu:
1018   - create a new menu or submenu or get the existing one (the parent menu name or id is passed as parameter, 
1019     if it is empty or -1, it means that main menu is created, otherwise submenu is created);
1020     this method returns an id of the menu/submenu;
1021   - add action with given id (must be created previously) and optional index and group number to the existing menu
1022     or submenu (menu name or id us passed as parameter)
1023     these methods return an id of the action.
1024   If error occurs, the -1 value is returned.
1025 */
1026 class CrMenu
1027 {
1028 public:
1029   CrMenu( const QString& subMenu, const int menu, const int group, const int idx ) 
1030     : myCase( 0 ), mySubMenuName( subMenu ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {}
1031   CrMenu( const QString& subMenu, const QString& menu, const int group, const int idx ) 
1032     : myCase( 1 ), mySubMenuName( subMenu ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {}
1033   CrMenu( const int id, const int menu, const int group, const int idx ) 
1034     : myCase( 2 ), myId( id ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {}
1035   CrMenu( const int id, const QString& menu, const int group, const int idx ) 
1036     : myCase( 3 ), myId( id ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {}
1037   CrMenu( QtxAction* action, const int menu, const int id, const int group, const int idx ) 
1038     : myCase( 4 ), myAction( action ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1039   CrMenu( QtxAction* action, const QString& menu, const int id, const int group, const int idx ) 
1040     : myCase( 5 ), myAction( action ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1041
1042   int execute( SALOME_PYQT_Module* module ) const
1043   {
1044     if ( module ) {
1045       switch ( myCase ) {
1046       case 0:
1047         return module->createMenu( mySubMenuName, myMenuId, -1, myGroup, myIndex );
1048       case 1:
1049         return module->createMenu( mySubMenuName, myMenuName, -1, myGroup, myIndex );
1050       case 2:
1051         return module->createMenu( myId, myMenuId, myGroup, myIndex );
1052       case 3:
1053         return module->createMenu( myId, myMenuName, myGroup, myIndex );
1054       case 4:
1055         return module->createMenu( myAction, myMenuId, myId, myGroup, myIndex );
1056       case 5:
1057         return module->createMenu( myAction, myMenuName, myId, myGroup, myIndex );
1058       }
1059     }
1060     return -1;
1061   }
1062 private:
1063    int        myCase;
1064    QString    myMenuName;
1065    int        myMenuId;
1066    QString    mySubMenuName;
1067    int        myGroup;
1068    QtxAction* myAction;
1069    int        myId;
1070    int        myIndex;
1071 };
1072 class TCreateMenuEvent: public SALOME_Event {
1073 public:
1074   typedef int TResult;
1075   TResult myResult;
1076   const CrMenu& myCrMenu;
1077   TCreateMenuEvent( const CrMenu& crMenu ) 
1078     : myResult( -1 ), myCrMenu( crMenu ) {}
1079   virtual void Execute() {
1080     if ( SalomeApp_Application* anApp = getApplication() ) {
1081       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1082       if ( !module )
1083         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1084       myResult = myCrMenu.execute( module );
1085     }
1086   }
1087 };
1088 int SalomePyQt::createMenu( const QString& subMenu, const int menu, const int group, const int idx )
1089 {
1090   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, group, idx ) ) );
1091 }
1092
1093 int SalomePyQt::createMenu( const QString& subMenu, const QString& menu, const int group, const int idx )
1094 {
1095   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, group, idx ) ) );
1096 }
1097
1098 int SalomePyQt::createMenu( const int id, const int menu, const int group, const int idx )
1099 {
1100   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
1101 }
1102
1103 int SalomePyQt::createMenu( const int id, const QString& menu, const int group, const int idx )
1104 {
1105   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
1106 }
1107
1108 int SalomePyQt::createMenu( QtxAction* a, const int menu, const int id, const int group, const int idx )
1109 {
1110   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
1111 }
1112
1113 int SalomePyQt::createMenu( QtxAction* a, const QString& menu, const int id, const int group, const int idx )
1114 {
1115   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
1116 }
1117
1118 /*!
1119   SalomePyQt::createSeparator
1120   Create a separator action which can be then used in the menu or toolbar.
1121 */
1122 class TCreateSepEvent: public SALOME_Event {
1123 public:
1124   typedef QtxAction* TResult;
1125   TResult myResult;
1126   TCreateSepEvent() 
1127     : myResult( 0 ) {}
1128   virtual void Execute() {
1129     if ( SalomeApp_Application* anApp = getApplication() ) {
1130       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1131       if ( !module )
1132         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1133       if ( module )
1134         myResult = (QtxAction*)module->createSeparator();
1135     }
1136   }
1137 };
1138 QtxAction* SalomePyQt::createSeparator()
1139 {
1140   return ProcessEvent( new TCreateSepEvent() );
1141 }
1142
1143 /*!
1144   SalomePyQt::createAction
1145   Create an action which can be then used in the menu or toolbar:
1146   - id         : the unique id action to be registered to;
1147   - menuText   : action text which should appear in menu;
1148   - tipText    : text which should appear in the tooltip;
1149   - statusText : text which should appear in the status bar when action is activated;
1150   - icon       : the name of the icon file (the actual icon file name can be coded in the translation files);
1151   - key        : the key accelrator for the action
1152   - toggle     : if true the action is checkable
1153 */
1154 class TCreateActionEvent: public SALOME_Event {
1155 public:
1156   typedef QtxAction* TResult;
1157   TResult myResult;
1158   int     myId;
1159   QString myMenuText;
1160   QString myTipText;
1161   QString myStatusText;
1162   QString myIcon;
1163   int     myKey;
1164   bool    myToggle;
1165   TCreateActionEvent( const int id, const QString& menuText, const QString& tipText, 
1166                       const QString& statusText, const QString& icon, const int key, const bool toggle ) 
1167     : myResult( 0 ), myId( id ), myMenuText( menuText ), myTipText( tipText ),
1168       myStatusText( statusText ), myIcon( icon ), myKey( key ), myToggle( toggle ) {}
1169   virtual void Execute() {
1170     if ( SalomeApp_Application* anApp = getApplication() ) {
1171       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1172       if ( !module )
1173         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1174       if ( module )
1175         myResult = (QtxAction*)module->createAction( myId, myTipText, myIcon, myMenuText, myStatusText, myKey, myToggle );
1176     }
1177   }
1178 };
1179 QtxAction* SalomePyQt::createAction( const int id,           const QString& menuText, 
1180                                      const QString& tipText, const QString& statusText, 
1181                                      const QString& icon,    const int key, const bool toggle )
1182 {
1183   return ProcessEvent( new TCreateActionEvent( id, menuText, tipText, statusText, icon, key, toggle ) );
1184 }
1185
1186 /*!
1187   SalomePyQt::action
1188   Get an action by its id. Returns 0 if the action with such id was not registered.
1189 */
1190 class TActionEvent: public SALOME_Event {
1191 public:
1192   typedef QtxAction* TResult;
1193   TResult myResult;
1194   int     myId;
1195   TActionEvent( const int id )
1196     : myResult( 0 ), myId( id ) {}
1197   virtual void Execute() {
1198     if ( SalomeApp_Application* anApp = getApplication() ) {
1199       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1200       if ( !module )
1201         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1202       if ( module )
1203         myResult = (QtxAction*)module->action( myId );
1204     }
1205   }
1206 };
1207 QtxAction* SalomePyQt::action( const int id )
1208 {
1209   return ProcessEvent( new TActionEvent( id ) );
1210 }
1211
1212 /*!
1213   SalomePyQt::actionId
1214   Get an action id. Returns -1 if the action was not registered.
1215 */
1216 class TActionIdEvent: public SALOME_Event {
1217 public:
1218   typedef  int TResult;
1219   TResult  myResult;
1220   const QtxAction* myAction;
1221   TActionIdEvent( const QtxAction* action )
1222     : myResult( -1 ), myAction( action ) {}
1223   virtual void Execute() {
1224     if ( SalomeApp_Application* anApp = getApplication() ) {
1225       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1226       if ( !module )
1227         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1228       if ( module )
1229         myResult = module->actionId( myAction );
1230     }
1231   }
1232 };
1233 int SalomePyQt::actionId( const QtxAction* a )
1234 {
1235   return ProcessEvent( new TActionIdEvent( a ) );
1236 }