From 4ebd07390f6f32bea4fdd4a52d1e21fa03031cc8 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 23 Sep 2020 12:32:08 +0200 Subject: [PATCH] QskSlider::tracking fixed --- src/controls/QskBoundedControl.cpp | 6 ++++ src/controls/QskBoundedControl.h | 2 ++ src/controls/QskSlider.cpp | 51 +++++++++++++++++++----------- src/controls/QskSlider.h | 1 + 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/controls/QskBoundedControl.cpp b/src/controls/QskBoundedControl.cpp index 74ac7e7f..4dae5ea0 100644 --- a/src/controls/QskBoundedControl.cpp +++ b/src/controls/QskBoundedControl.cpp @@ -140,4 +140,10 @@ qreal QskBoundedControl::valueAsRatio( qreal value ) const return ( value - m_minimum ) / ( m_maximum - m_minimum ); } +qreal QskBoundedControl::valueFromRatio( qreal ratio ) const +{ + ratio = qBound( 0.0, ratio, 1.0 ); + return m_minimum + ratio * ( m_maximum - m_minimum ); +} + #include "moc_QskBoundedControl.cpp" diff --git a/src/controls/QskBoundedControl.h b/src/controls/QskBoundedControl.h index 1b771094..76f6571b 100644 --- a/src/controls/QskBoundedControl.h +++ b/src/controls/QskBoundedControl.h @@ -33,7 +33,9 @@ class QSK_EXPORT QskBoundedControl : public QskControl QskIntervalF boundaries() const; qreal boundedValue( qreal ) const; + qreal valueAsRatio( qreal ) const; + qreal valueFromRatio( qreal ) const; public Q_SLOTS: void setMinimum( qreal ); diff --git a/src/controls/QskSlider.cpp b/src/controls/QskSlider.cpp index 16c26c1d..fd6f13e2 100644 --- a/src/controls/QskSlider.cpp +++ b/src/controls/QskSlider.cpp @@ -17,6 +17,12 @@ QSK_SYSTEM_STATE( QskSlider, Pressed, QskAspect::FirstSystemState << 2 ) QSK_SYSTEM_STATE( QskSlider, Minimum, QskAspect::FirstSystemState << 3 ) QSK_SYSTEM_STATE( QskSlider, Maximum, QskAspect::FirstSystemState << 4 ) +static inline QskAspect::Aspect qskAspectPosition( const QskSlider* slider ) +{ + using namespace QskAspect; + return slider->effectiveSubcontrol( QskSlider::Handle ) | Position | Metric; +} + class QskSlider::PrivateData { public: @@ -105,9 +111,7 @@ bool QskSlider::isTracking() const void QskSlider::aboutToShow() { - const auto subControl = effectiveSubcontrol( QskSlider::Handle ); - setMetric( subControl | QskAspect::Position, valueAsRatio() ); - + setMetric( qskAspectPosition( this ), valueAsRatio() ); Inherited::aboutToShow(); } @@ -161,14 +165,6 @@ void QskSlider::mouseMoveEvent( QMouseEvent* event ) if ( !isPressed() ) return; - if ( !m_data->tracking ) - { -#if 0 - // if tracking is false we need to update the position only - // without changing the value TODO.. -#endif - } - const auto r = subControlRect( Scale ); qreal newValue; @@ -184,7 +180,14 @@ void QskSlider::mouseMoveEvent( QMouseEvent* event ) newValue = m_data->pressedValue - distance / r.height() * boundaryLength(); } - setValue( newValue ); + if ( m_data->tracking ) + { + setValue( newValue ); + } + else + { + updatePosition( newValue, QskAnimationHint() ); + } } void QskSlider::mouseReleaseEvent( QMouseEvent* event ) @@ -217,22 +220,34 @@ void QskSlider::mouseReleaseEvent( QMouseEvent* event ) else pageDown(); } + else + { + if ( !m_data->tracking ) + { + const auto pos = metric( qskAspectPosition( this ) ); + setValue( valueFromRatio( pos ) ); + } + } setSkinStateFlag( Pressed, false ); Q_EMIT pressedChanged( false ); } void QskSlider::updatePosition() +{ + const auto hint = animation( qskAspectPosition( this ) | skinState() ); + updatePosition( value(), hint ); +} + +void QskSlider::updatePosition( qreal value, const QskAnimationHint& hint ) { using namespace QskAspect; - setSkinStateFlag( QskSlider::Minimum, value() <= minimum() ); - setSkinStateFlag( QskSlider::Maximum, value() >= maximum() ); + setSkinStateFlag( QskSlider::Minimum, value <= minimum() ); + setSkinStateFlag( QskSlider::Maximum, value >= maximum() ); - const auto aspect = effectiveSubcontrol( QskSlider::Handle ) | Position | Metric; - - const auto hint = animation( aspect | skinState() ); - const qreal pos = valueAsRatio(); + const auto aspect = qskAspectPosition( this ); + const qreal pos = valueAsRatio( value ); if ( hint.duration > 0 ) { diff --git a/src/controls/QskSlider.h b/src/controls/QskSlider.h index bd4da9dd..0ae39528 100644 --- a/src/controls/QskSlider.h +++ b/src/controls/QskSlider.h @@ -62,6 +62,7 @@ class QSK_EXPORT QskSlider : public QskBoundedValueInput private: void updatePosition(); + void updatePosition( qreal value, const QskAnimationHint& ); class PrivateData; std::unique_ptr< PrivateData > m_data;