aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/doc/how-tos/how-to-cpp-gauge/valuerange.cpp
blob: 62be0bc30edc8194035a60be801c8e49ea318891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "valuerange.h"

#include <algorithm>

QT_BEGIN_NAMESPACE

ValueRange::ValueRange(QObject *parent)
    : QObject(parent)
    , mInverted(false)
{
}

/*!
    \property ValueRange::minimumValue
    \brief the minimum value that \l value can assume

    The default value is \c 0.
*/
qreal ValueRange::minimumValue() const
{
    return mMinimumValue;
}

void ValueRange::setMinimumValue(qreal min)
{
    if (qFuzzyCompare(mMinimumValue, min))
        return;

    mMinimumValue = min;
    emit minimumValueChanged();
    emit constraintsChanged();
    if (mIsComplete) {
        setValue(mValue);
        updatePosition();
    }
}

/*!
    \property ValueRange::maximumValue
    \brief the maximum value that \l value can assume

    The default value is \c 100.
*/
qreal ValueRange::maximumValue() const
{
    return mMaximumValue;
}

void ValueRange::setMaximumValue(qreal max)
{
    if (qFuzzyCompare(mMaximumValue, max))
        return;

    mMaximumValue = max;
    emit maximumValueChanged();
    emit constraintsChanged();
    if (mIsComplete) {
        setValue(mMaximumValue);
        updatePosition();
    }
}

/*!
    \property ValueRange::stepSize

    This property holds the step size. The default value is \c 0.0.

    For example, if a user sets \l minimumValue to \c 0, \l maximumValue to
    \c 100, and stepSize to \c 30, the valid values for \l value would be:
    \c 0, \c 30, \c 60, \c 90, and \c 100.
*/

void ValueRange::setStepSize(qreal stepSize)
{
    stepSize = qMax(qreal(0.0), stepSize);
    if (qFuzzyCompare(mStepSize, stepSize))
        return;

    mStepSize = stepSize;
    emit constraintsChanged();
    emit stepSizeChanged();
}

qreal ValueRange::stepSize() const
{
    return mStepSize;
}


void ValueRange::classBegin()
{
}

void ValueRange::componentComplete()
{
    mIsComplete = true;
}

/*!
    \qmlproperty real ValueRange::value

    This property holds the value in the range \c from - \c to. The default value is \c 0.0.

    \sa position
*/
qreal ValueRange::value() const
{
    return mValue;
}

void ValueRange::setValue(qreal value)
{
    if (mIsComplete)
        value = mMinimumValue > mMaximumValue ? qBound(mMaximumValue, value, mMinimumValue) : qBound(mMinimumValue, value, mMaximumValue);

    if (qFuzzyCompare(mValue, value))
        return;

    mValue = value;
    updatePosition();
    emit valueChanged();
}

/*!
    \qmlproperty real ValueRange::position
    \readonly

    This property holds the logical position of the range's value.

    The position is expressed as a fraction of the control's size, in the range
    \c {0.0 - 1.0}. For visualizing a slider, the right-to-left aware
    \l visualPosition should be used instead.

    \sa value, visualPosition, valueAt()
*/
qreal ValueRange::position() const
{
    return mPosition;
}

/*!
    \qmlproperty real ValueRange::visualPosition
    \readonly

    This property holds the visual position of the range's value.

    The position is expressed as a fraction of the control's size, in the range
    \c {0.0 - 1.0}. When the control is \l {Control::mirrored}{mirrored}, the
    value is equal to \c {1.0 - position}. This makes the value suitable for
    visualizing the slider, taking right-to-left support into account.

    \sa position
*/
qreal ValueRange::visualPosition() const
{
    if (mInverted)
        return 1.0 - mPosition;
    return mPosition;
}

void ValueRange::setPosition(qreal pos)
{
    pos = std::clamp(pos, qreal(0.0), qreal(1.0));
    if (qFuzzyCompare(mPosition, pos))
        return;

    mPosition = pos;
    emit positionChanged();
    emit visualPositionChanged();
}


void ValueRange::updatePosition()
{
    qreal pos = 0;
    if (!qFuzzyCompare(mMinimumValue, mMaximumValue))
        pos = (mValue - mMinimumValue) / (mMaximumValue - mMinimumValue);
    setPosition(pos);
}

/*!
    \property ValueRange::inverted

    This property holds whether the \l minimumValue and \l maximumValue should be visually inverted.
*/

void ValueRange::setInverted(bool inverted)
{
    if (inverted == mInverted)
        return;

    mInverted = inverted;
    emit visualPositionChanged();
    emit invertedChanged();
}

bool ValueRange::inverted() const
{
    return mInverted;
}

QT_END_NAMESPACE