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