API cleanup of the Bounded controls

This commit is contained in:
Uwe Rathmann 2024-11-21 13:54:01 +01:00
parent 109fc7d7f4
commit d87c8e3d0b
9 changed files with 51 additions and 63 deletions

View File

@ -23,7 +23,7 @@ static inline bool qskHasOrigin( const QskSlider* )
static inline qreal qskTickValue( const QskSlider* slider, int index ) static inline qreal qskTickValue( const QskSlider* slider, int index )
{ {
if( slider->snap() ) if( slider->isSnapping() && slider->stepSize() )
return slider->minimum() + index * slider->stepSize(); return slider->minimum() + index * slider->stepSize();
if ( qskHasOrigin( slider ) ) if ( qskHasOrigin( slider ) )
@ -119,7 +119,7 @@ int QskMaterial3SliderSkinlet::sampleCount( const QskSkinnable* skinnable,
{ {
const auto slider = static_cast< const QskSlider* >( skinnable ); const auto slider = static_cast< const QskSlider* >( skinnable );
if( slider->snap() ) if( slider->isSnapping() && slider->stepSize() )
return qCeil( slider->boundaryLength() / slider->stepSize() ) + 1; return qCeil( slider->boundaryLength() / slider->stepSize() ) + 1;
// min/origin/max or max // min/origin/max or max

View File

@ -17,7 +17,7 @@ namespace
public: public:
enum Style enum Style
{ {
Continous, Continuous,
Discrete, Discrete,
Centered Centered
}; };
@ -33,15 +33,15 @@ namespace
{ {
case Discrete: case Discrete:
{ {
setSnap( true ); setSnapping( true );
setStepSize( 5 ); setStepSize( 5 );
setPageSteps( 4 ); setPageSteps( 4 );
break; break;
} }
case Continous: case Continuous:
{ {
setSnap( false ); setSnapping( false );
setStepSize( 1 ); setStepSize( 1 );
setPageSteps( 10 ); setPageSteps( 10 );
@ -104,7 +104,7 @@ InputPage::InputPage( QQuickItem* parent )
{ {
const auto orientation = static_cast< Qt::Orientation >( i + 1 ); const auto orientation = static_cast< Qt::Orientation >( i + 1 );
sliders[i].continous = new Slider( orientation, Slider::Continous ); sliders[i].continous = new Slider( orientation, Slider::Continuous );
sliders[i].discrete = new Slider( orientation, Slider::Discrete ); sliders[i].discrete = new Slider( orientation, Slider::Discrete );
} }

View File

@ -20,7 +20,7 @@ Slider::Slider( const QString& text, qreal min, qreal max,
m_slider = new QskSlider( this ); m_slider = new QskSlider( this );
m_slider->setBoundaries( min, max ); m_slider->setBoundaries( min, max );
m_slider->setStepSize( step ); m_slider->setStepSize( step );
m_slider->setSnap( true ); m_slider->setSnapping( true );
m_slider->setValue( value ); m_slider->setValue( value );
m_valueLabel = new QskTextLabel( this ); m_valueLabel = new QskTextLabel( this );

View File

@ -53,9 +53,9 @@ class QSK_EXPORT QskBoundedControl : public QskControl
void componentComplete() override; void componentComplete() override;
private:
void adjustBoundaries( bool increasing ); void adjustBoundaries( bool increasing );
private:
qreal m_minimum; qreal m_minimum;
qreal m_maximum; qreal m_maximum;
}; };

View File

@ -18,7 +18,7 @@ class QskBoundedInput::PrivateData
qreal stepSize = 0.1; qreal stepSize = 0.1;
uint pageSteps = 1; uint pageSteps = 1;
bool snap = false; bool snapping = false;
}; };
QskBoundedInput::QskBoundedInput( QQuickItem* parent ) QskBoundedInput::QskBoundedInput( QQuickItem* parent )
@ -53,7 +53,7 @@ void QskBoundedInput::setStepSize( qreal stepSize )
if ( isComponentComplete() ) if ( isComponentComplete() )
{ {
if ( m_data->snap && stepSize ) if ( m_data->snapping && stepSize )
alignInput(); alignInput();
} }
} }
@ -102,21 +102,21 @@ void QskBoundedInput::pageDown()
increment( -pageSize() ); increment( -pageSize() );
} }
void QskBoundedInput::setSnap( bool snap ) void QskBoundedInput::setSnapping( bool on )
{ {
if ( m_data->snap == snap ) if ( m_data->snapping == on )
return; return;
m_data->snap = snap; m_data->snapping = on;
Q_EMIT snapChanged( snap ); Q_EMIT snappingChanged( on );
if ( isComponentComplete() && snap ) if ( isComponentComplete() && m_data->snapping )
alignInput(); alignInput();
} }
bool QskBoundedInput::snap() const bool QskBoundedInput::isSnapping() const
{ {
return m_data->snap; return m_data->snapping;
} }
void QskBoundedInput::componentComplete() void QskBoundedInput::componentComplete()
@ -134,38 +134,6 @@ void QskBoundedInput::alignInput()
{ {
} }
qreal QskBoundedInput::alignedValue( qreal value ) const
{
value = boundedValue( value );
if ( value > minimum() && value < maximum() )
{
if ( m_data->snap && m_data->stepSize )
{
value = qRound( value / m_data->stepSize ) * m_data->stepSize;
value = boundedValue( value );
}
}
return value;
}
QskIntervalF QskBoundedInput::alignedInterval( const QskIntervalF& interval ) const
{
if ( m_data->snap )
{
if ( const auto step = m_data->stepSize )
{
const qreal lower = std::floor( interval.lowerBound() / step ) * step;
const qreal upper = std::ceil( interval.upperBound() / step ) * step;
return QskIntervalF( lower, upper );
}
}
return interval;
}
void QskBoundedInput::setReadOnly( bool readOnly ) void QskBoundedInput::setReadOnly( bool readOnly )
{ {
if ( readOnly == isReadOnly() ) if ( readOnly == isReadOnly() )

View File

@ -17,7 +17,7 @@ class QSK_EXPORT QskBoundedInput : public QskBoundedControl
Q_PROPERTY( qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged ) Q_PROPERTY( qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged )
Q_PROPERTY( uint pageSteps READ pageSteps WRITE setPageSteps NOTIFY pageStepsChanged ) Q_PROPERTY( uint pageSteps READ pageSteps WRITE setPageSteps NOTIFY pageStepsChanged )
Q_PROPERTY( bool snap READ snap WRITE setSnap NOTIFY snapChanged ) Q_PROPERTY( bool snapping READ isSnapping WRITE setSnapping NOTIFY snappingChanged )
Q_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged ) Q_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged )
using Inherited = QskBoundedControl; using Inherited = QskBoundedControl;
@ -30,10 +30,10 @@ class QSK_EXPORT QskBoundedInput : public QskBoundedControl
qreal stepSize() const; qreal stepSize() const;
qreal pageSize() const; // pageSteps() * stepSize() qreal pageSize() const; // pageSteps() * stepSize()
uint pageSteps() const; uint pageSteps() const;
void setSnap( bool ); void setSnapping( bool );
bool snap() const; bool isSnapping() const;
void setReadOnly( bool ); void setReadOnly( bool );
bool isReadOnly() const; bool isReadOnly() const;
@ -52,7 +52,7 @@ class QSK_EXPORT QskBoundedInput : public QskBoundedControl
Q_SIGNALS: Q_SIGNALS:
void stepSizeChanged( qreal ); void stepSizeChanged( qreal );
void pageStepsChanged( qreal ); void pageStepsChanged( qreal );
void snapChanged( bool ); void snappingChanged( bool );
void readOnlyChanged( bool ); void readOnlyChanged( bool );
@ -66,9 +66,6 @@ class QSK_EXPORT QskBoundedInput : public QskBoundedControl
void componentComplete() override; void componentComplete() override;
virtual void alignInput(); virtual void alignInput();
qreal alignedValue( qreal ) const;
QskIntervalF alignedInterval( const QskIntervalF& ) const;
qreal incrementForKey( const QKeyEvent* ) const; qreal incrementForKey( const QKeyEvent* ) const;
private: private:

View File

@ -88,7 +88,9 @@ void QskBoundedRangeInput::setRange( const QskIntervalF& range )
if ( isComponentComplete() ) if ( isComponentComplete() )
{ {
newRange = alignedInterval( newRange ); if ( isSnapping() && stepSize() )
newRange = newRange.fuzzyAligned( stepSize() );
newRange = fixupRange( newRange ); newRange = fixupRange( newRange );
} }
@ -128,7 +130,11 @@ QskIntervalF QskBoundedRangeInput::range() const
void QskBoundedRangeInput::alignInput() void QskBoundedRangeInput::alignInput()
{ {
setRangeInternal( alignedInterval( m_range ) ); auto newRange = m_range;
if ( isSnapping() && stepSize() )
newRange = newRange.fuzzyAligned( stepSize() );
setRangeInternal( newRange );
} }
QskIntervalF QskBoundedRangeInput::fixupRange( const QskIntervalF& range ) const QskIntervalF QskBoundedRangeInput::fixupRange( const QskIntervalF& range ) const

View File

@ -9,6 +9,24 @@
#include <qlocale.h> #include <qlocale.h>
#include <cfloat> #include <cfloat>
static qreal qskAlignedValue( const QskBoundedValueInput* input, qreal value )
{
value = input->boundedValue( value );
if ( value > input->minimum() && value < input->maximum() )
{
if ( input->isSnapping() && input->stepSize() )
{
const auto step = input->stepSize();
value = qRound( value / step ) * step;
value = input->boundedValue( value );
}
}
return value;
}
class QskBoundedValueInput::PrivateData class QskBoundedValueInput::PrivateData
{ {
public: public:
@ -45,7 +63,7 @@ int QskBoundedValueInput::decimals() const
void QskBoundedValueInput::alignInput() void QskBoundedValueInput::alignInput()
{ {
auto value = alignedValue( m_data->value ); auto value = qskAlignedValue( this, m_data->value );
value = fixupValue( value ); value = fixupValue( value );
setValueInternal( value ); setValueInternal( value );
@ -71,7 +89,7 @@ void QskBoundedValueInput::setValue( qreal value )
{ {
if ( isComponentComplete() ) if ( isComponentComplete() )
{ {
value = alignedValue( value ); value = qskAlignedValue( this, value );
value = fixupValue( value ); value = fixupValue( value );
} }

View File

@ -73,7 +73,6 @@ class QSK_EXPORT QskProgressIndicator : public QskBoundedControl
private: private:
void setValueInternal( qreal value ); void setValueInternal( qreal value );
void adjustBoundaries( bool increasing );
void adjustValue(); void adjustValue();
class PrivateData; class PrivateData;