ctkDoubleSpinBox.h 12 KB

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