ctkRangeWidget.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.commontk.org/LICENSE
  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 __ctkRangeWidget_h
  15. #define __ctkRangeWidget_h
  16. // Qt includes
  17. #include <QSlider>
  18. // CTK includes
  19. #include <ctkPimpl.h>
  20. #include "ctkWidgetsExport.h"
  21. class ctkDoubleRangeSlider;
  22. class ctkRangeWidgetPrivate;
  23. ///
  24. /// ctkRangeWidget is a wrapper around a ctkDoubleRangeSlider and 2 QSpinBoxes
  25. /// \image html http://www.commontk.org/images/1/14/CtkRangeWidget.png
  26. /// \sa ctkSliderSpinBoxWidget, ctkDoubleRangeSlider, QSpinBox
  27. class CTK_WIDGETS_EXPORT ctkRangeWidget : public QWidget
  28. {
  29. Q_OBJECT
  30. Q_PROPERTY(int decimals READ decimals WRITE setDecimals)
  31. Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep)
  32. Q_PROPERTY(double minimum READ minimum WRITE setMinimum)
  33. Q_PROPERTY(double maximum READ maximum WRITE setMaximum)
  34. Q_PROPERTY(double minimumValue READ minimumValue WRITE setMinimumValue)
  35. Q_PROPERTY(double maximumValue READ maximumValue WRITE setMaximumValue)
  36. Q_PROPERTY(QString prefix READ prefix WRITE setPrefix)
  37. Q_PROPERTY(QString suffix READ suffix WRITE setSuffix)
  38. Q_PROPERTY(double tickInterval READ tickInterval WRITE setTickInterval)
  39. Q_PROPERTY(bool autoSpinBoxWidth READ isAutoSpinBoxWidth WRITE setAutoSpinBoxWidth)
  40. Q_PROPERTY(Qt::Alignment spinBoxTextAlignment READ spinBoxTextAlignment WRITE setSpinBoxTextAlignment)
  41. Q_PROPERTY(Qt::Alignment spinBoxAlignment READ spinBoxAlignment WRITE setSpinBoxAlignment)
  42. Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
  43. Q_PROPERTY(bool symmetricMoves READ symmetricMoves WRITE setSymmetricMoves)
  44. public:
  45. /// Superclass typedef
  46. typedef QWidget Superclass;
  47. /// Constructor
  48. /// If \li parent is null, ctkRangeWidget will be a top-leve widget
  49. /// \note The \li parent can be set later using QWidget::setParent()
  50. explicit ctkRangeWidget(QWidget* parent = 0);
  51. /// Destructor
  52. virtual ~ctkRangeWidget();
  53. ///
  54. /// This property holds the sliders and spinbox minimum value.
  55. /// FIXME: Test following specs.
  56. /// When setting this property, the maximum is adjusted if necessary
  57. /// to ensure that the range remains valid.
  58. /// Also the slider's current value is adjusted to be within the new range.
  59. double minimum()const;
  60. void setMinimum(double minimum);
  61. ///
  62. /// This property holds the sliders and spinbox minimum value.
  63. /// FIXME: Test following specs.
  64. /// When setting this property, the maximum is adjusted if necessary
  65. /// to ensure that the range remains valid.
  66. /// Also the slider's current value is adjusted to be within the new range.
  67. double maximum()const;
  68. void setMaximum(double maximum);
  69. /// Description
  70. /// Utility function that set the min/max in once
  71. void setRange(double min, double max);
  72. ///
  73. /// This property holds the slider and spinbox minimum value.
  74. /// ctkRangeWidget forces the value to be within the
  75. /// legal range: minimum <= minimumValue <= maximumValue <= maximum.
  76. double minimumValue()const;
  77. ///
  78. /// This property holds the slider and spinbox maximum value.
  79. /// ctkRangeWidget forces the value to be within the
  80. /// legal range: minimum <= minimumValue <= maximumValue <= maximum.
  81. double maximumValue()const;
  82. ///
  83. /// Utility function that returns both values at the same time
  84. /// Returns minimumValue and maximumValue
  85. void values(double &minValue, double &maxValue)const;
  86. ///
  87. /// This property holds the single step.
  88. /// The smaller of two natural steps that the
  89. /// slider provides and typically corresponds to the
  90. /// user pressing an arrow key.
  91. double singleStep()const;
  92. void setSingleStep(double step);
  93. ///
  94. /// This property holds the precision of the spin box, in decimals.
  95. /// Sets how many decimals the spinbox will use for displaying and
  96. /// interpreting doubles.
  97. int decimals()const;
  98. void setDecimals(int decimals);
  99. ///
  100. /// This property holds the spin box's prefix.
  101. /// The prefix is prepended to the start of the displayed value.
  102. /// Typical use is to display a unit of measurement or a currency symbol
  103. QString prefix()const;
  104. void setPrefix(const QString& prefix);
  105. ///
  106. /// This property holds the spin box's suffix.
  107. /// The suffix is appended to the end of the displayed value.
  108. /// Typical use is to display a unit of measurement or a currency symbol
  109. QString suffix()const;
  110. void setSuffix(const QString& suffix);
  111. ///
  112. /// This property holds the interval between tickmarks.
  113. /// This is a value interval, not a pixel interval.
  114. /// If it is 0, the slider will choose between lineStep() and pageStep().
  115. /// The default value is 0.
  116. double tickInterval()const;
  117. void setTickInterval(double ti);
  118. ///
  119. /// This property holds the alignment of the spin boxes.
  120. /// Possible Values are Qt::AlignTop, Qt::AlignBottom, and Qt::AlignVCenter.
  121. /// By default, the alignment is Qt::AlignVCenter
  122. void setSpinBoxAlignment(Qt::Alignment alignment);
  123. Qt::Alignment spinBoxAlignment()const;
  124. ///
  125. /// This property holds the alignment of the text inside the spin boxes.
  126. /// Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.
  127. /// By default, the alignment is Qt::AlignLeft
  128. void setSpinBoxTextAlignment(Qt::Alignment alignment);
  129. Qt::Alignment spinBoxTextAlignment()const;
  130. ///
  131. /// This property holds whether slider tracking is enabled.
  132. /// If tracking is enabled (the default), the widget emits the valueChanged()
  133. /// signal while the slider or spinbox is being dragged. If tracking is
  134. /// disabled, the widget emits the valueChanged() signal only when the user
  135. /// releases the slider or spinbox.
  136. void setTracking(bool enable);
  137. bool hasTracking()const;
  138. ///
  139. /// Set/Get the auto spinbox width
  140. /// When the autoSpinBoxWidth property is on, the width of the SpinBox is
  141. /// set to the same width of the largest QSpinBox of its
  142. // ctkRangeWidget siblings.
  143. bool isAutoSpinBoxWidth()const;
  144. void setAutoSpinBoxWidth(bool autoWidth);
  145. ///
  146. /// When symmetricMoves is true, moving a handle will move the other handle
  147. /// symmetrically, otherwise the handles are independent. False by default
  148. bool symmetricMoves()const;
  149. void setSymmetricMoves(bool symmetry);
  150. public slots:
  151. ///
  152. /// Reset the slider and spinbox to zero (value and position)
  153. void reset();
  154. void setMinimumValue(double value);
  155. void setMaximumValue(double value);
  156. ///
  157. /// Utility function that set the min and max values at once
  158. void setValues(double minValue, double maxValue);
  159. signals:
  160. /// Use with care:
  161. /// sliderMoved is emitted only when the user moves the slider
  162. //void sliderMoved(double position);
  163. void minimumValueChanged(double value);
  164. void minimumValueIsChanging(double value);
  165. void maximumValueChanged(double value);
  166. void maximumValueIsChanging(double value);
  167. void valuesChanged(double minValue, double maxValue);
  168. void rangeChanged(double min, double max);
  169. protected slots:
  170. void startChanging();
  171. void stopChanging();
  172. void changeValues(double newMinValue, double newMaxValue);
  173. void changeMinimumValue(double value);
  174. void changeMaximumValue(double value);
  175. void setMinimumToMaximumSpinBox(double minimum);
  176. void setMaximumToMinimumSpinBox(double maximum);
  177. void onSliderRangeChanged(double min, double max);
  178. protected:
  179. virtual bool eventFilter(QObject *obj, QEvent *event);
  180. /// can be used to change the slider by a custom one
  181. ctkDoubleRangeSlider* slider()const;
  182. void setSlider(ctkDoubleRangeSlider* slider);
  183. protected:
  184. QScopedPointer<ctkRangeWidgetPrivate> d_ptr;
  185. private:
  186. Q_DECLARE_PRIVATE(ctkRangeWidget);
  187. Q_DISABLE_COPY(ctkRangeWidget);
  188. };
  189. #endif