Salome HOME
[MEDCalc] Mesh view and slice presentation.
[modules/med.git] / src / MEDCalc / gui / dialogs / WidgetPresentationParameters.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 "WidgetPresentationParameters.hxx"
21 #include <Basics_Utils.hxx>
22
23 using namespace std;
24
25 WidgetPresentationParameters::WidgetPresentationParameters(QWidget* parent)
26   : QWidget(parent), _blockSig(false)
27 {
28   _ui.setupUi(this); // To be done first
29
30   toggleWidget(false);
31   QObject::connect(_ui.comboBoxCompo,          SIGNAL(currentIndexChanged(int)),
32                    this,                       SLOT(onComboCompoIndexChanged(int)) );
33   QObject::connect(_ui.comboBoxMesh,          SIGNAL(currentIndexChanged(int)),
34                      this,                       SLOT(onComboMeshIndexChanged(int)) );
35   QObject::connect(_ui.comboBoxScalarBarRange, SIGNAL(currentIndexChanged(int)),
36                    this,                       SLOT(onComboScalarBarRangeIndexChanged(int)) );
37   QObject::connect(_ui.comboBoxColorMap,       SIGNAL(currentIndexChanged(int)),
38                    this,                       SLOT(onComboColorMapIndexChanged(int)) );
39   QObject::connect(_ui.comboBoxSliceOrient,       SIGNAL(currentIndexChanged(int)),
40                    this,                       SLOT(onComboOrientIndexChanged(int)) );
41   QObject::connect(_ui.spinBox,                SIGNAL(valueChanged(int)),
42                      this,                     SLOT(onSpinBoxValueChanged(int)) );
43 }
44
45 void
46 WidgetPresentationParameters::onComboCompoIndexChanged(int idx)
47 {
48   if (!_blockSig) emit comboCompoIndexChanged(idx);
49 }
50
51 void
52 WidgetPresentationParameters::onComboOrientIndexChanged(int idx)
53 {
54   if (!_blockSig) emit comboOrientIndexChanged(idx);
55 }
56
57
58 void
59 WidgetPresentationParameters::onComboMeshIndexChanged(int idx)
60 {
61   if (!_blockSig) emit comboMeshIndexChanged(idx);
62 }
63
64 void
65 WidgetPresentationParameters::onComboColorMapIndexChanged(int idx)
66 {
67   if (!_blockSig) emit comboColorMapIndexChanged(idx);
68 }
69
70 void
71 WidgetPresentationParameters::onComboScalarBarRangeIndexChanged(int idx)
72 {
73   if (!_blockSig) emit comboScalarBarRangeIndexChanged(idx);
74 }
75
76 void
77 WidgetPresentationParameters::onSpinBoxValueChanged(int val)
78 {
79   if (!_blockSig) emit spinBoxValueChanged(val);
80 }
81
82 void
83 WidgetPresentationParameters::toggleCommonFieldWidget(bool show)
84 {
85   _blockSig = true;
86   _ui.commonWidget->setEnabled(show);
87   _blockSig = false;
88 }
89
90 void
91 WidgetPresentationParameters::toggleWidget(bool show)
92 {
93   if (!show)
94     {
95       toggleCommonFieldWidget(true);
96
97       _blockSig = true;
98       _ui.widgetDynamic->hide();
99       setPresName("Choose a presentation");
100       // reset colorMap and scalarBarRange:
101       setColorMap(MEDCALC::COLOR_MAP_DEFAULT);
102       setScalarBarRange(MEDCALC::SCALAR_BAR_RANGE_DEFAULT);
103     }
104   else
105     {
106       _ui.widgetDynamic->show();
107       // It is the WidgetHelper responsability to re-show the widgets it needs
108       _ui.labelCompo->hide();
109       _ui.comboBoxCompo->hide();
110       _ui.labelMeshMode->hide();
111       _ui.comboBoxMesh->hide();
112       _ui.labelSpinBox->hide();
113       _ui.spinBox->hide();
114       _ui.labelSliceOrient->hide();
115       _ui.comboBoxSliceOrient->hide();
116       _blockSig = false;
117     }
118 }
119
120 bool
121 WidgetPresentationParameters::isShown() const
122 {
123   return _ui.widgetDynamic->isVisible();
124 }
125
126 string
127 WidgetPresentationParameters::getComponent() const
128 {
129   if (_ui.comboBoxCompo->currentIndex() == 0) // Euclidean norm
130       return "";
131
132   return _ui.comboBoxCompo->currentText().toStdString();
133 }
134
135 void
136 WidgetPresentationParameters::setComponents(vector<string> compos, int selecIndex)
137 {
138   // Show the widget:
139   _ui.labelCompo->show();
140   _ui.comboBoxCompo->show();
141
142   _blockSig = true;
143   _ui.comboBoxCompo->clear();
144   _ui.comboBoxCompo->addItem(tr("LAB_EUCLIDEAN_NORM"));
145   for(vector<string>::const_iterator it = compos.begin(); it != compos.end(); ++it)
146     _ui.comboBoxCompo->addItem(QString::fromStdString(*it));
147   _ui.comboBoxCompo->setCurrentIndex(selecIndex);
148   _blockSig = false;
149 }
150
151 void
152 WidgetPresentationParameters::setNbContour(int nbContour)
153 {
154   if (nbContour <= 0)
155     {
156       //TODO throw?
157       STDLOG("WidgetPresentationParameters::setNbContour(): invalid number of contours!");
158     }
159
160   // Show the widget:
161   _ui.labelSpinBox->setText(tr("LAB_NB_CONTOURS"));
162   _ui.labelSpinBox->show();
163   _ui.spinBox->show();
164
165   _blockSig = true;
166   _ui.spinBox->setValue(nbContour);
167   _blockSig = false;
168 }
169
170 void
171 WidgetPresentationParameters::setNbSlices(int nbSlices)
172 {
173   if (nbSlices <= 0)
174     {
175       //TODO throw?
176       STDLOG("WidgetPresentationParameters::setNbSlices(): invalid number of slices!");
177     }
178
179   // Show the widget:
180   _ui.labelSpinBox->setText(tr("LAB_NB_SLICES"));
181   _ui.labelSpinBox->show();
182   _ui.spinBox->show();
183
184   _blockSig = true;
185   _ui.spinBox->setValue(nbSlices);
186   _blockSig = false;
187 }
188
189 int WidgetPresentationParameters::getNbContour() const
190 {
191   return _ui.spinBox->value();
192 }
193
194 int WidgetPresentationParameters::getNbSlices() const
195 {
196   return _ui.spinBox->value();
197 }
198
199
200 void
201 WidgetPresentationParameters::setScalarBarRange(MEDCALC::MEDPresentationScalarBarRange sbrange)
202 {
203   int idx;
204   if (sbrange == MEDCALC::SCALAR_BAR_ALL_TIMESTEPS)
205     idx = _ui.comboBoxScalarBarRange->findText(tr("LAB_ALL_TIMESTEPS"));
206   else if (sbrange == MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP)
207     idx = _ui.comboBoxScalarBarRange->findText(tr("LAB_CURRENT_TIMESTEP"));
208
209   if (idx >= 0)
210     {
211       _blockSig = true;
212       _ui.comboBoxScalarBarRange->setCurrentIndex(idx);
213       _blockSig = false;
214     }
215   else
216     STDLOG("Strange!! No matching found - unable to set scalar bar range in GUI.");
217 }
218
219 void
220 WidgetPresentationParameters::setColorMap(MEDCALC::MEDPresentationColorMap colorMap)
221 {
222   int idx = -1;
223   if (colorMap == MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW)
224     idx = _ui.comboBoxColorMap->findText(tr("LAB_BLUE_TO_RED"));
225   else if (colorMap == MEDCALC::COLOR_MAP_COOL_TO_WARM)
226     idx = _ui.comboBoxColorMap->findText(tr("LAB_COOL_TO_WARM"));
227
228   if (idx >= 0)
229     {
230       _blockSig = true;
231       _ui.comboBoxColorMap->setCurrentIndex(idx);
232       _blockSig = false;
233     }
234
235   else
236     STDLOG("Strange!! No matching found - unable to set color map in GUI.");
237 }
238
239 void
240 WidgetPresentationParameters::setMeshMode(MEDCALC::MEDPresentationMeshMode mode)
241 {
242   // Show the widget:
243   _ui.labelMeshMode->show();
244   _ui.comboBoxMesh->show();
245
246   int idx;
247   switch(mode)
248   {
249     case MEDCALC::MESH_MODE_WIREFRAME:
250       idx = _ui.comboBoxMesh->findText(tr("LAB_MESH_WIREFRAME"));
251       break;
252     case MEDCALC::MESH_MODE_SURFACE:
253       idx = _ui.comboBoxMesh->findText(tr("LAB_MESH_SURFACE"));
254       break;
255     case MEDCALC::MESH_MODE_SURFACE_EDGES:
256       idx = _ui.comboBoxMesh->findText(tr("LAB_MESH_SURF_EDGES"));
257       break;
258     default:
259       idx = -1;
260   }
261   if (idx >= 0)
262     {
263       _blockSig = true;
264       _ui.comboBoxMesh->setCurrentIndex(idx);
265       _blockSig = false;
266     }
267   else
268     STDLOG("Strange!! No matching found - unable to set mesh mode in GUI.");
269 }
270
271 void
272 WidgetPresentationParameters::setSliceOrientation(MEDCALC::MEDPresentationSliceOrientation orient)
273 {
274   // Show the widget:
275   _ui.labelSliceOrient->show();
276   _ui.comboBoxSliceOrient->show();
277
278   int idx;
279   switch(orient)
280   {
281     case MEDCALC::SLICE_NORMAL_TO_X:
282       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_X"));
283       break;
284     case MEDCALC::SLICE_NORMAL_TO_Y:
285       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_Y"));
286       break;
287     case MEDCALC::SLICE_NORMAL_TO_Z:
288       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_Z"));
289       break;
290     case MEDCALC::SLICE_NORMAL_TO_XY:
291       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_XY"));
292       break;
293     case MEDCALC::SLICE_NORMAL_TO_XZ:
294       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_XZ"));
295       break;
296     case MEDCALC::SLICE_NORMAL_TO_YZ:
297       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_YZ"));
298       break;
299     case MEDCALC::SLICE_NORMAL_TO_XYZ:
300       idx = _ui.comboBoxSliceOrient->findText(tr("LAB_SLICE_NORMAL_TO_XYZ"));
301       break;
302     default:
303       idx = -1;
304   }
305   if (idx >= 0)
306     {
307       _blockSig = true;
308       _ui.comboBoxSliceOrient->setCurrentIndex(idx);
309       _blockSig = false;
310     }
311   else
312     STDLOG("Strange!! No matching found - unable to set slice orientation in GUI.");
313 }
314
315
316 MEDCALC::MEDPresentationSliceOrientation
317 WidgetPresentationParameters::getSliceOrientation() const
318 {
319   QString sbrange = _ui.comboBoxSliceOrient->currentText();
320   if (sbrange == tr("LAB_SLICE_NORMAL_TO_X")) {
321       return MEDCALC::SLICE_NORMAL_TO_X;
322   }
323   else if (sbrange == tr("LAB_SLICE_NORMAL_TO_Y")) {
324       return MEDCALC::SLICE_NORMAL_TO_Y;
325   }
326   else if (sbrange == tr("LAB_SLICE_NORMAL_TO_Z")) {
327       return MEDCALC::SLICE_NORMAL_TO_Z;
328   }
329   else if (sbrange == tr("LAB_SLICE_NORMAL_TO_XY")) {
330       return MEDCALC::SLICE_NORMAL_TO_XY;
331   }
332   else if (sbrange == tr("LAB_SLICE_NORMAL_TO_XZ")) {
333       return MEDCALC::SLICE_NORMAL_TO_XZ;
334   }
335   else if (sbrange == tr("LAB_SLICE_NORMAL_TO_YZ")) {
336       return MEDCALC::SLICE_NORMAL_TO_YZ;
337   }
338   else if (sbrange == tr("LAB_SLICE_NORMAL_TO_XYZ")) {
339       return MEDCALC::SLICE_NORMAL_TO_XYZ;
340   }
341   // Should not happen
342   STDLOG("Strange!! No matching found - returning SLICE_NORMAL_TO_X.");
343   return MEDCALC::SLICE_NORMAL_TO_X;
344 }
345
346 MEDCALC::MEDPresentationMeshMode
347 WidgetPresentationParameters::getMeshMode() const
348 {
349   QString mesm = _ui.comboBoxMesh->currentText();
350   if (mesm == tr("LAB_MESH_WIREFRAME")) {
351       return MEDCALC::MESH_MODE_WIREFRAME;
352   }
353   else if (mesm == tr("LAB_MESH_SURFACE")) {
354       return MEDCALC::MESH_MODE_SURFACE;
355   }
356   else if (mesm == tr("LAB_MESH_SURF_EDGES")) {
357       return MEDCALC::MESH_MODE_SURFACE_EDGES;
358   }
359   // Should not happen
360   STDLOG("Strange!! No matching found - returning MESH_MODE_WIREFRAME.");
361   return MEDCALC::MESH_MODE_WIREFRAME;
362 }
363
364
365 MEDCALC::MEDPresentationScalarBarRange
366 WidgetPresentationParameters::getScalarBarRange() const
367 {
368   QString sbrange = _ui.comboBoxScalarBarRange->currentText();
369   if (sbrange == tr("LAB_ALL_TIMESTEPS")) {
370     return MEDCALC::SCALAR_BAR_ALL_TIMESTEPS;
371   }
372   else if (sbrange == tr("LAB_CURRENT_TIMESTEP")) {
373     return MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP;
374   }
375   // Should not happen
376   STDLOG("Strange!! No matching found - returning SCALAR_BAR_ALL_TIMESTEPS.");
377   return MEDCALC::SCALAR_BAR_ALL_TIMESTEPS;
378 }
379
380 //double
381 //WidgetPresentationParameters::getScalarBarTimestep() const
382 //{
383 //  return _ui.doubleSpinBoxTimeStep->value();
384 //}
385 //
386 //double
387 //WidgetPresentationParameters::getScalarBarMinVal() const
388 //{
389 //  return _ui.doubleSpinBoxMinVal->value();
390 //}
391 //
392 //double
393 //WidgetPresentationParameters::getScalarBarMaxVal() const
394 //{
395 //  return _ui.doubleSpinBoxMaxVal->value();
396 //}
397
398 MEDCALC::MEDPresentationColorMap
399 WidgetPresentationParameters::getColorMap() const
400 {
401   QString colorMap = _ui.comboBoxColorMap->currentText();
402   if (colorMap == tr("LAB_BLUE_TO_RED")) {
403     return MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW;
404   }
405   else if (colorMap == tr("LAB_COOL_TO_WARM")) {
406     return MEDCALC::COLOR_MAP_COOL_TO_WARM;
407   }
408   // Should not happen
409   STDLOG("Strange!! No matching color map found - returning blue to red.");
410   return MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW;
411 }
412
413 void
414 WidgetPresentationParameters::setPresName(const std::string& name)
415 {
416   _ui.labelPresName->setText(QString::fromStdString(name));
417   QFont f(_ui.labelPresName->font());
418   f.setItalic(true);
419   _ui.labelPresName->setFont(f);
420 }