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