ctkRangeWidget.h 8.2 KB

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