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