fw4spl
SParameters.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #pragma once
8 
9 #include "guiQt/config.hpp"
10 
11 #include <fwGui/editor/IEditor.hpp>
12 
13 #include <QGridLayout>
14 #include <QLabel>
15 #include <QObject>
16 #include <QPointer>
17 #include <QPushButton>
18 #include <QSignalMapper>
19 #include <QSlider>
20 
21 namespace guiQt
22 {
23 
24 namespace editor
25 {
26 
95 class GUIQT_CLASS_API SParameters : public QObject,
97 {
98 Q_OBJECT
99 public:
101 
103  typedef ::fwCom::Signal< void (bool, std::string) > BooleanChangedSignalType;
104 
106  typedef ::fwCom::Signal< void (std::array<std::uint8_t, 4>, std::string) > ColorChangedSignalType;
107 
109  typedef ::fwCom::Signal< void (double, std::string) > DoubleChangedSignalType;
110  typedef ::fwCom::Signal< void (double, double, std::string) > Double2ChangedSignalType;
111  typedef ::fwCom::Signal< void (double, double, double, std::string) > Double3ChangedSignalType;
112 
114  typedef ::fwCom::Signal< void (int, std::string) > IntegerChangedSignalType;
115  typedef ::fwCom::Signal< void (int, int, std::string) > Integer2ChangedSignalType;
116  typedef ::fwCom::Signal< void (int, int, int, std::string) > Integer3ChangedSignalType;
117 
119  typedef ::fwCom::Signal< void (std::string, std::string) > EnumChangedSignalType;
120  typedef ::fwCom::Signal< void (int, std::string) > EnumChangedIndexSignalType;
121 
123  GUIQT_API SParameters() noexcept;
124 
126  GUIQT_API virtual ~SParameters() noexcept;
127 
129  GUIQT_API void configuring() override;
130 
132  GUIQT_API void starting() override;
133 
135  GUIQT_API void stopping() override;
136 
138  GUIQT_API void updating() override;
139 
140 private Q_SLOTS:
141 
143  void onChangeBoolean(int value);
144 
146  void onColorButton();
147 
149  void onChangeInteger(int value);
150 
152  void onChangeDouble(double value);
153 
155  void onChangeDoubleSlider(int value);
156 
158  void onChangeEnum(int value);
159 
161  void onSliderMapped(QLabel* label, QSlider* slider);
162 
164  void onDoubleSliderMapped(QLabel* label, QSlider* slider);
165 
167  void onResetBooleanMapped(QWidget* widget);
168 
170  void onResetColorMapped(QWidget* widget);
171 
173  void onResetIntegerMapped(QWidget* widget);
174 
176  void onResetDoubleMapped(QWidget* widget);
177 
179  void onSliderRangeMapped(QLabel* minLabel, QLabel* maxLabel, QSlider* slider);
180 
182  void onDoubleSliderRangeMapped(QLabel* minLabel, QLabel* maxLabel, QSlider* slider);
183 
184 private:
185 
187  QPushButton* createResetButton();
188 
190  void createBoolWidget(QGridLayout& layout, int row, const std::string& key, const std::string& defaultValue);
191 
193  void createColorWidget(QGridLayout& layout, int row, const std::string& key, const std::string& defaultValue);
194 
196  void createDoubleWidget(QGridLayout& layout, int row, const std::string& key, double defaultValue,
197  double min, double max, int count);
198 
200  void createDoubleSliderWidget(QGridLayout& layout, int row, const std::string& key, double defaultValue,
201  double min, double max, std::uint8_t decimals);
202 
204  void createIntegerSliderWidget(QGridLayout& layout, int row, const std::string& key,
205  int defaultValue, int min, int max);
206 
208  void createIntegerSpinWidget(QGridLayout& layout, int row, const std::string& key,
209  int defaultValue, int min, int max, int count);
210 
212  void createEnumWidget(QGridLayout& layout, int row, const std::string& key, const std::string& defaultValue,
213  const std::vector< std::string>& values, const std::vector<std::string>& data);
214 
216  void emitIntegerSignal(QObject* widget);
217 
219  void emitDoubleSignal(QObject* spinbox);
220 
222  void emitColorSignal(const QColor color, const std::string& key);
223 
228  void setBoolParameter(bool val, std::string key);
230 
232  void setColorParameter(std::array<std::uint8_t, 4> color, std::string key);
233 
235  void setDoubleParameter(double val, std::string key);
236 
238  void setDouble2Parameter(double val0, double val1, std::string key);
239 
241  void setDouble3Parameter(double val0, double val1, double val2, std::string key);
242 
244  void setIntParameter(int val, std::string key);
245 
247  void setInt2Parameter(int val0, int val1, std::string key);
248 
250  void setInt3Parameter(int val0, int val1, int val2, std::string key);
251 
253  void setEnumParameter(std::string val, std::string key);
254 
256  void setIntMinParameter(int min, std::string key);
257 
259  void setIntMaxParameter(int max, std::string key);
260 
262  void setDoubleMinParameter(double min, std::string key);
263 
265  void setDoubleMaxParameter(double max, std::string key);
267 
269  QWidget* getParamWidget(const std::string& key);
270 
272  static double getDoubleSliderValue(const QSlider* slider);
273 
276  static void setDoubleSliderRange(QSlider* slider, double currentValue);
277 
279  template <typename T>
280  static void setLabelMinimumSize(QLabel* label, T min, T max, std::uint8_t decimals = 0);
281 
282  template<typename T>
283  static QString valueToStringLabel(T value, std::uint8_t decimals);
284 
286  void blockSignals(bool block);
287 
289  bool m_blockSignals;
290 };
291 
292 //------------------------------------------------------------------------------
293 
294 template<> inline QString SParameters::valueToStringLabel<int>(int value, std::uint8_t)
295 {
296  return QString("%1").arg(value);
297 }
298 
299 //------------------------------------------------------------------------------
300 
301 template<> inline QString SParameters::valueToStringLabel<double>(double value, std::uint8_t decimals)
302 {
303  return QString::number(value, 'f', decimals);
304 }
305 
306 //------------------------------------------------------------------------------
307 
308 template<typename T>
309 void SParameters::setLabelMinimumSize(QLabel* label, T min, T max, std::uint8_t decimals)
310 {
311  const auto minString = valueToStringLabel(min, decimals);
312  const auto maxString = valueToStringLabel(max, decimals);
313 
314  // Create a dummy label with same properties
315  QLabel dummyLabel;
316  dummyLabel.setFont(label->font());
317  dummyLabel.setStyleSheet(label->styleSheet());
318 
319  // Fill it with the string of the max value and request the size from Qt
320  dummyLabel.setText(maxString);
321  const QSize sizeWithMaxValue = dummyLabel.sizeHint();
322 
323  // Fill it with the string of the min value and request the size from Qt
324  dummyLabel.setText(minString);
325  const QSize sizeWithMinValue = dummyLabel.sizeHint();
326 
327  // Compute the maximum size and set it to our label
328  const QSize maxSize = sizeWithMaxValue.expandedTo(sizeWithMinValue);
329  label->setMinimumSize(maxSize);
330 }
331 
332 //------------------------------------------------------------------------------
333 
334 } //namespace guiQt
335 } //namespace editor
::fwCom::Signal< void(double, std::string) > DoubleChangedSignalType
Double changed signal type.
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
STL namespace.
::fwCom::Signal< void(int, std::string) > IntegerChangedSignalType
Integer changed signal type.
Generic editor to interact with parameters.
Definition: SParameters.hpp:95
#define fwCoreServiceClassDefinitionsMacro(_classinfo_)
Generate common code for services classes.
::fwCom::Signal< void(std::string, std::string) > EnumChangedSignalType
Enum changed signal type.
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21