MIRA
PropertyEditorCommonDelegates.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_PROPERTYEDITORCOMMONDELEGATES_H_
48 #define _MIRA_PROPERTYEDITORCOMMONDELEGATES_H_
49 
50 #include <QSlider>
51 #include <QSpinBox>
52 #include <QComboBox>
53 #include <QDoubleSpinBox>
54 #include <QLineEdit>
55 #include <QCheckBox>
56 
57 #include <math/Saturate.h>
59 
61 
62 namespace mira {
63 
65 
66 class PropertyNode;
67 
68 class PropertyDelegate_Slider : public QSlider, public IPropertyDelegate_Edit
69 {
70  Q_OBJECT
71 public:
72  PropertyDelegate_Slider(PropertyNode* p, QWidget* parent);
73 
74  void initFromProperty() override;
75 public slots:
76  void slotSetValue(int value);
77 signals:
78  void propertyEdited() override;
79 };
80 
82 
84 {
85 public:
87  void handleEventFilter(QEvent* event);
88  double handleWheelEvent(QWheelEvent* event, QLineEdit* lineEdit, QChar decimalPoint, double defaultStep);
89  void highlightScrolledDigit(QLineEdit* lineEdit, QChar decimalPoint);
90  int scrolledDigit() const;
91 private:
92  bool mMouseMovedSinceLastScroll;
93  // <0 for digits behind decimal dot, >=0 for digits in front of decimal point, ==0, if no scrolled digit
94  int mScrolledDigit;
95  double mToAdd;
96 };
97 
99 
100 // helper functions, overloads allow selecting matching QString conversion function
101 bool stringToLL(const QString& input, int64& val);
102 bool stringToLL(const QString& input, uint64& val);
103 
108 template<typename T>
109 class LongSpinBoxBase : public QAbstractSpinBox
110 {
111 public:
112  explicit LongSpinBoxBase(QWidget* parent = 0) : QAbstractSpinBox(parent) {
113  lineEdit()->setText(textFromValue(m_value));
114  }
115 
116  T value() const
117  {
118  return m_value;
119  }
120 
121  T minimum() const
122  {
123  return m_minimum;
124  }
125 
126  void setMinimum(T min)
127  {
128  m_minimum = min;
129  m_maximum = std::max(min, m_maximum);
130  }
131 
132  T maximum() const
133  {
134  return m_maximum;
135  }
136 
137  void setMaximum(T max)
138  {
139  m_maximum = max;
140  m_minimum = std::min(max, m_minimum);
141  }
142 
143  void setRange(T min, T max)
144  {
145  setMinimum(min);
146  setMaximum(max);
147  }
148 
149  StepEnabled stepEnabled() const
150  {
151  return StepUpEnabled | StepDownEnabled;
152  }
153 
155  {
156  return m_step;
157  }
158 
160  {
161  m_step = std::max<T>(0, step);
162  }
163 
164  void stepBy(int steps) override
165  {
166  if (steps == 0)
167  return;
168 
169  auto new_value = m_value + steps * m_step;
170  if (steps < 0 && new_value > m_value) {
171  new_value = std::numeric_limits<T>::min();
172  } else if (steps > 0 && new_value < m_value) {
173  new_value = std::numeric_limits<T>::max();
174  }
175 
176  new_value = saturate(new_value, m_minimum, m_maximum);
177 
178  lineEdit()->setText(textFromValue(new_value));
179  setValue(new_value);
180  }
181 
182 protected:
183  QValidator::State validate(QString& input, int& pos) const override
184  {
185  T val;
186  bool ok = stringToLL(input, val);
187  if (!ok)
188  return QValidator::Invalid;
189 
190  if (val < m_minimum || val > m_maximum)
191  return QValidator::Invalid;
192 
193  return QValidator::Acceptable;
194  }
195 
196  T valueFromText(const QString& text) const
197  {
198  T val;
199  stringToLL(text, val);
200  return val;
201  }
202 
203  QString textFromValue(T val) const
204  {
205  return QString::number(val);
206  }
207 
208 public:
209  virtual void setValue(T val)
210  {
211  if (m_value == val)
212  return;
213 
214  m_value = val;
215  lineEdit()->setText(textFromValue(val));
216  }
217 
219  {
220  QString input = lineEdit()->text();
221  int pos;
222  if (QValidator::Acceptable == validate(input, pos))
223  setValue(valueFromText(input)); // this e.g. normalizes (valid) input like '1 '
224  else
225  lineEdit()->setText(textFromValue(m_value));
226  }
227 
228 protected:
233 };
234 
236  : public LongSpinBoxBase<int64>, private SpinBoxSmartScroll,
238 {
240 
241  // Q_OBJECT cannot be used in the base (template) class,
242  // so Qt properties/signals/slots are defined in this specialization
243  Q_OBJECT
244 
245  Q_PROPERTY(int64 minimum READ minimum WRITE setMinimum)
246  Q_PROPERTY(int64 maximum READ maximum WRITE setMaximum)
247 
248  Q_PROPERTY(int64 value READ value WRITE setValue NOTIFY valueChanged USER true)
249 
250 public:
251  PropertyDelegate_SpinBoxSigned(PropertyNode* p, QWidget* parent);
252 
253  void initFromProperty() override;
254 
256  void setValue(int64 val) override;
257 
259  template<typename T>
261  {
262  static_assert(std::is_integral<T>::value, "PropertyDelegate_SpinBoxSigned::limitTypeRange() requires integral type.");
263  static_assert(std::is_signed<T>::value, "PropertyDelegate_SpinBoxSigned::limitTypeRange() requires signed type.");
264 
265  setMinimum(propertyNode()->getHint<int64>("minimum", std::numeric_limits<T>::min()));
266  setMaximum(propertyNode()->getHint<int64>("maximum", std::numeric_limits<T>::max()));
267  }
268 
269 public slots:
270  void onEditFinished();
271  void slotSetValue(int64 value);
272 
273 signals:
274  void valueChanged(int64 v);
275  void propertyEdited() override;
276 
277 private:
278  void wheelEvent(QWheelEvent* event);
279 protected:
280  bool eventFilter(QObject* obj, QEvent* event);
281 };
282 
284  : public LongSpinBoxBase<uint64>, private SpinBoxSmartScroll,
286 {
288 
289  // Q_OBJECT cannot be used in the base (template) class,
290  // so Qt properties/signals/slots are defined in this specialization
291  Q_OBJECT
292 
293  Q_PROPERTY(uint64 minimum READ minimum WRITE setMinimum)
294  Q_PROPERTY(uint64 maximum READ maximum WRITE setMaximum)
295 
296  Q_PROPERTY(uint64 value READ value WRITE setValue NOTIFY valueChanged USER true)
297 
298 public:
299  PropertyDelegate_SpinBoxUnsigned(PropertyNode* p, QWidget* parent);
300 
301  void initFromProperty() override;
302 
304  void setValue(uint64 val) override;
305 
307  template<typename T>
309  {
310  static_assert(std::is_integral<T>::value, "PropertyDelegate_SpinBoxUnsigned::limitTypeRange() requires integral type.");
311  static_assert(!std::is_signed<T>::value, "PropertyDelegate_SpinBoxUnsigned::limitTypeRange() requires unsigned type.");
312 
313  setMinimum(propertyNode()->getHint<uint64>("minimum", std::numeric_limits<T>::min()));
314  setMaximum(propertyNode()->getHint<uint64>("maximum", std::numeric_limits<T>::max()));
315  }
316 
317 public slots:
318  void onEditFinished();
319  void slotSetValue(uint64 value);
320 
321 signals:
322  void valueChanged(uint64 v);
323  void propertyEdited() override;
324 
325 private:
326  void wheelEvent(QWheelEvent* event);
327 protected:
328  bool eventFilter(QObject* obj, QEvent* event);
329 };
330 
332 
334 {
335  Q_OBJECT
336 protected:
337  PropertyDelegate_ComboBoxBase(PropertyNode* p, QWidget* parent);
338 
339 public:
340  void initFromProperty() override;
341 
342 public slots:
343  void slotSetValue(int value);
344 signals:
345  void propertyEdited() override;
346 protected:
347  virtual QString getPropertyValue() = 0;
348  virtual void setPropertyValue(const QVariant& itemData) = 0;
349 };
350 
352 {
353 protected:
354  PropertyDelegate_ComboBoxIntegral(PropertyNode* p, QWidget* parent);
355 protected:
356  QString getPropertyValue() override;
357 };
358 
360 {
361 public:
364  {}
365 
366 protected:
367  void setPropertyValue(const QVariant& itemData) override;
368 };
369 
371 {
372 public:
375  {}
376 
377 protected:
378  void setPropertyValue(const QVariant& itemData) override;
379 };
380 
382 {
383 public:
384  PropertyDelegate_ComboBoxString(PropertyNode* p, QWidget* parent);
385 protected:
386  QString getPropertyValue() override;
387  void setPropertyValue(const QVariant& itemData) override;
388 };
389 
391 
393 {
394  Q_OBJECT
395 public:
396  PropertyDelegate_DoubleSlider(PropertyNode* p, QWidget* parent);
397 
398  void initFromProperty() override;
399 public slots:
400  void slotSetValue(int value);
401 signals:
402  void propertyEdited() override;
403 private:
404  double mMinimum;
405  double mMaximum;
406  double mStep;
407 };
408 
410 
411 class PropertyDelegate_DoubleSpinBox : public QDoubleSpinBox, private SpinBoxSmartScroll,
413 {
414  Q_OBJECT
415 public:
416  PropertyDelegate_DoubleSpinBox(PropertyNode* p, QWidget* parent);
417 
418  void initFromProperty() override;
419 
420  QString textFromValue(double value) const override;
421  double valueFromText(const QString& text) const override;
422  QValidator::State validate(QString& text, int& pos) const override;
423 public slots:
424  void slotSetValue(double value);
425 signals:
426  void propertyEdited() override;
427 public:
429  QLocale locale;
430 private:
431  void wheelEvent(QWheelEvent* event);
432 protected:
433  bool eventFilter(QObject* obj, QEvent* event);
434 };
435 
437 
438 class PropertyDelegate_LineEdit : public QLineEdit, public IPropertyDelegate_Edit
439 {
440  Q_OBJECT
441 public:
442  PropertyDelegate_LineEdit(PropertyNode* p, QWidget* parent);
443 
444  void initFromProperty() override;
445 public slots:
446  void slotSetValue();
447 signals:
448  void propertyEdited() override;
449 };
450 
452 
453 class PropertyDelegate_CheckBox : public QCheckBox, public IPropertyDelegate_Edit
454 {
455  Q_OBJECT
456 public:
457  PropertyDelegate_CheckBox(PropertyNode* p, QWidget* parent);
458 
459  void initFromProperty() override;
460 public slots:
461  void slotSetValue(bool value);
462 signals:
463  void propertyEdited() override;
464 };
465 
467 
468 }
469 
470 #endif
PropertyDelegate_LineEdit(PropertyNode *p, QWidget *parent)
Abstract base class for all derived property node classes.
Definition: PropertyNode.h:212
uint64_t uint64
Definition: Types.h:65
void setSingleStep(T step)
Definition: PropertyEditorCommonDelegates.h:159
void setPropertyValue(const QVariant &itemData) override
void onEditFinished()
Definition: PropertyEditorCommonDelegates.h:218
int precision
Definition: PropertyEditorCommonDelegates.h:428
Declaration and implementation of the property node hierarchy.
void limitRangeToType()
convenience function, sets limits matching a type&#39;s numeric limits (but PropertyHint takes priority!)...
Definition: PropertyEditorCommonDelegates.h:308
PropertyDelegate_DoubleSpinBox(PropertyNode *p, QWidget *parent)
T m_step
Definition: PropertyEditorCommonDelegates.h:231
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void setValue(int64 val) override
call Base::setValue and emit valueChanged(val)
QString textFromValue(T val) const
Definition: PropertyEditorCommonDelegates.h:203
void propertyEdited() override
Definition: PropertyEditorCommonDelegates.h:68
Definition: PropertyEditorDelegateUtils.h:73
double handleWheelEvent(QWheelEvent *event, QLineEdit *lineEdit, QChar decimalPoint, double defaultStep)
bool eventFilter(QObject *obj, QEvent *event)
virtual QString getPropertyValue()=0
void setPropertyValue(const QVariant &itemData) override
Definition: PropertyEditorCommonDelegates.h:438
QLocale locale
Definition: PropertyEditorCommonDelegates.h:429
T singleStep()
Definition: PropertyEditorCommonDelegates.h:154
Definition: PropertyEditorCommonDelegates.h:453
const T & saturate(const T &value, const T &minValue, const T &maxValue)
Saturate a value by limiting it to a certain interval.
Definition: Saturate.h:63
T m_value
Definition: PropertyEditorCommonDelegates.h:232
Saturation function for limiting a value to an interval.
Definition: PropertyEditorCommonDelegates.h:370
Utils for PropertyEditor delegates.
void highlightScrolledDigit(QLineEdit *lineEdit, QChar decimalPoint)
bool stringToLL(const QString &input, int64 &val)
virtual void setPropertyValue(const QVariant &itemData)=0
void stepBy(int steps) override
Definition: PropertyEditorCommonDelegates.h:164
Definition: PropertyEditorCommonDelegates.h:333
bool eventFilter(QObject *obj, QEvent *event)
void setPropertyValue(const QVariant &itemData) override
PropertyDelegate_ComboBoxIntegral(PropertyNode *p, QWidget *parent)
Definition: PropertyEditorCommonDelegates.h:381
QValidator::State validate(QString &input, int &pos) const override
Definition: PropertyEditorCommonDelegates.h:183
void setRange(T min, T max)
Definition: PropertyEditorCommonDelegates.h:143
T maximum() const
Definition: PropertyEditorCommonDelegates.h:132
T valueFromText(const QString &text) const
Definition: PropertyEditorCommonDelegates.h:196
T value() const
Definition: PropertyEditorCommonDelegates.h:116
void limitRangeToType()
convenience function, sets limits matching a type&#39;s numeric limits (but PropertyHint takes priority!)...
Definition: PropertyEditorCommonDelegates.h:260
PropertyDelegate_ComboBoxString(PropertyNode *p, QWidget *parent)
const PropertyNode * propertyNode() const
Definition: PropertyEditorDelegateUtils.h:88
Definition: PropertyEditorCommonDelegates.h:235
PropertyDelegate_Slider(PropertyNode *p, QWidget *parent)
void initFromProperty() override
void setValue(uint64 val) override
call Base::setValue and emit valueChanged(val)
PropertyDelegate_ComboBoxInt(PropertyNode *p, QWidget *parent)
Definition: PropertyEditorCommonDelegates.h:362
virtual void setValue(T val)
Definition: PropertyEditorCommonDelegates.h:209
Definition: PropertyEditorCommonDelegates.h:411
T m_maximum
Definition: PropertyEditorCommonDelegates.h:230
A spinbox for integral values of any type T (intended to be used with 64bit values), implemented by subclassing QAbstractSpinBox.
Definition: PropertyEditorCommonDelegates.h:109
int64_t int64
Definition: Types.h:61
void setMinimum(T min)
Definition: PropertyEditorCommonDelegates.h:126
LongSpinBoxBase(QWidget *parent=0)
Definition: PropertyEditorCommonDelegates.h:112
T m_minimum
Definition: PropertyEditorCommonDelegates.h:229
Definition: PropertyEditorCommonDelegates.h:83
PropertyHint step(const T &step)
Sets the attribute "step" to the specified value.
Definition: PropertyHint.h:265
PropertyDelegate_ComboBoxBase(PropertyNode *p, QWidget *parent)
Definition: PropertyEditorCommonDelegates.h:392
bool eventFilter(QObject *obj, QEvent *event)
PropertyDelegate_CheckBox(PropertyNode *p, QWidget *parent)
void setMaximum(T max)
Definition: PropertyEditorCommonDelegates.h:137
T minimum() const
Definition: PropertyEditorCommonDelegates.h:121
typename Reflector::ReflectState State
Definition: ReflectorMacros.h:59
StepEnabled stepEnabled() const
Definition: PropertyEditorCommonDelegates.h:149
PropertyDelegate_DoubleSlider(PropertyNode *p, QWidget *parent)
Definition: PropertyEditorCommonDelegates.h:351
Definition: PropertyEditorCommonDelegates.h:359
Definition: PropertyEditorCommonDelegates.h:283
double valueFromText(const QString &text) const override
void handleEventFilter(QEvent *event)
QValidator::State validate(QString &text, int &pos) const override
PropertyDelegate_ComboBoxUInt(PropertyNode *p, QWidget *parent)
Definition: PropertyEditorCommonDelegates.h:373
QString textFromValue(double value) const override