ctkDoubleSpinBox.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkDoubleSpinBox_h
  15. #define __ctkDoubleSpinBox_h
  16. // Qt includes
  17. #include <QMetaType>
  18. #include <QString>
  19. #include <QWidget>
  20. class QDoubleSpinBox;
  21. class QEvent;
  22. class QKeyEvent;
  23. class QObject;
  24. // CTK includes
  25. #include "ctkWidgetsExport.h"
  26. class ctkDoubleSpinBoxPrivate;
  27. /// \brief Custom SpinBox
  28. /// The ctkDoubleSpinBox internaly uses a QDoubleSpinBox while it retain controls
  29. /// over it.
  30. /// \sa ctkDoubleSlider, ctkSliderWidget, ctkRangeSlider
  31. class CTK_WIDGETS_EXPORT ctkDoubleSpinBox : public QWidget
  32. {
  33. Q_OBJECT
  34. Q_ENUMS(SetMode)
  35. Q_PROPERTY(SetMode setMode READ setMode WRITE setSetMode)
  36. Q_FLAGS(DecimalsOption DecimalsOptions)
  37. Q_PROPERTY(DecimalsOptions decimalsOption READ decimalsOption WRITE setDecimalsOption)
  38. Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
  39. Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
  40. Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
  41. Q_PROPERTY(QString suffix READ suffix WRITE setSuffix)
  42. Q_PROPERTY(QString cleanText READ cleanText)
  43. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  44. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  45. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  46. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  47. Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged USER true)
  48. /// This property controls whether decreasing the value by the mouse
  49. /// button or mouse wheel increases the value of the widget, and inverts the
  50. /// control similarly in the other way round or not. The property is switched off by
  51. /// default.
  52. /// \sa invertedControls(), setInvertedControls()
  53. Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
  54. public:
  55. /// SetMode enums for the spinbox behavior.
  56. /// SetAlways:
  57. /// No check is made and the given parameters is directly set
  58. /// on the internal QDoubleSpinBox.
  59. /// SetIfDifferent:
  60. /// Default mode, the given parameter is checked agains the
  61. /// current internal value and only set if they are different.
  62. /// For double, the comparison is based on the input parameters rounded
  63. /// with the current number of decimals (see round()).
  64. /// \sa setMode(), setSetMode(), round()
  65. enum SetMode
  66. {
  67. SetAlways,
  68. SetIfDifferent,
  69. };
  70. /// DecimalsOption enums the input style of the spinbox decimals.
  71. /// Fixed:
  72. /// Behaves just like a QDoubleSpinBox. The maximum number of decimals
  73. /// allowed is given by decimals().
  74. /// UseShortcuts:
  75. /// When the spinbox has focus, entering the shortcut "CTRL +"
  76. /// adds decimals to the current number of decimals. "CTRL -" decreases the
  77. /// number of decimals and "CTRL 0" returns it to its original decimals()
  78. /// value.
  79. ///
  80. /// Default option is UseShortcuts.
  81. /// \sa decimals(), currentDecimals()
  82. enum DecimalsOption
  83. {
  84. Fixed = 0x0,
  85. UseShortcuts = 0x01,
  86. };
  87. Q_DECLARE_FLAGS(DecimalsOptions, DecimalsOption)
  88. typedef QWidget Superclass;
  89. /// Constructor, creates a ctkDoubleSpinBox. The look and feel
  90. /// are the same as of a QDoubleSpinBox
  91. ctkDoubleSpinBox(QWidget* parent = 0);
  92. ctkDoubleSpinBox(ctkDoubleSpinBox::SetMode mode, QWidget* parent = 0);
  93. /// Get the spinbox current value
  94. /// \sa setValue(), cleanText()
  95. double value() const;
  96. /// Get the spinbox current displayed value
  97. /// \sa value(), cleanText()
  98. double displayedValue() const;
  99. /// Get the spinbox current text. This includes any prefix or suffix.
  100. /// \sa prefix(), suffix()
  101. QString text() const;
  102. /// Get the spinbox current text. This excludes any prefix or suffix.
  103. /// \sa value()
  104. QString cleanText() const;
  105. /// Set/Get the spinbox alignement
  106. Qt::Alignment alignment () const;
  107. void setAlignment (Qt::Alignment flag);
  108. /// Set/Get the frame
  109. void setFrame(bool frame);
  110. bool hasFrame() const;
  111. /// Add/Get a prefix to the displayed value. For example, one might want to
  112. /// add the $ sign.
  113. /// \sa suffix(), text()
  114. QString prefix() const;
  115. void setPrefix(const QString &prefix);
  116. /// Add/Get a suffix to the displayed value. For example, one might want to
  117. /// add the F (fahrenheit) sign.
  118. /// \sa prefix(), text()
  119. QString suffix() const;
  120. void setSuffix(const QString &suffix);
  121. /// Set/Get the single step. This represents by how much the value will
  122. /// decrease or increase when clicking the spinbox arrow or using stepUp or
  123. /// stepDown(). Default is 1.0.
  124. /// \sa setUp(), stepDown(), setDecimals().
  125. double singleStep() const;
  126. void setSingleStep(double value);
  127. /// Set/Get the range of the spinbox. Default range is [0.0, 9.9].
  128. double minimum() const;
  129. void setMinimum(double min);
  130. double maximum() const;
  131. void setMaximum(double max);
  132. void setRange(double min, double max);
  133. /// Set/Get number of displayed decimals.
  134. /// For a spinbox dealing only with integers, set this to 0.
  135. /// \sa ctkDoubleSpinBox::DecimalsOption
  136. int decimals() const;
  137. void setDecimals(int decimal);
  138. /// Returns the rounded value according to the number of decimals of
  139. /// the spinbox.
  140. /// \sa decimals()
  141. double round(double value) const;
  142. /// Get a pointer on the spinbox used internally. It can be useful to
  143. /// change display properties for example. To use with caution.
  144. /// \sa QDoubleSpinBox::QDoubleSpinBox
  145. QDoubleSpinBox* spinBox() const;
  146. /// Set the spinbox mode when using a set*() method.
  147. //// \sa round(), setValue(), setValueIfDifferent(), setValueAlways()
  148. ctkDoubleSpinBox::SetMode setMode() const;
  149. void setSetMode(SetMode mode);
  150. /// Set/Get the option used to input the decimals.
  151. /// \sa ctkDoubleSpinBox::DecimalsOption
  152. ctkDoubleSpinBox::DecimalsOptions decimalsOption();
  153. void setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option);
  154. /// This property holds whether or not the spin box inverts its wheel and key
  155. /// events.
  156. /// If this property is false, scrolling the mouse wheel "up" and using keys
  157. /// like page up will increase the spinbox's value towards its maximum.
  158. /// Otherwise pressing page up will move value towards the slider's minimum.
  159. void setInvertedControls(bool invertedControls);
  160. bool invertedControls() const;
  161. public Q_SLOTS:
  162. /// Set the value of the spinbox following the current mode.
  163. /// \sa setMode(), value(), setValueIfDifferent(), setValueAlways()
  164. void setValue(double value);
  165. /// Set the value of the spinbox followin the SetIfDifferent mode.
  166. /// \sa value(), setValue(), setMode(), setValueAlways()
  167. void setValueIfDifferent(double value);
  168. /// Set the value of the spinbox following the SetAlways mode.
  169. /// \sa value(), setValue(), setMode(), setValueIfDifferent()
  170. void setValueAlways(double value);
  171. /// Increase/Decrease the current value by a single step.
  172. /// \sa value(), singleStep()
  173. void stepUp();
  174. void stepDown();
  175. Q_SIGNALS:
  176. /// Emitted everytime the spinbox value is modified
  177. /// \sa QDoubleSpinBox::valueChanged()
  178. void valueChanged(double);
  179. void valueChanged(const QString &);
  180. /// Simple broadcast of the QAbstractSpinbox::editingFinished
  181. /// \sa QAbstractSpinbox::editingFinished
  182. void editingFinished();
  183. /// Signal emitted when the decimals of the displayed are changed.
  184. void decimalsChanged(int);
  185. protected:
  186. ctkDoubleSpinBoxPrivate* const d_ptr;
  187. bool eventFilter(QObject *obj, QEvent *event);
  188. private:
  189. Q_DECLARE_PRIVATE(ctkDoubleSpinBox);
  190. Q_DISABLE_COPY(ctkDoubleSpinBox);
  191. };
  192. Q_DECLARE_METATYPE(ctkDoubleSpinBox::SetMode)
  193. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkDoubleSpinBox::DecimalsOptions)
  194. #endif //__ctkDoubleSpinBox_h