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