Salome HOME
[MEDCalc] Multiple slices + other minor imps:
[modules/med.git] / src / MEDCalc / gui / PresentationController.cxx
1 // Copyright (C) 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 #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 "MEDPresentationDeflectionShape.hxx"
37
38 #include "MEDWidgetHelperMeshView.hxx"
39 #include "MEDWidgetHelperScalarMap.hxx"
40 #include "MEDWidgetHelperContour.hxx"
41 #include "MEDWidgetHelperSlices.hxx"
42 #include "MEDWidgetHelperPointSprite.hxx"
43 #include "MEDWidgetHelperVectorField.hxx"
44 //#include "MEDWidgetHelperDeflectionShape.hxx"
45
46 #include <SalomeApp_Application.h>
47 #include <SalomeApp_Study.h>
48 #include <SalomeApp_DataObject.h>
49
50 #include <SALOMEDS_SObject.hxx>
51 #include <SALOMEDS_Study.hxx>
52
53 #include <SUIT_Desktop.h>
54 #include <SUIT_Session.h>
55 #include <SUIT_ResourceMgr.h>
56 #include <QMessageBox>
57 #include <sstream>
58
59 #include "MEDFactoryClient.hxx"
60
61 static const int OPTIONS_VIEW_MODE_ID = 943;
62 static const int OPTIONS_VIEW_MODE_REPLACE_ID = 944;
63 static const int OPTIONS_VIEW_MODE_OVERLAP_ID = 945;
64 static const int OPTIONS_VIEW_MODE_NEW_LAYOUT_ID = 946;
65 static const int OPTIONS_VIEW_MODE_SPLIT_VIEW_ID = 947;
66
67 //! The only instance of the MEDPresentationManager
68 MEDCALC::MEDPresentationManager_ptr PresentationController::_presManager;
69
70 PresentationController::PresentationController(MEDModule* salomeModule) :
71         _salomeModule(salomeModule),
72         _consoleDriver(0),
73         _studyEditor(salomeModule->getStudyEditor()),
74         _presHelperMap(),
75         _currentWidgetHelper(0)
76 {
77   STDLOG("Creating a PresentationController");
78
79   _widgetPresentationParameters = new WidgetPresentationParameters();
80
81   QMainWindow* parent = salomeModule->getApp()->desktop();
82   _dockWidget = new QDockWidget(parent);
83   _dockWidget->setVisible(false);
84   _dockWidget->setWindowTitle(tr("TITLE_PRESENTATION_PARAMETERS"));
85   _dockWidget->setObjectName(tr("TITLE_PRESENTATION_PARAMETERS"));
86   _dockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
87   _dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
88   _dockWidget->setWidget(_widgetPresentationParameters);
89   parent->addDockWidget(Qt::LeftDockWidgetArea, _dockWidget);
90   //_dockWidget->show();
91
92   // Retrieve MEDFactory to get MEDPresentationManager (sometimes GUI needs to talk to the engine directly)
93   if ( ! _presManager ) {
94       _presManager = MEDFactoryClient::getFactory()->getPresentationManager();
95   }
96
97   // Connect to the click in the object browser
98   connect(salomeModule, SIGNAL( presentationSelected(int , const QString&, const QString&) ),
99           this, SLOT(onPresentationSelected(int , const QString&, const QString&) )     );
100 }
101
102 PresentationController::~PresentationController()
103 {
104   STDLOG("Deleting the resentationController");
105   // Clean allocated widget helpers:
106   for ( std::map<int, MEDWidgetHelper *>::iterator it = _presHelperMap.begin(); it != _presHelperMap.end(); ++it)
107     delete((*it).second);
108 }
109
110 /**
111  * [ABN] Created this probably because I don't know the right way to deal with non existent
112  * attributes in an object from the study ...
113  */
114 int
115 PresentationController::getIntParamFromStudyEditor(SALOMEDS::SObject_var obj, const char * name)
116 {
117   int theInt = -1;
118   try {
119       theInt = _studyEditor->getParameterInt(obj,name);
120   }
121   catch(...)  {  }
122   return theInt;
123 }
124
125 std::string
126 PresentationController::_getIconName(const std::string& name)
127 {
128   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
129   if (!mgr)
130     return name;
131
132   // Read value from preferences and suffix name to select icon theme
133   int theme = mgr->integerValue("MEDCalc", "icons");
134   if (theme == 0) {
135       return name + "_MODERN";
136   } else if (theme == 1) {
137       return name + "_CLASSIC";
138   }
139   return name + "_DEFAULT";
140 }
141
142 void
143 PresentationController::createActions()
144 {
145   STDLOG("Creating PresentationController actions");
146
147   // View Mode
148   int viewModeToolbarId = _salomeModule->createTool("View Mode", "ViewModeToolbar");
149   QtxActionGroup* ag = _salomeModule->createActionGroup(OPTIONS_VIEW_MODE_ID, true);
150   ag->setText("View mode");
151   ag->setUsesDropDown(true);
152   QString label   = tr("LAB_VIEW_MODE_REPLACE");
153   QString tooltip = tr("TIP_VIEW_MODE_REPLACE");
154   QAction* a = _salomeModule->createAction(OPTIONS_VIEW_MODE_REPLACE_ID,label,QIcon(),label,tooltip,0);
155   a->setCheckable(true);
156   a->setChecked(true);
157   ag->add(a);
158
159   label   = tr("LAB_VIEW_MODE_OVERLAP");
160   tooltip = tr("TIP_VIEW_MODE_OVERLAP");
161   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_OVERLAP_ID,label,QIcon(),label,tooltip,0);
162   a->setCheckable(true);
163   ag->add(a);
164
165   label   = tr("LAB_VIEW_MODE_NEW_LAYOUT");
166   tooltip = tr("TIP_VIEW_MODE_NEW_LAYOUT");
167   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID,label,QIcon(),label,tooltip,0);
168   a->setCheckable(true);
169   ag->add(a);
170
171   label   = tr("LAB_VIEW_MODE_SPLIT_VIEW");
172   tooltip = tr("TIP_VIEW_MODE_SPLIT_VIEW");
173   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID,label,QIcon(),label,tooltip,0);
174   a->setCheckable(true);
175   ag->add(a);
176
177   _salomeModule->createTool(OPTIONS_VIEW_MODE_ID, viewModeToolbarId);
178
179   // Presentations
180   int presentationToolbarId = _salomeModule->createTool("Presentations", "PresentationToolbar");
181   int presentationMenuId = _salomeModule->createMenu(tr("MENU_PRESENTATIONS"), -1, 1);
182
183   label   = tr("LAB_PRESENTATION_MESH_VIEW");
184   tooltip = tr("TIP_PRESENTATION_MESH_VIEW");
185   QString icon = tr(_getIconName("ICO_PRESENTATION_MESH_VIEW").c_str());
186   int actionId;
187   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeMeshView()),icon,tooltip);
188   _salomeModule->createTool(actionId, presentationToolbarId);
189   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
190   _salomeModule->createMenu(actionId, presentationMenuId);
191
192   label   = tr("LAB_PRESENTATION_SCALAR_MAP");
193   tooltip = tr("TIP_PRESENTATION_SCALAR_MAP");
194   icon = tr(_getIconName("ICO_PRESENTATION_SCALAR_MAP").c_str());
195   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeScalarMap()),icon,tooltip);
196   _salomeModule->createTool(actionId, presentationToolbarId);
197   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
198   _salomeModule->createMenu(actionId, presentationMenuId);
199
200   label   = tr("LAB_PRESENTATION_CONTOUR");
201   tooltip = tr("TIP_PRESENTATION_CONTOUR");
202   icon    = tr(_getIconName("ICO_PRESENTATION_CONTOUR").c_str());
203   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeContour()),icon,tooltip);
204   _salomeModule->createTool(actionId, presentationToolbarId);
205   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
206   _salomeModule->createMenu(actionId, presentationMenuId);
207
208   label   = tr("LAB_PRESENTATION_VECTOR_FIELD");
209   tooltip = tr("TIP_PRESENTATION_VECTOR_FIELD");
210   icon    = tr(_getIconName("ICO_PRESENTATION_VECTOR_FIELD").c_str());
211   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeVectorField()),icon,tooltip);
212   _salomeModule->createTool(actionId, presentationToolbarId);
213   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
214   _salomeModule->createMenu(actionId, presentationMenuId);
215
216   label   = tr("LAB_PRESENTATION_SLICES");
217   tooltip = tr("TIP_PRESENTATION_SLICES");
218   icon    = tr(_getIconName("ICO_PRESENTATION_SLICES").c_str());
219   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeSlices()),icon,tooltip);
220   _salomeModule->createTool(actionId, presentationToolbarId);
221   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
222   _salomeModule->createMenu(actionId, presentationMenuId);
223
224   label   = tr("LAB_PRESENTATION_DEFLECTION_SHAPE");
225   tooltip = tr("TIP_PRESENTATION_DEFLECTION_SHAPE");
226   icon    = tr(_getIconName("ICO_PRESENTATION_DEFLECTION_SHAPE").c_str());
227   actionId = _salomeModule->createStandardAction(label,this, SLOT(onVisualizeDeflectionShape()),icon,tooltip);
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()),icon,tooltip);
236   _salomeModule->createTool(actionId, presentationToolbarId);
237   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
238   _salomeModule->createMenu(actionId, presentationMenuId);
239
240   // Separator
241   _salomeModule->createMenu(_salomeModule->separator(), presentationMenuId);
242
243   label   = tr("LAB_DELETE_PRESENTATION");
244   tooltip = tr("TIP_DELETE_PRESENTATION");
245   icon    = tr(_getIconName("ICO_DELETE_PRESENTATION").c_str());
246   actionId = _salomeModule->createStandardAction(label,this, SLOT(onDeletePresentation()),icon,tooltip);
247   //  _salomeModule->createTool(actionId, presentationToolbarId);
248   //  _salomeModule->action(actionId)->setIconVisibleInMenu(true);
249   _salomeModule->createMenu(actionId, presentationMenuId);
250
251   //
252   // Actions for popup menu only
253   //
254   // Low level PARAVIS dump
255   label = tr("LAB_PARAVIS_DUMP");
256   //icon  = tr("ICO_DATASOURCE_EXPAND_FIELD");
257   actionId = _salomeModule->createStandardAction(label,this,SLOT(onParavisDump()),"");
258   _salomeModule->addActionInPopupMenu(actionId);
259
260
261 }
262
263 MEDCALC::ViewModeType
264 PresentationController::getSelectedViewMode() const
265 {
266   if (_salomeModule->action(OPTIONS_VIEW_MODE_REPLACE_ID)->isChecked()) {
267       return MEDCALC::VIEW_MODE_REPLACE;
268   }
269   else if (_salomeModule->action(OPTIONS_VIEW_MODE_OVERLAP_ID)->isChecked()) {
270       return MEDCALC::VIEW_MODE_OVERLAP;
271   }
272   else if (_salomeModule->action(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID)->isChecked()) {
273       return MEDCALC::VIEW_MODE_NEW_LAYOUT;
274   }
275   else if (_salomeModule->action(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID)->isChecked()) {
276       return MEDCALC::VIEW_MODE_SPLIT_VIEW;
277   }
278   // Should not happen
279   STDLOG("Strange!! No matching view mode found - returning VIEW_MODE_REPLACE.");
280   return MEDCALC::VIEW_MODE_REPLACE;
281 }
282
283 MEDCALC::ColorMapType
284 PresentationController::getSelectedColorMap() const
285 {
286   return _widgetPresentationParameters->getColorMap();
287 }
288
289 MEDCALC::ScalarBarRangeType
290 PresentationController::getSelectedScalarBarRange() const
291 {
292   return _widgetPresentationParameters->getScalarBarRange();
293 }
294
295 void
296 PresentationController::visualize(PresentationEvent::EventType eventType)
297 {
298   // We need a _studyEditor updated on the active study
299   _studyEditor->updateActiveStudy();
300
301   // Get the selected objects in the study (SObject)
302   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
303
304   // For each object, emit a signal to the workspace to request a
305   // visualisation using the tui command (so that the user can see how
306   // to make a view of an object from the tui console).
307   for (int i=0; i<listOfSObject->size(); i++) {
308       SALOMEDS::SObject_var soField = listOfSObject->at(i);
309       int fieldId = getIntParamFromStudyEditor(soField, FIELD_ID);
310       if (fieldId < 0)  // is it a field serie ?
311         {
312           int fieldSeriesId = getIntParamFromStudyEditor(soField, FIELD_SERIES_ID);
313           // If fieldId and fieldSeriesId equals -1, then it means that it is not a field
314           // managed by the MED module, and we stop this function process.
315           if ( fieldSeriesId < 0)
316             continue;
317           MEDCALC::FieldHandlerList* fieldHandlerList = MEDFactoryClient::getDataManager()->getFieldListInFieldseries(fieldSeriesId);
318           if (fieldHandlerList->length() < 0)
319             continue;
320           // For a field series, get the first real field entry:
321           MEDCALC::FieldHandler fieldHandler = (*fieldHandlerList)[0];
322           fieldId = fieldHandler.id;
323         }
324
325       MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
326       if (! fieldHandler) {
327           QMessageBox::warning(_salomeModule->getApp()->desktop(),
328                                tr("Operation not allowed"),
329                                tr("No field is defined"));
330           return;
331       }
332
333       PresentationEvent* event = new PresentationEvent();
334       event->eventtype = eventType;
335       event->fieldHandler = fieldHandler;
336       emit presentationSignal(event); // --> processPresentationEvent()
337   }
338 }
339
340 void
341 PresentationController::onVisualizeMeshView()
342 {
343   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW);
344 }
345
346 void
347 PresentationController::onVisualizeScalarMap()
348 {
349   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
350 }
351
352 void
353 PresentationController::onVisualizeContour()
354 {
355   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
356 }
357
358 void
359 PresentationController::onVisualizeVectorField()
360 {
361   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
362 }
363
364 void
365 PresentationController::onVisualizeSlices()
366 {
367   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
368 }
369
370 void
371 PresentationController::onVisualizeDeflectionShape()
372 {
373   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
374 }
375
376 void
377 PresentationController::onVisualizePointSprite()
378 {
379   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
380 }
381
382 void
383 PresentationController::onDeletePresentation()
384 {
385   // We need a _studyEditor updated on the active study
386   _studyEditor->updateActiveStudy();
387
388   // Get the selected objects in the study (SObject)
389   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
390
391   // For each object, emit a signal to the workspace to request pres deletion
392   for (int i=0; i<listOfSObject->size(); i++) {
393       SALOMEDS::SObject_var soPres = listOfSObject->at(i);
394       int presId = getIntParamFromStudyEditor(soPres,PRESENTATION_ID);
395       // If fieldId equals -1, then it means that it is not a field
396       // managed by the MED module, and we stop this function process.
397       if ( presId < 0 )
398         continue;
399
400       PresentationEvent* event = new PresentationEvent();
401       event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION;
402       event->presentationId = presId;
403       emit presentationSignal(event); // --> processPresentationEvent()
404   }
405 }
406
407 QString
408 PresentationController::getViewModePython() const
409 {
410   MEDCALC::ViewModeType viewMode = getSelectedViewMode();
411   switch(viewMode) {
412     case MEDCALC::VIEW_MODE_REPLACE: return "MEDCALC.VIEW_MODE_REPLACE";
413     case MEDCALC::VIEW_MODE_OVERLAP: return "MEDCALC.VIEW_MODE_OVERLAP";
414     case MEDCALC::VIEW_MODE_NEW_LAYOUT: return "MEDCALC.VIEW_MODE_NEW_LAYOUT";
415     case MEDCALC::VIEW_MODE_SPLIT_VIEW: return "MEDCALC.VIEW_MODE_SPLIT_VIEW";
416   }
417   return QString();
418 }
419
420 QString
421 PresentationController::getColorMapPython() const
422 {
423   MEDCALC::ColorMapType colorMap = getSelectedColorMap();
424   switch(colorMap) {
425     case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "MEDCALC.COLOR_MAP_BLUE_TO_RED_RAINBOW";
426     case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "MEDCALC.COLOR_MAP_COOL_TO_WARM";
427   }
428   return QString();
429 }
430
431 QString
432 PresentationController::getScalarBarRangePython() const
433 {
434   MEDCALC::ScalarBarRangeType colorMap = getSelectedScalarBarRange();
435   switch(colorMap) {
436     case MEDCALC::SCALAR_BAR_ALL_TIMESTEPS: return "MEDCALC.SCALAR_BAR_ALL_TIMESTEPS";
437     case MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP: return "MEDCALC.SCALAR_BAR_CURRENT_TIMESTEP";
438   }
439   return QString();
440 }
441
442 QString
443 PresentationController::getMeshModePython(const int mode) const
444 {
445   MEDCALC::MeshModeType mod = static_cast<MEDCALC::MeshModeType>(mode);
446   switch(mod) {
447     case MEDCALC::MESH_MODE_WIREFRAME:     return "MEDCALC.MESH_MODE_WIREFRAME";
448     case MEDCALC::MESH_MODE_SURFACE:       return "MEDCALC.MESH_MODE_SURFACE";
449     case MEDCALC::MESH_MODE_SURFACE_EDGES: return "MEDCALC.MESH_MODE_SURFACE_EDGES";
450   }
451   return QString();
452 }
453
454 QString
455 PresentationController::getSliceOrientationPython(const int orientation) const
456 {
457   MEDCALC::SliceOrientationType orient = static_cast<MEDCALC::SliceOrientationType>(orientation);
458   switch(orient) {
459     case MEDCALC::SLICE_NORMAL_TO_X:   return "MEDCALC.SLICE_NORMAL_TO_X";
460     case MEDCALC::SLICE_NORMAL_TO_Y:   return "MEDCALC.SLICE_NORMAL_TO_Y";
461     case MEDCALC::SLICE_NORMAL_TO_Z:   return "MEDCALC.SLICE_NORMAL_TO_Z";
462     case MEDCALC::SLICE_NORMAL_TO_XY:  return "MEDCALC.SLICE_NORMAL_TO_XY";
463     case MEDCALC::SLICE_NORMAL_TO_XZ:  return "MEDCALC.SLICE_NORMAL_TO_XZ";
464     case MEDCALC::SLICE_NORMAL_TO_YZ:  return "MEDCALC.SLICE_NORMAL_TO_YZ";
465     case MEDCALC::SLICE_NORMAL_TO_XYZ: return "MEDCALC.SLICE_NORMAL_TO_XYZ";
466   }
467   return QString();
468 }
469
470 std::string
471 PresentationController::getPresTypeFromWidgetHelper(int presId) const
472 {
473   std::map<int, MEDWidgetHelper *>::const_iterator it =_presHelperMap.find(presId);
474   if (it != _presHelperMap.end())
475     return (*it).second->getPythonTag();
476   return "UNKNOWN";
477 }
478
479 void
480 PresentationController::processPresentationEvent(const PresentationEvent* event) {
481   // --> Send commands to SALOME Python console
482   QString viewMode = getViewModePython();
483   QString colorMap = getColorMapPython();
484   QString scalarBarRange = getScalarBarRangePython();
485   MEDCALC::FieldHandler* fieldHandler = event->fieldHandler;
486   QStringList commands;
487   if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW ) {
488         commands += QString("presentation_id = medcalc.MakeMeshView(accessField(%1), viewMode=%2)").arg(fieldHandler->id).arg(viewMode);
489         commands += QString("presentation_id");
490     }
491   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP ) {
492       commands += QString("presentation_id = medcalc.MakeScalarMap(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
493           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
494       commands += QString("presentation_id");
495   }
496   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR ) {
497       commands += QString("presentation_id = medcalc.MakeContour(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
498             .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
499       commands += QString("presentation_id");
500   }
501   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_SLICES ) {
502       commands += QString("presentation_id = medcalc.MakeSlices(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
503             .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
504       commands += QString("presentation_id");
505   }
506   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD ) {
507       commands += QString("presentation_id = medcalc.MakeVectorField(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
508           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
509       commands += QString("presentation_id");
510   }
511   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE ) {
512       commands += QString("presentation_id = medcalc.MakePointSprite(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
513               .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
514       commands += QString("presentation_id");
515   }
516
517   //  else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE ) {
518   //    commands += QString("presentation_id = medcalc.MakeDeflectionShape(accessField(%1), %2)").arg(fieldHandler->id).arg(viewMode);
519   //    commands += QString("presentation_id");
520
521   // [ABN] using event mechanism for this is awkward? TODO: direct implementation in each
522   // dedicated widget helper class?
523   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_COMPONENT ) {
524       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
525       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
526       commands += QString("params.displayedComponent = '%1'").arg(QString::fromStdString(event->aString));
527       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
528   }
529   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_COLORMAP ) {
530       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
531       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
532       commands += QString("params.colorMap = %1").arg(getColorMapPython());
533       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
534   }
535   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_TIME_RANGE ) {
536       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
537       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
538       commands += QString("params.scalarBarRange = %1").arg(getScalarBarRangePython());
539       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
540   }
541   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NB_CONTOUR ) {
542       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
543       commands += QString("params = medcalc.GetContourParameters(%2)").arg(event->presentationId);
544       commands += QString("params.nbContours = %1").arg(event->anInteger);
545       commands += QString("medcalc.UpdateContour(%1, params)").arg(event->presentationId);
546   }
547   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_MESH_MODE ) {
548       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
549       commands += QString("params = medcalc.GetMeshViewParameters(%2)").arg(event->presentationId);
550       commands += QString("params.mode = %1").arg(getMeshModePython(event->anInteger));
551       commands += QString("medcalc.UpdateMeshView(%1, params)").arg(event->presentationId);
552   }
553   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NB_SLICES ) {
554       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
555       commands += QString("params = medcalc.GetSlicesParameters(%2)").arg(event->presentationId);
556       commands += QString("params.nbSlices = %1").arg(event->anInteger);
557       commands += QString("medcalc.UpdateSlices(%1, params)").arg(event->presentationId);
558   }
559   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_SLICE_ORIENTATION ) {
560       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
561       commands += QString("params = medcalc.GetSlicesParameters(%2)").arg(event->presentationId);
562       commands += QString("params.orientation = %1").arg(getSliceOrientationPython(event->anInteger));
563       commands += QString("medcalc.UpdateSlices(%1, params)").arg(event->presentationId);
564   }
565
566   else if ( event->eventtype == PresentationEvent::EVENT_DELETE_PRESENTATION ) {
567       commands += QString("medcalc.RemovePresentation(%1)").arg(event->presentationId);
568   }
569   else {
570       STDLOG("The event "<<event->eventtype<<" is not implemented yet");
571   }
572   _consoleDriver->exec(commands);
573 }
574
575 MEDWidgetHelper *
576 PresentationController::findOrCreateWidgetHelper(MEDCALC::MEDPresentationManager_ptr presManager,
577                                                  int presId, const std::string& type, const std::string& name )
578 {
579   std::map<int, MEDWidgetHelper *>::const_iterator it =_presHelperMap.find(presId);
580   if (it != _presHelperMap.end())
581     return (*it).second;
582   MEDWidgetHelper * wh = 0;
583   if (type == MEDPresentationMeshView::TYPE_NAME)
584     wh = new MEDWidgetHelperMeshView(this, _presManager, presId, name, _widgetPresentationParameters);
585   else if (type == MEDPresentationScalarMap::TYPE_NAME)
586     wh = new MEDWidgetHelperScalarMap(this, _presManager, presId, name, _widgetPresentationParameters);
587   else if (type == MEDPresentationContour::TYPE_NAME)
588     wh = new MEDWidgetHelperContour(this, _presManager, presId, name, _widgetPresentationParameters);
589   else if (type == MEDPresentationSlices::TYPE_NAME)
590     wh = new MEDWidgetHelperSlices(this, _presManager, presId, name, _widgetPresentationParameters);
591   else if (type == MEDPresentationVectorField::TYPE_NAME)
592     wh = new MEDWidgetHelperVectorField(this, _presManager, presId, name, _widgetPresentationParameters);
593   else if (type == MEDPresentationPointSprite::TYPE_NAME)
594     wh = new MEDWidgetHelperPointSprite(this, _presManager, presId, name, _widgetPresentationParameters);
595   else
596     {
597       const char * msg ="findOrCreateWidgetHelper(): NOT IMPLEMENTED !!!";
598       STDLOG(msg);
599     }
600   _presHelperMap[presId] = wh;
601   return wh;
602 }
603
604 void
605 PresentationController::onPresentationSelected(int presId, const QString& presType, const QString& presName)
606 {
607   if (presId == -1)
608     {
609       if (_widgetPresentationParameters->isShown())
610         {
611           _widgetPresentationParameters->toggleWidget(false);
612           if(_currentWidgetHelper)
613             _currentWidgetHelper->releaseWidget();
614         }
615     }
616   else
617     {
618       // Activate corresponding ParaView render view
619       _presManager->activateView(presId);
620       // Update widgets parameters
621       _currentWidgetHelper = findOrCreateWidgetHelper(_presManager, presId, presType.toStdString(), presName.toStdString());
622       _currentWidgetHelper->updateWidget(true);
623     }
624 }
625
626 void
627 PresentationController::onParavisDump()
628 {
629   // We need a _studyEditor updated on the active study
630   _studyEditor->updateActiveStudy();
631
632   // Get the selected objects in the study (SObject)
633   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
634
635   // For the first object only, request the dump
636   for (int i=0; i<listOfSObject->size(); i++) {
637       SALOMEDS::SObject_var soPres = listOfSObject->at(i);
638       int presId = getIntParamFromStudyEditor(soPres,PRESENTATION_ID);
639       // If fieldId equals -1, then it means that it is not a field
640       // managed by the MED module, and we stop this function process.
641       if ( presId < 0 )
642         continue;
643
644       std::string dump(_presManager->getParavisDump(presId));
645       std::cerr << "#====== ParaVis dump =============== " << std::endl;
646       std::cerr << dump;
647       std::cerr << "#====== End of ParaVis dump ======== " << std::endl;
648
649       break; // stop at the first one
650   }
651 }
652
653 void
654 PresentationController::updateTreeViewWithNewPresentation(long fieldId, long presentationId)
655 {
656   if (presentationId < 0) {
657       std::cerr << "Unknown presentation\n";
658       return;
659   }
660
661   std::string name(_presManager->getPresentationStringProperty(presentationId, MEDPresentation::PROP_NAME.c_str()));
662   std::string type = name;
663   std::string icon = std::string("ICO_") + type;
664   icon = _getIconName(icon);
665   std::string ico = tr(icon.c_str()).toStdString();
666
667   // Append presentation ID to the displayed name in the OB:
668   std::ostringstream oss;
669   name = tr(name.c_str()).toStdString();
670   oss << name << " (" << presentationId << ")";
671
672   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
673   _PTR(Study) studyDS = study->studyDS();
674
675   _salomeModule->engine()->registerPresentation(_CAST(Study, studyDS)->GetStudy(), fieldId,
676       oss.str().c_str(), type.c_str(),ico.c_str(), presentationId);
677
678
679   //  MEDCALC::ViewModeType viewMode = MEDFactoryClient::getPresentationManager()->getPresentationViewMode(presentationId);
680   //
681   //  // Remove sibling presentations if view mode is set to REPLACE
682   //  if (viewMode == MEDCALC::VIEW_MODE_REPLACE) {
683   //    MED_ORB::PresentationsList* presList = _salomeModule->engine()->getSiblingPresentations(_CAST(Study, studyDS)->GetStudy(), presentationId);
684   //    CORBA::ULong size = presList->length();
685   //
686   //    std::stringstream sstm;
687   //    sstm << "Removing sibling presentation(s): ";
688   //    for (int i = 0; i < size; ++i)
689   //      sstm << (*presList)[i] << "  ";
690   //    STDLOG(sstm.str());
691   //
692   //    for (int i = 0; i < size; ++i) {
693   //      PresentationEvent* event = new PresentationEvent();
694   //      event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION;
695   //      XmedDataObject* dataObject = new XmedDataObject();
696   //      dataObject->setPresentationId((*presList)[i]);
697   //      event->objectdata = dataObject;
698   //      emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent
699   //    }
700   //
701   //    delete presList;
702   //  }
703
704   // update Object browser
705   _salomeModule->getApp()->updateObjectBrowser(true);
706 }
707
708 void
709 PresentationController::updateTreeViewForPresentationRemoval(long presentationId)
710 {
711   if (presentationId < 0) {
712       std::cerr << "Unknown presentation\n";
713       return;
714   }
715
716   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
717   _PTR(Study) studyDS = study->studyDS();
718
719   _salomeModule->engine()->unregisterPresentation(_CAST(Study, studyDS)->GetStudy(), presentationId);
720
721   // update Object browser
722   _salomeModule->getApp()->updateObjectBrowser(true);
723 }
724
725 void
726 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
727 {
728   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
729     updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
730     // Deal with replace mode: presentations with invalid IDs have to be removed:
731     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
732     _PTR(Study) studyDS = study->studyDS();
733
734     MEDCALC::PresentationsList * lstManager = _presManager->getAllPresentations();
735     MED_ORB::PresentationsList * lstModule = _salomeModule->engine()->getStudyPresentations(_CAST(Study, studyDS)->GetStudy());
736     // The IDs not in the intersection needs deletion:
737     CORBA::Long * last = lstManager->get_buffer() + lstManager->length();
738     for (unsigned i = 0; i < lstModule->length(); i++)
739       {
740         CORBA::Long * ptr = std::find(lstManager->get_buffer(), last, (*lstModule)[i]);
741         if (ptr == last)
742           {
743             STDLOG("Removing pres " << (*lstModule)[i] << " from OB.");
744           // Presentation in module but not in manager anymore: to be deleted from OB:
745           updateTreeViewForPresentationRemoval((*lstModule)[i]);
746           }
747       }
748   }
749   else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) {
750       updateTreeViewForPresentationRemoval(event->presentationId);
751   }
752   else if ( event->type == MEDCALC::EVENT_MODIFY_PRESENTATION ) {
753       // Update parameter widget if shown:
754       if(_currentWidgetHelper)
755         _currentWidgetHelper->updateWidget(false);
756   }
757 }
758
759 void
760 PresentationController::showDockWidgets(bool isVisible)
761 {
762   _dockWidget->setVisible(isVisible);
763 }
764