ctkSpinBox.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // CTK includes
  22. #include "ctkWidgetsExport.h"
  23. class ctkSpinBoxPrivate;
  24. /// \brief Custom SpinBox
  25. /// The ctkSpinBox internaly uses a QDoubleSpinBox while it retain controls
  26. /// over it.
  27. /// \sa ctkDoubleSlider, ctkSliderWidget, ctkRangeSlider
  28. class CTK_WIDGETS_EXPORT ctkSpinBox : public QWidget
  29. {
  30. Q_OBJECT
  31. Q_ENUMS(SetMode)
  32. Q_PROPERTY(SetMode setMode READ setMode WRITE setSetMode)
  33. Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
  34. Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
  35. Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
  36. Q_PROPERTY(QString suffix READ suffix WRITE setSuffix)
  37. Q_PROPERTY(QString cleanText READ cleanText)
  38. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  39. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  40. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  41. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  42. Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged USER true)
  43. public:
  44. /// SetMode enums for the spinbox behavior.
  45. /// SetAlways:
  46. /// No check is made and the given parameters is directly set
  47. /// on the internal QDoubleSpinBox.
  48. /// SetIfDifferent:
  49. /// Default mode, the given parameter is checked agains the
  50. /// current internal value and only set if they are different.
  51. /// For double, the comparison is based on the input parameters rounded
  52. /// with the current number of decimals (see round()).
  53. /// \sa setMode(), setSetMode(), round()
  54. enum SetMode
  55. {
  56. SetAlways,
  57. SetIfDifferent,
  58. };
  59. typedef QWidget Superclass;
  60. /// Constructor, creates a ctkSpinBox. The look and feel
  61. /// are the same as of a QDoubleSpinBox
  62. ctkSpinBox(QWidget* parent = 0);
  63. ctkSpinBox(ctkSpinBox::SetMode mode, QWidget* parent = 0);
  64. /// Get the spinbox current value
  65. /// \sa setValue(), cleanText()
  66. double value() const;
  67. /// Get the spinbox current displayed value
  68. /// \sa value(), cleanText()
  69. double displayedValue() const;
  70. /// Get the spinbox current text. This includes any prefix or suffix.
  71. /// \sa prefix(), suffix()
  72. QString text() const;
  73. /// Get the spinbox current text. This excludes any prefix or suffix.
  74. /// \sa value()
  75. QString cleanText() const;
  76. /// Set/Get the spinbox alignement
  77. Qt::Alignment alignment () const;
  78. void setAlignment (Qt::Alignment flag);
  79. /// Set/Get the frame
  80. void setFrame(bool frame);
  81. bool hasFrame() const;
  82. /// Add/Get a prefix to the displayed value. For example, one might want to
  83. /// add the $ sign.
  84. /// \sa suffix(), text()
  85. QString prefix() const;
  86. void setPrefix(const QString &prefix);
  87. /// Add/Get a suffix to the displayed value. For example, one might want to
  88. /// add the F (fahrenheit) sign.
  89. /// \sa prefix(), text()
  90. QString suffix() const;
  91. void setSuffix(const QString &suffix);
  92. /// Set/Get the single step. This represents by how much the value will
  93. /// decrease or increase when clicking the spinbox arrow or using stepUp or
  94. /// stepDown(). Default is 1.0.
  95. /// \sa setUp(), stepDown(), setDecimals().
  96. double singleStep() const;
  97. void setSingleStep(double value);
  98. /// Set/Get the range of the spinbox. Default range is [0.0, 9.9].
  99. double minimum() const;
  100. void setMinimum(double min);
  101. double maximum() const;
  102. void setMaximum(double max);
  103. void setRange(double min, double max);
  104. /// Set/Get number of decimals displayed. For a spinbox dealing only with
  105. /// integers, set this to 0.
  106. /// \sa addOneDecimal(), removeOneDecimal()
  107. int decimals() const;
  108. void setDecimals(int decimal);
  109. /// Returns the rounded value according to the number of decimals of
  110. /// the spinbox.
  111. /// \sa decimals()
  112. double round(double value) const;
  113. /// Get a pointer on the spinbox used internally. It can be useful to
  114. /// change display properties for example. To use with caution.
  115. /// \sa QDoubleSpinBox::QDoubleSpinBox
  116. QDoubleSpinBox* spinBox() const;
  117. /// Set the spinbox mode when using a set*() method.
  118. //// \sa round(), setValue(), setValueIfDifferent(), setValueAlways()
  119. ctkSpinBox::SetMode setMode() const;
  120. void setSetMode(SetMode mode);
  121. public Q_SLOTS:
  122. /// Set the value of the spinbox following the current mode.
  123. /// \sa setMode(), value(), setValueIfDifferent(), setValueAlways()
  124. void setValue(double value);
  125. /// Set the value of the spinbox followin the SetIfDifferent mode.
  126. /// \sa value(), setValue(), setMode(), setValueAlways()
  127. void setValueIfDifferent(double value);
  128. /// Set the value of the spinbox following the SetAlways mode.
  129. /// \sa value(), setValue(), setMode(), setValueIfDifferent()
  130. void setValueAlways(double value);
  131. /// Increase/Decrease the current value by a single step.
  132. /// \sa value(), singleStep()
  133. void stepUp();
  134. void stepDown();
  135. Q_SIGNALS:
  136. /// Emitted everytime the spinbox value is modified
  137. /// \sa QDoubleSpinBox::valueChanged()
  138. void valueChanged(double);
  139. void valueChanged(const QString &);
  140. /// Simple broadcast of the QAbstractSpinbox::editingFinished
  141. /// \sa QAbstractSpinbox::editingFinished
  142. void editingFinished();
  143. protected:
  144. ctkSpinBoxPrivate* const d_ptr;
  145. private:
  146. Q_DECLARE_PRIVATE(ctkSpinBox);
  147. Q_DISABLE_COPY(ctkSpinBox);
  148. };
  149. Q_DECLARE_METATYPE(ctkSpinBox::SetMode)
  150. #endif //__ctkSpinBox_h