Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SALOME_PYQT_Module.h" // this include must be first!!!
21 #include "SalomePyQt.h"
22
23 #include <qapplication.h>
24 #include <qmenubar.h>
25 #include <qwidget.h>
26 #include <qpopupmenu.h>
27 #include <qimage.h>
28 #include <qstringlist.h>
29
30 #include "SALOME_Event.hxx"
31
32 #include "SUIT_Session.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_ResourceMgr.h"
35 #include "SUIT_Tools.h"
36 #include "STD_MDIDesktop.h"
37 #include "SalomeApp_Application.h"
38 #include "SalomeApp_Study.h"
39 #include "LightApp_SelectionMgr.h"
40 #include "OB_Browser.h"
41 #include "QtxAction.h"
42 #include "LogWindow.h"
43
44 using namespace std;
45
46 /*!
47   \return active application object [ static ]
48 */
49 static SalomeApp_Application* getApplication() {
50   if ( SUIT_Session::session() )
51     return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
52   return NULL;
53 }
54
55 /*!
56   \return active study or 0 if there is no study opened [ static ]
57 */
58 static SalomeApp_Study* getActiveStudy()
59 {
60   if ( getApplication() )
61     return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
62   return 0;
63 }
64
65 static QMap<SalomeApp_Application*, SALOME_Selection*> SelMap;
66
67 /*!
68   Creates or finds the selection object (one per study).
69 */
70 SALOME_Selection* SALOME_Selection::GetSelection( SalomeApp_Application* app )
71 {
72   SALOME_Selection* sel = 0;
73   if ( app && SelMap.find( app ) != SelMap.end() )
74     sel = SelMap[ app ];
75   else 
76     sel = SelMap[ app ] = new SALOME_Selection( app );
77   return sel;
78 }
79
80 /*!
81   Selection constructor.
82 */
83 SALOME_Selection::SALOME_Selection( QObject* p ) : QObject( p ), mySelMgr( 0 )
84 {
85   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( p );
86   if ( app ) {
87     mySelMgr = app->selectionMgr();
88     connect( mySelMgr, SIGNAL( selectionChanged() ), this, SIGNAL( currentSelectionChanged() ) );
89     connect( mySelMgr, SIGNAL( destroyed() ),        this, SLOT  ( onSelMgrDestroyed() ) );
90   }
91 }
92 /*!
93   Selection destructor. Removes selection object from the map.
94 */
95 SALOME_Selection::~SALOME_Selection()
96 {
97   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( parent() );
98   if ( app && SelMap.find( app ) != SelMap.end() )
99     SelMap.remove( app );
100 }
101
102 /*!
103   Watches for the selection manager destroying when study is closed.
104 */
105 void SALOME_Selection::onSelMgrDestroyed()
106 {
107   mySelMgr = 0;
108 }
109
110 /*!
111   Clears the selection.
112 */
113 void SALOME_Selection::Clear()
114 {
115   class TEvent: public SALOME_Event {
116     LightApp_SelectionMgr* mySelMgr;
117   public:
118     TEvent( LightApp_SelectionMgr* selMgr ) 
119       : mySelMgr( selMgr ) {}
120     virtual void Execute() {
121       if ( mySelMgr )
122         mySelMgr->clearSelected();
123     }
124   };
125   ProcessVoidEvent( new TEvent( mySelMgr ) );
126 }
127
128 /*!
129   Clears the selection.
130 */
131 void SALOME_Selection::ClearIObjects()
132 {
133   Clear();
134 }
135
136 /*!
137   Removes all selection filters.
138 */
139 void SALOME_Selection::ClearFilters()
140 {
141   class TEvent: public SALOME_Event {
142     LightApp_SelectionMgr* mySelMgr;
143   public:
144     TEvent( LightApp_SelectionMgr* selMgr ) 
145       : mySelMgr( selMgr ) {}
146     virtual void Execute() {
147       if ( mySelMgr )
148         mySelMgr->clearFilters();
149     }
150   };
151   ProcessVoidEvent( new TEvent( mySelMgr ) );
152 }
153
154 /*!
155   \return desktop (0 if error)
156 */
157 class TGetDesktopEvent: public SALOME_Event {
158 public:
159   typedef QWidget* TResult;
160   TResult myResult;
161   TGetDesktopEvent() : myResult( 0 ) {}
162   virtual void Execute() {
163     if ( getApplication() )
164       myResult = (QWidget*)( getApplication()->desktop() );
165   }
166 };
167
168 /*!
169   \return desktop
170 */
171 QWidget* SalomePyQt::getDesktop()
172 {
173   return ProcessEvent( new TGetDesktopEvent() );
174 }
175
176 /*!
177   \return workspace widget (0 if error)
178 */
179 class TGetMainFrameEvent: public SALOME_Event {
180 public:
181   typedef QWidget* TResult;
182   TResult myResult;
183   TGetMainFrameEvent() : myResult( 0 ) {}
184   virtual void Execute() {
185     if ( getApplication() ) {
186       SUIT_Desktop* aDesktop = getApplication()->desktop();
187       myResult = (QWidget*)( aDesktop->centralWidget() );
188     }
189   }
190 };
191
192 /*!
193   \return workspace widget (0 if error)
194 */
195 QWidget* SalomePyQt::getMainFrame()
196 {
197   return ProcessEvent( new TGetMainFrameEvent() );
198 }
199
200 /*!
201   SalomePyQt::getMainMenuBar
202   Gets main menu. Returns 0 in error.
203 */
204 class TGetMainMenuBarEvent: public SALOME_Event {
205 public:
206   typedef QMenuBar* TResult;
207   TResult myResult;
208   TGetMainMenuBarEvent() : myResult( 0 ) {}
209   virtual void Execute() {
210     if ( SalomeApp_Application* anApp = getApplication() ) {
211       myResult = anApp->desktop()->menuBar();
212     }
213   }
214 };
215
216 /*!
217   \return main menu
218   \retval 0 in error.
219 */
220 QMenuBar* SalomePyQt::getMainMenuBar() 
221 {
222   return ProcessEvent( new TGetMainMenuBarEvent() );
223 }
224
225 /*!
226   SalomePyQt::getPopupMenu
227   Gets an main menu's child popup menu by its id
228 */
229 class TGetPopupMenuEvent: public SALOME_Event {
230 public:
231   typedef QPopupMenu* TResult;
232   TResult  myResult;
233   MenuName myMenuName;
234   TGetPopupMenuEvent( const MenuName menu ) : myResult( 0 ), myMenuName( menu ) {}
235   virtual void Execute() {
236     if ( SalomeApp_Application* anApp = getApplication() ) {
237       QMenuBar* menuBar = anApp->desktop()->menuBar();
238       if ( menuBar ) {
239         QString menu;
240         switch( myMenuName) {
241         case File:
242           menu = QObject::tr( "MEN_DESK_FILE" );        break;
243         case View:
244           menu = QObject::tr( "MEN_DESK_VIEW" );        break;
245         case Edit:
246           menu = QObject::tr( "MEN_DESK_EDIT" );        break;
247         case Preferences:
248           menu = QObject::tr( "MEN_DESK_PREFERENCES" ); break;
249         case Tools:
250           menu = QObject::tr( "MEN_DESK_TOOLS" );       break;
251         case Window:
252           menu = QObject::tr( "MEN_DESK_WINDOW" );      break;
253         case Help:
254           menu = QObject::tr( "MEN_DESK_HELP" );        break;
255         }
256         for ( int i = 0; i < menuBar->count() && !myResult; i++ ) {
257           QMenuItem* item = menuBar->findItem( menuBar->idAt( i ) );
258           if ( item && item->text() == menu && item->popup() )
259             myResult = item->popup();
260         }
261       }
262     }
263   }
264 };
265
266 /*!
267   \return popup menu
268   \param menu - menu name
269 */
270 QPopupMenu* SalomePyQt::getPopupMenu( const MenuName menu )
271 {
272   return ProcessEvent( new TGetPopupMenuEvent( menu ) );
273 }
274
275 /*!
276   SalomePyQt::getStudyId
277   Returns active study's ID or 0 if there is no active study.
278 */
279 class TGetStudyIdEvent: public SALOME_Event {
280 public:
281   typedef int TResult;
282   TResult myResult;
283   TGetStudyIdEvent() : myResult( 0 ) {}
284   virtual void Execute() {
285     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
286       myResult = aStudy->studyDS()->StudyId();
287     }
288   }
289 };
290
291 /*!
292   SalomePyQt::getStudyId
293   Returns active study's ID or 0 if there is no active study.
294 */
295 int SalomePyQt::getStudyId()
296 {
297   return ProcessEvent( new TGetStudyIdEvent() );
298 }
299
300 /*!
301   SalomePyQt::getSelection
302   Creates a Selection object (to provide a compatibility with previous SALOME GUI).
303 */
304 class TGetSelectionEvent: public SALOME_Event {
305 public:
306   typedef SALOME_Selection* TResult;
307   TResult myResult;
308   TGetSelectionEvent() : myResult( 0 ) {}
309   virtual void Execute() {
310     myResult = SALOME_Selection::GetSelection( getApplication() );
311   }
312 };
313
314 /*!
315   Creates a Selection object (to provide a compatibility with previous SALOME GUI).
316   \return just created selection object
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
340 /*!
341   Puts an information message to the desktop's status bar
342   (with optional delay parameter given in seconds)
343   \param msg - message text 
344   \param sec - delay in seconds
345 */
346 void SalomePyQt::putInfo( const QString& msg, const int sec )
347 {
348   ProcessVoidEvent( new TPutInfoEvent( msg, sec ) );
349 }
350
351 /*!
352   SalomePyQt::getActiveComponent
353   Returns an active component name or empty string if there is no active component
354 */
355 class TGetActiveComponentEvent: public SALOME_Event {
356 public:
357   typedef QString TResult;
358   TResult myResult;
359   TGetActiveComponentEvent() {}
360   virtual void Execute() {
361     if ( SalomeApp_Application* anApp = getApplication() ) {
362       if ( CAM_Module* mod = anApp->activeModule() ) {
363         myResult = mod->name("");
364       }
365     }
366   }
367 };
368
369 /*!
370   \return an active component name or empty string if there is no active component
371 */
372 const QString SalomePyQt::getActiveComponent()
373 {
374   return ProcessEvent( new TGetActiveComponentEvent() );
375 }
376
377 /*!
378   SalomePyQt::updateObjBrowser
379   Updates an Object Browser of a given study.
380   If <studyId> <= 0 the active study's object browser is updated.
381   <updateSelection> parameter is obsolete parameter and currently not used. To be removed lately.
382 */
383 void SalomePyQt::updateObjBrowser( const int studyId, bool updateSelection )
384 {  
385   class TEvent: public SALOME_Event {
386     int  myStudyId;
387     bool myUpdateSelection;
388   public:
389     TEvent( const int studyId, bool updateSelection ) 
390       : myStudyId( studyId ), myUpdateSelection( updateSelection ) {}
391     virtual void Execute() {
392       if ( SUIT_Session::session() ) {
393         if ( getActiveStudy() && myStudyId <= 0 )
394           myStudyId = getActiveStudy()->id();
395         if ( myStudyId > 0 ) {
396           QPtrList<SUIT_Application> apps = SUIT_Session::session()->applications();
397           QPtrListIterator<SUIT_Application> it( apps );
398           for( ; it.current(); ++it ) {
399             SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( it.current() );
400             if ( anApp && anApp->activeStudy() && anApp->activeStudy()->id() == myStudyId )
401               anApp->updateObjectBrowser();
402           }
403         }
404       }
405     }
406   };
407   ProcessVoidEvent( new TEvent( studyId, updateSelection ) );
408 }
409
410 const char* DEFAULT_SECTION = "SalomePyQt";
411
412 /*!
413   SalomePyQt::addStringSetting
414   Adds a string setting to the application preferences
415   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
416   This function is obsolete. Use addSetting() instead.
417 */
418 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
419 {
420   class TEvent: public SALOME_Event {
421     QString myName;
422     QString myValue;
423     bool    myAutoValue;
424   public:
425     TEvent( const QString& name, const QString& value, bool autoValue ) 
426       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
427     virtual void Execute() {
428       if ( SUIT_Session::session() ) {
429         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
430         QStringList sl = QStringList::split( ":", myName );
431         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
432         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
433         if ( !_sec.isEmpty() && !_nam.isEmpty() )
434           resMgr->setValue( _sec, _nam, myValue );
435       }
436     }
437   };
438   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
439 }
440
441 /*!
442   SalomePyQt::addIntSetting
443   Adds an integer setting to the application preferences
444   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
445   This function is obsolete. Use addSetting() instead.
446 */
447 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
448 {
449   class TEvent: public SALOME_Event {
450     QString myName;
451     int     myValue;
452     bool    myAutoValue;
453   public:
454     TEvent( const QString& name, const int value, bool autoValue ) 
455       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
456     virtual void Execute() {
457       if ( SUIT_Session::session() ) {
458         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
459         QStringList sl = QStringList::split( ":", myName );
460         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
461         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
462         if ( !_sec.isEmpty() && !_nam.isEmpty() )
463           resMgr->setValue( _sec, _nam, myValue );
464       }
465     }
466   };
467   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
468 }
469
470 /*!
471   SalomePyQt::addDoubleSetting
472   Adds an double setting to the application preferences
473   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
474   This function is obsolete. Use addSetting() instead.
475 */
476 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
477 {
478   class TEvent: public SALOME_Event {
479     QString myName;
480     double  myValue;
481     bool    myAutoValue;
482   public:
483     TEvent( const QString& name, const double value, bool autoValue ) 
484       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
485     virtual void Execute() {
486       if ( SUIT_Session::session() ) {
487         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
488         QStringList sl = QStringList::split( ":", myName );
489         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
490         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
491         if ( !_sec.isEmpty() && !_nam.isEmpty() )
492           resMgr->setValue( _sec, _nam, myValue );
493       }
494     }
495   };
496   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
497 }
498
499 /*!
500   SalomePyQt::addBoolSetting
501   Adds an boolean setting to the application preferences
502   <autoValue> parameter is obsolete parameter and currently not used. To be removed lately.
503   This function is obsolete. Use addSetting() instead.
504 */
505 void SalomePyQt::addBoolSetting( const QString& name, const bool value, bool autoValue )
506 {
507   class TEvent: public SALOME_Event {
508     QString myName;
509     bool    myValue;
510     bool    myAutoValue;
511   public:
512     TEvent( const QString& name, const bool value, bool autoValue ) 
513       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
514     virtual void Execute() {
515       if ( SUIT_Session::session() ) {
516         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
517         QStringList sl = QStringList::split( ":", myName );
518         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
519         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
520         if ( !_sec.isEmpty() && !_nam.isEmpty() )
521           resMgr->setValue( _sec, _nam, myValue );
522       }
523     }
524   };
525   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
526 }
527
528 /*!
529   SalomePyQt::removeSettings
530   Removes a setting from the application preferences
531   This function is obsolete. Use removeSetting() instead.
532 */
533 void SalomePyQt::removeSettings( const QString& name )
534 {
535   class TEvent: public SALOME_Event {
536     QString myName;
537   public:
538     TEvent( const QString& name ) : myName( name ) {}
539     virtual void Execute() {
540       if ( SUIT_Session::session() ) {
541         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
542         QStringList sl = QStringList::split( ":", myName );
543         QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
544         QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
545         if ( !_sec.isEmpty() && !_nam.isEmpty() )
546           resMgr->remove( _sec, _nam );
547       }
548     }
549   };
550   ProcessVoidEvent( new TEvent( name ) );
551 }
552
553 /*!
554   SalomePyQt::getSetting
555   Gets a setting value (as string)
556   This function is obsolete. Use stringSetting(), integerSetting(), 
557   boolSetting(), stringSetting() or colorSetting() instead.
558 */
559 class TGetSettingEvent: public SALOME_Event {
560 public:
561   typedef QString TResult;
562   TResult myResult;
563   QString myName;
564   TGetSettingEvent( const QString& name ) : myName( name ) {}
565   virtual void Execute() {
566     if ( SUIT_Session::session() ) {
567       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
568       QStringList sl = QStringList::split( ":", myName );
569       QString _sec = sl.count() > 1 ? sl[ 0 ].stripWhiteSpace() : QString( DEFAULT_SECTION );
570       QString _nam = sl.count() > 1 ? sl[ 1 ].stripWhiteSpace() : sl.count() > 0 ? sl[ 0 ].stripWhiteSpace() : QString( "" );
571       myResult = ( !_sec.isEmpty() && !_nam.isEmpty() ) ? resMgr->stringValue( _sec, _nam, "" ) : QString( "" );
572     }
573   }
574 };
575
576 /*!
577   \return a setting value (as string)
578   This function is obsolete. Use stringSetting(), integerSetting(), 
579   boolSetting(), stringSetting() or colorSetting() instead.
580 */
581 QString SalomePyQt::getSetting( const QString& name )
582 {
583   return ProcessEvent( new TGetSettingEvent( name ) );
584 }
585
586 /*!
587   SalomePyQt::addSetting
588   Adds a double setting to the application preferences
589 */
590 void SalomePyQt::addSetting( const QString& section, const QString& name, const double value )
591 {
592   class TEvent: public SALOME_Event {
593     QString mySection;
594     QString myName;
595     double  myValue;
596   public:
597     TEvent( const QString& section, const QString& name, double value ) 
598       : mySection( section ), myName( name ), myValue( value ) {}
599     virtual void Execute() {
600       if ( SUIT_Session::session() ) {
601         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
602         if ( !mySection.isEmpty() && !myName.isEmpty() )
603           resMgr->setValue( mySection, myName, myValue );
604       }
605     }
606   };
607   ProcessVoidEvent( new TEvent( section, name, value ) );
608 }
609
610 /*!
611   SalomePyQt::addSetting
612   Adds an integer setting to the application preferences
613 */
614 void SalomePyQt::addSetting( const QString& section, const QString& name, const int value )
615 {
616   class TEvent: public SALOME_Event {
617     QString mySection;
618     QString myName;
619     int     myValue;
620   public:
621     TEvent( const QString& section, const QString& name, int value ) 
622       : mySection( section ), myName( name ), myValue( value ) {}
623     virtual void Execute() {
624       if ( SUIT_Session::session() ) {
625         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
626         if ( !mySection.isEmpty() && !myName.isEmpty() )
627           resMgr->setValue( mySection, myName, myValue );
628       }
629     }
630   };
631   ProcessVoidEvent( new TEvent( section, name, value ) );
632 }
633
634 /*!
635   SalomePyQt::addSetting
636   Adds a boolean setting to the application preferences
637   (note: the last "dumb" parameter is used in order to avoid
638   sip compilation error because of conflicting int and bool types)
639 */
640 void SalomePyQt::addSetting( const QString& section, const QString& name, const bool value, const int )
641 {
642   class TEvent: public SALOME_Event {
643     QString mySection;
644     QString myName;
645     bool    myValue;
646   public:
647     TEvent( const QString& section, const QString& name, bool value ) 
648       : mySection( section ), myName( name ), myValue( value ) {}
649     virtual void Execute() {
650       if ( SUIT_Session::session() ) {
651         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
652         if ( !mySection.isEmpty() && !myName.isEmpty() )
653           resMgr->setValue( mySection, myName, myValue );
654       }
655     }
656   };
657   ProcessVoidEvent( new TEvent( section, name, value ) );
658 }
659
660 /*!
661   SalomePyQt::addSetting
662   Adds a string setting to the application preferences
663 */
664 void SalomePyQt::addSetting( const QString& section, const QString& name, const QString& value )
665 {
666   class TEvent: public SALOME_Event {
667     QString mySection;
668     QString myName;
669     QString myValue;
670   public:
671     TEvent( const QString& section, const QString& name, const QString& value ) 
672       : mySection( section ), myName( name ), myValue( value ) {}
673     virtual void Execute() {
674       if ( SUIT_Session::session() ) {
675         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
676         if ( !mySection.isEmpty() && !myName.isEmpty() )
677           resMgr->setValue( mySection, myName, myValue );
678       }
679     }
680   };
681   ProcessVoidEvent( new TEvent( section, name, value ) );
682 }
683
684 /*!
685   SalomePyQt::addSetting
686   Adds a color setting to the application preferences
687 */
688 void SalomePyQt::addSetting( const QString& section, const QString& name, const QColor& value )
689 {
690   class TEvent: public SALOME_Event {
691     QString mySection;
692     QString myName;
693     QColor  myValue;
694   public:
695     TEvent( const QString& section, const QString& name, const QColor& value ) 
696       : mySection( section ), myName( name ), myValue( value ) {}
697     virtual void Execute() {
698       if ( SUIT_Session::session() ) {
699         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
700         if ( !mySection.isEmpty() && !myName.isEmpty() )
701           resMgr->setValue( mySection, myName, myValue );
702       }
703     }
704   };
705   ProcessVoidEvent( new TEvent( section, name, value ) );
706 }
707
708 /*!
709   SalomePyQt::integerSetting
710   Gets an integer setting from the application preferences
711 */
712 class TGetIntSettingEvent: public SALOME_Event {
713 public:
714   typedef int TResult;
715   TResult myResult;
716   QString mySection;
717   QString myName;
718   TResult myDefault;
719   TGetIntSettingEvent( const QString& section, const QString& name, const int def ) 
720     : mySection( section ), myName( name ), myDefault( def ) {}
721   virtual void Execute() {
722     if ( SUIT_Session::session() ) {
723       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
724       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->integerValue( mySection, myName, myDefault ) : myDefault;
725     }
726   }
727 };
728
729 /*!
730   \return an integer setting from the application preferences
731 */
732 int SalomePyQt::integerSetting( const QString& section, const QString& name, const int def )
733 {
734   return ProcessEvent( new TGetIntSettingEvent( section, name, def ) );
735 }
736
737 /*!
738   SalomePyQt::doubleSetting
739   Gets a double setting from the application preferences
740 */
741 class TGetDblSettingEvent: public SALOME_Event {
742 public:
743   typedef double TResult;
744   TResult myResult;
745   QString mySection;
746   QString myName;
747   TResult myDefault;
748   TGetDblSettingEvent( const QString& section, const QString& name, const double def ) 
749     : mySection( section ), myName( name ), myDefault( def ) {}
750   virtual void Execute() {
751     if ( SUIT_Session::session() ) {
752       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
753       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->doubleValue( mySection, myName, myDefault ) : myDefault;
754     }
755   }
756 };
757
758 /*!
759   \return an double setting from the application preferences
760 */
761 double SalomePyQt::doubleSetting( const QString& section, const QString& name, const double def )
762 {
763   return ProcessEvent( new TGetDblSettingEvent( section, name, def ) );
764 }
765
766 /*!
767   SalomePyQt::boolSetting
768   Gets a boolean setting from the application preferences
769 */
770 class TGetBoolSettingEvent: public SALOME_Event {
771 public:
772   typedef bool TResult;
773   TResult myResult;
774   QString mySection;
775   QString myName;
776   TResult myDefault;
777   TGetBoolSettingEvent( const QString& section, const QString& name, const bool def ) 
778     : mySection( section ), myName( name ), myDefault( def ) {}
779   virtual void Execute() {
780     if ( SUIT_Session::session() ) {
781       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
782       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->booleanValue( mySection, myName, myDefault ) : myDefault;
783     }
784   }
785 };
786
787 /*!
788   \return an boolean setting from the application preferences
789 */
790 bool SalomePyQt::boolSetting( const QString& section, const QString& name, const bool def )
791 {
792   return ProcessEvent( new TGetBoolSettingEvent( section, name, def ) );
793 }
794
795 /*!
796   SalomePyQt::stringSetting
797   Gets a string setting from the application preferences
798 */
799 class TGetStrSettingEvent: public SALOME_Event {
800 public:
801   typedef QString TResult;
802   TResult myResult;
803   QString mySection;
804   QString myName;
805   TResult myDefault;
806   TGetStrSettingEvent( const QString& section, const QString& name, const QString& def ) 
807     : mySection( section ), myName( name ), myDefault( def ) {}
808   virtual void Execute() {
809     if ( SUIT_Session::session() ) {
810       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
811       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault ) : myDefault;
812     }
813   }
814 };
815
816 /*!
817   \return an string setting from the application preferences
818 */
819 QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def )
820 {
821   return ProcessEvent( new TGetStrSettingEvent( section, name, def ) );
822 }
823
824 /*!
825   SalomePyQt::colorSetting
826   Gets a color setting from the application preferences
827 */
828 class TGetColorSettingEvent: public SALOME_Event {
829 public:
830   typedef QColor TResult;
831   TResult myResult;
832   QString mySection;
833   QString myName;
834   TResult myDefault;
835   TGetColorSettingEvent( const QString& section, const QString& name, const QColor& def ) 
836     : mySection( section ), myName( name ), myDefault( def ) {}
837   virtual void Execute() {
838     if ( SUIT_Session::session() ) {
839       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
840       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->colorValue( mySection, myName, myDefault ) : myDefault;
841     }
842   }
843 };
844
845 /*!
846   \return a color setting from the application preferences
847 */
848 QColor SalomePyQt::colorSetting ( const QString& section, const QString& name, const QColor& def )
849 {
850   return ProcessEvent( new TGetColorSettingEvent( section, name, def ) );
851 }
852
853 /*!
854   SalomePyQt::removeSetting
855   Removes a setting from the application preferences
856 */
857 void SalomePyQt::removeSetting( const QString& section, const QString& name )
858 {
859   class TEvent: public SALOME_Event {
860     QString mySection;
861     QString myName;
862   public:
863     TEvent( const QString& section, const QString& name ) : mySection( section ), myName( name ) {}
864     virtual void Execute() {
865       if ( SUIT_Session::session() ) {
866         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
867         if ( !mySection.isEmpty() && !myName.isEmpty() )
868           resMgr->remove( mySection, myName );
869       }
870     }
871   };
872   ProcessVoidEvent( new TEvent( section, name ) );
873 }
874
875 /*!
876   SalomePyQt::hasSetting
877   Returns True if the settings exists
878 */
879 class THasColorSettingEvent: public SALOME_Event {
880 public:
881   typedef bool TResult;
882   TResult myResult;
883   QString mySection;
884   QString myName;
885   THasColorSettingEvent( const QString& section, const QString& name ) 
886     : mySection( section ), myName( name ) {}
887   virtual void Execute() {
888     if ( SUIT_Session::session() ) {
889       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
890       myResult = resMgr->hasValue( mySection, myName );
891     }
892   }
893 };
894 bool SalomePyQt::hasSetting( const QString& section, const QString& name )
895 {
896   return ProcessEvent( new THasColorSettingEvent( section, name ) );
897 }
898
899 /*!
900   SalomePyQt::getFileName
901   Displays 'Open/Save file' dialog box and returns a user's choice (file name)
902 */
903 class TGetFileNameEvent: public SALOME_Event {
904 public:
905   typedef QString TResult;
906   TResult     myResult;
907   QWidget*    myParent;
908   QString     myInitial;
909   QStringList myFilters;
910   QString     myCaption;
911   bool        myOpen;
912   TGetFileNameEvent( QWidget*           parent, 
913                      const QString&     initial, 
914                      const QStringList& filters, 
915                      const QString&     caption,
916                      bool               open ) 
917     : myParent ( parent ), 
918       myInitial( initial ), 
919       myFilters( filters ), 
920       myCaption( caption ), 
921       myOpen ( open ) {}
922   virtual void Execute() {
923     if ( SalomeApp_Application* anApp = getApplication() ) {
924       myResult = anApp->getFileName( myOpen, myInitial, myFilters.join(";;"), myCaption, myParent );
925     }
926   }
927 };
928
929 /*!
930   Displays 'Open/Save file' dialog box and returns a user's choice (file name)
931 */
932 QString SalomePyQt::getFileName( QWidget*           parent, 
933                                  const QString&     initial, 
934                                  const QStringList& filters, 
935                                  const QString&     caption,
936                                  bool               open )
937 {
938   return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
939 }
940
941 /*!
942   SalomePyQt::getOpenFileNames
943   Displays 'Open files' dialog box and returns a user's choice (a list of file names)
944 */
945 class TGetOpenFileNamesEvent: public SALOME_Event {
946 public:
947   typedef QStringList TResult;
948   TResult     myResult;
949   QWidget*    myParent;
950   QString     myInitial;
951   QStringList myFilters;
952   QString     myCaption;
953   TGetOpenFileNamesEvent( QWidget*           parent, 
954                           const QString&     initial, 
955                           const QStringList& filters, 
956                           const QString&     caption ) 
957     : myParent ( parent ), 
958       myInitial( initial ), 
959       myFilters( filters ), 
960       myCaption( caption ) {}
961   virtual void Execute() {
962     if ( SalomeApp_Application* anApp = getApplication() ) {
963       myResult = anApp->getOpenFileNames( myInitial, myFilters.join(";;"), myCaption, myParent );
964     }
965   }
966 };
967
968 /*!
969   Displays 'Open files' dialog box and returns a user's choice (a list of file names)
970 */
971 QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
972                                           const QString&     initial, 
973                                           const QStringList& filters, 
974                                           const QString&     caption )
975 {
976   return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
977 }
978
979 /*!
980   SalomePyQt::getExistingDirectory
981   Displays 'Get Directory' dialog box and returns a user's choice (a directory name)
982 */
983 class TGetExistingDirectoryEvent: public SALOME_Event {
984 public:
985   typedef QString TResult;
986   TResult     myResult;
987   QWidget*    myParent;
988   QString     myInitial;
989   QString     myCaption;
990   TGetExistingDirectoryEvent( QWidget*           parent, 
991                               const QString&     initial, 
992                               const QString&     caption ) 
993     : myParent ( parent ), 
994       myInitial( initial ), 
995       myCaption( caption ) {}
996   virtual void Execute() {
997     if ( SalomeApp_Application* anApp = getApplication() ) {
998       myResult = anApp->getDirectory( myInitial, myCaption, myParent );
999     }
1000   }
1001 };
1002
1003 /*!
1004   Displays 'Get Directory' dialog box and returns a user's choice (a directory name)
1005 */
1006 QString SalomePyQt::getExistingDirectory( QWidget*       parent,
1007                                           const QString& initial,
1008                                           const QString& caption )
1009 {
1010   return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
1011 }
1012
1013 /*!
1014   SalomePyQt::helpContext
1015   Opens external browser to display 'context help' information
1016   current implementation does nothing.
1017 */
1018 void SalomePyQt::helpContext( const QString& source, const QString& context ) {
1019   class TEvent: public SALOME_Event {
1020     QString mySource;
1021     QString myContext;
1022   public:
1023     TEvent( const QString& source, const QString& context ) 
1024       : mySource( source ), myContext( context ) {}
1025     virtual void Execute() {
1026       if ( /*SalomeApp_Application* anApp =*/ getApplication() ) {
1027         // VSR: TODO
1028         // anApp->helpContext( mySource, myContext );
1029       }
1030     }
1031   };
1032   ProcessVoidEvent( new TEvent( source, context ) );
1033 }
1034
1035 /*!
1036   SalomePyQt::dumpView
1037   Dumps the contents of the currently active view to the image file 
1038   in the given format (JPEG, PNG, BMP are supported)
1039 */
1040 class TDumpViewEvent: public SALOME_Event {
1041 public:
1042   typedef bool TResult;
1043   TResult myResult;
1044   QString myFileName;
1045   TDumpViewEvent( const QString& filename ) 
1046     : myResult ( false ), myFileName( filename ) {}
1047   virtual void Execute() {
1048     if ( SalomeApp_Application* anApp = getApplication() ) {
1049       SUIT_ViewManager* vm = anApp->activeViewManager();
1050       if ( vm ) { 
1051         SUIT_ViewWindow* vw = vm->getActiveView();
1052         if ( vw ) {
1053           QImage im = vw->dumpView();
1054           if ( !im.isNull() && !myFileName.isEmpty() ) {
1055             QString fmt = SUIT_Tools::extension( myFileName ).upper();
1056             if ( fmt.isEmpty() ) fmt = QString( "BMP" ); // default format
1057             if ( fmt == "JPG" )  fmt = "JPEG";
1058             myResult = im.save( myFileName, fmt.latin1() );
1059           }
1060         }
1061       }
1062     }
1063   }
1064 };
1065
1066 /*!
1067   Dumps the contents of the currently active view to the image file 
1068   in the given format (JPEG, PNG, BMP are supported)
1069 */
1070 bool SalomePyQt::dumpView( const QString& filename )
1071 {
1072   return ProcessEvent( new TDumpViewEvent( filename ) );
1073 }
1074
1075 /*!
1076   SalomePyQt::defaultMenuGroup
1077   Returns default menu group
1078 */
1079 class TDefMenuGroupEvent: public SALOME_Event {
1080 public:
1081   typedef int TResult;
1082   TResult myResult;
1083   TDefMenuGroupEvent() : myResult( -1 ) {}
1084   virtual void Execute() {
1085     myResult = SALOME_PYQT_Module::defaultMenuGroup();
1086   }
1087 };
1088
1089 /*!
1090   \return default menu group
1091 */
1092 int SalomePyQt::defaultMenuGroup()
1093 {
1094   return ProcessEvent( new TDefMenuGroupEvent() );
1095 }
1096
1097 /*!
1098   SalomePyQt::createTool
1099   These methods allow operating with the toolbars:
1100   - create a new toolbar or get the existing one (the toolbar name is passed as parameter);
1101     this method returns an id of the toolbar;
1102   - add action with given id (must be created previously) and optional index to the existing toolbar
1103     (toobar is identified either by its id or by its name)
1104     these methods return an id of the action.
1105   If error occurs, the -1 value is returned.
1106 */
1107 class CrTool
1108 {
1109 public:
1110   CrTool( const QString& tBar ) 
1111     : myCase( 0 ), myTbName( tBar ) {}
1112   CrTool( const int id, const int tBar, const int idx ) 
1113     : myCase( 1 ), myId( id ), myTbId( tBar ), myIndex( idx ) {}
1114   CrTool( const int id, const QString& tBar, const int idx )
1115     : myCase( 2 ), myId( id ), myTbName( tBar ), myIndex( idx ) {}
1116   CrTool( QtxAction* action, const int tbId, const int id, const int idx )
1117     : myCase( 3 ), myAction( action ), myTbId( tbId ), myId( id ), myIndex( idx ) {}
1118   CrTool( QtxAction* action, const QString& tBar, const int id, const int idx )
1119     : myCase( 4 ), myAction( action ), myTbName( tBar ), myId( id ), myIndex( idx ) {}
1120
1121   int execute( SALOME_PYQT_Module* module ) const
1122   {
1123     if ( module ) {
1124       switch ( myCase ) {
1125       case 0:
1126         return module->createTool( myTbName );
1127       case 1:
1128         return module->createTool( myId, myTbId, myIndex );
1129       case 2:
1130         return module->createTool( myId, myTbName, myIndex );
1131       case 3:
1132         return module->createTool( myAction, myTbId, myId, myIndex );
1133       case 4:
1134         return module->createTool( myAction, myTbName, myId, myIndex );
1135       }
1136     }
1137     return -1;
1138   }
1139 private:
1140    int        myCase;
1141    QString    myTbName;
1142    int        myTbId;
1143    QtxAction* myAction;
1144    int        myId;
1145    int        myIndex;
1146 };
1147 class TCreateToolEvent: public SALOME_Event {
1148 public:
1149   typedef int TResult;
1150   TResult myResult;
1151   const CrTool& myCrTool;
1152   TCreateToolEvent( const CrTool& crTool ) 
1153     : myResult( -1 ), myCrTool( crTool ) {}
1154   virtual void Execute() {
1155     if ( SalomeApp_Application* anApp = getApplication() ) {
1156       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1157       if ( !module )
1158         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1159       myResult = myCrTool.execute( module );
1160     }
1161   }
1162 };
1163
1164 /*!
1165   create new toolbar or get existing by name 
1166 */
1167 int SalomePyQt::createTool( const QString& tBar )
1168 {
1169   return ProcessEvent( new TCreateToolEvent( CrTool( tBar ) ) );
1170 }
1171 /*! add action with id and index to the existing tollbar
1172 */
1173 int SalomePyQt::createTool( const int id, const int tBar, const int idx )
1174 {
1175   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
1176 }
1177 /*! add action with id and index to the existing tollbar
1178 */
1179 int SalomePyQt::createTool( const int id, const QString& tBar, const int idx )
1180 {
1181   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
1182 }
1183 /*! add action with id and index to the existing tollbar
1184 */
1185 int SalomePyQt::createTool( QtxAction* a, const int tBar, const int id, const int idx )
1186 {
1187   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
1188 }
1189 /*! add action with id and index to the existing tollbar
1190 */
1191 int SalomePyQt::createTool( QtxAction* a, const QString& tBar, const int id, const int idx )
1192 {
1193   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
1194 }
1195
1196 /*!
1197   SalomePyQt::createMenu
1198   These methods allow operating with the main menu:
1199   - create a new menu or submenu or get the existing one (the parent menu name or id is passed as parameter, 
1200     if it is empty or -1, it means that main menu is created, otherwise submenu is created);
1201     this method returns an id of the menu/submenu;
1202   - add action with given id (must be created previously) and optional index and group number to the existing menu
1203     or submenu (menu name or id us passed as parameter)
1204     these methods return an id of the action.
1205   If error occurs, the -1 value is returned.
1206 */
1207 class CrMenu
1208 {
1209 public:
1210   CrMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx ) 
1211     : myCase( 0 ), mySubMenuName( subMenu ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1212   CrMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx ) 
1213     : myCase( 1 ), mySubMenuName( subMenu ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1214   CrMenu( const int id, const int menu, const int group, const int idx ) 
1215     : myCase( 2 ), myId( id ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {}
1216   CrMenu( const int id, const QString& menu, const int group, const int idx ) 
1217     : myCase( 3 ), myId( id ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {}
1218   CrMenu( QtxAction* action, const int menu, const int id, const int group, const int idx ) 
1219     : myCase( 4 ), myAction( action ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1220   CrMenu( QtxAction* action, const QString& menu, const int id, const int group, const int idx ) 
1221     : myCase( 5 ), myAction( action ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1222
1223   int execute( SALOME_PYQT_Module* module ) const
1224   {
1225     if ( module ) {
1226       switch ( myCase ) {
1227       case 0:
1228         return module->createMenu( mySubMenuName, myMenuId, myId, myGroup, myIndex );
1229       case 1:
1230         return module->createMenu( mySubMenuName, myMenuName, myId, myGroup, myIndex );
1231       case 2:
1232         return module->createMenu( myId, myMenuId, myGroup, myIndex );
1233       case 3:
1234         return module->createMenu( myId, myMenuName, myGroup, myIndex );
1235       case 4:
1236         return module->createMenu( myAction, myMenuId, myId, myGroup, myIndex );
1237       case 5:
1238         return module->createMenu( myAction, myMenuName, myId, myGroup, myIndex );
1239       }
1240     }
1241     return -1;
1242   }
1243 private:
1244    int        myCase;
1245    QString    myMenuName;
1246    int        myMenuId;
1247    QString    mySubMenuName;
1248    int        myGroup;
1249    QtxAction* myAction;
1250    int        myId;
1251    int        myIndex;
1252 };
1253 class TCreateMenuEvent: public SALOME_Event {
1254 public:
1255   typedef int TResult;
1256   TResult myResult;
1257   const CrMenu& myCrMenu;
1258   TCreateMenuEvent( const CrMenu& crMenu ) 
1259     : myResult( -1 ), myCrMenu( crMenu ) {}
1260   virtual void Execute() {
1261     if ( SalomeApp_Application* anApp = getApplication() ) {
1262       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1263       if ( !module )
1264         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1265       myResult = myCrMenu.execute( module );
1266     }
1267   }
1268 };
1269 int SalomePyQt::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx )
1270 {
1271   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, id, group, idx ) ) );
1272 }
1273
1274 int SalomePyQt::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx )
1275 {
1276   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, id, group, idx ) ) );
1277 }
1278
1279 int SalomePyQt::createMenu( const int id, const int menu, const int group, const int idx )
1280 {
1281   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
1282 }
1283
1284 int SalomePyQt::createMenu( const int id, const QString& menu, const int group, const int idx )
1285 {
1286   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
1287 }
1288
1289 int SalomePyQt::createMenu( QtxAction* a, const int menu, const int id, const int group, const int idx )
1290 {
1291   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
1292 }
1293
1294 int SalomePyQt::createMenu( QtxAction* a, const QString& menu, const int id, const int group, const int idx )
1295 {
1296   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
1297 }
1298
1299 /*!
1300   SalomePyQt::createSeparator
1301   Create a separator action which can be then used in the menu or toolbar.
1302 */
1303 class TCreateSepEvent: public SALOME_Event {
1304 public:
1305   typedef QtxAction* TResult;
1306   TResult myResult;
1307   TCreateSepEvent() 
1308     : myResult( 0 ) {}
1309   virtual void Execute() {
1310     if ( SalomeApp_Application* anApp = getApplication() ) {
1311       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1312       if ( !module )
1313         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1314       if ( module )
1315         myResult = (QtxAction*)module->createSeparator();
1316     }
1317   }
1318 };
1319 QtxAction* SalomePyQt::createSeparator()
1320 {
1321   return ProcessEvent( new TCreateSepEvent() );
1322 }
1323
1324 /*!
1325   SalomePyQt::createAction
1326   Create an action which can be then used in the menu or toolbar:
1327   - id         : the unique id action to be registered to;
1328   - menuText   : action text which should appear in menu;
1329   - tipText    : text which should appear in the tooltip;
1330   - statusText : text which should appear in the status bar when action is activated;
1331   - icon       : the name of the icon file (the actual icon file name can be coded in the translation files);
1332   - key        : the key accelrator for the action
1333   - toggle     : if true the action is checkable
1334 */
1335 class TCreateActionEvent: public SALOME_Event {
1336 public:
1337   typedef QtxAction* TResult;
1338   TResult myResult;
1339   int     myId;
1340   QString myMenuText;
1341   QString myTipText;
1342   QString myStatusText;
1343   QString myIcon;
1344   int     myKey;
1345   bool    myToggle;
1346   TCreateActionEvent( const int id, const QString& menuText, const QString& tipText, 
1347                       const QString& statusText, const QString& icon, const int key, const bool toggle ) 
1348     : myResult( 0 ), myId( id ), myMenuText( menuText ), myTipText( tipText ),
1349       myStatusText( statusText ), myIcon( icon ), myKey( key ), myToggle( toggle ) {}
1350   virtual void Execute() {
1351     if ( SalomeApp_Application* anApp = getApplication() ) {
1352       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1353       if ( !module )
1354         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1355       if ( module )
1356         myResult = (QtxAction*)module->createAction( myId, myTipText, myIcon, myMenuText, myStatusText, myKey, myToggle );
1357     }
1358   }
1359 };
1360 QtxAction* SalomePyQt::createAction( const int id,           const QString& menuText, 
1361                                      const QString& tipText, const QString& statusText, 
1362                                      const QString& icon,    const int key, const bool toggle )
1363 {
1364   return ProcessEvent( new TCreateActionEvent( id, menuText, tipText, statusText, icon, key, toggle ) );
1365 }
1366
1367 /*!
1368   SalomePyQt::action
1369   Get an action by its id. Returns 0 if the action with such id was not registered.
1370 */
1371 class TActionEvent: public SALOME_Event {
1372 public:
1373   typedef QtxAction* TResult;
1374   TResult myResult;
1375   int     myId;
1376   TActionEvent( const int id )
1377     : myResult( 0 ), myId( id ) {}
1378   virtual void Execute() {
1379     if ( SalomeApp_Application* anApp = getApplication() ) {
1380       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1381       if ( !module )
1382         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1383       if ( module )
1384         myResult = (QtxAction*)module->action( myId );
1385     }
1386   }
1387 };
1388 QtxAction* SalomePyQt::action( const int id )
1389 {
1390   return ProcessEvent( new TActionEvent( id ) );
1391 }
1392
1393 /*!
1394   SalomePyQt::actionId
1395   Get an action id. Returns -1 if the action was not registered.
1396 */
1397 class TActionIdEvent: public SALOME_Event {
1398 public:
1399   typedef  int TResult;
1400   TResult  myResult;
1401   const QtxAction* myAction;
1402   TActionIdEvent( const QtxAction* action )
1403     : myResult( -1 ), myAction( action ) {}
1404   virtual void Execute() {
1405     if ( SalomeApp_Application* anApp = getApplication() ) {
1406       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1407       if ( !module )
1408         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1409       if ( module )
1410         myResult = module->actionId( myAction );
1411     }
1412   }
1413 };
1414 int SalomePyQt::actionId( const QtxAction* a )
1415 {
1416   return ProcessEvent( new TActionIdEvent( a ) );
1417 }
1418
1419 /*!
1420   SalomePyQt::clearMenu
1421   Clears given menu (recursively if necessary)
1422 */
1423 class TClearMenuEvent: public SALOME_Event {
1424 public:
1425   typedef  bool TResult;
1426   TResult  myResult;
1427   int      myId;
1428   int      myMenu;
1429   bool     myRemoveActions;
1430   TClearMenuEvent( const int id, const int menu, const bool removeActions )
1431     : myResult( false ), myId( id ), myMenu( menu ), myRemoveActions( removeActions ) {}
1432   virtual void Execute() {
1433     if ( SalomeApp_Application* anApp = getApplication() ) {
1434       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1435       if ( !module )
1436         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1437       if ( module )
1438         myResult = module->clearMenu( myId, myMenu, myRemoveActions );
1439     }
1440   }
1441 };
1442 bool SalomePyQt::clearMenu( const int id, const int menu, const bool removeActions )
1443 {
1444   return ProcessEvent( new TClearMenuEvent( id, menu, removeActions ) );
1445 }
1446
1447 /*!
1448   SalomePyQt::addGlobalPreference
1449   Adds global (not module) preferences group 
1450  */
1451 class TAddGlobalPrefEvent: public SALOME_Event {
1452 public:
1453   typedef int TResult;
1454   TResult myResult;
1455   QString myLabel;
1456   TAddGlobalPrefEvent( const QString& label )
1457     : myResult( -1 ), myLabel( label ) {}
1458   virtual void Execute() {
1459     if ( SalomeApp_Application* anApp = getApplication() ) {
1460       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1461       if ( !module )
1462         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1463       if ( module )
1464         myResult = module->addGlobalPreference( myLabel );
1465     }
1466   }
1467 };
1468 int SalomePyQt::addGlobalPreference( const QString& label )
1469 {
1470   return ProcessEvent( new TAddGlobalPrefEvent( label ) );
1471 }
1472
1473 /*!
1474   SalomePyQt::addPreference
1475   Adds preference 
1476  */
1477 class TAddPrefEvent: public SALOME_Event {
1478 public:
1479   typedef int TResult;
1480   TResult myResult;
1481   QString myLabel;
1482   TAddPrefEvent( const QString& label )
1483     : myResult( -1 ), myLabel( label ) {}
1484   virtual void Execute() {
1485     if ( SalomeApp_Application* anApp = getApplication() ) {
1486       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1487       if ( !module )
1488         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1489       if ( module )
1490         myResult = module->addPreference( myLabel );
1491     }
1492   }
1493 };
1494 int SalomePyQt::addPreference( const QString& label )
1495 {
1496   return ProcessEvent( new TAddPrefEvent( label ) );
1497 }
1498
1499 /*!
1500   SalomePyQt::addPreference
1501   Adds preference 
1502  */
1503 class TAddPrefParamEvent: public SALOME_Event {
1504 public:
1505   typedef int TResult;
1506   TResult myResult;
1507   QString myLabel;
1508   int     myPId;
1509   int     myType;
1510   QString mySection;
1511   QString myParam;
1512   TAddPrefParamEvent( const QString& label, 
1513                       const int pId, const int type,
1514                       const QString& section, 
1515                       const QString& param )
1516     : myResult( -1 ),
1517       myLabel( label ), myPId( pId ), myType( type ), 
1518       mySection( section ), myParam ( param ) {}
1519   virtual void Execute() {
1520     if ( SalomeApp_Application* anApp = getApplication() ) {
1521       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1522       if ( !module )
1523         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1524       if ( module )
1525         myResult = module->addPreference( myLabel, myPId, myType, mySection, myParam );
1526     }
1527   }
1528 };
1529 int SalomePyQt::addPreference( const QString& label, const int pId, const int type,
1530                                const QString& section, const QString& param )
1531 {
1532   return ProcessEvent( new TAddPrefParamEvent( label, pId, type, section, param ) );
1533 }
1534
1535 /*!
1536   SalomePyQt::preferenceProperty
1537   Gets the property value for the given (by id) preference
1538  */
1539 class TPrefPropEvent: public SALOME_Event {
1540 public:
1541   typedef QVariant TResult;
1542   TResult myResult;
1543   int     myId;
1544   QString myProp;
1545   TPrefPropEvent( const int id, const QString& prop )
1546     : myId( id ), myProp( prop )
1547   { 
1548     myResult = QVariant();
1549   }
1550   virtual void Execute() {
1551     if ( SalomeApp_Application* anApp = getApplication() ) {
1552       SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1553       if ( !module )
1554         module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1555       if ( module )
1556         myResult = module->preferenceProperty( myId, myProp );
1557     }
1558   }
1559 };
1560 QVariant SalomePyQt::preferenceProperty( const int id, const QString& prop )
1561 {
1562   return ProcessEvent( new TPrefPropEvent( id, prop ) );
1563 }
1564
1565 /*!
1566   SalomePyQt::setPreferenceProperty
1567   Sets the property value for the given (by id) preference
1568  */
1569 void SalomePyQt::setPreferenceProperty( const int id, 
1570                                         const QString& prop,
1571                                         const QVariant& var )
1572 {
1573   class TEvent: public SALOME_Event {
1574     int      myId;
1575     QString  myProp;
1576     QVariant myVar;
1577   public:
1578     TEvent( const int id, const QString& prop, const QVariant& var ) 
1579       : myId( id ), myProp( prop ), myVar( var ) {}
1580     virtual void Execute() {
1581       if ( SalomeApp_Application* anApp = getApplication() ) {
1582         SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1583         if ( !module )
1584           module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1585         if ( module )
1586           module->setPreferenceProperty( myId, myProp, myVar );
1587       }
1588     }
1589   };
1590   ProcessVoidEvent( new TEvent( id, prop, var) );
1591 }
1592
1593 /*!
1594   SalomePyQt::addPreferenceProperty
1595   Adds the property value to the list of values 
1596   for the given (by id) preference
1597
1598   This method allows creating properties which are QValueList<QVariant>
1599   - there is no way to pass such values directly to QVariant parameter
1600   from Python
1601  */
1602 void SalomePyQt::addPreferenceProperty( const int id, 
1603                                         const QString& prop,
1604                                         const int idx, 
1605                                         const QVariant& var )
1606 {
1607   class TEvent: public SALOME_Event {
1608     int      myId;
1609     QString  myProp;
1610     int      myIdx;
1611     QVariant myVar;
1612   public:
1613     TEvent( const int id, const QString& prop, const int idx, const QVariant& var ) 
1614       : myId( id ), myProp( prop ), myIdx( idx), myVar( var ) {}
1615     virtual void Execute() {
1616       if ( SalomeApp_Application* anApp = getApplication() ) {
1617         SALOME_PYQT_Module* module = SALOME_PYQT_Module::getInitModule();
1618         if ( !module )
1619           module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
1620         if ( module ) {
1621           QVariant var =  module->preferenceProperty( myId, myProp );
1622           if ( var.isValid() ) {
1623             if ( var.type() == QVariant::StringList ) {
1624               QStringList sl = var.asStringList();
1625               if ( myIdx >= 0 && myIdx < sl.count() ) 
1626                 sl[myIdx] = myVar.asString();
1627               else
1628                 sl.append( myVar.asString() );
1629               module->setPreferenceProperty( myId, myProp, sl );
1630             }
1631             else if ( var.type() == QVariant::List ) {
1632               QValueList<QVariant> vl = var.asList();
1633               if ( myIdx >= 0 && myIdx < vl.count() ) 
1634                 vl[myIdx] = myVar;
1635               else
1636                 vl.append( myVar );
1637               module->setPreferenceProperty( myId, myProp, vl );
1638             }
1639           }
1640           else {
1641             QValueList<QVariant> vl;
1642             vl.append( myVar );
1643             module->setPreferenceProperty( myId, myProp, vl );
1644           }
1645         }
1646       }
1647     }
1648   };
1649   ProcessVoidEvent( new TEvent( id, prop, idx, var) );
1650 }
1651
1652 /*!
1653   SalomePyQt::message
1654   Puts the message to the Log output window
1655  */
1656 void SalomePyQt::message( const QString& msg, bool addSeparator )
1657 {
1658   class TEvent: public SALOME_Event {
1659     QString  myMsg;
1660     bool     myAddSep;
1661   public:
1662     TEvent( const QString& msg, bool addSeparator ) 
1663       : myMsg( msg ), myAddSep( addSeparator ) {}
1664     virtual void Execute() {
1665       if ( SalomeApp_Application* anApp = getApplication() ) {
1666         LogWindow* lw = anApp->logWindow();
1667         if ( lw )
1668           lw->putMessage( myMsg, myAddSep );
1669       }
1670     }
1671   };
1672   ProcessVoidEvent( new TEvent( msg, addSeparator ) );
1673 }
1674
1675 /*!
1676   SalomePyQt::clearMessages
1677   Removes all the messages from the Log output window
1678  */
1679 void SalomePyQt::clearMessages()
1680 {
1681   class TEvent: public SALOME_Event {
1682   public:
1683     TEvent() {}
1684     virtual void Execute() {
1685       if ( SalomeApp_Application* anApp = getApplication() ) {
1686         LogWindow* lw = anApp->logWindow();
1687         if ( lw )
1688           lw->clear();
1689       }
1690     }
1691   };
1692   ProcessVoidEvent( new TEvent() );
1693 }