QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskBoundedValueInput.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskBoundedValueInput.h"
7#include "QskFunctions.h"
8
9#include <qlocale.h>
10#include <cfloat>
11
12class QskBoundedValueInput::PrivateData
13{
14 public:
15 qreal value = 0.0;
16 int decimals = 2;
17};
18
19QskBoundedValueInput::QskBoundedValueInput( QQuickItem* parent )
20 : Inherited( parent )
21 , m_data( new PrivateData )
22{
23}
24
25QskBoundedValueInput::~QskBoundedValueInput()
26{
27}
28
29void QskBoundedValueInput::setDecimals( int decimals )
30{
31 decimals = qBound( 0, decimals, DBL_MAX_10_EXP + DBL_DIG );
32 if ( decimals != m_data->decimals )
33 {
34 m_data->decimals = decimals;
35
36 update();
38 }
39}
40
41int QskBoundedValueInput::decimals() const
42{
43 return m_data->decimals;
44}
45
46void QskBoundedValueInput::alignInput()
47{
48 auto value = alignedValue( m_data->value );
49 value = fixupValue( value );
50
51 setValueInternal( value );
52}
53
54qreal QskBoundedValueInput::fixupValue( qreal value ) const
55{
56 return value;
57}
58
59void QskBoundedValueInput::setValueAsRatio( qreal ratio )
60{
61 ratio = qBound( 0.0, ratio, 1.0 );
62 setValue( minimum() + ratio * boundaryLength() );
63}
64
65qreal QskBoundedValueInput::valueAsRatio() const
66{
67 return valueAsRatio( m_data->value );
68}
69
70void QskBoundedValueInput::setValue( qreal value )
71{
72 if ( isComponentComplete() )
73 {
74 value = alignedValue( value );
75 value = fixupValue( value );
76 }
77
78 setValueInternal( value );
79}
80
81qreal QskBoundedValueInput::value() const
82{
83 return m_data->value;
84}
85
86void QskBoundedValueInput::increment( qreal offset )
87{
88 setValue( m_data->value + offset );
89}
90
91void QskBoundedValueInput::setValueInternal( qreal value )
92{
93 if ( !qskFuzzyCompare( value, m_data->value ) )
94 {
95 m_data->value = value;
96 Q_EMIT valueChanged( value );
97
98 update();
99 }
100}
101
102QString QskBoundedValueInput::valueText() const
103{
104 return textFromValue( value() );
105}
106
107QString QskBoundedValueInput::textFromValue( qreal value ) const
108{
109 return locale().toString( value, 'f', m_data->decimals );
110}
111
112#include "moc_QskBoundedValueInput.cpp"
QLocale locale
Definition QskControl.h:27
void resetImplicitSize()
Definition QskItem.cpp:721