From 12dfac153d473040ab98eec7bd9178897347b0e0 Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Thu, 28 Nov 2024 16:06:00 +0100 Subject: [PATCH] QskSlider::inverted introduced --- src/controls/QskSlider.cpp | 34 ++++++++++++++++++++++++++++--- src/controls/QskSlider.h | 7 +++++++ src/controls/QskSliderSkinlet.cpp | 20 +++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/controls/QskSlider.cpp b/src/controls/QskSlider.cpp index 02652602..c4494cdb 100644 --- a/src/controls/QskSlider.cpp +++ b/src/controls/QskSlider.cpp @@ -77,6 +77,7 @@ class QskSlider::PrivateData PrivateData( Qt::Orientation orientation ) : pressedValue( 0 ) , hasOrigin( false ) + , inverted( false ) , tracking( true ) , dragging( false ) , orientation( orientation ) @@ -89,6 +90,7 @@ class QskSlider::PrivateData qreal origin = 0.0; bool hasOrigin : 1; + bool inverted : 1; bool tracking : 1; bool dragging : 1; uint orientation : 2; @@ -140,6 +142,22 @@ Qt::Orientation QskSlider::orientation() const return static_cast< Qt::Orientation >( m_data->orientation ); } +void QskSlider::setInverted( bool on ) +{ + if ( on != m_data->inverted ) + { + m_data->inverted = on; + update(); + + Q_EMIT invertedChanged( on ); + } +} + +bool QskSlider::isInverted() const +{ + return m_data->inverted; +} + void QskSlider::setOrigin( qreal origin ) { if ( isComponentComplete() ) @@ -226,6 +244,9 @@ void QskSlider::mousePressEvent( QMouseEvent* event ) else ratio = ( scaleRect.bottom() - pos.y() ) / scaleRect.height(); + if ( m_data->inverted ) + ratio = 1.0 - ratio; + setValue( valueFromRatio( ratio ) ); } @@ -243,17 +264,21 @@ void QskSlider::mouseMoveEvent( QMouseEvent* event ) const auto mousePos = qskMousePosition( event ); const auto r = subControlRect( Scale ); + auto length = boundaryLength(); + if ( m_data->inverted ) + length = -length; + qreal newValue; if ( m_data->orientation == Qt::Horizontal ) { const auto distance = mousePos.x() - m_data->pressedPos.x(); - newValue = m_data->pressedValue + distance / r.width() * boundaryLength(); + newValue = m_data->pressedValue + distance / r.width() * length; } else { const auto distance = mousePos.y() - m_data->pressedPos.y(); - newValue = m_data->pressedValue - distance / r.height() * boundaryLength(); + newValue = m_data->pressedValue - distance / r.height() * length; } if ( m_data->tracking ) @@ -279,8 +304,11 @@ void QskSlider::mouseReleaseEvent( QMouseEvent* ) void QskSlider::keyPressEvent( QKeyEvent* event ) { - if ( const auto offset = qskKeyOffset( orientation(), event->key() ) ) + if ( auto offset = qskKeyOffset( orientation(), event->key() ) ) { + if ( m_data->inverted ) + offset = -offset; + increment( offset * stepSize() ); return; } diff --git a/src/controls/QskSlider.h b/src/controls/QskSlider.h index c0fd33ae..4262ef2d 100644 --- a/src/controls/QskSlider.h +++ b/src/controls/QskSlider.h @@ -16,6 +16,9 @@ class QSK_EXPORT QskSlider : public QskBoundedValueInput Q_PROPERTY( Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged ) + Q_PROPERTY( bool inverted READ isInverted + WRITE setInverted NOTIFY invertedChanged ) + Q_PROPERTY( bool tracking READ isTracking WRITE setTracking NOTIFY trackingChanged ) @@ -36,6 +39,9 @@ class QSK_EXPORT QskSlider : public QskBoundedValueInput void setOrientation( Qt::Orientation ); Qt::Orientation orientation() const; + void setInverted( bool ); + bool isInverted() const; + void resetOrigin(); qreal origin() const; @@ -51,6 +57,7 @@ class QSK_EXPORT QskSlider : public QskBoundedValueInput Q_SIGNALS: void orientationChanged( Qt::Orientation ); + void invertedChanged( bool ); void trackingChanged( bool ); void originChanged( qreal ); diff --git a/src/controls/QskSliderSkinlet.cpp b/src/controls/QskSliderSkinlet.cpp index eecfcf34..75dc4f03 100644 --- a/src/controls/QskSliderSkinlet.cpp +++ b/src/controls/QskSliderSkinlet.cpp @@ -251,6 +251,12 @@ QRectF QskSliderSkinlet::fillRect( auto pos1 = slider->valueAsRatio( slider->origin() ); auto pos2 = qBound( 0.0, slider->positionHint( Q::Handle ), 1.0 ); + if ( slider->isInverted() ) + { + pos1 = 1.0 - pos1; + pos2 = 1.0 - pos2; + } + if ( pos1 > pos2 ) qSwap( pos1, pos2 ); @@ -293,7 +299,10 @@ QRectF QskSliderSkinlet::handleRect( if ( handleSize.width() < 0.0 ) handleSize.setWidth( handleSize.height() ); - center.setX( r.left() + pos * r.width() ); + if ( slider->isInverted() ) + center.setX( r.right() - pos * r.width() ); + else + center.setX( r.left() + pos * r.width() ); } else { @@ -303,7 +312,10 @@ QRectF QskSliderSkinlet::handleRect( if ( handleSize.height() < 0.0 ) handleSize.setHeight( handleSize.width() ); - center.setY( r.bottom() - pos * r.height() ); + if ( slider->isInverted() ) + center.setY( r.top() + pos * r.height() ); + else + center.setY( r.bottom() - pos * r.height() ); } QRectF handleRect( 0, 0, handleSize.width(), handleSize.height() ); @@ -319,7 +331,9 @@ QRectF QskSliderSkinlet::tickRect( const QskSlider* slider, if ( !tickValue.canConvert< qreal >() ) return QRectF(); - const auto tickPos = slider->valueAsRatio( tickValue.value< qreal >() ); + auto tickPos = slider->valueAsRatio( tickValue.value< qreal >() ); + if ( slider->isInverted() ) + tickPos = 1.0 - tickPos; const auto r = subControlRect( slider, contentsRect, Q::Scale );