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