File indexing completed on 2024-04-14 14:08:53

0001 /*
0002     SPDX-FileCopyrightText: Kitware Inc.
0003     SPDX-License-Identifier: Apache-2.0
0004 */
0005 
0006 #ifndef __ctkRangeSlider_h
0007 #define __ctkRangeSlider_h
0008 
0009 // Qt includes
0010 #include <QSlider>
0011 
0012 // CTK includes
0013 //#include <ctkPimpl.h>
0014 
0015 class QStylePainter;
0016 class ctkRangeSliderPrivate;
0017 
0018 /// \ingroup Widgets
0019 ///
0020 /// A ctkRangeSlider is a slider that lets you input 2 values instead of one
0021 /// (see QSlider). These values are typically a lower and upper bound.
0022 /// Values are comprised between the range of the slider. See setRange(),
0023 /// minimum() and maximum(). The upper bound can't be smaller than the
0024 /// lower bound and vice-versa.
0025 /// When setting new values (setMinimumValue(), setMaximumValue() or
0026 /// setValues()), make sure they lie between the range (minimum(), maximum())
0027 /// of the slider, they would be forced otherwised. If it is not the behavior
0028 /// you desire, you can set the range first (setRange(), setMinimum(),
0029 /// setMaximum())
0030 /// TODO: support triggerAction(QAbstractSlider::SliderSingleStepSub) that
0031 /// moves both values at a time.
0032 /// \sa ctkDoubleRangeSlider, ctkDoubleSlider, ctkRangeWidget
0033 class ctkRangeSlider : public QSlider
0034 {
0035         Q_OBJECT
0036         Q_PROPERTY(int minimumValue READ minimumValue WRITE setMinimumValue)
0037         Q_PROPERTY(int maximumValue READ maximumValue WRITE setMaximumValue)
0038         Q_PROPERTY(int minimumPosition READ minimumPosition WRITE setMinimumPosition)
0039         Q_PROPERTY(int maximumPosition READ maximumPosition WRITE setMaximumPosition)
0040         Q_PROPERTY(bool symmetricMoves READ symmetricMoves WRITE setSymmetricMoves)
0041         Q_PROPERTY(QString handleToolTip READ handleToolTip WRITE setHandleToolTip)
0042 
0043     public:
0044         // Superclass typedef
0045         typedef QSlider Superclass;
0046         /// Constructor, builds a ctkRangeSlider that ranges from 0 to 100 and has
0047         /// a lower and upper values of 0 and 100 respectively, other properties
0048         /// are set the QSlider default properties.
0049         explicit ctkRangeSlider( Qt::Orientation o, QWidget* par = 0 );
0050         explicit ctkRangeSlider( QWidget* par = 0 );
0051         virtual ~ctkRangeSlider();
0052 
0053         ///
0054         /// This property holds the slider's current minimum value.
0055         /// The slider silently forces minimumValue to be within the legal range:
0056         /// minimum() <= minimumValue() <= maximumValue() <= maximum().
0057         /// Changing the minimumValue also changes the minimumPosition.
0058         int minimumValue() const;
0059 
0060         ///
0061         /// This property holds the slider's current maximum value.
0062         /// The slider forces the maximum value to be within the legal range:
0063         /// The slider silently forces maximumValue to be within the legal range:
0064         /// Changing the maximumValue also changes the maximumPosition.
0065         int maximumValue() const;
0066 
0067         ///
0068         /// This property holds the current slider minimum position.
0069         /// If tracking is enabled (the default), this is identical to minimumValue.
0070         int minimumPosition() const;
0071         void setMinimumPosition(int min);
0072 
0073         ///
0074         /// This property holds the current slider maximum position.
0075         /// If tracking is enabled (the default), this is identical to maximumValue.
0076         int maximumPosition() const;
0077         void setMaximumPosition(int max);
0078 
0079         ///
0080         /// Utility function that set the minimum position and
0081         /// maximum position at once.
0082         void setPositions(int min, int max);
0083 
0084         ///
0085         /// When symmetricMoves is true, moving a handle will move the other handle
0086         /// symmetrically, otherwise the handles are independent. False by default
0087         bool symmetricMoves()const;
0088         void setSymmetricMoves(bool symmetry);
0089 
0090         ///
0091         /// Controls the text to display for the handle tooltip. It is in addition
0092         /// to the widget tooltip.
0093         /// "%1" is replaced by the current value of the slider.
0094         /// Empty string (by default) means no tooltip.
0095         QString handleToolTip()const;
0096         void setHandleToolTip(const QString &toolTip);
0097 
0098         /// Returns true if the minimum value handle is down, false if it is up.
0099         /// \sa isMaximumSliderDown()
0100         bool isMinimumSliderDown()const;
0101         /// Returns true if the maximum value handle is down, false if it is up.
0102         /// \sa isMinimumSliderDown()
0103         bool isMaximumSliderDown()const;
0104 
0105     Q_SIGNALS:
0106         ///
0107         /// This signal is emitted when the slider minimum value has changed,
0108         /// with the new slider value as argument.
0109         void minimumValueChanged(int min);
0110         ///
0111         /// This signal is emitted when the slider maximum value has changed,
0112         /// with the new slider value as argument.
0113         void maximumValueChanged(int max);
0114         ///
0115         /// Utility signal that is fired when minimum or maximum values have changed.
0116         void valuesChanged(int min, int max);
0117 
0118         ///
0119         /// This signal is emitted when sliderDown is true and the slider moves.
0120         /// This usually happens when the user is dragging the minimum slider.
0121         /// The value is the new slider minimum position.
0122         /// This signal is emitted even when tracking is turned off.
0123         void minimumPositionChanged(int min);
0124 
0125         ///
0126         /// This signal is emitted when sliderDown is true and the slider moves.
0127         /// This usually happens when the user is dragging the maximum slider.
0128         /// The value is the new slider maximum position.
0129         /// This signal is emitted even when tracking is turned off.
0130         void maximumPositionChanged(int max);
0131 
0132         ///
0133         /// Utility signal that is fired when minimum or maximum positions
0134         /// have changed.
0135         void positionsChanged(int min, int max);
0136 
0137     public Q_SLOTS:
0138         ///
0139         /// This property holds the slider's current minimum value.
0140         /// The slider silently forces min to be within the legal range:
0141         /// minimum() <= min <= maximumValue() <= maximum().
0142         /// Note: Changing the minimumValue also changes the minimumPosition.
0143         /// \sa stMaximumValue, setValues, setMinimum, setMaximum, setRange
0144         void setMinimumValue(int min);
0145 
0146         ///
0147         /// This property holds the slider's current maximum value.
0148         /// The slider silently forces max to be within the legal range:
0149         /// minimum() <= minimumValue() <= max <= maximum().
0150         /// Note: Changing the maximumValue also changes the maximumPosition.
0151         /// \sa stMinimumValue, setValues, setMinimum, setMaximum, setRange
0152         void setMaximumValue(int max);
0153 
0154         ///
0155         /// Utility function that set the minimum value and maximum value at once.
0156         /// The slider silently forces min and max to be within the legal range:
0157         /// minimum() <= min <= max <= maximum().
0158         /// Note: Changing the minimumValue and maximumValue also changes the
0159         /// minimumPosition and maximumPosition.
0160         /// \sa setMinimumValue, setMaximumValue, setMinimum, setMaximum, setRange
0161         void setValues(int min, int max);
0162 
0163     protected Q_SLOTS:
0164         void onRangeChanged(int minimum, int maximum);
0165 
0166     protected:
0167         ctkRangeSlider( ctkRangeSliderPrivate* impl, Qt::Orientation o, QWidget* par = 0 );
0168         ctkRangeSlider( ctkRangeSliderPrivate* impl, QWidget* par = 0 );
0169 
0170         // Description:
0171         // Standard Qt UI events
0172         virtual void mousePressEvent(QMouseEvent* ev) override;
0173         virtual void mouseMoveEvent(QMouseEvent* ev) override;
0174         virtual void mouseReleaseEvent(QMouseEvent* ev) override;
0175 
0176         // Description:
0177         // Rendering is done here.
0178         virtual void paintEvent(QPaintEvent* ev) override;
0179         virtual void initMinimumSliderStyleOption(QStyleOptionSlider* option) const;
0180         virtual void initMaximumSliderStyleOption(QStyleOptionSlider* option) const;
0181 
0182         // Description:
0183         // Reimplemented for the tooltips
0184         virtual bool event(QEvent* event) override;
0185 
0186     protected:
0187         QScopedPointer<ctkRangeSliderPrivate> d_ptr;
0188 
0189     private:
0190         Q_DECLARE_PRIVATE(ctkRangeSlider);
0191         Q_DISABLE_COPY(ctkRangeSlider);
0192 };
0193 
0194 #endif
0195