]> SALOME platform Git repositories - modules/gui.git/blob - src/SALOME_PYQT/SalomePyQt/SalomePyQt.cxx
Salome HOME
45deaf3529588560967e318e32116d07c0f47e5f
[modules/gui.git] / src / SALOME_PYQT / SalomePyQt / SalomePyQt.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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
23 // File   : SalomePyQt.cxx
24 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
25
26 #ifdef WIN32
27 // E.A. : On windows with python 2.6, there is a conflict
28 // E.A. : between pymath.h and Standard_math.h which define
29 // E.A. : some same symbols : acosh, asinh, ...
30 #include <Standard_math.hxx>
31 #include <pymath.h>
32 #endif
33
34 #include "SALOME_PYQT_ModuleLight.h" // this include must be first!!!
35 #include "SALOME_PYQT_DataModelLight.h"
36 #include "SALOME_PYQT_PyModule.h"
37 #include "SalomePyQt.h"
38
39 #include "LightApp_SelectionMgr.h"
40 #include "LogWindow.h"
41 #ifndef DISABLE_OCCVIEWER
42 #include "OCCViewer_ViewWindow.h"
43 #include "OCCViewer_ViewFrame.h"
44 #endif // DISABLE_OCCVIEWER
45 #ifndef DISABLE_VTKVIEWER
46 #include "SVTK_ViewWindow.h"
47 #endif // DISABLE_VTKVIEWER
48 #ifndef DISABLE_PLOT2DVIEWER
49 #include "Plot2d_ViewManager.h"
50 #include "Plot2d_ViewWindow.h"
51 #endif // DISABLE_PLOT2DVIEWER
52 #ifndef DISABLE_PVVIEWER
53 #include "PVViewer_ViewManager.h"
54 #include "PVViewer_ViewModel.h"
55 #endif // DISABLE_PVVIEWER
56 #include "QtxActionMenuMgr.h"
57 #include "QtxWorkstack.h"
58 #include "QtxTreeView.h"
59 #include "SALOME_Event.h"
60 #include "STD_TabDesktop.h"
61 #include "SUIT_DataBrowser.h"
62 #include "SUIT_ResourceMgr.h"
63 #include "SUIT_Session.h"
64 #include "SUIT_Tools.h"
65 #include "SUIT_ViewManager.h"
66 #include "SUIT_ViewWindow.h"
67 #include "PyConsole_Console.h"
68
69 #include <QAction>
70 #include <QApplication>
71 #include <QPaintEvent>
72 #include <QCoreApplication>
73 #include <QVBoxLayout>
74
75 #include <utilities.h>
76
77 namespace
78 {
79   /*!
80     \brief Get the currently active application.
81     \internal
82     \return active application object or 0 if there is no any
83   */
84   LightApp_Application* getApplication()
85   {
86     if ( SUIT_Session::session() )
87       return dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() );
88     return 0;
89   }
90   
91   /*!
92     \brief Get the currently active study.
93     \internal
94     \return active study or 0 if there is no study opened
95   */
96   LightApp_Study* getActiveStudy()
97   {
98     if ( getApplication() )
99       return dynamic_cast<LightApp_Study*>( getApplication()->activeStudy() );
100     return 0;
101   }
102
103   /*!
104     \brief Get the currently active module.
105     \internal
106     This function returns correct result only if Python-based
107     module is currently active. Otherwize, 0 is returned.
108   */
109   LightApp_Module* getActiveModule()
110   {
111     LightApp_Module* module = 0;
112     if ( LightApp_Application* anApp = getApplication() ) {
113       module = PyModuleHelper::getInitModule();
114       if ( !module )
115         module = dynamic_cast<LightApp_Module*>( anApp->activeModule() );
116     }
117     return module;
118   }
119   
120   /*!
121     \brief Get the currently active Python module's helper.
122     \internal
123     This function returns correct result only if Python-based
124     module is currently active. Otherwize, 0 is returned.
125   */
126   PyModuleHelper* getPythonHelper()
127   {
128     LightApp_Module* module = getActiveModule();
129     PyModuleHelper* helper = module ? module->findChild<PyModuleHelper*>( "python_module_helper" ) : 0;
130     return helper;
131   }
132   
133   /*!
134     \brief Get SALOME verbose level
135     \internal
136     \return \c true if SALOME debug output is allowed or \c false otherwise
137   */
138   bool verbose()
139   {
140     bool isVerbose = false;
141     if ( getenv( "SALOME_VERBOSE" ) ) {
142       QString envVar = getenv( "SALOME_VERBOSE" );
143       bool ok;
144       int value = envVar.toInt( &ok );
145       isVerbose = ok && value != 0;
146     }
147     return isVerbose;
148   }
149
150   /*!
151     \brief Get menu item title
152     \internal
153     \param menuId menu identifier
154     \return menu title (localized)
155   */
156   QString getMenuName( const QString& menuId )
157   {
158     QStringList contexts;
159     contexts << "SalomeApp_Application" << "LightApp_Application" << "STD_TabDesktop" <<
160       "STD_MDIDesktop" << "STD_Application" << "SUIT_Application" << "";
161     QString menuName = menuId;
162     for ( int i = 0; i < contexts.count() && menuName == menuId; i++ )
163       menuName = QApplication::translate( contexts[i].toLatin1().data(), menuId.toLatin1().data() );
164     return menuName;
165   }
166
167   /*!
168     \brief Load module icon
169     \internal
170     \param module module name
171     \param fileName path to the icon file
172     \return icon
173   */
174   QIcon loadIconInternal( const QString& module, const QString& fileName )
175   {
176     QIcon icon;
177     
178     LightApp_Application* app = getApplication();
179     
180     if ( app && !fileName.isEmpty() ) {
181       QPixmap pixmap = app->resourceMgr()->loadPixmap( module, 
182                                                        QApplication::translate( module.toLatin1().data(), 
183                                                                                 fileName.toUtf8().data() ) );
184       if ( !pixmap.isNull() )
185         icon = QIcon( pixmap );
186     }
187     return icon;
188   }
189
190   /*!
191     \brief Gets window with specified identifier 
192     \internal
193     \param id window identifier 
194     \return pointer on the window
195   */
196   SUIT_ViewWindow* getWnd( const int id )
197   {
198     SUIT_ViewWindow* resWnd = 0;
199     
200     LightApp_Application* app = getApplication();
201     if ( app ) {
202       ViewManagerList vmlist = app->viewManagers();
203       foreach( SUIT_ViewManager* vm, vmlist ) {
204         QVector<SUIT_ViewWindow*> vwlist = vm->getViews();
205         foreach ( SUIT_ViewWindow* vw, vwlist ) {
206           if ( id == vw->getId() ) {
207             resWnd = vw;
208             break;
209           }
210         }
211       }
212     }
213     return resWnd;
214   }
215
216   /*!
217     \brief Map of created selection objects.
218     \internal
219   */
220   QMap<LightApp_Application*, SALOME_Selection*> SelMap;
221
222   /*!
223     \brief Default resource file section name.
224     \internal
225   */
226   const char* DEFAULT_SECTION = "SalomePyQt";
227 }
228
229 /*!
230   \class SALOME_Selection
231   \brief The class represents selection which can be used in Python.
232 */
233
234 /*!
235   \brief Get the selection object for the specified application.
236
237   Finds or creates the selection object (one per study).
238
239   \param app application object
240   \return selection object or 0 if \a app is invalid
241 */
242 SALOME_Selection* SALOME_Selection::GetSelection( LightApp_Application* app )
243 {
244   SALOME_Selection* sel = 0;
245   if ( app && SelMap.find( app ) != SelMap.end() )
246     sel = SelMap[ app ];
247   else 
248     sel = SelMap[ app ] = new SALOME_Selection( app );
249   return sel;
250 }
251
252
253 /*!
254   \brief Constructor.
255   \param p parent object
256 */
257 SALOME_Selection::SALOME_Selection( QObject* p ) : QObject( 0 ), mySelMgr( 0 )
258 {
259   LightApp_Application* app = dynamic_cast<LightApp_Application*>( p );
260   if ( app ) {
261     mySelMgr = app->selectionMgr();
262     connect( mySelMgr, SIGNAL( selectionChanged() ), this, SIGNAL( currentSelectionChanged() ) );
263     connect( mySelMgr, SIGNAL( destroyed() ),        this, SLOT  ( onSelMgrDestroyed() ) );
264   }
265 }
266
267 /*!
268   \brief Destructor.
269 */
270 SALOME_Selection::~SALOME_Selection()
271 {
272   LightApp_Application* app = 0;
273   QMap<LightApp_Application*, SALOME_Selection*>::Iterator it;
274   for ( it = SelMap.begin(); it != SelMap.end() && !app; ++it ) {
275     if ( it.value() == this ) app = it.key();
276   }
277   if ( app ) SelMap.remove( app );
278 }
279
280 /*!
281   \brief Called when selection manager is destroyed (usually 
282   when the study is closed).
283 */
284 void SALOME_Selection::onSelMgrDestroyed()
285 {
286   mySelMgr = 0;
287 }
288
289 /*!
290   \brief Clear the selection.
291 */
292 void SALOME_Selection::Clear()
293 {
294   class TEvent: public SALOME_Event
295   {
296     LightApp_SelectionMgr* mySelMgr;
297   public:
298     TEvent( LightApp_SelectionMgr* selMgr ) 
299       : mySelMgr( selMgr ) {}
300     virtual void Execute() 
301     {
302       if ( mySelMgr )
303         mySelMgr->clearSelected();
304     }
305   };
306   ProcessVoidEvent( new TEvent( mySelMgr ) );
307 }
308
309 /*!
310   \brief Clear the selection.
311 */
312 void SALOME_Selection::ClearIObjects()
313 {
314   Clear();
315 }
316
317 /*!
318   Removes all selection filters.
319 */
320 void SALOME_Selection::ClearFilters()
321 {
322   class TEvent: public SALOME_Event 
323   {
324     LightApp_SelectionMgr* mySelMgr;
325   public:
326     TEvent( LightApp_SelectionMgr* selMgr ) 
327       : mySelMgr( selMgr ) {}
328     virtual void Execute() 
329     {
330       if ( mySelMgr )
331         mySelMgr->clearFilters();
332     }
333   };
334   ProcessVoidEvent( new TEvent( mySelMgr ) );
335 }
336
337 /*!
338   \class UserDefinedContent
339   \brief The class represents base class for user defined widget that
340   can be inserted to the Preferences dialog.
341 */
342
343 /*!
344   \brief Constructor
345 */
346 UserDefinedContent::UserDefinedContent()
347   : QWidget()
348 {
349 }
350
351 /*!
352   \brief Called from Preferences dialog to store settings to the resource file.
353 */
354 void UserDefinedContent::store()
355 {
356 }
357
358 /*!
359   \brief Called from Preferences dialog to restore settings from the resource file.
360 */
361 void UserDefinedContent::retrieve()
362 {
363 }
364
365 /*!
366   \class SgPyQtUserDefinedContent
367   \brief A Wrapper for UserDefinedContent class.
368   \internal
369 */
370 class SgPyQtUserDefinedContent: public QtxUserDefinedContent
371 {
372 public:
373   SgPyQtUserDefinedContent(UserDefinedContent*);
374   virtual ~SgPyQtUserDefinedContent();
375
376   void store( QtxResourceMgr*, QtxPreferenceMgr* );
377   void retrieve( QtxResourceMgr*, QtxPreferenceMgr* );
378
379 private:
380   UserDefinedContent* myContent;
381 };
382
383 /*!
384   \brief Create custom item for Preferences dialog wrapping widget passed from Python.
385   \internal
386 */
387 SgPyQtUserDefinedContent::SgPyQtUserDefinedContent(UserDefinedContent* content)
388   : QtxUserDefinedContent( 0 ), myContent( content )
389 {
390   QVBoxLayout* l = new QVBoxLayout( this );
391   l->setContentsMargins( 0, 0, 0, 0 );
392   l->addWidget( myContent );
393 }
394
395 /*!
396   \brief Destructor.
397   \internal
398 */
399 SgPyQtUserDefinedContent::~SgPyQtUserDefinedContent()
400 {
401 }
402
403 /*!
404   \brief Called from Preferences dialog to store settings to the resource file.
405   \internal
406 */
407 void SgPyQtUserDefinedContent::store( QtxResourceMgr*, QtxPreferenceMgr* )
408 {
409   myContent->store();
410 }
411
412 /*!
413   \brief Called from Preferences dialog to restore settings from the resource file.
414   \internal
415 */
416 void SgPyQtUserDefinedContent::retrieve( QtxResourceMgr*, QtxPreferenceMgr* )
417 {
418   myContent->retrieve();
419 }
420
421 /*!
422   \class SalomePyQt
423   \brief The class provides utility functions which can be used in the Python
424   to operate with the SALOME GUI.
425
426   All the functionality of this class is implemented as static methods, so they
427   can be called with the class name prefixed or via creation of the class instance.
428   For example, next both ways of SalomePyQt class usage are legal:
429   \code
430   from SalomePyQt import *
431   sg = SalomePyQt()
432   # using SalomePyQt class instance
433   desktop = sg.getDesktop()
434   # using SalomePyQt class directly
435   menubar = SalomePyQt.getMainMenuBar()
436   \endcode
437 */
438
439 /*!
440   \fn QString SalomePyQt::getAppName();
441   \brief Get application name
442   \return application name
443 */
444
445 QString SalomePyQt::getAppName()
446 {
447   LightApp_Application* app = getApplication();
448   return app == 0 ? QString() : QString(app->metaObject()->className()).split("_").first();
449 }
450
451 /*!
452   \fn bool SalomePyQt::isLightApp();
453   \brief Check if SALOME GUI is running in "light" mode.
454   \return \c true if this is a "light" application; \c false otherwise
455 */
456
457 bool SalomePyQt::isLightApp()
458 {
459   return SalomePyQt::getAppName() != "SalomeApp";
460 }
461
462 /*!
463   \fn QWidget* SalomePyQt::getDesktop();
464   \brief Get the active application's desktop window.
465   \return desktop window or 0 if there is no any
466 */
467
468 class TGetDesktopEvent: public SALOME_Event 
469 {
470 public:
471   typedef QWidget* TResult;
472   TResult myResult;
473   TGetDesktopEvent() : myResult( 0 ) {}
474   virtual void Execute()
475   {
476     if ( getApplication() )
477       myResult = (QWidget*)( getApplication()->desktop() );
478   }
479 };
480 QWidget* SalomePyQt::getDesktop()
481 {
482   return ProcessEvent( new TGetDesktopEvent() );
483 }
484
485 /*!
486   \fn QWidget* SalomePyQt::getMainFrame();
487   \brief Get current application's main frame widget [obsolete].
488
489   Main frame widget is an internal widget of the application 
490   desktop window (workspace).
491
492   \return workspace widget (0 on any error)
493 */
494
495 class TGetMainFrameEvent: public SALOME_Event
496 {
497 public:
498   typedef QWidget* TResult;
499   TResult myResult;
500   TGetMainFrameEvent() : myResult( 0 ) {}
501   virtual void Execute()
502   {
503     if ( getApplication() ) {
504       SUIT_Desktop* aDesktop = getApplication()->desktop();
505       myResult = (QWidget*)( aDesktop->centralWidget() );
506     }
507   }
508 };
509 QWidget* SalomePyQt::getMainFrame()
510 {
511   return ProcessEvent( new TGetMainFrameEvent() );
512 }
513
514 /*!
515   \fn QMenuBar* SalomePyQt::getMainMenuBar();
516   \brief Get current application desktop's main menu.
517   \return main menu object (0 on any error)
518 */
519
520 class TGetMainMenuBarEvent: public SALOME_Event
521 {
522 public:
523   typedef QMenuBar* TResult;
524   TResult myResult;
525   TGetMainMenuBarEvent() : myResult( 0 ) {}
526   virtual void Execute()
527   {
528     if ( LightApp_Application* anApp = getApplication() ) {
529       myResult = anApp->desktop()->menuBar();
530     }
531   }
532 };
533 QMenuBar* SalomePyQt::getMainMenuBar() 
534 {
535   return ProcessEvent( new TGetMainMenuBarEvent() );
536 }
537
538 /*!
539   \fn QMenu* SalomePyQt::getPopupMenu( const MenuName menu );
540   \brief Get main menu's child popup submenu by its identifier.
541   
542   This function is obsolete. 
543   Use QMenu* SalomePyQt::getPopupMenu( const QString& menu ) instead.
544
545   \param menu menu identifier
546   \return popup submenu object or 0 if it does not exist
547 */
548
549 /*!
550   \fn QMenu* SalomePyQt::getPopupMenu( const QString& menu );
551   \brief Get main menu's child popup submenu by its name.
552   
553   The function creates menu if it does not exist.
554
555   \param menu menu name
556   \return popup submenu object (0 on any error)
557 */
558
559 class TGetPopupMenuEvent: public SALOME_Event
560 {
561 public:
562   typedef QMenu* TResult;
563   TResult myResult;
564   QString myMenuName;
565   TGetPopupMenuEvent( const QString& menu ) : myResult( 0 ), myMenuName( menu ) {}
566   virtual void Execute()
567   {
568     LightApp_Application* anApp = getApplication();
569     if ( anApp && !myMenuName.isEmpty() ) {
570       QtxActionMenuMgr* mgr = anApp->desktop()->menuMgr();
571       myResult = mgr->findMenu( myMenuName, -1, false ); // search only top menu
572     }
573   }
574 };
575
576 QMenu* SalomePyQt::getPopupMenu( const MenuName menu )
577 {
578   QString menuName;
579   switch( menu ) {
580   case File:
581     menuName = getMenuName( "MEN_DESK_FILE" );        break;
582   case View:
583     menuName = getMenuName( "MEN_DESK_VIEW" );        break;
584   case Edit:
585     menuName = getMenuName( "MEN_DESK_EDIT" );        break;
586   case Preferences:
587     menuName = getMenuName( "MEN_DESK_PREFERENCES" ); break;
588   case Tools:
589     menuName = getMenuName( "MEN_DESK_TOOLS" );       break;
590   case Window:
591     menuName = getMenuName( "MEN_DESK_WINDOW" );      break;
592   case Help:
593     menuName = getMenuName( "MEN_DESK_HELP" );        break;
594   }
595   return ProcessEvent( new TGetPopupMenuEvent( menuName ) );
596 }
597 QMenu* SalomePyQt::getPopupMenu( const QString& menu )
598 {
599   return ProcessEvent( new TGetPopupMenuEvent( menu ) );
600 }
601
602 /*!
603   \fn QTreeView* SalomePyQt::getObjectBrowser();
604   \brief Get object browser
605   \return object browser for the active study or 0 in case of error
606 */
607
608 class TGetObjectBrowserEvent: public SALOME_Event
609 {
610 public:
611   typedef QTreeView* TResult;
612   TResult myResult;
613   TGetObjectBrowserEvent() : myResult( 0 ) {}
614   virtual void Execute()
615   {
616     LightApp_Application* anApp = getApplication();
617     if ( anApp && anApp->objectBrowser() ) {
618       myResult = anApp->objectBrowser()->treeView();
619     }
620   }
621 };
622 QTreeView* SalomePyQt::getObjectBrowser()
623 {
624   return ProcessEvent( new TGetObjectBrowserEvent() );
625 }
626
627 /*!
628   \fn SALOME_Selection* SalomePyQt::getSelection();
629   \brief Get the selection object for the current study.
630
631   Creates a Selection object if it has not been created yet.
632
633   \return selection object (0 on error)
634 */
635
636 class TGetSelectionEvent: public SALOME_Event 
637 {
638 public:
639   typedef SALOME_Selection* TResult;
640   TResult myResult;
641   TGetSelectionEvent() : myResult( 0 ) {}
642   virtual void Execute() 
643   {
644     myResult = SALOME_Selection::GetSelection( getApplication() );
645   }
646 };
647 SALOME_Selection* SalomePyQt::getSelection()
648 {
649   return ProcessEvent( new TGetSelectionEvent() );
650 }
651
652 /*!
653   \fn QStringList* SalomePyQt::setSelection(const QStringList& );
654   \brief Send local selection for notification.
655
656   The list of locally selected objects (study entries) is sent for notification of
657   other listening entities (modules, viewers...).
658 */
659
660 class TSetSelectionEvent: public SALOME_Event
661 {
662   QStringList myEntryList;
663 public:
664   TSetSelectionEvent(const QStringList& entryList) : myEntryList(entryList) {}
665   virtual void Execute()
666   {
667         SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
668         if ( !module ) return;
669         module->setLocalSelected(myEntryList);
670   }
671 };
672 void SalomePyQt::setSelection( const QStringList& entryList)
673 {
674   return ProcessVoidEvent( new TSetSelectionEvent(entryList) );
675 }
676
677 /*!
678   \fn void SalomePyQt::enableSelector();
679   \brief enable PyQt_Selector (on module activation, for instance)
680 */
681
682 class TEnableSelectorEvent: public SALOME_Event
683 {
684 public:
685         TEnableSelectorEvent() {}
686   virtual void Execute()
687   {
688         SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
689         if ( !module ) return;
690         module->enableSelector();
691   }
692 };
693 void SalomePyQt::enableSelector()
694 {
695   return ProcessVoidEvent( new TEnableSelectorEvent() );
696 }
697
698
699 /*!
700   \fn void SalomePyQt::disableSelector();
701   \brief disable PyQt_Selector (on module activation, for instance)
702 */
703
704 class TdisableSelectorEvent: public SALOME_Event
705 {
706 public:
707         TdisableSelectorEvent() {}
708   virtual void Execute()
709   {
710         SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
711         if ( !module ) return;
712         module->disableSelector();
713   }
714 };
715 void SalomePyQt::disableSelector()
716 {
717   return ProcessVoidEvent( new TdisableSelectorEvent() );
718 }
719
720
721 /*!
722   \fn void SalomePyQt::putInfo( const QString& msg, const int sec );
723   \brief Put an information message to the current application's 
724   desktop status bar.
725
726   Optional second delay parameter (\a sec) can be used to specify
727   time of the message diplaying in seconds. If this parameter is less
728   or equal to zero, the constant message will be put.
729
730   \param msg message text 
731   \param sec message displaying time in seconds
732 */
733
734 class TPutInfoEvent: public SALOME_Event
735 {
736   QString myMsg;
737   int     mySecs;
738 public:
739   TPutInfoEvent( const QString& msg, const int sec = 0 ) : myMsg( msg ), mySecs( sec ) {}
740   virtual void Execute()
741   {
742     if ( LightApp_Application* anApp = getApplication() ) {
743       anApp->putInfo( myMsg, mySecs * 1000 );
744     }
745   }
746 };
747 void SalomePyQt::putInfo( const QString& msg, const int sec )
748 {
749   ProcessVoidEvent( new TPutInfoEvent( msg, sec ) );
750 }
751
752 /*!
753   \fn int SalomePyQt::showNotification( const QString& msg, const QString& title, const int sec );
754   \brief Show notification in the application's desktop window.
755
756   Optional third delay parameter (\a sec) can be used to specify
757   time of the notification diplaying in seconds. If this parameter is less
758   or equal to zero, the permanent notification will be put.
759
760   Notification can be forcibly hidden via hideNotification() method.
761
762   \param msg message text 
763   \param title title text 
764   \param sec notification displaying time in seconds
765   \return unique ID of the notification (can be used to hide notification)
766   \sa hideNotification()
767 */
768
769 class TShowNotifyEvent: public SALOME_Event
770 {
771   QString myMsg;
772   QString myTitle;
773   int     mySecs;
774
775 public:
776   typedef int TResult;
777   TResult myResult;
778
779 public:
780   TShowNotifyEvent( const QString& msg, const QString& title, const int sec = -1 ) : myMsg( msg ), myTitle( title), mySecs( sec ), myResult( -1 ) {}
781   virtual void Execute()
782   {
783     if ( LightApp_Application* anApp = getApplication() ) {
784       myResult = anApp->showNotification( myMsg, myTitle, mySecs * 1000 );
785     }
786   }
787 };
788
789 int SalomePyQt::showNotification( const QString& msg, const QString& title, const int sec )
790 {
791   return ProcessEvent( new TShowNotifyEvent( msg, title, sec ) );
792 }
793
794 /*!
795   \fn void SalomePyQt::hideNotification( const QString& msg );
796   \brief Remove notification with given message text from the application's desktop.
797
798   \param msg message text
799   \sa showNotification()
800 */
801
802 /*!
803   \fn void SalomePyQt::hideNotification( const int id );
804   \brief Remove notification with given \a id from the application's desktop.
805
806   \param id notification id
807   \sa showNotification()
808 */
809
810 class THideNotifyEvent: public SALOME_Event
811 {
812   int     myId;
813   QString myMsg;
814
815 public:
816   THideNotifyEvent( const QString& msg ) : myId( -1 ), myMsg( msg ) {}
817   THideNotifyEvent( const int id ) : myId( id ) {}
818   virtual void Execute()
819   {
820     if ( LightApp_Application* anApp = getApplication() ) {
821       if ( myId >= 0 )
822        anApp->hideNotification( myId );
823       else
824        anApp->hideNotification( myMsg );
825     }
826   }
827 };
828
829 void SalomePyQt::hideNotification( const QString& msg )
830 {
831   ProcessVoidEvent( new THideNotifyEvent( msg ) );
832 }
833
834 void SalomePyQt::hideNotification( const int id )
835 {
836   ProcessVoidEvent( new THideNotifyEvent( id ) );
837 }
838
839 /*!
840   \fn QStringList SalomePyQt::getComponents();
841   \brief Get all modules used in current GUI session.
842   \return List of modules
843 */
844
845 class TGetComponentsEvent: public SALOME_Event
846 {
847 public:
848   typedef QStringList TResult;
849   TResult myResult;
850   TGetComponentsEvent() {}
851   virtual void Execute() 
852   {
853     if ( LightApp_Application* anApp = getApplication() )
854     {
855       QStringList titles;
856       anApp->modules( titles, false );
857       foreach ( QString title, titles )
858       {
859         myResult << anApp->moduleName( title );
860       }
861     }
862   }
863 };
864 QStringList SalomePyQt::getComponents()
865 {
866   return ProcessEvent( new TGetComponentsEvent() );
867 }
868
869 /*!
870   \fn const QString SalomePyQt::getActiveComponent();
871   \brief Get the currently active module name (for the current study).
872   \return active module name or empty string if there is no active module
873 */
874
875 class TGetActiveComponentEvent: public SALOME_Event
876 {
877 public:
878   typedef QString TResult;
879   TResult myResult;
880   TGetActiveComponentEvent() {}
881   virtual void Execute() 
882   {
883     if ( LightApp_Application* anApp = getApplication() ) {
884       if ( CAM_Module* mod = anApp->activeModule() ) {
885         myResult = mod->name();
886       }
887     }
888   }
889 };
890 const QString SalomePyQt::getActiveComponent()
891 {
892   return ProcessEvent( new TGetActiveComponentEvent() );
893 }
894
895 /*!
896   \fn PyObject* SalomePyQt::getActivePythonModule();
897   \brief Access to Python module object currently loaded into SALOME_PYQT_ModuleLight container.
898   \return Python module object currently loaded into SALOME_PYQT_ModuleLight container
899 */
900
901 class TGetActivePyModuleEvent: public SALOME_Event
902 {
903 public:
904   typedef PyObject* TResult;
905   TResult myResult;
906   TGetActivePyModuleEvent() : myResult( Py_None ) {}
907   virtual void Execute() 
908   {
909     PyModuleHelper* helper = getPythonHelper();
910     if ( helper )
911       myResult = (PyObject*)helper->pythonModule();
912   }
913 };
914 PyObject* SalomePyQt::getActivePythonModule()
915 {
916   return ProcessEvent( new TGetActivePyModuleEvent() );
917 }
918
919 /*!
920   \fn bool SalomePyQt::activateModule( const QString& modName );
921   \brief Activates SALOME module with the given name
922   \return True if the module has been activated and False otherwise.
923 */
924
925 class TActivateModuleEvent: public SALOME_Event
926 {
927 public:
928   typedef bool TResult;
929   TResult myResult;
930   QString myModuleName;
931   TActivateModuleEvent( const QString& modName ) 
932   : myResult( false ), myModuleName( modName ) {}
933   virtual void Execute() 
934   {
935     if ( LightApp_Application* anApp = getApplication() ) {
936       myResult = anApp->activateModule( myModuleName );
937     }
938   }
939 };
940 bool SalomePyQt::activateModule( const QString& modName )
941 {
942   return ProcessEvent( new TActivateModuleEvent( modName ) );
943 }
944
945 /*!
946   \brief Update an Object Browser of the study.
947 */
948 void SalomePyQt::updateObjBrowser()
949 {  
950   class TEvent: public SALOME_Event
951   {
952   public:
953     TEvent() {}
954     virtual void Execute()
955     {
956       if ( SUIT_Session::session() ) {
957         if ( getActiveStudy() ) {
958           QList<SUIT_Application*> apps = SUIT_Session::session()->applications();
959           QList<SUIT_Application*>::Iterator it;
960           for( it = apps.begin(); it != apps.end(); ++it ) {
961             LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( *it );
962             if ( anApp && anApp->activeStudy() ) {
963               anApp->updateObjectBrowser();
964               return;
965             }
966           }
967         }
968       }
969     }
970   };
971   ProcessVoidEvent( new TEvent() );
972 }
973
974
975 /*!
976   SalomePyQt::isModified()
977   \return The modification status of the data model
978   for the currently active Python module
979   \note This function is supported for "light" Python-based SALOME modules only.
980   \sa setModified()
981 */
982 class TIsModifiedEvent: public SALOME_Event
983 {
984 public:
985   typedef bool TResult;
986   TResult myResult;
987   TIsModifiedEvent() : myResult( false ) {}
988   virtual void Execute() 
989   {
990     LightApp_Module* module = getActiveModule();
991     if ( !module )
992       return;
993     
994     SALOME_PYQT_DataModelLight* aModel =
995       dynamic_cast<SALOME_PYQT_DataModelLight*>( module->dataModel() );
996     if ( aModel ) {
997       myResult = aModel->isModified();
998     }
999     else {
1000       if ( verbose() ) printf( "SalomePyQt.isModified() function is not supported for the current module.\n" );
1001     }
1002   }
1003 };
1004 bool SalomePyQt::isModified()
1005 {
1006   return ProcessEvent(new TIsModifiedEvent());
1007 }
1008
1009 /*!
1010   SalomePyQt::setModified()
1011
1012   Sets the modification status of the data model for 
1013   the currently active Python module. This method should be used
1014   by the Python code in order to enable/disable "Save" operation
1015   depending on the module's data state.
1016
1017   \note This function is supported for "light" Python-based SALOME modules only.
1018
1019   \param New modification status of the data model
1020
1021   \sa isModified()
1022 */
1023 void SalomePyQt::setModified( bool flag )
1024 {  
1025   class TEvent: public SALOME_Event
1026   {
1027     bool myFlag;
1028   public:
1029     TEvent( bool flag ) 
1030       : myFlag( flag ) {}
1031     virtual void Execute()
1032     {
1033       LightApp_Module* module = getActiveModule();
1034       if ( !module )
1035         return;
1036
1037       SALOME_PYQT_DataModelLight* model =
1038         dynamic_cast<SALOME_PYQT_DataModelLight*>( module->dataModel() );
1039
1040       LightApp_Application* app = module->getApp();
1041
1042       if ( model && app ) {
1043         model->setModified( myFlag );
1044         app->updateActions();
1045       }
1046       else {
1047         if ( verbose() ) printf( "SalomePyQt.setModified() function is not supported for the current module.\n" );
1048       }
1049     }
1050   };
1051   ProcessVoidEvent( new TEvent( flag ) );
1052 }
1053
1054 /*!
1055   \brief Add string setting to the application preferences.
1056
1057   The parameter \a autoValue is obsolete parameter and currently is not used.
1058   This parameter will be removed in future, so try to avoid its usage in 
1059   your code.
1060
1061   This function is obsolete. Use one of addSetting() instead.
1062
1063   \param name setting name (it should be of kind <section:setting> where
1064   \c section is resources section name and \c setting is setting name)
1065   \param value new setting value
1066   \param autoValue (not used)
1067 */
1068 void SalomePyQt::addStringSetting( const QString& name, const QString& value, bool autoValue )
1069 {
1070   class TEvent: public SALOME_Event
1071   {
1072     QString myName;
1073     QString myValue;
1074     bool    myAutoValue;
1075   public:
1076     TEvent( const QString& name, const QString& value, bool autoValue ) 
1077       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
1078     virtual void Execute()
1079     {
1080       if ( SUIT_Session::session() ) {
1081         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1082         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
1083         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
1084         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
1085         if ( !_sec.isEmpty() && !_nam.isEmpty() )
1086           resMgr->setValue( _sec, _nam, myValue );
1087       }
1088     }
1089   };
1090   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
1091 }
1092
1093 /*!
1094   \brief Add integer setting to the application preferences.
1095
1096   The parameter \a autoValue is obsolete parameter and currently is not used.
1097   This parameter will be removed in future, so try to avoid its usage in 
1098   your code.
1099
1100   This function is obsolete. Use one of addSetting() instead.
1101
1102   \param name setting name (it should be of kind <section:setting> where
1103   \c section is resources section name and \c setting is setting name)
1104   \param value new setting value
1105   \param autoValue (not used)
1106 */
1107 void SalomePyQt::addIntSetting( const QString& name, const int value, bool autoValue)
1108 {
1109   class TEvent: public SALOME_Event 
1110   {
1111     QString myName;
1112     int     myValue;
1113     bool    myAutoValue;
1114   public:
1115     TEvent( const QString& name, const int value, bool autoValue ) 
1116       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
1117     virtual void Execute()
1118     {
1119       if ( SUIT_Session::session() ) {
1120         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1121         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
1122         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
1123         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
1124         if ( !_sec.isEmpty() && !_nam.isEmpty() )
1125           resMgr->setValue( _sec, _nam, myValue );
1126       }
1127     }
1128   };
1129   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
1130 }
1131
1132 /*!
1133   \brief Add double setting to the application preferences.
1134
1135   The parameter \a autoValue is obsolete parameter and currently is not used.
1136   This parameter will be removed in future, so try to avoid its usage in 
1137   your code.
1138
1139   This function is obsolete. Use one of addSetting() instead.
1140
1141   \param name setting name (it should be of kind <section:setting> where
1142   \c section is resources section name and \c setting is setting name)
1143   \param value new setting value
1144   \param autoValue (not used)
1145 */
1146 void SalomePyQt::addDoubleSetting( const QString& name, const double value, bool autoValue )
1147 {
1148   class TEvent: public SALOME_Event 
1149   {
1150     QString myName;
1151     double  myValue;
1152     bool    myAutoValue;
1153   public:
1154     TEvent( const QString& name, const double value, bool autoValue ) 
1155       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
1156     virtual void Execute() 
1157     {
1158       if ( SUIT_Session::session() ) {
1159         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1160         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
1161         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
1162         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
1163         if ( !_sec.isEmpty() && !_nam.isEmpty() )
1164           resMgr->setValue( _sec, _nam, myValue );
1165       }
1166     }
1167   };
1168   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
1169 }
1170
1171 /*!
1172   \brief Add boolean setting to the application preferences.
1173
1174   The parameter \a autoValue is obsolete parameter and currently is not used.
1175   This parameter will be removed in future, so try to avoid its usage in 
1176   your code.
1177
1178   This function is obsolete. Use one of addSetting() instead.
1179
1180   \param name setting name (it should be of kind <section:setting> where
1181   \c section is resources section name and \c setting is setting name)
1182   \param value new setting value
1183   \param autoValue (not used)
1184 */
1185 void SalomePyQt::addBoolSetting( const QString& name, const bool value, bool autoValue )
1186 {
1187   class TEvent: public SALOME_Event 
1188   {
1189     QString myName;
1190     bool    myValue;
1191     bool    myAutoValue;
1192   public:
1193     TEvent( const QString& name, const bool value, bool autoValue ) 
1194       : myName( name ), myValue( value ), myAutoValue( autoValue ) {}
1195     virtual void Execute() 
1196     {
1197       if ( SUIT_Session::session() ) {
1198         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1199         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
1200         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
1201         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
1202         if ( !_sec.isEmpty() && !_nam.isEmpty() )
1203           resMgr->setValue( _sec, _nam, myValue );
1204       }
1205     }
1206   };
1207   ProcessVoidEvent( new TEvent( name, value, autoValue ) );
1208 }
1209
1210 /*!
1211   \brief Remove setting from the application preferences.
1212
1213   This function is obsolete. Use removeSetting() instead.
1214
1215   \param name setting name (it should be of kind <section:setting> where
1216   \c section is resources section name and \c setting is setting name)
1217 */
1218 void SalomePyQt::removeSettings( const QString& name )
1219 {
1220   class TEvent: public SALOME_Event
1221   {
1222     QString myName;
1223   public:
1224     TEvent( const QString& name ) : myName( name ) {}
1225     virtual void Execute()
1226     {
1227       if ( SUIT_Session::session() ) {
1228         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1229         QStringList sl = myName.split( ":", QString::SkipEmptyParts );
1230         QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
1231         QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
1232         if ( !_sec.isEmpty() && !_nam.isEmpty() )
1233           resMgr->remove( _sec, _nam );
1234       }
1235     }
1236   };
1237   ProcessVoidEvent( new TEvent( name ) );
1238 }
1239
1240 /*!
1241   \fn QString SalomePyQt::getSetting( const QString& name );
1242   \brief Get application setting value (as string represenation).
1243
1244   This function is obsolete. Use stringSetting(), integerSetting(), 
1245   boolSetting(), stringSetting() or colorSetting() instead.
1246
1247   \param name setting name (it should be of kind <section:setting> where
1248   \c section is resources section name and \c setting is setting name)
1249   \return setting name (empty string if setting name is invalid)
1250 */
1251
1252 class TGetSettingEvent: public SALOME_Event 
1253 {
1254 public:
1255   typedef QString TResult;
1256   TResult myResult;
1257   QString myName;
1258   TGetSettingEvent( const QString& name ) : myName( name ) {}
1259   virtual void Execute() 
1260   {
1261     if ( SUIT_Session::session() ) {
1262       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1263       QStringList sl = myName.split( ":", QString::SkipEmptyParts );
1264       QString _sec = sl.count() > 1 ? sl[ 0 ].trimmed() : QString( DEFAULT_SECTION );
1265       QString _nam = sl.count() > 1 ? sl[ 1 ].trimmed() : sl.count() > 0 ? sl[ 0 ].trimmed() : QString( "" );
1266       myResult = ( !_sec.isEmpty() && !_nam.isEmpty() ) ? resMgr->stringValue( _sec, _nam, "" ) : QString( "" );
1267     }
1268   }
1269 };
1270 QString SalomePyQt::getSetting( const QString& name )
1271 {
1272   return ProcessEvent( new TGetSettingEvent( name ) );
1273 }
1274
1275 /*!
1276   \fn QString SalomePyQt::constant( const QString& name );
1277   \brief Get constant's value from application's resource manager.
1278
1279   \param name name of the constant 
1280   \return value of the constant
1281
1282   \sa setConstant()
1283 */
1284
1285 class TGetConstantEvent: public SALOME_Event 
1286 {
1287 public:
1288   typedef QString TResult;
1289   TResult myResult;
1290   QString myName;
1291   TGetConstantEvent( const QString& name ) : myName( name ) {}
1292   virtual void Execute() 
1293   {
1294     if ( SUIT_Session::session() )
1295       myResult = SUIT_Session::session()->resourceMgr()->constant( myName );
1296   }
1297 };
1298 QString SalomePyQt::constant( const QString& name )
1299 {
1300   return ProcessEvent( new TGetConstantEvent( name ) );
1301 }
1302
1303 /*!
1304   \brief Add constant to the application's resource manager.
1305
1306   This function is useful to specify programmatically specific
1307   variables that are referenced in the resource setting.
1308
1309   For example, some resource value can be set as "$(myroot)/data/files".
1310   Then, "mypath" constant can be set programmatically by the application
1311   depending on run-time requirements.
1312   
1313   \param section resources file section name 
1314   \param name name of the constant 
1315   \param value value of the constant 
1316
1317   \sa constant()
1318 */
1319 void SalomePyQt::setConstant( const QString& name, const QString& value )
1320 {
1321   class TEvent: public SALOME_Event 
1322   {
1323     QString myName, myValue;
1324   public:
1325     TEvent( const QString& name, const QString& value ) 
1326       : myName( name ), myValue( value ) {}
1327     virtual void Execute() 
1328     {
1329       if ( SUIT_Session::session() )
1330         SUIT_Session::session()->resourceMgr()->setConstant( myName, myValue );
1331     }
1332   };
1333   ProcessVoidEvent( new TEvent( name, value ) );
1334 }
1335
1336 /*!
1337   \brief Add double setting to the application preferences.
1338   \param section resources file section name 
1339   \param name setting name
1340   \param value new setting value
1341 */
1342 void SalomePyQt::addSetting( const QString& section, const QString& name, const double value )
1343 {
1344   class TEvent: public SALOME_Event 
1345   {
1346     QString mySection;
1347     QString myName;
1348     double  myValue;
1349   public:
1350     TEvent( const QString& section, const QString& name, double value ) 
1351       : mySection( section ), myName( name ), myValue( value ) {}
1352     virtual void Execute() 
1353     {
1354       if ( SUIT_Session::session() ) {
1355         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1356         if ( !mySection.isEmpty() && !myName.isEmpty() )
1357           resMgr->setValue( mySection, myName, myValue );
1358       }
1359     }
1360   };
1361   ProcessVoidEvent( new TEvent( section, name, value ) );
1362 }
1363
1364 /*!
1365   \brief Add integer setting to the application preferences.
1366   \param section resources file section name 
1367   \param name setting name
1368   \param value new setting value
1369 */
1370 void SalomePyQt::addSetting( const QString& section, const QString& name, const int value )
1371 {
1372   class TEvent: public SALOME_Event 
1373   {
1374     QString mySection;
1375     QString myName;
1376     int     myValue;
1377   public:
1378     TEvent( const QString& section, const QString& name, int value ) 
1379       : mySection( section ), myName( name ), myValue( value ) {}
1380     virtual void Execute() 
1381     {
1382       if ( SUIT_Session::session() ) {
1383         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1384         if ( !mySection.isEmpty() && !myName.isEmpty() )
1385           resMgr->setValue( mySection, myName, myValue );
1386       }
1387     }
1388   };
1389   ProcessVoidEvent( new TEvent( section, name, value ) );
1390 }
1391
1392 /*!
1393   \brief Add boolean setting to the application preferences.
1394   \param section resources file section name 
1395   \param name setting name
1396   \param value new setting value
1397   \param dumb this parameter is used in order to avoid sip compilation error 
1398   because of conflicting int and bool types
1399 */
1400 void SalomePyQt::addSetting( const QString& section, const QString& name, const bool value, const int /*dumb*/ )
1401 {
1402   class TEvent: public SALOME_Event 
1403   {
1404     QString mySection;
1405     QString myName;
1406     bool    myValue;
1407   public:
1408     TEvent( const QString& section, const QString& name, bool value ) 
1409       : mySection( section ), myName( name ), myValue( value ) {}
1410     virtual void Execute() 
1411     {
1412       if ( SUIT_Session::session() ) {
1413         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1414         if ( !mySection.isEmpty() && !myName.isEmpty() )
1415           resMgr->setValue( mySection, myName, myValue );
1416       }
1417     }
1418   };
1419   ProcessVoidEvent( new TEvent( section, name, value ) );
1420 }
1421
1422 /*!
1423   \brief Add string setting to the application preferences.
1424   \param section resources file section name 
1425   \param name setting name
1426   \param value new setting value
1427 */
1428 void SalomePyQt::addSetting( const QString& section, const QString& name, const QString& value )
1429 {
1430   class TEvent: public SALOME_Event 
1431   {
1432     QString mySection;
1433     QString myName;
1434     QString myValue;
1435   public:
1436     TEvent( const QString& section, const QString& name, const QString& value ) 
1437       : mySection( section ), myName( name ), myValue( value ) {}
1438     virtual void Execute() 
1439     {
1440       if ( SUIT_Session::session() ) {
1441         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1442         if ( !mySection.isEmpty() && !myName.isEmpty() )
1443           resMgr->setValue( mySection, myName, myValue );
1444       }
1445     }
1446   };
1447   ProcessVoidEvent( new TEvent( section, name, value ) );
1448 }
1449
1450 /*!
1451   \brief Add color setting to the application preferences.
1452   \param section resources file section name 
1453   \param name setting name
1454   \param value new setting value
1455 */
1456 void SalomePyQt::addSetting( const QString& section, const QString& name, const QColor& value )
1457 {
1458   class TEvent: public SALOME_Event 
1459   {
1460     QString mySection;
1461     QString myName;
1462     QColor  myValue;
1463   public:
1464     TEvent( const QString& section, const QString& name, const QColor& value ) 
1465       : mySection( section ), myName( name ), myValue( value ) {}
1466     virtual void Execute() 
1467     {
1468       if ( SUIT_Session::session() ) {
1469         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1470         if ( !mySection.isEmpty() && !myName.isEmpty() )
1471           resMgr->setValue( mySection, myName, myValue );
1472       }
1473     }
1474   };
1475   ProcessVoidEvent( new TEvent( section, name, value ) );
1476 }
1477
1478 /*!
1479   \brief Add byte array setting to the application preferences.
1480   \param section resources file section name 
1481   \param name setting name
1482   \param value new setting value
1483 */
1484 void SalomePyQt::addSetting( const QString& section, const QString& name, const QByteArray& value )
1485 {
1486   class TEvent: public SALOME_Event 
1487   {
1488     QString    mySection;
1489     QString    myName;
1490     QByteArray myValue;
1491   public:
1492     TEvent( const QString& section, const QString& name, const QByteArray& value ) 
1493       : mySection( section ), myName( name ), myValue( value ) {}
1494     virtual void Execute() 
1495     {
1496       if ( SUIT_Session::session() ) {
1497         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1498         if ( !mySection.isEmpty() && !myName.isEmpty() )
1499           resMgr->setValue( mySection, myName, myValue );
1500       }
1501     }
1502   };
1503   ProcessVoidEvent( new TEvent( section, name, value ) );
1504 }
1505
1506 /*!
1507   \brief Add font setting to the application preferences.
1508   \param section resources file section name 
1509   \param name setting name
1510   \param value new setting value
1511 */
1512 void SalomePyQt::addSetting( const QString& section, const QString& name, const QFont& value )
1513 {
1514   class TEvent: public SALOME_Event 
1515   {
1516     QString    mySection;
1517     QString    myName;
1518     QFont      myValue;
1519   public:
1520     TEvent( const QString& section, const QString& name, const QFont& value ) 
1521       : mySection( section ), myName( name ), myValue( value ) {}
1522     virtual void Execute() 
1523     {
1524       if ( SUIT_Session::session() ) {
1525         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1526         if ( !mySection.isEmpty() && !myName.isEmpty() )
1527           resMgr->setValue( mySection, myName, myValue );
1528       }
1529     }
1530   };
1531   ProcessVoidEvent( new TEvent( section, name, value ) );
1532 }
1533
1534 /*!
1535   \fn int SalomePyQt::integerSetting( const QString& section, 
1536                                       const QString& name, 
1537                                       const int def );
1538   \brief Get integer setting from the application preferences.
1539   \param section resources file section name 
1540   \param name setting name
1541   \param def default value which is returned if the setting is not found
1542   \return setting value
1543 */
1544
1545 class TGetIntSettingEvent: public SALOME_Event 
1546 {
1547 public:
1548   typedef int TResult;
1549   TResult myResult;
1550   QString mySection;
1551   QString myName;
1552   TResult myDefault;
1553   TGetIntSettingEvent( const QString& section, const QString& name, const int def ) 
1554     : mySection( section ), myName( name ), myDefault( def ) {}
1555   virtual void Execute() 
1556   {
1557     if ( SUIT_Session::session() ) {
1558       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1559       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->integerValue( mySection, myName, myDefault ) : myDefault;
1560     }
1561   }
1562 };
1563 int SalomePyQt::integerSetting( const QString& section, const QString& name, const int def )
1564 {
1565   return ProcessEvent( new TGetIntSettingEvent( section, name, def ) );
1566 }
1567
1568 /*!
1569   \fn double SalomePyQt::doubleSetting( const QString& section, 
1570                                         const QString& name, 
1571                                         const double def );
1572   \brief Get double setting from the application preferences.
1573   \param section resources file section name 
1574   \param name setting name
1575   \param def default value which is returned if the setting is not found
1576   \return setting value
1577 */
1578
1579 class TGetDblSettingEvent: public SALOME_Event 
1580 {
1581 public:
1582   typedef double TResult;
1583   TResult myResult;
1584   QString mySection;
1585   QString myName;
1586   TResult myDefault;
1587   TGetDblSettingEvent( const QString& section, const QString& name, const double def ) 
1588     : mySection( section ), myName( name ), myDefault( def ) {}
1589   virtual void Execute() 
1590   {
1591     if ( SUIT_Session::session() ) {
1592       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1593       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->doubleValue( mySection, myName, myDefault ) : myDefault;
1594     }
1595   }
1596 };
1597 double SalomePyQt::doubleSetting( const QString& section, const QString& name, const double def )
1598 {
1599   return ProcessEvent( new TGetDblSettingEvent( section, name, def ) );
1600 }
1601
1602 /*!
1603   \fn bool SalomePyQt::boolSetting( const QString& section, 
1604                                     const QString& name, 
1605                                     const bool def );
1606   \brief Get boolean setting from the application preferences.
1607   \param section resources file section name 
1608   \param name setting name
1609   \param def default value which is returned if the setting is not found
1610   \return setting value
1611 */
1612
1613 class TGetBoolSettingEvent: public SALOME_Event 
1614 {
1615 public:
1616   typedef bool TResult;
1617   TResult myResult;
1618   QString mySection;
1619   QString myName;
1620   TResult myDefault;
1621   TGetBoolSettingEvent( const QString& section, const QString& name, const bool def ) 
1622     : mySection( section ), myName( name ), myDefault( def ) {}
1623   virtual void Execute() 
1624   {
1625     if ( SUIT_Session::session() ) {
1626       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1627       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->booleanValue( mySection, myName, myDefault ) : myDefault;
1628     }
1629   }
1630 };
1631 bool SalomePyQt::boolSetting( const QString& section, const QString& name, const bool def )
1632 {
1633   return ProcessEvent( new TGetBoolSettingEvent( section, name, def ) );
1634 }
1635
1636 /*!
1637   \fn QString SalomePyQt::stringSetting( const QString& section, 
1638                                          const QString& name, 
1639                                          const QString& def, 
1640                                          const bool subst );
1641   \brief Get string setting from the application preferences.
1642   \param section resources file section name 
1643   \param name setting name
1644   \param def default value which is returned if the setting is not found
1645   \param subst \c true to make substitution, \c false to get "raw" value
1646   \return setting value
1647 */
1648
1649 class TGetStrSettingEvent: public SALOME_Event
1650 {
1651 public:
1652   typedef QString TResult;
1653   TResult myResult;
1654   QString mySection;
1655   QString myName;
1656   bool mySubst;
1657   TResult myDefault;
1658   TGetStrSettingEvent( const QString& section, const QString& name, const QString& def, const bool subst ) 
1659     : mySection( section ), myName( name ), myDefault( def ), mySubst( subst ) {}
1660   virtual void Execute() 
1661   {
1662     if ( SUIT_Session::session() ) {
1663       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1664       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->stringValue( mySection, myName, myDefault, mySubst ) : myDefault;
1665     }
1666   }
1667 };
1668 QString SalomePyQt::stringSetting( const QString& section, const QString& name, const QString& def, const bool subst )
1669 {
1670   return ProcessEvent( new TGetStrSettingEvent( section, name, def, subst ) );
1671 }
1672
1673 /*!
1674   \fn QColor SalomePyQt::colorSetting( const QString& section, 
1675                                        const QString& name, 
1676                                        const QColor def );
1677   \brief Get color setting from the application preferences.
1678   \param section resources file section name 
1679   \param name setting name
1680   \param def default value which is returned if the setting is not found
1681   \return setting value
1682 */
1683
1684 class TGetColorSettingEvent: public SALOME_Event 
1685 {
1686 public:
1687   typedef QColor TResult;
1688   TResult myResult;
1689   QString mySection;
1690   QString myName;
1691   TResult myDefault;
1692   TGetColorSettingEvent( const QString& section, const QString& name, const QColor& def ) 
1693     : mySection( section ), myName( name ), myDefault( def ) {}
1694   virtual void Execute() 
1695   {
1696     if ( SUIT_Session::session() ) {
1697       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1698       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->colorValue( mySection, myName, myDefault ) : myDefault;
1699     }
1700   }
1701 };
1702 QColor SalomePyQt::colorSetting ( const QString& section, const QString& name, const QColor& def )
1703 {
1704   return ProcessEvent( new TGetColorSettingEvent( section, name, def ) );
1705 }
1706
1707 /*!
1708   \fn QByteArray SalomePyQt::byteArraySetting( const QString& section, 
1709                                                const QString& name, 
1710                                                const QByteArray& def );
1711   \brief Get byte array setting from the application preferences.
1712   \param section resources file section name 
1713   \param name setting name
1714   \param def default value which is returned if the setting is not found
1715   \return setting value
1716 */
1717
1718 class TGetByteArraySettingEvent: public SALOME_Event 
1719 {
1720 public:
1721   typedef QByteArray TResult;
1722   TResult myResult;
1723   QString mySection;
1724   QString myName;
1725   TResult myDefault;
1726   TGetByteArraySettingEvent( const QString& section, const QString& name, const QByteArray& def ) 
1727     : mySection( section ), myName( name ), myDefault( def ) {}
1728   virtual void Execute() 
1729   {
1730     if ( SUIT_Session::session() ) {
1731       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1732       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->byteArrayValue( mySection, myName, myDefault ) : myDefault;
1733     }
1734   }
1735 };
1736 QByteArray SalomePyQt::byteArraySetting ( const QString& section, const QString& name, const QByteArray& def )
1737 {
1738   return ProcessEvent( new TGetByteArraySettingEvent( section, name, def ) );
1739 }
1740
1741 /*!
1742   \fn QByteArray SalomePyQt::fontSetting( const QString& section, 
1743                                           const QString& name, 
1744                                           const QFont& def );
1745   \brief Get font setting from the application preferences.
1746   \param section resources file section name 
1747   \param name setting name
1748   \param def default value which is returned if the setting is not found
1749   \return setting value
1750 */
1751
1752 class TGetFontSettingEvent: public SALOME_Event 
1753 {
1754 public:
1755   typedef QFont TResult;
1756   TResult myResult;
1757   QString mySection;
1758   QString myName;
1759   TResult myDefault;
1760   TGetFontSettingEvent( const QString& section, const QString& name, const QFont& def ) 
1761     : mySection( section ), myName( name ), myDefault( def ) {}
1762   virtual void Execute() 
1763   {
1764     if ( SUIT_Session::session() ) {
1765       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1766       myResult = ( !mySection.isEmpty() && !myName.isEmpty() ) ? resMgr->fontValue( mySection, myName, myDefault ) : myDefault;
1767     }
1768   }
1769 };
1770 QFont SalomePyQt::fontSetting ( const QString& section, const QString& name, const QFont& def )
1771 {
1772   return ProcessEvent( new TGetFontSettingEvent( section, name, def ) );
1773 }
1774
1775 /*!
1776   \brief Remove setting from the application preferences.
1777   \param section resources file section name 
1778   \param name setting name
1779 */
1780 void SalomePyQt::removeSetting( const QString& section, const QString& name )
1781 {
1782   class TEvent: public SALOME_Event 
1783   {
1784     QString mySection;
1785     QString myName;
1786   public:
1787     TEvent( const QString& section, const QString& name ) : mySection( section ), myName( name ) {}
1788     virtual void Execute() 
1789     {
1790       if ( SUIT_Session::session() ) {
1791         SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1792         if ( !mySection.isEmpty() && !myName.isEmpty() )
1793           resMgr->remove( mySection, myName );
1794       }
1795     }
1796   };
1797   ProcessVoidEvent( new TEvent( section, name ) );
1798 }
1799
1800 /*!
1801   \fn bool SalomePyQt::hasSetting( const QString& section, const QString& name );
1802   \brief Check setting existence in the application preferences.
1803   \param section resources file section name 
1804   \param name setting name
1805   \return \c true if setting exists
1806 */
1807
1808 class THasSettingEvent: public SALOME_Event 
1809 {
1810 public:
1811   typedef bool TResult;
1812   TResult myResult;
1813   QString mySection;
1814   QString myName;
1815   THasSettingEvent( const QString& section, const QString& name ) 
1816     : mySection( section ), myName( name ) {}
1817   virtual void Execute() 
1818   {
1819     if ( SUIT_Session::session() ) {
1820       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1821       myResult = resMgr->hasValue( mySection, myName );
1822     }
1823   }
1824 };
1825 bool SalomePyQt::hasSetting( const QString& section, const QString& name )
1826 {
1827   return ProcessEvent( new THasSettingEvent( section, name ) );
1828 }
1829
1830 /*!
1831   \fn QStringList SalomePyQt::parameters( const QString& section );
1832   \brief Get names of preference items stored within the given section.
1833   \param section resources file section's name 
1834   \return \c list of preferences items
1835 */
1836
1837 /*!
1838   \fn QStringList SalomePyQt::parameters( const QStringList& section );
1839   \brief Get names of preference items stored within the given section.
1840   \param section resources file section's name 
1841   \return \c list of preferences items
1842 */
1843
1844 class TParametersEvent: public SALOME_Event 
1845 {
1846 public:
1847   typedef QStringList TResult;
1848   TResult myResult;
1849   QStringList mySection;
1850   TParametersEvent( const QString& section ) 
1851   {
1852     mySection << section;
1853   }
1854   TParametersEvent( const QStringList& section ) 
1855     : mySection( section )
1856   {}
1857   virtual void Execute() 
1858   {
1859     if ( SUIT_Session::session() ) {
1860       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1861       myResult = resMgr->parameters( mySection );
1862     }
1863   }
1864 };
1865 QStringList SalomePyQt::parameters( const QString& section )
1866 {
1867   return ProcessEvent( new TParametersEvent( section ) );
1868 }
1869 QStringList SalomePyQt::parameters( const QStringList& section )
1870 {
1871   return ProcessEvent( new TParametersEvent( section ) );
1872 }
1873
1874 /*!
1875   \fn QString SalomePyQt::getFileName( QWidget*           parent, 
1876                                        const QString&     initial, 
1877                                        const QStringList& filters, 
1878                                        const QString&     caption,
1879                                        bool               open );
1880   \brief Show 'Open/Save file' dialog box for file selection 
1881          and return a user's choice (selected file name).
1882   \param parent parent widget
1883   \param initial initial directory the dialog box to be opened in
1884   \param filters list of files filters (wildcards)
1885   \param caption dialog box title
1886   \param open if \c true, "Open File" dialog box is shown; 
1887          otherwise "Save File" dialog box is shown
1888   \return selected file name (null string if user cancels operation)
1889 */
1890
1891 class TGetFileNameEvent: public SALOME_Event 
1892 {
1893 public:
1894   typedef QString TResult;
1895   TResult     myResult;
1896   QWidget*    myParent;
1897   QString     myInitial;
1898   QStringList myFilters;
1899   QString     myCaption;
1900   bool        myOpen;
1901   TGetFileNameEvent( QWidget*           parent, 
1902                      const QString&     initial, 
1903                      const QStringList& filters, 
1904                      const QString&     caption,
1905                      bool               open ) 
1906     : myParent ( parent ), 
1907       myInitial( initial ), 
1908       myFilters( filters ), 
1909       myCaption( caption ), 
1910       myOpen ( open ) {}
1911   virtual void Execute() 
1912   {
1913     if ( LightApp_Application* anApp = getApplication() ) {
1914       myResult = anApp->getFileName( myOpen, myInitial, myFilters.join(";;"), 
1915                                      myCaption, myParent );
1916     }
1917   }
1918 };
1919 QString SalomePyQt::getFileName( QWidget*           parent, 
1920                                  const QString&     initial, 
1921                                  const QStringList& filters, 
1922                                  const QString&     caption,
1923                                  bool               open )
1924 {
1925   return ProcessEvent( new TGetFileNameEvent( parent, initial, filters, caption, open ) );
1926 }
1927
1928 /*!
1929   \fn QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
1930                                                 const QString&     initial, 
1931                                                 const QStringList& filters, 
1932                                                 const QString&     caption );
1933   \brief Show 'Open files' dialog box for multiple files selection
1934          and return a user's choice (selected file names list).
1935   \param parent parent widget
1936   \param initial initial directory the dialog box to be opened in
1937   \param filters list of files filters (wildcards)
1938   \param caption dialog box title
1939   \return selected file names list (empty list if user cancels operation)
1940 */
1941
1942 class TGetOpenFileNamesEvent: public SALOME_Event 
1943 {
1944 public:
1945   typedef QStringList TResult;
1946   TResult     myResult;
1947   QWidget*    myParent;
1948   QString     myInitial;
1949   QStringList myFilters;
1950   QString     myCaption;
1951   TGetOpenFileNamesEvent( QWidget*           parent, 
1952                           const QString&     initial, 
1953                           const QStringList& filters, 
1954                           const QString&     caption ) 
1955     : myParent ( parent ), 
1956       myInitial( initial ), 
1957       myFilters( filters ), 
1958       myCaption( caption ) {}
1959   virtual void Execute() 
1960   {
1961     if ( LightApp_Application* anApp = getApplication() ) {
1962       myResult = anApp->getOpenFileNames( myInitial, myFilters.join(";;"), myCaption, myParent );
1963     }
1964   }
1965 };
1966 QStringList SalomePyQt::getOpenFileNames( QWidget*           parent, 
1967                                           const QString&     initial, 
1968                                           const QStringList& filters, 
1969                                           const QString&     caption )
1970 {
1971   return ProcessEvent( new TGetOpenFileNamesEvent( parent, initial, filters, caption ) );
1972 }
1973
1974 /*!
1975   \fn QString SalomePyQt::getExistingDirectory( QWidget*       parent,
1976                                                 const QString& initial,
1977                                                 const QString& caption );
1978   \brief Show 'Get Directory' dialog box for the directory selection
1979          and return a user's choice (selected directory name).
1980   \param parent parent widget
1981   \param initial initial directory the dialog box to be opened in
1982   \param caption dialog box title
1983   \return selected directory name (null string if user cancels operation)
1984 */
1985
1986 class TGetExistingDirectoryEvent: public SALOME_Event 
1987 {
1988 public:
1989   typedef QString TResult;
1990   TResult     myResult;
1991   QWidget*    myParent;
1992   QString     myInitial;
1993   QString     myCaption;
1994   TGetExistingDirectoryEvent( QWidget*           parent, 
1995                               const QString&     initial, 
1996                               const QString&     caption ) 
1997     : myParent ( parent ), 
1998       myInitial( initial ), 
1999       myCaption( caption ) {}
2000   virtual void Execute() 
2001   {
2002     if ( LightApp_Application* anApp = getApplication() ) {
2003       myResult = anApp->getDirectory( myInitial, myCaption, myParent );
2004     }
2005   }
2006 };
2007 QString SalomePyQt::getExistingDirectory( QWidget*       parent,
2008                                           const QString& initial,
2009                                           const QString& caption )
2010 {
2011   return ProcessEvent( new TGetExistingDirectoryEvent( parent, initial, caption ) );
2012 }
2013
2014 /*!
2015   \fn QString SalomePyQt::loadIcon( const QString& filename );
2016   \brief Load an icon from the module resources by the specified file name.
2017   \param fileName icon file name
2018   \return icon object
2019 */
2020
2021 class TLoadIconEvent: public SALOME_Event 
2022 {
2023 public:
2024   typedef QIcon TResult;
2025   TResult     myResult;
2026   QString     myModule;
2027   QString     myFileName;
2028   TLoadIconEvent( const QString& module, const QString& filename ) 
2029     : myModule( module ), 
2030       myFileName ( filename ) {}
2031   virtual void Execute() 
2032   {
2033     myResult = loadIconInternal( myModule, myFileName );
2034   }
2035 };
2036 QIcon SalomePyQt::loadIcon( const QString& module, const QString& filename )
2037 {
2038   return ProcessEvent( new TLoadIconEvent( module, filename ) );
2039 }
2040
2041 /*!
2042   \brief Open external browser to display context help information.
2043   \todo
2044
2045   Current implementation does nothing.
2046
2047   \param source documentation (HTML) file name
2048   \param context context (for example, HTML ancor name)
2049 */
2050 void SalomePyQt::helpContext( const QString& source, const QString& context ) 
2051 {
2052   class TEvent: public SALOME_Event 
2053   {
2054     QString mySource;
2055     QString myContext;
2056   public:
2057     TEvent( const QString& source, const QString& context ) 
2058       : mySource( source ), myContext( context ) {}
2059     virtual void Execute() 
2060     {
2061       if ( LightApp_Application* anApp = getApplication() ) {
2062         anApp->onHelpContextModule( "", mySource, myContext );
2063       }
2064     }
2065   };
2066   ProcessVoidEvent( new TEvent( source, context ) );
2067 }
2068
2069 /*!
2070   \fn int SalomePyQt::defaultMenuGroup();
2071   \brief Get detault menu group identifier which can be used when 
2072   creating menus (insert custom menu commands).
2073   \return default menu group ID
2074 */
2075
2076 class TDefMenuGroupEvent: public SALOME_Event 
2077 {
2078 public:
2079   typedef int TResult;
2080   TResult myResult;
2081   TDefMenuGroupEvent() : myResult( -1 ) {}
2082   virtual void Execute() 
2083   {
2084     myResult = PyModuleHelper::defaultMenuGroup();
2085   }
2086 };
2087 int SalomePyQt::defaultMenuGroup()
2088 {
2089   return ProcessEvent( new TDefMenuGroupEvent() );
2090 }
2091
2092 class CrTool
2093 {
2094 public:
2095   CrTool( const QString& tBar, const QString& nBar ) 
2096     : myCase( 0 ), myTbTitle( tBar ), myTbName( nBar)  {}
2097   CrTool( const int id, const int tBar, const int idx ) 
2098     : myCase( 1 ), myId( id ), myTbId( tBar ), myIndex( idx ) {}
2099   CrTool( const int id, const QString& tBar, const int idx )
2100     : myCase( 2 ), myId( id ), myTbTitle( tBar ), myIndex( idx ) {}
2101   CrTool( QAction* action, const int tbId, const int id, const int idx )
2102     : myCase( 3 ), myAction( action ), myTbId( tbId ), myId( id ), myIndex( idx ) {}
2103   CrTool( QAction* action, const QString& tBar, const int id, const int idx )
2104     : myCase( 4 ), myAction( action ), myTbTitle( tBar ), myId( id ), myIndex( idx ) {}
2105
2106   int execute() const
2107   {
2108     switch ( myCase ) {
2109     case 0:
2110       if ( getActiveModule() )
2111         return getActiveModule()->createTool( myTbTitle, myTbName );
2112       else if ( getApplication() )
2113         return getApplication()->createTool( myTbTitle, myTbName );
2114     case 1:
2115       if ( getActiveModule() )
2116         return getActiveModule()->createTool( myId, myTbId, myIndex );
2117       else if ( getApplication() )
2118         return getApplication()->createTool( myId, myTbId, myIndex );
2119     case 2:
2120       if ( getActiveModule() )
2121         return getActiveModule()->createTool( myId, myTbTitle, myIndex );
2122       else if ( getApplication() )
2123         return getApplication()->createTool( myId, myTbTitle, myIndex );
2124     case 3:
2125       if ( getActiveModule() )
2126         return getActiveModule()->createTool( myAction, myTbId, myId, myIndex );
2127       else if ( getApplication() )
2128         return getApplication()->createTool( myAction, myTbId, myId, myIndex );
2129     case 4:
2130       if ( getActiveModule() )
2131         return getActiveModule()->createTool( myAction, myTbTitle, myId, myIndex );
2132       else if ( getApplication() )
2133         return getApplication()->createTool( myAction, myTbTitle, myId, myIndex );
2134     }
2135     return -1;
2136   }
2137 private:
2138    int        myCase;
2139    QString    myTbTitle;
2140    QString    myTbName;
2141    int        myTbId;
2142    QAction*   myAction;
2143    int        myId;
2144    int        myIndex;
2145 };
2146
2147 class TCreateToolEvent: public SALOME_Event 
2148 {
2149 public:
2150   typedef int TResult;
2151   TResult myResult;
2152   const CrTool& myCrTool;
2153   TCreateToolEvent( const CrTool& crTool ) 
2154     : myResult( -1 ), myCrTool( crTool ) {}
2155   virtual void Execute() 
2156   {
2157     myResult = myCrTool.execute();
2158   }
2159 };
2160
2161 /*!
2162   \brief Create toolbar with specified name.
2163   \param tBar toolbar title (language-dependent)
2164   \param nBar toolbar name (language-independent) [optional]
2165   \return toolbar ID or -1 if toolbar creation is failed
2166 */
2167 int SalomePyQt::createTool( const QString& tBar, const QString& nBar )
2168 {
2169   return ProcessEvent( new TCreateToolEvent( CrTool( tBar, nBar ) ) );
2170 }
2171
2172 /*! 
2173   \brief Insert action with specified \a id to the toolbar.
2174   \param id action ID
2175   \param tBar toolbar ID
2176   \param idx required index in the toolbar
2177   \return action ID or -1 if action could not be added
2178 */
2179 int SalomePyQt::createTool( const int id, const int tBar, const int idx )
2180 {
2181   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
2182 }
2183
2184 /*!
2185   \brief Insert action with specified \a id to the toolbar.
2186   \param id action ID
2187   \param tBar toolbar name
2188   \param idx required index in the toolbar
2189   \return action ID or -1 if action could not be added
2190 */
2191 int SalomePyQt::createTool( const int id, const QString& tBar, const int idx )
2192 {
2193   return ProcessEvent( new TCreateToolEvent( CrTool( id, tBar, idx ) ) );
2194 }
2195
2196 /*!
2197   \brief Insert action to the toolbar.
2198   \param a action
2199   \param tBar toolbar ID
2200   \param id required action ID
2201   \param idx required index in the toolbar
2202   \return action ID or -1 if action could not be added
2203 */
2204 int SalomePyQt::createTool( QAction* a, const int tBar, const int id, const int idx )
2205 {
2206   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
2207 }
2208
2209
2210 /*!
2211   \brief Clear given toolbar.
2212   \param title toolbar's title
2213 */
2214 void SalomePyQt::clearTool( const QString& title )
2215 {
2216   class TEvent: public SALOME_Event
2217   {
2218     QString myTitle;
2219   public:
2220     TEvent( const QString& title ) 
2221       : myTitle( title ) {}
2222     virtual void Execute() 
2223     {
2224       if ( getActiveModule() )
2225         return getActiveModule()->clearTool( myTitle );
2226       else if ( getApplication() )
2227         return getApplication()->clearTool( myTitle );
2228     }
2229   };
2230   ProcessVoidEvent( new TEvent( title ) );
2231 }
2232
2233 /*!
2234   \brief Insert action to the toolbar.
2235   \param a action
2236   \param tBar toolbar name
2237   \param id required action ID
2238   \param idx required index in the toolbar
2239   \return action ID or -1 if action could not be added
2240 */
2241 int SalomePyQt::createTool( QAction* a, const QString& tBar, const int id, const int idx )
2242 {
2243   return ProcessEvent( new TCreateToolEvent( CrTool( a, tBar, id, idx ) ) );
2244 }
2245
2246 class CrMenu
2247 {
2248 public:
2249   CrMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx ) 
2250     : myCase( 0 ), mySubMenuName( subMenu ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
2251   CrMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx ) 
2252     : myCase( 1 ), mySubMenuName( subMenu ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
2253   CrMenu( const int id, const int menu, const int group, const int idx ) 
2254     : myCase( 2 ), myId( id ), myMenuId( menu ), myGroup( group ), myIndex( idx ) {}
2255   CrMenu( const int id, const QString& menu, const int group, const int idx ) 
2256     : myCase( 3 ), myId( id ), myMenuName( menu ), myGroup( group ), myIndex( idx ) {}
2257   CrMenu( QAction* action, const int menu, const int id, const int group, const int idx ) 
2258     : myCase( 4 ), myAction( action ), myMenuId( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
2259   CrMenu( QAction* action, const QString& menu, const int id, const int group, const int idx ) 
2260     : myCase( 5 ), myAction( action ), myMenuName( menu ), myId( id ), myGroup( group ), myIndex( idx ) {}
2261
2262   int execute() const
2263   {
2264     switch ( myCase ) {
2265     case 0:
2266       if ( getActiveModule() )
2267         return getActiveModule()->createMenu( mySubMenuName, myMenuId, myId, myGroup, myIndex );
2268       else if ( getApplication() )
2269         return getApplication()->createMenu( mySubMenuName, myMenuId, myId, myGroup, myIndex );
2270     case 1:
2271       if ( getActiveModule() )
2272         return getActiveModule()->createMenu( mySubMenuName, myMenuName, myId, myGroup, myIndex );
2273       else if ( getApplication() )
2274         return getApplication()->createMenu( mySubMenuName, myMenuName, myId, myGroup, myIndex );
2275     case 2:
2276       if ( getActiveModule() )
2277         return getActiveModule()->createMenu( myId, myMenuId, myGroup, myIndex );
2278       else if ( getApplication() )
2279         return getApplication()->createMenu( myId, myMenuId, myGroup, myIndex );
2280     case 3:
2281       if ( getActiveModule() )
2282         return getActiveModule()->createMenu( myId, myMenuName, myGroup, myIndex );
2283       else if ( getApplication() )
2284         return getApplication()->createMenu( myId, myMenuName, myGroup, myIndex );
2285     case 4:
2286       if ( getActiveModule() )
2287         return getActiveModule()->createMenu( myAction, myMenuId, myId, myGroup, myIndex );
2288       else if ( getApplication() )
2289         return getApplication()->createMenu( myAction, myMenuId, myId, myGroup, myIndex );
2290     case 5:
2291       if ( getActiveModule() )
2292         return getActiveModule()->createMenu( myAction, myMenuName, myId, myGroup, myIndex );
2293       else if ( getApplication() )
2294         return getApplication()->createMenu( myAction, myMenuName, myId, myGroup, myIndex );
2295     }
2296     return -1;
2297   }
2298 private:
2299    int        myCase;
2300    QString    myMenuName;
2301    int        myMenuId;
2302    QString    mySubMenuName;
2303    int        myGroup;
2304    QAction*   myAction;
2305    int        myId;
2306    int        myIndex;
2307 };
2308
2309 class TCreateMenuEvent: public SALOME_Event
2310 {
2311 public:
2312   typedef int TResult;
2313   TResult myResult;
2314   const CrMenu& myCrMenu;
2315   TCreateMenuEvent( const CrMenu& crMenu ) 
2316     : myResult( -1 ), myCrMenu( crMenu ) {}
2317   virtual void Execute()
2318   {
2319     myResult = myCrMenu.execute();
2320   }
2321 };
2322
2323 /*!
2324   \brief Create main menu.
2325   \param subMenu menu name
2326   \param menu parent menu ID
2327   \param id required menu ID
2328   \param group menu group ID
2329   \param idx required index in the menu
2330   \return menu ID or -1 if menu could not be added
2331 */
2332 int SalomePyQt::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx )
2333 {
2334   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, id, group, idx ) ) );
2335 }
2336
2337 /*!
2338   \brief Create main menu.
2339   \param subMenu menu name
2340   \param menu parent menu name (list of menu names separated by "|")
2341   \param id required menu ID
2342   \param group menu group ID
2343   \param idx required index in the menu
2344   \return menu ID or -1 if menu could not be added
2345 */
2346 int SalomePyQt::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx )
2347 {
2348   return ProcessEvent( new TCreateMenuEvent( CrMenu( subMenu, menu, id, group, idx ) ) );
2349 }
2350
2351 /*!
2352   \brief Insert action to the main menu.
2353   \param id action ID
2354   \param menu parent menu ID
2355   \param group menu group ID
2356   \param idx required index in the menu
2357   \return action ID or -1 if action could not be added
2358 */
2359 int SalomePyQt::createMenu( const int id, const int menu, const int group, const int idx )
2360 {
2361   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
2362 }
2363
2364 /*!
2365   \brief Insert action to the main menu.
2366   \param id action ID
2367   \param menu parent menu name (list of menu names separated by "|")
2368   \param group menu group ID
2369   \param idx required index in the menu
2370   \return action ID or -1 if action could not be added
2371 */
2372 int SalomePyQt::createMenu( const int id, const QString& menu, const int group, const int idx )
2373 {
2374   return ProcessEvent( new TCreateMenuEvent( CrMenu( id, menu, group, idx ) ) );
2375 }
2376
2377 /*!
2378   \brief Insert action to the main menu.
2379   \param a action
2380   \param menu parent menu ID
2381   \param group menu group ID
2382   \param idx required index in the menu
2383   \return action ID or -1 if action could not be added
2384 */
2385 int SalomePyQt::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
2386 {
2387   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
2388 }
2389
2390 /*!
2391   \brief Insert action to the main menu.
2392   \param a action
2393   \param menu parent menu name (list of menu names separated by "|")
2394   \param group menu group ID
2395   \param idx required index in the menu
2396   \return action ID or -1 if action could not be added
2397 */
2398 int SalomePyQt::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
2399 {
2400   return ProcessEvent( new TCreateMenuEvent( CrMenu( a, menu, id, group, idx ) ) );
2401 }
2402
2403 /*!
2404   \fn QAction* SalomePyQt::createSeparator();
2405   \brief Create separator action which can be used in the menu or toolbar.
2406   \return new separator action
2407 */
2408
2409 class TCreateSepEvent: public SALOME_Event 
2410 {
2411 public:
2412   typedef QAction* TResult;
2413   TResult myResult;
2414   TCreateSepEvent() 
2415     : myResult( 0 ) {}
2416   virtual void Execute() 
2417   {
2418     LightApp_Module* module = getActiveModule();
2419     if ( module )
2420       myResult = (QAction*)module->separator();
2421   }
2422 };
2423 QAction* SalomePyQt::createSeparator()
2424 {
2425   return ProcessEvent( new TCreateSepEvent() );
2426 }
2427
2428 /*!
2429   \fn QAction* SalomePyQt::createAction( const int      id,
2430                                          const QString& menuText, 
2431                                          const QString& tipText, 
2432                                          const QString& statusText, 
2433                                          const QString& icon,
2434                                          const int      key, 
2435                                          const bool     toggle );
2436   \brief Create an action which can be then used in the menu or toolbar.
2437   \param id the unique id action to be registered to
2438   \param menuText action text which should appear in menu
2439   \param tipText text which should appear in the tooltip
2440   \param statusText text which should appear in the status bar when action is activated
2441   \param icon the name of the icon file (the actual icon file name can be coded in the translation files)
2442   \param key the key accelrator for the action
2443   \param toggle if \c true the action is checkable
2444 */
2445
2446 class TCreateActionEvent: public SALOME_Event 
2447 {
2448 public:
2449   typedef QAction* TResult;
2450   TResult myResult;
2451   int     myId;
2452   QString myMenuText;
2453   QString myTipText;
2454   QString myStatusText;
2455   QString myIcon;
2456   int     myKey;
2457   bool    myToggle;
2458   TCreateActionEvent( const int id, const QString& menuText, const QString& tipText, 
2459                       const QString& statusText, const QString& icon, const int key, const bool toggle ) 
2460     : myResult( 0 ), myId( id ), myMenuText( menuText ), myTipText( tipText ),
2461       myStatusText( statusText ), myIcon( icon ), myKey( key ), myToggle( toggle ) {}
2462   virtual void Execute()
2463   {
2464     LightApp_Module* module = getActiveModule();
2465     if ( module ) {
2466       QIcon icon = loadIconInternal( module->name(), myIcon );
2467       myResult = (QAction*)module->action( myId );
2468       if ( myResult ) {
2469         if ( myResult->toolTip().isEmpty() && !myTipText.isEmpty() ) 
2470           myResult->setToolTip( myTipText );
2471         if ( myResult->text().isEmpty() && !myMenuText.isEmpty() )
2472           myResult->setText( myMenuText );
2473         if ( myResult->icon().isNull() && !icon.isNull() ) 
2474           myResult->setIcon( icon );
2475         if ( myResult->statusTip().isEmpty() && !myStatusText.isEmpty() )
2476           myResult->setStatusTip( myStatusText );
2477         if ( myResult->shortcut().isEmpty() && myKey )
2478           myResult->setShortcut( myKey );
2479         if ( myResult->isCheckable() != myToggle )
2480           myResult->setCheckable( myToggle );
2481       }
2482       else {
2483         myResult = (QAction*)module->createAction( myId, myTipText, icon, myMenuText, myStatusText, myKey, module, myToggle );
2484       }
2485       // for Python module, automatically connect action to callback slot
2486       PyModuleHelper* helper = module->findChild<PyModuleHelper*>( "python_module_helper" );
2487       if ( helper ) helper->connectAction( myResult );
2488     }
2489   }
2490 };
2491 QAction* SalomePyQt::createAction( const int id,           const QString& menuText, 
2492                                    const QString& tipText, const QString& statusText, 
2493                                    const QString& icon,    const int key, const bool toggle )
2494 {
2495   return ProcessEvent( new TCreateActionEvent( id, menuText, tipText, statusText, icon, key, toggle ) );
2496 }
2497
2498 /*!
2499   \fn QtxActionGroup* SalomePyQt::createActionGroup( const int id, const bool exclusive );
2500   \brief Create an action group which can be then used in the menu or toolbar
2501   \param id         : the unique id action group to be registered to
2502   \param exclusive  : if \c true the action group does exclusive toggling
2503 */
2504
2505 struct TCreateActionGroupEvent: public SALOME_Event 
2506 {
2507   typedef QtxActionGroup* TResult;
2508   TResult myResult;
2509   int     myId;
2510   bool    myExclusive;
2511   TCreateActionGroupEvent( const int id, const bool exclusive )
2512     : myId( id ), myExclusive( exclusive ) {}
2513   virtual void Execute()
2514   {
2515     LightApp_Module* module = getActiveModule();
2516     if ( module )
2517       myResult = module->createActionGroup( myId, myExclusive );
2518   }
2519 };
2520 QtxActionGroup* SalomePyQt::createActionGroup( const int id, const bool exclusive )
2521 {
2522   return ProcessEvent( new TCreateActionGroupEvent( id, exclusive ) );
2523 }
2524
2525 /*!
2526   \fn QAction* SalomePyQt::action( const int id );
2527   \brief Get action by specified identifier.
2528   \return action or 0 if action is not registered
2529 */
2530
2531 class TActionEvent: public SALOME_Event 
2532 {
2533 public:
2534   typedef QAction* TResult;
2535   TResult myResult;
2536   int     myId;
2537   TActionEvent( const int id )
2538     : myResult( 0 ), myId( id ) {}
2539   virtual void Execute()
2540   {
2541     LightApp_Module* module = getActiveModule();
2542     if ( module )
2543       myResult = (QAction*)module->action( myId );
2544   }
2545 };
2546 QAction* SalomePyQt::action( const int id )
2547 {
2548   return ProcessEvent( new TActionEvent( id ) );
2549 }
2550
2551 /*!
2552   \fn int SalomePyQt::actionId( const QAction* a );
2553   \brief Get an action identifier. 
2554   \return action ID or -1 if action is not registered
2555 */
2556
2557 class TActionIdEvent: public SALOME_Event 
2558 {
2559 public:
2560   typedef  int TResult;
2561   TResult  myResult;
2562   const QAction* myAction;
2563   TActionIdEvent( const QAction* action )
2564     : myResult( -1 ), myAction( action ) {}
2565   virtual void Execute()
2566   {
2567     LightApp_Module* module = getActiveModule();
2568     if ( module )
2569       myResult = module->actionId( myAction );
2570   }
2571 };
2572 int SalomePyQt::actionId( const QAction* a )
2573 {
2574   return ProcessEvent( new TActionIdEvent( a ) );
2575 }
2576
2577 /*!
2578   \fn int SalomePyQt::addGlobalPreference( const QString& label );
2579   \brief Add global (not module-related) preferences group.
2580   \param label global preferences group name
2581   \return preferences group identifier
2582 */
2583
2584 class TAddGlobalPrefEvent: public SALOME_Event
2585 {
2586 public:
2587   typedef int TResult;
2588   TResult myResult;
2589   QString myLabel;
2590   TAddGlobalPrefEvent( const QString& label )
2591     : myResult( -1 ), myLabel( label ) {}
2592   virtual void Execute() 
2593   {
2594     LightApp_Module* module = getActiveModule();
2595     if ( module ) {
2596       LightApp_Preferences* pref = module->getApp()->preferences();
2597       if ( pref )
2598         myResult = pref->addPreference( myLabel, -1 );
2599     }
2600   }
2601 };
2602 int SalomePyQt::addGlobalPreference( const QString& label )
2603 {
2604   return ProcessEvent( new TAddGlobalPrefEvent( label ) );
2605 }
2606
2607 /*!
2608   \fn int SalomePyQt::addPreference( const QString& label );
2609   \brief Add module-related preferences group.
2610   \param label preferences group name
2611   \return preferences group identifier
2612 */
2613
2614 class TAddPrefEvent: public SALOME_Event 
2615 {
2616 public:
2617   typedef int TResult;
2618   TResult myResult;
2619   QString myLabel;
2620   TAddPrefEvent( const QString& label )
2621     : myResult( -1 ), myLabel( label ) {}
2622   virtual void Execute() 
2623   {
2624     LightApp_Module* module = getActiveModule();
2625     if ( module ) {
2626       LightApp_Preferences* pref = module->getApp()->preferences();
2627       if ( pref ) {
2628         int cId = pref->addPreference( module->moduleName(), -1 );
2629         if ( cId != -1 )
2630           myResult = pref->addPreference( myLabel, cId );
2631       }
2632     }
2633   }
2634 };
2635 int SalomePyQt::addPreference( const QString& label )
2636 {
2637   return ProcessEvent( new TAddPrefEvent( label ) );
2638 }
2639
2640 /*!
2641   \fn int SalomePyQt::addPreference( const QString& label, const int pId, const int type,
2642                                      const QString& section, const QString& param );
2643   \brief Add module-related preferences.
2644   \param label preferences group name
2645   \param pId parent preferences group id
2646   \param type preferences type
2647   \param section resources file section name
2648   \param param resources file setting name
2649   \return preferences identifier
2650 */
2651
2652 class TAddPrefParamEvent: public SALOME_Event
2653 {
2654 public:
2655   typedef int TResult;
2656   TResult myResult;
2657   QString myLabel;
2658   int     myPId;
2659   int     myType;
2660   QString mySection;
2661   QString myParam;
2662   TAddPrefParamEvent( const QString& label, 
2663                       const int pId, const int type,
2664                       const QString& section, 
2665                       const QString& param )
2666     : myResult( -1 ),
2667       myLabel( label ), myPId( pId ), myType( type ), 
2668       mySection( section ), myParam ( param ) {}
2669   virtual void Execute()
2670   {
2671     LightApp_Module* module = getActiveModule();
2672     if ( module ) {
2673       LightApp_Preferences* pref = module->getApp()->preferences();
2674       if ( pref )
2675         myResult = pref->addPreference( module->moduleName(), myLabel, myPId, myType, mySection, myParam );
2676     }
2677   }
2678 };
2679 int SalomePyQt::addPreference( const QString& label, const int pId, const int type,
2680                                const QString& section, const QString& param )
2681 {
2682   return ProcessEvent( new TAddPrefParamEvent( label, pId, type, section, param ) );
2683 }
2684
2685 /*!
2686   \fn QVariant SalomePyQt::preferenceProperty( const int id, const QString& prop );
2687   \brief Get the preferences property.
2688   \param id preferences identifier
2689   \param prop preferences property name
2690   \return preferences property value or null QVariant if property is not set
2691 */
2692
2693 class TPrefPropEvent: public SALOME_Event
2694 {
2695 public:
2696   typedef QVariant TResult;
2697   TResult myResult;
2698   int     myId;
2699   QString myProp;
2700   TPrefPropEvent( const int id, const QString& prop )
2701     : myId( id ), myProp( prop ) {}
2702   virtual void Execute()
2703   {
2704     LightApp_Module* module = getActiveModule();
2705     if ( module ) {
2706       LightApp_Preferences* pref = module->getApp()->preferences();
2707       if ( pref )
2708         myResult = pref->itemProperty( myProp, myId );
2709     }
2710   }
2711 };
2712 QVariant SalomePyQt::preferenceProperty( const int id, const QString& prop )
2713 {
2714   return ProcessEvent( new TPrefPropEvent( id, prop ) );
2715 }
2716
2717 /*!
2718   \brief Set the preferences property.
2719   \param id preferences identifier
2720   \param prop preferences property name
2721   \param var preferences property value
2722 */
2723 void SalomePyQt::setPreferenceProperty( const int id, 
2724                                         const QString& prop,
2725                                         const QVariant& var )
2726 {
2727   class TEvent: public SALOME_Event
2728   {
2729     int      myId;
2730     QString  myProp;
2731     QVariant myVar;
2732   public:
2733     TEvent( const int id, const QString& prop, const QVariant& var ) 
2734       : myId( id ), myProp( prop ), myVar( var ) {}
2735     virtual void Execute() 
2736     {
2737       LightApp_Module* module = getActiveModule();
2738       if ( module ) {
2739         LightApp_Preferences* pref = module->getApp()->preferences();
2740         if ( pref )
2741           pref->setItemProperty( myProp, myVar, myId );
2742       }
2743     }
2744   };
2745   ProcessVoidEvent( new TEvent( id, prop, var ) );
2746 }
2747
2748 /*!
2749   \brief Set specific widget as a custom preferences item.
2750   \param id preferences identifier
2751   \param prop preferences property name
2752   \param widget custom widget
2753 */
2754 void SalomePyQt::setPreferencePropertyWg( const int id, 
2755                                           const QString& prop,
2756                                           UserDefinedContent* widget )
2757 {
2758   class TEvent: public SALOME_Event
2759   {
2760     int      myId;
2761     QString  myProp;
2762     UserDefinedContent* myWidget;
2763   public:
2764     TEvent( const int id, const QString& prop, UserDefinedContent* widget ) 
2765       : myId( id ), myProp( prop ), myWidget( widget ) {}
2766     virtual void Execute() 
2767     {
2768       LightApp_Module* module = getActiveModule();
2769       if ( module ) {
2770         LightApp_Preferences* pref = module->getApp()->preferences();
2771         if ( pref ) {
2772           pref->setItemProperty( myProp, (qint64) new SgPyQtUserDefinedContent( myWidget ), myId );
2773         }
2774       }
2775     }
2776   };
2777   ProcessVoidEvent( new TEvent( id, prop, widget ) );
2778 }
2779
2780 /*!
2781   \brief Add the property value to the list of values.
2782
2783   This method allows creating properties which are QList<QVariant>
2784   - there is no way to pass such values directly to QVariant parameter with PyQt.
2785
2786   \param id preferences identifier
2787   \param prop preferences property name
2788   \param idx preferences property index
2789   \param var preferences property value for the index \a idx
2790 */
2791 void SalomePyQt::addPreferenceProperty( const int id, 
2792                                         const QString& prop,
2793                                         const int idx, 
2794                                         const QVariant& var )
2795 {
2796   class TEvent: public SALOME_Event
2797   {
2798     int      myId;
2799     QString  myProp;
2800     int      myIdx;
2801     QVariant myVar;
2802   public:
2803     TEvent( const int id, const QString& prop, const int idx, const QVariant& var ) 
2804       : myId( id ), myProp( prop ), myIdx( idx), myVar( var ) {}
2805     virtual void Execute()
2806     {
2807       LightApp_Module* module = getActiveModule();
2808       if ( module ) {
2809         LightApp_Preferences* pref = module->getApp()->preferences();
2810         if ( pref ) {
2811           QVariant var =  pref->itemProperty( myProp, myId );
2812           if ( var.isValid() ) {
2813             if ( var.type() == QVariant::StringList ) {
2814               QStringList sl = var.toStringList();
2815               if ( myIdx >= 0 && myIdx < sl.count() ) 
2816                 sl[myIdx] = myVar.toString();
2817               else
2818                 sl.append( myVar.toString() );
2819               pref->setItemProperty( myProp, sl, myId );
2820             }
2821             else if ( var.type() == QVariant::List ) {
2822               QList<QVariant> vl = var.toList();
2823               if ( myIdx >= 0 && myIdx < vl.count() ) 
2824                 vl[myIdx] = myVar;
2825               else
2826                 vl.append( myVar );
2827               pref->setItemProperty( myProp, vl, myId );
2828             }
2829           }
2830           else {
2831             QList<QVariant> vl;
2832             vl.append( myVar );
2833             pref->setItemProperty( myProp, vl, myId );
2834           }
2835         }
2836       }
2837     }
2838   };
2839   ProcessVoidEvent( new TEvent( id, prop, idx, var) );
2840 }
2841
2842 /*!
2843   \brief Put the message to the Log messages output window
2844   \param msg message text (it can be of simple rich text format)
2845   \param addSeparator boolean flag which specifies if it is necessary 
2846          to separate the message with predefined separator
2847 */
2848 void SalomePyQt::message( const QString& msg, bool addSeparator )
2849 {
2850   class TEvent: public SALOME_Event
2851   {
2852     QString  myMsg;
2853     bool     myAddSep;
2854   public:
2855     TEvent( const QString& msg, bool addSeparator ) 
2856       : myMsg( msg ), myAddSep( addSeparator ) {}
2857     virtual void Execute()
2858     {
2859       if ( LightApp_Application* anApp = getApplication() ) {
2860         LogWindow* lw = anApp->logWindow();
2861         if ( lw )
2862           lw->putMessage( myMsg, myAddSep );
2863       }
2864     }
2865   };
2866   ProcessVoidEvent( new TEvent( msg, addSeparator ) );
2867 }
2868
2869 /*!
2870   \brief Remove all the messages from the Log messages output window.
2871 */
2872 void SalomePyQt::clearMessages()
2873 {
2874   class TEvent: public SALOME_Event
2875   {
2876   public:
2877     TEvent() {}
2878     virtual void Execute()
2879     {
2880       if ( LightApp_Application* anApp = getApplication() ) {
2881         LogWindow* lw = anApp->logWindow();
2882         if ( lw )
2883           lw->clear();
2884       }
2885     }
2886   };
2887   ProcessVoidEvent( new TEvent() );
2888 }
2889
2890 /*!
2891   \fn bool SalomePyQt::dumpView( const QString& filename, const int id = 0 );
2892   \brief Dump the contents of the id view window. If id is 0 then current active view is processed. 
2893   to the image file in the specified format.
2894
2895   For the current moment JPEG, PNG and BMP images formats are supported.
2896   The image format is defined automatically by the file name extension.
2897   By default, BMP format is used.
2898
2899   \param filename image file name
2900   \return operation status (\c true on success)
2901 */
2902
2903 class TDumpViewEvent: public SALOME_Event 
2904 {
2905 public:
2906   typedef bool TResult;
2907   TResult myResult;
2908   QString myFileName;
2909   int myWndId;
2910   TDumpViewEvent( const QString& filename, const int id ) 
2911     : myResult ( false ), myFileName( filename ), myWndId( id ) {}
2912   virtual void Execute() 
2913   {
2914     SUIT_ViewWindow* wnd = 0;
2915     if ( !myWndId ) {
2916       if ( LightApp_Application* anApp = getApplication() ) {
2917         SUIT_ViewManager* vm = anApp->activeViewManager();
2918         if ( vm )
2919           wnd = vm->getActiveView();
2920       }
2921       myWndId = wnd->getId();
2922     }
2923     else {
2924       wnd = dynamic_cast<SUIT_ViewWindow*>( getWnd( myWndId ) );
2925     }
2926     if ( wnd ) {
2927       QString fmt = SUIT_Tools::extension( myFileName ).toUpper();
2928 #ifndef DISABLE_PLOT2DVIEWER
2929       Plot2d_ViewWindow* wnd2D = dynamic_cast<Plot2d_ViewWindow*>( wnd );
2930       if ( wnd2D ) {
2931         qApp->postEvent( wnd2D->getViewFrame(), new QPaintEvent( QRect( 0, 0, wnd2D->getViewFrame()->width(), wnd2D->getViewFrame()->height() ) ) );
2932         qApp->postEvent( wnd2D, new QPaintEvent( QRect( 0, 0, wnd2D->width(), wnd2D->height() ) ) );
2933         qApp->processEvents();
2934         if ( fmt == "PS" || fmt == "EPS" || fmt == "PDF" ) {
2935           myResult = wnd2D->getViewFrame()->print( myFileName, fmt );
2936           return;
2937         }
2938       }
2939 #endif // DISABLE_PLOT2DVIEWER
2940       QImage im = wnd->dumpView();
2941       if ( !im.isNull() && !myFileName.isEmpty() ) {
2942         if ( fmt.isEmpty() ) fmt = QString( "BMP" ); // default format
2943         if ( fmt == "JPG" )  fmt = "JPEG";
2944         myResult = im.save( myFileName, fmt.toLatin1() );
2945       }
2946     }
2947   }
2948 };
2949 bool SalomePyQt::dumpView( const QString& filename, const int id )
2950 {
2951   return ProcessEvent( new TDumpViewEvent( filename, id ) );
2952 }
2953
2954 /*!
2955   \fn QList<int> SalomePyQt::getViews();
2956   \brief Get list of integer identifiers of all the currently opened views
2957   \return list of integer identifiers of all the currently opened views
2958 */
2959
2960 class TGetViews: public SALOME_Event
2961 {
2962 public:
2963   typedef QList<int> TResult;
2964   TResult myResult;
2965   TGetViews() {}
2966   virtual void Execute() 
2967   {
2968     myResult.clear();
2969     LightApp_Application* app  = getApplication();
2970     if ( app ) {
2971       STD_TabDesktop* tabDesk = dynamic_cast<STD_TabDesktop*>( app->desktop() );
2972       if ( tabDesk ) {
2973         QList<SUIT_ViewWindow*> wndlist = tabDesk->windows();
2974         SUIT_ViewWindow* wnd;
2975         foreach ( wnd, wndlist )
2976           myResult.append( wnd->getId() );
2977       }
2978     }
2979   }
2980 };
2981 QList<int> SalomePyQt::getViews()
2982 {
2983   return ProcessEvent( new TGetViews() );
2984 }
2985
2986 /*!
2987   \fn int SalomePyQt::getActiveView();
2988   \brief Get integer identifier of the currently active view
2989   \return integer identifier of the currently active view
2990 */
2991
2992 class TGetActiveView: public SALOME_Event
2993 {
2994 public:
2995   typedef int TResult;
2996   TResult myResult;
2997   TGetActiveView()
2998     : myResult( -1 ) {}
2999   virtual void Execute() 
3000   {
3001     LightApp_Application* app = getApplication();
3002     if ( app ) {
3003       SUIT_ViewManager* viewMgr = app->activeViewManager();
3004       if ( viewMgr ) {
3005         SUIT_ViewWindow* wnd = viewMgr->getActiveView();
3006         if ( wnd )
3007           myResult = wnd->getId();
3008       }
3009     }
3010   }
3011 };
3012 int SalomePyQt::getActiveView()
3013 {
3014   return ProcessEvent( new TGetActiveView() );
3015 }
3016
3017 /*!                      
3018   \fn QString SalomePyQt::getViewType( const int id );
3019   \brief Get type of the specified view, e.g. "OCCViewer"
3020   \param id window identifier
3021   \return view type
3022 */ 
3023
3024 class TGetViewType: public SALOME_Event
3025 {
3026 public:
3027   typedef QString TResult;
3028   TResult myResult;
3029   int myWndId;
3030   TGetViewType( const int id )
3031     : myWndId( id ) {}
3032   virtual void Execute() 
3033   {
3034     SUIT_ViewWindow* wnd = getWnd( myWndId );
3035     if ( wnd ) {
3036       SUIT_ViewManager* viewMgr = wnd->getViewManager();
3037       if ( viewMgr )
3038         myResult = viewMgr->getType();
3039     }
3040   }
3041 };
3042 QString SalomePyQt::getViewType( const int id )
3043 {
3044   return ProcessEvent( new TGetViewType( id ) );
3045 }
3046
3047 /*!
3048   \fn bool SalomePyQt::setViewTitle( const int id, const QString& title );
3049   \brief Change view caption  
3050   \param id window identifier
3051   \param title new window title
3052   \return \c true if operation is completed successfully and \c false otherwise 
3053 */
3054
3055 class TSetViewTitle: public SALOME_Event
3056 {
3057 public:
3058   typedef bool TResult;
3059   TResult myResult;
3060   int myWndId;
3061   QString myTitle;
3062   TSetViewTitle( const int id, const QString& title )
3063     : myResult( false ),
3064       myWndId( id ),
3065       myTitle( title ) {}
3066   virtual void Execute() 
3067   {
3068     SUIT_ViewWindow* wnd = getWnd( myWndId );
3069     if ( wnd ) {
3070       wnd->setWindowTitle( myTitle );
3071       myResult = true;
3072     }
3073   }
3074 };
3075 bool SalomePyQt::setViewTitle( const int id, const QString& title )
3076 {
3077   return ProcessEvent( new TSetViewTitle( id, title ) );
3078 }
3079
3080 /*!
3081   \fn bool SalomePyQt::setViewSize( const int w, const int h, const int id );
3082   \brief Set view size
3083   \param w window width
3084   \param h window height
3085   \param id window identifier
3086   \return \c true if operation is completed successfully and \c false otherwise 
3087 */
3088
3089 class TSetViewSize: public SALOME_Event
3090 {
3091 public:
3092   typedef bool TResult;
3093   TResult myResult;
3094   int myWndWidth;
3095   int myWndHeight;
3096   int myWndId;
3097   TSetViewSize( const int w, const int h, const int id )
3098     : myResult( false ),
3099       myWndWidth( w ),
3100       myWndHeight( h ),
3101       myWndId( id ) {}
3102   virtual void Execute() 
3103   {
3104     SUIT_ViewWindow* wnd = 0;
3105     if ( !myWndId ) {
3106       if ( LightApp_Application* anApp = getApplication() ) {
3107         SUIT_ViewManager* vm = anApp->activeViewManager();
3108         if ( vm )
3109           wnd = vm->getActiveView();
3110       }
3111     }
3112     else {
3113       wnd = dynamic_cast<SUIT_ViewWindow*>( getWnd( myWndId ) );
3114     }
3115     if ( wnd ) {
3116       SUIT_ViewManager* viewMgr = wnd->getViewManager();
3117       if ( viewMgr ) {
3118         QString type = viewMgr->getType();
3119         if ( type == "OCCViewer") {
3120 #ifndef DISABLE_OCCVIEWER
3121           // specific processing for OCC viewer:
3122           // OCC view can embed up to 4 sub-views, split according to the specified layout;
3123           // - if there is only one sub-view active; it will be resized;
3124           // - if there are several sub-views, each of them will be resized.
3125           OCCViewer_ViewWindow* occView = qobject_cast<OCCViewer_ViewWindow*>( wnd );
3126           for ( int i = OCCViewer_ViewFrame::BOTTOM_RIGHT; i <= OCCViewer_ViewFrame::TOP_RIGHT; i++ ) {
3127             if ( occView && occView->getView( i ) ) {
3128               occView->getView( i )->centralWidget()->resize( myWndWidth, myWndHeight );
3129               myResult = true;
3130             }
3131           }
3132 #endif // DISABLE_OCCVIEWER
3133         }
3134         else if ( type == "ParaView") {
3135 #ifndef DISABLE_PVVIEWER
3136           // specific processing for ParaView viewer:
3137           // hierarchy of ParaView viewer is much complex than for usual view;
3138           // we look for sub-widget named "Viewport"
3139           QList<QWidget*> lst = wnd->findChildren<QWidget*>( "Viewport" );
3140           if ( !lst.isEmpty() ) {
3141             lst[0]->resize( myWndWidth, myWndHeight );
3142             myResult = true;
3143           }
3144 #endif // DISABLE_PVVIEWER
3145         }
3146         else {
3147           if ( wnd->centralWidget() ) {
3148             wnd->centralWidget()->resize( myWndWidth, myWndHeight );
3149             myResult = true;
3150           }
3151         }
3152       }
3153     }
3154   }
3155 };
3156 bool SalomePyQt::setViewSize( const int w, const int h, const int id )
3157 {
3158   return ProcessEvent( new TSetViewSize( w, h, id ) );
3159 }
3160
3161 /*!
3162   \fn bool SalomePyQt::setViewRotationPoint( const double x, const double y, const double z, const int id );
3163   \brief Set view rotation point
3164   \param x coordinate X view rotation point
3165   \param y coordinate Y view rotation point
3166   \param z coordinate Z view rotation point
3167   \param id window identifier
3168   \return \c true if operation is completed successfully and \c false otherwise 
3169 */
3170
3171 class TSetViewRotationPoint: public SALOME_Event
3172 {
3173 public:
3174   typedef bool TResult;
3175   TResult myResult;
3176   double myX;
3177   double myY;
3178   double myZ;
3179   int myWndId;
3180   TSetViewRotationPoint( const double x, const double y, const double z, const int id )
3181     : myResult( false ),
3182       myX( x ),
3183       myY( y ),
3184       myZ( z ),
3185       myWndId( id ) {}
3186   virtual void Execute() 
3187   {
3188     SUIT_ViewWindow* wnd = 0;
3189     if ( !myWndId ) {
3190       if ( LightApp_Application* anApp = getApplication() ) {
3191         SUIT_ViewManager* vm = anApp->activeViewManager();
3192         if ( vm )
3193           wnd = vm->getActiveView();
3194       }
3195     }
3196     else {
3197       wnd = dynamic_cast<SUIT_ViewWindow*>( getWnd( myWndId ) );
3198     }
3199     if ( wnd ) {
3200       SUIT_ViewManager* viewMgr = wnd->getViewManager();
3201       if ( viewMgr ) {
3202         QString type = viewMgr->getType();
3203         if ( type == "OCCViewer") {
3204 #ifndef DISABLE_OCCVIEWER
3205           // specific processing for OCC viewer:
3206           // OCC view can embed up to 4 sub-views, split according to the specified layout;
3207           // - if there is only one sub-view active; its rotation point will be changed;
3208           // - if there are several sub-views, rotaion points of each of them will be changed.
3209           OCCViewer_ViewWindow* occView = qobject_cast<OCCViewer_ViewWindow*>( wnd );
3210           if ( occView ) {
3211             for ( int i = OCCViewer_ViewFrame::BOTTOM_RIGHT; i <= OCCViewer_ViewFrame::TOP_RIGHT; i++ ) {
3212               if ( occView && occView->getView( i ) ) {
3213                 occView->getView( i )->activateSetRotationSelected( myX, myY, myZ );
3214                 myResult = true;
3215               }
3216             }
3217           }
3218 #endif // DISABLE_OCCVIEWER
3219         }
3220         else if ( type == "VTKViewer") {
3221 #ifndef DISABLE_VTKVIEWER
3222           SVTK_ViewWindow* vtkView = qobject_cast<SVTK_ViewWindow*>( wnd );
3223           if ( vtkView )
3224           {
3225             double aCenter[3] = { myX, myY, myZ };
3226             vtkView->activateSetRotationSelected( (void*)aCenter );
3227             myResult = true;
3228           }
3229 #endif // DISABLE_VTKVIEWER
3230         }
3231       }
3232     }
3233   }
3234 };
3235 bool SalomePyQt::setViewRotationPoint( const double x, const double y, const double z, const int id )
3236 {
3237   return ProcessEvent( new TSetViewRotationPoint( x, y, z, id ) );
3238 }
3239
3240 /*!
3241   \fn QString SalomePyQt::getViewTitle( const int id );
3242   \brief Get view caption  
3243   \param id window identifier
3244   \return view caption  
3245 */
3246
3247 class TGetViewTitle: public SALOME_Event
3248 {
3249 public:
3250   typedef QString TResult;
3251   TResult myResult;
3252   int myWndId;
3253   TGetViewTitle( const int id )
3254     : myWndId( id ) {}
3255   virtual void Execute() 
3256   {
3257     SUIT_ViewWindow* wnd = getWnd( myWndId );
3258     if ( wnd )
3259       myResult = wnd->windowTitle();
3260   }
3261 };
3262 QString SalomePyQt::getViewTitle( const int id )
3263 {
3264   return ProcessEvent( new TGetViewTitle( id ) );
3265 }
3266
3267 /*!
3268   \fn QList<int> SalomePyQt::findViews( const QString& type );
3269   \brief Get list of integer identifiers of all the 
3270          currently opened views of the specified type
3271   \param type viewer type
3272   \return list of integer identifiers 
3273 */
3274
3275 class TFindViews: public SALOME_Event
3276 {
3277 public:
3278   typedef QList<int> TResult;
3279   TResult myResult;
3280   QString myType;
3281   TFindViews( const QString& type )
3282     : myType( type ) {}
3283   virtual void Execute() 
3284   {
3285     myResult.clear();
3286     LightApp_Application* app  = getApplication();
3287     if ( app ) {
3288       ViewManagerList vmList;
3289       app->viewManagers( myType, vmList );
3290       SUIT_ViewManager* viewMgr;
3291       foreach ( viewMgr, vmList ) {
3292         QVector<SUIT_ViewWindow*> vec = viewMgr->getViews();
3293         for ( int i = 0, n = vec.size(); i < n; i++ ) {
3294           SUIT_ViewWindow* wnd = vec[ i ];
3295           if ( wnd )
3296             {
3297               MESSAGE("SUIT_ViewWindow*: "<< wnd << " id: " << wnd->getId());
3298               myResult.append( wnd->getId() );
3299             }
3300         }
3301       }
3302     }
3303   }
3304 };
3305 QList<int> SalomePyQt::findViews( const QString& type )
3306 {
3307   return ProcessEvent( new TFindViews( type ) );
3308 }
3309
3310 /*!
3311   \fn bool SalomePyQt::activateView( const int id );
3312   \brief Activate view
3313   \param id window identifier
3314   \return \c true if operation is completed successfully and \c false otherwise 
3315 */
3316
3317 class TActivateView: public SALOME_Event
3318 {
3319 public:
3320   typedef bool TResult;
3321   TResult myResult;
3322   int myWndId;
3323   TActivateView( const int id )
3324     : myResult( false ),
3325       myWndId( id ) {}
3326   virtual void Execute() 
3327   {
3328     SUIT_ViewWindow* wnd = getWnd( myWndId );
3329     MESSAGE("window id:" << myWndId << " SUIT_ViewWindow*: " << wnd);
3330     if ( wnd ) {
3331       wnd->setFocus();
3332       myResult = true;
3333     }
3334   }
3335 };
3336 bool SalomePyQt::activateView( const int id )
3337 {
3338   return ProcessEvent( new TActivateView( id ) );
3339 }
3340
3341 /*!
3342   \fn bool SalomePyQt::activateManagerAndView( const int id );
3343   \brief Activate view manager and view: useful for a view embedded in a module main Window
3344   \param id window identifier
3345   \return \c true if operation is completed successfully and \c false otherwise
3346  */
3347
3348 class TActivateViewManagerAndView: public SALOME_Event
3349 {
3350 public:
3351   typedef bool TResult;
3352   TResult myResult;
3353   int myWndId;
3354   TActivateViewManagerAndView( const int id )
3355     : myResult( false ),
3356       myWndId( id ) {}
3357   virtual void Execute()
3358   {
3359     SUIT_ViewWindow* wnd = getWnd( myWndId );
3360     MESSAGE("window id:" << myWndId << " SUIT_ViewWindow*: " << wnd);
3361     if ( wnd )
3362       {
3363         LightApp_Application* app  = getApplication();
3364         app->desktop()->windowActivated(wnd); // equivalent to app->setActiveViewManager(wnd->getViewManager())
3365         wnd->setFocus();
3366         myResult = true;
3367       }
3368   }
3369 };
3370 bool SalomePyQt::activateViewManagerAndView( const int id )
3371 {
3372   return ProcessEvent( new TActivateViewManagerAndView( id ) );
3373 }
3374
3375 /*!
3376  *
3377  */
3378
3379 class TGetViewWidget: public SALOME_Event
3380 {
3381 public:
3382   typedef QWidget* TResult;
3383   TResult myResult;
3384   int myWndId;
3385   TGetViewWidget( const int id )
3386     : myResult( 0 ),
3387       myWndId( id ) {}
3388   virtual void Execute()
3389   {
3390     SUIT_ViewWindow* wnd = getWnd( myWndId );
3391     if ( wnd ) {
3392         myResult = (QWidget*)wnd;
3393     }
3394   }
3395 };
3396 QWidget* SalomePyQt::getViewWidget( const int id)
3397 {
3398   return ProcessEvent( new TGetViewWidget( id ) );
3399 }
3400
3401
3402 /*!
3403   \fn int SalomePyQt::createView( const QString& type, bool visible = true, const int width = 0, const int height = 0 );
3404   \brief Create new view and activate it
3405   \param type viewer type
3406   \param visible
3407   \param width
3408   \param height
3409   \return integer identifier of created view (or -1 if view could not be created)
3410 */
3411
3412 class TCreateView: public SALOME_Event
3413 {
3414 public:
3415   typedef int TResult;
3416   TResult myResult;
3417   QString myType;
3418   bool myVisible;
3419   int myWidth;
3420   int myHeight;
3421   bool myDetached;
3422   TCreateView( const QString& theType, bool visible, const int width, const int height, bool detached )
3423     : myResult( -1 ),
3424       myType( theType ),
3425       myVisible(visible),
3426       myWidth(width),
3427       myHeight(height),
3428       myDetached(detached) {}
3429   virtual void Execute() 
3430   {
3431     LightApp_Application* app  = getApplication();
3432     if ( app ) {
3433       SUIT_ViewManager* viewMgr = app->createViewManager( myType, myDetached );
3434       if ( viewMgr ) {
3435         QWidget* wnd = viewMgr->getActiveView();
3436         myResult = viewMgr->getActiveView()->getId();
3437         if ( wnd ) {
3438           if ( !myVisible )
3439             wnd->setVisible(false);
3440           if ( !myVisible && myWidth == 0 && myHeight == 0 ) {
3441             myWidth = 1024;
3442             myHeight = 768;
3443           }
3444           if (myWidth > 0 && myHeight > 0) {
3445 #ifndef DISABLE_PLOT2DVIEWER
3446             Plot2d_ViewWindow* wnd2D = dynamic_cast<Plot2d_ViewWindow*>( wnd );
3447             if ( wnd2D ) wnd = wnd2D->getViewFrame();
3448 #endif // DISABLE_PLOT2DVIEWER
3449             wnd->setGeometry( 0, 0, myWidth, myHeight );
3450           }
3451         }
3452       }
3453     }
3454   }
3455 };
3456 int SalomePyQt::createView( const QString& type, bool visible, const int width, const int height, bool detached )
3457 {
3458   int ret = ProcessEvent( new TCreateView( type, visible, width, height, detached ) );
3459   QCoreApplication::processEvents();
3460   return ret;
3461 }
3462
3463 /*!
3464   \fn int SalomePyQt::createView( const QString& type, QWidget* w );
3465   \brief Create new view with custom widget embedded and activate it
3466   \param type viewer type
3467   \param w custom widget
3468   \return integer identifier of created view (or -1 if view could not be created)
3469 */
3470
3471 class TCreateViewWg: public SALOME_Event
3472 {
3473 public:
3474   typedef int TResult;
3475   TResult myResult;
3476   QString myType;
3477   QWidget* myWidget;
3478   TCreateViewWg( const QString& theType, QWidget* w )
3479     : myResult( -1 ),
3480       myType( theType ),
3481       myWidget( w ) {}
3482   virtual void Execute() 
3483   {
3484     LightApp_Application* app  = getApplication();
3485     if ( app ) {
3486       SUIT_ViewManager* viewMgr = app->createViewManager( myType, myWidget );
3487       if ( viewMgr ) {
3488         SUIT_ViewWindow* wnd = viewMgr->getActiveView();
3489         if ( wnd )
3490           myResult = wnd->getId();
3491       }
3492     }
3493   }
3494 };
3495 int SalomePyQt::createView( const QString& type, QWidget* w )
3496 {
3497   int ret = ProcessEvent( new TCreateViewWg( type, w ) );
3498   QCoreApplication::processEvents();
3499   return ret;
3500 }
3501
3502 /*!
3503   \fn bool SalomePyQt::closeView( const int id );
3504   \brief Close view
3505   \param id window identifier
3506   \return \c true if operation is completed successfully and \c false otherwise 
3507 */
3508
3509 class TCloseView: public SALOME_Event
3510 {
3511 public:
3512   typedef bool TResult;
3513   TResult myResult;
3514   int myWndId;
3515   TCloseView( const int id )
3516     : myResult( false ),
3517       myWndId( id ) {}
3518   virtual void Execute() 
3519   {
3520     SUIT_ViewWindow* wnd = getWnd( myWndId );
3521     if ( wnd ) {
3522       SUIT_ViewManager* viewMgr = wnd->getViewManager();
3523       if ( viewMgr ) {
3524         wnd->close();
3525         myResult = true;
3526       }
3527     }
3528   }
3529 };
3530 bool SalomePyQt::closeView( const int id )
3531 {
3532   return ProcessEvent( new TCloseView( id ) );
3533 }
3534
3535 /*!
3536   \fn int SalomePyQt::cloneView( const int id );
3537   \brief Clone view (if this operation is supported for specified view type)
3538   \param id window identifier
3539   \return integer identifier of the cloned view or -1 or operation could not be performed
3540 */
3541
3542 class TCloneView: public SALOME_Event
3543 {
3544 public:
3545   typedef int TResult;
3546   TResult myResult;
3547   int myWndId;
3548   TCloneView( const int id )
3549     : myResult( -1 ),
3550       myWndId( id ) {}
3551   virtual void Execute() 
3552   {
3553     SUIT_ViewWindow* wnd = getWnd( myWndId );
3554     if ( wnd ) {
3555       SUIT_ViewManager* viewMgr = wnd->getViewManager();
3556       if ( viewMgr ) {
3557 #ifndef DISABLE_OCCVIEWER
3558         if ( wnd->inherits( "OCCViewer_ViewWindow" ) ) {
3559           OCCViewer_ViewWindow* occView = (OCCViewer_ViewWindow*)( wnd );
3560           occView->onCloneView();
3561           wnd = viewMgr->getActiveView();
3562           if ( wnd )
3563             myResult = wnd->getId();
3564         }
3565 #endif // DISABLE_OCCVIEWER
3566 #ifndef DISABLE_PLOT2DVIEWER
3567         if ( wnd->inherits( "Plot2d_ViewWindow" ) ) {
3568           Plot2d_ViewManager* viewMgr2d = dynamic_cast<Plot2d_ViewManager*>( viewMgr );
3569           Plot2d_ViewWindow* srcWnd2d = dynamic_cast<Plot2d_ViewWindow*>( wnd );
3570           if ( viewMgr2d && srcWnd2d ) {
3571             Plot2d_ViewWindow* resWnd = viewMgr2d->cloneView( srcWnd2d );
3572             myResult = resWnd->getId();
3573           }
3574         }
3575 #endif // DISABLE_OCCVIEWER
3576       }
3577     }
3578   }
3579 };
3580 int SalomePyQt::cloneView( const int id )
3581 {
3582   return ProcessEvent( new TCloneView( id ) );
3583 }
3584
3585 /*!
3586   \fn bool SalomePyQt::setViewVisible( const int id, const bool visible )
3587   \brief Set view visibility.
3588   \param id window identifier
3589   \param visible new visiblity
3590 */
3591
3592 void SalomePyQt::setViewVisible( const int id, const bool visible )
3593 {
3594   class TEvent: public SALOME_Event
3595   {
3596     int myWndId;
3597     bool myVisible;
3598   public:
3599     TEvent( const int id, const bool visible )
3600       : myWndId( id ), myVisible( visible ) {}
3601     virtual void Execute()
3602     {
3603       SUIT_ViewWindow* wnd = getWnd( myWndId );
3604       if ( wnd ) wnd->setVisible( myVisible );
3605     }
3606   };
3607   ProcessVoidEvent( new TEvent( id, visible ) );
3608 }
3609
3610 /*!
3611   \fn bool SalomePyQt::isViewVisible( const int id );
3612   \brief Check whether view is visible ( i.e. it is on the top of the views stack)
3613   \param id window identifier
3614   \return \c true if view is visible and \c false otherwise 
3615 */
3616
3617 class TIsViewVisible: public SALOME_Event
3618 {
3619 public:
3620   typedef bool TResult;
3621   TResult myResult;
3622   int myWndId;
3623   TIsViewVisible( const int id )
3624     : myResult( false ),
3625       myWndId( id ) {}
3626   virtual void Execute() 
3627   {
3628     SUIT_ViewWindow* wnd = getWnd( myWndId );
3629     if ( wnd )
3630     {
3631       QWidget* p = wnd->parentWidget();
3632       myResult = ( p && p->isVisibleTo( p->parentWidget() ) );
3633     }
3634   }
3635 };
3636 bool SalomePyQt::isViewVisible( const int id )
3637 {
3638   return ProcessEvent( new TIsViewVisible( id ) );
3639 }
3640   
3641 /*!
3642   \fn bool SalomePyQt::setViewClosable( const int id, const bool on );
3643   \brief Set / clear view's "closable" option. By default any view is closable
3644         (i.e. can be closed by the user).
3645   \param id window identifier
3646   \param on new "closable" option's value
3647 */
3648
3649 void SalomePyQt::setViewClosable( const int id, const bool on )
3650 {
3651   class TEvent: public SALOME_Event
3652   {
3653     int myWndId;
3654     bool myOn;
3655   public:
3656     TEvent( const int id, const bool on )
3657       : myWndId( id ), myOn( on ) {}
3658     virtual void Execute()
3659     {
3660       SUIT_ViewWindow* wnd = getWnd( myWndId );
3661       if ( wnd ) wnd->setClosable( myOn );
3662     }
3663   };
3664   ProcessVoidEvent( new TEvent( id, on ) );
3665 }
3666
3667 /*!
3668   \fn bool SalomePyQt::isViewClosable( const int id );
3669   \brief Check whether view is closable (i.e. can be closed by the user)
3670   \param id window identifier
3671   \return \c true if view is closable or \c false otherwise 
3672 */
3673
3674 class TIsViewClosable: public SALOME_Event
3675 {
3676 public:
3677   typedef bool TResult;
3678   TResult myResult;
3679   int myWndId;
3680   TIsViewClosable( const int id )
3681     : myResult( true ),
3682       myWndId( id ) {}
3683   virtual void Execute() 
3684   {
3685     SUIT_ViewWindow* wnd = getWnd( myWndId );
3686     if ( wnd )
3687       myResult = wnd->closable();
3688   }
3689 };
3690
3691 bool SalomePyQt::isViewClosable( const int id )
3692 {
3693   return ProcessEvent( new TIsViewClosable( id ) );
3694 }
3695
3696 /*!
3697   \fn bool SalomePyQt::groupAllViews();
3698   \brief Group all views to the single tab area
3699   \return \c true if operation is completed successfully and \c false otherwise 
3700 */
3701
3702 class TGroupAllViews: public SALOME_Event
3703 {
3704 public:
3705   typedef bool TResult;
3706   TResult myResult;
3707   TGroupAllViews()
3708     : myResult( false ) {}
3709   virtual void Execute() 
3710   {
3711     LightApp_Application* app  = getApplication();
3712     if ( app ) {
3713       STD_TabDesktop* tabDesk = dynamic_cast<STD_TabDesktop*>( app->desktop() );
3714       if ( tabDesk ) {
3715         QtxWorkstack* wStack = tabDesk->workstack();
3716         if ( wStack ) {
3717           wStack->stack();
3718           myResult = true;
3719         }
3720       }
3721     }
3722   }
3723 };
3724 bool SalomePyQt::groupAllViews()
3725 {
3726   return ProcessEvent( new TGroupAllViews() );
3727 }
3728
3729 /*!
3730   \fn bool SalomePyQt::splitView( const int id, const Orientation ori, const Action action );
3731   \brief Split tab area to which view with identifier belongs to
3732   \param id window identifier
3733   \param ori orientation of split operation
3734   \param action action to be performed
3735   \return \c true if operation is completed successfully \c false otherwise 
3736 */
3737
3738 class TSplitView: public SALOME_Event
3739 {
3740 public:
3741   typedef bool TResult;
3742   TResult myResult;
3743   int myWndId;
3744   Orientation myOri;
3745   Action myAction;
3746   TSplitView( const int id, 
3747               const Orientation ori, 
3748               const Action action )
3749     : myResult( false ),
3750       myWndId( id ),
3751       myOri( ori ),
3752       myAction( action ) {}
3753   virtual void Execute() 
3754   {
3755     SUIT_ViewWindow* wnd = getWnd( myWndId );
3756     if ( wnd ) {
3757       // activate view
3758       // wnd->setFocus(); ???
3759
3760       // split workstack
3761       if ( getApplication() ) {
3762         STD_TabDesktop* desk = 
3763           dynamic_cast<STD_TabDesktop*>( getApplication()->desktop() );
3764         if ( desk ) {
3765           QtxWorkstack* wStack = desk->workstack();
3766           if ( wStack ) {
3767             Qt::Orientation qtOri = 
3768               ( myOri == Horizontal ) ? Qt::Horizontal : Qt::Vertical;
3769
3770             QtxWorkstack::SplitType sType;
3771             if ( myAction == MoveWidget )
3772               sType = QtxWorkstack::SplitMove;
3773             else if ( myAction == LeaveWidget )
3774               sType = QtxWorkstack::SplitStay;
3775             else 
3776               sType = QtxWorkstack::SplitAt;
3777
3778             wStack->Split( wnd, qtOri, sType );
3779             myResult = true;
3780           }
3781         }
3782       }
3783     }
3784   }
3785 };
3786 bool SalomePyQt::splitView( const int id, const Orientation ori, const Action action )
3787 {
3788   return ProcessEvent( new TSplitView( id, ori, action ) );
3789 }
3790
3791 /*!
3792   \fn bool SalomePyQt::moveView( const int id, const int id_to, const bool before );
3793   \brief Move view with the first identifier to the same area which 
3794          another view with the second identifier belongs to
3795   \param id source window identifier
3796   \param id_to destination window identifier  
3797   param before specifies whether the first viewt has to be moved before or after 
3798         the second view
3799   \return \c true if operation is completed successfully and \c false otherwise 
3800 */
3801
3802 class TMoveView: public SALOME_Event
3803 {
3804 public:
3805   typedef bool TResult;
3806   TResult myResult;
3807   int myWndId;
3808   int myWndToId;
3809   bool myIsBefore;
3810   TMoveView( const int id, const int id_to, const bool before )
3811     : myResult( false ),
3812     myWndId( id ),
3813     myWndToId( id_to ),
3814     myIsBefore( before ) {}
3815   virtual void Execute() 
3816   {
3817     SUIT_ViewWindow* wnd = getWnd( myWndId );
3818     SUIT_ViewWindow* wnd_to = getWnd( myWndToId );
3819     if ( wnd && wnd_to ) {
3820       QtxWorkstack* wStack = dynamic_cast<STD_TabDesktop*>( 
3821         getApplication()->desktop() )->workstack();
3822       if ( wStack )
3823         myResult = wStack->move( wnd, wnd_to, myIsBefore );
3824     }
3825   }
3826 };
3827 bool SalomePyQt::moveView( const int id, const int id_to, const bool before )
3828 {
3829   return ProcessEvent( new TMoveView( id, id_to, before ) );
3830 }
3831
3832 /*!
3833   \fn QList<int> SalomePyQt::neighbourViews( const int id );
3834   \brief Get list of views identifiers that belongs to the same area as 
3835          specified view (excluding it)
3836   \param id window identifier
3837   \return list of views identifiers
3838 */
3839
3840 class TNeighbourViews: public SALOME_Event
3841 {
3842 public:
3843   typedef QList<int> TResult;
3844   TResult myResult;
3845   int myWndId;
3846   TNeighbourViews( const int id )
3847     : myWndId( id ) {}
3848   virtual void Execute() 
3849   {
3850     myResult.clear();
3851     SUIT_ViewWindow* wnd = getWnd( myWndId );
3852     if ( wnd ) {
3853       QtxWorkstack* wStack = dynamic_cast<STD_TabDesktop*>( 
3854         getApplication()->desktop() )->workstack();
3855       if ( wStack ) {
3856         QWidgetList wgList = wStack->windowList( wnd );
3857         QWidget* wg;
3858         foreach ( wg, wgList ) {
3859           SUIT_ViewWindow* tmpWnd = dynamic_cast<SUIT_ViewWindow*>( wg );
3860           if ( tmpWnd && tmpWnd != wnd )
3861             myResult.append( tmpWnd->getId() );
3862         }
3863       }
3864     }
3865   }
3866 };
3867 QList<int> SalomePyQt::neighbourViews( const int id )
3868 {
3869   return ProcessEvent( new TNeighbourViews( id ) );
3870 }
3871
3872
3873 /*!
3874   \fn void SalomePyQt::createRoot();
3875   \brief Initialize root data object.
3876
3877   Does nothing if root is already initialized.
3878 */
3879
3880 void SalomePyQt::createRoot()
3881 {
3882   class TEvent: public SALOME_Event
3883   {
3884   public:
3885     TEvent() {}
3886     virtual void Execute() 
3887     {
3888       SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
3889       if ( module ) {
3890         SALOME_PYQT_DataModelLight* dm =
3891           dynamic_cast<SALOME_PYQT_DataModelLight*>( module->dataModel() );
3892         if ( dm )
3893           dm->getRoot();
3894       }
3895       else {
3896         if ( verbose() ) printf( "SalomePyQt.createRoot() function is not supported for the current module.\n" );
3897       }
3898     }
3899   };
3900   ProcessVoidEvent( new TEvent() );
3901 }
3902
3903 /*!
3904   \fn QString SalomePyQt::createObject( const QString& parent );
3905   \brief Create empty data object
3906   \param parent entry of parent data object
3907   \return entry of created data object
3908 */
3909
3910 class TCreateEmptyObjectEvent: public SALOME_Event
3911 {
3912 public:
3913   typedef QString TResult;
3914   TResult  myResult;
3915   QString  myParent;
3916   TCreateEmptyObjectEvent( const QString& parent )
3917     : myParent( parent ) {}
3918   virtual void Execute() 
3919   {
3920     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
3921     if ( module ) {
3922        myResult = module->createObject( myParent );
3923     }
3924     else {
3925       if ( verbose() ) printf( "SalomePyQt.createObject() function is not supported for the current module.\n" );
3926     }
3927   }
3928 };
3929 QString SalomePyQt::createObject( const QString& parent )
3930 {
3931   return ProcessEvent( new TCreateEmptyObjectEvent( parent ) );
3932 }
3933
3934 /*!
3935   \fn QString SalomePyQt::createObject( const QString& name, const QString& icon,
3936                                         const QString& tooltip,const QString& parent );
3937   \brief Create new data object with specified name, icon and tooltip
3938   \param name data object name
3939   \param icon data object icon
3940   \param toolTip data object tooltip
3941   \param parent entry of parent data object
3942   \return entry of created data object
3943 */
3944
3945 class TCreateObjectEvent: public SALOME_Event 
3946 {
3947 public:
3948   typedef QString TResult;
3949   TResult myResult;
3950   QString myParent;
3951   QString myName;
3952   QString myIcon;
3953   QString myToolTip;
3954   TCreateObjectEvent( const QString& name,
3955                       const QString& icon,
3956                       const QString& tooltip,
3957                       const QString& parent )
3958     : myName( name ),
3959       myIcon( icon ),
3960       myToolTip( tooltip ),
3961       myParent( parent ) {}
3962   virtual void Execute()
3963   {
3964     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
3965     if ( module ) {
3966       myResult = module->createObject( myName, myIcon, myToolTip, myParent );
3967     }
3968     else {
3969       if ( verbose() ) printf( "SalomePyQt.createObject() function is not supported for the current module.\n" );
3970     }
3971   }
3972 };
3973 QString SalomePyQt::createObject( const QString& name,
3974                                   const QString& icon,
3975                                   const QString& toolTip,
3976                                   const QString& parent )
3977 {
3978   return ProcessEvent( new TCreateObjectEvent( name, icon, toolTip, parent ) );
3979 }
3980
3981
3982 /*!
3983   \fn void SalomePyQt::setName( const QString& entry, const QString& name );
3984   \brief Set data object name
3985   \param entry data object entry
3986   \param name data object name
3987 */
3988 class TSetNameEvent: public SALOME_Event
3989 {
3990 public:
3991   QString myEntry;
3992   QString myName;
3993   TSetNameEvent( const QString& entry,
3994                  const QString& name )
3995   : myEntry( entry ),
3996     myName( name ) {}
3997   virtual void Execute()
3998   {
3999     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4000     if ( module ) {
4001       module->setName( myEntry, myName );
4002     }
4003     else {
4004       if ( verbose() ) printf( "SalomePyQt.setName() function is not supported for the current module.\n" );
4005     }
4006   }
4007 };
4008 void SalomePyQt::setName( const QString& entry, const QString& name )
4009 {
4010   ProcessVoidEvent( new TSetNameEvent( entry, name ) );
4011 }
4012
4013 /*!
4014   \fn void SalomePyQt::setIcon( const QString& entry, const QString& icon );
4015   \brief Set data object icon
4016   \param entry data object entry
4017   \param icon data object icon file name (icon is loaded from module resources)
4018 */
4019
4020 class TSetIconEvent: public SALOME_Event
4021 {
4022 public:
4023   QString myEntry;
4024   QString myIcon;
4025   TSetIconEvent( const QString& entry,
4026                  const QString& icon )
4027   : myEntry( entry ),
4028     myIcon( icon ) {}
4029   virtual void Execute()
4030   {
4031     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4032     if ( module ) {
4033       module->setIcon( myEntry, myIcon );
4034     }
4035     else {
4036       if ( verbose() ) printf( "SalomePyQt.setIcon() function is not supported for the current module.\n" );
4037     }
4038   }
4039 };
4040
4041 void SalomePyQt::setIcon( const QString& entry, const QString& icon )
4042 {
4043   ProcessVoidEvent( new TSetIconEvent( entry, icon ) );
4044 }
4045
4046 /*!
4047   \fn void SalomePyQt::setToolTip( const QString& entry, const QString& toolTip );
4048   \brief Set data object tooltip
4049   \param entry data object entry
4050   \param toolTip data object tooltip
4051 */
4052
4053 class TSetToolTipEvent: public SALOME_Event
4054 {
4055 public:
4056   QString myEntry;
4057   QString myToolTip;
4058   TSetToolTipEvent( const QString& entry,
4059                     const QString& toolTip )
4060     : myEntry( entry ),
4061       myToolTip( toolTip ) {}
4062   virtual void Execute()
4063   {
4064     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4065     if ( module ) {
4066       module->setToolTip( myEntry, myToolTip );
4067     }
4068     else {
4069       if ( verbose() ) printf( "SalomePyQt.setToolTip() function is not supported for the current module.\n" );
4070     }
4071   }
4072 };
4073 void SalomePyQt::setToolTip( const QString& entry, const QString& toolTip )
4074 {
4075   ProcessVoidEvent( new TSetToolTipEvent( entry, toolTip ) );
4076 }
4077
4078 /*!
4079   \fn void SalomePyQt::setReference( const QString& entry, const QString& refEntry );
4080   \brief Set reference to another data object
4081   \param entry data object entry
4082   \param refEntry referenced data object entry
4083 */
4084
4085 class TSetRefEvent: public SALOME_Event
4086 {
4087 public:
4088   QString myEntry;
4089   QString myRefEntry;
4090   TSetRefEvent( const QString& entry,
4091                 const QString& refEntry )
4092     : myEntry( entry ),
4093       myRefEntry( refEntry ) {}
4094   virtual void Execute()
4095   {
4096     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4097     if ( module ) {
4098       module->setReference( myEntry, myRefEntry );
4099     }
4100     else {
4101       if ( verbose() ) printf( "SalomePyQt.setReference() function is not supported for the current module.\n" );
4102     }
4103   }
4104 };
4105 void SalomePyQt::setReference( const QString& entry, const QString& refEntry )
4106 {
4107   ProcessVoidEvent( new TSetRefEvent( entry, refEntry ) );
4108 }
4109
4110 /*!
4111   \fn void SalomePyQt::setColor( const QString& entry, const QColor& color );
4112   \brief Set data object color
4113   \param entry data object entry
4114   \param color data object color
4115  */
4116
4117 class TSetColorEvent: public SALOME_Event
4118 {
4119 public:
4120   QString myEntry;
4121   QColor  myColor;
4122   TSetColorEvent( const QString& entry,
4123                   const QColor& color )
4124     : myEntry( entry ),
4125       myColor( color ) {}
4126   virtual void Execute()
4127   {
4128     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4129     if ( module ) {
4130       module->setColor( myEntry, myColor );
4131     }
4132     else {
4133       if ( verbose() ) printf( "SalomePyQt.setColor() function is not supported for the current module.\n" );
4134     }
4135   }
4136 };
4137 void SalomePyQt::setColor( const QString& entry, const QColor& color )
4138 {
4139   ProcessVoidEvent( new TSetColorEvent( entry, color ) );
4140 }
4141
4142 /*!
4143   \fn QString SalomePyQt::getName( const QString& entry );
4144   \brief Get data object name
4145   \param entry data object entry
4146   \return data object name
4147 */
4148
4149 class TGetNameEvent: public SALOME_Event
4150 {
4151 public:
4152   typedef QString TResult;
4153   TResult myResult;
4154   QString myEntry;
4155   TGetNameEvent( const QString& entry )
4156     : myEntry( entry ) {}
4157   virtual void Execute()
4158   {
4159     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4160     if ( module ) {
4161       myResult = module->getName( myEntry );
4162     }
4163     else {
4164       if ( verbose() ) printf( "SalomePyQt.getName() function is not supported for the current module.\n" );
4165     }
4166   }
4167 };
4168 QString SalomePyQt::getName( const QString& entry )
4169 {
4170   return ProcessEvent( new TGetNameEvent( entry ) );
4171 }
4172
4173 /*!
4174   \fn QString SalomePyQt::getToolTip( const QString& entry );
4175   \brief Get data object tooltip
4176   \param entry data object entry
4177   \return data object tooltip
4178 */
4179
4180 class TGetToolTipEvent: public SALOME_Event
4181 {
4182 public:
4183   typedef QString TResult;
4184   TResult myResult;
4185   QString myEntry;
4186   TGetToolTipEvent( const QString& entry )
4187   : myEntry( entry ) {}
4188   virtual void Execute()
4189   {
4190     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4191     if ( module ) {
4192       myResult = module->getToolTip( myEntry );
4193     }
4194     else {
4195       if ( verbose() ) printf( "SalomePyQt.getToolTip() function is not supported for the current module.\n" );
4196     }
4197   }
4198 };
4199 QString SalomePyQt::getToolTip( const QString& entry )
4200 {
4201   return ProcessEvent( new TGetToolTipEvent( entry ) );
4202 }
4203
4204 /*
4205   \fn QString SalomePyQt::getReference( const QString& entry );
4206   \brief Get entry of the referenced object (if there's any)
4207   \param entry data object entry
4208   \return referenced data object entry
4209 */
4210
4211 class TGetRefEvent: public SALOME_Event
4212 {
4213 public:
4214   typedef QString TResult;
4215   TResult myResult;
4216   QString myEntry;
4217   TGetRefEvent( const QString& entry )
4218   : myEntry( entry ) {}
4219   virtual void Execute()
4220   {
4221     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4222     if ( module ) {
4223       myResult = module->getReference( myEntry );
4224     }
4225     else {
4226       if ( verbose() ) printf( "SalomePyQt.getReference() function is not supported for the current module.\n" );
4227     }
4228   }
4229 };
4230 QString SalomePyQt::getReference( const QString& entry )
4231 {
4232   return ProcessEvent( new TGetRefEvent( entry ) );
4233 }
4234
4235 /*!
4236   \fn QColor SalomePyQt::getColor( const QString& entry );
4237   \brief Get data object color
4238   \param entry data object entry
4239   \return data object color
4240 */
4241
4242 class TGetColorEvent: public SALOME_Event
4243 {
4244 public:
4245   typedef QColor TResult;
4246   TResult myResult;
4247   QString myEntry;
4248   TGetColorEvent( const QString& entry )
4249   : myEntry( entry ) {}
4250   virtual void Execute()
4251   {
4252     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4253     if ( module ) {
4254       myResult = module->getColor( myEntry );
4255     }
4256     else {
4257       if ( verbose() ) printf( "SalomePyQt.getColor() function is not supported for the current module.\n" );
4258     }
4259   }
4260 };
4261 QColor SalomePyQt::getColor( const QString& entry )
4262 {
4263   return ProcessEvent( new TGetColorEvent( entry ) );
4264 }
4265
4266 /*!
4267   \fn void SalomePyQt::removeChildren( const QString& entry );
4268   \brief Remove all child data objects from specified data object
4269   \param entry data object entry
4270 */
4271
4272 class TRemoveChildEvent: public SALOME_Event
4273 {
4274 public:
4275   QString myEntry;
4276   TRemoveChildEvent( const QString& entry )
4277   : myEntry( entry ) {}
4278   virtual void Execute()
4279   {
4280     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4281     if ( module ) {
4282       module->removeChildren( myEntry );
4283     }
4284     else {
4285       if ( verbose() ) printf( "SalomePyQt.removeChildren() function is not supported for the current module.\n" );
4286     }
4287   }
4288 };
4289 void SalomePyQt::removeChildren( const QString& entry )
4290 {
4291   ProcessVoidEvent( new TRemoveChildEvent( entry ) );
4292 }
4293 void SalomePyQt::removeChild( const QString& entry )
4294 {
4295   if ( verbose() ) printf( "SalomePyQt.removeChild() function is obsolete. Use SalomePyQt.removeChildren() instead." );
4296   removeChildren( entry );
4297 }
4298
4299 /*!
4300   \fn void SalomePyQt::removeObject( const QString& entry );
4301   \brief Remove object by entry
4302   \param entry data object entry
4303 */
4304
4305 class TRemoveObjectEvent: public SALOME_Event
4306 {
4307 public:
4308   QString myEntry;
4309   
4310   TRemoveObjectEvent( const QString& entry )
4311   : myEntry( entry ) {}
4312   virtual void Execute()
4313   {
4314     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4315     if ( module ) {
4316       module->removeObject( myEntry );
4317     }
4318     else {
4319       if ( verbose() ) printf( "SalomePyQt.removeObject() function is not supported for the current module.\n" );
4320     }
4321   }
4322 };
4323 void SalomePyQt::removeObject( const QString& entry )
4324 {
4325   ProcessVoidEvent( new TRemoveObjectEvent( entry ) );
4326 }
4327
4328 /*!
4329   \fn QStringList SalomePyQt::getChildren( const QString& entry, const bool recursive );
4330   \brief Get entries of all child data objects of specified data object
4331   \param entry data object entry
4332   \param recursive \c true for recursive processing
4333 */
4334
4335 class TGetChildrenEvent: public SALOME_Event
4336 {
4337 public:
4338   typedef QStringList TResult;
4339   TResult myResult;
4340   QString myEntry;
4341   bool    myRecursive; 
4342   TGetChildrenEvent( const QString& entry, const bool recursive )
4343     : myEntry( entry ),
4344       myRecursive( recursive ) {}
4345   virtual void Execute()
4346   {
4347     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4348     if ( module ) {
4349       myResult = module->getChildren( myEntry, myRecursive );
4350     }
4351     else {
4352       if ( verbose() ) printf( "SalomePyQt.getChildren() function is not supported for the current module.\n" );
4353     }
4354   }
4355 };
4356 QStringList SalomePyQt::getChildren( const QString& entry, const bool recursive )
4357 {
4358   return ProcessEvent( new TGetChildrenEvent( entry, recursive ) ); 
4359 }
4360
4361 #ifndef DISABLE_PLOT2DVIEWER
4362 // Next set of methods relates to the Plot2d viewer functionality
4363
4364 /*!
4365   \fn void SalomePyQt::displayCurve( const int id, Plot2d_Curve* theCurve )
4366   \brief Display theCurve in view
4367   \param id window identifier
4368   \param theCurve curve to display
4369 */
4370
4371 class TDisplayCurve: public SALOME_Event
4372 {
4373 public:
4374   int myWndId;
4375   Plot2d_Curve* myCurve;
4376   TDisplayCurve( const int id, Plot2d_Curve* theCurve ) : myWndId( id ), myCurve( theCurve ) {}
4377   virtual void Execute() {
4378     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4379     if ( wnd )
4380       wnd->getViewFrame()->displayCurve( myCurve );
4381   }
4382 };
4383 void SalomePyQt::displayCurve( const int id, Plot2d_Curve* theCurve )
4384 {
4385   ProcessVoidEvent( new TDisplayCurve( id, theCurve ) ); 
4386 }
4387
4388 /*!
4389   \fn void SalomePyQt::eraseCurve( const int id, Plot2d_Curve* theCurve )
4390   \brief Erase theCurve in view
4391   \param id window identifier
4392   \param theCurve curve to erase
4393 */
4394
4395 class TEraseCurve: public SALOME_Event
4396 {
4397 public:
4398   int myWndId;
4399   Plot2d_Curve* myCurve;
4400   TEraseCurve( const int id, Plot2d_Curve* theCurve ) : myWndId( id ), myCurve( theCurve ) {}
4401   virtual void Execute() {
4402     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4403     wnd->getViewFrame()->eraseCurve( myCurve );
4404   }
4405 };
4406 void SalomePyQt::eraseCurve( const int id, Plot2d_Curve* theCurve )
4407 {
4408   ProcessVoidEvent( new TEraseCurve( id, theCurve ) ); 
4409 }
4410
4411 /*!
4412   \fn void SalomePyQt::deleteCurve( Plot2d_Curve* theCurve )
4413   \brief Delete theCurve from all views
4414   \param theCurve curve to delete
4415 */
4416
4417 class TDeleteCurve: public SALOME_Event
4418 {
4419 public:
4420   Plot2d_Curve* myCurve;
4421   TDeleteCurve( Plot2d_Curve* theCurve ) : myCurve( theCurve ) {}
4422   virtual void Execute() {
4423     LightApp_Application* app  = getApplication();
4424     if ( app ) {
4425       STD_TabDesktop* tabDesk = dynamic_cast<STD_TabDesktop*>( app->desktop() );
4426       if ( tabDesk ) {
4427         QList<SUIT_ViewWindow*> wndlist = tabDesk->windows();
4428         SUIT_ViewWindow* wnd;
4429         foreach ( wnd, wndlist ) {
4430           Plot2d_ViewWindow* aP2d = dynamic_cast<Plot2d_ViewWindow*>( wnd );
4431           if ( aP2d )
4432             aP2d->getViewFrame()->eraseObject( myCurve );
4433         }
4434       }
4435     }
4436   }
4437 };
4438 void SalomePyQt::eraseCurve( Plot2d_Curve* theCurve )
4439 {
4440   ProcessVoidEvent( new TDeleteCurve( theCurve ) );
4441 }
4442
4443 /*!
4444   \brief updateCurves (repaint) curves in view window.
4445 */
4446 void SalomePyQt::updateCurves( const int id )
4447 {
4448   class TEvent: public SALOME_Event
4449   {
4450   public:
4451     int myWndId;
4452     TEvent( const int id ) : myWndId( id ) {}
4453     virtual void Execute()
4454     {
4455       Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4456       if ( wnd )
4457         wnd->getViewFrame()->DisplayAll();
4458     }
4459   };
4460   ProcessVoidEvent( new TEvent( id ) );
4461 }
4462
4463 /*!
4464   \fn QString SalomePyQt::getPlot2dTitle( const int id, ObjectType type = MainTitle )
4465   \brief Get title of corresponding type
4466   \param id window identifier
4467   \param type is type of title
4468   \return title of corresponding type
4469 */
4470
4471 class TGetPlot2dTitle: public SALOME_Event
4472 {
4473 public:
4474   typedef QString TResult;
4475   TResult myResult;
4476   int myWndId;
4477   ObjectType myType;
4478   TGetPlot2dTitle(const int id, ObjectType type) :
4479     myWndId( id ),
4480     myType( type ) {}
4481   virtual void Execute() {
4482     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4483     if ( wnd )
4484       myResult = wnd->getViewFrame()->getTitle( (Plot2d_ViewFrame::ObjectType)myType );
4485   }
4486 };
4487 QString SalomePyQt::getPlot2dTitle( const int id, ObjectType type )
4488 {
4489   return ProcessEvent( new TGetPlot2dTitle( id, type ) ); 
4490 }
4491
4492
4493 /*!
4494   \fn void SalomePyQt::setPlot2dTitle( const int id, const QString& title, ObjectType type = MainTitle, bool show = true )
4495   \brief Set title of corresponding type
4496   \param id window identifier
4497   \param title
4498   \param type is type of title
4499   \param show
4500 */
4501
4502 class TSetPlot2dTitle: public SALOME_Event
4503 {
4504 public:
4505   int myWndId;
4506   Plot2d_Curve* myCurve;
4507   QString myTitle;
4508   ObjectType myType;
4509   bool myShow;
4510   TSetPlot2dTitle( const int id, const QString& title, ObjectType type, bool show ) :
4511     myWndId( id ),
4512     myTitle( title ),
4513     myType( type ),
4514     myShow( show ) {}
4515   virtual void Execute() {
4516     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4517     wnd->getViewFrame()->setTitle( myShow, myTitle, (Plot2d_ViewFrame::ObjectType)myType, false );
4518   }
4519 };
4520 void SalomePyQt::setPlot2dTitle( const int id, const QString& title, ObjectType type, bool show )
4521 {
4522   ProcessVoidEvent( new TSetPlot2dTitle( id, title, type, show ) ); 
4523 }
4524
4525 /*!
4526   \fn QList<int> SalomePyQt::getPlot2dFitRangeByCurves( const int id )
4527   \brief Get list of Plot2d view ranges
4528   \param id window identifier
4529   \return list of view ranges (XMin, XMax, YMin, YMax)
4530 */
4531
4532 class TFitRangeByCurves: public SALOME_Event
4533 {
4534 public:
4535   typedef QList<double> TResult;
4536   TResult myResult;
4537   int myWndId;
4538   TFitRangeByCurves( const int id )
4539     : myWndId( id ) {}
4540   virtual void Execute() 
4541   {
4542     myResult.clear();
4543     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4544     if ( wnd ) {
4545       double XMin, XMax, YMin, YMax, Y2Min, Y2Max;
4546       wnd->getViewFrame()->getFitRangeByCurves( XMin, XMax, YMin, YMax, Y2Min, Y2Max );
4547       myResult.append( XMin );
4548       myResult.append( XMax );
4549       myResult.append( YMin );
4550       myResult.append( YMax );
4551     }
4552   }
4553 };
4554 QList<double> SalomePyQt::getPlot2dFitRangeByCurves( const int id )
4555 {
4556   return ProcessEvent( new TFitRangeByCurves( id ) );
4557 }
4558
4559 /*!
4560   \fn QList<int> SalomePyQt::getPlot2dFitRangeCurrent( const int id )
4561   \brief Get list of current Plot2d view ranges
4562   \param id window identifier
4563   \return list of view ranges (XMin, XMax, YMin, YMax)
4564 */
4565
4566 class TFitRangeCurrent: public SALOME_Event
4567 {
4568 public:
4569   typedef QList<double> TResult;
4570   TResult myResult;
4571   int myWndId;
4572   TFitRangeCurrent( const int id )
4573     : myWndId( id ) {}
4574   virtual void Execute() 
4575   {
4576     myResult.clear();
4577     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4578     if ( wnd ) {
4579       double XMin, XMax, YMin, YMax, Y2Min, Y2Max;
4580       wnd->getViewFrame()->getFitRanges( XMin, XMax, YMin, YMax, Y2Min, Y2Max );
4581       myResult.append( XMin );
4582       myResult.append( XMax );
4583       myResult.append( YMin );
4584       myResult.append( YMax );
4585     }
4586   }
4587 };
4588 QList<double> SalomePyQt::getPlot2dFitRangeCurrent( const int id )
4589 {
4590   return ProcessEvent( new TFitRangeCurrent( id ) );
4591 }
4592
4593 /*!
4594   \fn void SalomePyQt::setPlot2dFitRange( const int id, const double XMin, const double XMax, const double YMin, const double YMax )
4595   \brief Set range of Plot2d view
4596   \param id window identifier
4597   \param XMin
4598   \param XMax
4599   \param YMin
4600   \param YMax
4601 */
4602
4603 class TPlot2dFitRange: public SALOME_Event
4604 {
4605 public:
4606   int myWndId;
4607   double myXMin;
4608   double myXMax;
4609   double myYMin;
4610   double myYMax;
4611   TPlot2dFitRange( const int id, const double XMin, const double XMax, const double YMin, const double YMax ) :
4612     myWndId( id ),
4613     myXMin( XMin ),
4614     myXMax( XMax ),
4615     myYMin( YMin ),
4616     myYMax( YMax ) {}
4617   virtual void Execute() {
4618     Plot2d_ViewWindow* wnd = dynamic_cast<Plot2d_ViewWindow*>( getWnd( myWndId ) );
4619     if ( wnd )
4620       wnd->getViewFrame()->fitData( 0, myXMin, myXMax, myYMin, myYMax );
4621   }
4622 };
4623 void SalomePyQt::setPlot2dFitRange( const int id, const double XMin, const double XMax, const double YMin, const double YMax )
4624 {
4625   ProcessVoidEvent( new TPlot2dFitRange( id, XMin, XMax, YMin, YMax ) ); 
4626 }
4627
4628 // End of methods related to the Plot2d viewer functionality
4629 #endif // DISABLE_PLOT2DVIEWER
4630
4631 /*!
4632   \brief Process Qt event loop
4633 */
4634 void SalomePyQt::processEvents()
4635 {
4636   QCoreApplication::processEvents();
4637 }
4638
4639 /*!
4640   \brief Set visibility state for given object
4641   \param theEntry study ID of the object
4642   \param theState visibility state
4643 */
4644 void SalomePyQt::setVisibilityState( const QString& theEntry, VisibilityState theState )
4645 {
4646   class TEvent: public SALOME_Event
4647   {
4648     QString myEntry;
4649     int myState;
4650   public:
4651     TEvent( const QString& theEntry, int theState ):
4652       myEntry( theEntry ), myState( theState ) {}
4653     virtual void Execute() 
4654     {
4655       LightApp_Study* aStudy = getActiveStudy();
4656       if ( !aStudy )
4657         return;
4658       aStudy->setVisibilityState( myEntry, (Qtx::VisibilityState)myState );
4659     }
4660   };
4661   ProcessVoidEvent( new TEvent( theEntry, theState ) );
4662 }
4663
4664 /*!
4665   \fn VisibilityState SalomePyQt::getVisibilityState( const QString& theEntry )
4666   \brief Get visibility state for given object
4667   \param theEntry study ID of the object
4668   \return visibility state
4669 */
4670
4671 class TGetVisibilityStateEvent: public SALOME_Event 
4672 {
4673 public:
4674   typedef int TResult;
4675   TResult myResult;
4676   QString myEntry;
4677   TGetVisibilityStateEvent( const QString& theEntry ) : myResult( 0 ), myEntry( theEntry ) {}
4678   virtual void Execute()
4679   {
4680     LightApp_Study* aStudy = getActiveStudy();
4681     if ( aStudy )
4682       myResult = aStudy->visibilityState( myEntry );
4683   }
4684 };
4685 VisibilityState SalomePyQt::getVisibilityState( const QString& theEntry )
4686 {
4687   return (VisibilityState) ProcessEvent( new TGetVisibilityStateEvent( theEntry ) );
4688 }
4689
4690 /*!
4691   \brief Set position of given object in the tree
4692   \param theEntry study ID of the object
4693   \param thePos position
4694 */
4695 void SalomePyQt::setObjectPosition( const QString& theEntry, int thePos )
4696 {
4697   class TEvent: public SALOME_Event
4698   {
4699     QString myEntry;
4700     int myPos;
4701   public:
4702     TEvent( const QString& theEntry, int thePos ):
4703       myEntry( theEntry ), myPos( thePos ) {}
4704     virtual void Execute() 
4705     {
4706       SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4707       if ( module )
4708         module->setObjectPosition( myEntry, myPos );
4709     }
4710   };
4711   ProcessVoidEvent( new TEvent( theEntry, thePos ) );
4712 }
4713
4714 /*!
4715   \fn int SalomePyQt::getObjectPosition( const QString& theEntry )
4716   \brief Get position of given object in the tree
4717   \param theEntry study ID of the object
4718   \return position
4719 */
4720
4721 class TGetObjectPositionEvent: public SALOME_Event 
4722 {
4723 public:
4724   typedef int TResult;
4725   TResult myResult;
4726   QString myEntry;
4727   TGetObjectPositionEvent( const QString& theEntry ) : myResult( 0 ), myEntry( theEntry ) {}
4728   virtual void Execute()
4729   {
4730     SALOME_PYQT_ModuleLight* module = dynamic_cast<SALOME_PYQT_ModuleLight*>( getActiveModule() );
4731     if ( module )
4732       myResult = module->getObjectPosition( myEntry );
4733   }
4734 };
4735 int SalomePyQt::getObjectPosition( const QString& theEntry )
4736 {
4737   return ProcessEvent( new TGetObjectPositionEvent( theEntry ) );
4738 }
4739
4740 /*!
4741   \brief Start recordind a log of Python commands from embedded console
4742   \param theFileName output lof file name
4743 */
4744 void SalomePyQt::startPyLog( const QString& theFileName )
4745 {
4746   class TEvent: public SALOME_Event
4747   {
4748     QString myFileName;
4749   public:
4750     TEvent( const QString& theFileName ):
4751       myFileName( theFileName ) {}
4752     virtual void Execute() 
4753     {
4754       if ( getApplication() ) {
4755         PyConsole_Console* pyConsole = getApplication()->pythonConsole( false );
4756         if ( pyConsole ) pyConsole->startLog( myFileName );
4757       }
4758     }
4759   };
4760   ProcessVoidEvent( new TEvent( theFileName ) );
4761 }
4762
4763 /*!
4764   \brief Stop recordind a log of Python commands from embedded console
4765 */
4766 void SalomePyQt::stopPyLog()
4767 {
4768   class TEvent: public SALOME_Event
4769   {
4770   public:
4771     TEvent() {}
4772     virtual void Execute() 
4773     {
4774       if ( getApplication() ) {
4775         PyConsole_Console* pyConsole = getApplication()->pythonConsole( false );
4776         if ( pyConsole ) pyConsole->stopLog();
4777       }
4778     }
4779   };
4780   ProcessVoidEvent( new TEvent() );
4781 }