Salome HOME
Copyrights update
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUI / SALOME_PYQT_Module.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 //=============================================================================
20 // File      : SALOME_PYQT_Module.cxx
21 // Created   : 25/04/05
22 // Author    : Vadim SANDLER
23 // Project   : SALOME
24 // Copyright : 2003-2005 CEA/DEN, EDF R&D
25 // $Header   : $
26 //=============================================================================
27
28 #include "SALOME_PYQT_Module.h"
29
30 #include "PyInterp_Dispatcher.h"
31 #include "SUIT_ResourceMgr.h"
32 #include "STD_MDIDesktop.h"
33 #include "STD_TabDesktop.h"
34 #include "SalomeApp_Application.h"
35 #include "SalomeApp_Study.h"
36
37 #include "QtxWorkstack.h"
38 #include <SALOME_LifeCycleCORBA.hxx>
39 #include <Container_init_python.hxx>
40
41 #include <qfile.h>
42 #include <qdom.h>
43 #include <qworkspace.h>
44 #include <qpopupmenu.h>
45
46 #include "SALOME_PYQT_SipDefs.h"
47 #if defined(SIP_VERS_v4_old) || defined(SIP_VERS_v4_new)
48 #include "sipAPISalomePyQtGUI.h"
49 #else
50 #include "sipSalomePyQtGUIDeclSalomePyQtGUI.h"
51 #endif
52
53 #include <sipqtQWidget.h>
54 #include <sipqtQPopupMenu.h>
55
56 #include <CORBA.h>
57
58 using namespace std;
59
60 ///////////////////////////////////////////////////////////////////////////////
61 // Default name of the module, replaced at the moment of module creation
62 #define __DEFAULT_NAME__ "SALOME_PYQT_Module"
63
64 ///////////////////////////////////////////////////////////////////////////////
65 // If __CALL_OLD_METHODS__ macro is not defined the invoking of obsolete Python 
66 // module's methods like setSetting(), definePopup(), etc. is blocked.
67 // This macro is defined by default (in Makefile)
68 #ifdef __CALL_OLD_METHODS__
69 const bool IsCallOldMethods = true;
70 #else 
71 const bool IsCallOldMethods = false;
72 #endif
73
74 ///////////////////////////////////////////////////////////////////////////////
75 // NB: Python requests.
76 // General rule for Python requests created by SALOME_PYQT_Module:
77 // all requests should be executed SYNCHRONOUSLY within the main GUI thread.
78 // However, it is obligatory that ANY Python call is wrapped with a request object,
79 // so that ALL Python API calls are serialized with PyInterp_Dispatcher.
80 ///////////////////////////////////////////////////////////////////////////////
81
82 //=============================================================================
83 // The class for parsing of the XML resource files.
84 // Used for backward compatibility with existing Python modules.
85 //=============================================================================
86 class SALOME_PYQT_XmlHandler
87 {
88 public:
89   SALOME_PYQT_XmlHandler( SALOME_PYQT_Module* module, const QString& fileName );
90   void createActions();
91   void createPopup  ( QPopupMenu*    menu, 
92                       const QString& context, 
93                       const QString& parent, 
94                       const QString& object );
95
96 protected:
97   void createToolBar   ( QDomNode& parentNode );
98   void createMenu      ( QDomNode& parentNode, 
99                          const int parentMenuId = -1 );
100
101   void insertPopupItems( QDomNode&   parentNode, 
102                          QPopupMenu* menu );
103
104 private:
105   SALOME_PYQT_Module* myModule;
106   QDomDocument        myDoc;
107 };
108
109 //=============================================================================
110 // SALOME_PYQT_Module class implementation (implements CAM_Module API for
111 // all Python-based SALOME module
112 //=============================================================================
113
114 // While the SalomePyQtGUI library is not imported in Python it's initialization function
115 // should be called manually (and only once) in order to initialize global sip data
116 // and to get C API from sip : sipBuildResult for example
117 #if defined(SIP_STATIC_MODULE)
118 extern "C" void initSalomePyQtGUI();
119 #else
120 PyMODINIT_FUNC initSalomePyQtGUI();
121 #endif
122
123 /*!
124  * This function creates an instance of SALOME_PYQT_Module object by request
125  * of and application object when the module is loaded.
126  */
127 extern "C" {
128   SALOME_PYQT_EXPORT CAM_Module* createModule() {
129     static bool alreadyInitialized = false;
130     if ( !alreadyInitialized ) {
131       // call only once (see above) !
132       PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
133       initSalomePyQtGUI(); 
134       PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
135       alreadyInitialized = !alreadyInitialized;
136     }
137     return new SALOME_PYQT_Module();
138   }
139 }
140
141 /*! 
142  * Static variables definition
143  */
144 SALOME_PYQT_Module::InterpMap SALOME_PYQT_Module::myInterpMap;
145 SALOME_PYQT_Module* SALOME_PYQT_Module::myInitModule = 0;
146
147 /*!
148  * Little trick : provide an access to being activated Python module from outside;
149  * needed by the SalomePyQt library :(
150 */
151 SALOME_PYQT_Module* SALOME_PYQT_Module::getInitModule() 
152
153   return myInitModule; 
154 }
155
156 /*!
157  * Constructor
158  */
159 SALOME_PYQT_Module::SALOME_PYQT_Module() :
160        SalomeApp_Module( __DEFAULT_NAME__ ), myModule( 0 ), myXmlHandler ( 0 )
161 {
162   myMenuActionList.setAutoDelete( false );
163   myPopupActionList.setAutoDelete( false );
164   myToolbarActionList.setAutoDelete( false );
165 }
166
167 /*!
168  * Destructor
169  */
170 SALOME_PYQT_Module::~SALOME_PYQT_Module()
171 {
172   myMenuActionList.clear();
173   myPopupActionList.clear();
174   myToolbarActionList.clear();
175   if ( myXmlHandler )
176     delete myXmlHandler;
177 }
178
179 /*!
180  * Initialization of the module.
181  * Inherited from CAM_Module.
182  *
183  * This method is used for creation of the menus, toolbars and other staff.
184  * There are two ways:
185  * - for obsolete modules this method first tries to read <module>_<language>.xml 
186  *   resource file which contains a menu, toolbars and popup menus description.
187  * - new modules can create menus by by calling the corresponding methods of SalomePyQt
188  *   Python API in the Python module's initialize() method which is called from here.
189  * NOTE: if postponed modules loading is not used, the active study might be not defined
190  * yet at this stage, so initialize() method should not perform any study-based initialization.
191  */
192 void SALOME_PYQT_Module::initialize( CAM_Application* app )
193 {
194   SalomeApp_Module::initialize( app );
195
196   // Try to get XML resource file name
197   SUIT_ResourceMgr* aResMgr = getApp()->resourceMgr();
198   QString aLang = aResMgr->stringValue( "language", "language", QString::null );
199   if ( aLang.isEmpty() ) aLang = QString( "en" );
200   QString aName = name( "" );
201   QString aFileName = aName + "_" + aLang + ".xml";
202   aFileName = aResMgr->path( "resources", aName, aFileName );
203  
204   // parse XML file if it is found and create actions
205   if ( !myXmlHandler && !aFileName.isEmpty() ) {
206     myXmlHandler = new SALOME_PYQT_XmlHandler( this, aFileName );
207     myXmlHandler->createActions();
208   }
209
210   // perform internal initialization and call module's initialize() method
211   // InitializeReq: request class for internal init() operation
212   class InitializeReq : public PyInterp_Request
213   {
214   public:
215     InitializeReq( CAM_Application*    _app,
216                    SALOME_PYQT_Module* _obj ) 
217       : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
218         myApp( _app ),
219         myObj( _obj ) {}
220     
221   protected:
222     virtual void execute()
223     {
224       myObj->init( myApp );
225     }
226
227   private:
228     CAM_Application*    myApp;
229     SALOME_PYQT_Module* myObj;
230   };
231
232   // Posting the request
233   PyInterp_Dispatcher::Get()->Exec( new InitializeReq( app, this ) );
234 }
235
236 /*!
237  * Activation of the module.
238  * Inherited from CAM_Module.
239  */
240 bool SALOME_PYQT_Module::activateModule( SUIT_Study* theStudy )
241 {
242   MESSAGE( "SALOME_PYQT_Module::activateModule" );
243
244   bool res = SalomeApp_Module::activateModule( theStudy );
245   
246   if ( !res )
247     return res;
248
249   // ActivateReq: request class for internal activate() operation
250   class ActivateReq : public PyInterp_Request
251   {
252   public:
253     ActivateReq( SUIT_Study*         _study, 
254                  SALOME_PYQT_Module* _obj ) 
255       : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
256         myStudy ( _study ),
257         myObj   ( _obj   ) {}
258     
259   protected:
260     virtual void execute()
261     {
262       myObj->activate( myStudy );
263     }
264
265   private:
266     SUIT_Study*         myStudy;
267     SALOME_PYQT_Module* myObj;
268   };
269
270   // Posting the request
271   PyInterp_Dispatcher::Get()->Exec( new ActivateReq( theStudy, this ) );
272
273   // activate menus, toolbars, etc
274   setMenuShown( true );
275   setToolShown( true );
276
277   return true;
278 }
279
280 /*!
281  * Deactivation of the module.
282  * Inherited from CAM_Module.
283  */
284 bool SALOME_PYQT_Module::deactivateModule( SUIT_Study* theStudy )
285 {
286   MESSAGE( "SALOME_PYQT_Module::deactivateModule" );
287
288   bool res = SalomeApp_Module::deactivateModule( theStudy );
289
290   // deactivate menus, toolbars, etc
291   setMenuShown( false );
292   setToolShown( false );
293
294   // DeactivateReq: request class for internal deactivate() operation
295   class DeactivateReq : public PyInterp_LockRequest
296   {
297   public:
298     DeactivateReq( PyInterp_base*      _py_interp, 
299                    SUIT_Study*         _study, 
300                    SALOME_PYQT_Module* _obj ) 
301       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
302         myStudy ( _study ),
303         myObj   ( _obj   ) {}
304     
305   protected:
306     virtual void execute()
307     {
308       myObj->deactivate( myStudy );
309     }
310
311   private:
312     SUIT_Study*         myStudy;
313     SALOME_PYQT_Module* myObj;
314   };
315
316   // Posting the request
317   PyInterp_Dispatcher::Get()->Exec( new DeactivateReq( myInterp, theStudy, this ) );
318
319   return res;
320 }
321
322 /*!
323  * Processes GUI action (from main menu, toolbar or context popup menu)
324  */
325 void SALOME_PYQT_Module::onGUIEvent()
326 {
327   // get sender action
328   const QObject* obj = sender();
329   if ( !obj || !obj->inherits( "QAction" ) )
330     return;
331   QAction* action = (QAction*)obj;
332
333   // get action ID
334   int id = actionId( action );
335   MESSAGE( "SALOME_PYQT_Module::onGUIEvent: id = " << id );
336
337   // perform synchronous request to Python event dispatcher
338   class GUIEvent : public PyInterp_LockRequest
339   {
340   public:
341     GUIEvent( PyInterp_base*      _py_interp, 
342               SALOME_PYQT_Module* _obj,
343               int                 _id ) 
344       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
345         myId    ( _id  ),
346         myObj   ( _obj ) {}
347     
348   protected:
349     virtual void execute()
350     {
351       myObj->guiEvent( myId );
352     }
353
354   private:
355     int                 myId;
356     SALOME_PYQT_Module* myObj;
357   };
358
359   // Posting the request
360   PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) );
361 }
362
363 /*!
364  * Processes GUI action (from context popup menu, only for XML-based actions!)
365  */
366 void SALOME_PYQT_Module::onGUIEvent( int id ) 
367 {
368   // perform synchronous request to Python event dispatcher
369   class GUIEvent : public PyInterp_LockRequest
370   {
371   public:
372     GUIEvent( PyInterp_base*      _py_interp, 
373               SALOME_PYQT_Module* _obj,
374               int                 _id ) 
375       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
376         myId    ( _id  ),
377         myObj   ( _obj ) {}
378     
379   protected:
380     virtual void execute()
381     {
382       myObj->guiEvent( myId );
383     }
384
385   private:
386     int                 myId;
387     SALOME_PYQT_Module* myObj;
388   };
389
390   // Posting the request
391   PyInterp_Dispatcher::Get()->Exec( new GUIEvent( myInterp, this, id ) );
392 }
393
394 /*! 
395   Context popup menu request.
396   Called when user activates popup menu in some window (view, object browser, etc).
397   */
398 void SALOME_PYQT_Module::contextMenuPopup( const QString& theContext, QPopupMenu* thePopupMenu, QString& /*title*/ )
399 {
400   MESSAGE( "SALOME_PYQT_Module::contextMenuPopup : " << theContext.latin1() );
401   // perform synchronous request to Python event dispatcher
402   class PopupMenuEvent : public PyInterp_LockRequest
403   {
404   public:
405     PopupMenuEvent( PyInterp_base*     _py_interp, 
406                     SALOME_PYQT_Module* _obj,
407                     const QString&      _context, 
408                     QPopupMenu*        _popup ) 
409       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
410         myContext( _context ), 
411         myPopup  ( _popup  ),
412         myObj    ( _obj )   {}
413     
414   protected:
415     virtual void execute()
416     {
417       myObj->contextMenu( myContext, myPopup );
418     }
419
420   private:
421     SALOME_PYQT_Module* myObj;
422     QString             myContext;
423     QPopupMenu*         myPopup;
424   };
425
426   // Posting the request only if dispatcher is not busy!
427   // Executing the request synchronously
428   if ( !PyInterp_Dispatcher::Get()->IsBusy() )
429     PyInterp_Dispatcher::Get()->Exec( new PopupMenuEvent( myInterp, this, theContext, thePopupMenu ) );
430 }
431
432 /*!
433  * Defines the dockable window associated with the module.
434  * To fill the list of windows the correspondind Python module's windows() 
435  * method is called from SALOME_PYQT_Module::init() method.
436  * By default, ObjectBrowser, PythonConsole and LogWindow are provided.
437  */
438 void SALOME_PYQT_Module::windows( QMap<int, int>& mappa ) const
439 {
440   // First clear the output parameters 
441   QMap<int, int>::ConstIterator it;
442   for ( it = myWindowsMap.begin(); it != myWindowsMap.end(); ++it ) {
443     mappa[ it.key() ] = it.data();
444   }
445 }
446
447 /*!
448  * Defines the compatible views which should be opened on module activation.
449  * To fill the list of views the correspondind Python module's views() 
450  * method is called from SALOME_PYQT_Module::init() method.
451  * By default, the list is empty.
452  */
453 void SALOME_PYQT_Module::viewManagers( QStringList& listik ) const
454 {
455   for ( QStringList::ConstIterator it = myViewMgrList.begin(); it != myViewMgrList.end(); ++it ) {
456     listik.append( *it );
457   }
458 }
459
460 /*!
461  * Performs internal initialization
462  * - initializes/gets the Python interpreter (one per study)
463  * - imports the Python module
464  * - passes the workspace widget to the Python module
465  * - calls Python module's initialize() method
466  * - calls Python module's windows() method
467  * - calls Python module's views() method
468  */
469 void SALOME_PYQT_Module::init( CAM_Application* app )
470 {
471   // reset interpreter to NULL
472   myInterp = NULL;
473
474   // get study Id
475   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( app );
476   if ( !anApp )
477     return;
478
479   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
480   if ( !aStudy )
481     return;
482   int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
483
484   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
485   initInterp( aStudyId );
486   if ( !myInterp ) 
487     return; // Error 
488
489   // import Python GUI module
490   importModule();
491   if ( !myModule )
492     return; // Error 
493  
494   myInitModule = this;
495
496   if ( IsCallOldMethods ) { // __CALL_OLD_METHODS__
497     // call Python module's setWorkspace() method
498     setWorkSpace();
499   }                         //__CALL_OLD_METHODS__
500
501   // then call Python module's initialize() method
502   // ... first get python lock
503   PyLockWrapper aLock = myInterp->GetLockWrapper();
504   // ... (the Python module is already imported)
505   // ... finally call Python module's initialize() method
506   PyObjWrapper res( PyObject_CallMethod( myModule, "initialize", "" ) );
507   if( !res ) {
508     // VSR: this method may not be implemented in Python module
509     // PyErr_Print();
510     PyErr_Clear();
511   }
512   
513   // get the windows list from the Python module by calling windows() method
514   // ... first put default values
515   myWindowsMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::DockLeft );
516   myWindowsMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::DockBottom );
517   // VSR: LogWindow is not yet implemented
518   // myWindowsMap.insert( SalomeApp_Application::WT_LogWindow,     Qt::DockBottom );
519
520   PyObjWrapper res1( PyObject_CallMethod( myModule, "windows", "" ) );
521   if( !res1 ) {
522     // VSR: this method may not be implemented in Python module
523     // PyErr_Print();
524     PyErr_Clear();
525   }
526   else {
527     myWindowsMap.clear();
528     if ( PyDict_Check( res1 ) ) {
529       PyObject* key;
530       PyObject* value;
531       int pos = 0;
532       while ( PyDict_Next( res1, &pos, &key, &value ) ) {
533         // parse the return value
534         // it should be a map: {integer:integer}
535         int aKey, aValue;
536         if( key && PyInt_Check( key ) && value && PyInt_Check( value ) ) {
537           aKey   = PyInt_AsLong( key );
538           aValue = PyInt_AsLong( value );
539           myWindowsMap[ aKey ] = aValue;
540         }
541       }
542     }
543   }
544   // get the windows list from the Python module by calling views() method
545   PyObjWrapper res2( PyObject_CallMethod( myModule, "views", "" ) );
546   if( !res2 ) {
547     // VSR: this method may not be implemented in Python module
548     // PyErr_Print();
549     PyErr_Clear();
550   }
551   else {
552     // parse the return value
553     // result can be one string...
554     if ( PyString_Check( res2 ) ) {
555       myViewMgrList.append( PyString_AsString( res2 ) );
556     }
557     // ... or list of strings
558     else if ( PyList_Check( res2 ) ) {
559       int size = PyList_Size( res2 );
560       for ( int i = 0; i < size; i++ ) {
561         PyObject* value = PyList_GetItem( res2, i );
562         if( value && PyString_Check( value ) ) {
563           myViewMgrList.append( PyString_AsString( value ) );
564         }
565       }
566     }
567   }
568   myInitModule = 0;
569 }
570
571 /*!
572  * Performs internal activation: 
573  * - initializes/gets the Python interpreter (one per study)
574  * - imports the Python GUI module
575  * - calls Python module's setSettings() method (obsolete function, used for compatibility with old code)
576  *   or activate() method (for new modules)
577  */
578 void SALOME_PYQT_Module::activate( SUIT_Study* theStudy )
579 {
580   // get study Id
581   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theStudy );
582   int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
583
584   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
585   initInterp( aStudyId );
586   if ( !myInterp ) 
587     return; // Error 
588
589   // import Python GUI module
590   importModule();
591   if ( !myModule )
592     return; // Error 
593  
594   // get python lock
595   PyLockWrapper aLock = myInterp->GetLockWrapper();
596
597   if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
598     // call Python module's setSettings() method (obsolete)
599     PyObjWrapper res( PyObject_CallMethod( myModule, "setSettings", "" ) );
600     if( !res ) {
601       // VSR: this method may not be implemented in Python module
602       // PyErr_Print();
603       PyErr_Clear();
604     }
605   }                         //__CALL_OLD_METHODS__
606
607   // call Python module's activate() method (for the new modules)
608   PyObjWrapper res1( PyObject_CallMethod( myModule, "activate", "" ) );
609   if( !res1 ) {
610     // VSR: this method may not be implemented in Python module
611     // PyErr_Print();
612     PyErr_Clear();
613   }
614 }
615
616 /*!
617  * Performs internal deactivation: 
618  * - calls Python module's deactivate() method
619  */
620 void SALOME_PYQT_Module::deactivate( SUIT_Study* theStudy )
621 {
622   // check if the subinterpreter is initialized and Python module is imported
623   if ( !myInterp || !myModule ) {
624     // Error! Python subinterpreter should be initialized and module should be imported first!
625     return;
626   }
627   // then call Python module's deactivate() method
628   PyObjWrapper res( PyObject_CallMethod( myModule, "deactivate", "" ) );
629   if( !res ) {
630     // VSR: this method may not be implemented in Python module
631     // PyErr_Print();
632     PyErr_Clear();
633   }
634 }
635
636 /*!
637  * Called when active the study is actived (user brings its desktop to top)
638  * - initializes/gets the Python interpreter (one per study)
639  * - imports the Python GUI module
640  * - calls Python module's activeStudyChanged() method
641  */
642 void SALOME_PYQT_Module::studyChanged( SUIT_Study* theStudy )
643 {
644   // get study Id
645   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( theStudy );
646   int aStudyId = aStudy ? aStudy->studyDS()->StudyId() : 0;
647
648   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
649   initInterp( aStudyId );
650   if ( !myInterp ) 
651     return; // Error 
652
653   // import Python GUI module
654   importModule();
655   if ( !myModule )
656     return; // Error 
657  
658   // get python lock
659   PyLockWrapper aLock = myInterp->GetLockWrapper();
660
661   // call Python module's activeStudyChanged() method
662   PyObjWrapper res( PyObject_CallMethod( myModule, "activeStudyChanged", "i", aStudyId ) );
663   if( !res ) {
664     // VSR: this method may not be implemented in Python module
665     // PyErr_Print();
666     PyErr_Clear();
667   }
668 }
669
670 /*!
671  * Get module engine, returns nil var if engine is not found in LifeCycleCORBA
672  */
673 Engines::Component_var SALOME_PYQT_Module::getEngine() const
674 {
675   Engines::Component_var comp;
676   // temporary solution
677   try {
678     comp = getApp()->lcc()->FindOrLoad_Component( "FactoryServerPy", name( "" ) );
679   }
680   catch (CORBA::Exception&) {
681   }
682   return comp;
683 }
684
685 /*!
686  * Get module engine IOR, returns empty string if engine is not found in LifeCycleCORBA
687  */
688 QString SALOME_PYQT_Module::engineIOR() const
689 {
690   if ( !CORBA::is_nil( getEngine() ) )
691     return QString( getApp()->orb()->object_to_string( getEngine() ) );
692   return QString( "" );
693 }
694
695 /*! 
696  * Called when study desktop is activated.
697  * Used for notifying about changing of the active study.
698  */
699 void SALOME_PYQT_Module::studyActivated()
700 {
701   // StudyChangedReq: request class for internal studyChanged() operation
702   class StudyChangedReq : public PyInterp_Request
703   {
704   public:
705     StudyChangedReq( SUIT_Study*         _study, 
706                      SALOME_PYQT_Module* _obj ) 
707       : PyInterp_Request( 0, true ), // this request should be processed synchronously (sync == true)
708         myStudy ( _study ),
709         myObj   ( _obj   ) {}
710     
711   protected:
712     virtual void execute()
713     {
714       myObj->studyChanged( myStudy );
715     }
716
717   private:
718     SUIT_Study*         myStudy;
719     SALOME_PYQT_Module* myObj;
720   };
721
722   // Posting the request
723   PyInterp_Dispatcher::Get()->Exec( new StudyChangedReq( application()->activeStudy(), this ) );
724 }
725
726 /*!
727  * Processes context popup menu request
728  * - calls Python module's definePopup(...) method (obsolete function, used for compatibility with old code)
729  *   to define the popup menu context
730  * - parses XML resourses file (if exists) and fills the popup menu with the items)
731  * - calls Python module's customPopup(...) method (obsolete function, used for compatibility with old code)
732  *   to allow module to customize the popup menu
733  * - for new modules calls createPopupMenu() function to allow the modules to build the popup menu
734  *   by using insertItem(...) Qt functions.
735  */
736 void SALOME_PYQT_Module::contextMenu( const QString& theContext, QPopupMenu* thePopupMenu )
737 {
738   // Python interpreter should be initialized and Python module should be
739   // import first
740   if ( !myInterp || !myModule )
741     return;
742   
743   QString aContext( theContext ), aObject( "" ), aParent( "" );
744
745   if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
746     // call definePopup() Python module's function
747     // this is obsolete function, used only for compatibility reasons
748     PyObjWrapper res(PyObject_CallMethod( myModule, 
749                                           "definePopup", 
750                                           "sss",
751                                           aContext.latin1(), 
752                                           aObject.latin1(), 
753                                           aParent.latin1() ) );
754     if( !res ) {
755       // VSR: this method may not be implemented in Python module
756       // PyErr_Print();
757       PyErr_Clear();
758     }
759     else {
760       // parse return value
761       char *co, *ob, *pa;
762       if( PyArg_ParseTuple( res, "sss", &co, &ob, &pa ) ) {
763         aContext = co;
764         aObject  = ob;
765         aParent  = pa;
766       }
767     }
768   }                        //__CALL_OLD_METHODS__
769
770   // first try to create menu via XML parser:
771   // we create popup menus without help of QtxPopupMgr
772   if ( myXmlHandler )
773     myXmlHandler->createPopup( thePopupMenu, aContext, aParent, aObject );
774
775   PyObjWrapper sipPopup( sipBuildResult( 0, "M", thePopupMenu, sipClass_QPopupMenu ) );
776
777   // then call Python module's createPopupMenu() method (for new modules)
778   PyObjWrapper res1( PyObject_CallMethod( myModule,
779                                           "createPopupMenu",
780                                           "Os",
781                                           sipPopup.get(),
782                                           aContext.latin1() ) );
783   if( !res1 ) {
784     // VSR: this method may not be implemented in Python module
785     // PyErr_Print();
786     PyErr_Clear();
787   }
788
789   if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
790     // call customPopup() Python module's function
791     // this is obsolete function, used only for compatibility reasons
792     PyObjWrapper res2( PyObject_CallMethod( myModule,
793                                             "customPopup",
794                                             "Osss",
795                                             sipPopup.get(),
796                                             aContext.latin1(), 
797                                             aObject.latin1(), 
798                                             aParent.latin1() ) );
799     if( !res2 ) {
800       // VSR: this method may not be implemented in Python module
801       // PyErr_Print();
802       PyErr_Clear();
803     }
804   }                        //__CALL_OLD_METHODS__
805 }
806
807 /*!
808  * Processes GUI event
809  * - calls Python module's OnGUIEvent() method
810  */ 
811 void SALOME_PYQT_Module::guiEvent( const int theId )
812 {
813   // Python interpreter should be initialized and Python module should be
814   // import first
815   if ( !myInterp || !myModule )
816     return;
817   
818   PyObjWrapper res( PyObject_CallMethod( myModule, "OnGUIEvent", "i", theId ) );
819   if( !res ) {
820     // VSR: this method may not be implemented in Python module
821     // PyErr_Print();
822     PyErr_Clear();
823   }
824 }
825
826 /*!
827  *  Initialises python subinterpreter (one per study)
828  */
829 void SALOME_PYQT_Module::initInterp( int theStudyId )
830 {
831   // check study Id
832   if ( !theStudyId ) {
833     // Error! Study Id must not be 0!
834     myInterp = NULL;
835     return;
836   }
837   // try to find the subinterpreter
838   if( myInterpMap.find( theStudyId ) != myInterpMap.end() ) {
839     // found!
840     myInterp = myInterpMap[ theStudyId ];
841     return;
842   }
843   // not found - create a new one!
844   ///////////////////////////////////////////////////////////////////
845   // Attention: the creation of Python interpretor must be protected 
846   // by a C++ Lock because of C threads
847   ///////////////////////////////////////////////////////////////////
848   myInterp = new SALOME_PYQT_PyInterp();
849   myInterp->initialize();
850   myInterpMap[ theStudyId ] = myInterp;
851    
852   // import 'salome' module and call 'salome_init' method;
853   // do it only once on interpreter creation
854   // ... first get python lock
855   PyLockWrapper aLock = myInterp->GetLockWrapper();
856   // ... then import a module
857   PyObjWrapper aMod = PyImport_ImportModule( "salome" );
858   if( !aMod ) {
859     // Error!
860     PyErr_Print();
861     return;
862   }
863   // ... then call a method
864   PyObjWrapper aRes( PyObject_CallMethod( aMod, "salome_init", "" ) );
865   if( !aRes ) {
866     // Error!
867     PyErr_Print();
868     return;
869   }
870 }
871
872 /*!
873  *  Imports Python GUI module and remember the reference to the module
874  *  !!! initInterp() should be called first!!!
875  */
876 void SALOME_PYQT_Module::importModule()
877 {
878   // check if the subinterpreter is initialized
879   if ( !myInterp ) {
880     // Error! Python subinterpreter should be initialized first!
881     myModule = 0;
882     return;
883   }
884   // import Python GUI module and puts it in <myModule> attribute
885   // ... first get python lock
886   PyLockWrapper aLock = myInterp->GetLockWrapper();
887   // ... then import a module
888   QString aMod = QString( name("") ) + "GUI";
889   myModule = PyImport_ImportModule( (char*)( aMod.latin1() ) );
890   if( !myModule ) {
891     // Error!
892     PyErr_Print();
893     return;
894   }
895 }
896
897 /*!
898  *  Calls <module>.setWorkSpace() method with PyQt QWidget object to use with
899  *  interpreter.
900  *  !!! initInterp() and importModule() should be called first!!!
901  */
902 void SALOME_PYQT_Module::setWorkSpace()
903 {
904   // check if the subinterpreter is initialized and Python module is imported
905   if ( !myInterp || !myModule ) {
906     // Error! Python subinterpreter should be initialized and module should be imported first!
907     return;
908   }
909
910   // call setWorkspace() method
911   // ... first get python lock
912   PyLockWrapper aLock = myInterp->GetLockWrapper();
913
914   // ... then try to import SalomePyQt module. If it's not possible don't go on.
915   PyObjWrapper aQtModule( PyImport_ImportModule( "SalomePyQt" ) );
916   if( !aQtModule ) {
917     // Error!
918     PyErr_Print();
919     return;
920   }  
921
922   if ( IsCallOldMethods ) { //__CALL_OLD_METHODS__
923     // ... then get workspace object
924     QWidget* aWorkspace = 0;
925     if ( getApp()->desktop()->inherits( "STD_MDIDesktop" ) ) {
926       STD_MDIDesktop* aDesktop = dynamic_cast<STD_MDIDesktop*>( getApp()->desktop() );
927       if ( aDesktop )
928         aWorkspace = aDesktop->workspace();
929     }
930     else if ( getApp()->desktop()->inherits( "STD_TabDesktop" ) ) {
931       STD_TabDesktop* aDesktop = dynamic_cast<STD_TabDesktop*>( getApp()->desktop() );
932       if ( aDesktop )
933         aWorkspace = aDesktop->workstack();
934     }
935     PyObjWrapper pyws( sipBuildResult( 0, "M", aWorkspace, sipClass_QWidget ) );
936     // ... and finally call Python module's setWorkspace() method (obsolete)
937     PyObjWrapper res( PyObject_CallMethod( myModule, "setWorkSpace", "O", pyws.get() ) );
938     if( !res ) {
939       // VSR: this method may not be implemented in Python module
940       // PyErr_Print();
941       PyErr_Clear();
942     }
943   }                         //__CALL_OLD_METHODS__
944 }
945
946 /*!
947  * Adds an action into private action list [internal usage]
948  */
949 void SALOME_PYQT_Module::addAction( const PyQtGUIAction type, QAction* action )
950 {
951   switch ( type ) {
952   case PYQT_ACTION_MENU:
953     myMenuActionList.append( action );
954     break;
955   case PYQT_ACTION_TOOLBAL:
956     myToolbarActionList.append( action );
957     break;
958   case PYQT_ACTION_POPUP:
959     myPopupActionList.append( action );
960     break;
961   }
962 }
963
964
965 /*!
966  * The next methods just call the parent implementation.
967  * This is done to open protected methods from CAM_Module class.
968 */
969 int SALOME_PYQT_Module::createTool( const QString& name )
970 {
971   return SalomeApp_Module::createTool( name );
972 }
973 int SALOME_PYQT_Module::createTool( const int id, const int tBar, const int idx )
974 {
975   return SalomeApp_Module::createTool( id, tBar, idx );
976 }
977 int SALOME_PYQT_Module::createTool( const int id, const QString& tBar, const int idx )
978 {
979   return SalomeApp_Module::createTool( id, tBar, idx );
980 }
981 int SALOME_PYQT_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
982 {
983   return SalomeApp_Module::createTool( a, tBar, id, idx );
984 }
985 int SALOME_PYQT_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
986 {
987   return SalomeApp_Module::createTool( a, tBar, id, idx );
988 }
989 int SALOME_PYQT_Module::createMenu( const QString& subMenu, const int menu, const int id, const int group, const int idx )
990 {
991   return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx );
992 }
993 int SALOME_PYQT_Module::createMenu( const QString& subMenu, const QString& menu, const int id, const int group, const int idx )
994 {
995   return SalomeApp_Module::createMenu( subMenu, menu, id, group, idx );
996 }
997 int SALOME_PYQT_Module::createMenu( const int id, const int menu, const int group, const int idx )
998 {
999   return SalomeApp_Module::createMenu( id, menu, group, idx );
1000 }
1001 int SALOME_PYQT_Module::createMenu( const int id, const QString& menu, const int group, const int idx )
1002 {
1003   return SalomeApp_Module::createMenu( id, menu, group, idx );
1004 }
1005 int SALOME_PYQT_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int idx )
1006 {
1007   return SalomeApp_Module::createMenu( a, menu, id, group, idx );
1008 }
1009 int SALOME_PYQT_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int idx )
1010 {
1011   return SalomeApp_Module::createMenu( a, menu, id, group, idx );
1012 }
1013 QAction* SALOME_PYQT_Module::createSeparator()
1014 {
1015   return SalomeApp_Module::separator();
1016 }
1017 QAction* SALOME_PYQT_Module::action( const int id ) const
1018 {
1019   QAction* a = SalomeApp_Module::action( id );
1020   if ( !a ) // try own action map for menu items
1021     a = SalomeApp_Module::action( id + PYQT_ACTION_MENU );
1022   if ( !a ) // try own action map for toolbar items
1023     a = SalomeApp_Module::action( id + PYQT_ACTION_TOOLBAL );
1024   if ( !a ) // try own action map for popup items
1025     a = SalomeApp_Module::action( id + PYQT_ACTION_POPUP );
1026   return a;
1027 }
1028 int SALOME_PYQT_Module::actionId( const QAction* a ) const
1029 {
1030   int id = SalomeApp_Module::actionId( a );
1031   if ( myMenuActionList.contains( a ) )    // check own action map for menu items
1032     id -= PYQT_ACTION_MENU;
1033   if ( myToolbarActionList.contains( a ) ) // check own action map for toolbar items
1034     id -= PYQT_ACTION_TOOLBAL;
1035   if ( myPopupActionList.contains( a ) )   // check own action map for popup items
1036     id -= PYQT_ACTION_POPUP;
1037   return id;
1038 }
1039 QAction* SALOME_PYQT_Module::createAction( const int id, const QString& text, const QString& icon, 
1040                                            const QString& menu, const QString& tip, const int key,
1041                                            const bool toggle )
1042 {
1043   QIconSet anIcon;
1044   if ( !icon.isEmpty() ) {
1045     QPixmap pixmap  = getApp()->resourceMgr()->loadPixmap( name(""), tr( icon ) );
1046     if ( !pixmap.isNull() )
1047       anIcon = QIconSet( pixmap );
1048   }
1049   return SalomeApp_Module::createAction( id, text, anIcon, menu, tip, key, getApp()->desktop(), toggle, this, SLOT( onGUIEvent() ) );
1050 }
1051
1052
1053 //=============================================================================
1054 // SALOME_PYQT_XmlHandler class implementation
1055 //=============================================================================
1056
1057 // gets an tag name for the dom element [ static ]
1058 // returns an empty string if the element does not have tag name
1059 static QString tagName( const QDomElement& element ) {
1060  return element.tagName().stripWhiteSpace();
1061 }
1062
1063 // gets an attribute by it's name for the dom element [ static ]
1064 // returns an empty string if the element does not have such attribute
1065 static QString attribute( const QDomElement& element, const QString& attName ) {
1066   return element.attribute( attName ).stripWhiteSpace();
1067 }
1068
1069 // checks the given value for the boolean value [ static ]
1070 // returns TRUE if string is "true", "yes" or "1"
1071 static bool checkBool( const QString& value ) {
1072   return ( value == "true" || value == "yes" || value == "1" );
1073 }
1074
1075 // checks the given value for the integer value [ static ]
1076 // returns -1 if item is empty or presents and invalid number
1077 static int checkInt( const QString& value ) 
1078 {
1079   return value.isEmpty() ? -1 : value.toInt();
1080 }
1081
1082 /*!
1083  * Constructor
1084  */
1085 SALOME_PYQT_XmlHandler::SALOME_PYQT_XmlHandler( SALOME_PYQT_Module* module, const QString& fileName ) 
1086      : myModule( module )
1087 {
1088   QFile aFile( fileName );
1089   if ( !aFile.open( IO_ReadOnly ) )
1090     return;
1091   if ( !myDoc.setContent( &aFile ) ) {
1092       aFile.close();
1093       return;
1094   }
1095   aFile.close();
1096 }
1097
1098 /*!
1099   Called by SALOME_PYQT_Module::initialize() in order to create actions 
1100   (menus, toolbars, popup menus)
1101  */
1102 void SALOME_PYQT_XmlHandler::createActions()
1103 {
1104   // get document element
1105   QDomElement aDocElem = myDoc.documentElement();
1106
1107   // get main menu actions
1108   QDomNodeList aMenuList = aDocElem.elementsByTagName( "menu-item" );
1109   for ( int i = 0; i < aMenuList.count(); i++ ) {
1110     QDomNode n = aMenuList.item( i );
1111     createMenu( n );
1112   }
1113
1114   // create toolbars actions
1115   QDomNodeList aToolsList = aDocElem.elementsByTagName( "toolbar" );
1116   for ( int i = 0; i < aToolsList.count(); i++ ) {
1117     QDomNode n = aToolsList.item( i );
1118     createToolBar( n );
1119   }
1120 }
1121
1122 /*!
1123  *  Creates popup menu
1124  */
1125 void SALOME_PYQT_XmlHandler::createPopup( QPopupMenu*    menu, 
1126                                           const QString& context, 
1127                                           const QString& parent, 
1128                                           const QString& object )
1129 {
1130   // get document element
1131   QDomElement aDocElem = myDoc.documentElement();
1132
1133   // get popup menus actions
1134   QDomNodeList aPopupList = aDocElem.elementsByTagName( "popupmenu" );
1135   for ( int i = 0; i < aPopupList.count(); i++ ) {
1136     QDomNode n = aPopupList.item( i );
1137     if ( !n.isNull() && n.isElement() ) {
1138       QDomElement e = n.toElement();
1139       QString lab = attribute( e, "label-id"   );
1140       QString ctx = attribute( e, "context-id" );
1141       QString prt = attribute( e, "parent-id"  );
1142       QString obj = attribute( e, "object-id"  );
1143       if ( ctx == context && prt == parent && obj == object )  {
1144         insertPopupItems( n, menu );
1145         break;
1146       }
1147     }
1148   }
1149 }
1150
1151 /*!
1152   Create main menu with child actions
1153  */
1154 void SALOME_PYQT_XmlHandler::createMenu( QDomNode& parentNode, const int parentMenuId )
1155 {
1156   if ( !myModule )
1157     return;
1158   
1159   if ( parentNode.isNull() )
1160     return;
1161
1162   QDomElement parentElement = parentNode.toElement(); 
1163   if ( !parentElement.isNull() ) {
1164     QString plabel = attribute( parentElement, "label-id" );
1165     int     pid    = checkInt( attribute( parentElement, "item-id" ) );
1166     int     ppos   = checkInt( attribute( parentElement, "pos-id" ) );
1167     if ( !plabel.isEmpty() ) {
1168       // create menu
1169       int menuId = myModule->createMenu( plabel,         // label
1170                                          parentMenuId,   // parent menu ID, should be -1 for main menu
1171                                          pid,            // ID
1172                                          80,             // group ID
1173                                          ppos );         // position
1174       QDomNode node = parentNode.firstChild();
1175       while ( !node.isNull() ) {
1176         if ( node.isElement() ) {
1177           QDomElement elem = node.toElement();
1178           QString aTagName = tagName( elem );
1179           if ( aTagName == "popup-item" ) {
1180             int     id      = checkInt( attribute( elem, "item-id" ) );
1181             int     pos     = checkInt( attribute( elem, "pos-id" ) );
1182             QString label   = attribute( elem, "label-id" );
1183             QString icon    = attribute( elem, "icon-id" );
1184             QString tooltip = attribute( elem, "tooltip-id" );
1185             QString accel   = attribute( elem, "accel-id" );
1186             bool    toggle  = checkBool( attribute( elem, "toggle-id" ) );
1187             ////QString execute = attribute( elem, "execute-action" );               // not used
1188
1189             // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1190             // also check if the action with given ID is already created
1191             if ( id != -1 && !myModule->action( SALOME_PYQT_Module::PYQT_ACTION_MENU + id ) ) {
1192               // little trick to have several actions with same ID for menus and toolbars
1193               id = SALOME_PYQT_Module::PYQT_ACTION_MENU + id;
1194               // create menu action
1195               QAction* action = myModule->createAction( id,                               // ID
1196                                                         tooltip,                          // tooltip
1197                                                         icon,                             // icon
1198                                                         label,                            // menu text
1199                                                         tooltip,                          // status-bar text
1200                                                         QKeySequence( accel ),            // keyboard accelerator
1201                                                         toggle );                         // toogled action
1202               myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_MENU, action );
1203               myModule->createMenu( action, menuId, -1, 80, pos );
1204             }
1205           }
1206           else if ( aTagName == "submenu" ) {
1207             // create sub-menu
1208             createMenu( node, menuId );
1209           }
1210           else if ( aTagName == "separator" ) {
1211             // create menu separator
1212             int     pos     = checkInt( attribute( elem, "pos-id" ) );
1213             QAction* action = myModule->createSeparator();
1214             myModule->createMenu( action, menuId, -1, 80, pos );
1215           }
1216         }
1217         node = node.nextSibling();
1218       }
1219     }
1220   }
1221 }
1222
1223 /*!
1224   Create a toolbar with child actions
1225  */
1226 void SALOME_PYQT_XmlHandler::createToolBar( QDomNode& parentNode )
1227 {
1228   if ( !myModule )
1229     return;
1230   
1231   if ( parentNode.isNull() )
1232     return;
1233
1234   QDomElement parentElement = parentNode.toElement(); 
1235   if ( !parentElement.isNull() ) {
1236     QString aLabel = attribute( parentElement, "label-id" );
1237     if ( !aLabel.isEmpty() ) {
1238       // create toolbar
1239       int tbId = myModule->createTool( aLabel );
1240       QDomNode node = parentNode.firstChild();
1241       while ( !node.isNull() ) {
1242         if ( node.isElement() ) {
1243           QDomElement elem = node.toElement();
1244           QString aTagName = tagName( elem );
1245           if ( aTagName == "toolbutton-item" ) {
1246             int     id      = checkInt( attribute( elem, "item-id" ) );
1247             int     pos     = checkInt( attribute( elem, "pos-id" ) );
1248             QString label   = attribute( elem, "label-id" );
1249             QString icon    = attribute( elem, "icon-id" );
1250             QString tooltip = attribute( elem, "tooltip-id" );
1251             QString accel   = attribute( elem, "accel-id" );
1252             bool    toggle  = checkBool( attribute( elem, "toggle-id" ) );
1253             ////QString execute = attribute( elem, "execute-action" );               // not used
1254
1255             // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1256             // also check if the action with given ID is already created
1257             if ( id != -1 && !myModule->action( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL + id ) ) {
1258               // little trick to have several actions with same ID for menus and toolbars
1259               id = SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL + id;
1260               // create toolbar action
1261               QAction* action = myModule->createAction( id,                               // ID
1262                                                         tooltip,                          // tooltip
1263                                                         icon,                             // icon
1264                                                         label,                            // menu text
1265                                                         tooltip,                          // status-bar text
1266                                                         QKeySequence( accel ),            // keyboard accelerator
1267                                                         toggle );                         // toogled action
1268               myModule->addAction( SALOME_PYQT_Module::PYQT_ACTION_TOOLBAL, action );
1269               myModule->createTool( action, tbId, -1, pos );
1270             }
1271           }
1272           else if ( aTagName == "separatorTB" ) {
1273             // create toolbar separator
1274             int     pos     = checkInt( attribute( elem, "pos-id" ) );
1275             QAction* action = myModule->createSeparator();
1276             myModule->createTool( action, tbId, -1, pos );
1277           }
1278         }
1279         node = node.nextSibling();
1280       }
1281     }
1282   }      
1283 }
1284
1285 void SALOME_PYQT_XmlHandler::insertPopupItems( QDomNode& parentNode, QPopupMenu* menu )
1286 {
1287   if ( !myModule )
1288     return;
1289   
1290   if ( parentNode.isNull() )
1291     return;
1292
1293   // we create popup menus without help of QtxPopupMgr
1294   QDomNode node = parentNode.firstChild();
1295   while ( !node.isNull() ) {
1296     if ( node.isElement() ) {
1297       QDomElement elem = node.toElement();
1298       QString aTagName = tagName( elem );
1299       if ( aTagName == "popup-item" ) {
1300         // insert a command item
1301         int     id      = checkInt( attribute( elem, "item-id" ) );
1302         int     pos     = checkInt( attribute( elem, "pos-id" ) );
1303         QString label   = attribute( elem, "label-id" );
1304         QString icon    = attribute( elem, "icon-id" );
1305         /////QString tooltip = attribute( elem, "tooltip-id" );                   // not used
1306         QString accel   = attribute( elem, "accel-id" );
1307         /////bool    toggle  = checkBool( attribute( elem, "toggle-id" ) );       // not used
1308         /////QString execute = attribute( elem, "execute-action" );               // not used
1309
1310         QIconSet anIcon;
1311         if ( !icon.isEmpty() ) {
1312           QPixmap pixmap  = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon );
1313           if ( !pixmap.isNull() )
1314             anIcon = QIconSet( pixmap );
1315         }
1316         
1317         // -1 action ID is not allowed : it means that <item-id> attribute is missed in the XML file!
1318         // also check if the action with given ID is already created
1319         if ( id != -1 ) {
1320           menu->insertItem( anIcon, label, myModule, SLOT( onGUIEvent(int) ), QKeySequence( accel ), id, pos );
1321         }
1322       }
1323       else if ( aTagName == "submenu" ) {
1324         // create sub-menu
1325         int     id    = checkInt( attribute( elem, "item-id" ) );
1326         int     pos   = checkInt( attribute( elem, "pos-id" ) );
1327         QString label = attribute( elem, "label-id" );
1328         QString icon    = attribute( elem, "icon-id" );
1329
1330         QIconSet anIcon;
1331         if ( !icon.isEmpty() ) {
1332           QPixmap pixmap  = myModule->getApp()->resourceMgr()->loadPixmap( myModule->name(""), icon );
1333           if ( !pixmap.isNull() )
1334             anIcon = QIconSet( pixmap );
1335         }
1336
1337         QPopupMenu* newPopup = new QPopupMenu( menu, label );
1338         menu->insertItem( anIcon, label, newPopup, id, pos );
1339         insertPopupItems( node, newPopup );
1340       }
1341       else if ( aTagName == "separator" ) {
1342         // create menu separator
1343         int     pos     = checkInt( attribute( elem, "pos-id" ) );
1344         menu->insertSeparator( pos );
1345       }
1346     }
1347     node = node.nextSibling();
1348   }
1349 }