qskinny/src/controls/QskBoundedValueInput.cpp

85 lines
1.8 KiB
C++
Raw Normal View History

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 "QskBoundedValueInput.h"
2017-07-21 18:21:34 +02:00
#include "QskFunctions.h"
QskBoundedValueInput::QskBoundedValueInput( QQuickItem* parent )
2020-07-25 14:19:14 +02:00
: QskBoundedInput( parent )
2017-07-21 18:21:34 +02:00
{
}
QskBoundedValueInput::~QskBoundedValueInput()
2017-07-21 18:21:34 +02:00
{
}
2020-07-25 14:19:14 +02:00
qreal QskBoundedValueInput::alignedValue( qreal value ) const
2017-07-21 18:21:34 +02:00
{
2020-07-25 14:19:14 +02:00
if ( snap() )
2017-07-21 18:21:34 +02:00
{
2020-07-25 14:19:14 +02:00
if ( const auto step = stepSize() )
value = qRound( value / step ) * step;
2017-07-21 18:21:34 +02:00
}
2020-07-25 14:19:14 +02:00
return qBound( minimum(), value, maximum() );
}
2017-07-21 18:21:34 +02:00
2020-07-25 14:19:14 +02:00
void QskBoundedValueInput::alignInput()
{
const auto newValue = alignedValue( m_value );
2017-07-21 18:21:34 +02:00
2020-07-25 14:19:14 +02:00
if ( newValue != m_value )
2017-07-21 18:21:34 +02:00
{
2020-07-25 14:19:14 +02:00
m_value = newValue;
2017-07-21 18:21:34 +02:00
Q_EMIT valueChanged( newValue );
}
}
qreal QskBoundedValueInput::fixupValue( qreal value ) const
2017-07-21 18:21:34 +02:00
{
return value;
2017-07-21 18:21:34 +02:00
}
void QskBoundedValueInput::setValueAsRatio( qreal ratio )
2017-07-21 18:21:34 +02:00
{
ratio = qBound( 0.0, ratio, 1.0 );
2020-07-25 14:19:14 +02:00
setValue( minimum() + ratio * boundaryLength() );
}
qreal QskBoundedValueInput::valueAsRatio() const
{
2020-07-25 14:19:14 +02:00
return ( m_value - minimum() ) / boundaryLength();
2017-07-21 18:21:34 +02:00
}
void QskBoundedValueInput::setValue( qreal value )
2017-07-21 18:21:34 +02:00
{
if ( isComponentComplete() )
{
2020-07-25 14:19:14 +02:00
value = alignedValue( value );
2017-07-21 18:21:34 +02:00
}
value = fixupValue( value );
2020-07-25 14:19:14 +02:00
if ( !qskFuzzyCompare( value, m_value ) )
2017-07-21 18:21:34 +02:00
{
2020-07-25 14:19:14 +02:00
m_value = value;
2017-07-21 18:21:34 +02:00
Q_EMIT valueChanged( value );
update();
}
}
qreal QskBoundedValueInput::value() const
2017-07-21 18:21:34 +02:00
{
2020-07-25 14:19:14 +02:00
return m_value;
2017-07-21 18:21:34 +02:00
}
2020-07-25 14:19:14 +02:00
void QskBoundedValueInput::increment( qreal offset )
2017-07-21 18:21:34 +02:00
{
2020-07-25 14:19:14 +02:00
setValue( m_value + offset );
2017-07-21 18:21:34 +02:00
}
#include "moc_QskBoundedValueInput.cpp"