7#include "QskAnimationHint.h"
10#include "QskFunctions.h"
21static QRectF qskHandleSelectionRect( const
QskSlider* slider )
23 return slider->subControlRect( QskSlider::Handle );
26static QRectF qskSliderSelectionRect(
const QskSlider* slider )
28 const qreal margin = 10.0;
31 const auto handleRect = qskHandleSelectionRect( slider );
34 if ( slider->orientation() == Qt::Horizontal )
36 r.setTop( qMin( r.top(), handleRect.top() ) );
37 r.setBottom( qMax( r.bottom(), handleRect.bottom() ) );
38 r.setLeft( scaleRect.left() - margin );
39 r.setRight( scaleRect.right() + margin );
43 r.setLeft( qMin( r.left(), handleRect.left() ) );
44 r.setRight( qMax( r.right(), handleRect.right() ) );
45 r.setTop( scaleRect.top() - margin );
46 r.setBottom( scaleRect.bottom() + margin );
52static inline int qskKeyOffset( Qt::Orientation orientation,
int key )
54 if ( orientation == Qt::Horizontal )
56 if ( key == Qt::Key_Left )
59 if ( key == Qt::Key_Right )
64 if ( key == Qt::Key_Down )
67 if ( key == Qt::Key_Up )
74class QskSlider::PrivateData
77 PrivateData( Qt::Orientation orientation )
83 , orientation( orientation )
99QskSlider::QskSlider( QQuickItem* parent )
104QskSlider::QskSlider( Qt::Orientation orientation, QQuickItem* parent )
105 : Inherited( parent )
106 , m_data( new PrivateData( orientation ) )
108 setAcceptHoverEvents(
true );
109 setFocusPolicy( Qt::StrongFocus );
111 if ( orientation == Qt::Horizontal )
112 initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Fixed );
114 initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Minimum );
116 connect(
this, &QskSlider::boundariesChanged,
this, &QskSlider::moveHandle );
117 connect(
this, &QskSlider::valueChanged,
this, &QskSlider::moveHandle );
120QskSlider::~QskSlider()
124void QskSlider::setOrientation( Qt::Orientation orientation )
126 if ( orientation != m_data->orientation )
128 m_data->orientation = orientation;
136 Q_EMIT orientationChanged( this->orientation() );
140Qt::Orientation QskSlider::orientation()
const
142 return static_cast< Qt::Orientation
>( m_data->orientation );
145void QskSlider::setInverted(
bool on )
147 if ( on != m_data->inverted )
149 m_data->inverted = on;
152 Q_EMIT invertedChanged( on );
156bool QskSlider::isInverted()
const
158 return m_data->inverted;
161void QskSlider::setOrigin( qreal origin )
163 if ( isComponentComplete() )
164 origin = boundedValue( origin );
166 if( !m_data->hasOrigin || !qskFuzzyCompare( m_data->origin, origin ) )
168 m_data->hasOrigin =
true;
169 m_data->origin = origin;
172 Q_EMIT originChanged( origin );
176void QskSlider::resetOrigin()
178 if ( m_data->hasOrigin )
180 m_data->hasOrigin =
false;
183 Q_EMIT originChanged( origin() );
187qreal QskSlider::origin()
const
189 if ( m_data->hasOrigin )
190 return boundedValue( m_data->origin );
195bool QskSlider::hasOrigin()
const
197 return m_data->hasOrigin;
205void QskSlider::setTracking(
bool on )
207 if ( on != m_data->tracking )
209 m_data->tracking = on;
210 Q_EMIT trackingChanged( on );
214bool QskSlider::isTracking()
const
216 return m_data->tracking;
219void QskSlider::componentComplete()
221 Inherited::componentComplete();
222 if ( m_data->hasOrigin )
223 m_data->origin = boundedValue( m_data->origin );
228 setPositionHint( Handle, valueAsRatio() );
232void QskSlider::mousePressEvent( QMouseEvent* event )
234 const auto pos = qskMousePosition( event );
235 if ( !qskHandleSelectionRect(
this ).contains( pos ) )
237 const auto r = qskSliderSelectionRect(
this );
238 if ( !r.contains( pos ) )
240 Inherited::mousePressEvent( event );
247 if ( m_data->orientation == Qt::Horizontal )
248 ratio = ( pos.x() - scaleRect.left() ) / scaleRect.width();
250 ratio = ( scaleRect.bottom() - pos.y() ) / scaleRect.height();
252 if ( m_data->inverted )
255 setValue( valueFromRatio( ratio ) );
260 m_data->pressedPos = pos;
261 m_data->pressedValue = value();
264void QskSlider::mouseMoveEvent( QMouseEvent* event )
266 if ( !hasSkinState( Pressed ) )
269 const auto mousePos = qskMousePosition( event );
272 auto length = boundaryLength();
273 if ( m_data->inverted )
278 if ( m_data->orientation == Qt::Horizontal )
280 const auto distance = mousePos.x() - m_data->pressedPos.x();
281 newValue = m_data->pressedValue + distance / r.width() * length;
285 const auto distance = mousePos.y() - m_data->pressedPos.y();
286 newValue = m_data->pressedValue - distance / r.height() * length;
289 if ( m_data->tracking )
291 m_data->dragging =
true;
292 setValue( newValue );
293 m_data->dragging =
false;
302void QskSlider::mouseReleaseEvent( QMouseEvent* )
304 if ( !m_data->tracking && ( m_data->pressedValue != value() ) )
305 Q_EMIT valueChanged( value() );
310void QskSlider::keyPressEvent( QKeyEvent* event )
312 if (
auto offset = qskKeyOffset( orientation(), event->key() ) )
314 if ( m_data->inverted )
317 increment( offset * stepSize() );
321 if ( m_data->hasOrigin )
323 switch( event->key() )
327 setValue( origin() );
339 Inherited::keyPressEvent( event );
342void QskSlider::moveHandle()
345 if ( !m_data->dragging )
351 moveHandleTo( value(), hint );
356 const qreal pos = valueAsRatio( value );
358 if ( hint.isValid() )
360 const qreal oldPos = positionHint( Handle );
361 setPositionHint( Handle, pos );
364 startTransition( aspect, hint, oldPos, pos );
368 setPositionHint( Handle, pos );
374#include "moc_QskSlider.cpp"
Variation
Some sort of variation.
QRectF subControlRect(QskAspect::Subcontrol) const
void setSizePolicy(QskSizePolicy)
virtual void aboutToShow()
QskAnimationHint animationHint(QskAspect, QskSkinHintStatus *=nullptr) const
void setSkinStateFlag(QskAspect::State, bool on=true)
QskAspect::Variation effectiveVariation() const override
void aboutToShow() override