1 // Copyright (C) 2011-2012 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.
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
19 // Author : Guillaume Boulant (EDF)
21 #include "StandardApp_Module.hxx"
23 #include <SUIT_Desktop.h>
24 #include <SUIT_Study.h>
25 #include <SalomeApp_Application.h>
26 #include <SALOME_LifeCycleCORBA.hxx>
27 #include "QtxPopupMgr.h"
29 #include CORBA_CLIENT_HEADER(SALOMEDS)
30 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
36 #include <Basics_Utils.hxx>
37 #include "QtHelper.hxx"
40 StandardApp_Module::StandardApp_Module() :
41 SalomeApp_Module( "" ),
44 // Note that it's no use to specify here the moduleName because it's
45 // automatically determined by the name of the GUI library
46 // (lib<moduleName>.so) just before initialize step. If you want
47 // nevertheless to setup the module name you should specified this
48 // name in the initialize function below using the CAM_module
49 // function setModuleName(QString&). You are warn then that the
50 // loading of resource files may be incorect and should be manually
51 // managed. In conclusion, if you don't need a special
52 // configuration, please conform to the standard convention: if
53 // <moduleName> is the name of the module as declared in the GUI
54 // configuration file (SalomeApp.xml), then you just have to provide
55 // a library whose name is lib<moduleName>.so and which contains a
56 // factory "extern C" function 'CAM_Module* createModule()'
59 int StandardApp_Module::ACTIONID_DEFAULT_INIT_VALUE = 910;
60 int StandardApp_Module::ACTIONID_UNDEFINED = -1;
62 // Module's initialization
63 void StandardApp_Module::initialize( CAM_Application* app )
66 // ----------------------------------------------------------------
67 // Standard initialization
68 // ----------------------------------------------------------------
70 SalomeApp_Module::initialize( app );
71 _resourceMgr = app->resourceMgr();
72 // Please note that the moduleName() function is inherited from
73 // CAM_module classe, and that its value is determined automatically
74 // from the name specified in the SalomeApp.xml associated to this
75 // module and which corresponds to the name of the GUI library
76 // (lib<moduleName>.so). For XSALOME, see
77 // share/salome/resources/xsalome/SalomeApp.xml in the install
79 _defaultMenuId = this->createMenu( QCHARSTAR(moduleName()), -1, -1, 30 );
80 _defaultToolbarId = this->createTool ( QCHARSTAR(moduleName()) );
81 _actionId_internalCount = StandardApp_Module::ACTIONID_DEFAULT_INIT_VALUE;
84 // ----------------------------------------------------------------
85 // The following initializes the GUI widget and associated actions
86 // ----------------------------------------------------------------
87 // These functions may be redefined in specialized version of this class
88 this->createModuleWidgets();
89 this->createModuleActions();
92 QIcon StandardApp_Module::createIcon(const QString& iconName) {
93 // The icon file is supposed to be available througth the resource
94 // manager, i.e. in the folder containing the XSALOME resources (see
95 // specification in the module file SalomeApp.xml).
96 QPixmap aPixmap = _resourceMgr->loadPixmap( QCHARSTAR(moduleName()), iconName );
97 return QIcon( aPixmap );
100 int StandardApp_Module::newActionId() {
101 _actionId_internalCount++;
102 return _actionId_internalCount;
106 * This function creates an action and connects it to a button in the
107 * toolbar and to an item in the menu associated to this module. This
108 * function applies a common and standard procedure with a maximum of
109 * default values. If no identifier is specified, then a static
110 * internal counter is used to associate an identifier to the created
111 * action. In any case, the action identifier is returned.
113 * Note that the action (object of type QAction) can be retrieved
114 * using the method "QAction * action(int identifier)" of the super
117 int StandardApp_Module::createStandardAction(const QString& label,
118 QObject * slotobject,
119 const char* slotmember,
120 const QString& iconName,
121 const QString& tooltip,
122 const int identifier)
125 // If the tooltip is not defined, we choose instead the label text.
126 QString effToolTip(tooltip);
127 if ( effToolTip.isEmpty() )
130 QIcon actionIcon = this->createIcon(iconName);
132 // If the identifier is not specified, then we use an internal
134 int effIdentifier = identifier;
135 if ( effIdentifier == StandardApp_Module::ACTIONID_UNDEFINED ) {
136 effIdentifier=newActionId();
139 // Creating the action
140 QAction * action= this->createAction( effIdentifier, label, actionIcon,
141 label, effToolTip, 0, getApp()->desktop(),
142 false, slotobject, slotmember);
144 return effIdentifier;
148 * Integrate the action in the default toolbar
150 void StandardApp_Module::addActionInToolbar(int actionId) {
151 this->createTool( actionId, _defaultToolbarId );
155 * Integrate the action in the default menu
157 void StandardApp_Module::addActionInMenubar(int actionId) {
158 this->createMenu( actionId, _defaultMenuId, 10 );
162 * Add the specified action as an item in the popup menu, with the
163 * specified visible rule. The default is "visible for object browser".
165 void StandardApp_Module::addActionInPopupMenu(int actionId,const QString& rule) {
166 // _GBO_ for a fine customization of the rule (for example with a
167 // test on the type of the selected object), see the LIGTH module:
168 // implement "LightApp_Selection* createSelection() const;"
170 QtxPopupMgr* mgr = this->popupMgr();
171 mgr->insert ( this->action( actionId ), parentId, 0 );
172 mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
176 * This function can be used to create additionnal widget for this
177 * module GUI (e.g. docked widget). It can be redefined in a
178 * specialized version of this class.
180 void StandardApp_Module::createModuleWidgets() {
181 // Nothing to do in this default gui
185 * This function can be used to defined the list of actions for this
186 * module GUI. It can be redefined in a specialized version of this
188 * Depending on wether you use the method "createStandardAction" or
189 * not, the actions will be automatically plugged in the toolbar and
190 * the menu associated to the module.
192 void StandardApp_Module::createModuleActions() {
193 int actionId = this->createStandardAction("Test", this, SLOT(OnTest()),
194 "f1.png", "Run the default test function");
195 this->addActionInToolbar(actionId);
198 // Get compatible dockable windows.
199 void StandardApp_Module::windows( QMap<int, int>& theMap ) const
202 theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
203 theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
206 // Module's engine IOR
207 QString StandardApp_Module::engineIOR() const
209 CORBA::String_var anIOR = getApp()->orb()->object_to_string(getEngine());
210 return QString( anIOR.in() );
214 * This specifies the filename for the icon to be used for the study
215 * component associated to the module. Note that this icon could be
216 * different than the module icon which is defined by the
217 * SalomeApp.xml file dedicated to this module (see the shared
218 * resources folder dedicated to the module) and which is used for the
219 * toolbar of the SALOME application.
221 QString StandardApp_Module::studyIconName()
223 // By default, we return the module icone name
224 return iconName(); // inherited from CAM_Module
228 * This can be used to switch the layout of main application
229 * dockwidgets to one of the predefined configuration (see enum
230 * DockLayoutType). When a layout is set, the previous layout is
231 * memorized and can be restored using the unsetDockLayout function (1
232 * step undo). It is typically to be used in the functions
233 * activateModule to setup the layout and deactivateModule to unset
234 * the layout, i.e. restore to the previous state.
236 void StandardApp_Module::setDockLayout(DockLayoutType layoutType) {
237 SUIT_Desktop* desk = getApp()->desktop();
238 _areaAtBottomLeftCorner = desk->corner(Qt::BottomLeftCorner);
239 _areaAtBottomRightCorner = desk->corner(Qt::BottomRightCorner);
241 if ( layoutType == DOCKLAYOUT_LEFT_VLARGE ) {
242 desk->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
243 desk->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
245 desk->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
246 desk->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
251 * This function restores the layout state that was previously in
252 * place before the last setDockLayout call.
254 void StandardApp_Module::unsetDockLayout() {
255 SUIT_Desktop* desk = getApp()->desktop();
256 desk->setCorner(Qt::BottomLeftCorner, _areaAtBottomLeftCorner);
257 desk->setCorner(Qt::BottomRightCorner, _areaAtBottomRightCorner);
260 // Module's activation
261 bool StandardApp_Module::activateModule( SUIT_Study* theStudy )
263 bool bOk = SalomeApp_Module::activateModule( theStudy );
265 setMenuShown( true );
266 setToolShown( true );
268 if ( this->createStudyComponentAtActivation() ) {
269 this->createStudyComponent(theStudy);
276 * This function should be implemented in a specialized class and must
277 * return true if you want to automatically create a study component
278 * for this module at activation step (when you first load the module
279 * for a given study). The default function return true.
281 bool StandardApp_Module::createStudyComponentAtActivation() {
286 * This creates a root entry in the active study for this module, i.e
287 * a SComponent with the name of the module and the icon specified for
288 * the module. This component is associated to the engine (return by
289 * getEngine()) if the engine is a SALOMEDS::Driver.
291 void StandardApp_Module::createStudyComponent(SUIT_Study* theStudy) {
293 SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
294 CORBA::Object_var aSMObject = aNamingService->Resolve("/myStudyManager");
295 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
296 SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID(theStudy->id());
298 SALOMEDS::SComponent_var aFather = aDSStudy->FindComponent(QCHARSTAR(moduleName()));
299 if (aFather->_is_nil())
301 SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
302 aFather = aStudyBuilder->NewComponent(QCHARSTAR(moduleName()));
303 SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
304 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
305 aName->SetValue(QCHARSTAR(moduleName()));
307 anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
308 SALOMEDS::AttributePixMap_var aPixMap = SALOMEDS::AttributePixMap::_narrow(anAttr);
309 aPixMap->SetPixMap(QCHARSTAR(studyIconName()));
311 // WARN: The engine should be associated to the SComponent IF
312 // AND ONLY IF it is a SALOMEDS::Driver. Otherwise, there is no
313 // need to do that, and it could even lead to exception
314 // raising (eh, you work on SALOME isn't it?)
315 SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(this->getEngine());
316 if ( ! driver->_is_nil() ) {
317 STDLOG("Associate the SComponent to the engine");
318 aStudyBuilder->DefineComponentInstance(aFather, this->getEngine());
324 // Module's deactivation
325 bool StandardApp_Module::deactivateModule( SUIT_Study* theStudy )
327 setMenuShown( false );
328 setToolShown( false );
330 return SalomeApp_Module::deactivateModule( theStudy );
333 void StandardApp_Module::OnTest()
336 STDLOG("OnTest: engine IOR = "<<QCHARSTAR(engineIOR()));