Salome HOME
[MEDCalc]: deletion of an existing 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
28 #include <SalomeApp_Application.h>
29 #include <SalomeApp_Study.h>
30 #include <SalomeApp_DataObject.h>
31
32 #include <SALOMEDS_SObject.hxx>
33 #include <SALOMEDS_Study.hxx>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38 #include <QMessageBox>
39
40 static const int OPTIONS_VIEW_MODE_ID = 943;
41 static const int OPTIONS_VIEW_MODE_REPLACE_ID = 944;
42 static const int OPTIONS_VIEW_MODE_OVERLAP_ID = 945;
43 static const int OPTIONS_VIEW_MODE_NEW_LAYOUT_ID = 946;
44 static const int OPTIONS_VIEW_MODE_SPLIT_VIEW_ID = 947;
45
46 PresentationController::PresentationController(MEDModule* salomeModule) :
47     _salomeModule(salomeModule),
48     _studyEditor(salomeModule->getStudyEditor())
49 {
50   STDLOG("Creating a PresentationController");
51
52   _widgetPresentationParameters = new WidgetPresentationParameters();
53
54   QMainWindow* parent = salomeModule->getApp()->desktop();
55   _dockWidget = new QDockWidget(parent);
56   _dockWidget->setVisible(false);
57   _dockWidget->setWindowTitle(tr("TITLE_PRESENTATION_PARAMETERS"));
58   _dockWidget->setObjectName(tr("TITLE_PRESENTATION_PARAMETERS"));
59   _dockWidget->setFeatures(QDockWidget::AllDockWidgetFeatures);
60   _dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
61   _dockWidget->setWidget(_widgetPresentationParameters);
62   parent->addDockWidget(Qt::LeftDockWidgetArea, _dockWidget);
63   //_dockWidget->show();
64 }
65
66 PresentationController::~PresentationController()
67 {
68   STDLOG("Deleting the PresentationController");
69 }
70
71 std::string
72 PresentationController::_getIconName(const std::string& name)
73 {
74   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
75   if (!mgr)
76     return name;
77
78   // Read value from preferences and suffix name to select icon theme
79   int theme = mgr->integerValue("MEDCalc", "icons");
80   if (theme == 0) {
81     return name + "_MODERN";
82   } else if (theme == 1) {
83     return name + "_CLASSIC";
84   }
85   return name + "_DEFAULT";
86 }
87
88 void
89 PresentationController::createActions()
90 {
91   STDLOG("Creating PresentationController actions");
92
93   // View Mode
94   int viewModeToolbarId = _salomeModule->createTool("View Mode", "ViewModeToolbar");
95   QtxActionGroup* ag = _salomeModule->createActionGroup(OPTIONS_VIEW_MODE_ID, true);
96   ag->setText("View mode");
97   ag->setUsesDropDown(true);
98   QString label   = tr("LAB_VIEW_MODE_REPLACE");
99   QString tooltip = tr("TIP_VIEW_MODE_REPLACE");
100   QAction* a = _salomeModule->createAction(OPTIONS_VIEW_MODE_REPLACE_ID,label,QIcon(),label,tooltip,0);
101   a->setCheckable(true);
102   a->setChecked(true);
103   ag->add(a);
104
105   label   = tr("LAB_VIEW_MODE_OVERLAP");
106   tooltip = tr("TIP_VIEW_MODE_OVERLAP");
107   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_OVERLAP_ID,label,QIcon(),label,tooltip,0);
108   a->setCheckable(true);
109   ag->add(a);
110
111   label   = tr("LAB_VIEW_MODE_NEW_LAYOUT");
112   tooltip = tr("TIP_VIEW_MODE_NEW_LAYOUT");
113   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID,label,QIcon(),label,tooltip,0);
114   a->setCheckable(true);
115   ag->add(a);
116
117   label   = tr("LAB_VIEW_MODE_SPLIT_VIEW");
118   tooltip = tr("TIP_VIEW_MODE_SPLIT_VIEW");
119   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID,label,QIcon(),label,tooltip,0);
120   a->setCheckable(true);
121   ag->add(a);
122
123   _salomeModule->createTool(OPTIONS_VIEW_MODE_ID, viewModeToolbarId);
124
125   // Presentations
126   int presentationToolbarId = _salomeModule->createTool("Presentations", "PresentationToolbar");
127   int presentationMenuId = _salomeModule->createMenu(tr("MENU_PRESENTATIONS"), -1, 1);
128
129   label   = tr("LAB_PRESENTATION_SCALAR_MAP");
130   tooltip = tr("TIP_PRESENTATION_SCALAR_MAP");
131   QString icon = tr(_getIconName("ICO_PRESENTATION_SCALAR_MAP").c_str());
132   int actionId;
133   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeScalarMap()),icon,tooltip);
134   _salomeModule->createTool(actionId, presentationToolbarId);
135   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
136   _salomeModule->createMenu(actionId, presentationMenuId);
137
138   label   = tr("LAB_PRESENTATION_CONTOUR");
139   tooltip = tr("TIP_PRESENTATION_CONTOUR");
140   icon    = tr(_getIconName("ICO_PRESENTATION_CONTOUR").c_str());
141   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeContour()),icon,tooltip);
142   _salomeModule->createTool(actionId, presentationToolbarId);
143   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
144   _salomeModule->createMenu(actionId, presentationMenuId);
145
146   label   = tr("LAB_PRESENTATION_VECTOR_FIELD");
147   tooltip = tr("TIP_PRESENTATION_VECTOR_FIELD");
148   icon    = tr(_getIconName("ICO_PRESENTATION_VECTOR_FIELD").c_str());
149   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeVectorField()),icon,tooltip);
150   _salomeModule->createTool(actionId, presentationToolbarId);
151   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
152   _salomeModule->createMenu(actionId, presentationMenuId);
153
154   label   = tr("LAB_PRESENTATION_SLICES");
155   tooltip = tr("TIP_PRESENTATION_SLICES");
156   icon    = tr(_getIconName("ICO_PRESENTATION_SLICES").c_str());
157   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeSlices()),icon,tooltip);
158   _salomeModule->createTool(actionId, presentationToolbarId);
159   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
160   _salomeModule->createMenu(actionId, presentationMenuId);
161
162   label   = tr("LAB_PRESENTATION_DEFLECTION_SHAPE");
163   tooltip = tr("TIP_PRESENTATION_DEFLECTION_SHAPE");
164   icon    = tr(_getIconName("ICO_PRESENTATION_DEFLECTION_SHAPE").c_str());
165   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeDeflectionShape()),icon,tooltip);
166   _salomeModule->createTool(actionId, presentationToolbarId);
167   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
168   _salomeModule->createMenu(actionId, presentationMenuId);
169
170   label   = tr("LAB_PRESENTATION_POINT_SPRITE");
171   tooltip = tr("TIP_PRESENTATION_POINT_SPRITE");
172   icon    = tr(_getIconName("ICO_PRESENTATION_POINT_SPRITE").c_str());
173   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizePointSprite()),icon,tooltip);
174   _salomeModule->createTool(actionId, presentationToolbarId);
175   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
176   _salomeModule->createMenu(actionId, presentationMenuId);
177
178   label   = tr("LAB_DELETE_PRESENTATION");
179   tooltip = tr("TIP_DELETE_PRESENTATION");
180   icon    = tr(_getIconName("ICO_DELETE_PRESENTATION").c_str());
181   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnDeletePresentation()),icon,tooltip);
182 //  _salomeModule->createTool(actionId, presentationToolbarId);
183 //  _salomeModule->action(actionId)->setIconVisibleInMenu(true);
184   _salomeModule->createMenu(actionId, presentationMenuId);
185
186 }
187
188 MEDCALC::MEDPresentationViewMode
189 PresentationController::getSelectedViewMode()
190 {
191   if (_salomeModule->action(OPTIONS_VIEW_MODE_REPLACE_ID)->isChecked()) {
192     return MEDCALC::VIEW_MODE_REPLACE;
193   }
194   else if (_salomeModule->action(OPTIONS_VIEW_MODE_OVERLAP_ID)->isChecked()) {
195     return MEDCALC::VIEW_MODE_OVERLAP;
196   }
197   else if (_salomeModule->action(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID)->isChecked()) {
198     return MEDCALC::VIEW_MODE_NEW_LAYOUT;
199   }
200   else if (_salomeModule->action(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID)->isChecked()) {
201     return MEDCALC::VIEW_MODE_SPLIT_VIEW;
202   }
203   // Should not happen
204   STDLOG("Strange!! No matching view mode found - returning VIEW_MODE_REPLACE.");
205   return MEDCALC::VIEW_MODE_REPLACE;
206 }
207
208 MEDCALC::MEDPresentationColorMap
209 PresentationController::getSelectedColorMap()
210 {
211   return _widgetPresentationParameters->getColorMap();
212 }
213
214 void
215 PresentationController::visualize(PresentationEvent::EventType eventType)
216 {
217   // We need a _studyEditor updated on the active study
218   _studyEditor->updateActiveStudy();
219
220   // Get the selected objects in the study (SObject)
221   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
222
223   // For each object, emit a signal to the workspace to request a
224   // visualisation using the tui command (so that the user can see how
225   // to make a view of an object from the tui console).
226   for (int i=0; i<listOfSObject->size(); i++) {
227     SALOMEDS::SObject_var soField = listOfSObject->at(i);
228     int fieldId = -1;
229     try {
230         fieldId = _studyEditor->getParameterInt(soField,FIELD_ID);    }
231     catch(...)    { }
232     if (fieldId < 0)  // is it a field serie ?
233       {
234         int fieldSeriesId = -1;
235         try {
236             fieldSeriesId = _studyEditor->getParameterInt(soField,FIELD_SERIES_ID);      }
237         catch(...)  { }
238         // If fieldId and fieldSeriesId equals -1, then it means that it is not a field
239         // managed by the MED module, and we stop this function process.
240         if ( fieldSeriesId < 0)
241           continue;
242         MEDCALC::FieldHandlerList* fieldHandlerList = MEDFactoryClient::getDataManager()->getFieldListInFieldseries(fieldSeriesId);
243         if (fieldHandlerList->length() < 0)
244           continue;
245         // For a field series, get the first real field entry:
246         MEDCALC::FieldHandler fieldHandler = (*fieldHandlerList)[0];
247         fieldId = fieldHandler.id;
248       }
249
250     MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
251     if (! fieldHandler) {
252       QMessageBox::warning(_salomeModule->getApp()->desktop(),
253          tr("Operation not allowed"),
254          tr("No field is defined"));
255       return;
256     }
257
258     PresentationEvent* event = new PresentationEvent();
259     event->eventtype = eventType;
260     XmedDataObject* dataObject = new XmedDataObject();
261     dataObject->setFieldHandler(*fieldHandler);
262     event->objectdata = dataObject;
263     emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent
264   }
265 }
266
267 void
268 PresentationController::OnVisualizeScalarMap()
269 {
270   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
271 }
272
273 void
274 PresentationController::OnVisualizeContour()
275 {
276   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
277 }
278
279 void
280 PresentationController::OnVisualizeVectorField()
281 {
282   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
283 }
284
285 void
286 PresentationController::OnVisualizeSlices()
287 {
288   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
289 }
290
291 void
292 PresentationController::OnVisualizeDeflectionShape()
293 {
294   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
295 }
296
297 void
298 PresentationController::OnVisualizePointSprite()
299 {
300   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
301 }
302
303 void
304 PresentationController::OnDeletePresentation()
305 {
306   // We need a _studyEditor updated on the active study
307   _studyEditor->updateActiveStudy();
308
309   // Get the selected objects in the study (SObject)
310   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
311
312   // For each object, emit a signal to the workspace to request pres deletion
313   for (int i=0; i<listOfSObject->size(); i++) {
314     SALOMEDS::SObject_var soPres = listOfSObject->at(i);
315     int presId = _studyEditor->getParameterInt(soPres,PRESENTATION_ID);
316     // If fieldId equals -1, then it means that it is not a field
317     // managed by the MED module, and we stop this function process.
318     if ( presId < 0 )
319       continue;
320
321     STDLOG("Requesting deletion of presentation: ")
322     std::ostringstream oss;
323     oss << presId;
324     STDLOG("    - Pres id:          " + oss.str());
325
326     PresentationEvent* event = new PresentationEvent();
327     event->eventtype = PresentationEvent::EVENT_DELETE_PRESENTATION;
328     XmedDataObject* dataObject = new XmedDataObject();
329     dataObject->setPresentationId(presId);
330     event->objectdata = dataObject;
331     emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent
332   }
333 }
334
335 void
336 PresentationController::updateTreeViewWithNewPresentation(long fieldId, long presentationId)
337 {
338   if (presentationId < 0) {
339     std::cerr << "Unknown presentation\n";
340     return;
341   }
342
343   std::string name = MEDFactoryClient::getPresentationManager()->getPresentationProperty(presentationId, "name");
344   std::string icon = std::string("ICO_") + name;
345   icon = _getIconName(icon);
346   name = tr(name.c_str()).toStdString();
347   std::string label = tr(icon.c_str()).toStdString();
348
349   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
350   _PTR(Study) studyDS = study->studyDS();
351
352   _salomeModule->engine()->registerPresentation(_CAST(Study, studyDS)->GetStudy(), fieldId, name.c_str(), label.c_str(), presentationId);
353
354   // update Object browser
355   _salomeModule->getApp()->updateObjectBrowser(true);
356 }
357
358 void
359 PresentationController::updateTreeViewForPresentationRemoval(long presentationId)
360 {
361   if (presentationId < 0) {
362     std::cerr << "Unknown presentation\n";
363     return;
364   }
365
366   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
367   _PTR(Study) studyDS = study->studyDS();
368
369   _salomeModule->engine()->unregisterPresentation(_CAST(Study, studyDS)->GetStudy(), presentationId);
370
371   // update Object browser
372   _salomeModule->getApp()->updateObjectBrowser(true);
373 }
374
375 void
376 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
377 {
378   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
379     this->updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
380   }
381   else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) {
382     this->updateTreeViewForPresentationRemoval(event->presentationId);
383   }
384 }
385
386 void
387 PresentationController::showDockWidgets(bool isVisible)
388 {
389   STDLOG("Switching PresentationController visibility to: " << isVisible);
390   _dockWidget->setVisible(isVisible);
391 }