Salome HOME
e50f65e24e2c1f1b984a60495d43eae00f0a4e47
[modules/gui.git] / src / SALOME_PYQT / SalomePyQt / SalomePyQt.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : SalomePyQt.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #include <SALOME_PYQT_Module.h> // this include must be first!!!
26 #include "SalomePyQt.h"
27
28 #include <QApplication>
29 #include <QMenuBar>
30 #include <QMenu>
31 #include <QImage>
32 #include <QStringList>
33 #include <QAction>
34
35 #include <SALOME_Event.h>
36
37 #include <QtxActionMenuMgr.h>
38 #include <QtxActionGroup.h>
39 #include <QtxWorkstack.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_ResourceMgr.h>
43 #include <SUIT_Tools.h>
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_ViewWindow.h>
46 #include <STD_TabDesktop.h>
47 #include <SalomeApp_Application.h>
48 #include <SalomeApp_Study.h>
49 #include <LightApp_SelectionMgr.h>
50 #include <LogWindow.h>
51 #include <OCCViewer_ViewWindow.h>
52 #include <Plot2d_ViewManager.h>
53 #include <Plot2d_ViewWindow.h>
54
55 /*!
56   \brief Get the currently active application.
57   \internal
58   \return active application object or 0 if there is no any
59 */
60 static SalomeApp_Application* getApplication()
61 {
62   if ( SUIT_Session::session() )
63     return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
64   return 0;
65 }
66
67 /*!
68   \brief Get the currently active study.
69   \internal
70   \return active study or 0 if there is no study opened
71 */
72 static SalomeApp_Study* getActiveStudy()
73 {
74   if ( getApplication() )
75     return dynamic_cast<SalomeApp_Study*>( getApplication()->activeStudy() );
76   return 0;
77 }
78
79 /*!
80   \brief Get the currently active module.
81   \internal
82   This function returns correct result only if Python-based
83   module is currently active. Otherwize, 0 is returned.
84 */
85 static SALOME_PYQT_Module* getActiveModule()
86 {
87   SALOME_PYQT_Module* module = 0;
88   if ( SalomeApp_Application* anApp = getApplication() ) {
89     module = SALOME_PYQT_Module::getInitModule();
90     if ( !module )
91       module = dynamic_cast<SALOME_PYQT_Module*>( anApp->activeModule() );
92   }
93   return module;
94 }
95
96 /*!
97   \class SALOME_Selection
98   \brief The class represents selection which can be used in Python.
99 */
100
101 /*!
102   \brief Map of created selection objects.
103   \internal
104 */
105 static QMap<SalomeApp_Application*, SALOME_Selection*> SelMap;
106
107 /*!
108   \brief Get the selection object for the specified application.
109
110   Finds or creates the selection object (one per study).
111
112   \param app application object
113   \return selection object or 0 if \a app is invalid
114 */
115 SALOME_Selection* SALOME_Selection::GetSelection( SalomeApp_Application* app )
116 {
117   SALOME_Selection* sel = 0;
118   if ( app && SelMap.find( app ) != SelMap.end() )
119     sel = SelMap[ app ];
120   else 
121     sel = SelMap[ app ] = new SALOME_Selection( app );
122   return sel;
123 }
124
125 /*!
126   \brief Constructor.
127   \param p parent object
128 */
129 SALOME_Selection::SALOME_Selection( QObject* p ) : QObject( p ), mySelMgr( 0 )
130 {
131   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( p );
132   if ( app ) {
133     mySelMgr = app->selectionMgr();
134     connect( mySelMgr, SIGNAL( selectionChanged() ), this, SIGNAL( currentSelectionChanged() ) );
135     connect( mySelMgr, SIGNAL( destroyed() ),        this, SLOT  ( onSelMgrDestroyed() ) );
136   }
137 }
138 /*!
139   \brief Destructor.
140 */
141 SALOME_Selection::~SALOME_Selection()
142 {
143   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( parent() );
144   if ( app && SelMap.find( app ) != SelMap.end() )
145     SelMap.remove( app );
146 }
147
148 /*!
149   \brief Called when selection manager is destroyed (usually 
150   when the study is closed).
151 */
152 void SALOME_Selection::onSelMgrDestroyed()
153 {
154   mySelMgr = 0;
155 }
156
157 /*!
158   \brief Clear the selection.
159 */
160 void SALOME_Selection::Clear()
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->clearSelected();
170     }
171   };
172   ProcessVoidEvent( new TEvent( mySelMgr ) );
173 }
174
175 /*!
176   \brief Clear the selection.
177 */
178 void SALOME_Selection::ClearIObjects()
179 {
180   Clear();
181 }
182
183 /*!
184   Removes all selection filters.
185 */
186 void SALOME_Selection::ClearFilters()
187 {
188   class TEvent: public SALOME_Event {
189     LightApp_SelectionMgr* mySelMgr;
190   public:
191     TEvent( LightApp_SelectionMgr* selMgr ) 
192       : mySelMgr( selMgr ) {}
193     virtual void Execute() {
194       if ( mySelMgr )
195         mySelMgr->clearFilters();
196     }
197   };
198   ProcessVoidEvent( new TEvent( mySelMgr ) );
199 }
200
201 /*!
202   \class SalomePyQt
203   \brief The class provides utility functions which can be used in the Python
204   to operate with the SALOME GUI.
205
206   All the functionality of this class is implemented as static methods, so they
207   can be called with the class name prefixed or via creation of the class instance.
208   For example, next both ways of SalomePyQt class usage are legal:
209   \code
210   from SalomePyQt import *
211   sg = SalomePyQt()
212   # using SalomePyQt class instance
213   desktop = sg.getDesktop()
214   # using SalomePyQt class directly
215   menubar = SalomePyQt.getMainMenuBar()
216   \endcode
217 */
218
219 /*!
220   \fn QWidget* SalomePyQt::getDesktop();
221   \brief Get the active application's desktop window.
222   \return desktop window or 0 if there is no any
223 */
224
225 class TGetDesktopEvent: public SALOME_Event 
226 {
227 public:
228   typedef QWidget* TResult;
229   TResult myResult;
230   TGetDesktopEvent() : myResult( 0 ) {}
231   virtual void Execute()
232   {
233     if ( getApplication() )
234       myResult = (QWidget*)( getApplication()->desktop() );
235   }
236 };
237 QWidget* SalomePyQt::getDesktop()
238 {
239   return ProcessEvent( new TGetDesktopEvent() );
240 }
241
242 /*!
243   \fn QWidget* SalomePyQt::getMainFrame();
244   \brief Get current application's main frame widget [obsolete].
245
246   Main frame widget is an internal widget of the application 
247   desktop window (workspace).
248
249   \return workspace widget (0 on any error)
250 */
251
252 class TGetMainFrameEvent: public SALOME_Event
253 {
254 public:
255   typedef QWidget* TResult;
256   TResult myResult;
257   TGetMainFrameEvent() : myResult( 0 ) {}
258   virtual void Execute()
259   {
260     if ( getApplication() ) {
261       SUIT_Desktop* aDesktop = getApplication()->desktop();
262       myResult = (QWidget*)( aDesktop->centralWidget() );
263     }
264   }
265 };
266 QWidget* SalomePyQt::getMainFrame()
267 {
268   return ProcessEvent( new TGetMainFrameEvent() );
269 }
270
271 /*!
272   \fn QMenuBar* SalomePyQt::getMainMenuBar();
273   \brief Get current application desktop's main menu.
274   \return main menu object (0 on any error)
275 */
276
277 class TGetMainMenuBarEvent: public SALOME_Event
278 {
279 public:
280   typedef QMenuBar* TResult;
281   TResult myResult;
282   TGetMainMenuBarEvent() : myResult( 0 ) {}
283   virtual void Execute()
284   {
285     if ( SalomeApp_Application* anApp = getApplication() ) {
286       myResult = anApp->desktop()->menuBar();
287     }
288   }
289 };
290 QMenuBar* SalomePyQt::getMainMenuBar() 
291 {
292   return ProcessEvent( new TGetMainMenuBarEvent() );
293 }
294
295 /*!
296   QMenu* SalomePyQt::getPopupMenu( const MenuName menu );
297   \brief Get main menu's child popup submenu by its identifier.
298   
299   This function is obsolete. 
300   Use QMenu* SalomePyQt::getPopupMenu( const QString& menu ) instead.
301
302   \param menu menu identifier
303   \return popup submenu object or 0 if it does not exist
304 */
305
306 /*!
307   QMenu* SalomePyQt::getPopupMenu( const QString& menu );
308   \brief Get main menu's child popup submenu by its name.
309   
310   The function creates menu if it does not exist.
311
312   \param menu menu name
313   \return popup submenu object (0 on any error)
314 */
315
316 class TGetPopupMenuEvent: public SALOME_Event
317 {
318 public:
319   typedef QMenu* TResult;
320   TResult myResult;
321   QString myMenuName;
322   TGetPopupMenuEvent( const QString& menu ) : myResult( 0 ), myMenuName( menu ) {}
323   virtual void Execute()
324   {
325     SalomeApp_Application* anApp = getApplication();
326     if ( anApp && !myMenuName.isEmpty() ) {
327       QtxActionMenuMgr* mgr = anApp->desktop()->menuMgr();
328       myResult = mgr->findMenu( myMenuName, -1, false ); // search only top menu
329     }
330   }
331 };
332
333 static QString getMenuName( const QString& menuId )
334 {
335   QStringList contexts;
336   contexts << "SalomeApp_Application" << "LightApp_Application" << "STD_TabDesktop" <<
337     "STD_MDIDesktop" << "STD_Application" << "SUIT_Application" << "";
338   QString menuName = menuId;
339   for ( int i = 0; i < contexts.count() && menuName == menuId; i++ )
340     menuName = QApplication::translate( contexts[i].toLatin1().data(), menuId.toLatin1().data() );
341   return menuName;
342 }
343
344 QMenu* SalomePyQt::getPopupMenu( const MenuName menu )
345 {
346   QString menuName;
347   switch( menu ) {
348   case File:
349     menuName = getMenuName( "MEN_DESK_FILE" );        break;
350   case View:
351     menuName = getMenuName( "MEN_DESK_VIEW" );        break;
352   case Edit:
353     menuName = getMenuName( "MEN_DESK_EDIT" );        break;
354   case Preferences:
355     menuName = getMenuName( "MEN_DESK_PREFERENCES" ); break;
356   case Tools:
357     menuName = getMenuName( "MEN_DESK_TOOLS" );       break;
358   case Window:
359     menuName = getMenuName( "MEN_DESK_WINDOW" );      break;
360   case Help:
361     menuName = getMenuName( "MEN_DESK_HELP" );        break;
362   }
363   return ProcessEvent( new TGetPopupMenuEvent( menuName ) );
364 }
365 QMenu* SalomePyQt::getPopupMenu( const QString& menu )
366 {
367   return ProcessEvent( new TGetPopupMenuEvent( menu ) );
368 }
369
370 /*!
371   \fn int SalomePyQt::getStudyId();
372   \brief Get active study's identifier.
373   \return active study ID or 0 if there is no active study
374 */
375
376 class TGetStudyIdEvent: public SALOME_Event
377 {
378 public:
379   typedef int TResult;
380   TResult myResult;
381   TGetStudyIdEvent() : myResult( 0 ) {}
382   virtual void Execute()
383   {
384     if ( SalomeApp_Study* aStudy = getActiveStudy() ) {
385       myResult = aStudy->studyDS()->StudyId();
386     }
387   }
388 };
389 int SalomePyQt::getStudyId()
390 {
391   return ProcessEvent( new TGetStudyIdEvent() );
392 }
393
394 /*!
395   \fn SALOME_Selection* SalomePyQt::getSelection()
396   \brief Get the selection object for the current study.
397
398   Creates a Selection object if it has not been created yet.
399
400   \return selection object (0 on error)
401 */
402
403 class TGetSelectionEvent: public SALOME_Event 
404 {
405 public:
406   typedef SALOME_Selection* TResult;
407   TResult myResult;
408   TGetSelectionEvent() : myResult( 0 ) {}
409   virtual void Execute() 
410   {
411     myResult = SALOME_Selection::GetSelection( getApplication() );
412   }
413 };
414 SALOME_Selection* SalomePyQt::getSelection()
415 {
416   return ProcessEvent( new TGetSelectionEvent() );
417 }
418
419 /*!
420   \fn void SalomePyQt::putInfo( const QString& msg, const int sec )
421   \brief Put an information message to the current application's 
422   desktop status bar.
423
424   Optional second delay parameter (\a sec) can be used to specify
425   time of the message diplaying in seconds. If this parameter is less
426   or equal to zero, the constant message will be put.
427
428   \param msg message text 
429   \param sec message displaying time in seconds
430 */
431
432 class TPutInfoEvent: public SALOME_Event
433 {
434   QString myMsg;
435   int     mySecs;
436 public:
437   TPutInfoEvent( const QString& msg, const int sec = 0 ) : myMsg( msg ), mySecs( sec ) {}
438   virtual void Execute()
439   {
440     if ( SalomeApp_Application* anApp = getApplication() ) {
441       anApp->putInfo( myMsg, mySecs * 1000 );
442     }
443   }
444 };
445 void SalomePyQt::putInfo( const QString& msg, const int sec )
446 {
447   ProcessVoidEvent( new TPutInfoEvent( msg, sec ) );
448 }
449
450 /*!
451   \fn const QString SalomePyQt::getActiveComponent();
452   \brief Get the currently active module name (for the current study).
453   \return active module name or empty string if there is no active module
454 */
455
456 class TGetActiveComponentEvent: public SALOME_Event
457 {
458 public:
459   typedef QString TResult;
460   TResult myResult;
461   TGetActiveComponentEvent() {}
462   virtual void Execute() 
463   {
464     if ( SalomeApp_Application* anApp = getApplication() ) {
465       if ( CAM_Module* mod = anApp->activeModule() ) {
466         myResult = mod->name();
467       }
468     }
469   }
470 };
471 const QString SalomePyQt::getActiveComponent()
472 {
473   return ProcessEvent( new TGetActiveComponentEvent() );
474 }
475
476 /*!
477   \brief Update an Object Browser of the specified (by identifier) study.
478
479   If \a studyId <= 0 the active study's object browser is updated.
480   The \a updateSelection parameter is obsolete and currently is not used. 
481   This parameter will be removed in future, so try to avoid its usage in 
482   your code.
483
484   \brief studyId study identifier
485   \brief updateSelection update selection flag (not used)
486   \sa getActiveStudy()
487 */
488 void SalomePyQt::updateObjBrowser( const int studyId, bool updateSelection )
489 {  
490   class TEvent: public SALOME_Event
491   {
492     int  myStudyId;
493     bool myUpdateSelection;
494   public:
495     TEvent( const int studyId, bool updateSelection ) 
496       : myStudyId( studyId ), myUpdateSelection( updateSelection ) {}
497     virtual void Execute()
498     {
499       if ( SUIT_Session::session() ) {
500         if ( getActiveStudy() && myStudyId <= 0 )
501           myStudyId = getActiveStudy()->id();
502         if ( myStudyId > 0 ) {
503           QList<SUIT_Application*> apps = SUIT_Session::session()->applications();
504           QList<SUIT_Application*>::Iterator it;
505           for( it = apps.begin(); it != apps.end(); ++it ) {
506             SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( *it );
507             if ( anApp && anApp->activeStudy() && anApp->activeStudy()->id() == myStudyId ) {
508               anApp->updateObjectBrowser();
509               return;
510             }
511           }
512         }
513       }
514     }
515   };
516   ProcessVoidEvent( new TEvent( studyId, updateSelection ) );
517 }
518
519 /*!
520   \brief Default resource file section name.
521   \internal
522 */
523 static const char* DEFAULT_SECTION = "SalomePyQt";
524
525 /*!
526   \brief Add string setting to the application preferences.
527
528   The parameter \a autoValue is obsolete parameter and currently is not used.
529   This parameter will be removed in future, so try to avoid its usage in 
530   your code.
531
532   This function is obsolete. Use one of addSetting() instead.
533
534   \param name setting name (it should be of kind <section:setting> where
535   \c section is resources section name and \c setting is setting name)
536   \param value new setting value
537   \param autoValue (not used)
538 */
539 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
540 {
541   class TEvent: public SALOME_Event
542   {
543     QString myName;
544     QString myValue;
545     bool    myAutoValue;
546   public:
547     TEvent( const QString& name, const QString& value, bool autoValue ) 
548       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
549     virtual void Execute() {
550       if ( SUIT_Session::session() ) {
551         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
552         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
553         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
554         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
555         if ( !_sec.isEmpty() && !_nam.isEmpty() )
556           resMgr->setValue( _sec, _nam, myValue );
557       }
558     }
559   };
560   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
561 }
562
563 /*!
564   \brief Add integer setting to the application preferences.
565
566   The parameter \a autoValue is obsolete parameter and currently is not used.
567   This parameter will be removed in future, so try to avoid its usage in 
568   your code.
569
570   This function is obsolete. Use one of addSetting() instead.
571
572   \param name setting name (it should be of kind <section:setting> where
573   \c section is resources section name and \c setting is setting name)
574   \param value new setting value
575   \param autoValue (not used)
576 */
577 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
578 {
579   class TEvent: public SALOME_Event 
580   {
581     QString myName;
582     int     myValue;
583     bool    myAutoValue;
584   public:
585     TEvent( const QString& name, const int value, bool autoValue ) 
586       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
587     virtual void Execute()
588     {
589       if ( SUIT_Session::session() ) {
590         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
591         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
592         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
593         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
594         if ( !_sec.isEmpty() && !_nam.isEmpty() )
595           resMgr->setValue( _sec, _nam, myValue );
596       }
597     }
598   };
599   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
600 }
601
602 /*!
603   \brief Add double setting to the application preferences.
604
605   The parameter \a autoValue is obsolete parameter and currently is not used.
606   This parameter will be removed in future, so try to avoid its usage in 
607   your code.
608
609   This function is obsolete. Use one of addSetting() instead.
610
611   \param name setting name (it should be of kind <section:setting> where
612   \c section is resources section name and \c setting is setting name)
613   \param value new setting value
614   \param autoValue (not used)
615 */
616 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
617 {
618   class TEvent: public SALOME_Event 
619   {
620     QString myName;
621     double  myValue;
622     bool    myAutoValue;
623   public:
624     TEvent( const QString& name, const double value, bool autoValue ) 
625       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
626     virtual void Execute() 
627     {
628       if ( SUIT_Session::session() ) {
629         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
630         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
631         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
632         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
633         if ( !_sec.isEmpty() && !_nam.isEmpty() )
634           resMgr->setValue( _sec, _nam, myValue );
635       }
636     }
637   };
638   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
639 }
640
641 /*!
642   \brief Add boolean setting to the application preferences.
643
644   The parameter \a autoValue is obsolete parameter and currently is not used.
645   This parameter will be removed in future, so try to avoid its usage in 
646   your code.
647
648   This function is obsolete. Use one of addSetting() instead.
649
650   \param name setting name (it should be of kind <section:setting> where
651   \c section is resources section name and \c setting is setting name)
652   \param value new setting value
653   \param autoValue (not used)
654 */
655 void SalomePyQt::addBoolSetting( const QString& name, const bool value, bool autoValue )
656 {
657   class TEvent: public SALOME_Event 
658   {
659     QString myName;
660     bool    myValue;
661     bool    myAutoValue;
662   public:
663     TEvent( const QString& name, const bool value, bool autoValue ) 
664       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
665     virtual void Execute() 
666     {
667       if ( SUIT_Session::session() ) {
668         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
669         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
670         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
671         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
672         if ( !_sec.isEmpty() && !_nam.isEmpty() )
673           resMgr->setValue( _sec, _nam, myValue );
674       }
675     }
676   };
677   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
678 }
679
680 /*!
681   \brief Remove setting from the application preferences.
682
683   This function is obsolete. Use removeSetting() instead.
684
685   \param name setting name (it should be of kind <section:setting> where
686   \c section is resources section name and \c setting is setting name)
687 */
688 void SalomePyQt::removeSettings( const QString& name )
689 {
690   class TEvent: public SALOME_Event {
691     QString myName;
692   public:
693     TEvent( const QString& name ) : myName( name ) {}
694     virtual void Execute() {
695       if ( SUIT_Session::session() ) {
696         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
697         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
698         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
699         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
700         if ( !_sec.isEmpty() && !_nam.isEmpty() )
701           resMgr->remove( _sec, _nam );
702       }
703     }
704   };
705   ProcessVoidEvent( new TEvent( name ) );
706 }
707
708 /*!
709   \fn QString SalomePyQt::getSetting( const QString& name );
710   \brief Get application setting value (as string represenation).
711
712   This function is obsolete. Use stringSetting(), integerSetting(), 
713   boolSetting(), stringSetting() or colorSetting() instead.
714
715   \param name setting name (it should be of kind <section:setting> where
716   \c section is resources section name and \c setting is setting name)
717   \return setting name (empty string if setting name is invalid)
718 */
719
720 class TGetSettingEvent: public SALOME_Event 
721 {
722 public:
723   typedef QString TResult;
724   TResult myResult;
725   QString myName;
726   TGetSettingEvent( const QString& name ) : myName( name ) {}
727   virtual void Execute() 
728   {
729     if ( SUIT_Session::session() ) {
730       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
731       QStringList sl = myName.split( ":", QString::SkipEmptyParts );
732       QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
733       QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
734       myResult = ( !_sec.isEmpty() && !_nam.isEmpty() ) ? resMgr->stringValue( _sec, _nam, "" ) : QString( "" );
735     }
736   }
737 };
738 QString SalomePyQt::getSetting( const QString& name )
739 {
740   return ProcessEvent( new TGetSettingEvent( name ) );
741 }
742
743 /*!
744   \brief Add double setting to the application preferences.
745   \param section resources file section name 
746   \param name setting name
747   \param value new setting value
748 */
749 void SalomePyQt::addSetting( const QString& section, const QString& name, const double value )
750 {
751   class TEvent: public SALOME_Event 
752   {
753     QString mySection;
754     QString myName;
755     double  myValue;
756   public:
757     TEvent( const QString& section, const QString& name, double value ) 
758       : mySection( section ), myName( name ), myValue( value ) {}
759     virtual void Execute() 
760     {
761       if ( SUIT_Session::session() ) {
762         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
763         if ( !mySection.isEmpty() && !myName.isEmpty() )
764           resMgr->setValue( mySection, myName, myValue );
765       }
766     }
767   };
768   ProcessVoidEvent( new TEvent( section, name, value ) );
769 }
770
771 /*!
772   \brief Add integer setting to the application preferences.
773   \param section resources file section name 
774   \param name setting name
775   \param value new setting value
776 */
777 void SalomePyQt::addSetting( const QString& section, const QString& name, const int value )
778 {
779   class TEvent: public SALOME_Event 
780   {
781     QString mySection;
782     QString myName;
783     int     myValue;
784   public:
785     TEvent( const QString& section, const QString& name, int value ) 
786       : mySection( section ), myName( name ), myValue( value ) {}
787     virtual void Execute() 
788     {
789       if ( SUIT_Session::session() ) {
790         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
791         if ( !mySection.isEmpty() && !myName.isEmpty() )
792           resMgr->setValue( mySection, myName, myValue );
793       }
794     }
795   };
796   ProcessVoidEvent( new TEvent( section, name, value ) );
797 }
798
799 /*!
800   \brief Add boolean setting to the application preferences.
801   \param section resources file section name 
802   \param name setting name
803   \param value new setting value
804   \param dumb this parameter is used in order to avoid sip compilation error 
805   because of conflicting int and bool types
806 */
807 void SalomePyQt::addSetting( const QString& section, const QString& name, const bool value, const int /*dumb*/ )
808 {
809   class TEvent: public SALOME_Event 
810   {
811     QString mySection;
812     QString myName;
813     bool    myValue;
814   public:
815     TEvent( const QString& section, const QString& name, bool value ) 
816       : mySection( section ), myName( name ), myValue( value ) {}
817     virtual void Execute() 
818     {
819       if ( SUIT_Session::session() ) {
820         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
821         if ( !mySection.isEmpty() && !myName.isEmpty() )
822           resMgr->setValue( mySection, myName, myValue );
823       }
824     }
825   };
826   ProcessVoidEvent( new TEvent( section, name, value ) );
827 }
828
829 /*!
830   \brief Add string setting to the application preferences.
831   \param section resources file section name 
832   \param name setting name
833   \param value new setting value
834 */
835 void SalomePyQt::addSetting( const QString& section, const QString& name, const QString& value )
836 {
837   class TEvent: public SALOME_Event 
838   {
839     QString mySection;
840     QString myName;
841     QString myValue;
842   public:
843     TEvent( const QString& section, const QString& name, const QString& value ) 
844       : mySection( section ), myName( name ), myValue( value ) {}
845     virtual void Execute() 
846     {
847       if ( SUIT_Session::session() ) {
848         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
849         if ( !mySection.isEmpty() && !myName.isEmpty() )
850           resMgr->setValue( mySection, myName, myValue );
851       }
852     }
853   };
854   ProcessVoidEvent( new TEvent( section, name, value ) );
855 }
856
857 /*!
858   \brief Add color setting to the application preferences.
859   \param section resources file section name 
860   \param name setting name
861   \param value new setting value
862 */
863 void SalomePyQt::addSetting( const QString& section, const QString& name, const QColor& value )
864 {
865   class TEvent: public SALOME_Event 
866   {
867     QString mySection;
868     QString myName;
869     QColor  myValue;
870   public:
871     TEvent( const QString& section, const QString& name, const QColor& value ) 
872       : mySection( section ), myName( name ), myValue( value ) {}
873     virtual void Execute() 
874     {
875       if ( SUIT_Session::session() ) {
876         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
877         if ( !mySection.isEmpty() && !myName.isEmpty() )
878           resMgr->setValue( mySection, myName, myValue );
879       }
880     }
881   };
882   ProcessVoidEvent( new TEvent( section, name, value ) );
883 }
884
885 /*!
886   \fn int SalomePyQt::integerSetting( const QString& section, 
887                                       const QString& name, 
888                                       const int def );
889   \brief Get integer setting from the application preferences.
890   \param section resources file section name 
891   \param name setting name
892   \param def default value which is returned if the setting is not found
893   \return setting value
894 */
895
896 class TGetIntSettingEvent: public SALOME_Event 
897 {
898 public:
899   typedef int TResult;
900   TResult myResult;
901   QString mySection;
902   QString myName;
903   TResult myDefault;
904   TGetIntSettingEvent( const QString& section, const QString& name, const int def ) 
905     : mySection( section ), myName( name ), myDefault( def ) {}
906   virtual void Execute() 
907   {
908     if ( SUIT_Session::session() ) {
909       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
910       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->integerValue( mySection, myName, myDefault ) : myDefault;
911     }
912   }
913 };
914 int SalomePyQt::integerSetting( const QString& section, const QString& name, const int def )
915 {
916   return ProcessEvent( new TGetIntSettingEvent( section, name, def ) );
917 }
918
919 /*!
920   \fn double SalomePyQt::doubleSetting( const QString& section, 
921                                         const QString& name, 
922                                         const double def );
923   \brief Get double setting from the application preferences.
924   \param section resources file section name 
925   \param name setting name
926   \param def default value which is returned if the setting is not found
927   \return setting value
928 */
929
930 class TGetDblSettingEvent: public SALOME_Event 
931 {
932 public:
933   typedef double TResult;
934   TResult myResult;
935   QString mySection;
936   QString myName;
937   TResult myDefault;
938   TGetDblSettingEvent( const QString& section, const QString& name, const double def ) 
939     : mySection( section ), myName( name ), myDefault( def ) {}
940   virtual void Execute() 
941   {
942     if ( SUIT_Session::session() ) {
943       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
944       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->doubleValue( mySection, myName, myDefault ) : myDefault;
945     }
946   }
947 };
948 double SalomePyQt::doubleSetting( const QString& section, const QString& name, const double def )
949 {
950   return ProcessEvent( new TGetDblSettingEvent( section, name, def ) );
951 }
952
953 /*!
954   \fn bool SalomePyQt::boolSetting( const QString& section, 
955                                     const QString& name, 
956                                     const bool def );
957   \brief Get boolean setting from the application preferences.
958   \param section resources file section name 
959   \param name setting name
960   \param def default value which is returned if the setting is not found
961   \return setting value
962 */
963
964 class TGetBoolSettingEvent: public SALOME_Event 
965 {
966 public:
967   typedef bool TResult;
968   TResult myResult;
969   QString mySection;
970   QString myName;
971   TResult myDefault;
972   TGetBoolSettingEvent( const QString& section, const QString& name, const bool def ) 
973     : mySection( section ), myName( name ), myDefault( def ) {}
974   virtual void Execute() 
975   {
976     if ( SUIT_Session::session() ) {
977       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
978       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->booleanValue( mySection, myName, myDefault ) : myDefault;
979     }
980   }
981 };
982 bool SalomePyQt::boolSetting( const QString& section, const QString& name, const bool def )
983 {
984   return ProcessEvent( new TGetBoolSettingEvent( section, name, def ) );
985 }
986
987 /*!
988   \fn QString SalomePyQt::stringSetting( const QString& section, 
989                                          const QString& name, 
990                                          const QString& def );
991   \brief Get string setting from the application preferences.
992   \param section resources file section name 
993   \param name setting name
994   \param def default value which is returned if the setting is not found
995   \return setting value
996 */
997
998 class TGetStrSettingEvent: public SALOME_Event
999 {
1000 public:
1001   typedef QString TResult;
1002   TResult myResult;
1003   QString mySection;
1004   QString myName;
1005   TResult myDefault;
1006   TGetStrSettingEvent( const QString& section, const QString& name, const QString& def ) 
1007     : mySection( section ), myName( name ), myDefault( def ) {}
1008   virtual void Execute() 
1009   {
1010     if ( SUIT_Session::session() ) {
1011       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1012       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault ) : myDefault;
1013     }
1014   }
1015 };
1016 QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def )
1017 {
1018   return ProcessEvent( new TGetStrSettingEvent( section, name, def ) );
1019 }
1020
1021 /*!
1022   \fn QColor SalomePyQt::colorSetting( const QString& section, 
1023                                        const QString& name, 
1024                                        const QColor def );
1025   \brief Get color setting from the application preferences.
1026   \param section resources file section name 
1027   \param name setting name
1028   \param def default value which is returned if the setting is not found
1029   \return setting value
1030 */
1031
1032 class TGetColorSettingEvent: public SALOME_Event 
1033 {
1034 public:
1035   typedef QColor TResult;
1036   TResult myResult;
1037   QString mySection;
1038   QString myName;
1039   TResult myDefault;
1040   TGetColorSettingEvent( const QString& section, const QString& name, const QColor& def ) 
1041     : mySection( section ), myName( name ), myDefault( def ) {}
1042   virtual void Execute() 
1043   {
1044     if ( SUIT_Session::session() ) {
1045       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1046       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->colorValue( mySection, myName, myDefault ) : myDefault;
1047     }
1048   }
1049 };
1050 QColor SalomePyQt::colorSetting ( const QString& section, const QString& name, const QColor& def )
1051 {
1052   return ProcessEvent( new TGetColorSettingEvent( section, name, def ) );
1053 }
1054
1055 /*!
1056   \brief Remove setting from the application preferences.
1057   \param section resources file section name 
1058   \param name setting name
1059 */
1060 void SalomePyQt::removeSetting( const QString& section, const QString& name )
1061 {
1062   class TEvent: public SALOME_Event 
1063   {
1064     QString mySection;
1065     QString myName;
1066   public:
1067     TEvent( const QString& section, const QString& name ) : mySection( section ), myName( name ) {}
1068     virtual void Execute() 
1069     {
1070       if ( SUIT_Session::session() ) {
1071         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1072         if ( !mySection.isEmpty() && !myName.isEmpty() )
1073           resMgr->remove( mySection, myName );
1074       }
1075     }
1076   };
1077   ProcessVoidEvent( new TEvent( section, name ) );
1078 }
1079
1080 /*!
1081   \fn bool SalomePyQt::hasSetting( const QString& section, const QString& name );
1082   \brief Check setting existence in the application preferences.
1083   \param section resources file section name 
1084   \param name setting name
1085   \return \c true if setting exists
1086 */
1087
1088 class THasColorSettingEvent: public SALOME_Event 
1089 {
1090 public:
1091   typedef bool TResult;
1092   TResult myResult;
1093   QString mySection;
1094   QString myName;
1095   THasColorSettingEvent( const QString& section, const QString& name ) 
1096     : mySection( section ), myName( name ) {}
1097   virtual void Execute() 
1098   {
1099     if ( SUIT_Session::session() ) {
1100       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1101       myResult = resMgr->hasValue( mySection, myName );
1102     }
1103   }
1104 };
1105 bool SalomePyQt::hasSetting( const QString& section, const QString& name )
1106 {
1107   return ProcessEvent( new THasColorSettingEvent( section, name ) );
1108 }
1109
1110 /*!
1111   \fn QString SalomePyQt::getFileName( QWidget*           parent, 
1112                                        const QString&     initial, 
1113                                        const QStringList& filters, 
1114                                        const QString&     caption,
1115                                        bool               open );
1116   \brief Show 'Open/Save file' dialog box for file selection 
1117          and return a user's choice (selected file name).
1118   \param parent parent widget
1119   \param initial initial directory the dialog box to be opened in
1120   \param filters list of files filters (wildcards)
1121   \param caption dialog box title
1122   \param open if \c true, "Open File" dialog box is shown; 
1123          otherwise "Save File" dialog box is shown
1124   \return selected file name (null string if user cancels operation)
1125 */
1126
1127 class TGetFileNameEvent: public SALOME_Event 
1128 {
1129 public:
1130   typedef QString TResult;
1131   TResult     myResult;
1132   QWidget*    myParent;
1133   QString     myInitial;
1134   QStringList myFilters;
1135   QString     myCaption;
1136   bool        myOpen;
1137   TGetFileNameEvent( QWidget*           parent, 
1138                      const QString&     initial, 
1139                      const QStringList& filters, 
1140                      const QString&     caption,
1141                      bool               open ) 
1142     : myParent ( parent ), 
1143       myInitial( initial ), 
1144       myFilters( filters ), 
1145       myCaption( caption ), 
1146       myOpen ( open ) {}
1147   virtual void Execute() 
1148   {
1149     if ( SalomeApp_Application* anApp = getApplication() ) {
1150       myResult = anApp->getFileName( myOpen, myInitial, myFilters.join(";;"), 
1151                                      myCaption, myParent );
1152     }
1153   }
1154 };
1155 QString SalomePyQt::getFileName( QWidget*           parent, 
1156                                  const QString&     initial, 
1157                                  const QStringList& filters, 
1158                                  const QString&     caption,
1159                                  bool               open )
1160 {
1161   return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
1162 }
1163
1164 /*!
1165   \fn QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
1166                                                 const QString&     initial, 
1167                                                 const QStringList& filters, 
1168                                                 const QString&     caption );
1169   \brief Show 'Open files' dialog box for multiple files selection
1170          and return a user's choice (selected file names list).
1171   \param parent parent widget
1172   \param initial initial directory the dialog box to be opened in
1173   \param filters list of files filters (wildcards)
1174   \param caption dialog box title
1175   \return selected file names list (empty list if user cancels operation)
1176 */
1177
1178 class TGetOpenFileNamesEvent: public SALOME_Event 
1179 {
1180 public:
1181   typedef QStringList TResult;
1182   TResult     myResult;
1183   QWidget*    myParent;
1184   QString     myInitial;
1185   QStringList myFilters;
1186   QString     myCaption;
1187   TGetOpenFileNamesEvent( QWidget*           parent, 
1188                           const QString&     initial, 
1189                           const QStringList& filters, 
1190                           const QString&     caption ) 
1191     : myParent ( parent ), 
1192       myInitial( initial ), 
1193       myFilters( filters ), 
1194       myCaption( caption ) {}
1195   virtual void Execute() 
1196   {
1197     if ( SalomeApp_Application* anApp = getApplication() ) {
1198       myResult = anApp->getOpenFileNames( myInitial, myFilters.join(";;"), myCaption, myParent );
1199     }
1200   }
1201 };
1202 QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
1203                                           const QString&     initial, 
1204                                           const QStringList& filters, 
1205                                           const QString&     caption )
1206 {
1207   return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
1208 }
1209
1210 /*!
1211   \fn QString SalomePyQt::getExistingDirectory( QWidget*       parent,
1212                                                 const QString& initial,
1213                                                 const QString& caption );
1214   \brief Show 'Get Directory' dialog box for the directory selection
1215          and return a user's choice (selected directory name).
1216   \param parent parent widget
1217   \param initial initial directory the dialog box to be opened in
1218   \param caption dialog box title
1219   \return selected directory name (null string if user cancels operation)
1220 */
1221
1222 class TGetExistingDirectoryEvent: public SALOME_Event 
1223 {
1224 public:
1225   typedef QString TResult;
1226   TResult     myResult;
1227   QWidget*    myParent;
1228   QString     myInitial;
1229   QString     myCaption;
1230   TGetExistingDirectoryEvent( QWidget*           parent, 
1231                               const QString&     initial, 
1232                               const QString&     caption ) 
1233     : myParent ( parent ), 
1234       myInitial( initial ), 
1235       myCaption( caption ) {}
1236   virtual void Execute() 
1237   {
1238     if ( SalomeApp_Application* anApp = getApplication() ) {
1239       myResult = anApp->getDirectory( myInitial, myCaption, myParent );
1240     }
1241   }
1242 };
1243 QString SalomePyQt::getExistingDirectory( QWidget*       parent,
1244                                           const QString& initial,
1245                                           const QString& caption )
1246 {
1247   return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
1248 }
1249
1250 /*!
1251   \brief Open external browser to display context help information.
1252   \todo
1253
1254   Current implementation does nothing.
1255
1256   \param source documentation (HTML) file name
1257   \param context context (for example, HTML ancor name)
1258 */
1259 void SalomePyQt::helpContext( const QString& source, const QString& context ) 
1260 {
1261   class TEvent: public SALOME_Event 
1262   {
1263     QString mySource;
1264     QString myContext;
1265   public:
1266     TEvent( const QString& source, const QString& context ) 
1267       : mySource( source ), myContext( context ) {}
1268     virtual void Execute() 
1269     {
1270       if ( SalomeApp_Application* anApp = getApplication() ) {
1271         anApp->onHelpContextModule( "", mySource, myContext );
1272       }
1273     }
1274   };
1275   ProcessVoidEvent( new TEvent( source, context ) );
1276 }
1277
1278 /*!
1279   \fn bool SalomePyQt::dumpView( const QString& filename );
1280   \brief Dump the contents of the currently active view window 
1281   to the image file in the specified format.
1282
1283   For the current moment JPEG, PNG and BMP images formats are supported.
1284   The image format is defined automatically by the file name extension.
1285   By default, BMP format is used.
1286
1287   \param filename image file name
1288   \return operation status (\c true on success)
1289 */
1290
1291 class TDumpViewEvent: public SALOME_Event 
1292 {
1293 public:
1294   typedef bool TResult;
1295   TResult myResult;
1296   QString myFileName;
1297   TDumpViewEvent( const QString& filename ) 
1298     : myResult ( false ), myFileName( filename ) {}
1299   virtual void Execute() 
1300   {
1301     if ( SalomeApp_Application* anApp = getApplication() ) {
1302       SUIT_ViewManager* vm = anApp->activeViewManager();
1303       if ( vm ) { 
1304         SUIT_ViewWindow* vw = vm->getActiveView();
1305         if ( vw ) {
1306           QImage im = vw->dumpView();
1307           if ( !im.isNull() && !myFileName.isEmpty() ) {
1308             QString fmt = SUIT_Tools::extension( myFileName ).toUpper();
1309             if ( fmt.isEmpty() ) fmt = QString( "BMP" ); // default format
1310             if ( fmt == "JPG" )  fmt = "JPEG";
1311             myResult = im.save( myFileName, fmt.toLatin1() );
1312           }
1313         }
1314       }
1315     }
1316   }
1317 };
1318 bool SalomePyQt::dumpView( const QString& filename )
1319 {
1320   return ProcessEvent( new TDumpViewEvent( filename ) );
1321 }
1322
1323 /*!
1324   \fn int SalomePyQt::defaultMenuGroup();
1325   \brief Get detault menu group identifier which can be used when 
1326   creating menus (insert custom menu commands).
1327   \return default menu group ID
1328 */
1329
1330 class TDefMenuGroupEvent: public SALOME_Event 
1331 {
1332 public:
1333   typedef int TResult;
1334   TResult myResult;
1335   TDefMenuGroupEvent() : myResult( -1 ) {}
1336   virtual void Execute() 
1337   {
1338     myResult = SALOME_PYQT_Module::defaultMenuGroup();
1339   }
1340 };
1341 int SalomePyQt::defaultMenuGroup()
1342 {
1343   return ProcessEvent( new TDefMenuGroupEvent() );
1344 }
1345
1346 class CrTool
1347 {
1348 public:
1349   CrTool( const QString& tBar ) 
1350     : myCase( 0 ), myTbName( tBar ) {}
1351   CrTool( const int id, const int tBar, const int idx ) 
1352     : myCase( 1 ), myId( id ), myTbId( tBar ), myIndex( idx ) {}
1353   CrTool( const int id, const QString& tBar, const int idx )
1354     : myCase( 2 ), myId( id ), myTbName( tBar ), myIndex( idx ) {}
1355   CrTool( QAction* action, const int tbId, const int id, const int idx )
1356     : myCase( 3 ), myAction( action ), myTbId( tbId ), myId( id ), myIndex( idx ) {}
1357   CrTool( QAction* action, const QString& tBar, const int id, const int idx )
1358     : myCase( 4 ), myAction( action ), myTbName( tBar ), myId( id ), myIndex( idx ) {}
1359
1360   int execute( SALOME_PYQT_Module* module ) const
1361   {
1362     if ( module ) {
1363       switch ( myCase ) {
1364       case 0:
1365         return module->createTool( myTbName );
1366       case 1:
1367         return module->createTool( myId, myTbId, myIndex );
1368       case 2:
1369         return module->createTool( myId, myTbName, myIndex );
1370       case 3:
1371         return module->createTool( myAction, myTbId, myId, myIndex );
1372       case 4:
1373         return module->createTool( myAction, myTbName, myId, myIndex );
1374       }
1375     }
1376     return -1;
1377   }
1378 private:
1379    int        myCase;
1380    QString    myTbName;
1381    int        myTbId;
1382    QAction*   myAction;
1383    int        myId;
1384    int        myIndex;
1385 };
1386
1387 class TCreateToolEvent: public SALOME_Event 
1388 {
1389 public:
1390   typedef int TResult;
1391   TResult myResult;
1392   const CrTool& myCrTool;
1393   TCreateToolEvent( const CrTool& crTool ) 
1394     : myResult( -1 ), myCrTool( crTool ) {}
1395   virtual void Execute() 
1396   {
1397     SALOME_PYQT_Module* module = getActiveModule();
1398     if ( module )
1399       myResult = myCrTool.execute( module );
1400   }
1401 };
1402
1403 /*!
1404   \brief Create toolbar with specified name.
1405   \param tBar toolbar name
1406   \return toolbar ID or -1 if toolbar creation is failed
1407 */
1408 int SalomePyQt::createTool( const QString& tBar )
1409 {
1410   return ProcessEvent( new TCreateToolEvent( CrTool( tBar ) ) );
1411 }
1412
1413 /*! 
1414   \brief Insert action with specified \a id to the toolbar.
1415   \param id action ID
1416   \param tBar toolbar ID
1417   \param idx required index in the toolbar
1418   \return action ID or -1 if action could not be added
1419 */
1420 int SalomePyQt::createTool( const int id, const int tBar, const int idx )
1421 {
1422   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
1423 }
1424
1425 /*!
1426   \brief Insert action with specified \a id to the toolbar.
1427   \param id action ID
1428   \param tBar toolbar name
1429   \param idx required index in the toolbar
1430   \return action ID or -1 if action could not be added
1431 */
1432 int SalomePyQt::createTool( const int id, const QString& tBar, const int idx )
1433 {
1434   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
1435 }
1436
1437 /*!
1438   \brief Insert action to the toolbar.
1439   \param a action
1440   \param tBar toolbar ID
1441   \param id required action ID
1442   \param idx required index in the toolbar
1443   \return action ID or -1 if action could not be added
1444 */
1445 int SalomePyQt::createTool( QAction* a, const int tBar, const int id, const int idx )
1446 {
1447   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
1448 }
1449
1450 /*!
1451   \brief Insert action to the toolbar.
1452   \param a action
1453   \param tBar toolbar name
1454   \param id required action ID
1455   \param idx required index in the toolbar
1456   \return action ID or -1 if action could not be added
1457 */
1458 int SalomePyQt::createTool( QAction* a, const QString& tBar, const int id, const int idx )
1459 {
1460   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
1461 }
1462
1463 class CrMenu
1464 {
1465 public:
1466   CrMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx ) 
1467     : myCase( 0 ), mySubMenuName( subMenu ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1468   CrMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx ) 
1469     : myCase( 1 ), mySubMenuName( subMenu ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1470   CrMenu( const int id, const int menu, const int group, const int idx ) 
1471     : myCase( 2 ), myId( id ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {}
1472   CrMenu( const int id, const QString& menu, const int group, const int idx ) 
1473     : myCase( 3 ), myId( id ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {}
1474   CrMenu( QAction* action, const int menu, const int id, const int group, const int idx ) 
1475     : myCase( 4 ), myAction( action ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1476   CrMenu( QAction* action, const QString& menu, const int id, const int group, const int idx ) 
1477     : myCase( 5 ), myAction( action ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
1478
1479   int execute( SALOME_PYQT_Module* module ) const
1480   {
1481     if ( module ) {
1482       switch ( myCase ) {
1483       case 0:
1484         return module->createMenu( mySubMenuName, myMenuId, myId, myGroup, myIndex );
1485       case 1:
1486         return module->createMenu( mySubMenuName, myMenuName, myId, myGroup, myIndex );
1487       case 2:
1488         return module->createMenu( myId, myMenuId, myGroup, myIndex );
1489       case 3:
1490         return module->createMenu( myId, myMenuName, myGroup, myIndex );
1491       case 4:
1492         return module->createMenu( myAction, myMenuId, myId, myGroup, myIndex );
1493       case 5:
1494         return module->createMenu( myAction, myMenuName, myId, myGroup, myIndex );
1495       }
1496     }
1497     return -1;
1498   }
1499 private:
1500    int        myCase;
1501    QString    myMenuName;
1502    int        myMenuId;
1503    QString    mySubMenuName;
1504    int        myGroup;
1505    QAction*   myAction;
1506    int        myId;
1507    int        myIndex;
1508 };
1509
1510 class TCreateMenuEvent: public SALOME_Event
1511 {
1512 public:
1513   typedef int TResult;
1514   TResult myResult;
1515   const CrMenu& myCrMenu;
1516   TCreateMenuEvent( const CrMenu& crMenu ) 
1517     : myResult( -1 ), myCrMenu( crMenu ) {}
1518   virtual void Execute()
1519   {
1520     SALOME_PYQT_Module* module = getActiveModule();
1521     if ( module )
1522       myResult = myCrMenu.execute( module );
1523   }
1524 };
1525
1526 /*!
1527   \brief Create main menu.
1528   \param subMenu menu name
1529   \param menu parent menu ID
1530   \param id required menu ID
1531   \param group menu group ID
1532   \param idx required index in the menu
1533   \return menu ID or -1 if menu could not be added
1534 */
1535 int SalomePyQt::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx )
1536 {
1537   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, id, group, idx ) ) );
1538 }
1539
1540 /*!
1541   \brief Create main menu.
1542   \param subMenu menu name
1543   \param menu parent menu name (list of menu names separated by "|")
1544   \param id required menu ID
1545   \param group menu group ID
1546   \param idx required index in the menu
1547   \return menu ID or -1 if menu could not be added
1548 */
1549 int SalomePyQt::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx )
1550 {
1551   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, id, group, idx ) ) );
1552 }
1553
1554 /*!
1555   \brief Insert action to the main menu.
1556   \param id action ID
1557   \param menu parent menu ID
1558   \param group menu group ID
1559   \param idx required index in the menu
1560   \return action ID or -1 if action could not be added
1561 */
1562 int SalomePyQt::createMenu( const int id, const int menu, const int group, const int idx )
1563 {
1564   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
1565 }
1566
1567 /*!
1568   \brief Insert action to the main menu.
1569   \param id action ID
1570   \param menu parent menu name (list of menu names separated by "|")
1571   \param group menu group ID
1572   \param idx required index in the menu
1573   \return action ID or -1 if action could not be added
1574 */
1575 int SalomePyQt::createMenu( const int id, const QString& menu, const int group, const int idx )
1576 {
1577   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
1578 }
1579
1580 /*!
1581   \brief Insert action to the main menu.
1582   \param a action
1583   \param menu parent menu ID
1584   \param group menu group ID
1585   \param idx required index in the menu
1586   \return action ID or -1 if action could not be added
1587 */
1588 int SalomePyQt::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
1589 {
1590   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
1591 }
1592
1593 /*!
1594   \brief Insert action to the main menu.
1595   \param a action
1596   \param menu parent menu name (list of menu names separated by "|")
1597   \param group menu group ID
1598   \param idx required index in the menu
1599   \return action ID or -1 if action could not be added
1600 */
1601 int SalomePyQt::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
1602 {
1603   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
1604 }
1605
1606 /*!
1607   \fn QAction* SalomePyQt::createSeparator();
1608   \brief Create separator action which can be used in the menu or toolbar.
1609   \return new separator action
1610 */
1611
1612 class TCreateSepEvent: public SALOME_Event 
1613 {
1614 public:
1615   typedef QAction* TResult;
1616   TResult myResult;
1617   TCreateSepEvent() 
1618     : myResult( 0 ) {}
1619   virtual void Execute() 
1620   {
1621     SALOME_PYQT_Module* module = getActiveModule();
1622     if ( module )
1623       myResult = (QAction*)module->separator();
1624   }
1625 };
1626 QAction* SalomePyQt::createSeparator()
1627 {
1628   return ProcessEvent( new TCreateSepEvent() );
1629 }
1630
1631 /*!
1632   \fn QAction* SalomePyQt::createAction( const int      id,
1633                                            const QString& menuText, 
1634                                            const QString& tipText, 
1635                                            const QString& statusText, 
1636                                            const QString& icon,
1637                                            const int      key, 
1638                                            const bool     toggle )
1639   \brief Create an action which can be then used in the menu or toolbar.
1640   \param id the unique id action to be registered to
1641   \param menuText action text which should appear in menu
1642   \param tipText text which should appear in the tooltip
1643   \param statusText text which should appear in the status bar when action is activated
1644   \param icon the name of the icon file (the actual icon file name can be coded in the translation files)
1645   \param key the key accelrator for the action
1646   \param toggle if \c true the action is checkable
1647 */
1648
1649 class TCreateActionEvent: public SALOME_Event 
1650 {
1651 public:
1652   typedef QAction* TResult;
1653   TResult myResult;
1654   int     myId;
1655   QString myMenuText;
1656   QString myTipText;
1657   QString myStatusText;
1658   QString myIcon;
1659   int     myKey;
1660   bool    myToggle;
1661   TCreateActionEvent( const int id, const QString& menuText, const QString& tipText, 
1662                       const QString& statusText, const QString& icon, const int key, const bool toggle ) 
1663     : myResult( 0 ), myId( id ), myMenuText( menuText ), myTipText( tipText ),
1664       myStatusText( statusText ), myIcon( icon ), myKey( key ), myToggle( toggle ) {}
1665   virtual void Execute()
1666   {
1667     SALOME_PYQT_Module* module = getActiveModule();
1668     if ( module )
1669       myResult = (QAction*)module->createAction( myId, myTipText, myIcon, myMenuText, myStatusText, myKey, myToggle );
1670   }
1671 };
1672 QAction* SalomePyQt::createAction( const int id,           const QString& menuText, 
1673                                      const QString& tipText, const QString& statusText, 
1674                                      const QString& icon,    const int key, const bool toggle )
1675 {
1676   return ProcessEvent( new TCreateActionEvent( id, menuText, tipText, statusText, icon, key, toggle ) );
1677 }
1678
1679 /*!
1680   \fn QtxActionGroup* SalomePyQt::createActionGroup( const int id, const bool exclusive )
1681   \brief Create an action group which can be then used in the menu or toolbar
1682   \param id         : the unique id action group to be registered to
1683   \param exclusive  : if \c true the action group does exclusive toggling
1684 */
1685
1686 struct TcreateActionGroupEvent: public SALOME_Event {
1687   typedef QtxActionGroup* TResult;
1688   TResult myResult;
1689   int     myId;
1690   bool    myExclusive;
1691   TcreateActionGroupEvent( const int id, const bool exclusive )
1692     : myId( id ), myExclusive( exclusive ) {}
1693   virtual void Execute()
1694   {
1695     SALOME_PYQT_Module* module = getActiveModule();
1696     if ( module )
1697       myResult = module->createActionGroup( myId, myExclusive );
1698   }
1699 };
1700 QtxActionGroup* SalomePyQt::createActionGroup(const int id, const bool exclusive)
1701 {
1702   return ProcessEvent( new TcreateActionGroupEvent( id, exclusive ) );
1703 }
1704
1705 /*!
1706   \fn QAction* SalomePyQt::action( const int id )
1707   \brief Get action by specified identifier.
1708   \return action or 0 if action is not registered
1709 */
1710
1711 class TActionEvent: public SALOME_Event 
1712 {
1713 public:
1714   typedef QAction* TResult;
1715   TResult myResult;
1716   int     myId;
1717   TActionEvent( const int id )
1718     : myResult( 0 ), myId( id ) {}
1719   virtual void Execute()
1720   {
1721     SALOME_PYQT_Module* module = getActiveModule();
1722     if ( module )
1723       myResult = (QAction*)module->action( myId );
1724   }
1725 };
1726 QAction* SalomePyQt::action( const int id )
1727 {
1728   return ProcessEvent( new TActionEvent( id ) );
1729 }
1730
1731 /*!
1732   \fn int SalomePyQt::actionId( const QAction* a );
1733   \brief Get an action identifier. 
1734   \return action ID or -1 if action is not registered
1735 */
1736
1737 class TActionIdEvent: public SALOME_Event 
1738 {
1739 public:
1740   typedef  int TResult;
1741   TResult  myResult;
1742   const QAction* myAction;
1743   TActionIdEvent( const QAction* action )
1744     : myResult( -1 ), myAction( action ) {}
1745   virtual void Execute()
1746   {
1747     SALOME_PYQT_Module* module = getActiveModule();
1748     if ( module )
1749       myResult = module->actionId( myAction );
1750   }
1751 };
1752 int SalomePyQt::actionId( const QAction* a )
1753 {
1754   return ProcessEvent( new TActionIdEvent( a ) );
1755 }
1756
1757 /*!
1758   \fn int SalomePyQt::addGlobalPreference( const QString& label );
1759   \brief Add global (not module-related) preferences group.
1760   \param label global preferences group name
1761   \return preferences group identifier
1762 */
1763
1764 class TAddGlobalPrefEvent: public SALOME_Event
1765 {
1766 public:
1767   typedef int TResult;
1768   TResult myResult;
1769   QString myLabel;
1770   TAddGlobalPrefEvent( const QString& label )
1771     : myResult( -1 ), myLabel( label ) {}
1772   virtual void Execute() 
1773   {
1774     SALOME_PYQT_Module* module = getActiveModule();
1775     if ( module )
1776       myResult = module->addGlobalPreference( myLabel );
1777   }
1778 };
1779 int SalomePyQt::addGlobalPreference( const QString& label )
1780 {
1781   return ProcessEvent( new TAddGlobalPrefEvent( label ) );
1782 }
1783
1784 /*!
1785   \fn int SalomePyQt::addPreference( const QString& label );
1786   \brief Add module-related preferences group.
1787   \param label preferences group name
1788   \return preferences group identifier
1789 */
1790
1791 class TAddPrefEvent: public SALOME_Event 
1792 {
1793 public:
1794   typedef int TResult;
1795   TResult myResult;
1796   QString myLabel;
1797   TAddPrefEvent( const QString& label )
1798     : myResult( -1 ), myLabel( label ) {}
1799   virtual void Execute() 
1800   {
1801     SALOME_PYQT_Module* module = getActiveModule();
1802     if ( module )
1803       myResult = module->addPreference( myLabel );
1804   }
1805 };
1806 int SalomePyQt::addPreference( const QString& label )
1807 {
1808   return ProcessEvent( new TAddPrefEvent( label ) );
1809 }
1810
1811 /*!
1812   \fn int SalomePyQt::addPreference( const QString& label, const int pId, const int type,
1813                                      const QString& section, const QString& param );
1814   \brief Add module-related preferences.
1815   \param label preferences group name
1816   \param pId parent preferences group id
1817   \param type preferences type
1818   \param section resources file section name
1819   \param param resources file setting name
1820   \return preferences identifier
1821 */
1822
1823 class TAddPrefParamEvent: public SALOME_Event
1824 {
1825 public:
1826   typedef int TResult;
1827   TResult myResult;
1828   QString myLabel;
1829   int     myPId;
1830   int     myType;
1831   QString mySection;
1832   QString myParam;
1833   TAddPrefParamEvent( const QString& label, 
1834                       const int pId, const int type,
1835                       const QString& section, 
1836                       const QString& param )
1837     : myResult( -1 ),
1838       myLabel( label ), myPId( pId ), myType( type ), 
1839       mySection( section ), myParam ( param ) {}
1840   virtual void Execute()
1841   {
1842     SALOME_PYQT_Module* module = getActiveModule();
1843     if ( module )
1844       myResult = module->addPreference( myLabel, myPId, myType, mySection, myParam );
1845   }
1846 };
1847 int SalomePyQt::addPreference( const QString& label, const int pId, const int type,
1848                                const QString& section, const QString& param )
1849 {
1850   return ProcessEvent( new TAddPrefParamEvent( label, pId, type, section, param ) );
1851 }
1852
1853 /*!
1854   \fn QVariant SalomePyQt::preferenceProperty( const int id, const QString& prop );
1855   \brief Get the preferences property.
1856   \param id preferences identifier
1857   \param prop preferences property name
1858   \return preferences property value or null QVariant if property is not set
1859 */
1860
1861 class TPrefPropEvent: public SALOME_Event
1862 {
1863 public:
1864   typedef QVariant TResult;
1865   TResult myResult;
1866   int     myId;
1867   QString myProp;
1868   TPrefPropEvent( const int id, const QString& prop )
1869     : myId( id ), myProp( prop ) {}
1870   virtual void Execute()
1871   {
1872     SALOME_PYQT_Module* module = getActiveModule();
1873     if ( module )
1874       myResult = module->preferenceProperty( myId, myProp );
1875   }
1876 };
1877 QVariant SalomePyQt::preferenceProperty( const int id, const QString& prop )
1878 {
1879   return ProcessEvent( new TPrefPropEvent( id, prop ) );
1880 }
1881
1882 /*!
1883   \brief Set the preferences property.
1884   \param id preferences identifier
1885   \param prop preferences property name
1886   \param var preferences property value
1887 */
1888 void SalomePyQt::setPreferenceProperty( const int id, 
1889                                         const QString& prop,
1890                                         const QVariant& var )
1891 {
1892   class TEvent: public SALOME_Event
1893   {
1894     int      myId;
1895     QString  myProp;
1896     QVariant myVar;
1897   public:
1898     TEvent( const int id, const QString& prop, const QVariant& var ) 
1899       : myId( id ), myProp( prop ), myVar( var ) {}
1900     virtual void Execute() 
1901     {
1902       SALOME_PYQT_Module* module = getActiveModule();
1903       if ( module )
1904         module->setPreferenceProperty( myId, myProp, myVar );
1905     }
1906   };
1907   ProcessVoidEvent( new TEvent( id, prop, var) );
1908 }
1909
1910 /*!
1911   \brief Add the property value to the list of values.
1912
1913   This method allows creating properties which are QList<QVariant>
1914   - there is no way to pass such values directly to QVariant parameter with PyQt.
1915
1916   \param id preferences identifier
1917   \param prop preferences property name
1918   \param idx preferences property index
1919   \param var preferences property value for the index \a idx
1920 */
1921 void SalomePyQt::addPreferenceProperty( const int id, 
1922                                         const QString& prop,
1923                                         const int idx, 
1924                                         const QVariant& var )
1925 {
1926   class TEvent: public SALOME_Event
1927   {
1928     int      myId;
1929     QString  myProp;
1930     int      myIdx;
1931     QVariant myVar;
1932   public:
1933     TEvent( const int id, const QString& prop, const int idx, const QVariant& var ) 
1934       : myId( id ), myProp( prop ), myIdx( idx), myVar( var ) {}
1935     virtual void Execute()
1936     {
1937       SALOME_PYQT_Module* module = getActiveModule();
1938       if ( module ) {
1939         QVariant var =  module->preferenceProperty( myId, myProp );
1940         if ( var.isValid() ) {
1941           if ( var.type() == QVariant::StringList ) {
1942             QStringList sl = var.toStringList();
1943             if ( myIdx >= 0 && myIdx < sl.count() ) 
1944               sl[myIdx] = myVar.toString();
1945             else
1946               sl.append( myVar.toString() );
1947             module->setPreferenceProperty( myId, myProp, sl );
1948           }
1949           else if ( var.type() == QVariant::List ) {
1950             QList<QVariant> vl = var.toList();
1951             if ( myIdx >= 0 && myIdx < vl.count() ) 
1952               vl[myIdx] = myVar;
1953             else
1954               vl.append( myVar );
1955             module->setPreferenceProperty( myId, myProp, vl );
1956           }
1957         }
1958         else {
1959           QList<QVariant> vl;
1960           vl.append( myVar );
1961           module->setPreferenceProperty( myId, myProp, vl );
1962         }
1963       }
1964     }
1965   };
1966   ProcessVoidEvent( new TEvent( id, prop, idx, var) );
1967 }
1968
1969 /*!
1970   \brief Put the message to the Log messages output window
1971   \param msg message text (it can be of simple rich text format)
1972   \param addSeparator boolean flag which specifies if it is necessary 
1973          to separate the message with predefined separator
1974 */
1975 void SalomePyQt::message( const QString& msg, bool addSeparator )
1976 {
1977   class TEvent: public SALOME_Event
1978   {
1979     QString  myMsg;
1980     bool     myAddSep;
1981   public:
1982     TEvent( const QString& msg, bool addSeparator ) 
1983       : myMsg( msg ), myAddSep( addSeparator ) {}
1984     virtual void Execute()
1985     {
1986       if ( SalomeApp_Application* anApp = getApplication() ) {
1987         LogWindow* lw = anApp->logWindow();
1988         if ( lw )
1989           lw->putMessage( myMsg, myAddSep );
1990       }
1991     }
1992   };
1993   ProcessVoidEvent( new TEvent( msg, addSeparator ) );
1994 }
1995
1996 /*!
1997   \brief Remove all the messages from the Log messages output window.
1998 */
1999 void SalomePyQt::clearMessages()
2000 {
2001   class TEvent: public SALOME_Event
2002   {
2003   public:
2004     TEvent() {}
2005     virtual void Execute()
2006     {
2007       if ( SalomeApp_Application* anApp = getApplication() ) {
2008         LogWindow* lw = anApp->logWindow();
2009         if ( lw )
2010           lw->clear();
2011       }
2012     }
2013   };
2014   ProcessVoidEvent( new TEvent() );
2015 }
2016
2017 /*!
2018   \brief Gets window with specified identifier 
2019   \internal
2020   \param id window identifier 
2021   \return pointer on the window
2022 */
2023 static SUIT_ViewWindow* getWnd( const int id )
2024 {
2025   SUIT_ViewWindow* resWnd = 0;
2026
2027   SalomeApp_Application* app  = getApplication();
2028   if ( app )
2029   {
2030     STD_TabDesktop* tabDesk = dynamic_cast<STD_TabDesktop*>( app->desktop() );
2031     if ( tabDesk )
2032     {
2033       QList<SUIT_ViewWindow*> wndlist = tabDesk->windows();
2034       SUIT_ViewWindow* wnd;
2035       foreach ( wnd, wndlist )
2036       {
2037         if ( id == wnd->getId() )
2038         {
2039           resWnd = wnd;
2040           break;
2041         }
2042       }
2043     }
2044   }
2045
2046   return resWnd;
2047 }
2048
2049 /*!
2050   \fn QList<int> SalomePyQt::getViews()
2051   \brief Get list of integer identifiers of all the currently opened views
2052   \return list of integer identifiers of all the currently opened views
2053 */
2054
2055 class TGetViews: public SALOME_Event
2056 {
2057 public:
2058   typedef QList<int> TResult;
2059   TResult myResult;
2060   TGetViews() {}
2061   virtual void Execute() 
2062   {
2063     myResult.clear();
2064     SalomeApp_Application* app  = getApplication();
2065     if ( app )
2066     {
2067       STD_TabDesktop* tabDesk = dynamic_cast<STD_TabDesktop*>( app->desktop() );
2068       if ( tabDesk )
2069       {
2070         QList<SUIT_ViewWindow*> wndlist = tabDesk->windows();
2071         SUIT_ViewWindow* wnd;
2072         foreach ( wnd, wndlist )
2073           myResult.append( wnd->getId() );
2074       }
2075     }
2076   }
2077 };
2078 QList<int> SalomePyQt::getViews()
2079 {
2080   return ProcessEvent( new TGetViews() );
2081 }
2082
2083 /*!
2084   \fn int SalomePyQt::getActiveView()
2085   \brief Get integer identifier of the currently active view
2086   \return integer identifier of the currently active view
2087 */
2088
2089 class TGetActiveView: public SALOME_Event
2090 {
2091 public:
2092   typedef int TResult;
2093   TResult myResult;
2094   TGetActiveView()
2095     : myResult( -1 ) {}
2096   virtual void Execute() 
2097   {
2098     SalomeApp_Application* app = getApplication();
2099     if ( app )
2100     {
2101       SUIT_ViewManager* viewMgr = app->activeViewManager();
2102       if ( viewMgr )
2103       {
2104         SUIT_ViewWindow* wnd = viewMgr->getActiveView();
2105         if ( wnd )
2106           myResult = wnd->getId();
2107       }
2108     }
2109   }
2110 };
2111 int SalomePyQt::getActiveView()
2112 {
2113   return ProcessEvent( new TGetActiveView() );
2114 }
2115
2116 /*!                      
2117   \fn QString SalomePyQt::getViewType( const int id )
2118   \brief Get type of the specified view, e.g. "OCCViewer"
2119   \param id window identifier
2120   \return view type
2121 */ 
2122
2123 class TGetViewType: public SALOME_Event
2124 {
2125 public:
2126   typedef QString TResult;
2127   TResult myResult;
2128   int myWndId;
2129   TGetViewType( const int id )
2130     : myWndId( id ) {}
2131   virtual void Execute() 
2132   {
2133     SUIT_ViewWindow* wnd = getWnd( myWndId );
2134     if ( wnd )
2135     {
2136       SUIT_ViewManager* viewMgr = wnd->getViewManager();
2137       if ( viewMgr )
2138         myResult = viewMgr->getType();
2139     }
2140   }
2141 };
2142 QString SalomePyQt::getViewType( const int id )
2143 {
2144   return ProcessEvent( new TGetViewType( id ) );
2145 }
2146
2147 /*!
2148   \fn bool SalomePyQt::setViewTitle( const int id, const QString& title )
2149   \brief Change view caption  
2150   \param id window identifier
2151   \param title new window title
2152   \return \c true if operation is completed successfully and \c false otherwise 
2153 */
2154
2155 class TSetViewTitle: public SALOME_Event
2156 {
2157 public:
2158   typedef bool TResult;
2159   TResult myResult;
2160   int myWndId;
2161   QString myTitle;
2162   TSetViewTitle( const int id, const QString& title )
2163     : myResult( false ),
2164       myWndId( id ),
2165       myTitle( title ) {}
2166   virtual void Execute() 
2167   {
2168     SUIT_ViewWindow* wnd = getWnd( myWndId );
2169     if ( wnd )
2170     {
2171       wnd->setWindowTitle( myTitle );
2172       myResult = true;
2173     }
2174   }
2175 };
2176 bool SalomePyQt::setViewTitle( const int id, const QString& title )
2177 {
2178   return ProcessEvent( new TSetViewTitle( id, title ) );
2179 }
2180
2181
2182 /*!
2183   \fn QString SalomePyQt::getViewTitle( const int id )
2184   \brief Get view caption  
2185   \param id window identifier
2186   \return view caption  
2187 */
2188
2189 class TGetViewTitle: public SALOME_Event
2190 {
2191 public:
2192   typedef QString TResult;
2193   TResult myResult;
2194   int myWndId;
2195   TGetViewTitle( const int id )
2196     : myWndId( id ) {}
2197   virtual void Execute() 
2198   {
2199     SUIT_ViewWindow* wnd = getWnd( myWndId );
2200     if ( wnd )
2201       myResult = wnd->windowTitle();
2202   }
2203 };
2204 QString SalomePyQt::getViewTitle( const int id )
2205 {
2206   return ProcessEvent( new TGetViewTitle( id ) );
2207 }
2208
2209 /*!
2210   \fn QList<int> SalomePyQt::findViews( const QString& type )
2211   \brief Get list of integer identifiers of all the 
2212          currently opened views of the specified type
2213   \param type viewer type
2214   \return list of integer identifiers 
2215 */
2216
2217 class TFindViews: public SALOME_Event
2218 {
2219 public:
2220   typedef QList<int> TResult;
2221   TResult myResult;
2222   QString myType;
2223   TFindViews( const QString& type )
2224     : myType( type ) {}
2225   virtual void Execute() 
2226   {
2227     myResult.clear();
2228     SalomeApp_Application* app  = getApplication();
2229     if ( app )
2230     {
2231       ViewManagerList vmList;
2232       app->viewManagers( myType, vmList );
2233       SUIT_ViewManager* viewMgr;
2234       foreach ( viewMgr, vmList )
2235       {
2236         QVector<SUIT_ViewWindow*> vec = viewMgr->getViews();
2237         for ( int i = 0, n = vec.size(); i < n; i++ )
2238         {
2239           SUIT_ViewWindow* wnd = vec[ i ];
2240           if ( wnd )
2241             myResult.append( wnd->getId() );
2242         }
2243       }
2244     }
2245   }
2246 };
2247 QList<int> SalomePyQt::findViews( const QString& type )
2248 {
2249   return ProcessEvent( new TFindViews( type ) );
2250 }
2251
2252 /*!
2253   \fn bool SalomePyQt::activateView( const int id )
2254   \brief Activate view
2255   \param id window identifier
2256   \return \c true if operation is completed successfully and \c false otherwise 
2257 */
2258
2259 class TActivateView: public SALOME_Event
2260 {
2261 public:
2262   typedef bool TResult;
2263   TResult myResult;
2264   int myWndId;
2265   TActivateView( const int id )
2266     : myResult( false ),
2267       myWndId( id ) {}
2268   virtual void Execute() 
2269   {
2270     SUIT_ViewWindow* wnd = getWnd( myWndId );
2271     if ( wnd )
2272     {
2273       wnd->setFocus();
2274       myResult = true;
2275     }
2276   }
2277 };
2278 bool SalomePyQt::activateView( const int id )
2279 {
2280   return ProcessEvent( new TActivateView( id ) );
2281 }
2282
2283 /*!
2284   \fn int SalomePyQt::createView( const QString& type )
2285   \brief Create new view and activate it
2286   \param type viewer type
2287   \return integer identifier of created view (or -1 if view could not be created)
2288 */
2289
2290 class TCreateView: public SALOME_Event
2291 {
2292 public:
2293   typedef int TResult;
2294   TResult myResult;
2295   QString myType;
2296   TCreateView( const QString& theType )
2297     : myResult( -1 ),
2298       myType( theType ) {}
2299   virtual void Execute() 
2300   {
2301     SalomeApp_Application* app  = getApplication();
2302     if ( app )
2303     {
2304       SUIT_ViewManager* viewMgr = app->createViewManager( myType );
2305       if ( viewMgr )
2306       {
2307         SUIT_ViewWindow* wnd = viewMgr->getActiveView();
2308         if ( wnd )
2309           myResult = wnd->getId();
2310       }
2311     }
2312   }
2313 };
2314 int SalomePyQt::createView( const QString& type )
2315 {
2316   return ProcessEvent( new TCreateView( type ) );
2317 }
2318
2319 /*!
2320   \fn bool SalomePyQt::closeView( const int id )
2321   \brief Close view
2322   \param id window identifier
2323   \return \c true if operation is completed successfully and \c false otherwise 
2324 */
2325
2326 class TCloseView: public SALOME_Event
2327 {
2328 public:
2329   typedef bool TResult;
2330   TResult myResult;
2331   int myWndId;
2332   TCloseView( const int id )
2333     : myResult( false ),
2334       myWndId( id ) {}
2335   virtual void Execute() 
2336   {
2337     SUIT_ViewWindow* wnd = getWnd( myWndId );
2338     if ( wnd )
2339     {
2340       SUIT_ViewManager* viewMgr = wnd->getViewManager();
2341       if ( viewMgr )
2342       {
2343         wnd->close();
2344         myResult = true;
2345       }
2346     }
2347   }
2348 };
2349 bool SalomePyQt::closeView( const int id )
2350 {
2351   return ProcessEvent( new TCloseView( id ) );
2352 }
2353
2354 /*!
2355   \fn int SalomePyQt::cloneView( const int id )
2356   \brief Clone view (if this operation is supported for specified view type)
2357   \param id window identifier
2358   \return integer identifier of the cloned view or -1 or operation could not be performed
2359 */
2360
2361 class TCloneView: public SALOME_Event
2362 {
2363 public:
2364   typedef int TResult;
2365   TResult myResult;
2366   int myWndId;
2367   TCloneView( const int id )
2368     : myResult( -1 ),
2369       myWndId( id ) {}
2370   virtual void Execute() 
2371   {
2372     SUIT_ViewWindow* wnd = getWnd( myWndId );
2373     if ( wnd )
2374     {
2375       SUIT_ViewManager* viewMgr = wnd->getViewManager();
2376       if ( viewMgr )
2377       {
2378         if ( wnd->inherits( "OCCViewer_ViewWindow" ) )
2379         {
2380           OCCViewer_ViewWindow* occView = (OCCViewer_ViewWindow*)( wnd );
2381           occView->onCloneView();
2382
2383           wnd = viewMgr->getActiveView();
2384           if ( wnd )
2385             myResult = wnd->getId();
2386         }
2387         else if ( wnd->inherits( "Plot2d_ViewWindow" ) ) 
2388         {
2389           Plot2d_ViewManager* viewMgr2d = dynamic_cast<Plot2d_ViewManager*>( viewMgr );
2390           Plot2d_ViewWindow* srcWnd2d = dynamic_cast<Plot2d_ViewWindow*>( wnd );
2391           if ( viewMgr2d && srcWnd2d )
2392           {
2393             Plot2d_ViewWindow* resWnd = viewMgr2d->cloneView( srcWnd2d );
2394             myResult = resWnd->getId();
2395           }
2396         }
2397       }
2398     }
2399   }
2400 };
2401 int SalomePyQt::cloneView( const int id )
2402 {
2403   return ProcessEvent( new TCloneView( id ) );
2404 }
2405
2406 /*!
2407   \fn bool SalomePyQt::isViewVisible( const int id )
2408   \brief Check whether view is visible ( i.e. it is on the top of the views stack)
2409   \param id window identifier
2410   \return \c true if view is visible and \c false otherwise 
2411 */
2412
2413 class TIsViewVisible: public SALOME_Event
2414 {
2415 public:
2416   typedef bool TResult;
2417   TResult myResult;
2418   int myWndId;
2419   TIsViewVisible( const int id )
2420     : myResult( false ),
2421       myWndId( id ) {}
2422   virtual void Execute() 
2423   {
2424     SUIT_ViewWindow* wnd = getWnd( myWndId );
2425     if ( wnd )
2426     {
2427       QWidget* p = wnd->parentWidget();
2428       myResult = ( p && p->isVisibleTo( p->parentWidget() ) );
2429     }
2430   }
2431 };
2432 bool SalomePyQt::isViewVisible( const int id )
2433 {
2434   return ProcessEvent( new TIsViewVisible( id ) );
2435 }
2436   
2437 /*!
2438   \fn bool SalomePyQt::groupAllViews()
2439   \brief Group all views to the single tab area
2440   \return \c true if operation is completed successfully and \c false otherwise 
2441 */
2442
2443 class TGroupAllViews: public SALOME_Event
2444 {
2445 public:
2446   typedef bool TResult;
2447   TResult myResult;
2448   TGroupAllViews()
2449     : myResult( false ) {}
2450   virtual void Execute() 
2451   {
2452     SalomeApp_Application* app  = getApplication();
2453     if ( app )
2454     {
2455       STD_TabDesktop* tabDesk = dynamic_cast<STD_TabDesktop*>( app->desktop() );
2456       if ( tabDesk )
2457       {
2458         QtxWorkstack* wStack = tabDesk->workstack();
2459         if ( wStack )
2460         {
2461           wStack->stack();
2462           myResult = true;
2463         }
2464       }
2465     }
2466   }
2467 };
2468 bool SalomePyQt::groupAllViews()
2469 {
2470   return ProcessEvent( new TGroupAllViews() );
2471 }
2472
2473 /*!
2474   \fn bool SalomePyQt::splitView( const int id, const Orientation ori, const Action action )
2475   \brief Split tab area to which view with identifier belongs to
2476   \param id window identifier
2477   \param ori orientation of split operation
2478   \param action action to be performed
2479   \return \c true if operation is completed successfully \c false otherwise 
2480 */
2481
2482 class TSplitView: public SALOME_Event
2483 {
2484 public:
2485   typedef bool TResult;
2486   TResult myResult;
2487   int myWndId;
2488   Orientation myOri;
2489   Action myAction;
2490   TSplitView( const int id, 
2491               const Orientation ori, 
2492               const Action action )
2493     : myResult( false ),
2494       myWndId( id ),
2495       myOri( ori ),
2496       myAction( action ) {}
2497   virtual void Execute() 
2498   {
2499     SUIT_ViewWindow* wnd = getWnd( myWndId );
2500     if ( wnd )
2501     {
2502       // activate view
2503       // wnd->setFocus(); ???
2504
2505       // split workstack
2506       if ( getApplication() )
2507       {
2508         STD_TabDesktop* desk = 
2509           dynamic_cast<STD_TabDesktop*>( getApplication()->desktop() );
2510         if ( desk )
2511         {
2512           QtxWorkstack* wStack = desk->workstack();
2513           if ( wStack )
2514           {
2515             Qt::Orientation qtOri = 
2516               ( myOri == Horizontal ) ? Qt::Horizontal : Qt::Vertical;
2517
2518             QtxWorkstack::SplitType sType;
2519             if ( myAction == MoveWidget )
2520               sType = QtxWorkstack::SplitMove;
2521             else if ( myAction == LeaveWidget )
2522               sType = QtxWorkstack::SplitStay;
2523             else 
2524               sType = QtxWorkstack::SplitAt;
2525
2526             wStack->Split( wnd, qtOri, sType );
2527             myResult = true;
2528           }
2529         }
2530       }
2531     }
2532   }
2533 };
2534 bool SalomePyQt::splitView( const int id, const Orientation ori, const Action action )
2535 {
2536   return ProcessEvent( new TSplitView( id, ori, action ) );
2537 }
2538
2539 /*!
2540   \fn bool SalomePyQt::moveView( const int id, const int id_to, const bool before )
2541   \brief Move view with the first identifier to the same area which 
2542          another view with the second identifier belongs to
2543   \param id source window identifier
2544   \param id_to destination window identifier  
2545   param before specifies whether the first viewt has to be moved before or after 
2546         the second view
2547   \return \c true if operation is completed successfully and \c false otherwise 
2548 */
2549
2550 class TMoveView: public SALOME_Event
2551 {
2552 public:
2553   typedef bool TResult;
2554   TResult myResult;
2555   int myWndId;
2556   int myWndToId;
2557   bool myIsBefore;
2558   TMoveView( const int id, const int id_to, const bool before )
2559     : myResult( false ),
2560     myWndId( id ),
2561     myWndToId( id_to ),
2562     myIsBefore( before ) {}
2563   virtual void Execute() 
2564   {
2565     SUIT_ViewWindow* wnd = getWnd( myWndId );
2566     SUIT_ViewWindow* wnd_to = getWnd( myWndToId );
2567     if ( wnd && wnd_to )
2568     {
2569       QtxWorkstack* wStack = dynamic_cast<STD_TabDesktop*>( 
2570         getApplication()->desktop() )->workstack();
2571       if ( wStack )
2572         myResult = wStack->move( wnd, wnd_to, myIsBefore );
2573     }
2574   }
2575 };
2576 bool SalomePyQt::moveView( const int id, const int id_to, const bool before )
2577 {
2578   return ProcessEvent( new TMoveView( id, id_to, before ) );
2579 }
2580
2581 /*!
2582   \fn QList<int> SalomePyQt::neighbourViews( const int id )
2583   \brief Get list of views identifiers that belongs to the same area as 
2584          specified view (excluding it)
2585   \param id window identifier
2586   \return list of views identifiers
2587 */
2588
2589 class TNeighbourViews: public SALOME_Event
2590 {
2591 public:
2592   typedef QList<int> TResult;
2593   TResult myResult;
2594   int myWndId;
2595   TNeighbourViews( const int id )
2596     : myWndId( id ) {}
2597   virtual void Execute() 
2598   {
2599     myResult.clear();
2600     SUIT_ViewWindow* wnd = getWnd( myWndId );
2601     if ( wnd )
2602     {
2603       QtxWorkstack* wStack = dynamic_cast<STD_TabDesktop*>( 
2604         getApplication()->desktop() )->workstack();
2605       if ( wStack )
2606       {
2607         QWidgetList wgList = wStack->windowList( wnd );
2608         QWidget* wg;
2609         foreach ( wg, wgList )
2610         {
2611           SUIT_ViewWindow* tmpWnd = dynamic_cast<SUIT_ViewWindow*>( wg );
2612           if ( tmpWnd && tmpWnd != wnd )
2613             myResult.append( tmpWnd->getId() );
2614         }
2615       }
2616     }
2617   }
2618 };
2619 QList<int> SalomePyQt::neighbourViews( const int id )
2620 {
2621   return ProcessEvent( new TNeighbourViews( id ) );
2622 }