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
12static qreal qskAlignedValue( const QskBoundedValueInput* input, qreal value )
13{
14 value = input->boundedValue( value );
15
16 if ( value > input->minimum() && value < input->maximum() )
17 {
18 if ( input->isSnapping() && input->stepSize() )
19 {
20 const auto step = input->stepSize();
21
22 value = qRound( value / step ) * step;
23 value = input->boundedValue( value );
24 }
25 }
26
27 return value;
28}
29
30class QskBoundedValueInput::PrivateData
31{
32 public:
33 qreal value = 0.0;
34 int decimals = 2;
35};
36
37QskBoundedValueInput::QskBoundedValueInput( QQuickItem* parent )
38 : Inherited( parent )
39 , m_data( new PrivateData )
40{
41}
42
43QskBoundedValueInput::~QskBoundedValueInput()
44{
45}
46
47void QskBoundedValueInput::setDecimals( int decimals )
48{
49 decimals = qBound( 0, decimals, DBL_MAX_10_EXP + DBL_DIG );
50 if ( decimals != m_data->decimals )
51 {
52 m_data->decimals = decimals;
53
54 update();
56 }
57}
58
59int QskBoundedValueInput::decimals() const
60{
61 return m_data->decimals;
62}
63
64void QskBoundedValueInput::keyPressEvent( QKeyEvent* event )
65{
66 switch( event->key() )
67 {
68 case Qt::Key_Home:
69 setValue( minimum() );
70 break;
71
72 case Qt::Key_End:
73 setValue( maximum() );
74 break;
75 }
76
77 Inherited::keyPressEvent( event );
78}
79
80void QskBoundedValueInput::alignInput()
81{
82 auto value = qskAlignedValue( this, m_data->value );
83 value = fixupValue( value );
84
85 setValueInternal( value );
86}
87
88qreal QskBoundedValueInput::fixupValue( qreal value ) const
89{
90 return value;
91}
92
93void QskBoundedValueInput::setValueAsRatio( qreal ratio )
94{
95 ratio = qBound( 0.0, ratio, 1.0 );
96 setValue( minimum() + ratio * boundaryLength() );
97}
98
99qreal QskBoundedValueInput::valueAsRatio() const
100{
101 return valueAsRatio( m_data->value );
102}
103
104void QskBoundedValueInput::setValue( qreal value )
105{
106 if ( isComponentComplete() )
107 {
108 value = qskAlignedValue( this, value );
109 value = fixupValue( value );
110 }
111
112 setValueInternal( value );
113}
114
115qreal QskBoundedValueInput::value() const
116{
117 return m_data->value;
118}
119
120void QskBoundedValueInput::increment( qreal offset )
121{
122 setValue( m_data->value + offset );
123}
124
125void QskBoundedValueInput::setValueInternal( qreal value )
126{
127 if ( !qskFuzzyCompare( value, m_data->value ) )
128 {
129 m_data->value = value;
130 Q_EMIT valueChanged( value );
131
132 update();
133 }
134}
135
136QString QskBoundedValueInput::valueText() const
137{
138 return textFromValue( value() );
139}
140
141QString QskBoundedValueInput::textFromValue( qreal value ) const
142{
143 return locale().toString( value, 'f', m_data->decimals );
144}
145
146#include "moc_QskBoundedValueInput.cpp"
QLocale locale
Definition QskControl.h:27
void resetImplicitSize()
Definition QskItem.cpp:727