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