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