Salome HOME
Add method updateObjectBrowser() for the SalomeApp_Application
[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 "SalomePyQt.h"
11
12 #include <qapplication.h>
13 #include <qmenubar.h>
14 #include <qstringlist.h>
15
16 #include "SALOME_Event.hxx"
17
18 #include "SUIT_Session.h"
19 #include "SUIT_Desktop.h"
20 #include "SUIT_ResourceMgr.h"
21 #include "STD_MDIDesktop.h"
22 #include "SalomeApp_Application.h"
23 #include "SalomeApp_Study.h"
24 #include "SalomeApp_SelectionMgr.h"
25 #include "OB_Browser.h"
26
27 using namespace std;
28
29 //====================================================================================
30 // static functions
31 //====================================================================================
32 /*!
33   getApplication()
34   Returns active application object [ static ]
35 */
36 static SalomeApp_Application* getApplication() {
37   if ( SUIT_Session::session() )
38     return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
39   return NULL;
40 }
41
42 /*!
43   getActiveStudy()
44   Gets active study or 0 if there is no study opened [ static ]
45 */
46 static SalomeApp_Study* getActiveStudy()
47 {
48   if ( getApplication() )
49     return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
50   return 0;
51 }
52
53 //====================================================================================
54 // SALOME_Selection class.
55 //====================================================================================
56 /*!
57   SALOME_Selection::SALOME_Selection
58   Selection constructor. Gets an instance of selection manager.
59 */
60 SALOME_Selection::SALOME_Selection() : mySelMgr( 0 )
61 {
62   if ( SalomeApp_Application* anApp = getApplication() ) {
63     mySelMgr = anApp->selectionMgr();
64     connect( mySelMgr, SIGNAL( selectionChanged() ), this, SIGNAL( currentSelectionChanged() ) );
65     connect( mySelMgr, SIGNAL( destroyed() ),        this, SLOT  ( onSelMgrDestroyed() ) );
66   }
67 }
68
69 /*!
70   SALOME_Selection::onSelMgrDestroyed
71   Watches for the selection manager destroying when study is closed.
72 */
73 void SALOME_Selection::onSelMgrDestroyed()
74 {
75   mySelMgr = 0;
76 }
77
78 /*!
79   SALOME_Selection::Clear
80   Clears the selection.
81 */
82 void SALOME_Selection::Clear()
83 {
84   class TEvent: public SALOME_Event {
85     SalomeApp_SelectionMgr* mySelMgr;
86   public:
87     TEvent( SalomeApp_SelectionMgr* selMgr ) 
88       : mySelMgr( selMgr ) {}
89     virtual void Execute() {
90       if ( mySelMgr )
91         mySelMgr->clearSelected();
92     }
93   };
94   ProcessVoidEvent( new TEvent( mySelMgr ) );
95 }
96
97 /*!
98   SALOME_Selection::ClearIObjects
99   Clears the selection.
100 */
101 void SALOME_Selection::ClearIObjects()
102 {
103   Clear();
104 }
105
106 /*!
107   SALOME_Selection::ClearFilters
108   Removes all selection filters.
109 */
110 void SALOME_Selection::ClearFilters()
111 {
112   class TEvent: public SALOME_Event {
113     SalomeApp_SelectionMgr* mySelMgr;
114   public:
115     TEvent( SalomeApp_SelectionMgr* selMgr ) 
116       : mySelMgr( selMgr ) {}
117     virtual void Execute() {
118       if ( mySelMgr )
119         mySelMgr->clearFilters();
120     }
121   };
122 }
123
124 //====================================================================================
125 // SalomePyQt class
126 //====================================================================================
127
128 /*!
129   SalomePyQt::getDesktop
130   Gets desktop. Returns 0 in error.
131 */
132 class TGetDesktopEvent: public SALOME_Event {
133 public:
134   typedef QWidget* TResult;
135   TResult myResult;
136   TGetDesktopEvent() : myResult( 0 ) {}
137   virtual void Execute() {
138     if ( getApplication() )
139       myResult = (QWidget*)( getApplication()->desktop() );
140   }
141 };
142 QWidget* SalomePyQt::getDesktop()
143 {
144   return ProcessEvent( new TGetDesktopEvent() );
145 }
146
147 /*!
148   SalomePyQt::getMainFrame
149   Gets workspace widget. Returns 0 in error.
150 */
151 class TGetMainFrameEvent: public SALOME_Event {
152 public:
153   typedef QWidget* TResult;
154   TResult myResult;
155   TGetMainFrameEvent() : myResult( 0 ) {}
156   virtual void Execute() {
157     if ( getApplication() ) {
158       SUIT_Desktop* aDesktop = getApplication()->desktop();
159       myResult = (QWidget*)( aDesktop->centralWidget() );
160     }
161   }
162 };
163 QWidget* SalomePyQt::getMainFrame()
164 {
165   return ProcessEvent( new TGetMainFrameEvent() );
166 }
167
168 /*!
169   SalomePyQt::getMainMenuBar
170   Gets main menu. Returns 0 in error.
171 */
172 class TGetMainMenuBarEvent: public SALOME_Event {
173 public:
174   typedef QMenuBar* TResult;
175   TResult myResult;
176   TGetMainMenuBarEvent() : myResult( 0 ) {}
177   virtual void Execute() {
178     if ( SalomeApp_Application* anApp = getApplication() ) {
179       myResult = anApp->desktop()->menuBar();
180     }
181   }
182 };
183 QMenuBar* SalomePyQt::getMainMenuBar() 
184 {
185   return ProcessEvent( new TGetMainMenuBarEvent() );
186 }
187
188 /*!
189   SalomePyQt::getPopupMenu
190   Gets an main menu's child popup menu by its id
191 */
192 class TGetPopupMenuEvent: public SALOME_Event {
193 public:
194   typedef QPopupMenu* TResult;
195   TResult  myResult;
196   MenuName myMenuName;
197   TGetPopupMenuEvent( const MenuName menu ) : myResult( 0 ), myMenuName( menu ) {}
198   virtual void Execute() {
199     if ( SalomeApp_Application* anApp = getApplication() ) {
200       QMenuBar* menuBar = anApp->desktop()->menuBar();
201       if ( menuBar ) {
202         QString menu;
203         switch( myMenuName) {
204         case File:
205           menu = QObject::tr( "MEN_DESK_FILE" );        break;
206         case View:
207           menu = QObject::tr( "MEN_DESK_VIEW" );        break;
208         case Edit:
209           menu = QObject::tr( "MEN_DESK_EDIT" );        break;
210         case Preferences:
211           menu = QObject::tr( "MEN_DESK_PREFERENCES" ); break;
212         case Tools:
213           menu = QObject::tr( "MEN_DESK_TOOLS" );       break;
214         case Window:
215           menu = QObject::tr( "MEN_DESK_WINDOW" );      break;
216         case Help:
217           menu = QObject::tr( "MEN_DESK_HELP" );        break;
218         }
219         for ( int i = 0; i < menuBar->count() && !myResult; i++ ) {
220           QMenuItem* item = menuBar->findItem( menuBar->idAt( i ) );
221           if ( item && item->text() == menu && item->popup() )
222             myResult = item->popup();
223         }
224       }
225     }
226   }
227 };
228 QPopupMenu* SalomePyQt::getPopupMenu( const MenuName menu )
229 {
230   return ProcessEvent( new TGetPopupMenuEvent( menu ) );
231 }
232
233 /*!
234   SalomePyQt::getStudyId
235   Returns active study's ID or 0 if there is no active study.
236 */
237 class TGetStudyIdEvent: public SALOME_Event {
238 public:
239   typedef int TResult;
240   TResult myResult;
241   TGetStudyIdEvent() : myResult( 0 ) {}
242   virtual void Execute() {
243     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
244       myResult = aStudy->studyDS()->StudyId();
245     }
246   }
247 };
248 int SalomePyQt::getStudyId()
249 {
250   return ProcessEvent( new TGetStudyIdEvent() );
251 }
252
253 /*!
254   SalomePyQt::getSelection
255   Creates a Selection object (to provide a compatibility with previous SALOME GUI).
256 */
257 class TGetSelectionEvent: public SALOME_Event {
258 public:
259   typedef SALOME_Selection* TResult;
260   TResult myResult;
261   TGetSelectionEvent() : myResult( 0 ) {}
262   virtual void Execute() {
263     myResult = new SALOME_Selection();
264   }
265 };
266 SALOME_Selection* SalomePyQt::getSelection()
267 {
268   return ProcessEvent( new TGetSelectionEvent() );
269 }
270
271 /*!
272   SalomePyQt::putInfo
273   Puts an information message to the desktop's status bar
274   (with optional delay parameter given in seconds)
275 */
276 class TPutInfoEvent: public SALOME_Event {
277   QString myMsg;
278   int     mySecs;
279 public:
280   TPutInfoEvent( const QString& msg, const int sec = 0 ) : myMsg( msg ), mySecs( sec ) {}
281   virtual void Execute() {
282     if ( SalomeApp_Application* anApp = getApplication() ) {
283       anApp->putInfo( myMsg, mySecs * 1000 );
284     }
285   }
286 };
287 void SalomePyQt::putInfo( const QString& msg, const int sec )
288 {
289   ProcessVoidEvent( new TPutInfoEvent( msg, sec ) );
290 }
291
292 /*!
293   SalomePyQt::getActiveComponent
294   Returns an active component name or empty string if there is no active component
295 */
296 class TGetActiveComponentEvent: public SALOME_Event {
297 public:
298   typedef QString TResult;
299   TResult myResult;
300   TGetActiveComponentEvent() {}
301   virtual void Execute() {
302     if ( SalomeApp_Application* anApp = getApplication() ) {
303       if ( CAM_Module* mod = anApp->activeModule() ) {
304         myResult = mod->name("");
305       }
306     }
307   }
308 };
309 const QString SalomePyQt::getActiveComponent()
310 {
311   return ProcessEvent( new TGetActiveComponentEvent() );
312 }
313
314 /*!
315   SalomePyQt::updateObjBrowser
316   Updates an Object Browser of a given study.
317   If <studyId> <= 0 the active study's object browser is updated.
318   <updateSelection> parameter is obsolete parameter and currently not used. To be removed lately.
319 */
320 void SalomePyQt::updateObjBrowser( const int studyId, bool updateSelection )
321 {  
322   class TEvent: public SALOME_Event {
323     int  myStudyId;
324     bool myUpdateSelection;
325   public:
326     TEvent( const int studyId, bool updateSelection ) 
327       : myStudyId( studyId ), myUpdateSelection( updateSelection ) {}
328     virtual void Execute() {
329       if ( SUIT_Session::session() ) {
330         if ( getActiveStudy() && myStudyId <= 0 )
331           myStudyId = getActiveStudy()->id();
332         if ( myStudyId > 0 ) {
333           QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
334           QPtrListIterator<SUIT_Application> it( apps );
335           for( ; it.current(); ++it ) {
336             SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( it.current() );
337             if ( anApp && anApp->activeStudy() && anApp->activeStudy()->id() == myStudyId )
338               anApp->updateObjectBrowser();
339           }
340         }
341       }
342     }
343   };
344   ProcessVoidEvent( new TEvent( studyId, updateSelection ) );
345 }
346
347 const char* DEFAULT_SECTION = "SalomePyQt";
348
349 /*!
350   SalomePyQt::addStringSetting
351   Adds a string setting to the application preferences
352   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
353   This function is obsolete. Use addSetting() instead.
354 */
355 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
356 {
357   class TEvent: public SALOME_Event {
358     QString myName;
359     QString myValue;
360     bool    myAutoValue;
361   public:
362     TEvent( const QString& name, const QString& value, bool autoValue ) 
363       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
364     virtual void Execute() {
365       if ( SUIT_Session::session() ) {
366         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
367         QStringList sl = QStringList::split( ":", myName );
368         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
369         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
370         if ( !_sec.isEmpty() && !_nam.isEmpty() )
371           resMgr->setValue( _sec, _nam, myValue );
372       }
373     }
374   };
375   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
376 }
377
378 /*!
379   SalomePyQt::addIntSetting
380   Adds an integer setting to the application preferences
381   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
382   This function is obsolete. Use addSetting() instead.
383 */
384 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
385 {
386   class TEvent: public SALOME_Event {
387     QString myName;
388     int     myValue;
389     bool    myAutoValue;
390   public:
391     TEvent( const QString& name, const int value, bool autoValue ) 
392       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
393     virtual void Execute() {
394       if ( SUIT_Session::session() ) {
395         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
396         QStringList sl = QStringList::split( ":", myName );
397         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
398         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
399         if ( !_sec.isEmpty() && !_nam.isEmpty() )
400           resMgr->setValue( _sec, _nam, myValue );
401       }
402     }
403   };
404   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
405 }
406
407 /*!
408   SalomePyQt::addDoubleSetting
409   Adds an double setting to the application preferences
410   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
411   This function is obsolete. Use addSetting() instead.
412 */
413 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
414 {
415   class TEvent: public SALOME_Event {
416     QString myName;
417     double  myValue;
418     bool    myAutoValue;
419   public:
420     TEvent( const QString& name, const double value, bool autoValue ) 
421       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
422     virtual void Execute() {
423       if ( SUIT_Session::session() ) {
424         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
425         QStringList sl = QStringList::split( ":", myName );
426         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
427         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
428         if ( !_sec.isEmpty() && !_nam.isEmpty() )
429           resMgr->setValue( _sec, _nam, myValue );
430       }
431     }
432   };
433   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
434 }
435
436 /*!
437   SalomePyQt::removeSettings
438   Removes a setting from the application preferences
439   This function is obsolete. Use removeSetting() instead.
440 */
441 void SalomePyQt::removeSettings( const QString& name )
442 {
443   class TEvent: public SALOME_Event {
444     QString myName;
445   public:
446     TEvent( const QString& name ) : myName( name ) {}
447     virtual void Execute() {
448       if ( SUIT_Session::session() ) {
449         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
450         QStringList sl = QStringList::split( ":", myName );
451         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
452         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
453         if ( !_sec.isEmpty() && !_nam.isEmpty() )
454           resMgr->remove( _sec, _nam );
455       }
456     }
457   };
458   ProcessVoidEvent( new TEvent( name ) );
459 }
460
461 /*!
462   SalomePyQt::getSetting
463   Gets a setting value (as string)
464   This function is obsolete. Use stringSetting(), integerSetting(), 
465   boolSetting(), stringSetting() or colorSetting() instead.
466 */
467 class TGetSettingEvent: public SALOME_Event {
468 public:
469   typedef QString TResult;
470   TResult myResult;
471   QString myName;
472   TGetSettingEvent( const QString& name ) : myName( name ) {}
473   virtual void Execute() {
474     if ( SUIT_Session::session() ) {
475       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
476       QStringList sl = QStringList::split( ":", myName );
477       QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
478       QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
479       myResult = ( !_sec.isEmpty() && !_nam.isEmpty() ) ? resMgr->stringValue( _sec, _nam, "" ) : QString( "" );
480     }
481   }
482 };
483 QString SalomePyQt::getSetting( const QString& name )
484 {
485   return ProcessEvent( new TGetSettingEvent( name ) );
486 }
487
488 /*!
489   SalomePyQt::addSetting
490   Adds a double setting to the application preferences
491 */
492 void SalomePyQt::addSetting( const QString& section, const QString& name, const double value )
493 {
494   class TEvent: public SALOME_Event {
495     QString mySection;
496     QString myName;
497     double  myValue;
498   public:
499     TEvent( const QString& section, const QString& name, double value ) 
500       : mySection( section ), myName( name ), myValue( value ) {}
501     virtual void Execute() {
502       if ( SUIT_Session::session() ) {
503         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
504         if ( !mySection.isEmpty() && !myName.isEmpty() )
505           resMgr->setValue( mySection, myName, myValue );
506       }
507     }
508   };
509   ProcessVoidEvent( new TEvent( section, name, value ) );
510 }
511
512 /*!
513   SalomePyQt::addSetting
514   Adds an integer setting to the application preferences
515 */
516 void SalomePyQt::addSetting( const QString& section, const QString& name, const int value )
517 {
518   class TEvent: public SALOME_Event {
519     QString mySection;
520     QString myName;
521     int     myValue;
522   public:
523     TEvent( const QString& section, const QString& name, int value ) 
524       : mySection( section ), myName( name ), myValue( value ) {}
525     virtual void Execute() {
526       if ( SUIT_Session::session() ) {
527         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
528         if ( !mySection.isEmpty() && !myName.isEmpty() )
529           resMgr->setValue( mySection, myName, myValue );
530       }
531     }
532   };
533   ProcessVoidEvent( new TEvent( section, name, value ) );
534 }
535
536 /*!
537   SalomePyQt::addSetting
538   Adds a string setting to the application preferences
539 */
540 void SalomePyQt::addSetting( const QString& section, const QString& name, const QString& value )
541 {
542   class TEvent: public SALOME_Event {
543     QString mySection;
544     QString myName;
545     QString myValue;
546   public:
547     TEvent( const QString& section, const QString& name, const QString& value ) 
548       : mySection( section ), myName( name ), myValue( value ) {}
549     virtual void Execute() {
550       if ( SUIT_Session::session() ) {
551         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
552         if ( !mySection.isEmpty() && !myName.isEmpty() )
553           resMgr->setValue( mySection, myName, myValue );
554       }
555     }
556   };
557   ProcessVoidEvent( new TEvent( section, name, value ) );
558 }
559
560 /*!
561   SalomePyQt::addSetting
562   Adds a color setting to the application preferences
563 */
564 void SalomePyQt::addSetting( const QString& section, const QString& name, const QColor& value )
565 {
566   class TEvent: public SALOME_Event {
567     QString mySection;
568     QString myName;
569     QColor  myValue;
570   public:
571     TEvent( const QString& section, const QString& name, const QColor& value ) 
572       : mySection( section ), myName( name ), myValue( value ) {}
573     virtual void Execute() {
574       if ( SUIT_Session::session() ) {
575         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
576         if ( !mySection.isEmpty() && !myName.isEmpty() )
577           resMgr->setValue( mySection, myName, myValue );
578       }
579     }
580   };
581   ProcessVoidEvent( new TEvent( section, name, value ) );
582 }
583
584 /*!
585   SalomePyQt::integerSetting
586   Gets an integer setting from the application preferences
587 */
588 class TGetIntSettingEvent: public SALOME_Event {
589 public:
590   typedef int TResult;
591   TResult myResult;
592   QString mySection;
593   QString myName;
594   TResult myDefault;
595   TGetIntSettingEvent( const QString& section, const QString& name, const int def ) 
596     : mySection( section ), myName( name ), myDefault( def ) {}
597   virtual void Execute() {
598     if ( SUIT_Session::session() ) {
599       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
600       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->integerValue( mySection, myName, myDefault ) : myDefault;
601     }
602   }
603 };
604 int SalomePyQt::integerSetting( const QString& section, const QString& name, const int def )
605 {
606   return ProcessEvent( new TGetIntSettingEvent( section, name, def ) );
607 }
608
609 /*!
610   SalomePyQt::doubleSetting
611   Gets a double setting from the application preferences
612 */
613 class TGetDblSettingEvent: public SALOME_Event {
614 public:
615   typedef double TResult;
616   TResult myResult;
617   QString mySection;
618   QString myName;
619   TResult myDefault;
620   TGetDblSettingEvent( const QString& section, const QString& name, const double def ) 
621     : mySection( section ), myName( name ), myDefault( def ) {}
622   virtual void Execute() {
623     if ( SUIT_Session::session() ) {
624       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
625       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->doubleValue( mySection, myName, myDefault ) : myDefault;
626     }
627   }
628 };
629 double SalomePyQt::doubleSetting( const QString& section, const QString& name, const int def )
630 {
631   return ProcessEvent( new TGetDblSettingEvent( section, name, def ) );
632 }
633
634 /*!
635   SalomePyQt::boolSetting
636   Gets a boolean setting from the application preferences
637 */
638 class TGetBoolSettingEvent: public SALOME_Event {
639 public:
640   typedef bool TResult;
641   TResult myResult;
642   QString mySection;
643   QString myName;
644   TResult myDefault;
645   TGetBoolSettingEvent( const QString& section, const QString& name, const bool def ) 
646     : mySection( section ), myName( name ), myDefault( def ) {}
647   virtual void Execute() {
648     if ( SUIT_Session::session() ) {
649       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
650       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->booleanValue( mySection, myName, myDefault ) : myDefault;
651     }
652   }
653 };
654 bool SalomePyQt::boolSetting( const QString& section, const QString& name, const bool def )
655 {
656   return ProcessEvent( new TGetBoolSettingEvent( section, name, def ) );
657 }
658
659 /*!
660   SalomePyQt::stringSetting
661   Gets a string setting from the application preferences
662 */
663 class TGetStrSettingEvent: public SALOME_Event {
664 public:
665   typedef QString TResult;
666   TResult myResult;
667   QString mySection;
668   QString myName;
669   TResult myDefault;
670   TGetStrSettingEvent( const QString& section, const QString& name, const QString& def ) 
671     : mySection( section ), myName( name ), myDefault( def ) {}
672   virtual void Execute() {
673     if ( SUIT_Session::session() ) {
674       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
675       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault ) : myDefault;
676     }
677   }
678 };
679 QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def )
680 {
681   return ProcessEvent( new TGetStrSettingEvent( section, name, def ) );
682 }
683
684 /*!
685   SalomePyQt::colorSetting
686   Gets a color setting from the application preferences
687 */
688 class TGetColorSettingEvent: public SALOME_Event {
689 public:
690   typedef QColor TResult;
691   TResult myResult;
692   QString mySection;
693   QString myName;
694   TResult myDefault;
695   TGetColorSettingEvent( const QString& section, const QString& name, const QColor& def ) 
696     : mySection( section ), myName( name ), myDefault( def ) {}
697   virtual void Execute() {
698     if ( SUIT_Session::session() ) {
699       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
700       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->colorValue( mySection, myName, myDefault ) : myDefault;
701     }
702   }
703 };
704 QColor SalomePyQt::colorSetting ( const QString& section, const QString& name, const QColor& def )
705 {
706   return ProcessEvent( new TGetColorSettingEvent( section, name, def ) );
707 }
708
709 /*!
710   SalomePyQt::removeSetting
711   Removes a setting from the application preferences
712 */
713 void SalomePyQt::removeSetting( const QString& section, const QString& name )
714 {
715   class TEvent: public SALOME_Event {
716     QString mySection;
717     QString myName;
718   public:
719     TEvent( const QString& section, const QString& name ) : mySection( section ), myName( name ) {}
720     virtual void Execute() {
721       if ( SUIT_Session::session() ) {
722         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
723         if ( !mySection.isEmpty() && !myName.isEmpty() )
724           resMgr->remove( mySection, myName );
725       }
726     }
727   };
728   ProcessVoidEvent( new TEvent( section, name ) );
729 }
730
731 /*!
732   SalomePyQt::getFileName
733   Displays 'Open/Save file' dialog box and returns a user's choice (file name)
734 */
735 class TGetFileNameEvent: public SALOME_Event {
736 public:
737   typedef QString TResult;
738   TResult     myResult;
739   QWidget*    myParent;
740   QString     myInitial;
741   QStringList myFilters;
742   QString     myCaption;
743   bool        myOpen;
744   TGetFileNameEvent( QWidget*           parent, 
745                      const QString&     initial, 
746                      const QStringList& filters, 
747                      const QString&     caption,
748                      bool               open ) 
749     : myParent ( parent ), 
750       myInitial( initial ), 
751       myFilters( filters ), 
752       myCaption( caption ), 
753       myOpen ( open ) {}
754   virtual void Execute() {
755     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
756       // VSR: TODO
757       // myResult = QAD_FileDlg::getFileName(parent, initial, filters, caption, open);
758     }
759   }
760 };
761 QString SalomePyQt::getFileName( QWidget*           parent, 
762                                  const QString&     initial, 
763                                  const QStringList& filters, 
764                                  const QString&     caption,
765                                  bool               open )
766 {
767   return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
768 }
769
770 /*!
771   SalomePyQt::getOpenFileNames
772   Displays 'Open files' dialog box and returns a user's choice (a list of file names)
773 */
774 class TGetOpenFileNamesEvent: public SALOME_Event {
775 public:
776   typedef QStringList TResult;
777   TResult     myResult;
778   QWidget*    myParent;
779   QString     myInitial;
780   QStringList myFilters;
781   QString     myCaption;
782   TGetOpenFileNamesEvent( QWidget*           parent, 
783                           const QString&     initial, 
784                           const QStringList& filters, 
785                           const QString&     caption ) 
786     : myParent ( parent ), 
787       myInitial( initial ), 
788       myFilters( filters ), 
789       myCaption( caption ) {}
790   virtual void Execute() {
791     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
792       // VSR: TODO
793       // myResult = QAD_FileDlg::getOpenFileNames(parent, initial, filters, caption);
794     }
795   }
796 };
797 QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
798                                           const QString&     initial, 
799                                           const QStringList& filters, 
800                                           const QString&     caption )
801 {
802   return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
803 }
804
805 /*!
806   SalomePyQt::getExistingDirectory
807   Displays 'Get Directory' dialog box and returns a user's choice (a directory name)
808 */
809 class TGetExistingDirectoryEvent: public SALOME_Event {
810 public:
811   typedef QString TResult;
812   TResult     myResult;
813   QWidget*    myParent;
814   QString     myInitial;
815   QString     myCaption;
816   TGetExistingDirectoryEvent( QWidget*           parent, 
817                               const QString&     initial, 
818                               const QString&     caption ) 
819     : myParent ( parent ), 
820       myInitial( initial ), 
821       myCaption( caption ) {}
822   virtual void Execute() {
823     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
824       // VSR: TODO
825       // myResult = QAD_FileDlg::getExistingDirectory(parent, initial, caption);
826     }
827   }
828 };
829 QString SalomePyQt::getExistingDirectory( QWidget*       parent,
830                                           const QString& initial,
831                                           const QString& caption )
832 {
833   return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
834 }
835
836 /*!
837   SalomePyQt::helpContext
838   Opens external browser to display 'context help' information
839   current implementation does nothing.
840 */
841 void SalomePyQt::helpContext( const QString& source, const QString& context ) {
842   class TEvent: public SALOME_Event {
843     QString mySource;
844     QString myContext;
845   public:
846     TEvent( const QString& source, const QString& context ) 
847       : mySource( source ), myContext( context ) {}
848     virtual void Execute() {
849       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
850         // VSR: TODO
851 ////QAD_Application::getDesktop()->helpContext(source, context);
852       }
853     }
854   };
855   ProcessVoidEvent( new TEvent( source, context ) );
856 }
857
858 /*!
859   SalomePyQt::dumpView
860   Dumps the contents of the currently active view to the image file 
861   in the given format (JPEG, PNG, BMP are supported)
862 */
863 class TDumpViewEvent: public SALOME_Event {
864 public:
865   typedef bool TResult;
866   TResult myResult;
867   QString myFileName;
868   TDumpViewEvent( const QString& filename ) 
869     : myResult ( false ), myFileName( filename ) {}
870   virtual void Execute() {
871     if ( /*SalomeApp_Study* aStudy = */getActiveStudy() ) {
872       // VSR: TODO
873 //   QAD_Study* activeStudy = QAD_Application::getDesktop()->getActiveApp()->getActiveStudy();
874 //   if ( !activeStudy )
875 //     return false;
876 //   QAD_ViewFrame* activeViewFrame = activeStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame();
877 //   if ( !activeViewFrame )
878 //     return false;
879 //   if ( !activeViewFrame->getViewWidget() )
880 //     return false;
881
882 //   qApp->processEvents();
883 //   QPixmap px = QPixmap::grabWindow( activeViewFrame->getViewWidget()->winId() );
884 //   if ( !filename.isNull() ) {
885 //     QString fmt = QAD_Tools::getFileExtensionFromPath( filename ).upper();
886 //     if ( fmt.isEmpty() )
887 //       fmt = QString( "PNG" ); // default format
888 //     if ( fmt == "JPG" )
889 //       fmt = "JPEG";
890 //     bool bOk = px.save( filename, fmt.latin1() );
891 //     return bOk;
892 //   }
893 //   return false;
894     }
895   }
896 };
897 bool SalomePyQt::dumpView( const QString& filename )
898 {
899   return ProcessEvent( new TDumpViewEvent( filename ) );
900 }
901