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