QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskBoundedRangeInput.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskBoundedRangeInput.h"
7#include "QskIntervalF.h"
8#include "QskFunctions.h"
9
10QskBoundedRangeInput::QskBoundedRangeInput( QQuickItem* parent )
11 : QskBoundedInput( parent )
12{
13}
14
15QskBoundedRangeInput::QskBoundedRangeInput(
16 const QskIntervalF& range, QQuickItem* parent )
17 : QskBoundedInput( parent )
18 , m_range( range )
19{
20}
21
22QskBoundedRangeInput::~QskBoundedRangeInput()
23{
24}
25
26void QskBoundedRangeInput::setLowerValueAsRatio( qreal ratio )
27{
28 ratio = qBound( 0.0, ratio, 1.0 );
29 setLowerValue( minimum() + ratio * boundaryLength() );
30}
31
32qreal QskBoundedRangeInput::lowerValueAsRatio() const
33{
34 return ( lowerValue() - minimum() ) / boundaryLength();
35}
36
37void QskBoundedRangeInput::setUpperValueAsRatio( qreal ratio )
38{
39 ratio = qBound( 0.0, ratio, 1.0 );
40 setUpperValue( minimum() + ratio * boundaryLength() );
41}
42
43qreal QskBoundedRangeInput::upperValueAsRatio() const
44{
45 return ( upperValue() - minimum() ) / boundaryLength();
46}
47
48void QskBoundedRangeInput::setLowerValue( qreal value )
49{
50 if ( isComponentComplete() )
51 {
52 value = qMin( value, m_range.upperBound() );
53 value = boundedValue( value );
54 }
55
56 setRange( QskIntervalF( value, m_range.upperBound() ) );
57}
58
59qreal QskBoundedRangeInput::lowerValue() const
60{
61 return m_range.lowerBound();
62}
63
64void QskBoundedRangeInput::setUpperValue( qreal value )
65{
66 if ( isComponentComplete() )
67 {
68 value = qMax( m_range.lowerBound(), value );
69 value = boundedValue( value );
70 }
71
72 setRange( QskIntervalF( m_range.lowerBound(), value ) );
73}
74
75qreal QskBoundedRangeInput::upperValue() const
76{
77 return m_range.upperBound();
78}
79
80void QskBoundedRangeInput::setRange( qreal lowerValue, qreal upperValue )
81{
82 setRange( QskIntervalF( lowerValue, upperValue ) );
83}
84
85void QskBoundedRangeInput::setRange( const QskIntervalF& range )
86{
87 auto newRange = range;
88
89 if ( isComponentComplete() )
90 {
91 if ( isSnapping() && stepSize() )
92 newRange = newRange.fuzzyAligned( stepSize() );
93
94 newRange = fixupRange( newRange );
95 }
96
97 setRangeInternal( range );
98}
99
100void QskBoundedRangeInput::resetRange()
101{
102 if ( m_range.isValid() )
103 {
104 m_range.invalidate();
105 Q_EMIT rangeChanged( m_range );
106 }
107}
108
109void QskBoundedRangeInput::setRangeInternal( const QskIntervalF& range )
110{
111 if ( range != m_range )
112 {
113 const auto oldRange = m_range;
114 m_range = range;
115
116 if ( !qskFuzzyCompare( m_range.lowerBound(), oldRange.lowerBound() ) )
117 Q_EMIT lowerValueChanged( m_range.lowerBound() );
118
119 if ( !qskFuzzyCompare( m_range.upperBound(), oldRange.upperBound() ) )
120 Q_EMIT upperValueChanged( m_range.upperBound() );
121
122 Q_EMIT rangeChanged( m_range );
123 }
124}
125
126QskIntervalF QskBoundedRangeInput::range() const
127{
128 return m_range;
129}
130
131void QskBoundedRangeInput::alignInput()
132{
133 auto newRange = m_range;
134 if ( isSnapping() && stepSize() )
135 newRange = newRange.fuzzyAligned( stepSize() );
136
137 setRangeInternal( newRange );
138}
139
140QskIntervalF QskBoundedRangeInput::fixupRange( const QskIntervalF& range ) const
141{
142 return range;
143}
144
145#include "moc_QskBoundedRangeInput.cpp"