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