Salome HOME
#18963 Minimize compiler warnings
[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<(int)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       int fieldId = _salomeModule->getIntParamFromStudyEditor(soObj, FIELD_ID);
298       int meshId = _salomeModule->getIntParamFromStudyEditor(soObj, MESH_ID);
299       MEDCALC::FieldHandler* fieldHandler = 0;
300       MEDCALC::MeshHandler* meshHandler = 0;
301
302       // is it a mesh?
303       if (meshId >= 0)
304         {
305           if (eventType != PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW)
306             continue;
307           meshHandler = MEDFactoryClient::getDataManager()->getMeshHandler(meshId);
308         }
309       else
310         {
311           if (fieldId < 0)  // is it a field series?
312             {
313               int fieldSeriesId = _salomeModule->getIntParamFromStudyEditor(soObj, FIELD_SERIES_ID);
314               // If fieldId and fieldSeriesId equals -1, then it means that it is not a field
315               // managed by the MED module, and we stop this function process.
316               if ( fieldSeriesId < 0)
317                   continue;
318
319               // get the current timestamp
320               double timestamp = _salomeModule->getCurrentAnimationTimestamp();
321               // get the field id a the current timestamp
322               fieldId = MEDFactoryClient::getDataManager()->getFieldIdAtTimestamp(fieldSeriesId, timestamp);
323             }
324           fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
325         }
326
327       if ((!fieldHandler) && (!meshHandler)) {
328           QMessageBox::warning(_salomeModule->getApp()->desktop(),
329                                tr("Operation not allowed"),
330                                tr("No field (or mesh) is defined"));
331           return;
332       }
333
334       PresentationEvent* event = new PresentationEvent();
335       event->eventtype = eventType;
336       event->fieldHandler = fieldHandler;
337       event->meshHandler = meshHandler;
338       emit presentationSignal(event); // --> processPresentationEvent()
339   }
340 }
341
342 void
343 PresentationController::onVisualizeMeshView()
344 {
345   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW);
346 }
347
348 void
349 PresentationController::onVisualizeScalarMap()
350 {
351   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
352 }
353
354 void
355 PresentationController::onVisualizeContour()
356 {
357   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
358 }
359
360 void
361 PresentationController::onVisualizeVectorField()
362 {
363   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
364 }
365
366 void
367 PresentationController::onVisualizeSlices()
368 {
369   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
370 }
371
372 void
373 PresentationController::onVisualizeDeflectionShape()
374 {
375   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
376 }
377
378 void
379 PresentationController::onVisualizePointSprite()
380 {
381   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
382 }
383
384 void
385 PresentationController::onDeletePresentation()
386 {
387   // Get the selected objects in the study (SObject)
388   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
389
390   // For each object, emit a signal to the workspace to request pres deletion
391   for (int i=0; i<(int)listOfSObject->size(); i++) {
392       SALOMEDS::SObject_var soPres = listOfSObject->at(i);
393       std::string name(_studyEditor->getName(soPres));
394       if (soPres->_is_nil() || name == "MEDCalc")
395         return;
396       int presId = _salomeModule->getIntParamFromStudyEditor(soPres,PRESENTATION_ID);
397       // If fieldId equals -1, then it means that it is not a field
398       // managed by the MED module, and we stop this function process.
399       if ( presId < 0 )
400         continue;
401
402       PresentationEvent* event = new PresentationEvent();
403       event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION;
404       event->presentationId = presId;
405       emit presentationSignal(event); // --> processPresentationEvent()
406   }
407 }
408
409 QString
410 PresentationController::getViewModePython() const
411 {
412   MEDCALC::ViewModeType viewMode = getSelectedViewMode();
413   switch(viewMode) {
414     case MEDCALC::VIEW_MODE_REPLACE: return "MEDCALC.VIEW_MODE_REPLACE";
415     case MEDCALC::VIEW_MODE_OVERLAP: return "MEDCALC.VIEW_MODE_OVERLAP";
416     case MEDCALC::VIEW_MODE_NEW_LAYOUT: return "MEDCALC.VIEW_MODE_NEW_LAYOUT";
417     case MEDCALC::VIEW_MODE_SPLIT_VIEW: return "MEDCALC.VIEW_MODE_SPLIT_VIEW";
418   }
419   return QString();
420 }
421
422 QString
423 PresentationController::getColorMapPython() const
424 {
425   MEDCALC::ColorMapType colorMap = getSelectedColorMap();
426   switch(colorMap) {
427     case MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW: return "MEDCALC.COLOR_MAP_BLUE_TO_RED_RAINBOW";
428     case MEDCALC::COLOR_MAP_COOL_TO_WARM: return "MEDCALC.COLOR_MAP_COOL_TO_WARM";
429   }
430   return QString();
431 }
432
433 QString
434 PresentationController::getScalarBarRangePython() const
435 {
436   MEDCALC::ScalarBarRangeType colorMap = getSelectedScalarBarRange();
437   switch(colorMap) {
438     case MEDCALC::SCALAR_BAR_ALL_TIMESTEPS: return "MEDCALC.SCALAR_BAR_ALL_TIMESTEPS";
439     case MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP: return "MEDCALC.SCALAR_BAR_CURRENT_TIMESTEP";
440   }
441   return QString();
442 }
443
444 QString
445 PresentationController::getMeshModePython(const int mode) const
446 {
447   MEDCALC::MeshModeType mod = static_cast<MEDCALC::MeshModeType>(mode);
448   switch(mod) {
449     case MEDCALC::MESH_MODE_WIREFRAME:     return "MEDCALC.MESH_MODE_WIREFRAME";
450     case MEDCALC::MESH_MODE_SURFACE:       return "MEDCALC.MESH_MODE_SURFACE";
451     case MEDCALC::MESH_MODE_SURFACE_EDGES: return "MEDCALC.MESH_MODE_SURFACE_EDGES";
452   }
453   return QString();
454 }
455
456 QString
457 PresentationController::getSliceOrientationPython(const int orientation) const
458 {
459   MEDCALC::SliceOrientationType orient = static_cast<MEDCALC::SliceOrientationType>(orientation);
460   switch(orient) {
461     case MEDCALC::SLICE_NORMAL_TO_X:   return "MEDCALC.SLICE_NORMAL_TO_X";
462     case MEDCALC::SLICE_NORMAL_TO_Y:   return "MEDCALC.SLICE_NORMAL_TO_Y";
463     case MEDCALC::SLICE_NORMAL_TO_Z:   return "MEDCALC.SLICE_NORMAL_TO_Z";
464     case MEDCALC::SLICE_NORMAL_TO_XY:  return "MEDCALC.SLICE_NORMAL_TO_XY";
465     case MEDCALC::SLICE_NORMAL_TO_XZ:  return "MEDCALC.SLICE_NORMAL_TO_XZ";
466     case MEDCALC::SLICE_NORMAL_TO_YZ:  return "MEDCALC.SLICE_NORMAL_TO_YZ";
467     case MEDCALC::SLICE_NORMAL_TO_XYZ: return "MEDCALC.SLICE_NORMAL_TO_XYZ";
468   }
469   return QString();
470 }
471
472 std::string
473 PresentationController::getPresTypeFromWidgetHelper(int presId) const
474 {
475   std::map<int, MEDWidgetHelper *>::const_iterator it =_presHelperMap.find(presId);
476   if (it != _presHelperMap.end())
477     return (*it).second->getPythonTag();
478   return "UNKNOWN";
479 }
480
481 void
482 PresentationController::processPresentationEvent(const PresentationEvent* event) {
483   // --> Send commands to SALOME Python console
484   QString viewMode = getViewModePython();
485   QString colorMap = getColorMapPython();
486   QString scalarBarRange = getScalarBarRangePython();
487   MEDCALC::FieldHandler* fieldHandler = event->fieldHandler;
488   QStringList commands;
489
490   // [ABN] using event mechanism for all this is awkward? TODO: direct implementation in each
491   // dedicated widget helper class?
492
493   if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_MESH_VIEW ) {
494       // Do we request mesh view from a field or from a mesh only?
495       int meshId = event->meshHandler ? event->meshHandler->id : event->fieldHandler->meshid;
496       commands += QString("presentation_id = medcalc.MakeMeshView(%1, viewMode=%2)").arg(meshId).arg(viewMode);
497       commands += QString("presentation_id");
498     }
499   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP ) {
500       commands += QString("presentation_id = medcalc.MakeScalarMap(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_CONTOUR ) {
505       commands += QString("presentation_id = medcalc.MakeContour(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
506             .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
507       commands += QString("presentation_id");
508   }
509   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_SLICES ) {
510       commands += QString("presentation_id = medcalc.MakeSlices(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
511             .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
512       commands += QString("presentation_id");
513   }
514   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD ) {
515       commands += QString("presentation_id = medcalc.MakeVectorField(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
516           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
517       commands += QString("presentation_id");
518   }
519   else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE ) {
520       commands += QString("presentation_id = medcalc.MakePointSprite(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
521               .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
522       commands += QString("presentation_id");
523   }
524     else if ( event->eventtype == PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE ) {
525       commands += QString("presentation_id = medcalc.MakeDeflectionShape(accessField(%1), viewMode=%2, scalarBarRange=%3, colorMap=%4)")
526           .arg(fieldHandler->id).arg(viewMode).arg(scalarBarRange).arg(colorMap);
527       commands += QString("presentation_id");
528   }
529
530
531   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_COMPONENT ) {
532       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
533       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
534       commands += QString("params.displayedComponent = '%1'").arg(QString::fromStdString(event->aString));
535       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
536   }
537   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_COLORMAP ) {
538       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
539       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
540       commands += QString("params.colorMap = %1").arg(getColorMapPython());
541       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
542   }
543   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_TIME_RANGE ) {
544       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
545       commands += QString("params = medcalc.Get%1Parameters(%2)").arg(QString::fromStdString(typ)).arg(event->presentationId);
546       commands += QString("params.scalarBarRange = %1").arg(getScalarBarRangePython());
547       commands += QString("medcalc.Update%1(%2, params)").arg(QString::fromStdString(typ)).arg(event->presentationId);
548   }
549   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NB_CONTOUR ) {
550       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
551       commands += QString("params = medcalc.GetContourParameters(%2)").arg(event->presentationId);
552       commands += QString("params.nbContours = %1").arg(event->anInteger);
553       commands += QString("medcalc.UpdateContour(%1, params)").arg(event->presentationId);
554   }
555   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_MESH_MODE ) {
556       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
557       commands += QString("params = medcalc.GetMeshViewParameters(%2)").arg(event->presentationId);
558       commands += QString("params.mode = %1").arg(getMeshModePython(event->anInteger));
559       commands += QString("medcalc.UpdateMeshView(%1, params)").arg(event->presentationId);
560   }
561   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_NB_SLICES ) {
562       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
563       commands += QString("params = medcalc.GetSlicesParameters(%2)").arg(event->presentationId);
564       commands += QString("params.nbSlices = %1").arg(event->anInteger);
565       commands += QString("medcalc.UpdateSlices(%1, params)").arg(event->presentationId);
566   }
567   else if ( event->eventtype == PresentationEvent::EVENT_CHANGE_SLICE_ORIENTATION ) {
568       std::string typ = getPresTypeFromWidgetHelper(event->presentationId);
569       commands += QString("params = medcalc.GetSlicesParameters(%2)").arg(event->presentationId);
570       commands += QString("params.orientation = %1").arg(getSliceOrientationPython(event->anInteger));
571       commands += QString("medcalc.UpdateSlices(%1, params)").arg(event->presentationId);
572   }
573
574   else if ( event->eventtype == PresentationEvent::EVENT_DELETE_PRESENTATION ) {
575       commands += QString("medcalc.RemovePresentation(%1)").arg(event->presentationId);
576   }
577   else {
578       STDLOG("The event "<<event->eventtype<<" is not implemented yet");
579   }
580   _consoleDriver->exec(commands);
581 }
582
583 MEDWidgetHelper *
584 PresentationController::findOrCreateWidgetHelper(MEDCALC::MEDPresentationManager_ptr /*presManager*/,                  // todo: unused
585                                                  int presId, const std::string& type, const std::string& name )
586 {
587   std::map<int, MEDWidgetHelper *>::const_iterator it =_presHelperMap.find(presId);
588   if (it != _presHelperMap.end())
589     return (*it).second;
590   MEDWidgetHelper * wh = 0;
591   if (type == MEDPresentationMeshView::TYPE_NAME)
592     wh = new MEDWidgetHelperMeshView(this, _presManager, presId, name, _widgetPresentationParameters);
593   else if (type == MEDPresentationScalarMap::TYPE_NAME)
594     wh = new MEDWidgetHelperScalarMap(this, _presManager, presId, name, _widgetPresentationParameters);
595   else if (type == MEDPresentationContour::TYPE_NAME)
596     wh = new MEDWidgetHelperContour(this, _presManager, presId, name, _widgetPresentationParameters);
597   else if (type == MEDPresentationSlices::TYPE_NAME)
598     wh = new MEDWidgetHelperSlices(this, _presManager, presId, name, _widgetPresentationParameters);
599   else if (type == MEDPresentationVectorField::TYPE_NAME)
600     wh = new MEDWidgetHelperVectorField(this, _presManager, presId, name, _widgetPresentationParameters);
601   else if (type == MEDPresentationPointSprite::TYPE_NAME)
602     wh = new MEDWidgetHelperPointSprite(this, _presManager, presId, name, _widgetPresentationParameters);
603   else if (type == MEDPresentationDeflectionShape::TYPE_NAME)
604     wh = new MEDWidgetHelperDeflectionShape(this, _presManager, presId, name, _widgetPresentationParameters);
605   else
606     {
607       STDLOG("findOrCreateWidgetHelper(): NOT IMPLEMENTED !!!");
608       return wh;
609     }
610   _presHelperMap[presId] = wh;
611   return wh;
612 }
613
614 void
615 PresentationController::onPresentationSelected(int presId, const QString& presType, const QString& presName)
616 {
617   if (presId == -1)
618     {
619       if (_widgetPresentationParameters->isShown())
620         {
621           _widgetPresentationParameters->toggleWidget(false);
622           if(_currentWidgetHelper)
623             _currentWidgetHelper->releaseWidget();
624         }
625     }
626   else
627     {
628       if(_currentWidgetHelper)
629         _currentWidgetHelper->releaseWidget();
630       // Activate corresponding ParaView render view
631       _presManager->activateView(presId);
632       // Update widgets parameters
633       _currentWidgetHelper = findOrCreateWidgetHelper(_presManager, presId, presType.toStdString(), presName.toStdString());
634       _currentWidgetHelper->updateWidget(true);
635     }
636 }
637
638 void
639 PresentationController::onParavisDump()
640 {
641   // Get the selected objects in the study (SObject)
642   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
643
644   // For the first object only, request the dump
645   for (int i=0; i<(int)listOfSObject->size(); i++) {
646       SALOMEDS::SObject_var soPres = listOfSObject->at(i);
647       std::string name(_studyEditor->getName(soPres));
648       if (soPres->_is_nil() || name == "MEDCalc")
649         return;
650       int presId = _salomeModule->getIntParamFromStudyEditor(soPres,PRESENTATION_ID);
651       // If fieldId equals -1, then it means that it is not a field
652       // managed by the MED module, and we stop this function process.
653       if ( presId < 0 )
654         continue;
655
656       std::string dump(_presManager->getParavisDump(presId));
657       std::cerr << "#====== ParaVis dump (presentation "  << presId << ") =====" << std::endl;
658       std::cerr << dump;
659       std::cerr << "#====== End of ParaVis dump =============== " << std::endl;
660
661       break; // stop at the first one
662   }
663 }
664
665 void
666 PresentationController::updateTreeViewWithNewPresentation(long dataId, long presentationId)
667 {
668   if (presentationId < 0) {
669       std::cerr << "Unknown presentation\n";
670       return;
671   }
672
673   std::string name(_presManager->getPresentationStringProperty(presentationId, MEDPresentation::PROP_NAME.c_str()));
674   std::string type = name;
675   std::string icon = std::string("ICO_") + type;
676   icon = _getIconName(icon);
677   std::string ico = tr(icon.c_str()).toStdString();
678
679   // Append presentation ID to the displayed name in the OB:
680   std::ostringstream oss;
681   name = tr(name.c_str()).toStdString();
682   oss << name << " (" << presentationId << ")";
683
684   // Mesh views are always registered at the mesh level:
685   if (type == MEDPresentationMeshView::TYPE_NAME)
686     {
687       _salomeModule->engine()->registerPresentationMesh(dataId, oss.str().c_str(), type.c_str(),ico.c_str(), presentationId);
688     }
689   else
690     _salomeModule->engine()->registerPresentationField(dataId, oss.str().c_str(), type.c_str(),ico.c_str(), presentationId);
691
692   // update Object browser
693   _salomeModule->getApp()->updateObjectBrowser(true);
694
695   // auto-select new presentation
696   std::string entry = _salomeModule->engine()->getStudyPresentationEntry(presentationId);
697   SALOME_ListIO selectedObjects;
698   LightApp_Study* lightStudy = dynamic_cast<LightApp_Study*>( _salomeModule->application()->activeStudy() );
699   QString component = lightStudy->componentDataType( entry.c_str() );
700   selectedObjects.Append( new SALOME_InteractiveObject( (const char*)entry.c_str(),
701                                                         (const char*)component.toLatin1(),
702                                                         ""/*refobj->Name().c_str()*/ ) );
703   //QStringList selectedObjects;
704   //selectedObjects << QString(entry.c_str());
705   LightApp_SelectionMgr* aSelectionMgr = _salomeModule->getApp()->selectionMgr();
706   aSelectionMgr->setSelectedObjects(selectedObjects, false);
707
708   // emit onPresentationSelected
709   int presId = -1;
710   _salomeModule->itemClickGeneric(name, type, presId);
711   onPresentationSelected(presId, QString::fromStdString(type), QString::fromStdString(name));
712 }
713
714 void
715 PresentationController::updateTreeViewForPresentationRemoval(long presentationId)
716 {
717   if (presentationId < 0) {
718       std::cerr << "Unknown presentation\n";
719       return;
720   }
721
722   _salomeModule->engine()->unregisterPresentation(presentationId);
723
724   // update Object browser
725   _salomeModule->getApp()->updateObjectBrowser(true);
726 }
727
728 void
729 PresentationController::_dealWithReplaceMode()
730 {
731   // Deal with replace mode: presentations with invalid IDs have to be removed:
732
733   MEDCALC::PresentationsList * lstManager = _presManager->getAllPresentations();
734   MED_ORB::PresentationsList * lstModule = _salomeModule->engine()->getStudyPresentations();
735   // The IDs not in the intersection needs deletion:
736   CORBA::Long * last = lstManager->get_buffer() + lstManager->length();
737   for (unsigned i = 0; i < lstModule->length(); i++) {
738     CORBA::Long * ptr = std::find(lstManager->get_buffer(), last, (*lstModule)[i]);
739     if (ptr == last) {
740       STDLOG("Removing pres " << (*lstModule)[i] << " from OB.");
741       // Presentation in module but not in manager anymore: to be deleted from OB:
742       updateTreeViewForPresentationRemoval((*lstModule)[i]);
743     }
744   }
745 }
746
747 void
748 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
749 {
750   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
751     if (event->dataId == -1) {
752       // A file has been loaded, and we want to create a default presentation (MeshView) for it
753       QString viewMode = getViewModePython();
754       QStringList commands;
755       commands += QString("presentation_id = medcalc.MakeMeshView(medcalc.GetFirstMeshFromDataSource(source_id), viewMode=%1)").arg(viewMode);
756       commands += QString("presentation_id");
757       _consoleDriver->exec(commands);
758     }
759     else {
760       updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
761       _dealWithReplaceMode();
762     }
763   }
764   else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) {
765       updateTreeViewForPresentationRemoval(event->presentationId);
766       // Hide parameter widget if necessary:
767       onPresentationSelected(-1, "", "");
768   }
769   else if ( event->type == MEDCALC::EVENT_MODIFY_PRESENTATION ) {
770       // Update parameter widget if shown:
771       if(_currentWidgetHelper)
772         _currentWidgetHelper->updateWidget(false);
773   }
774 }
775
776 void
777 PresentationController::showDockWidgets(bool isVisible)
778 {
779   _dockWidget->setVisible(isVisible);
780 }