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