ctkRangeSlider.h 7.2 KB

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