ctkSpinBox.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 __ctkSpinBox_h
  15. #define __ctkSpinBox_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 ctkSpinBoxPrivate;
  27. /// \brief Custom SpinBox
  28. /// The ctkSpinBox internaly uses a QDoubleSpinBox while it retain controls
  29. /// over it.
  30. /// \sa ctkDoubleSlider, ctkSliderWidget, ctkRangeSlider
  31. class CTK_WIDGETS_EXPORT ctkSpinBox : 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. public:
  49. /// SetMode enums for the spinbox behavior.
  50. /// SetAlways:
  51. /// No check is made and the given parameters is directly set
  52. /// on the internal QDoubleSpinBox.
  53. /// SetIfDifferent:
  54. /// Default mode, the given parameter is checked agains the
  55. /// current internal value and only set if they are different.
  56. /// For double, the comparison is based on the input parameters rounded
  57. /// with the current number of decimals (see round()).
  58. /// \sa setMode(), setSetMode(), round()
  59. enum SetMode
  60. {
  61. SetAlways,
  62. SetIfDifferent,
  63. };
  64. /// DecimalsOption enums the input style of the spinbox decimals.
  65. /// Fixed:
  66. /// Behaves just like a QDoubleSpinBox. The maximum number of decimals
  67. /// allowed is given by decimals().
  68. /// UseShortcuts:
  69. /// When the spinbox has focus, entering the shortcut "CTRL +"
  70. /// adds decimals to the current number of decimals. "CTRL -" decreases the
  71. /// number of decimals and "CTRL 0" returns it to its original decimals()
  72. /// value.
  73. ///
  74. /// Default option is UseShortcuts.
  75. /// \sa decimals(), currentDecimals()
  76. enum DecimalsOption
  77. {
  78. Fixed = 0x0,
  79. UseShortcuts = 0x01,
  80. };
  81. Q_DECLARE_FLAGS(DecimalsOptions, DecimalsOption)
  82. typedef QWidget Superclass;
  83. /// Constructor, creates a ctkSpinBox. The look and feel
  84. /// are the same as of a QDoubleSpinBox
  85. ctkSpinBox(QWidget* parent = 0);
  86. ctkSpinBox(ctkSpinBox::SetMode mode, QWidget* parent = 0);
  87. /// Get the spinbox current value
  88. /// \sa setValue(), cleanText()
  89. double value() const;
  90. /// Get the spinbox current displayed value
  91. /// \sa value(), cleanText()
  92. double displayedValue() const;
  93. /// Get the spinbox current text. This includes any prefix or suffix.
  94. /// \sa prefix(), suffix()
  95. QString text() const;
  96. /// Get the spinbox current text. This excludes any prefix or suffix.
  97. /// \sa value()
  98. QString cleanText() const;
  99. /// Set/Get the spinbox alignement
  100. Qt::Alignment alignment () const;
  101. void setAlignment (Qt::Alignment flag);
  102. /// Set/Get the frame
  103. void setFrame(bool frame);
  104. bool hasFrame() const;
  105. /// Add/Get a prefix to the displayed value. For example, one might want to
  106. /// add the $ sign.
  107. /// \sa suffix(), text()
  108. QString prefix() const;
  109. void setPrefix(const QString &prefix);
  110. /// Add/Get a suffix to the displayed value. For example, one might want to
  111. /// add the F (fahrenheit) sign.
  112. /// \sa prefix(), text()
  113. QString suffix() const;
  114. void setSuffix(const QString &suffix);
  115. /// Set/Get the single step. This represents by how much the value will
  116. /// decrease or increase when clicking the spinbox arrow or using stepUp or
  117. /// stepDown(). Default is 1.0.
  118. /// \sa setUp(), stepDown(), setDecimals().
  119. double singleStep() const;
  120. void setSingleStep(double value);
  121. /// Set/Get the range of the spinbox. Default range is [0.0, 9.9].
  122. double minimum() const;
  123. void setMinimum(double min);
  124. double maximum() const;
  125. void setMaximum(double max);
  126. void setRange(double min, double max);
  127. /// Set/Get number of displayed decimals.
  128. /// For a spinbox dealing only with integers, set this to 0.
  129. /// \sa ctkSpinBox::DecimalsOption
  130. int decimals() const;
  131. void setDecimals(int decimal);
  132. /// Returns the rounded value according to the number of decimals of
  133. /// the spinbox.
  134. /// \sa decimals()
  135. double round(double value) const;
  136. /// Get a pointer on the spinbox used internally. It can be useful to
  137. /// change display properties for example. To use with caution.
  138. /// \sa QDoubleSpinBox::QDoubleSpinBox
  139. QDoubleSpinBox* spinBox() const;
  140. /// Set the spinbox mode when using a set*() method.
  141. //// \sa round(), setValue(), setValueIfDifferent(), setValueAlways()
  142. ctkSpinBox::SetMode setMode() const;
  143. void setSetMode(SetMode mode);
  144. /// Set/Get the option used to input the decimals.
  145. /// \sa ctkSpinBox::DecimalsOption
  146. ctkSpinBox::DecimalsOptions decimalsOption();
  147. void setDecimalsOption(ctkSpinBox::DecimalsOptions option);
  148. public Q_SLOTS:
  149. /// Set the value of the spinbox following the current mode.
  150. /// \sa setMode(), value(), setValueIfDifferent(), setValueAlways()
  151. void setValue(double value);
  152. /// Set the value of the spinbox followin the SetIfDifferent mode.
  153. /// \sa value(), setValue(), setMode(), setValueAlways()
  154. void setValueIfDifferent(double value);
  155. /// Set the value of the spinbox following the SetAlways mode.
  156. /// \sa value(), setValue(), setMode(), setValueIfDifferent()
  157. void setValueAlways(double value);
  158. /// Increase/Decrease the current value by a single step.
  159. /// \sa value(), singleStep()
  160. void stepUp();
  161. void stepDown();
  162. Q_SIGNALS:
  163. /// Emitted everytime the spinbox value is modified
  164. /// \sa QDoubleSpinBox::valueChanged()
  165. void valueChanged(double);
  166. void valueChanged(const QString &);
  167. /// Simple broadcast of the QAbstractSpinbox::editingFinished
  168. /// \sa QAbstractSpinbox::editingFinished
  169. void editingFinished();
  170. /// Signal emitted when the decimals of the displayed are changed.
  171. void decimalsChanged(int);
  172. protected:
  173. ctkSpinBoxPrivate* const d_ptr;
  174. bool eventFilter(QObject *obj, QEvent *event);
  175. private:
  176. Q_DECLARE_PRIVATE(ctkSpinBox);
  177. Q_DISABLE_COPY(ctkSpinBox);
  178. };
  179. Q_DECLARE_METATYPE(ctkSpinBox::SetMode)
  180. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkSpinBox::DecimalsOptions)
  181. #endif //__ctkSpinBox_h