QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskBoundedControl.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskBoundedControl.h"
7#include "QskFunctions.h"
8#include "QskIntervalF.h"
9
10QskBoundedControl::QskBoundedControl( QQuickItem* parent )
11 : QskBoundedControl( 0.0, 1.0, parent )
12{
13}
14
15QskBoundedControl::QskBoundedControl( qreal min, qreal max, QQuickItem* parent )
16 : QskControl( parent )
17 , m_minimum( qMin( min, max ) )
18 , m_maximum( qMax( min, max ) )
19{
20}
21
22QskBoundedControl::~QskBoundedControl()
23{
24}
25
26void QskBoundedControl::setMinimum( qreal minimum )
27{
28 if ( qskFuzzyCompare( m_minimum, minimum ) )
29 return;
30
31 m_minimum = minimum;
32 Q_EMIT minimumChanged( minimum );
33
34 if ( isComponentComplete() )
35 adjustBoundaries( false );
36
37 Q_EMIT boundariesChanged( boundaries() );
38 update();
39}
40
41qreal QskBoundedControl::minimum() const
42{
43 return m_minimum;
44}
45
46void QskBoundedControl::setMaximum( qreal maximum )
47{
48 if ( qskFuzzyCompare( m_maximum, maximum ) )
49 return;
50
51 m_maximum = maximum;
52 Q_EMIT maximumChanged( maximum );
53
54 if ( isComponentComplete() )
55 adjustBoundaries( true );
56
57 Q_EMIT boundariesChanged( boundaries() );
58 update();
59}
60
61qreal QskBoundedControl::maximum() const
62{
63 return m_maximum;
64}
65
66void QskBoundedControl::setBoundaries( qreal min, qreal max )
67{
68 if ( max < min )
69 max = min;
70
71 const auto oldMin = m_minimum;
72 const auto oldMax = m_maximum;
73
74 if ( min == oldMin && max == oldMax )
75 return;
76
77 m_minimum = min;
78 m_maximum = max;
79
80 if ( isComponentComplete() )
81 adjustBoundaries( false );
82
83 if ( m_minimum != oldMin )
84 Q_EMIT minimumChanged( m_minimum );
85
86 if ( m_maximum != oldMax )
87 Q_EMIT maximumChanged( m_maximum );
88
89 Q_EMIT boundariesChanged( boundaries() );
90 update();
91}
92
93void QskBoundedControl::setBoundaries( const QskIntervalF& boundaries )
94{
95 setBoundaries( boundaries.lowerBound(), boundaries.upperBound() );
96}
97
98QskIntervalF QskBoundedControl::boundaries() const
99{
100 return QskIntervalF( m_minimum, m_maximum );
101}
102
103void QskBoundedControl::adjustBoundaries( bool increasing )
104{
105 if ( m_maximum >= m_minimum )
106 return;
107
108 if ( increasing )
109 {
110 m_minimum = m_maximum;
111 Q_EMIT minimumChanged( m_minimum );
112 }
113 else
114 {
115 m_maximum = m_minimum;
116 Q_EMIT maximumChanged( m_maximum );
117 }
118
119 Q_EMIT boundariesChanged( boundaries() );
120 update();
121}
122
123qreal QskBoundedControl::boundaryLength() const
124{
125 return m_maximum - m_minimum;
126}
127
128void QskBoundedControl::componentComplete()
129{
131 adjustBoundaries( true );
132}
133
134qreal QskBoundedControl::boundedValue( qreal value ) const
135{
136 if ( qskFuzzyCompare( value, minimum() ) )
137 return minimum();
138
139 if ( qskFuzzyCompare( value, maximum() ) )
140 return maximum();
141
142 return qBound( minimum(), value, maximum() );
143}
144
145qreal QskBoundedControl::valueAsRatio( qreal value ) const
146{
147 const auto range = m_maximum - m_minimum;
148 if ( qFuzzyIsNull( range ) )
149 return 0.0;
150
151 return ( value - m_minimum ) / range;
152}
153
154qreal QskBoundedControl::valueFromRatio( qreal ratio ) const
155{
156 ratio = qBound( 0.0, ratio, 1.0 );
157 return m_minimum + ratio * ( m_maximum - m_minimum );
158}
159
160#include "moc_QskBoundedControl.cpp"
Base class of all controls.
Definition QskControl.h:23
void componentComplete() override
Definition QskItem.cpp:272