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