ctkDoubleSpinBox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 QLineEdit;
  24. class QObject;
  25. // CTK includes
  26. #include "ctkWidgetsExport.h"
  27. class ctkDoubleSpinBoxPrivate;
  28. /// \brief Custom SpinBox
  29. /// The ctkDoubleSpinBox internaly uses a QDoubleSpinBox while it retain controls
  30. /// over it.
  31. /// \sa ctkDoubleSlider, ctkSliderWidget, ctkRangeSlider
  32. class CTK_WIDGETS_EXPORT ctkDoubleSpinBox : public QWidget
  33. {
  34. Q_OBJECT
  35. Q_ENUMS(SetMode)
  36. Q_FLAGS(DecimalsOption DecimalsOptions)
  37. Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
  38. Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
  39. Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
  40. Q_PROPERTY(QString suffix READ suffix WRITE setSuffix)
  41. Q_PROPERTY(QString cleanText READ cleanText)
  42. /// This property holds the precision of the spin box, in decimals.
  43. /// Sets how many decimals the spinbox will use for displaying and
  44. /// interpreting doubles.
  45. /// If the flag DecimalsByShortcuts is set, decimals can be increased/decreased by
  46. /// Ctrl+/Ctrl-, Ctrl0 restores the original decimals value.
  47. /// If the flag DecimalsAsMax and/or DecimalsAsMin are set, decimals behave also
  48. /// as the max and/or min number of decimals settable by DecimalsByShortcuts,
  49. /// DecimalsByKey and DecimalsByValue.
  50. /// 2 by default.
  51. /// \sa decimalsOption, decimals(), setDecimals(), decimalsChanged
  52. Q_PROPERTY(int decimals READ decimals WRITE setDecimals NOTIFY decimalsChanged)
  53. /// This property provides more controls over the decimals.
  54. /// The default (DecimalsByShortcuts|InsertDecimals) behaves as a QDoubleSpinbox
  55. /// with an explicit control of decimals via shortcuts.
  56. /// \sa DecimalsOptions, decimals
  57. Q_PROPERTY(DecimalsOptions decimalsOption READ decimalsOption WRITE setDecimalsOption)
  58. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  59. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  60. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  61. /// \sa setMode, decimals
  62. Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged USER true)
  63. /// This property controls how setValue behaves.
  64. /// \sa SetMode, setMode(), setSetMode(), value
  65. Q_PROPERTY(SetMode setMode READ setMode WRITE setSetMode)
  66. /// This property controls whether decreasing the value by the mouse
  67. /// button or mouse wheel increases the value of the widget, and inverts the
  68. /// control similarly in the other way round or not. The property is switched off by
  69. /// default.
  70. /// \sa invertedControls(), setInvertedControls()
  71. Q_PROPERTY(bool invertedControls READ invertedControls WRITE setInvertedControls)
  72. public:
  73. /// SetMode enums for the spinbox behavior.
  74. /// SetAlways:
  75. /// No check is made and the given parameters is directly set
  76. /// on the internal QDoubleSpinBox.
  77. /// SetIfDifferent:
  78. /// Default mode, the given parameter is checked agains the
  79. /// current internal value and only set if they are different.
  80. /// For double, the comparison is based on the input parameters rounded
  81. /// with the current number of decimals (see round()).
  82. /// \sa setMode(), setSetMode(), round()
  83. enum SetMode
  84. {
  85. SetAlways,
  86. SetIfDifferent,
  87. };
  88. /// DecimalsOption enums the input style of the spinbox decimals.
  89. /// Default option is DecimalsByShortcuts.
  90. /// \sa decimals(), currentDecimals()
  91. enum DecimalsOption
  92. {
  93. /// Behaves just like a QDoubleSpinBox. The maximum number of decimals
  94. /// allowed is given by decimals().
  95. FixedDecimals = 0x000,
  96. /// When the spinbox has focus, entering the shortcut "CTRL +"
  97. /// adds decimals to the current number of decimals. "CTRL -" decreases the
  98. /// number of decimals and "CTRL 0" returns it to its original decimals()
  99. /// value.
  100. DecimalsByShortcuts = 0x001,
  101. /// Allow the number of decimals to be controlled by adding/removing digits
  102. /// with key strokes.
  103. /// \sa InsertDecimals, ReplaceDecimals
  104. DecimalsByKey = 0x002,
  105. /// Allow the number of decimals to be controlled by the value set by
  106. /// setValue().
  107. DecimalsByValue = 0x004,
  108. /// This flag controls whether inserted intermediate decimals increase the
  109. /// number of decimals (on) or not (off).
  110. /// It is incompatible with the ReplaceDecimals flag.
  111. /// \sa DecimalsByKey.
  112. InsertDecimals = 0x008,
  113. /// This flag controls whether inserted intermediate decimals replace the
  114. /// existing decimals (on) or not (off). This is similar to Insert but just
  115. /// for decimal digits.
  116. /// It is incompatible with the InsertDecimals flag.
  117. /// \sa DecimalsByKey, InsertDecimals
  118. ReplaceDecimals = 0x010,
  119. /// Use the "decimals" property as a maximum limit for the number of
  120. /// decimals.
  121. DecimalsAsMax = 0x020,
  122. /// Use the "decimals" property as a minimum limit for the number of
  123. /// decimals.
  124. DecimalsAsMin = 0x040,
  125. /// Even if the number of decimals is 0, it enforces the decimal to be visible
  126. /// (e.g. "0." )
  127. /// \sa decimals
  128. DecimalPointAlwaysVisible = 0x080
  129. };
  130. Q_DECLARE_FLAGS(DecimalsOptions, DecimalsOption)
  131. typedef QWidget Superclass;
  132. /// Constructor, creates a ctkDoubleSpinBox. The look and feel
  133. /// are the same as of a QDoubleSpinBox
  134. ctkDoubleSpinBox(QWidget* parent = 0);
  135. ctkDoubleSpinBox(ctkDoubleSpinBox::SetMode mode, QWidget* parent = 0);
  136. /// Get the spinbox current value
  137. /// \sa setValue(), cleanText()
  138. double value() const;
  139. /// Get the spinbox current displayed value
  140. /// \sa value(), cleanText()
  141. double displayedValue() const;
  142. /// Get the spinbox current text. This includes any prefix or suffix.
  143. /// \sa prefix(), suffix()
  144. QString text() const;
  145. /// Get the spinbox current text. This excludes any prefix or suffix.
  146. /// \sa value()
  147. QString cleanText() const;
  148. /// Set/Get the spinbox alignement
  149. Qt::Alignment alignment () const;
  150. void setAlignment (Qt::Alignment flag);
  151. /// Set/Get the frame
  152. void setFrame(bool frame);
  153. bool hasFrame() const;
  154. /// Add/Get a prefix to the displayed value. For example, one might want to
  155. /// add the $ sign.
  156. /// \sa suffix(), text()
  157. QString prefix() const;
  158. void setPrefix(const QString &prefix);
  159. /// Add/Get a suffix to the displayed value. For example, one might want to
  160. /// add the F (fahrenheit) sign.
  161. /// \sa prefix(), text()
  162. QString suffix() const;
  163. void setSuffix(const QString &suffix);
  164. /// Set/Get the single step. This represents by how much the value will
  165. /// decrease or increase when clicking the spinbox arrow or using stepUp or
  166. /// stepDown(). Default is 1.0.
  167. /// \sa setUp(), stepDown(), setDecimals().
  168. double singleStep() const;
  169. void setSingleStep(double value);
  170. /// Set/Get the range of the spinbox. Default range is [0.0, 9.9].
  171. double minimum() const;
  172. void setMinimum(double min);
  173. double maximum() const;
  174. void setMaximum(double max);
  175. void setRange(double min, double max);
  176. /// Set/Get number of displayed decimals.
  177. /// For a spinbox dealing only with integers, set this to 0.
  178. /// \sa ctkDoubleSpinBox::DecimalsOption
  179. int decimals() const;
  180. /// Returns the rounded value according to the number of decimals of
  181. /// the spinbox.
  182. /// \sa decimals()
  183. double round(double value) const;
  184. /// Get a pointer on the spinbox used internally. It can be useful to
  185. /// change display properties for example. To use with caution.
  186. /// \sa QDoubleSpinBox, lineEdit()
  187. QDoubleSpinBox* spinBox() const;
  188. /// Get a pointer on the line edit of the spinbox.
  189. /// \sa QLineEdit, spinBox()
  190. QLineEdit* lineEdit()const;
  191. /// Set the spinbox mode when using a set*() method.
  192. //// \sa round(), setValue(), setValueIfDifferent(), setValueAlways()
  193. ctkDoubleSpinBox::SetMode setMode() const;
  194. void setSetMode(SetMode mode);
  195. /// Set/Get the option used to input the decimals.
  196. /// \sa ctkDoubleSpinBox::DecimalsOption
  197. ctkDoubleSpinBox::DecimalsOptions decimalsOption();
  198. void setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions option);
  199. /// This property holds whether or not the spin box inverts its wheel and key
  200. /// events.
  201. /// If this property is false, scrolling the mouse wheel "up" and using keys
  202. /// like page up will increase the spinbox's value towards its maximum.
  203. /// Otherwise pressing page up will move value towards the slider's minimum.
  204. void setInvertedControls(bool invertedControls);
  205. bool invertedControls() const;
  206. public Q_SLOTS:
  207. /// Set the value of the spinbox following the current mode.
  208. /// \sa setMode(), value(), setValueIfDifferent(), setValueAlways()
  209. void setValue(double value);
  210. /// Set the value of the spinbox followin the SetIfDifferent mode.
  211. /// \sa value(), setValue(), setMode(), setValueAlways()
  212. void setValueIfDifferent(double value);
  213. /// Set the value of the spinbox following the SetAlways mode.
  214. /// \sa value(), setValue(), setMode(), setValueIfDifferent()
  215. void setValueAlways(double value);
  216. /// Increase/Decrease the current value by a single step.
  217. /// \sa value(), singleStep()
  218. void stepUp();
  219. void stepDown();
  220. /// Set the decimals property value.
  221. /// \sa decimals
  222. void setDecimals(int decimal);
  223. Q_SIGNALS:
  224. /// Emitted everytime the spinbox value is modified
  225. /// \sa QDoubleSpinBox::valueChanged()
  226. void valueChanged(double);
  227. void valueChanged(const QString &);
  228. /// Simple broadcast of the QAbstractSpinbox::editingFinished
  229. /// \sa QAbstractSpinbox::editingFinished
  230. void editingFinished();
  231. /// Signal emitted when the decimals of the displayed are changed.
  232. void decimalsChanged(int);
  233. protected:
  234. ctkDoubleSpinBoxPrivate* const d_ptr;
  235. /// Reimplemented to support shortcuts.
  236. virtual void keyPressEvent(QKeyEvent* event);
  237. /// Reimplemented to support shortcuts on the double spinbox.
  238. virtual bool eventFilter(QObject *obj, QEvent *event);
  239. private:
  240. Q_DECLARE_PRIVATE(ctkDoubleSpinBox);
  241. Q_DISABLE_COPY(ctkDoubleSpinBox);
  242. };
  243. Q_DECLARE_METATYPE(ctkDoubleSpinBox::SetMode)
  244. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkDoubleSpinBox::DecimalsOptions)
  245. #endif //__ctkDoubleSpinBox_h