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