Salome HOME
[MEDCalc] icons and preferences
[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 "DatasourceConstants.hxx"
22 #include "MEDModule.hxx"
23 #include "Basics_Utils.hxx"
24 #include "QtxActionGroup.h"
25 #include "MEDFactoryClient.hxx"
26
27 #include <SalomeApp_Application.h>
28 #include <SalomeApp_Study.h>
29 #include <SalomeApp_DataObject.h>
30
31 #include <SALOMEDS_SObject.hxx>
32 #include <SALOMEDS_Study.hxx>
33
34 #include <SUIT_Desktop.h>
35 #include <SUIT_Session.h>
36 #include <SUIT_ResourceMgr.h>
37 #include <QMessageBox>
38
39 static const int OPTIONS_VIEW_MODE_ID = 943;
40 static const int OPTIONS_VIEW_MODE_REPLACE_ID = 944;
41 static const int OPTIONS_VIEW_MODE_OVERLAP_ID = 945;
42 static const int OPTIONS_VIEW_MODE_NEW_LAYOUT_ID = 946;
43 static const int OPTIONS_VIEW_MODE_SPLIT_VIEW_ID = 947;
44
45 PresentationController::PresentationController(MEDModule* salomeModule)
46 {
47   STDLOG("Creating a PresentationController");
48   _salomeModule = salomeModule;
49   _studyEditor = _salomeModule->getStudyEditor();
50 }
51
52 PresentationController::~PresentationController()
53 {
54   STDLOG("Deleting the PresentationController");
55 }
56
57 std::string
58 PresentationController::_getIconName(const std::string& name)
59 {
60   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
61   if (!mgr)
62     return name;
63
64   // Read value from preferences and suffix name to select icon theme
65   int theme = mgr->integerValue("MEDCalc", "icons");
66   if (theme == 0) {
67     return name + "_MODERN";
68   } else if (theme == 1) {
69     return name + "_CLASSIC";
70   }
71   return name + "_DEFAULT";
72 }
73
74 void
75 PresentationController::createActions()
76 {
77   STDLOG("Creating PresentationController actions");
78   int toolbarId = _salomeModule->createTool("View Mode", "PresentationToolbar");
79
80   int presentationMenuId = _salomeModule->createMenu(tr("MENU_PRESENTATIONS"), -1, 1);
81
82   // View Mode
83   QtxActionGroup* ag = _salomeModule->createActionGroup(OPTIONS_VIEW_MODE_ID, true);
84   ag->setText("View mode");
85   ag->setUsesDropDown(true);
86
87   QString label   = tr("LAB_VIEW_MODE_REPLACE");
88   QString tooltip = tr("TIP_VIEW_MODE_REPLACE");
89   QAction* a = _salomeModule->createAction(OPTIONS_VIEW_MODE_REPLACE_ID,label,QIcon(),label,tooltip,0);
90   a->setCheckable(true);
91   a->setChecked(true);
92   ag->add(a);
93
94   label   = tr("LAB_VIEW_MODE_OVERLAP");
95   tooltip = tr("TIP_VIEW_MODE_OVERLAP");
96   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_OVERLAP_ID,label,QIcon(),label,tooltip,0);
97   a->setCheckable(true);
98   ag->add(a);
99
100   label   = tr("LAB_VIEW_MODE_NEW_LAYOUT");
101   tooltip = tr("TIP_VIEW_MODE_NEW_LAYOUT");
102   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID,label,QIcon(),label,tooltip,0);
103   a->setCheckable(true);
104   ag->add(a);
105
106   label   = tr("LAB_VIEW_MODE_SPLIT_VIEW");
107   tooltip = tr("TIP_VIEW_MODE_SPLIT_VIEW");
108   a = _salomeModule->createAction(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID,label,QIcon(),label,tooltip,0);
109   a->setCheckable(true);
110   ag->add(a);
111
112   _salomeModule->createTool(OPTIONS_VIEW_MODE_ID, toolbarId);
113
114   // Presentations
115   label   = tr("LAB_PRESENTATION_SCALAR_MAP");
116   tooltip = tr("TIP_PRESENTATION_SCALAR_MAP");
117   QString icon = tr(_getIconName("ICO_PRESENTATION_SCALAR_MAP").c_str());
118   int actionId;
119   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeScalarMap()),icon,tooltip);
120   _salomeModule->createTool(actionId, toolbarId);
121   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
122   _salomeModule->createMenu(actionId, presentationMenuId);
123
124   label   = tr("LAB_PRESENTATION_CONTOUR");
125   tooltip = tr("TIP_PRESENTATION_CONTOUR");
126   icon    = tr(_getIconName("ICO_PRESENTATION_CONTOUR").c_str());
127   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeContour()),icon,tooltip);
128   _salomeModule->createTool(actionId, toolbarId);
129   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
130   _salomeModule->createMenu(actionId, presentationMenuId);
131
132   label   = tr("LAB_PRESENTATION_VECTOR_FIELD");
133   tooltip = tr("TIP_PRESENTATION_VECTOR_FIELD");
134   icon    = tr(_getIconName("ICO_PRESENTATION_VECTOR_FIELD").c_str());
135   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeVectorField()),icon,tooltip);
136   _salomeModule->createTool(actionId, toolbarId);
137   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
138   _salomeModule->createMenu(actionId, presentationMenuId);
139
140   label   = tr("LAB_PRESENTATION_SLICES");
141   tooltip = tr("TIP_PRESENTATION_SLICES");
142   icon    = tr(_getIconName("ICO_PRESENTATION_SLICES").c_str());
143   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeSlices()),icon,tooltip);
144   _salomeModule->createTool(actionId, toolbarId);
145   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
146   _salomeModule->createMenu(actionId, presentationMenuId);
147
148   label   = tr("LAB_PRESENTATION_DEFLECTION_SHAPE");
149   tooltip = tr("TIP_PRESENTATION_DEFLECTION_SHAPE");
150   icon    = tr(_getIconName("ICO_PRESENTATION_DEFLECTION_SHAPE").c_str());
151   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizeDeflectionShape()),icon,tooltip);
152   _salomeModule->createTool(actionId, toolbarId);
153   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
154   _salomeModule->createMenu(actionId, presentationMenuId);
155
156   label   = tr("LAB_PRESENTATION_POINT_SPRITE");
157   tooltip = tr("TIP_PRESENTATION_POINT_SPRITE");
158   icon    = tr(_getIconName("ICO_PRESENTATION_POINT_SPRITE").c_str());
159   actionId = _salomeModule->createStandardAction(label,this, SLOT(OnVisualizePointSprite()),icon,tooltip);
160   _salomeModule->createTool(actionId, toolbarId);
161   _salomeModule->action(actionId)->setIconVisibleInMenu(true);
162   _salomeModule->createMenu(actionId, presentationMenuId);
163 }
164
165 MEDCALC::MEDPresentationViewMode
166 PresentationController::getSelectedViewMode()
167 {
168   if (_salomeModule->action(OPTIONS_VIEW_MODE_REPLACE_ID)->isChecked()) {
169     return MEDCALC::VIEW_MODE_REPLACE;
170   }
171   else if (_salomeModule->action(OPTIONS_VIEW_MODE_OVERLAP_ID)->isChecked()) {
172     return MEDCALC::VIEW_MODE_OVERLAP;
173   }
174   else if (_salomeModule->action(OPTIONS_VIEW_MODE_NEW_LAYOUT_ID)->isChecked()) {
175     return MEDCALC::VIEW_MODE_NEW_LAYOUT;
176   }
177   else if (_salomeModule->action(OPTIONS_VIEW_MODE_SPLIT_VIEW_ID)->isChecked()) {
178     return MEDCALC::VIEW_MODE_SPLIT_VIEW;
179   }
180 }
181
182 void
183 PresentationController::visualize(PresentationEvent::EventType eventType)
184 {
185   // We need a _studyEditor updated on the active study
186   _studyEditor->updateActiveStudy();
187
188   // Get the selected objects in the study (SObject)
189   SALOME_StudyEditor::SObjectList* listOfSObject = _studyEditor->getSelectedObjects();
190
191   // For each object, emit a signal to the workspace to request a
192   // visualisation using the tui command (so that the user can see how
193   // to make a view of an object from the tui console).
194   for (int i=0; i<listOfSObject->size(); i++) {
195     SALOMEDS::SObject_var soField = listOfSObject->at(i);
196     int fieldId = _studyEditor->getParameterInt(soField,OBJECT_ID);
197     // If fieldId equals -1, then it means that it is not a field
198     // managed by the MED module, and we stop this function process.
199     if ( fieldId < 0 )
200       continue;
201
202     MEDCALC::FieldHandler* fieldHandler = MEDFactoryClient::getDataManager()->getFieldHandler(fieldId);
203     if (! fieldHandler) {
204       QMessageBox::warning(_salomeModule->getApp()->desktop(),
205          tr("Operation not allowed"),
206          tr("No field is defined"));
207       return;
208     }
209
210     PresentationEvent* event = new PresentationEvent();
211     event->eventtype = eventType;
212     XmedDataObject* dataObject = new XmedDataObject();
213     dataObject->setFieldHandler(*fieldHandler);
214     event->objectdata = dataObject;
215     emit presentationSignal(event);
216   }
217 }
218
219 void
220 PresentationController::OnVisualizeScalarMap()
221 {
222   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SCALAR_MAP);
223 }
224
225 void
226 PresentationController::OnVisualizeContour()
227 {
228   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_CONTOUR);
229 }
230
231 void
232 PresentationController::OnVisualizeVectorField()
233 {
234   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_VECTOR_FIELD);
235 }
236
237 void
238 PresentationController::OnVisualizeSlices()
239 {
240   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_SLICES);
241 }
242
243 void
244 PresentationController::OnVisualizeDeflectionShape()
245 {
246   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_DEFLECTION_SHAPE);
247 }
248
249 void
250 PresentationController::OnVisualizePointSprite()
251 {
252   this->visualize(PresentationEvent::EVENT_VIEW_OBJECT_POINT_SPRITE);
253 }
254
255 void
256 PresentationController::updateTreeViewWithNewPresentation(long fieldId, long presentationId)
257 {
258   if (presentationId < 0) {
259     std::cerr << "Unknown presentation\n";
260     return;
261   }
262
263   std::string name = MEDFactoryClient::getPresentationManager()->getPresentationProperty(presentationId, "name");
264   std::string icon = std::string("ICO_") + name;
265   icon = _getIconName(icon);
266   name = tr(name.c_str()).toStdString();
267   std::string label = tr(icon.c_str()).toStdString();
268
269   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>(_salomeModule->application()->activeStudy());
270   _PTR(Study) studyDS = study->studyDS();
271
272   _salomeModule->engine()->registerPresentation(_CAST(Study, studyDS)->GetStudy(), fieldId, name.c_str(), label.c_str());
273
274   // update Object browser
275   _salomeModule->getApp()->updateObjectBrowser(true);
276 }
277
278 void
279 PresentationController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
280 {
281   if ( event->type == MEDCALC::EVENT_ADD_PRESENTATION ) {
282     this->updateTreeViewWithNewPresentation(event->dataId, event->presentationId);
283   }
284 }