Salome HOME
[MEDCalc] Presentation removal
[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 {
48   STDLOG("Creating a PresentationController");
49   _salomeModule = salomeModule;
50   _studyEditor = _salomeModule->getStudyEditor();
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
179 MEDCALC::MEDPresentationViewMode
180 PresentationController::getSelectedViewMode()
181 {
182   if (_salomeModule->action(OPTIONS_VIEW_MODE_REPLACE_ID)->isChecked()) {
183     return MEDCALC::VIEW_MODE_REPLACE;
184   }
185   else if (_salomeModule->action(OPTIONS_VIEW_MODE_OVERLAP_ID)->isChecked()) {
186     return MEDCALC::VIEW_MODE_OVERLAP;
187   }
188   else if (_salomeModule->action(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID)->isChecked()) {
189     return MEDCALC::VIEW_MODE_NEW_LAYOUT;
190   }
191   else if (_salomeModule->action(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID)->isChecked()) {
192     return MEDCALC::VIEW_MODE_SPLIT_VIEW;
193   }
194   // Should not happen
195   STDLOG("Strange!! No matching view mode found - returning VIEW_MODE_REPLACE.");
196   return MEDCALC::VIEW_MODE_REPLACE;
197 }
198
199 MEDCALC::MEDPresentationColorMap
200 PresentationController::getSelectedColorMap()
201 {
202   return _widgetPresentationParameters->getColorMap();
203 }
204
205 void
206 PresentationController::visualize(PresentationEvent::EventType eventType)
207 {
208   // We need a _studyEditor updated on the active study
209   _studyEditor->updateActiveStudy();
210
211   // Get the selected objects in the study (SObject)
212   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
213
214   // For each object, emit a signal to the workspace to request a
215   // visualisation using the tui command (so that the user can see how
216   // to make a view of an object from the tui console).
217   for (int i=0; i<listOfSObject->size(); i++) {
218     SALOMEDS::SObject_var soField = listOfSObject->at(i);
219     int fieldId = _studyEditor->getParameterInt(soField,FIELD_ID);
220     // If fieldId equals -1, then it means that it is not a field
221     // managed by the MED module, and we stop this function process.
222     if ( fieldId < 0 )
223       continue;
224
225     MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
226     if (! fieldHandler) {
227       QMessageBox::warning(_salomeModule->getApp()->desktop(),
228          tr("Operation not allowed"),
229          tr("No field is defined"));
230       return;
231     }
232
233     PresentationEvent* event = new PresentationEvent();
234     event->eventtype = eventType;
235     XmedDataObject* dataObject = new XmedDataObject();
236     dataObject->setFieldHandler(*fieldHandler);
237     event->objectdata = dataObject;
238     emit presentationSignal(event); // --> WorkspaceController::processPresentationEvent
239   }
240 }
241
242 void
243 PresentationController::OnVisualizeScalarMap()
244 {
245   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
246 }
247
248 void
249 PresentationController::OnVisualizeContour()
250 {
251   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
252 }
253
254 void
255 PresentationController::OnVisualizeVectorField()
256 {
257   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
258 }
259
260 void
261 PresentationController::OnVisualizeSlices()
262 {
263   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
264 }
265
266 void
267 PresentationController::OnVisualizeDeflectionShape()
268 {
269   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
270 }
271
272 void
273 PresentationController::OnVisualizePointSprite()
274 {
275   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
276 }
277
278 void
279 PresentationController::updateTreeViewWithNewPresentation(long fieldId, long presentationId)
280 {
281   if (presentationId < 0) {
282     std::cerr << "Unknown presentation\n";
283     return;
284   }
285
286   std::string name = MEDFactoryClient::getPresentationManager()->getPresentationProperty(presentationId, "name");
287   std::string icon = std::string("ICO_") + name;
288   icon = _getIconName(icon);
289   name = tr(name.c_str()).toStdString();
290   std::string label = tr(icon.c_str()).toStdString();
291
292   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
293   _PTR(Study) studyDS = study->studyDS();
294
295   _salomeModule->engine()->registerPresentation(_CAST(Study, studyDS)->GetStudy(), fieldId, name.c_str(), label.c_str(), presentationId);
296
297   // update Object browser
298   _salomeModule->getApp()->updateObjectBrowser(true);
299 }
300
301 void
302 PresentationController::updateTreeViewForPresentationRemoval(long presentationId)
303 {
304   if (presentationId < 0) {
305     std::cerr << "Unknown presentation\n";
306     return;
307   }
308
309   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
310   _PTR(Study) studyDS = study->studyDS();
311
312   _salomeModule->engine()->unregisterPresentation(_CAST(Study, studyDS)->GetStudy(), presentationId);
313
314   // update Object browser
315   _salomeModule->getApp()->updateObjectBrowser(true);
316 }
317
318 void
319 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
320 {
321   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
322     this->updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
323   }
324   else if ( event->type == MEDCALC::EVENT_REMOVE_PRESENTATION ) {
325     this->updateTreeViewForPresentationRemoval(event->presentationId);
326   }
327 }
328
329 void
330 PresentationController::showDockWidgets(bool isVisible)
331 {
332   STDLOG("Switching PresentationController visibility to: " << isVisible);
333   _dockWidget->setVisible(isVisible);
334 }