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