Salome HOME
Mantis 23461 - MED module exception
[modules/med.git] / src / MEDCalc / gui / MEDModule.cxx
1 // Copyright (C) 2007-2017  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 #include <PVViewer_GUIElements.h>
46 #endif
47
48 #include "MEDFactoryClient.hxx"
49 #include "MEDPresentationManager_i.hxx"
50
51 #include <QTimer>
52 #include <sstream>
53
54 #include <pqAnimationManager.h>
55 #include <pqPVApplicationCore.h>
56
57 //! The only instance of the reference to engine
58 MED_ORB::MED_Gen_var MEDModule::_MED_engine;
59
60 MEDModule::MEDModule() :
61   SalomeApp_Module("MED"), _studyEditor(0),
62   _datasourceController(0), _workspaceController(0), _presentationController(0),
63   _processingController(0), _pvGuiElements(0)
64 {
65   // Note also that we can't use the getApp() function here because
66   // the initialize(...) function has not been called yet.
67
68   init(); // internal initialization
69 }
70
71 MEDModule::~MEDModule()
72 {
73   // Clean up engine:
74   STDLOG("MEDModule::~MEDModule(): cleaning up engine side.");
75   _MED_engine->cleanUp();
76   MEDFactoryClient::getFactory()->getPresentationManager()->cleanUp();
77   MEDFactoryClient::getFactory()->getDataManager()->cleanUp();
78
79   if (_studyEditor)
80     delete _studyEditor;
81   if (_datasourceController)
82     delete _datasourceController;
83   //if (_workspaceController)
84   //  delete _workspaceController;
85   if (_presentationController)
86     delete _presentationController;
87   if (_processingController)
88     delete _processingController;
89 }
90
91 MED_ORB::MED_Gen_var
92 MEDModule::engine()
93 {
94   init(); // initialize engine, if necessary
95   return _MED_engine;
96 }
97
98 void
99 MEDModule::init()
100 {
101   // initialize MED module engine (load, if necessary)
102   if ( CORBA::is_nil( _MED_engine ) ) {
103     Engines::EngineComponent_var comp =
104       SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "MED" );
105     if (CORBA::is_nil( comp ))
106         STDLOG("Could not FindOrLoad_Component MED");
107     _MED_engine = MED_ORB::MED_Gen::_narrow( comp );
108     if (CORBA::is_nil( _MED_engine ))
109         STDLOG("Could not narrow MED engine");
110   }
111 }
112
113 //void MEDModule::onEventLoopStarted()
114 //{
115 //  if(!getApp()->isMainEventLoopStarted())
116 //    {
117 //      QTimer::singleShot(100, this, SLOT(onEventLoopStarted()));
118 //      return;
119 //    }
120 //}
121
122 void
123 MEDModule::initialize( CAM_Application* app )
124 {
125   // call the parent implementation
126   SalomeApp_Module::initialize( app );
127
128   getApp()->objectBrowser()->setAutoOpenLevel(5);
129
130   if (app && app->desktop()) {
131     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(doubleClicked(const QModelIndex&)),
132             this, SLOT(onDblClick(const QModelIndex&)));
133     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(clicked(const QModelIndex&)),
134                 this, SLOT(onClick(const QModelIndex&)));
135   }
136
137   // The following initializes the GUI widget and associated actions
138   this->createModuleWidgets();
139   this->createModuleActions();
140
141   // Now that the workspace controller is created, ParaView core application has normally been started,
142   // and hidden GUI elements have been created.  We can fire the VCR toolbar activation:
143   initToolbars();
144 }
145
146 QString
147 MEDModule::engineIOR() const
148 {
149   init(); // initialize engine, if necessary
150   CORBA::String_var anIOR = getApp()->orb()->object_to_string( _MED_engine.in() );
151   return QString( anIOR.in() );
152 }
153
154 QString
155 MEDModule::iconName() const
156 {
157   return tr("ICO_MED_SMALL");
158 }
159
160 void
161 MEDModule::windows( QMap<int, int>& theMap ) const
162 {
163   // want Object browser, in the left area
164   theMap.insert( SalomeApp_Application::WT_ObjectBrowser,
165                  Qt::LeftDockWidgetArea );
166 #ifndef DISABLE_PYCONSOLE
167   // want Python console, in the bottom area
168   theMap.insert( SalomeApp_Application::WT_PyConsole,
169                  Qt::BottomDockWidgetArea );
170 #endif
171 }
172
173 void
174 MEDModule::viewManagers( QStringList& list ) const
175 {
176 #ifndef DISABLE_PVVIEWER
177   list.append( PVViewer_Viewer::Type() );
178 #endif
179 }
180
181 void
182 MEDModule::createPreferences()
183 {
184   int genTab = addPreference(tr("PREF_TAB_GENERAL"));
185
186   int themeGroup = addPreference(tr("PREF_THEME_GROUP"), genTab);
187   setPreferenceProperty(themeGroup, "columns", 2);
188   int icons = addPreference(tr("PREF_ICONS"), themeGroup, LightApp_Preferences::Selector, "MEDCalc", "icons" );
189   QStringList iconsThemes;
190   iconsThemes.append(tr("PREF_ICON_THEME_MODERN"));
191   iconsThemes.append(tr("PREF_ICON_THEME_CLASSIC"));
192   QList<QVariant> indices;
193   indices.append(0);
194   indices.append(1);
195   setPreferenceProperty(icons, "strings", iconsThemes);
196   setPreferenceProperty(icons, "indexes", indices);
197 }
198
199 bool
200 MEDModule::activateModule( SUIT_Study* theStudy )
201 {
202   if ( CORBA::is_nil( _MED_engine ) )
203     return false;
204
205   // call parent implementation
206   bool bOk = SalomeApp_Module::activateModule( theStudy );
207   if (!bOk)
208     return false;
209
210   // show own menus
211   setMenuShown( true );
212   // show own toolbars
213   setToolShown( true );
214
215   //this->createStudyComponent(theStudy);
216   _workspaceController->showDockWidgets(true);
217   _presentationController->showDockWidgets(true);
218   //this->setDockLayout(StandardApp_Module::DOCKLAYOUT_LEFT_VLARGE);
219
220   // Mark the start of the main event loop - important for test playback:
221 //  QObject::connect(getApp(), SIGNAL(activated(SUIT_Application *)), this, SLOT(onEventLoopStarted(SUIT_Application *)));
222 //  QTimer::singleShot(0, this, SLOT(onEventLoopStarted()));
223
224   // return the activation status
225   return bOk;
226 }
227
228 bool
229 MEDModule::deactivateModule( SUIT_Study* theStudy )
230 {
231   _workspaceController->showDockWidgets(false);
232   _presentationController->showDockWidgets(false);
233   //this->unsetDockLayout();
234
235   // hide own menus
236   setMenuShown( false );
237   // hide own toolbars
238   setToolShown( false );
239
240   // call parent implementation and return the activation status
241   return SalomeApp_Module::deactivateModule( theStudy );
242 }
243
244 //
245 // =====================================================================
246 // This part add custom widgets (a dockwidget containing a tree view
247 // in this example) and add special actions in the toolbox of the
248 // module.
249 // =====================================================================
250 //
251
252 /*!
253  * This function implements the interface StandardApp_Module. It
254  * creates the widgets specific for this module, in particular the
255  * workspace widget and the dataspace widget.
256  */
257 void
258 MEDModule::createModuleWidgets() {
259   _studyEditor = new SALOME_AppStudyEditor(getApp());
260   _datasourceController = new DatasourceController(this);
261   _workspaceController = new WorkspaceController(this);
262   _xmedDataModel  = new XmedDataModel();
263   _workspaceController->setDataModel(_xmedDataModel);
264   _presentationController = new PresentationController(this);
265   // ABN: ultimately console driver should be owned by module: everyone needs it, not only WorkspaceController
266   _presentationController->setConsoleDriver(_workspaceController->getConsoleDriver());
267   _processingController = new ProcessingController(this);
268 #ifdef MED_HAS_QTTESTING
269   _testController = new TestController(this);
270 #endif
271
272   connect(_datasourceController, SIGNAL(datasourceSignal(const DatasourceEvent*)),
273     _workspaceController, SLOT(processDatasourceEvent(const DatasourceEvent*)));
274
275   connect(_presentationController, SIGNAL(presentationSignal(const PresentationEvent*)),
276     _presentationController, SLOT(processPresentationEvent(const PresentationEvent*)));
277
278   connect(_processingController, SIGNAL(processingSignal(const ProcessingEvent*)),
279     _workspaceController, SLOT(processProcessingEvent(const ProcessingEvent*)));
280
281   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
282     _datasourceController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
283
284   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
285     _presentationController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
286 }
287
288 void
289 MEDModule::initToolbars()
290 {
291   // VCR and Time toolbars:
292   SUIT_Desktop* dsk = getApp()->desktop();
293   _pvGuiElements = PVViewer_GUIElements::GetInstance(dsk);
294
295   _pvGuiElements->getVCRToolbar();  // make sure VCR toolbar is built
296   _pvGuiElements->setToolBarVisible(false);
297   _pvGuiElements->setVCRTimeToolBarVisible(true);
298
299   // Emit signal in order to make sure that animation scene is set - same trick as in PARAVIS module activation
300   QMetaObject::invokeMethod( pqPVApplicationCore::instance()->animationManager(),
301                              "activeSceneChanged",
302                              Q_ARG( pqAnimationScene*, pqPVApplicationCore::instance()->animationManager()->getActiveScene() ) );
303
304 #ifdef MED_HAS_QTTESTING
305   connect(_workspaceController, SIGNAL(workspaceSignal(const MEDCALC::MedEvent*)),
306     _testController, SLOT(processWorkspaceEvent(const MEDCALC::MedEvent*)));
307 #endif
308 }
309
310 void
311 MEDModule::createModuleActions() {
312   _datasourceController->createActions();
313   _workspaceController->createActions();
314   _presentationController->createActions();
315   _processingController->createActions();
316 #ifdef MED_HAS_QTTESTING
317   _testController->createActions();
318 #endif
319 }
320
321 int
322 MEDModule::createStandardAction(const QString& label,
323                                 QObject* slotobject,
324                                 const char* slotmember,
325                                 const QString& iconName,
326                                 const QString& tooltip)
327 {
328   SUIT_Desktop* dsk = getApp()->desktop();
329   SUIT_ResourceMgr* resMgr = getApp()->resourceMgr();
330
331   // If the tooltip is not defined, we choose instead the label text.
332   QString effToolTip(tooltip);
333   if ( effToolTip.isEmpty() )
334     effToolTip = label;
335
336   QIcon ico;
337   if (iconName.isEmpty())
338     ico = QIcon();
339   else
340     ico = QIcon(resMgr->loadPixmap("MED", iconName));
341
342   QAction* action = createAction(-1,
343                                  label,
344                                  ico,
345                                  label,
346                                  effToolTip,
347                                  0,
348                                  dsk,
349                                  false,
350                                  slotobject,
351                                  slotmember
352                                  );
353   return actionId(action);
354 }
355
356 void
357 MEDModule::addActionInPopupMenu(int actionId,const QString& menus,const QString& rule)
358 {
359   // _GBO_ for a fine customization of the rule (for example with a
360   // test on the type of the selected object), see the LIGHT module:
361   // implement "LightApp_Selection*    createSelection() const;"
362   int parentId = -1;
363   QtxPopupMgr* mgr = this->popupMgr();
364   this->action( actionId )->setIconVisibleInMenu(true);
365   if (! menus.isEmpty())
366     mgr->insert ( this->action( actionId ), menus, parentId, 0 );
367   else
368     mgr->insert ( this->action( actionId ), parentId, 0 );
369   mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
370 }
371
372 //MEDCALC::ViewModeType
373 //MEDModule::getSelectedViewMode() const
374 //{
375 //  return _presentationController->getSelectedViewMode();
376 //}
377 //
378 //MEDCALC::ColorMapType
379 //MEDModule::getSelectedColorMap() const
380 //{
381 //  return _presentationController->getSelectedColorMap();
382 //}
383 //
384 //MEDCALC::ScalarBarRangeType
385 //MEDModule::getSelectedScalarBarRange() const
386 //{
387 //  return _presentationController->getSelectedScalarBarRange();
388 //}
389
390
391 /**
392  * Returns presentation name, type and ID from the currently selected presentation in the object
393  * browser.
394  */
395 bool
396 MEDModule::itemClickGeneric(std::string & name, std::string & type, int & presId) const
397 {
398   DataObjectList dol = getApp()->objectBrowser()->getSelected();
399   if (dol.isEmpty())
400     return false;
401   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
402   if (!item)
403     return false;
404   SalomeApp_DataModel *model = dynamic_cast<SalomeApp_DataModel*>(dataModel());
405   if (!model)
406     return false;
407
408   if (item->componentDataType().toStdString() != "MED")
409     return false;
410   _PTR(SObject) obj = item->object();
411   _PTR(GenericAttribute) anAttribute;
412
413   if (! obj->FindAttribute(anAttribute, "AttributeName"))
414     return false;
415   _PTR(AttributeName) attrName(anAttribute);
416   name = attrName->Value();
417
418   if (! obj->FindAttribute(anAttribute, "AttributeParameter"))
419     return false;
420   _PTR(AttributeParameter) attrParam(anAttribute);
421   if (! attrParam->IsSet(IS_PRESENTATION, PT_BOOLEAN)
422       || ! attrParam->GetBool(IS_PRESENTATION)) { // Not a presentation
423       return false;
424   }
425 //  if (!attrParam->IsSet(FIELD_ID, PT_INTEGER))
426 //    return false;
427 //  fieldId = attrParam->GetInt(FIELD_ID);
428   if (!attrParam->IsSet(PRESENTATION_ID, PT_INTEGER))
429       return false;
430   presId = attrParam->GetInt(PRESENTATION_ID);
431   if (!attrParam->IsSet(PRESENTATION_TYPE, PT_STRING))
432     return false;
433   type = attrParam->GetString(PRESENTATION_TYPE);
434   return true;
435 }
436
437 void
438 MEDModule::onClick(const QModelIndex & index)
439 {
440   int presId;
441   std::string name, type;
442   if (!itemClickGeneric(name, type, presId))
443     {
444       // Not a presentation - clear widget:
445       emit presentationSelected(-1, QString(""), QString(""));
446       return;
447     }
448
449 //  STDLOG("Presentation selection");
450 //  std::ostringstream oss;
451 //  oss << fieldId << " / " << presId;
452 //  STDLOG("    - Field id / pres id:   " + oss.str());
453 //  STDLOG("    - Presentation type: " + type);
454 //  STDLOG("    - Presentation name: " + name);
455
456   emit presentationSelected(presId, QString::fromStdString(type), QString::fromStdString(name) );  // caught by PresentationController
457 }
458
459 void
460 MEDModule::onDblClick(const QModelIndex& index)
461 {
462   int presId;
463   std::string name, type;
464   if (!itemClickGeneric(name, type, presId))
465     return;
466
467 //  STDLOG("Presentation double click");
468 //  STDLOG("  Presentation infos:");
469 //  STDLOG("    - Component:         " + item->componentDataType().toStdString());
470 //  STDLOG("    - Item entry:        " + item->entry().toStdString());
471 //  STDLOG("    - Item name:         " + item->name().toStdString());
472 //  std::ostringstream oss;
473 //  oss << fieldId;
474 //  STDLOG("    - Field id:          " + oss.str());
475 //  STDLOG("    - Presentation name: " + name);
476 }
477
478 void
479 MEDModule::requestSALOMETermination() const
480 {
481   STDLOG("Requesting SALOME termination!!");
482   SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE, 1 );  // killServers = True
483 }
484
485
486 //bool MEDModule::hasMainEventLoopStarted() const
487 //{
488 //  return _eventLoopStarted;
489 //}
490
491 int
492 MEDModule::getIntParamFromStudyEditor(SALOMEDS::SObject_var obj, const char* name)
493 {
494   if (obj->_is_nil())
495     return -1;
496
497   SALOMEDS::GenericAttribute_var anAttr;
498   SALOMEDS::AttributeParameter_var aParam;
499   if ( obj->FindAttribute(anAttr,"AttributeParameter") ) {
500     aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
501     if (aParam->IsSet(name, PT_INTEGER))
502       return aParam->GetInt(name);
503   }
504   return -1;
505 }