Salome HOME
Exit procedure added in tests. Not killing all servers yet.
[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 #include <MEDCalcConstants.hxx>
25
26 #include "SALOME_LifeCycleCORBA.hxx"
27 #include "QtxPopupMgr.h"
28
29 #include <LightApp_Preferences.h>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_DataBrowser.h>
34 #include <SalomeApp_Study.h>
35 #include <SalomeApp_DataObject.h>
36 #include <SalomeApp_DataModel.h>
37
38 #include <SALOMEconfig.h>
39 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
40 #include <SALOMEDS_SObject.hxx>
41 #include <SALOMEDS_Study.hxx>
42
43 #ifndef DISABLE_PVVIEWER
44 #include "PVViewer_ViewModel.h"
45 #endif
46
47 #include <sstream>
48
49 //! The only instance of the reference to engine
50 MED_ORB::MED_Gen_var MEDModule::myEngine;
51
52 MEDModule::MEDModule() :
53   SalomeApp_Module("MED"), _studyEditor(0), _datasourceController(0), _workspaceController(0), _presentationController(0), _processingController(0)
54 {
55   // Note also that we can't use the getApp() function here because
56   // the initialize(...) function has not been called yet.
57
58   init(); // internal initialization
59 }
60
61 MEDModule::~MEDModule()
62 {
63   if (_studyEditor)
64     delete _studyEditor;
65   if (_datasourceController)
66     delete _datasourceController;
67   //if (_workspaceController)
68   //  delete _workspaceController;
69   if (_presentationController)
70     delete _presentationController;
71   if (_processingController)
72     delete _processingController;
73 }
74
75 MED_ORB::MED_Gen_var
76 MEDModule::engine()
77 {
78   init(); // initialize engine, if necessary
79   return myEngine;
80 }
81
82 void
83 MEDModule::init()
84 {
85   // initialize MED module engine (load, if necessary)
86   if ( CORBA::is_nil( myEngine ) ) {
87     Engines::EngineComponent_var comp =
88       SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "MED" );
89     myEngine = MED_ORB::MED_Gen::_narrow( comp );
90   }
91 }
92
93 void
94 MEDModule::initialize( CAM_Application* app )
95 {
96   // call the parent implementation
97   SalomeApp_Module::initialize( app );
98
99   if (app && app->desktop()) {
100     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(doubleClicked(const QModelIndex&)),
101             this, SLOT(onDblClick(const QModelIndex&)));
102   }
103
104   // The following initializes the GUI widget and associated actions
105   this->createModuleWidgets();
106   this->createModuleActions();
107 }
108
109 QString
110 MEDModule::engineIOR() const
111 {
112   init(); // initialize engine, if necessary
113   CORBA::String_var anIOR = getApp()->orb()->object_to_string( myEngine.in() );
114   return QString( anIOR.in() );
115 }
116
117 QString
118 MEDModule::iconName() const
119 {
120   return tr("ICO_MED_SMALL");
121 }
122
123 void
124 MEDModule::windows( QMap<int, int>& theMap ) const
125 {
126   // want Object browser, in the left area
127   theMap.insert( SalomeApp_Application::WT_ObjectBrowser,
128                  Qt::LeftDockWidgetArea );
129 #ifndef DISABLE_PYCONSOLE
130   // want Python console, in the bottom area
131   theMap.insert( SalomeApp_Application::WT_PyConsole,
132                  Qt::BottomDockWidgetArea );
133 #endif
134 }
135
136 void
137 MEDModule::viewManagers( QStringList& list ) const
138 {
139 #ifndef DISABLE_PVVIEWER
140   list.append( PVViewer_Viewer::Type() );
141 #endif
142 }
143
144 void
145 MEDModule::createPreferences()
146 {
147   int genTab = addPreference(tr("PREF_TAB_GENERAL"));
148
149   int themeGroup = addPreference(tr("PREF_THEME_GROUP"), genTab);
150   setPreferenceProperty(themeGroup, "columns", 2);
151   int icons = addPreference(tr("PREF_ICONS"), themeGroup, LightApp_Preferences::Selector, "MEDCalc", "icons" );
152   QStringList iconsThemes;
153   iconsThemes.append(tr("PREF_ICON_THEME_MODERN"));
154   iconsThemes.append(tr("PREF_ICON_THEME_CLASSIC"));
155   QList<QVariant> indices;
156   indices.append(0);
157   indices.append(1);
158   setPreferenceProperty(icons, "strings", iconsThemes);
159   setPreferenceProperty(icons, "indexes", indices);
160 }
161
162 bool
163 MEDModule::activateModule( SUIT_Study* theStudy )
164 {
165   if ( CORBA::is_nil( myEngine ) )
166     return false;
167
168   // call parent implementation
169   bool bOk = SalomeApp_Module::activateModule( theStudy );
170   if (!bOk)
171     return false;
172
173   // show own menus
174   setMenuShown( true );
175   // show own toolbars
176   setToolShown( true );
177
178   //this->createStudyComponent(theStudy);
179   _workspaceController->showDockWidgets(true);
180   _presentationController->showDockWidgets(true);
181   //this->setDockLayout(StandardApp_Module::DOCKLAYOUT_LEFT_VLARGE);
182
183   // return the activation status
184   return bOk;
185 }
186
187 bool
188 MEDModule::deactivateModule( SUIT_Study* theStudy )
189 {
190   _workspaceController->showDockWidgets(false);
191   _presentationController->showDockWidgets(false);
192   //this->unsetDockLayout();
193
194   // hide own menus
195   setMenuShown( false );
196   // hide own toolbars
197   setToolShown( false );
198
199   // call parent implementation and return the activation status
200   return SalomeApp_Module::deactivateModule( theStudy );
201 }
202
203 //
204 // =====================================================================
205 // This part add custom widgets (a dockwidget containing a tree view
206 // in this example) and add special actions in the toolbox of the
207 // module.
208 // =====================================================================
209 //
210
211 /*!
212  * This function implements the interface StandardApp_Module. It
213  * creates the widgets specific for this module, in particular the
214  * workspace widget and the dataspace widget.
215  */
216 void
217 MEDModule::createModuleWidgets() {
218   _studyEditor = new SALOME_AppStudyEditor(getApp());
219   _datasourceController = new DatasourceController(this);
220   _workspaceController = new WorkspaceController(this);
221   _xmedDataModel  = new XmedDataModel();
222   _workspaceController->setDataModel(_xmedDataModel);
223   _presentationController = new PresentationController(this);
224   _processingController = new ProcessingController(this);
225 #ifdef MED_HAS_QTTESTING
226   _testController = new TestController(this);
227 #endif
228
229   connect(_datasourceController, SIGNAL(datasourceSignal(const DatasourceEvent*)),
230     _workspaceController, SLOT(processDatasourceEvent(const DatasourceEvent*)));
231
232   connect(_presentationController, SIGNAL(presentationSignal(const PresentationEvent*)),
233     _workspaceController, SLOT(processPresentationEvent(const PresentationEvent*)));
234
235   connect(_processingController, SIGNAL(processingSignal(const ProcessingEvent*)),
236     _workspaceController, SLOT(processProcessingEvent(const ProcessingEvent*)));
237
238   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
239     _datasourceController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
240
241   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
242     _presentationController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
243
244 #ifdef MED_HAS_QTTESTING
245   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
246     _testController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
247 #endif
248 }
249
250 void
251 MEDModule::createModuleActions() {
252   _datasourceController->createActions();
253   _workspaceController->createActions();
254   _presentationController->createActions();
255   _processingController->createActions();
256 #ifdef MED_HAS_QTTESTING
257   _testController->createActions();
258 #endif
259 }
260
261 int
262 MEDModule::createStandardAction(const QString& label,
263                                 QObject* slotobject,
264                                 const char* slotmember,
265                                 const QString& iconName,
266                                 const QString& tooltip)
267 {
268   SUIT_Desktop* dsk = getApp()->desktop();
269   SUIT_ResourceMgr* resMgr = getApp()->resourceMgr();
270
271   // If the tooltip is not defined, we choose instead the label text.
272   QString effToolTip(tooltip);
273   if ( effToolTip.isEmpty() )
274     effToolTip = label;
275
276   QIcon ico;
277   if (iconName.isEmpty())
278     ico = QIcon();
279   else
280     ico = QIcon(resMgr->loadPixmap("MED", iconName));
281
282   QAction* action = createAction(-1,
283                                  label,
284                                  ico,
285                                  label,
286                                  effToolTip,
287                                  0,
288                                  dsk,
289                                  false,
290                                  slotobject,
291                                  slotmember
292                                  );
293   return actionId(action);
294 }
295
296 void
297 MEDModule::addActionInPopupMenu(int actionId,const QString& menus,const QString& rule)
298 {
299   // _GBO_ for a fine customization of the rule (for example with a
300   // test on the type of the selected object), see the LIGTH module:
301   // implement "LightApp_Selection*    createSelection() const;"
302   int parentId = -1;
303   QtxPopupMgr* mgr = this->popupMgr();
304   this->action( actionId )->setIconVisibleInMenu(true);
305   if (! menus.isEmpty())
306     mgr->insert ( this->action( actionId ), menus, parentId, 0 );
307   else
308     mgr->insert ( this->action( actionId ), parentId, 0 );
309   mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
310 }
311
312 MEDCALC::MEDPresentationViewMode
313 MEDModule::getSelectedViewMode()
314 {
315   return _presentationController->getSelectedViewMode();
316 }
317
318 MEDCALC::MEDPresentationColorMap
319 MEDModule::getSelectedColorMap()
320 {
321   return _presentationController->getSelectedColorMap();
322 }
323
324 void
325 MEDModule::onDblClick(const QModelIndex& index)
326 {
327   DataObjectList dol = getApp()->objectBrowser()->getSelected();
328   if (dol.isEmpty())
329     return;
330   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
331   if (!item)
332     return;
333   SalomeApp_DataModel *model = dynamic_cast<SalomeApp_DataModel*>(dataModel());
334   if (!model)
335     return;
336
337   if (item->componentDataType().toStdString() != "MED")
338     return;
339   _PTR(SObject) obj = item->object();
340   _PTR(GenericAttribute) anAttribute;
341
342   if (! obj->FindAttribute(anAttribute, "AttributeName"))
343     return;
344   _PTR(AttributeName) attrName(anAttribute);
345   std::string name = attrName->Value();
346
347   if (! obj->FindAttribute(anAttribute, "AttributeParameter"))
348     return;
349   _PTR(AttributeParameter) attrParam(anAttribute);
350   if (! attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN)
351       || ! attrParam->GetBool(IS_PRESENTATION)) { // Not a presentation
352     return;
353   }
354   if (!attrParam->IsSet(FIELD_ID, PT_INTEGER))
355     return;
356   int fieldId = attrParam->GetInt(FIELD_ID);
357
358   STDLOG("Presentation edition: NOT IMPLEMENTED YET");
359   STDLOG("  Presention infos:");
360   STDLOG("    - Component:         " + item->componentDataType().toStdString());
361   STDLOG("    - Item entry:        " + item->entry().toStdString());
362   STDLOG("    - Item name:         " + item->name().toStdString());
363   std::ostringstream oss;
364   oss << fieldId;
365   STDLOG("    - Field id:          " + oss.str());
366   STDLOG("    - Presentation name: " + name);
367
368 }
369
370 void
371 MEDModule::requestSALOMETermination() const
372 {
373   SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE, 1 );  // killServers = True
374 }