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