2017-07-21 18:21:34 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "QskSlider.h"
|
|
|
|
#include "QskAnimationHint.h"
|
2018-08-03 08:15:28 +02:00
|
|
|
#include "QskAspect.h"
|
2020-10-25 15:56:22 +01:00
|
|
|
#include "QskIntervalF.h"
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
QSK_SUBCONTROL( QskSlider, Panel )
|
|
|
|
QSK_SUBCONTROL( QskSlider, Groove )
|
|
|
|
QSK_SUBCONTROL( QskSlider, Fill )
|
|
|
|
QSK_SUBCONTROL( QskSlider, Scale )
|
|
|
|
QSK_SUBCONTROL( QskSlider, Handle )
|
|
|
|
|
2020-09-21 12:53:25 +02:00
|
|
|
QSK_SYSTEM_STATE( QskSlider, Pressed, QskAspect::FirstSystemState << 2 )
|
|
|
|
QSK_SYSTEM_STATE( QskSlider, Minimum, QskAspect::FirstSystemState << 3 )
|
|
|
|
QSK_SYSTEM_STATE( QskSlider, Maximum, QskAspect::FirstSystemState << 4 )
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2020-09-23 12:32:08 +02:00
|
|
|
static inline QskAspect::Aspect qskAspectPosition( const QskSlider* slider )
|
|
|
|
{
|
|
|
|
using namespace QskAspect;
|
|
|
|
return slider->effectiveSubcontrol( QskSlider::Handle ) | Position | Metric;
|
|
|
|
}
|
|
|
|
|
2020-10-23 13:38:00 +02:00
|
|
|
static inline QPointF qskMousePosition( const QMouseEvent* event )
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
|
|
|
|
return event->position();
|
|
|
|
#else
|
|
|
|
return event->localPos();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
class QskSlider::PrivateData
|
|
|
|
{
|
2018-08-03 08:15:28 +02:00
|
|
|
public:
|
|
|
|
PrivateData( Qt::Orientation orientation )
|
|
|
|
: pressedValue( 0 )
|
|
|
|
, tracking( true )
|
|
|
|
, orientation( orientation )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF pressedPos;
|
|
|
|
qreal pressedValue;
|
|
|
|
bool tracking : 1;
|
|
|
|
Qt::Orientation orientation : 2;
|
|
|
|
};
|
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
QskSlider::QskSlider( QQuickItem* parent )
|
|
|
|
: QskSlider( Qt::Horizontal, parent )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
QskSlider::QskSlider( Qt::Orientation orientation, QQuickItem* parent )
|
|
|
|
: Inherited( parent )
|
|
|
|
, m_data( new PrivateData( orientation ) )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2017-10-17 17:34:00 +02:00
|
|
|
setAcceptHoverEvents( true );
|
2018-08-03 08:15:28 +02:00
|
|
|
setFocusPolicy( Qt::StrongFocus );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
if ( orientation == Qt::Horizontal )
|
2017-08-31 09:09:05 +02:00
|
|
|
initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Fixed );
|
2017-07-21 18:21:34 +02:00
|
|
|
else
|
2017-08-31 09:09:05 +02:00
|
|
|
initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Minimum );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2020-09-23 12:43:46 +02:00
|
|
|
connect( this, &QskSlider::boundariesChanged, this, &QskSlider::moveHandle );
|
|
|
|
connect( this, &QskSlider::valueChanged, this, &QskSlider::moveHandle );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QskSlider::~QskSlider()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QskSlider::isPressed() const
|
|
|
|
{
|
|
|
|
return skinState() & Pressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskSlider::setOrientation( Qt::Orientation orientation )
|
|
|
|
{
|
|
|
|
if ( orientation != m_data->orientation )
|
|
|
|
{
|
|
|
|
m_data->orientation = orientation;
|
|
|
|
#if 1
|
|
|
|
// swapping the size policy: guess this is what a user expects
|
|
|
|
setSizePolicy( sizePolicy( Qt::Vertical ), sizePolicy( Qt::Horizontal ) );
|
|
|
|
#endif
|
|
|
|
resetImplicitSize();
|
|
|
|
update();
|
|
|
|
|
|
|
|
Q_EMIT orientationChanged( m_data->orientation );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientation QskSlider::orientation() const
|
|
|
|
{
|
|
|
|
return m_data->orientation;
|
|
|
|
}
|
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
QskAspect::Placement QskSlider::effectivePlacement() const
|
|
|
|
{
|
2019-04-17 16:33:17 +02:00
|
|
|
return static_cast< QskAspect::Placement >( m_data->orientation );
|
2017-10-17 17:34:00 +02:00
|
|
|
}
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
void QskSlider::setTracking( bool on )
|
|
|
|
{
|
|
|
|
if ( on != m_data->tracking )
|
|
|
|
{
|
|
|
|
m_data->tracking = on;
|
|
|
|
Q_EMIT trackingChanged( on );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QskSlider::isTracking() const
|
|
|
|
{
|
|
|
|
return m_data->tracking;
|
|
|
|
}
|
|
|
|
|
2020-09-21 11:31:37 +02:00
|
|
|
void QskSlider::aboutToShow()
|
|
|
|
{
|
2020-09-23 12:32:08 +02:00
|
|
|
setMetric( qskAspectPosition( this ), valueAsRatio() );
|
2020-09-21 11:31:37 +02:00
|
|
|
Inherited::aboutToShow();
|
|
|
|
}
|
|
|
|
|
2019-09-10 17:01:47 +02:00
|
|
|
QSizeF QskSlider::contentsSizeHint(
|
|
|
|
Qt::SizeHint which, const QSizeF& ) const
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2019-09-10 17:01:47 +02:00
|
|
|
if ( which != Qt::PreferredSize )
|
|
|
|
return QSizeF();
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
const qreal dim = metric( QskSlider::Panel | QskAspect::Size );
|
|
|
|
return ( m_data->orientation == Qt::Horizontal )
|
|
|
|
? QSizeF( 4 * dim, dim ) : QSizeF( dim, 4 * dim );
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF QskSlider::handleSize() const
|
|
|
|
{
|
|
|
|
return handleRect().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF QskSlider::handleRect() const
|
|
|
|
{
|
2017-08-28 10:15:47 +02:00
|
|
|
return subControlRect( QskSlider::Handle );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskSlider::mousePressEvent( QMouseEvent* event )
|
|
|
|
{
|
|
|
|
if ( handleRect().contains( event->pos() ) )
|
|
|
|
{
|
2017-10-24 19:32:54 +02:00
|
|
|
// Case 1: press started in the handle, start sliding
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
m_data->pressedPos = event->pos();
|
|
|
|
m_data->pressedValue = value();
|
|
|
|
setSkinStateFlag( Pressed );
|
|
|
|
Q_EMIT pressedChanged( true );
|
|
|
|
}
|
2017-10-24 19:32:54 +02:00
|
|
|
else if ( !pageSize() )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2017-10-24 19:32:54 +02:00
|
|
|
// Case 2: pageSize is not used, we're done here
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Case 3: pressed outside of the handle, page the scroller in
|
|
|
|
// the direction of the press requires an auto-repeat behavior
|
|
|
|
// until the slider reaches the destination, or it simply jumps
|
|
|
|
// there (configurable)
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskSlider::mouseMoveEvent( QMouseEvent* event )
|
|
|
|
{
|
|
|
|
if ( !isPressed() )
|
|
|
|
return;
|
|
|
|
|
2020-10-23 13:38:00 +02:00
|
|
|
const auto mousePos = qskMousePosition( event );
|
2017-08-28 10:15:47 +02:00
|
|
|
const auto r = subControlRect( Scale );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
qreal newValue;
|
|
|
|
|
|
|
|
if ( m_data->orientation == Qt::Horizontal )
|
|
|
|
{
|
2020-10-23 13:38:00 +02:00
|
|
|
const auto distance = mousePos.x() - m_data->pressedPos.x();
|
2020-07-25 12:50:26 +02:00
|
|
|
newValue = m_data->pressedValue + distance / r.width() * boundaryLength();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-23 13:38:00 +02:00
|
|
|
const auto distance = mousePos.y() - m_data->pressedPos.y();
|
2020-07-25 12:50:26 +02:00
|
|
|
newValue = m_data->pressedValue - distance / r.height() * boundaryLength();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 12:32:08 +02:00
|
|
|
if ( m_data->tracking )
|
|
|
|
{
|
|
|
|
setValue( newValue );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-23 12:43:46 +02:00
|
|
|
moveHandleTo( newValue, QskAnimationHint() );
|
2020-09-23 12:32:08 +02:00
|
|
|
}
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskSlider::mouseReleaseEvent( QMouseEvent* event )
|
|
|
|
{
|
|
|
|
if ( !isPressed() ) // Page event
|
|
|
|
{
|
2020-10-23 13:38:00 +02:00
|
|
|
const auto mousePos = qskMousePosition( event );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
const auto szHandle = handleSize();
|
|
|
|
const auto rect = contentsRect();
|
|
|
|
|
|
|
|
bool up;
|
|
|
|
if ( m_data->orientation == Qt::Horizontal )
|
|
|
|
{
|
|
|
|
const qreal w = szHandle.width();
|
|
|
|
|
2020-10-23 13:38:00 +02:00
|
|
|
const qreal x = ( mousePos.x() - rect.x() - w * 0.5 ) / ( rect.width() - w );
|
2020-07-25 12:50:26 +02:00
|
|
|
up = x > valueAsRatio();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const qreal h = szHandle.height();
|
|
|
|
|
2020-10-23 13:38:00 +02:00
|
|
|
const qreal y = ( mousePos.y() - rect.y() - h * 0.5 ) / ( rect.height() - h );
|
2020-07-25 12:50:26 +02:00
|
|
|
up = y < 1.0 - valueAsRatio();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( up )
|
|
|
|
pageUp();
|
|
|
|
else
|
|
|
|
pageDown();
|
|
|
|
}
|
2020-09-23 12:32:08 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( !m_data->tracking )
|
|
|
|
{
|
2020-09-23 12:43:46 +02:00
|
|
|
const auto pos = handlePosition();
|
2020-09-23 12:32:08 +02:00
|
|
|
setValue( valueFromRatio( pos ) );
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
setSkinStateFlag( Pressed, false );
|
|
|
|
Q_EMIT pressedChanged( false );
|
|
|
|
}
|
|
|
|
|
2020-09-23 12:43:46 +02:00
|
|
|
qreal QskSlider::handlePosition() const
|
|
|
|
{
|
|
|
|
return metric( qskAspectPosition( this ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskSlider::moveHandle()
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2020-09-23 12:32:08 +02:00
|
|
|
const auto hint = animation( qskAspectPosition( this ) | skinState() );
|
2020-09-23 12:43:46 +02:00
|
|
|
moveHandleTo( value(), hint );
|
2020-09-23 12:32:08 +02:00
|
|
|
}
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2020-09-23 12:43:46 +02:00
|
|
|
void QskSlider::moveHandleTo( qreal value, const QskAnimationHint& hint )
|
2020-09-23 12:32:08 +02:00
|
|
|
{
|
|
|
|
using namespace QskAspect;
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2020-09-23 12:32:08 +02:00
|
|
|
setSkinStateFlag( QskSlider::Minimum, value <= minimum() );
|
|
|
|
setSkinStateFlag( QskSlider::Maximum, value >= maximum() );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2020-09-23 12:32:08 +02:00
|
|
|
const auto aspect = qskAspectPosition( this );
|
|
|
|
const qreal pos = valueAsRatio( value );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
if ( hint.duration > 0 )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2017-08-22 19:47:06 +02:00
|
|
|
const qreal oldPos = metric( aspect );
|
2017-07-21 18:21:34 +02:00
|
|
|
setMetric( aspect, pos );
|
2017-08-22 19:47:06 +02:00
|
|
|
|
2017-10-17 17:34:00 +02:00
|
|
|
startTransition( aspect, hint, oldPos, pos );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setMetric( aspect, pos );
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskSlider.cpp"
|