Salome HOME
refs #1458: disable chained panning on operations
[modules/gui.git] / src / GuiHelpers / StandardApp_Module.cxx
1 // Copyright (C) 2011-2016  CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Guillaume Boulant (EDF)
20
21 #include "StandardApp_Module.hxx"
22
23 #include <SUIT_Desktop.h>
24 #include <SUIT_Study.h>
25 #include <SalomeApp_Application.h>
26 #include <SALOME_LifeCycleCORBA.hxx>
27 #include "QtxPopupMgr.h"
28
29 #include CORBA_CLIENT_HEADER(SALOMEDS)
30 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
31
32 // QT Includes
33 #include <QIcon>
34 #include <QAction>
35
36 #include <Basics_Utils.hxx>
37 #include "QtHelper.hxx"
38
39 // Constructor
40 StandardApp_Module::StandardApp_Module() :
41   SalomeApp_Module( "" )
42 {
43   // Note that it's no use to specify here the moduleName because it's
44   // automatically determined by the name of the GUI library
45   // (lib<moduleName>.so) just before initialize step. If you want
46   // nevertheless to setup the module name you should specified this
47   // name in the initialize function below using the CAM_module
48   // function setModuleName(QString&). You are warn then that the
49   // loading of resource files may be incorect and should be manually
50   // managed. In conclusion, if you don't need a special
51   // configuration, please conform to the standard convention: if
52   // <moduleName> is the name of the module as declared in the GUI
53   // configuration file (SalomeApp.xml), then you just have to provide
54   // a library whose name is lib<moduleName>.so and which contains a
55   // factory "extern C" function 'CAM_Module* createModule()'
56 }
57
58 int StandardApp_Module::ACTIONID_DEFAULT_INIT_VALUE = 910;
59 int StandardApp_Module::ACTIONID_UNDEFINED = -1;
60
61 // Module's initialization
62 void StandardApp_Module::initialize( CAM_Application* app )
63 {
64   //
65   // ----------------------------------------------------------------
66   // Standard initialization
67   // ----------------------------------------------------------------
68   //
69   SalomeApp_Module::initialize( app );
70   _resourceMgr = app->resourceMgr();
71   // Please note that the moduleName() function is inherited from
72   // CAM_module classe, and that its value is determined automatically
73   // from the name specified in the SalomeApp.xml associated to this
74   // module and which corresponds to the name of the GUI library
75   // (lib<moduleName>.so). For XSALOME, see
76   // share/salome/resources/xsalome/SalomeApp.xml in the install
77   // directory.
78   _defaultMenuId = this->createMenu( QCHARSTAR(moduleName()), -1, -1, 30 );
79   _defaultToolbarId = this->createTool ( QCHARSTAR(moduleName()), moduleName() );
80   _actionId_internalCount = StandardApp_Module::ACTIONID_DEFAULT_INIT_VALUE;
81
82   //
83   // ----------------------------------------------------------------
84   // The following initializes the GUI widget and associated actions
85   // ----------------------------------------------------------------
86   // These functions may be redefined in specialized version of this class
87   this->createModuleWidgets();
88   this->createModuleActions();
89 }
90
91 QIcon StandardApp_Module::createIcon(const QString& iconName) {
92   // The icon file is supposed to be available througth the resource
93   // manager, i.e. in the folder containing the XSALOME resources (see
94   // specification in the module file SalomeApp.xml).
95   QPixmap aPixmap = _resourceMgr->loadPixmap( QCHARSTAR(moduleName()), iconName );
96   return QIcon( aPixmap );
97 }
98
99 int StandardApp_Module::newActionId() {
100   _actionId_internalCount++;
101   return _actionId_internalCount;
102 }
103
104 /*!
105  * This function creates an action and connects it to a button in the
106  * toolbar and to an item in the menu associated to this module.  This
107  * function applies a common and standard procedure with a maximum of
108  * default values. If no identifier is specified, then a static
109  * internal counter is used to associate an identifier to the created
110  * action. In any case, the action identifier is returned.
111  *
112  * Note that the action (object of type QAction) can be retrieved
113  * using the method "QAction * action(int identifier)" of the super
114  * class.
115  */
116 int StandardApp_Module::createStandardAction(const QString& label,
117                                              QObject * slotobject,
118                                              const char* slotmember,
119                                              const QString& iconName,
120                                              const QString& tooltip,
121                                              const int identifier)
122 {
123
124   // If the tooltip is not defined, we choose instead the label text.
125   QString effToolTip(tooltip);
126   if ( effToolTip.isEmpty() )
127     effToolTip = label;
128
129   QIcon actionIcon = this->createIcon(iconName);
130
131   // If the identifier is not specified, then we use an internal
132   // counter.
133   int effIdentifier = identifier;
134   if ( effIdentifier == StandardApp_Module::ACTIONID_UNDEFINED ) {
135     effIdentifier=newActionId();
136   }
137
138   // Creating the action
139   QAction * action= this->createAction( effIdentifier, label, actionIcon,
140                                         label, effToolTip, 0, getApp()->desktop(),
141                                         false, slotobject, slotmember);
142
143   return effIdentifier;
144 }
145
146 /**
147  * Integrate the action in the default toolbar
148  */
149 void StandardApp_Module::addActionInToolbar(int actionId) {
150   this->createTool( actionId, _defaultToolbarId );
151 }
152
153 /**
154  * Integrate action in the specified menu. If the menu is not
155  * specified, then it is integrated into the menu associated to the
156  * SALOME module.
157  */
158 void StandardApp_Module::addActionInMenubar(int actionId, int menuId) {
159   if ( menuId == UNSPECIFIED_INT) {
160     menuId = _defaultMenuId;
161   }
162   this->action( actionId )->setIconVisibleInMenu(true);
163   this->createMenu( actionId, menuId, 10 );
164 }
165
166
167
168 /**
169  * Add the specified action as an item in the popup menu, with the
170  * specified visible rule. The default is "visible for object browser".
171  */
172 void StandardApp_Module::addActionInPopupMenu(int actionId,const QString& menus,const QString& rule) {
173   // _GBO_ for a fine customization of the rule (for example with a
174   // test on the type of the selected object), see the LIGTH module:
175   // implement "LightApp_Selection*    createSelection() const;"
176   int parentId = -1;
177   QtxPopupMgr* mgr = this->popupMgr();
178   this->action( actionId )->setIconVisibleInMenu(true);
179   if (! menus.isEmpty())
180     mgr->insert ( this->action( actionId ), menus, parentId, 0 );
181   else
182     mgr->insert ( this->action( actionId ), parentId, 0 );
183   mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
184 }
185
186 /*!
187  * This function can be used to create additionnal widget for this
188  * module GUI (e.g. docked widget). It can be redefined in a
189  * specialized version of this class.
190  */
191 void StandardApp_Module::createModuleWidgets() {
192   // Nothing to do in this default gui
193 }
194
195 /*!
196  * This function can be used to defined the list of actions for this
197  * module GUI.  It can be redefined in a specialized version of this
198  * class.
199  * Depending on wether you use the method "createStandardAction" or
200  * not, the actions will be automatically plugged in the toolbar and
201  * the menu associated to the module.
202  */
203 void StandardApp_Module::createModuleActions() {
204   int actionId = this->createStandardAction("Test", this, SLOT(OnTest()),
205                                             "f1.png", "Run the default test function");
206   this->addActionInToolbar(actionId);
207 }
208
209 // Get compatible dockable windows.
210 void StandardApp_Module::windows( QMap<int, int>& theMap ) const
211 {
212   theMap.clear();
213   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
214 #ifndef DISABLE_PYCONSOLE
215   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
216 #endif
217 }
218
219 // Module's engine IOR
220 QString StandardApp_Module::engineIOR() const
221 {
222   CORBA::String_var anIOR = getApp()->orb()->object_to_string(getEngine());
223   return QString( anIOR.in() );
224 }
225
226 /*!
227  * This specifies the filename for the icon to be used for the study
228  * component associated to the module. Note that this icon could be
229  * different than the module icon which is defined by the
230  * SalomeApp.xml file dedicated to this module (see the shared
231  * resources folder dedicated to the module) and which is used for the
232  * toolbar of the SALOME application.
233  */
234 QString StandardApp_Module::studyIconName()
235 {
236   // By default, we return the module icone name
237   return iconName(); // inherited from CAM_Module
238 }
239
240 /*!
241  * This can be used to switch the layout of main application
242  * dockwidgets to one of the predefined configuration (see enum
243  * DockLayoutType). When a layout is set, the previous layout is
244  * memorized and can be restored using the unsetDockLayout function (1
245  * step undo). It is typically to be used in the functions
246  * activateModule to setup the layout and deactivateModule to unset
247  * the layout, i.e. restore to the previous state.
248  */
249 void StandardApp_Module::setDockLayout(DockLayoutType layoutType) {
250   SUIT_Desktop* desk = getApp()->desktop();
251   _areaAtBottomLeftCorner = desk->corner(Qt::BottomLeftCorner);
252   _areaAtBottomRightCorner = desk->corner(Qt::BottomRightCorner);
253
254   if ( layoutType == DOCKLAYOUT_LEFT_VLARGE ) {
255     desk->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
256     desk->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
257   } else {
258     desk->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
259     desk->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
260   }
261 }
262
263 /*!
264  * This function restores the layout state that was previously in
265  * place before the last setDockLayout call.
266  */
267 void StandardApp_Module::unsetDockLayout() {
268   SUIT_Desktop* desk = getApp()->desktop();
269   desk->setCorner(Qt::BottomLeftCorner, _areaAtBottomLeftCorner);
270   desk->setCorner(Qt::BottomRightCorner, _areaAtBottomRightCorner);
271 }
272
273 // Module's activation
274 bool StandardApp_Module::activateModule( SUIT_Study* theStudy )
275 {
276   bool bOk = SalomeApp_Module::activateModule( theStudy );
277
278   setMenuShown( true );
279   setToolShown( true );
280
281   if ( this->createStudyComponentAtActivation() ) {
282     this->createStudyComponent(theStudy);
283   }
284
285   return bOk;
286 }
287
288 /*!
289  * This function should be implemented in a specialized class and must
290  * return true if you want to automatically create a study component
291  * for this module at activation step (when you first load the module
292  * for a given study). The default function return true.
293  */
294 bool StandardApp_Module::createStudyComponentAtActivation() {
295   return true;
296 }
297
298 /*!
299  * This creates a root entry in the active study for this module, i.e
300  * a SComponent with the name of the module and the icon specified for
301  * the module. This component is associated to the engine (return by
302  * getEngine()) if the engine is a SALOMEDS::Driver.
303  */
304 void StandardApp_Module::createStudyComponent(SUIT_Study* theStudy) {
305
306   SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
307   CORBA::Object_var aSMObject = aNamingService->Resolve("/myStudyManager");
308   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
309   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID(theStudy->id());
310
311   SALOMEDS::SComponent_var aFather = aDSStudy->FindComponent(QCHARSTAR(moduleName()));
312   if (aFather->_is_nil())
313     {
314       SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
315       aFather = aStudyBuilder->NewComponent(QCHARSTAR(moduleName()));
316       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
317       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
318       aName->SetValue(QCHARSTAR(moduleName()));
319       aName->Destroy();
320       anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
321       SALOMEDS::AttributePixMap_var aPixMap = SALOMEDS::AttributePixMap::_narrow(anAttr);
322       aPixMap->SetPixMap(QCHARSTAR(studyIconName()));
323
324       // WARN: The engine should be associated to the SComponent IF
325       // AND ONLY IF it is a SALOMEDS::Driver. Otherwise, there is no
326       // need to do that, and it could even lead to exception
327       // raising (eh, you work on SALOME isn't it?)
328       SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(this->getEngine());
329       if ( ! driver->_is_nil() ) {
330         STDLOG("Associate the SComponent to the engine");
331         aStudyBuilder->DefineComponentInstance(aFather, this->getEngine());
332       }
333     }
334
335 }
336
337 // Module's deactivation
338 bool StandardApp_Module::deactivateModule( SUIT_Study* theStudy )
339 {
340   setMenuShown( false );
341   setToolShown( false );
342
343   return SalomeApp_Module::deactivateModule( theStudy );
344 }
345
346 void StandardApp_Module::OnTest()
347 {
348   // Just for test
349   STDLOG("OnTest: engine IOR = "<<QCHARSTAR(engineIOR()));
350 }
351