Salome HOME
Merge multi-study removal branch
[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 #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   if (_studyEditor)
74     delete _studyEditor;
75   if (_datasourceController)
76     delete _datasourceController;
77   //if (_workspaceController)
78   //  delete _workspaceController;
79   if (_presentationController)
80     delete _presentationController;
81   if (_processingController)
82     delete _processingController;
83 }
84
85 MED_ORB::MED_Gen_var
86 MEDModule::engine()
87 {
88   init(); // initialize engine, if necessary
89   return _MED_engine;
90 }
91
92 void
93 MEDModule::init()
94 {
95   // initialize MED module engine (load, if necessary)
96   if ( CORBA::is_nil( _MED_engine ) ) {
97     Engines::EngineComponent_var comp =
98       SalomeApp_Application::lcc()->FindOrLoad_Component( "FactoryServer", "MED" );
99     if (CORBA::is_nil( comp ))
100         STDLOG("Could not FindOrLoad_Component MED");
101     _MED_engine = MED_ORB::MED_Gen::_narrow( comp );
102     if (CORBA::is_nil( _MED_engine ))
103         STDLOG("Could not narrow MED engine");
104   }
105 }
106
107 //void MEDModule::onEventLoopStarted()
108 //{
109 //  if(!getApp()->isMainEventLoopStarted())
110 //    {
111 //      QTimer::singleShot(100, this, SLOT(onEventLoopStarted()));
112 //      return;
113 //    }
114 //}
115
116 void
117 MEDModule::initialize( CAM_Application* app )
118 {
119   // call the parent implementation
120   SalomeApp_Module::initialize( app );
121
122   getApp()->objectBrowser()->setAutoOpenLevel(5);
123
124   if (app && app->desktop()) {
125     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(doubleClicked(const QModelIndex&)),
126             this, SLOT(onDblClick(const QModelIndex&)));
127     connect((QObject*) (getApp()->objectBrowser()->treeView()), SIGNAL(clicked(const QModelIndex&)),
128                 this, SLOT(onClick(const QModelIndex&)));
129   }
130
131   // The following initializes the GUI widget and associated actions
132   this->createModuleWidgets();
133   this->createModuleActions();
134
135   // Now that the workspace controller is created, ParaView core application has normally been started,
136   // and hidden GUI elements have been created.  We can fire the VCR toolbar activation:
137   initToolbars();
138 }
139
140 QString
141 MEDModule::engineIOR() const
142 {
143   init(); // initialize engine, if necessary
144   CORBA::String_var anIOR = getApp()->orb()->object_to_string( _MED_engine.in() );
145   return QString( anIOR.in() );
146 }
147
148 QString
149 MEDModule::iconName() const
150 {
151   return tr("ICO_MED_SMALL");
152 }
153
154 void
155 MEDModule::windows( QMap<int, int>& theMap ) const
156 {
157   // want Object browser, in the left area
158   theMap.insert( SalomeApp_Application::WT_ObjectBrowser,
159                  Qt::LeftDockWidgetArea );
160 #ifndef DISABLE_PYCONSOLE
161   // want Python console, in the bottom area
162   theMap.insert( SalomeApp_Application::WT_PyConsole,
163                  Qt::BottomDockWidgetArea );
164 #endif
165 }
166
167 void
168 MEDModule::viewManagers( QStringList& list ) const
169 {
170 #ifndef DISABLE_PVVIEWER
171   list.append( PVViewer_Viewer::Type() );
172 #endif
173 }
174
175 void
176 MEDModule::createPreferences()
177 {
178   int genTab = addPreference(tr("PREF_TAB_GENERAL"));
179
180   int themeGroup = addPreference(tr("PREF_THEME_GROUP"), genTab);
181   setPreferenceProperty(themeGroup, "columns", 2);
182   int icons = addPreference(tr("PREF_ICONS"), themeGroup, LightApp_Preferences::Selector, "MEDCalc", "icons" );
183   QStringList iconsThemes;
184   iconsThemes.append(tr("PREF_ICON_THEME_MODERN"));
185   iconsThemes.append(tr("PREF_ICON_THEME_CLASSIC"));
186   QList<QVariant> indices;
187   indices.append(0);
188   indices.append(1);
189   setPreferenceProperty(icons, "strings", iconsThemes);
190   setPreferenceProperty(icons, "indexes", indices);
191 }
192
193 bool
194 MEDModule::activateModule( SUIT_Study* theStudy )
195 {
196   if ( CORBA::is_nil( _MED_engine ) )
197     return false;
198
199   // call parent implementation
200   bool bOk = SalomeApp_Module::activateModule( theStudy );
201   if (!bOk)
202     return false;
203
204   // show own menus
205   setMenuShown( true );
206   // show own toolbars
207   setToolShown( true );
208
209   //this->createStudyComponent(theStudy);
210   _workspaceController->showDockWidgets(true);
211   _presentationController->showDockWidgets(true);
212   //this->setDockLayout(StandardApp_Module::DOCKLAYOUT_LEFT_VLARGE);
213
214   // Mark the start of the main event loop - important for test playback:
215 //  QObject::connect(getApp(), SIGNAL(activated(SUIT_Application *)), this, SLOT(onEventLoopStarted(SUIT_Application *)));
216 //  QTimer::singleShot(0, this, SLOT(onEventLoopStarted()));
217
218   // return the activation status
219   return bOk;
220 }
221
222 bool
223 MEDModule::deactivateModule( SUIT_Study* theStudy )
224 {
225  // Clean up engine:
226   STDLOG("MEDModule::deactivateModule(): cleaning up engine side.");
227   _MED_engine->cleanUp();
228   MEDFactoryClient::getFactory()->getPresentationManager()->cleanUp();
229   MEDFactoryClient::getFactory()->getDataManager()->cleanUp();
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 }