Salome HOME
920e620a134284d37538188745ee973badfba662
[modules/med.git] / src / MEDCalc / gui / PresentationController.cxx
1 // Copyright (C) 2016-2023  CEA, EDF
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 #include "PresentationController.hxx"
21 #include <MEDCalcConstants.hxx>
22 #include "MEDModule.hxx"
23 #include "Basics_Utils.hxx"
24 #include "QtxActionGroup.h"
25 #include "QtxActionToolMgr.h"
26 #include "MEDFactoryClient.hxx"
27 #include "MEDPresentationManager_i.hxx"
28 #include "XmedConsoleDriver.hxx"
29
30 #include "MEDPresentationMeshView.hxx"
31 #include "MEDPresentationScalarMap.hxx"
32 #include "MEDPresentationContour.hxx"
33 #include "MEDPresentationSlices.hxx"
34 #include "MEDPresentationPointSprite.hxx"
35 #include "MEDPresentationVectorField.hxx"
36 #include "MEDPresentationPlot3D.hxx"
37 #include "MEDPresentationStreamLines.hxx"
38 #include "MEDPresentationCutSegment.hxx"
39 #include "MEDPresentationDeflectionShape.hxx"
40
41 #include "MEDWidgetHelperMeshView.hxx"
42 #include "MEDWidgetHelperScalarMap.hxx"
43 #include "MEDWidgetHelperContour.hxx"
44 #include "MEDWidgetHelperSlices.hxx"
45 #include "MEDWidgetHelperPointSprite.hxx"
46 #include "MEDWidgetHelperPlot3D.hxx"
47 #include "MEDWidgetHelperStreamLines.hxx"
48 #include "MEDWidgetHelperCutSegment.hxx"
49 #include "MEDWidgetHelperVectorField.hxx"
50 #include "MEDWidgetHelperDeflectionShape.hxx"
51
52 #include <SalomeApp_Application.h>
53 #include <SalomeApp_Study.h>
54 #include <SalomeApp_DataObject.h>
55
56 #include <SALOME_ListIO.hxx>
57 #include <LightApp_SelectionMgr.h>
58
59 #include <SALOMEDS_SObject.hxx>
60 #include <SALOMEDS_Study.hxx>
61
62 #include <SUIT_Desktop.h>
63 #include <SUIT_Session.h>
64 #include <SUIT_ResourceMgr.h>
65 #include <QMessageBox>
66 #include <sstream>
67 #include <regex>
68
69 #include "MEDFactoryClient.hxx"
70
71 static const int OPTIONS_VIEW_MODE_ID = 943;
72 static const int OPTIONS_VIEW_MODE_REPLACE_ID = 944;
73 static const int OPTIONS_VIEW_MODE_OVERLAP_ID = 945;
74 static const int OPTIONS_VIEW_MODE_NEW_LAYOUT_ID = 946;
75 static const int OPTIONS_VIEW_MODE_SPLIT_VIEW_ID = 947;
76
77 //! The only instance of the MEDPresentationManager
78 MEDCALC::MEDPresentationManager_ptr PresentationController::_presManager;
79
80 PresentationController::PresentationController(MEDModule* salomeModule) :
81         _salomeModule(salomeModule),
82         _consoleDriver(0),
83         _studyEditor(salomeModule->getStudyEditor()),
84         _presHelperMap(),
85         _currentWidgetHelper(0)
86 {
87   STDLOG("Creating a PresentationController");
88
89   _widgetPresentationParameters = new WidgetPresentationParameters();
90
91   QMainWindow* parent = salomeModule->getApp()->desktop();
92   _dockWidget = new QDockWidget(parent);
93   _dockWidget->setVisible(false);
94   _dockWidget->setWindowTitle(tr("TITLE_PRESENTATION_PARAMETERS"));
95   _dockWidget->setObjectName(tr("TITLE_PRESENTATION_PARAMETERS"));
96   _dockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
97   _dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
98   _dockWidget->setWidget(_widgetPresentationParameters);
99   parent->addDockWidget(Qt::LeftDockWidgetArea, _dockWidget);
100   //_dockWidget->show();
101
102   // Retrieve MEDFactory to get MEDPresentationManager (sometimes GUI needs to talk to the engine directly)
103   if ( ! _presManager ) {
104       _presManager = MEDFactoryClient::getFactory()->getPresentationManager();
105   }
106
107   // Connect to the click in the object browser
108   connect(salomeModule, SIGNAL( presentationSelected(int , const QString&, const QString&) ),
109           this, SLOT(onPresentationSelected(int , const QString&, const QString&) )     );
110 }
111
112 PresentationController::~PresentationController()
113 {
114   STDLOG("Deleting the resentationController");
115   // Clean allocated widget helpers:
116   for ( std::map<int, MEDWidgetHelper *>::iterator it = _presHelperMap.begin(); it != _presHelperMap.end(); ++it)
117     delete((*it).second);
118 }
119
120 std::string
121 PresentationController::_getIconName(const std::string& name)
122 {
123   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
124   if (!mgr)
125     return name;
126
127   // Read value from preferences and suffix name to select icon theme
128   int theme = mgr->integerValue("MEDCalc", "icons");
129   if (theme == 0) {
130       return name + "_MODERN";
131   } else if (theme == 1) {
132       return name + "_CLASSIC";
133   }
134   return name + "_DEFAULT";
135 }
136
137 void
138 PresentationController::createActions()
139 {
140   STDLOG("Creating PresentationController actions");
141
142   // View Mode
143   int viewModeToolbarId = _salomeModule->createTool("View Mode", "ViewModeToolbar");
144   QtxActionGroup* ag = _salomeModule->createActionGroup(OPTIONS_VIEW_MODE_ID, true);
145   ag->setText("View mode");
146   ag->setUsesDropDown(true);
147   QString label   = tr("LAB_VIEW_MODE_REPLACE");
148   QString tooltip = tr("TIP_VIEW_MODE_REPLACE");
149   QAction* a = _salomeModule->createAction(OPTIONS_VIEW_MODE_REPLACE_ID,label,QIcon(),label,tooltip,0);
150   a->setCheckable(true);
151   a->setChecked(true);
152   ag->add(a);
153
154   label   = tr("LAB_VIEW_MODE_OVERLAP");
155   tooltip = tr("TIP_VIEW_MODE_OVERLAP");
156   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_OVERLAP_ID,label,QIcon(),label,tooltip,0);
157   a->setCheckable(true);
158   ag->add(a);
159
160   label   = tr("LAB_VIEW_MODE_NEW_LAYOUT");
161   tooltip = tr("TIP_VIEW_MODE_NEW_LAYOUT");
162   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID,label,QIcon(),label,tooltip,0);
163   a->setCheckable(true);
164   ag->add(a);
165
166   label   = tr("LAB_VIEW_MODE_SPLIT_VIEW");
167   tooltip = tr("TIP_VIEW_MODE_SPLIT_VIEW");
168   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID,label,QIcon(),label,tooltip,0);
169   a->setCheckable(true);
170   ag->add(a);
171
172   _salomeModule->createTool(OPTIONS_VIEW_MODE_ID, viewModeToolbarId);
173
174   // Presentations
175   int presentationToolbarId = _salomeModule->createTool("Presentations", "PresentationToolbar");
176   int presentationMenuId = _salomeModule->createMenu(tr("MENU_PRESENTATIONS"), -1, -1, 10);
177
178   label   = tr("LAB_PRESENTATION_MESH_VIEW");
179   tooltip = tr("TIP_PRESENTATION_MESH_VIEW");
180   QString icon = tr(_getIconName("ICO_PRESENTATION_MESH_VIEW").c_str());
181   int actionId;
182   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeMeshView()),icon,tooltip);
183   _salomeModule->createTool(actionId, presentationToolbarId);
184   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
185   _salomeModule->createMenu(actionId, presentationMenuId);
186
187   label   = tr("LAB_PRESENTATION_SCALAR_MAP");
188   tooltip = tr("TIP_PRESENTATION_SCALAR_MAP");
189   icon = tr(_getIconName("ICO_PRESENTATION_SCALAR_MAP").c_str());
190   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeScalarMap()),
191                                                  icon, tooltip, FIELDSOp::OpScalarMap);
192   _salomeModule->createTool(actionId, presentationToolbarId);
193   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
194   _salomeModule->createMenu(actionId, presentationMenuId);
195
196   label   = tr("LAB_PRESENTATION_CONTOUR");
197   tooltip = tr("TIP_PRESENTATION_CONTOUR");
198   icon    = tr(_getIconName("ICO_PRESENTATION_CONTOUR").c_str());
199   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeContour()),
200                                                  icon, tooltip, FIELDSOp::OpContour);
201   _salomeModule->createTool(actionId, presentationToolbarId);
202   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
203   _salomeModule->createMenu(actionId, presentationMenuId);
204
205   label   = tr("LAB_PRESENTATION_VECTOR_FIELD");
206   tooltip = tr("TIP_PRESENTATION_VECTOR_FIELD");
207   icon    = tr(_getIconName("ICO_PRESENTATION_VECTOR_FIELD").c_str());
208   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeVectorField()),
209                                                  icon, tooltip, FIELDSOp::OpVectorFields);
210   _salomeModule->createTool(actionId, presentationToolbarId);
211   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
212   _salomeModule->createMenu(actionId, presentationMenuId);
213
214   label   = tr("LAB_PRESENTATION_SLICES");
215   tooltip = tr("TIP_PRESENTATION_SLICES");
216   icon    = tr(_getIconName("ICO_PRESENTATION_SLICES").c_str());
217   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeSlices()),
218                                                  icon, tooltip, FIELDSOp::OpSlices);
219   _salomeModule->createTool(actionId, presentationToolbarId);
220   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
221   _salomeModule->createMenu(actionId, presentationMenuId);
222
223   label   = tr("LAB_PRESENTATION_DEFLECTION_SHAPE");
224   tooltip = tr("TIP_PRESENTATION_DEFLECTION_SHAPE");
225   icon    = tr(_getIconName("ICO_PRESENTATION_DEFLECTION_SHAPE").c_str());
226   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeDeflectionShape()),
227                                                  icon, tooltip, FIELDSOp::OpDeflectionShape);
228   _salomeModule->createTool(actionId, presentationToolbarId);
229   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
230   _salomeModule->createMenu(actionId, presentationMenuId);
231
232   label   = tr("LAB_PRESENTATION_POINT_SPRITE");
233   tooltip = tr("TIP_PRESENTATION_POINT_SPRITE");
234   icon    = tr(_getIconName("ICO_PRESENTATION_POINT_SPRITE").c_str());
235   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizePointSprite()),
236                                                  icon, tooltip, FIELDSOp::OpPointSprite);
237   _salomeModule->createTool(actionId, presentationToolbarId);
238   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
239   _salomeModule->createMenu(actionId, presentationMenuId);
240
241   // sphinx doc: begin of plot3d gui items
242   label   = tr("LAB_PRESENTATION_PLOT3D");
243   tooltip = tr("TIP_PRESENTATION_PLOT3D");
244   icon    = tr(_getIconName("ICO_PRESENTATION_PLOT3D").c_str());
245   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizePlot3D()),
246                                                  icon, tooltip, FIELDSOp::OpPlot3D);
247   _salomeModule->createTool(actionId, presentationToolbarId);
248   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
249   _salomeModule->createMenu(actionId, presentationMenuId);
250   // sphinx doc: end of plot3d gui items
251
252   label   = tr("LAB_PRESENTATION_STREAM_LINES");
253   tooltip = tr("TIP_PRESENTATION_STREAM_LINES");
254   icon    = tr(_getIconName("ICO_PRESENTATION_STREAM_LINES").c_str());
255   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeStreamLines()),
256                                                  icon, tooltip, FIELDSOp::OpStreamLines);
257   _salomeModule->createTool(actionId, presentationToolbarId);
258   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
259   _salomeModule->createMenu(actionId, presentationMenuId);
260
261   label   = tr("LAB_PRESENTATION_CUT_SEGMENT");
262   tooltip = tr("TIP_PRESENTATION_CUT_SEGMENT");
263   icon    = tr(_getIconName("ICO_PRESENTATION_CUT_SEGMENT").c_str());
264   actionId = _salomeModule->createStandardAction(label, this, SLOT(onVisualizeCutSegment()),
265                                                  icon, tooltip, FIELDSOp::OpCutSegment);
266   _salomeModule->createTool(actionId, presentationToolbarId);
267   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
268   _salomeModule->createMenu(actionId, presentationMenuId);
269
270
271   // Separator
272   _salomeModule->createMenu(_salomeModule->separator(), presentationMenuId);
273
274   label   = tr("LAB_DELETE_PRESENTATION");
275   tooltip = tr("TIP_DELETE_PRESENTATION");
276   icon    = tr("ICO_DELETE_PRESENTATION");
277   actionId = _salomeModule->createStandardAction(label,this, SLOT(onDeletePresentation()),icon,tooltip);
278   //  _salomeModule->createTool(actionId, presentationToolbarId);
279   //  _salomeModule->action(actionId)->setIconVisibleInMenu(true);
280   _salomeModule->createMenu(actionId, presentationMenuId);
281
282   // Low level PARAVIS dump
283   label = tr("LAB_PARAVIS_DUMP");
284   tooltip = tr("TIP_PARAVIS_DUMP");
285   actionId = _salomeModule->createStandardAction(label,this,SLOT(onParavisDump()),"");
286   _salomeModule->createMenu(actionId, presentationMenuId);
287
288   //
289   // Actions for popup menu only
290   //
291
292 }
293
294 MEDCALC::ViewModeType
295 PresentationController::getSelectedViewMode() const
296 {
297   if (_salomeModule->action(OPTIONS_VIEW_MODE_REPLACE_ID)->isChecked()) {
298       return MEDCALC::VIEW_MODE_REPLACE;
299   }
300   else if (_salomeModule->action(OPTIONS_VIEW_MODE_OVERLAP_ID)->isChecked()) {
301       return MEDCALC::VIEW_MODE_OVERLAP;
302   }
303   else if (_salomeModule->action(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID)->isChecked()) {
304       return MEDCALC::VIEW_MODE_NEW_LAYOUT;
305   }
306   else if (_salomeModule->action(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID)->isChecked()) {
307       return MEDCALC::VIEW_MODE_SPLIT_VIEW;
308   }
309   // Should not happen
310   STDLOG("Strange!! No matching view mode found - returning VIEW_MODE_REPLACE.");
311   return MEDCALC::VIEW_MODE_REPLACE;
312 }
313
314 MEDCALC::ColorMapType
315 PresentationController::getSelectedColorMap() const
316 {
317   return _widgetPresentationParameters->getColorMap();
318 }
319
320 MEDCALC::ScalarBarRangeType
321 PresentationController::getSelectedScalarBarRange() const
322 {
323   return _widgetPresentationParameters->getScalarBarRange();
324 }
325
326 void
327 PresentationController::visualize(PresentationEvent::EventType eventType)
328 {
329   // Get the selected objects in the study (SObject)
330   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
331
332   // For each object, emit a signal to the workspace to request a
333   // visualisation using the tui command (so that the user can see how
334   // to make a view of an object from the tui console).
335   for (int i=0; i<(int)listOfSObject->size(); i++) {
336       SALOMEDS::SObject_var soObj = listOfSObject->at(i);
337       std::string name(_studyEditor->getName(soObj));
338       if (soObj->_is_nil() || name == "MEDCalc")
339         return;
340       int fieldId = _salomeModule->getIntParamFromStudyEditor(soObj, FIELD_ID);
341       int meshId = _salomeModule->getIntParamFromStudyEditor(soObj, MESH_ID);
342       MEDCALC::FieldHandler* fieldHandler = 0;
343       MEDCALC::MeshHandler* meshHandler = 0;
344
345       // is it a mesh?
346       if (meshId >= 0)
347         {
348           if (eventType != PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW)
349             continue;
350           meshHandler = MEDFactoryClient::getDataManager()->getMeshHandler(meshId);
351         }
352       else
353         {
354           if (fieldId < 0)  // is it a field series?
355             {
356               int fieldSeriesId = _salomeModule->getIntParamFromStudyEditor(soObj, FIELD_SERIES_ID);
357               // If fieldId and fieldSeriesId equals -1, then it means that it is not a field
358               // managed by the MED module, and we stop this function process.
359               if ( fieldSeriesId < 0)
360                   continue;
361
362               // get the current timestamp
363               double timestamp = _salomeModule->getCurrentAnimationTimestamp();
364               // get the field id a the current timestamp
365               fieldId = MEDFactoryClient::getDataManager()->getFieldIdAtTimestamp(fieldSeriesId, timestamp);
366             }
367           fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
368         }
369
370       if ((!fieldHandler) && (!meshHandler)) {
371           QMessageBox::warning(_salomeModule->getApp()->desktop(),
372                                tr("Operation not allowed"),
373                                tr("No field (or mesh) is defined"));
374           return;
375       }
376
377       PresentationEvent* event = new PresentationEvent();
378       event->eventtype = eventType;
379       event->fieldHandler = fieldHandler;
380       event->meshHandler = meshHandler;
381       emit presentationSignal(event); // --> processPresentationEvent()
382   }
383 }
384
385 void
386 PresentationController::onVisualizeMeshView()
387 {
388   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW);
389 }
390
391 void
392 PresentationController::onVisualizeScalarMap()
393 {
394   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
395 }
396
397 void
398 PresentationController::onVisualizeContour()
399 {
400   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
401 }
402
403 void
404 PresentationController::onVisualizeVectorField()
405 {
406   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
407 }
408
409 void
410 PresentationController::onVisualizeSlices()
411 {
412   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
413 }
414
415 void
416 PresentationController::onVisualizeDeflectionShape()
417 {
418   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
419 }
420
421 void
422 PresentationController::onVisualizePointSprite()
423 {
424   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
425 }
426
427 // sphinx doc: begin of onVisualizePlot3D
428 void
429 PresentationController::onVisualizePlot3D()
430 {
431   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_PLOT3D);
432 }
433 // sphinx doc: end of onVisualizePlot3D
434
435 void
436 PresentationController::onVisualizeStreamLines()
437 {
438   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_STREAM_LINES);
439 }
440
441 void
442 PresentationController::onVisualizeCutSegment()
443 {
444   // Cut segment presentation "creates" new view, so switch off visibility state update 
445   // because pqActiveObjects::viewChanged is emmited 
446   _salomeModule->visibilityStateUpdateOff();
447   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CUT_SEGMENT);
448   _salomeModule->visibilityStateUpdateOn();
449   _salomeModule->updateVisibilityState();
450 }
451
452 void
453 PresentationController::onDeletePresentation()
454 {
455   // Get the selected objects in the study (SObject)
456   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
457
458   // For each object, emit a signal to the workspace to request pres deletion
459   for (int i=0; i<(int)listOfSObject->size(); i++) {
460       SALOMEDS::SObject_var soPres = listOfSObject->at(i);
461       std::string name(_studyEditor->getName(soPres));
462       if (soPres->_is_nil() || name == "MEDCalc")
463         return;
464       int presId = _salomeModule->getIntParamFromStudyEditor(soPres,PRESENTATION_ID);
465       // If fieldId equals -1, then it means that it is not a field
466       // managed by the MED module, and we stop this function process.
467       if ( presId < 0 )
468         continue;
469
470       PresentationEvent* event = new PresentationEvent();
471       event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION;
472       event->presentationId = presId;
473       emit presentationSignal(event); // --> processPresentationEvent()
474   }
475 }
476
477 QString
478 PresentationController::getViewModePython() const
479 {
480   MEDCALC::ViewModeType viewMode = getSelectedViewMode();
481   switch(viewMode) {
482     case MEDCALC::VIEW_MODE_REPLACE: return "MEDCALC.VIEW_MODE_REPLACE";
483     case MEDCALC::VIEW_MODE_OVERLAP: return "MEDCALC.VIEW_MODE_OVERLAP";
484     case MEDCALC::VIEW_MODE_NEW_LAYOUT: return "MEDCALC.VIEW_MODE_NEW_LAYOUT";
485     case MEDCALC::VIEW_MODE_SPLIT_VIEW: return "MEDCALC.VIEW_MODE_SPLIT_VIEW";
486   }
487   return QString();
488 }
489
490 QString
491 PresentationController::getColorMapPython() const
492 {
493   MEDCALC::ColorMapType colorMap = getSelectedColorMap();
494   switch(colorMap) {
495     case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "MEDCALC.COLOR_MAP_BLUE_TO_RED_RAINBOW";
496     case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "MEDCALC.COLOR_MAP_COOL_TO_WARM";
497   }
498   return QString();
499 }
500
501 QString
502 PresentationController::getScalarBarRangePython() const
503 {
504   MEDCALC::ScalarBarRangeType scalarBarRange = getSelectedScalarBarRange();
505   switch(scalarBarRange) {
506     case MEDCALC::SCALAR_BAR_ALL_TIMESTEPS: return "MEDCALC.SCALAR_BAR_ALL_TIMESTEPS";
507     case MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP: return "MEDCALC.SCALAR_BAR_CURRENT_TIMESTEP";
508     case MEDCALC::SCALAR_BAR_CUSTOM_RANGE: return "MEDCALC.SCALAR_BAR_CUSTOM_RANGE";
509   }
510   return QString();
511 }
512
513 QString
514 PresentationController::getMeshModePython(const int mode) const
515 {
516   MEDCALC::MeshModeType mod = static_cast<MEDCALC::MeshModeType>(mode);
517   switch(mod) {
518     case MEDCALC::MESH_MODE_WIREFRAME:     return "MEDCALC.MESH_MODE_WIREFRAME";
519     case MEDCALC::MESH_MODE_SURFACE:       return "MEDCALC.MESH_MODE_SURFACE";
520     case MEDCALC::MESH_MODE_SURFACE_EDGES: return "MEDCALC.MESH_MODE_SURFACE_EDGES";
521   }
522   return QString();
523 }
524
525 QString
526 PresentationController::getSliceOrientationPython(const int orientation) const
527 {
528   MEDCALC::SliceOrientationType orient = static_cast<MEDCALC::SliceOrientationType>(orientation);
529   switch(orient) {
530     case MEDCALC::SLICE_NORMAL_TO_X:   return "MEDCALC.SLICE_NORMAL_TO_X";
531     case MEDCALC::SLICE_NORMAL_TO_Y:   return "MEDCALC.SLICE_NORMAL_TO_Y";
532     case MEDCALC::SLICE_NORMAL_TO_Z:   return "MEDCALC.SLICE_NORMAL_TO_Z";
533     case MEDCALC::SLICE_NORMAL_TO_XY:  return "MEDCALC.SLICE_NORMAL_TO_XY";
534     case MEDCALC::SLICE_NORMAL_TO_XZ:  return "MEDCALC.SLICE_NORMAL_TO_XZ";
535     case MEDCALC::SLICE_NORMAL_TO_YZ:  return "MEDCALC.SLICE_NORMAL_TO_YZ";
536     case MEDCALC::SLICE_NORMAL_TO_XYZ: return "MEDCALC.SLICE_NORMAL_TO_XYZ";
537   }
538   return QString();
539 }
540
541 QString
542 PresentationController::getIntegrDirTypePython(const int intDir) const
543 {
544   MEDCALC::IntegrationDirType type = static_cast<MEDCALC::IntegrationDirType>(intDir);
545   switch(type) {
546     case MEDCALC::INTEGRATION_DIR_BOTH:     return "MEDCALC.INTEGRATION_DIR_BOTH";
547     case MEDCALC::INTEGRATION_DIR_FORWARD:  return "MEDCALC.INTEGRATION_DIR_FORWARD";
548     case MEDCALC::INTEGRATION_DIR_BACKWARD: return "MEDCALC.INTEGRATION_DIR_BACKWARD";
549   }
550   return QString();
551 }
552
553
554 std::string
555 PresentationController::getPresTypeFromWidgetHelper(int presId) const
556 {
557   std::map<int, MEDWidgetHelper *>::const_iterator it =_presHelperMap.find(presId);
558   if (it != _presHelperMap.end())
559     return (*it).second->getPythonTag();
560   return "UNKNOWN";
561 }
562
563 void
564 PresentationController::emitPresentationSignal(const PresentationEvent* event) 
565 {
566   emit presentationSignal(event);
567 }
568
569 void
570 PresentationController::processPresentationEvent(const PresentationEvent* event) {
571   // --> Send commands to SALOME Python console
572   QString viewMode = getViewModePython();
573   QString colorMap = getColorMapPython();
574   QString scalarBarRange = getScalarBarRangePython();
575   MEDCALC::FieldHandler* fieldHandler = event->fieldHandler;
576   QStringList commands;
577
578   // [ABN] using event mechanism for all this is awkward? TODO: direct implementation in each
579   // dedicated widget helper class?
580
581   if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW ) {
582       // Do we request mesh view from a field or from a mesh only?
583       int meshId = event->meshHandler ? event->meshHandler->id : event->fieldHandler->meshid;
584       commands += QString("presentation_id = medcalc.MakeMeshView(%1, viewMode=%2)").arg(meshId).arg(viewMode);
585       commands += QString("presentation_id");
586     }
587   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP ) {
588       commands += QString("presentation_id = medcalc.MakeScalarMap(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
589           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
590       commands += QString("presentation_id");
591   }
592   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR ) {
593       commands += QString("presentation_id = medcalc.MakeContour(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
594             .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
595       commands += QString("presentation_id");
596   }
597   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_SLICES ) {
598       commands += QString("presentation_id = medcalc.MakeSlices(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
599             .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
600       commands += QString("presentation_id");
601   }
602   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD ) {
603       commands += QString("presentation_id = medcalc.MakeVectorField(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
604           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
605       commands += QString("presentation_id");
606   }
607   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE ) {
608       commands += QString("presentation_id = medcalc.MakePointSprite(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
609               .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
610       commands += QString("presentation_id");
611   }
612   // sphinx doc: begin of plot3d prs creation
613   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_PLOT3D ) {
614       commands += QString("presentation_id = medcalc.MakePlot3D(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
615               .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
616       commands += QString("presentation_id");
617   }
618   // sphinx doc: end of plot3d prs creation
619   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_STREAM_LINES ) {
620       commands += QString("presentation_id = medcalc.MakeStreamLines(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
621               .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
622       commands += QString("presentation_id");
623   }
624   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_CUT_SEGMENT ) {
625       commands += QString("presentation_id = medcalc.MakeCutSegment(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
626               .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
627       commands += QString("presentation_id");
628   }
629   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE ) {
630       commands += QString("presentation_id = medcalc.MakeDeflectionShape(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
631           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
632       commands += QString("presentation_id");
633   }
634
635
636   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_COMPONENT ) {
637       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
638       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
639       commands += QString("params.displayedComponent = '%1'").arg(QString::fromStdString(event->aString));
640       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
641   }
642   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_COLORMAP ) {
643       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
644       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
645       commands += QString("params.colorMap = %1").arg(getColorMapPython());
646       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
647   }
648   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_TIME_RANGE ) {
649       bool customRangeFlag =
650         (bool)_presManager->getPresentationIntProperty(event->presentationId, MEDPresentation::PROP_HIDE_DATA_OUTSIDE_CUSTOM_RANGE.c_str());
651       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
652       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
653       commands += QString("params.scalarBarRange = %1").arg(getScalarBarRangePython());
654       if (getSelectedScalarBarRange() == MEDCALC::SCALAR_BAR_CUSTOM_RANGE) {
655         commands += QString("params.scalarBarRangeArray = [%1, %2]").arg(event->aDouble1).arg(event->aDouble2);
656       }
657       else {
658         if (customRangeFlag) // switch off hideDataOutsideCustomRange
659         {
660           commands += QString("params.hideDataOutsideCustomRange = False");
661         }
662       }
663       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
664   }
665   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NB_CONTOUR ) {
666       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
667       commands += QString("params = medcalc.GetContourParameters(%2)").arg(event->presentationId);
668       commands += QString("params.nbContours = %1").arg(event->anInteger);
669       commands += QString("medcalc.UpdateContour(%1, params)").arg(event->presentationId);
670   }
671   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_MESH_MODE ) {
672       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
673       commands += QString("params = medcalc.GetMeshViewParameters(%2)").arg(event->presentationId);
674       commands += QString("params.mode = %1").arg(getMeshModePython(event->anInteger));
675       commands += QString("medcalc.UpdateMeshView(%1, params)").arg(event->presentationId);
676   }
677   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NB_SLICES ) {
678       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
679       commands += QString("params = medcalc.GetSlicesParameters(%2)").arg(event->presentationId);
680       commands += QString("params.nbSlices = %1").arg(event->anInteger);
681       commands += QString("medcalc.UpdateSlices(%1, params)").arg(event->presentationId);
682   }
683   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_SLICE_ORIENTATION ) {
684       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
685       commands += QString("params = medcalc.GetSlicesParameters(%2)").arg(event->presentationId);
686       commands += QString("params.orientation = %1").arg(getSliceOrientationPython(event->anInteger));
687       commands += QString("medcalc.UpdateSlices(%1, params)").arg(event->presentationId);
688   }
689   // sphinx doc: begin of plot3d prs update
690   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_PLANE_POS ) {
691       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
692       commands += QString("params = medcalc.GetPlot3DParameters(%2)").arg(event->presentationId);
693       commands += QString("params.planePos = %1").arg(event->aDouble3);
694       commands += QString("medcalc.UpdatePlot3D(%1, params)").arg(event->presentationId);
695   }
696   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NORMAL ) {
697       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
698       commands += QString("params = medcalc.GetPlot3DParameters(%2)").arg(event->presentationId);
699       commands += QString("params.planeNormal = [%1, %2, %3]").arg(event->aDoubleN[0]).arg(event->aDoubleN[1]).arg(event->aDoubleN[2]);
700       commands += QString("medcalc.UpdatePlot3D(%1, params)").arg(event->presentationId);
701   }
702   // sphinx doc: end of plot3d prs update
703   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_CUT_POINT1 ) {
704       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
705       commands += QString("params = medcalc.GetCutSegmentParameters(%2)").arg(event->presentationId);
706       commands += QString("params.point1 = [%1, %2, %3]").arg(event->aDoubleP1[0]).arg(event->aDoubleP1[1]).arg(event->aDoubleP1[2]);
707       commands += QString("medcalc.UpdateCutSegment(%1, params)").arg(event->presentationId);
708   }
709   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_CUT_POINT2 ) {
710       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
711       commands += QString("params = medcalc.GetCutSegmentParameters(%2)").arg(event->presentationId);
712       commands += QString("params.point2 = [%1, %2, %3]").arg(event->aDoubleP2[0]).arg(event->aDoubleP2[1]).arg(event->aDoubleP2[2]);
713       commands += QString("medcalc.UpdateCutSegment(%1, params)").arg(event->presentationId);
714   }
715   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_INTEGR_DIR ) {
716       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
717       commands += QString("params = medcalc.GetStreamLinesParameters(%2)").arg(event->presentationId);
718       commands += QString("params.integrDir = %1").arg(getIntegrDirTypePython(event->anInteger));
719       commands += QString("medcalc.UpdateStreamLines(%1, params)").arg(event->presentationId);
720   }
721   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_CONTOUR_COMPONENT) {
722       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
723       commands += QString("params = medcalc.GetContourParameters(%2)").arg(event->presentationId);
724       commands += QString("params.contourComponent = '%1'").arg(event->aString.c_str());
725       commands += QString("medcalc.UpdateContour(%1, params)").arg(event->presentationId);
726   }
727   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_SCALE_FACTOR ) {
728       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
729       commands += QString("params = medcalc.GetVectorFieldParameters(%2)").arg(event->presentationId);
730       commands += QString("params.scaleFactor = %1").arg(event->aDouble3);
731       commands += QString("medcalc.UpdateVectorField(%1, params)").arg(event->presentationId);
732   }
733
734   else if (event->eventtype == PresentationEvent::EVENT_CHANGE_CUSTOM_SCALE_FACTOR) {
735   std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
736   commands += QString("params = medcalc.GetVectorFieldParameters(%2)").arg(event->presentationId);
737   commands += QString("params.customScaleFactor = %1").arg(event->anInteger);
738   if(event->anInteger) {
739     commands += QString("params.scaleFactor = %1").arg(event->aDouble3);
740   }
741   commands += QString("medcalc.UpdateVectorField(%1, params)").arg(event->presentationId);
742   }
743
744   else if ( event->eventtype == PresentationEvent::EVENT_DELETE_PRESENTATION ) {
745       commands += QString("medcalc.RemovePresentation(%1)").arg(event->presentationId);
746
747   }
748   else if (event->eventtype == PresentationEvent::EVENT_DISPLAY_PRESENTATION ||
749            event->eventtype == PresentationEvent::EVENT_ERASE_PRESENTATION) {
750     commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(getPresTypeFromWidgetHelper(event->presentationId)))
751       .arg(event->presentationId);
752     QString visility = event->eventtype == PresentationEvent::EVENT_DISPLAY_PRESENTATION ? QString("True") : QString("False");
753     commands += QString("params.visibility = %1").arg(visility);
754     commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(getPresTypeFromWidgetHelper(event->presentationId)))
755       .arg(event->presentationId);
756   }
757   else if (event->eventtype == PresentationEvent::EVENT_SCALAR_BAR_VISIBILITY_CHANGED ||
758            event->eventtype == PresentationEvent::EVENT_HIDE_DATA_OUTSIDE_CUSTOM_RANGE_CHANGED) {
759     std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
760     QString param;
761     switch (event->eventtype) {
762     case PresentationEvent::EVENT_SCALAR_BAR_VISIBILITY_CHANGED: 
763       param = QString("scalarBarVisibility"); 
764       break;
765     case PresentationEvent::EVENT_HIDE_DATA_OUTSIDE_CUSTOM_RANGE_CHANGED:
766       param = QString("hideDataOutsideCustomRange"); 
767       break;
768     default: break;
769     }
770     commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
771     commands += QString("params.%1 = %2").arg(param).arg( event->anInteger ? QString("True") : QString("False"));
772     if (event->eventtype == PresentationEvent::EVENT_HIDE_DATA_OUTSIDE_CUSTOM_RANGE_CHANGED && event->anInteger) {
773       commands += QString("params.scalarBarRangeArray = [%1, %2]").arg(event->aDouble1).arg(event->aDouble2);
774     }
775     commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
776   }
777   else if (event->eventtype == PresentationEvent::EVENT_CUSTOM_RANGE_CHANGED)
778   {
779   std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
780     commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
781     commands += QString("params.scalarBarRangeArray = [%1, %2]").arg(event->aDouble1).arg(event->aDouble2);
782     commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
783   }
784   else {
785       STDLOG("The event "<<event->eventtype<<" is not implemented yet");
786   }
787   _consoleDriver->exec(commands);
788 }
789
790 MEDWidgetHelper *
791 PresentationController::findOrCreateWidgetHelper(MEDCALC::MEDPresentationManager_ptr /*presManager*/,                  // todo: unused
792                                                  int presId, const std::string& type, const std::string& name )
793 {
794   std::map<int, MEDWidgetHelper *>::const_iterator it =_presHelperMap.find(presId);
795   if (it != _presHelperMap.end())
796     return (*it).second;
797   MEDWidgetHelper * wh = 0;
798   if (type == MEDPresentationMeshView::TYPE_NAME)
799     wh = new MEDWidgetHelperMeshView(this, _presManager, presId, name, _widgetPresentationParameters);
800   else if (type == MEDPresentationScalarMap::TYPE_NAME)
801     wh = new MEDWidgetHelperScalarMap(this, _presManager, presId, name, _widgetPresentationParameters);
802   else if (type == MEDPresentationContour::TYPE_NAME)
803     wh = new MEDWidgetHelperContour(this, _presManager, presId, name, _widgetPresentationParameters);
804   else if (type == MEDPresentationSlices::TYPE_NAME)
805     wh = new MEDWidgetHelperSlices(this, _presManager, presId, name, _widgetPresentationParameters);
806   else if (type == MEDPresentationVectorField::TYPE_NAME)
807     wh = new MEDWidgetHelperVectorField(this, _presManager, presId, name, _widgetPresentationParameters);
808   else if (type == MEDPresentationPointSprite::TYPE_NAME)
809     wh = new MEDWidgetHelperPointSprite(this, _presManager, presId, name, _widgetPresentationParameters);
810   else if (type == MEDPresentationPlot3D::TYPE_NAME)
811     wh = new MEDWidgetHelperPlot3D(this, _presManager, presId, name, _widgetPresentationParameters);
812   else if (type == MEDPresentationStreamLines::TYPE_NAME)
813     wh = new MEDWidgetHelperStreamLines(this, _presManager, presId, name, _widgetPresentationParameters);
814   else if (type == MEDPresentationCutSegment::TYPE_NAME)
815     wh = new MEDWidgetHelperCutSegment(this, _presManager, presId, name, _widgetPresentationParameters);
816   else if (type == MEDPresentationDeflectionShape::TYPE_NAME)
817     wh = new MEDWidgetHelperDeflectionShape(this, _presManager, presId, name, _widgetPresentationParameters);
818   else
819     {
820       STDLOG("findOrCreateWidgetHelper(): NOT IMPLEMENTED !!!");
821       return wh;
822     }
823   _presHelperMap[presId] = wh;
824   return wh;
825 }
826
827 void
828 PresentationController::onPresentationSelected(int presId, const QString& presType, const QString& presName)
829 {
830   if (presId == -1)
831     {
832       if (_widgetPresentationParameters->isShown())
833         {
834           _widgetPresentationParameters->toggleWidget(false);
835           if(_currentWidgetHelper)
836             _currentWidgetHelper->releaseWidget();
837         }
838     }
839   else
840     {
841       if(_currentWidgetHelper)
842         _currentWidgetHelper->releaseWidget();
843       // Activate corresponding ParaView render view
844       _presManager->activateView(presId);
845       // Update widgets parameters
846       _currentWidgetHelper = findOrCreateWidgetHelper(_presManager, presId, presType.toStdString(), presName.toStdString());
847       _currentWidgetHelper->updateWidget(true);
848     }
849 }
850
851 void
852 PresentationController::onParavisDump()
853 {
854   // Get the selected objects in the study (SObject)
855   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
856
857   // For the first object only, request the dump
858   for (int i=0; i<(int)listOfSObject->size(); i++) {
859       SALOMEDS::SObject_var soPres = listOfSObject->at(i);
860       std::string name(_studyEditor->getName(soPres));
861       if (soPres->_is_nil() || name == "MEDCalc")
862         return;
863       int presId = _salomeModule->getIntParamFromStudyEditor(soPres,PRESENTATION_ID);
864       // If fieldId equals -1, then it means that it is not a field
865       // managed by the MED module, and we stop this function process.
866       if ( presId < 0 )
867         continue;
868
869       std::string dump(_presManager->getParavisDump(presId));
870       std::cerr << "#====== ParaVis dump (presentation "  << presId << ") =====" << std::endl;
871       std::cerr << dump;
872       std::cerr << "#====== End of ParaVis dump =============== " << std::endl;
873
874       break; // stop at the first one
875   }
876 }
877
878 void
879 PresentationController::updateTreeViewWithNewPresentation(long dataId, long presentationId)
880 {
881   if (presentationId < 0) {
882       std::cerr << "Unknown presentation\n";
883       return;
884   }
885
886   std::string name(_presManager->getPresentationStringProperty(presentationId, MEDPresentation::PROP_NAME.c_str()));
887   std::string type = name;
888   std::string icon = std::string("ICO_") + type;
889   icon = _getIconName(icon);
890   std::string ico = tr(icon.c_str()).toStdString();
891
892   // Append presentation ID to the displayed name in the OB:
893   std::ostringstream oss;
894   name = tr(name.c_str()).toStdString();
895   oss << name << " (" << presentationId << ")";
896
897   // Mesh views are always registered at the mesh level:
898   if (type == MEDPresentationMeshView::TYPE_NAME)
899     {
900       _salomeModule->engine()->registerPresentationMesh(dataId, oss.str().c_str(), type.c_str(),ico.c_str(), presentationId);
901     }
902   else
903     _salomeModule->engine()->registerPresentationField(dataId, oss.str().c_str(), type.c_str(),ico.c_str(), presentationId);
904
905   // update Object browser
906   _salomeModule->getApp()->updateObjectBrowser(true);
907
908   // auto-select new presentation
909   std::string entry = _salomeModule->engine()->getStudyPresentationEntry(presentationId);
910   SALOME_ListIO selectedObjects;
911   LightApp_Study* lightStudy = dynamic_cast<LightApp_Study*>( _salomeModule->application()->activeStudy() );
912   QString component = lightStudy->componentDataType( entry.c_str() );
913   selectedObjects.Append( new SALOME_InteractiveObject( (const char*)entry.c_str(),
914                                                         (const char*)component.toLatin1(),
915                                                         ""/*refobj->Name().c_str()*/ ) );
916   //QStringList selectedObjects;
917   //selectedObjects << QString(entry.c_str());
918   LightApp_SelectionMgr* aSelectionMgr = _salomeModule->getApp()->selectionMgr();
919   aSelectionMgr->setSelectedObjects(selectedObjects, false);
920
921   // emit onPresentationSelected
922   int presId = -1;
923   _salomeModule->itemClickGeneric(name, type, presId);
924   onPresentationSelected(presId, QString::fromStdString(type), QString::fromStdString(name));
925   updateVisibilityState(presId);
926 }
927
928 void
929 PresentationController::updateTreeViewForPresentationRemoval(long presentationId)
930 {
931   if (presentationId < 0) {
932       std::cerr << "Unknown presentation\n";
933       return;
934   }
935
936   _salomeModule->engine()->unregisterPresentation(presentationId);
937
938   // update Object browser
939   _salomeModule->getApp()->updateObjectBrowser(true);
940 }
941
942 void
943 PresentationController::_dealWithReplaceMode()
944 {
945   // Deal with replace mode: presentations with invalid IDs have to be removed:
946
947   MEDCALC::PresentationsList * lstManager = _presManager->getAllPresentations();
948   MED_ORB::PresentationsList * lstModule = _salomeModule->engine()->getStudyPresentations();
949   // The IDs not in the intersection needs deletion:
950   CORBA::Long * last = lstManager->get_buffer() + lstManager->length();
951   for (unsigned i = 0; i < lstModule->length(); i++) {
952     CORBA::Long * ptr = std::find(lstManager->get_buffer(), last, (*lstModule)[i]);
953     if (ptr == last) {
954       STDLOG("Removing pres " << (*lstModule)[i] << " from OB.");
955       // Presentation in module but not in manager anymore: to be deleted from OB:
956       updateTreeViewForPresentationRemoval((*lstModule)[i]);
957     }
958   }
959 }
960
961 void
962 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
963 {
964   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
965     if (event->dataId == -1) {
966       // A file has been loaded, and we want to create a default presentation (MeshView) for it
967       QString viewMode = getViewModePython();
968       QStringList commands;
969       commands += QString("presentation_id = medcalc.MakeMeshView(medcalc.GetFirstMeshFromDataSource(source_id), viewMode=%1)").arg(viewMode);
970       commands += QString("presentation_id");
971       _consoleDriver->exec(commands);
972     }
973     else {
974       updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
975       _dealWithReplaceMode();
976       // Update parameter widget if shown: some parameters should be updated after presentation has been added
977       if (_currentWidgetHelper)
978         _currentWidgetHelper->updateWidget(false);
979     }
980   }
981   else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) {
982       updateTreeViewForPresentationRemoval(event->presentationId);
983       // Hide parameter widget if necessary:
984       onPresentationSelected(-1, "", "");
985   }
986   else if ( event->type == MEDCALC::EVENT_MODIFY_PRESENTATION ) {
987       // Update parameter widget if shown:
988       if(_currentWidgetHelper)
989         _currentWidgetHelper->updateWidget(false);
990   }
991   else if (event->type == MEDCALC::EVENT_VISIBILITY_CHANGED) {
992     updateVisibilityState(event->presentationId);
993   }
994 }
995
996 void
997 PresentationController::showDockWidgets(bool isVisible)
998 {
999   _dockWidget->setVisible(isVisible);
1000 }
1001
1002 void PresentationController::updateVisibilityState(long presId) 
1003 {
1004   char* str = _salomeModule->engine()->getStudyPresentationEntry(presId);
1005   if (str) {
1006     QStringList entries;
1007     entries.append(str);
1008     _salomeModule->updateVisibilityState(false, entries);
1009   }
1010 }
1011
1012 std::string PresentationController::presentationName2Type(const std::string& name) {
1013   return std::regex_replace(name, std::regex("MEDPresentation"), std::string(""));
1014 }
1015
1016 void PresentationController::resetPVSession()
1017 {
1018   QStringList commands;
1019   commands += QString("pvs.ResetSession()");
1020   _consoleDriver->exec(commands);
1021 }