1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOME_PYQT_ModuleLight.cxx
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
23 #include "SALOME_PYQT_DataModelLight.h"
24 #include "SALOME_PYQT_ModuleLight.h"
25 #include "SALOME_PYQT_PyModule.h"
27 #include "CAM_Application.h"
28 #include "SUITApp_init_python.hxx"
29 #include "SUIT_DataObjectIterator.h"
30 #include "LightApp_Application.h"
31 #include "SUIT_DataBrowser.h"
32 #include "sipAPISalomePyQtGUILight.h"
34 #ifndef GUI_DISABLE_CORBA
35 #include <Container_init_python.hxx>
38 #include <QCoreApplication>
40 // Py_ssize_t for old Pythons
41 // This code is as recommended by"
42 // http://www.python.org/dev/peps/pep-0353/#conversion-guidelines
43 //#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
44 //typedef int Py_ssize_t;
45 //# define PY_SSIZE_T_MAX INT_MAX
46 //# define PY_SSIZE_T_MIN INT_MIN
50 // NB: Python requests.
51 // General rule for Python requests created by Python-based GUI modules
52 // (SALOME_PYQT_ModuleLight and other ones):
53 // all requests should be executed SYNCHRONOUSLY within the main GUI thread.
54 // However, it is obligatory that ANY Python call is wrapped with a request object,
55 // so that ALL Python API calls are serialized with PyInterp_Dispatcher.
57 // NB: Library initialization
58 // Since the SalomePyQtGUILight library is not imported in Python it's initialization function
59 // should be called manually (and only once) in order to initialize global sip data
60 // and to get C API from sip : sipBuildResult for example
63 #define INIT_FUNCTION initSalomePyQtGUILight
64 #if defined(SIP_STATIC_MODULE)
65 extern "C" void INIT_FUNCTION();
67 PyMODINIT_FUNC INIT_FUNCTION();
71 \fn CAM_Module* createModule()
72 \brief Module factory function.
75 Creates an instance of SALOME_PYQT_Module object by request
76 of an application when the module is loaded and initialized.
78 \return new module object
83 SALOME_PYQT_LIGHT_EXPORT CAM_Module* createModule()
85 QCoreApplication* app = QCoreApplication::instance();
86 bool alreadyInitialized = app && app->property( "salome_pyqt_gui_light_initialized" ).toBool();
88 // make initialization only once (see comment above) !
89 if ( !alreadyInitialized ) {
90 PyLockWrapper lck; // GIL acquisition
92 if ( app ) app->setProperty( "salome_pyqt_gui_light_initialized", true );
95 return new SALOME_PYQT_ModuleLight();
100 \class SALOME_PYQT_ModuleLight
101 \brief This class implements GUI module for "light-weight" (no-CORBA-engine)
102 Python-based SALOME modules.
108 SALOME_PYQT_ModuleLight::SALOME_PYQT_ModuleLight()
109 : LightApp_Module( "noname" ) // name is set explicitly at the module initialization
112 myHelper = new PyModuleHelper( this );
118 SALOME_PYQT_ModuleLight::~SALOME_PYQT_ModuleLight()
120 // as myHelper is a QObject, it should be deleted automatically
124 \brief Initialization of the module.
125 \param app parent application object
126 \sa PyModuleHelper::initialize()
128 void SALOME_PYQT_ModuleLight::initialize( CAM_Application* app )
130 // call base implementation
131 LightApp_Module::initialize( app );
133 // ... then call helper
134 myHelper->initialize( app );
135 SUIT_DataBrowser* ob = getApp()->objectBrowser();
136 if (ob && ob->model()) {
137 connect( ob->model(), SIGNAL( clicked( SUIT_DataObject*, int ) ),
138 myHelper, SLOT( onObjectBrowserClicked( SUIT_DataObject*, int ) ), Qt::UniqueConnection );
143 \brief Activation of the module.
144 \param study parent study
145 \return \c true if activation is successful and \c false otherwise
146 \sa PyModuleHelper::activate()
148 bool SALOME_PYQT_ModuleLight::activateModule( SUIT_Study* study )
150 // call base implementation and then helper
151 return LightApp_Module::activateModule( study ) && myHelper->activate( study );
155 \brief Deactivation of the module.
156 \param study parent study
157 \return \c true if deactivation is successful and \c false otherwise
158 \sa PyModuleHelper::deactivate()
160 bool SALOME_PYQT_ModuleLight::deactivateModule( SUIT_Study* study )
163 bool res = myHelper->deactivate( study );
165 // ... then call base implementation
166 return LightApp_Module::deactivateModule( study ) && res;
170 \brief Close of the module.
172 This function is usually used in order to close the module's
173 specific menus and toolbars and perform other such actions
174 required when the module is closed.
176 void SALOME_PYQT_ModuleLight::onModelClosed()
179 myHelper->modelClosed(application()->activeStudy());
180 LightApp_Module::onModelClosed();
185 \brief Get the dockable windows associated with the module.
186 \param winMap output map of dockable windows in form { <window_type> : <dock_area> }
187 \sa PyModuleHelper::windows()
189 void SALOME_PYQT_ModuleLight::windows( QMap<int, int>& winMap ) const
191 // get list of dockable windows from helper
192 winMap = myHelper->windows();
196 \brief Define the compatible view windows associated with the module.
197 \param viewList output list of view windows types
198 \sa PyModuleHelper::viewManagers()
200 void SALOME_PYQT_ModuleLight::viewManagers( QStringList& viewList ) const
202 // get list of view types from helper
203 viewList = myHelper->viewManagers();
207 \brief Process study activation.
208 \sa PyModuleHelper::studyActivated()
210 void SALOME_PYQT_ModuleLight::studyActivated()
213 myHelper->studyActivated( application()->activeStudy() );
217 \brief Process context popup menu request.
218 \param context popup menu context (e.g. "ObjectBrowser")
219 \param menu popup menu
220 \param title popup menu title (not used)
221 \sa PyModuleHelper::contextMenu()
223 void SALOME_PYQT_ModuleLight::contextMenuPopup( const QString& context,
228 myHelper->contextMenu( context, menu );
232 \brief Export preferences for the Python module.
233 \sa PyModuleHelper::createPreferences()
235 void SALOME_PYQT_ModuleLight::createPreferences()
238 myHelper->createPreferences();
242 \brief Process module's preferences changing.
243 \param section preference resources section
244 \param parameter preference resources parameter name
245 \sa PyModuleHelper::preferencesChanged()
247 void SALOME_PYQT_ModuleLight::preferencesChanged( const QString& section, const QString& parameter )
250 myHelper->preferencesChanged( section, parameter );
254 \brief Save module data. Called when user saves study.
255 \param files output list of files where module stores data
256 \sa PyModuleHelper::save()
258 void SALOME_PYQT_ModuleLight::save( QStringList& files )
261 myHelper->save( files );
265 \brief Load module data. Called when user opens study
266 and activates module.
267 \param files list of files where module data is stored
268 \sa PyModuleHelper::load()
270 bool SALOME_PYQT_ModuleLight::load( const QStringList& files )
273 return myHelper->load( files );
277 \brief Dump module data to the Python script.
278 Called when user activates dump study operation.
279 \param files output list of files where module stores python script
280 \sa PyModuleHelper::dumpPython()
282 void SALOME_PYQT_ModuleLight::dumpPython( QStringList& files )
285 myHelper->dumpPython( files );
289 \brief Test if object \a what can be dragged by the user.
290 \param what data object being tested
291 \return \c true if object can be dragged or \c false otherwise
292 \sa PyModuleHelper::isDraggable()
294 bool SALOME_PYQT_ModuleLight::isDraggable( const SUIT_DataObject* what ) const
297 return myHelper->isDraggable( what );
301 \brief Test if drop operation can be done on the \a where object.
302 \param where data object being tested
303 \return \c true if if drop operation is supported by object or \c false otherwise
304 \sa PyModuleHelper::isDropAccepted()
306 bool SALOME_PYQT_ModuleLight::isDropAccepted( const SUIT_DataObject* where ) const
309 return myHelper->isDropAccepted( where );
313 \brief Perform drop operation
314 \param what list of data objects being dropped
315 \param where target data object for drop operation
316 \param row line (child item index) where drop operation is performed to
317 \param action current drop action (copy or move)
318 \sa PyModuleHelper::dropObjects()
320 void SALOME_PYQT_ModuleLight::dropObjects( const DataObjectList& what, SUIT_DataObject* where,
321 const int row, Qt::DropAction action )
324 myHelper->dropObjects( what, where, row, action );
328 \brief Create new empty data object
329 \param parent entry of parent data object
330 \return entry of created data object
332 QString SALOME_PYQT_ModuleLight::createObject( const QString& parent )
335 SALOME_PYQT_DataObjectLight* obj = 0;
337 if ( !parent.isEmpty() ) {
338 SALOME_PYQT_DataObjectLight* parentObj = findObject( parent );
340 obj = new SALOME_PYQT_DataObjectLight( parentObj );
343 SALOME_PYQT_DataModelLight* dm =
344 dynamic_cast<SALOME_PYQT_DataModelLight*>( dataModel() );
346 obj = new SALOME_PYQT_DataObjectLight( dm->getRoot() );
349 entry = obj->entry();
354 \brief Create new data object with specified name, icon and tooltip
355 \param name data object name
356 \param icon data object icon
357 \param toolTip data object tooltip
358 \param parent entry of parent data object
359 \return entry of created data object
361 QString SALOME_PYQT_ModuleLight::createObject( const QString& name,
363 const QString& toolTip,
364 const QString& parent )
366 QString entry = createObject( parent );
367 SALOME_PYQT_DataObjectLight* obj = findObject( entry );
370 obj->setToolTip( toolTip );
371 obj->setIcon( icon );
377 \brief Set data object name
378 \param entry data object entry
379 \param name data object name
381 void SALOME_PYQT_ModuleLight::setName( const QString& entry, const QString& name )
383 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
385 dataObj->setName( name );
389 \brief Get data object name
390 \param entry data object entry
391 \return data object name
393 QString SALOME_PYQT_ModuleLight::getName( const QString& entry ) const
396 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
398 name = dataObj->name();
403 \brief Set data object icon
404 \param entry data object entry
405 \param icon data object icon file name (icon is loaded from module resources)
407 void SALOME_PYQT_ModuleLight::setIcon( const QString& entry, const QString& icon )
409 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
411 dataObj->setIcon( icon );
415 \brief Set data object tooltip
416 \param entry data object entry
417 \param toolTip data object tooltip
419 void SALOME_PYQT_ModuleLight::setToolTip( const QString& entry, const QString& toolTip )
421 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
423 dataObj->setToolTip( toolTip );
427 \brief Get data object tooltip
428 \param entry data object entry
429 \return data object tooltip
431 QString SALOME_PYQT_ModuleLight::getToolTip( const QString& entry ) const
434 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
436 toolTip = dataObj->toolTip();
441 \brief Set data object color
442 \param entry data object entry
443 \param color data object color
445 void SALOME_PYQT_ModuleLight::setColor( const QString& entry, const QColor& color )
447 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
449 dataObj->setColor( color );
453 \brief Get data object color
454 \param entry data object entry
455 \return data object color
457 QColor SALOME_PYQT_ModuleLight::getColor( const QString& entry ) const
460 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
462 color = dataObj->color( SUIT_DataObject::Foreground );
466 void SALOME_PYQT_ModuleLight::setObjectPosition( const QString& theEntry, int thePos )
468 SALOME_PYQT_DataObjectLight* dataObj = findObject( theEntry );
470 dataObj->setPosition(thePos);
473 int SALOME_PYQT_ModuleLight::getObjectPosition( const QString& theEntry )
475 SALOME_PYQT_DataObjectLight* dataObj = findObject( theEntry );
477 return dataObj->position();
483 \brief Set reference to another data object
484 \param entry data object entry
485 \param refEntry referenced data object entry
487 void SALOME_PYQT_ModuleLight::setReference( const QString& entry, const QString& refEntry )
489 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
491 dataObj->setRefEntry( refEntry );
495 \brief Get entry of the referenced object (if there's any)
496 \param entry data object entry
497 \return referenced data object entry
499 QString SALOME_PYQT_ModuleLight::getReference( const QString& entry ) const
502 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
504 refEntry = dataObj->refEntry();
509 \brief Remove object by entry
510 \param entry data object entry
512 void SALOME_PYQT_ModuleLight::removeObject( const QString& entry )
514 SALOME_PYQT_DataObjectLight* dataObj = findObject( entry );
515 if ( dataObj && dataObj->parent() )
516 dataObj->parent()->removeChild( dataObj );
520 \brief Remove all child data objects from specified data object
521 \param entry data object entry
523 void SALOME_PYQT_ModuleLight::removeChildren( const QString& entry )
525 SUIT_DataObject* dataObj = 0;
526 if ( !entry.isEmpty() ) {
527 dataObj = findObject( entry );
530 SALOME_PYQT_DataModelLight* dm =
531 dynamic_cast<SALOME_PYQT_DataModelLight*>( dataModel() );
532 if ( dm ) dataObj = dm->getRoot();
535 DataObjectList children;
536 dataObj->children( children );
537 QListIterator<SUIT_DataObject*> it( children );
538 while ( it.hasNext() ) {
539 dataObj->removeChild( it.next() );
545 \brief Get entries of all child data objects of specified data object
546 \param entry data object entry
547 \param recursive \c true for recursive processing
549 QStringList SALOME_PYQT_ModuleLight::getChildren( const QString& entry, const bool recursive ) const
551 QStringList entryList;
552 SUIT_DataObject* dataObj = 0;
553 if ( !entry.isEmpty() ) {
554 dataObj = findObject( entry );
557 SALOME_PYQT_DataModelLight* dm =
558 dynamic_cast<SALOME_PYQT_DataModelLight*>( dataModel() );
559 if ( dm ) dataObj = dm->getRoot();
563 dataObj->children( lst, recursive );
564 QListIterator<SUIT_DataObject*> it( lst );
565 while ( it.hasNext() ) {
566 SALOME_PYQT_DataObjectLight* sobj = dynamic_cast<SALOME_PYQT_DataObjectLight*>( it.next() );
567 entryList.append( sobj->entry() );
574 \brief Create new instance of data model and return it.
576 CAM_DataModel* SALOME_PYQT_ModuleLight::createDataModel()
578 return new SALOME_PYQT_DataModelLight( this );
582 \brief Find data object by its entry
583 \param entry data object entry
584 \return data object with given entry or 0 if object isn't found
586 SALOME_PYQT_DataObjectLight* SALOME_PYQT_ModuleLight::findObject( const QString& entry ) const
588 SALOME_PYQT_DataObjectLight* obj = 0;
589 SALOME_PYQT_DataModelLight* dm =
590 dynamic_cast<SALOME_PYQT_DataModelLight*>( dataModel() );
591 if ( !entry.isEmpty() && dm ) {
592 for ( SUIT_DataObjectIterator it( dm->getRoot(), SUIT_DataObjectIterator::DepthLeft ); it.current() && !obj; ++it ) {
593 SALOME_PYQT_DataObjectLight* curentobj =
594 dynamic_cast<SALOME_PYQT_DataObjectLight*>( it.current() );
595 if ( curentobj && curentobj->entry() == entry )