Salome HOME
[MEDCalc] Contour 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.comboBoxScalarBarRange, SIGNAL(currentIndexChanged(int)),
34                    this,                       SLOT(onComboScalarBarRangeIndexChanged(int)) );
35   QObject::connect(_ui.comboBoxColorMap,       SIGNAL(currentIndexChanged(int)),
36                    this,                       SLOT(onComboColorMapIndexChanged(int)) );
37   QObject::connect(_ui.spinBox,                SIGNAL(valueChanged(int)),
38                      this,                     SLOT(onSpinBoxValueChanged(int)) );
39 }
40
41 void
42 WidgetPresentationParameters::onComboCompoIndexChanged(int idx)
43 {
44   if (!_blockSig) emit comboCompoIndexChanged(idx);
45 }
46
47 void
48 WidgetPresentationParameters::onComboColorMapIndexChanged(int idx)
49 {
50   if (!_blockSig) emit comboColorMapIndexChanged(idx);
51 }
52
53 void
54 WidgetPresentationParameters::onComboScalarBarRangeIndexChanged(int idx)
55 {
56   if (!_blockSig) emit comboScalarBarRangeIndexChanged(idx);
57 }
58
59 void
60 WidgetPresentationParameters::onSpinBoxValueChanged(int val)
61 {
62   if (!_blockSig) emit spinBoxValueChanged(val);
63 }
64
65 void
66 WidgetPresentationParameters::toggleWidget(bool show)
67 {
68   if (!show)
69     {
70       _blockSig = true;
71       _ui.widgetDynamic->hide();
72       setPresName("Choose a presentation");
73       // reset colorMap and scalarBarRange:
74       setColorMap(MEDCALC::COLOR_MAP_DEFAULT);
75       setScalarBarRange(MEDCALC::SCALAR_BAR_RANGE_DEFAULT);
76     }
77   else
78     {
79       _ui.widgetDynamic->show();
80       // It is the WidgetHelper responsability to re-show the widgets it needs
81       _ui.labelCompo->hide();
82       _ui.comboBoxCompo->hide();
83       _ui.labelSpinBox->hide();
84       _ui.spinBox->hide();
85       _ui.labelSliceOrient->hide();
86       _ui.comboBoxSliceOrient->hide();
87       _blockSig = false;
88     }
89 }
90
91 bool
92 WidgetPresentationParameters::isShown() const
93 {
94   return _ui.widgetDynamic->isVisible();
95 }
96
97 string
98 WidgetPresentationParameters::getComponent() const
99 {
100   if (_ui.comboBoxCompo->currentIndex() == 0) // Euclidean norm
101       return "";
102
103   return _ui.comboBoxCompo->currentText().toStdString();
104 }
105
106 void
107 WidgetPresentationParameters::setComponents(vector<string> compos, int selecIndex)
108 {
109   // Show the widget:
110   _ui.labelCompo->show();
111   _ui.comboBoxCompo->show();
112
113   _blockSig = true;
114   _ui.comboBoxCompo->clear();
115   _ui.comboBoxCompo->addItem(tr("LAB_EUCLIDEAN_NORM"));
116   for(vector<string>::const_iterator it = compos.begin(); it != compos.end(); ++it)
117     _ui.comboBoxCompo->addItem(QString::fromStdString(*it));
118   _ui.comboBoxCompo->setCurrentIndex(selecIndex);
119   _blockSig = false;
120 }
121
122 void
123 WidgetPresentationParameters::setNbContour(int nbContour)
124 {
125   if (nbContour <= 0)
126     {
127       //TODO throw?
128       STDLOG("WidgetPresentationParameters::setNbContour(): invalid number of contours!");
129     }
130
131   // Show the widget:
132   _ui.labelSpinBox->setText(tr("LAB_NB_CONTOUR"));
133   _ui.labelSpinBox->show();
134   _ui.spinBox->show();
135
136   _blockSig = true;
137   _ui.spinBox->setValue(nbContour);
138   _blockSig = false;
139 }
140
141 int WidgetPresentationParameters::getNbContour() const
142 {
143   return _ui.spinBox->value();
144 }
145
146 void
147 WidgetPresentationParameters::setScalarBarRange(MEDCALC::MEDPresentationScalarBarRange sbrange)
148 {
149   int idx;
150   if (sbrange == MEDCALC::SCALAR_BAR_ALL_TIMESTEPS)
151     idx = _ui.comboBoxScalarBarRange->findText(tr("LAB_ALL_TIMESTEPS"));
152   else if (sbrange == MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP)
153     idx = _ui.comboBoxScalarBarRange->findText(tr("LAB_CURRENT_TIMESTEP"));
154
155   if (idx >= 0)
156     {
157       _blockSig = true;
158       _ui.comboBoxScalarBarRange->setCurrentIndex(idx);
159       _blockSig = false;
160     }
161   else
162     STDLOG("Strange!! No matching found - unable to set scalar bar range in GUI.");
163 }
164
165 void
166 WidgetPresentationParameters::setColorMap(MEDCALC::MEDPresentationColorMap colorMap)
167 {
168   int idx;
169   if (colorMap == MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW)
170     idx = _ui.comboBoxColorMap->findText(tr("LAB_BLUE_TO_RED"));
171   else if (colorMap == MEDCALC::COLOR_MAP_COOL_TO_WARM)
172     idx = _ui.comboBoxColorMap->findText(tr("LAB_COOL_TO_WARM"));
173
174   if (idx >= 0)
175     {
176       _blockSig = true;
177       _ui.comboBoxColorMap->setCurrentIndex(idx);
178       _blockSig = false;
179     }
180
181   else
182     STDLOG("Strange!! No matching found - unable to set color map in GUI.");
183 }
184
185
186 MEDCALC::MEDPresentationScalarBarRange
187 WidgetPresentationParameters::getScalarBarRange() const
188 {
189   QString sbrange = _ui.comboBoxScalarBarRange->currentText();
190   if (sbrange == tr("LAB_ALL_TIMESTEPS")) {
191     return MEDCALC::SCALAR_BAR_ALL_TIMESTEPS;
192   }
193   else if (sbrange == tr("LAB_CURRENT_TIMESTEP")) {
194     return MEDCALC::SCALAR_BAR_CURRENT_TIMESTEP;
195   }
196   // Should not happen
197   STDLOG("Strange!! No matching found - returning SCALAR_BAR_ALL_TIMESTEPS.");
198   return MEDCALC::SCALAR_BAR_ALL_TIMESTEPS;
199 }
200
201 //double
202 //WidgetPresentationParameters::getScalarBarTimestep() const
203 //{
204 //  return _ui.doubleSpinBoxTimeStep->value();
205 //}
206 //
207 //double
208 //WidgetPresentationParameters::getScalarBarMinVal() const
209 //{
210 //  return _ui.doubleSpinBoxMinVal->value();
211 //}
212 //
213 //double
214 //WidgetPresentationParameters::getScalarBarMaxVal() const
215 //{
216 //  return _ui.doubleSpinBoxMaxVal->value();
217 //}
218
219 MEDCALC::MEDPresentationColorMap
220 WidgetPresentationParameters::getColorMap() const
221 {
222   QString colorMap = _ui.comboBoxColorMap->currentText();
223   if (colorMap == tr("LAB_BLUE_TO_RED")) {
224     return MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW;
225   }
226   else if (colorMap == tr("LAB_COOL_TO_WARM")) {
227     return MEDCALC::COLOR_MAP_COOL_TO_WARM;
228   }
229   // Should not happen
230   STDLOG("Strange!! No matching color map found - returning blue to red.");
231   return MEDCALC::COLOR_MAP_BLUE_TO_RED_RAINBOW;
232 }
233
234 QComboBox *
235 WidgetPresentationParameters::getComboBoxCompo()
236 {
237   return _ui.comboBoxCompo;
238 }
239
240 void
241 WidgetPresentationParameters::setPresName(const std::string& name)
242 {
243   _ui.labelPresName->setText(QString::fromStdString(name));
244   QFont f(_ui.labelPresName->font());
245   f.setItalic(true);
246   _ui.labelPresName->setFont(f);
247 }