Salome HOME
7d5770b6de890c25a4b72dcd82d6401e42c8e732
[modules/med.git] / src / MEDCalc / gui / MEDModule.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF 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, 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
20 // Author : Guillaume Boulant (EDF)
21
22 #include "MEDModule.hxx"
23 #include "QtHelper.hxx"
24
25 #include "SALOME_LifeCycleCORBA.hxx"
26 #include "QtxPopupMgr.h"
27
28 #include <LightApp_Preferences.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_Session.h>
32 #include <SalomeApp_Study.h>
33
34 #ifndef DISABLE_PVVIEWER
35 #include "PVViewer_ViewModel.h"
36 #endif
37
38 //! The only instance of the reference to engine
39 MED_ORB::MED_Gen_var MEDModule::myEngine;
40
41 MEDModule::MEDModule() :
42   SalomeApp_Module("MED"), _studyEditor(0), _datasourceController(0), _workspaceController(0), _presentationController(0)
43 {
44   // Note also that we can't use the getApp() function here because
45   // the initialize(...) function has not been called yet.
46
47   init(); // internal initialization
48 }
49
50 MEDModule::~MEDModule()
51 {
52   if (_studyEditor)
53     delete _studyEditor;
54   if (_datasourceController)
55     delete _datasourceController;
56   //if (_workspaceController)
57   //  delete _workspaceController;
58   if (_presentationController)
59     delete _presentationController;
60 }
61
62 MED_ORB::MED_Gen_var
63 MEDModule::engine()
64 {
65   init(); // initialize engine, if necessary
66   return myEngine;
67 }
68
69 void
70 MEDModule::init()
71 {
72   // initialize MED module engine (load, if necessary)
73   if ( CORBA::is_nil( myEngine ) ) {
74     Engines::EngineComponent_var comp =
75       SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "MED" );
76     myEngine = MED_ORB::MED_Gen::_narrow( comp );
77   }
78 }
79
80 void
81 MEDModule::initialize( CAM_Application* app )
82 {
83   // call the parent implementation
84   SalomeApp_Module::initialize( app );
85
86   // The following initializes the GUI widget and associated actions
87   this->createModuleWidgets();
88   this->createModuleActions();
89 }
90
91 QString
92 MEDModule::engineIOR() const
93 {
94   init(); // initialize engine, if necessary
95   CORBA::String_var anIOR = getApp()->orb()->object_to_string( myEngine.in() );
96   return QString( anIOR.in() );
97 }
98
99 QString
100 MEDModule::iconName() const
101 {
102   return tr("ICO_MED_SMALL");
103 }
104
105 void
106 MEDModule::windows( QMap<int, int>& theMap ) const
107 {
108   // want Object browser, in the left area
109   theMap.insert( SalomeApp_Application::WT_ObjectBrowser,
110                  Qt::LeftDockWidgetArea );
111 #ifndef DISABLE_PYCONSOLE
112   // want Python console, in the bottom area
113   theMap.insert( SalomeApp_Application::WT_PyConsole,
114                  Qt::BottomDockWidgetArea );
115 #endif
116 }
117
118 void
119 MEDModule::viewManagers( QStringList& list ) const
120 {
121 #ifndef DISABLE_PVVIEWER
122   list.append( PVViewer_Viewer::Type() );
123 #endif
124 }
125
126 void
127 MEDModule::createPreferences()
128 {
129   int genTab = addPreference(tr("PREF_TAB_GENERAL"));
130
131   int themeGroup = addPreference(tr("PREF_THEME_GROUP"), genTab);
132   setPreferenceProperty(themeGroup, "columns", 2);
133   int icons = addPreference(tr("PREF_ICONS"), themeGroup, LightApp_Preferences::Selector, "MEDCalc", "icons" );
134   QStringList iconsThemes;
135   iconsThemes.append(tr("PREF_ICON_THEME_MODERN"));
136   iconsThemes.append(tr("PREF_ICON_THEME_CLASSIC"));
137   QList<QVariant> indices;
138   indices.append(0);
139   indices.append(1);
140   setPreferenceProperty(icons, "strings", iconsThemes);
141   setPreferenceProperty(icons, "indexes", indices);
142 }
143
144 bool
145 MEDModule::activateModule( SUIT_Study* theStudy )
146 {
147   if ( CORBA::is_nil( myEngine ) )
148     return false;
149
150   // call parent implementation
151   bool bOk = SalomeApp_Module::activateModule( theStudy );
152   if (!bOk)
153     return false;
154
155   // show own menus
156   setMenuShown( true );
157   // show own toolbars
158   setToolShown( true );
159
160   //this->createStudyComponent(theStudy);
161   _workspaceController->showDockWidgets(true);
162   //this->setDockLayout(StandardApp_Module::DOCKLAYOUT_LEFT_VLARGE);
163
164   // return the activation status
165   return bOk;
166 }
167
168 bool
169 MEDModule::deactivateModule( SUIT_Study* theStudy )
170 {
171   _workspaceController->showDockWidgets(false);
172   //this->unsetDockLayout();
173
174   // hide own menus
175   setMenuShown( false );
176   // hide own toolbars
177   setToolShown( false );
178
179   // call parent implementation and return the activation status
180   return SalomeApp_Module::deactivateModule( theStudy );
181 }
182
183 //
184 // =====================================================================
185 // This part add custom widgets (a dockwidget containing a tree view
186 // in this example) and add special actions in the toolbox of the
187 // module.
188 // =====================================================================
189 //
190
191 /*!
192  * This function implements the interface StandardApp_Module. It
193  * creates the widgets specific for this module, in particular the
194  * workspace widget and the dataspace widget.
195  */
196 void
197 MEDModule::createModuleWidgets() {
198   _studyEditor = new SALOME_AppStudyEditor(getApp());
199   _datasourceController = new DatasourceController(this);
200   _workspaceController = new WorkspaceController(this);
201   _xmedDataModel  = new XmedDataModel();
202   _workspaceController->setDataModel(_xmedDataModel);
203   _presentationController = new PresentationController(this);
204
205   connect(_datasourceController, SIGNAL(datasourceSignal(const DatasourceEvent*)),
206     _workspaceController, SLOT(processDatasourceEvent(const DatasourceEvent*)));
207
208   connect(_presentationController, SIGNAL(presentationSignal(const PresentationEvent*)),
209     _workspaceController, SLOT(processPresentationEvent(const PresentationEvent*)));
210
211   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
212     _datasourceController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
213
214   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
215     _presentationController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
216 }
217
218 void
219 MEDModule::createModuleActions() {
220   _datasourceController->createActions();
221   _workspaceController->createActions();
222   _presentationController->createActions();
223 }
224
225 int
226 MEDModule::createStandardAction(const QString& label,
227                                 QObject* slotobject,
228                                 const char* slotmember,
229                                 const QString& iconName,
230                                 const QString& tooltip)
231 {
232   SUIT_Desktop* dsk = getApp()->desktop();
233   SUIT_ResourceMgr* resMgr = getApp()->resourceMgr();
234
235   // If the tooltip is not defined, we choose instead the label text.
236   QString effToolTip(tooltip);
237   if ( effToolTip.isEmpty() )
238     effToolTip = label;
239
240   QAction* action = createAction(-1,
241                                  label,
242                                  resMgr->loadPixmap("MED", iconName),
243                                  label,
244                                  effToolTip,
245                                  0,
246                                  dsk,
247                                  false,
248                                  slotobject,
249                                  slotmember
250                                  );
251   return actionId(action);
252 }
253
254 void
255 MEDModule::addActionInPopupMenu(int actionId,const QString& menus,const QString& rule)
256 {
257   // _GBO_ for a fine customization of the rule (for example with a
258   // test on the type of the selected object), see the LIGTH module:
259   // implement "LightApp_Selection*    createSelection() const;"
260   int parentId = -1;
261   QtxPopupMgr* mgr = this->popupMgr();
262   this->action( actionId )->setIconVisibleInMenu(true);
263   if (! menus.isEmpty())
264     mgr->insert ( this->action( actionId ), menus, parentId, 0 );
265   else
266     mgr->insert ( this->action( actionId ), parentId, 0 );
267   mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
268 }
269
270 MEDCALC::MEDPresentationViewMode
271 MEDModule::getSelectedViewMode()
272 {
273   return _presentationController->getSelectedViewMode();
274 }