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