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