ctkRangeSlider.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 __ctkRangeSlider_h
  15. #define __ctkRangeSlider_h
  16. // Qt includes
  17. #include <QSlider>
  18. // CTK includes
  19. #include <ctkPimpl.h>
  20. #include "ctkWidgetsExport.h"
  21. class QStylePainter;
  22. class ctkRangeSliderPrivate;
  23. /// \ingroup Widgets
  24. ///
  25. /// A ctkRangeSlider is a slider that lets you input 2 values instead of one
  26. /// (see QSlider). These values are typically a lower and upper bound.
  27. /// Values are comprised between the range of the slider. See setRange(),
  28. /// minimum() and maximum(). The upper bound can't be smaller than the
  29. /// lower bound and vice-versa.
  30. /// When setting new values (setMinimumValue(), setMaximumValue() or
  31. /// setValues()), make sure they lie between the range (minimum(), maximum())
  32. /// of the slider, they would be forced otherwised. If it is not the behavior
  33. /// you desire, you can set the range first (setRange(), setMinimum(),
  34. /// setMaximum())
  35. /// TODO: support triggerAction(QAbstractSlider::SliderSingleStepSub) that
  36. /// moves both values at a time.
  37. /// \sa ctkDoubleRangeSlider, ctkDoubleSlider, ctkRangeWidget
  38. class CTK_WIDGETS_EXPORT ctkRangeSlider : public QSlider
  39. {
  40. Q_OBJECT
  41. Q_PROPERTY(int minimumValue READ minimumValue WRITE setMinimumValue)
  42. Q_PROPERTY(int maximumValue READ maximumValue WRITE setMaximumValue)
  43. Q_PROPERTY(int minimumPosition READ minimumPosition WRITE setMinimumPosition)
  44. Q_PROPERTY(int maximumPosition READ maximumPosition WRITE setMaximumPosition)
  45. Q_PROPERTY(bool symmetricMoves READ symmetricMoves WRITE setSymmetricMoves)
  46. Q_PROPERTY(QString handleToolTip READ handleToolTip WRITE setHandleToolTip)
  47. public:
  48. // Superclass typedef
  49. typedef QSlider Superclass;
  50. /// Constructor, builds a ctkRangeSlider that ranges from 0 to 100 and has
  51. /// a lower and upper values of 0 and 100 respectively, other properties
  52. /// are set the QSlider default properties.
  53. explicit ctkRangeSlider( Qt::Orientation o, QWidget* par= 0 );
  54. explicit ctkRangeSlider( QWidget* par = 0 );
  55. virtual ~ctkRangeSlider();
  56. ///
  57. /// This property holds the slider's current minimum value.
  58. /// The slider silently forces minimumValue to be within the legal range:
  59. /// minimum() <= minimumValue() <= maximumValue() <= maximum().
  60. /// Changing the minimumValue also changes the minimumPosition.
  61. int minimumValue() const;
  62. ///
  63. /// This property holds the slider's current maximum value.
  64. /// The slider forces the maximum value to be within the legal range:
  65. /// The slider silently forces maximumValue to be within the legal range:
  66. /// Changing the maximumValue also changes the maximumPosition.
  67. int maximumValue() const;
  68. ///
  69. /// This property holds the current slider minimum position.
  70. /// If tracking is enabled (the default), this is identical to minimumValue.
  71. int minimumPosition() const;
  72. void setMinimumPosition(int min);
  73. ///
  74. /// This property holds the current slider maximum position.
  75. /// If tracking is enabled (the default), this is identical to maximumValue.
  76. int maximumPosition() const;
  77. void setMaximumPosition(int max);
  78. ///
  79. /// Utility function that set the minimum position and
  80. /// maximum position at once.
  81. void setPositions(int min, int max);
  82. ///
  83. /// When symmetricMoves is true, moving a handle will move the other handle
  84. /// symmetrically, otherwise the handles are independent. False by default
  85. bool symmetricMoves()const;
  86. void setSymmetricMoves(bool symmetry);
  87. ///
  88. /// Controls the text to display for the handle tooltip. It is in addition
  89. /// to the widget tooltip.
  90. /// "%1" is replaced by the current value of the slider.
  91. /// Empty string (by default) means no tooltip.
  92. QString handleToolTip()const;
  93. void setHandleToolTip(const QString& toolTip);
  94. Q_SIGNALS:
  95. ///
  96. /// This signal is emitted when the slider minimum value has changed,
  97. /// with the new slider value as argument.
  98. void minimumValueChanged(int min);
  99. ///
  100. /// This signal is emitted when the slider maximum value has changed,
  101. /// with the new slider value as argument.
  102. void maximumValueChanged(int max);
  103. ///
  104. /// Utility signal that is fired when minimum or maximum values have changed.
  105. void valuesChanged(int min, int max);
  106. ///
  107. /// This signal is emitted when sliderDown is true and the slider moves.
  108. /// This usually happens when the user is dragging the minimum slider.
  109. /// The value is the new slider minimum position.
  110. /// This signal is emitted even when tracking is turned off.
  111. void minimumPositionChanged(int min);
  112. ///
  113. /// This signal is emitted when sliderDown is true and the slider moves.
  114. /// This usually happens when the user is dragging the maximum slider.
  115. /// The value is the new slider maximum position.
  116. /// This signal is emitted even when tracking is turned off.
  117. void maximumPositionChanged(int max);
  118. ///
  119. /// Utility signal that is fired when minimum or maximum positions
  120. /// have changed.
  121. void positionsChanged(int min, int max);
  122. public Q_SLOTS:
  123. ///
  124. /// This property holds the slider's current minimum value.
  125. /// The slider silently forces min to be within the legal range:
  126. /// minimum() <= min <= maximumValue() <= maximum().
  127. /// Note: Changing the minimumValue also changes the minimumPosition.
  128. /// \sa stMaximumValue, setValues, setMinimum, setMaximum, setRange
  129. void setMinimumValue(int min);
  130. ///
  131. /// This property holds the slider's current maximum value.
  132. /// The slider silently forces max to be within the legal range:
  133. /// minimum() <= minimumValue() <= max <= maximum().
  134. /// Note: Changing the maximumValue also changes the maximumPosition.
  135. /// \sa stMinimumValue, setValues, setMinimum, setMaximum, setRange
  136. void setMaximumValue(int max);
  137. ///
  138. /// Utility function that set the minimum value and maximum value at once.
  139. /// The slider silently forces min and max to be within the legal range:
  140. /// minimum() <= min <= max <= maximum().
  141. /// Note: Changing the minimumValue and maximumValue also changes the
  142. /// minimumPosition and maximumPosition.
  143. /// \sa setMinimumValue, setMaximumValue, setMinimum, setMaximum, setRange
  144. void setValues(int min, int max);
  145. protected Q_SLOTS:
  146. void onRangeChanged(int minimum, int maximum);
  147. protected:
  148. ctkRangeSlider( ctkRangeSliderPrivate* impl, Qt::Orientation o, QWidget* par= 0 );
  149. ctkRangeSlider( ctkRangeSliderPrivate* impl, QWidget* par = 0 );
  150. // Description:
  151. // Standard Qt UI events
  152. virtual void mousePressEvent(QMouseEvent* ev);
  153. virtual void mouseMoveEvent(QMouseEvent* ev);
  154. virtual void mouseReleaseEvent(QMouseEvent* ev);
  155. bool isMinimumSliderDown()const;
  156. bool isMaximumSliderDown()const;
  157. // Description:
  158. // Rendering is done here.
  159. virtual void paintEvent(QPaintEvent* ev);
  160. virtual void initMinimumSliderStyleOption(QStyleOptionSlider* option) const;
  161. virtual void initMaximumSliderStyleOption(QStyleOptionSlider* option) const;
  162. // Description:
  163. // Reimplemented for the tooltips
  164. virtual bool event(QEvent* event);
  165. protected:
  166. QScopedPointer<ctkRangeSliderPrivate> d_ptr;
  167. private:
  168. Q_DECLARE_PRIVATE(ctkRangeSlider);
  169. Q_DISABLE_COPY(ctkRangeSlider);
  170. };
  171. #endif