1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Controls module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qquickspinboxvalidator_p.h" |
41 | |
42 | #if QT_CONFIG(validator) |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | QQuickSpinBoxValidator1::QQuickSpinBoxValidator1(QObject *parent) |
47 | : QValidator(parent), m_value(0), m_step(1), m_initialized(false) |
48 | { |
49 | m_validator.setTop(99); |
50 | m_validator.setBottom(0); |
51 | m_validator.setDecimals(0); |
52 | m_validator.setNotation(QDoubleValidator::StandardNotation); |
53 | |
54 | QLocale locale; |
55 | locale.setNumberOptions(QLocale::OmitGroupSeparator); |
56 | setLocale(locale); |
57 | |
58 | connect(sender: this, SIGNAL(valueChanged()), receiver: this, SIGNAL(textChanged())); |
59 | connect(sender: this, SIGNAL(minimumValueChanged()), receiver: this, SIGNAL(textChanged())); |
60 | connect(sender: this, SIGNAL(maximumValueChanged()), receiver: this, SIGNAL(textChanged())); |
61 | connect(sender: this, SIGNAL(decimalsChanged()), receiver: this, SIGNAL(textChanged())); |
62 | connect(sender: this, SIGNAL(prefixChanged()), receiver: this, SIGNAL(textChanged())); |
63 | connect(sender: this, SIGNAL(suffixChanged()), receiver: this, SIGNAL(textChanged())); |
64 | } |
65 | |
66 | QQuickSpinBoxValidator1::~QQuickSpinBoxValidator1() |
67 | { |
68 | } |
69 | |
70 | QString QQuickSpinBoxValidator1::text() const |
71 | { |
72 | return textFromValue(value: m_value); |
73 | } |
74 | |
75 | qreal QQuickSpinBoxValidator1::value() const |
76 | { |
77 | return m_value; |
78 | } |
79 | |
80 | void QQuickSpinBoxValidator1::setValue(qreal value) |
81 | { |
82 | if (m_initialized) { |
83 | value = qBound(min: minimumValue(), val: value, max: maximumValue()); |
84 | value = QString::number(value, f: 'f', prec: m_validator.decimals()).toDouble(); |
85 | } |
86 | |
87 | if (m_value != value) { |
88 | m_value = value; |
89 | |
90 | if (m_initialized) |
91 | emit valueChanged(); |
92 | } |
93 | } |
94 | |
95 | qreal QQuickSpinBoxValidator1::minimumValue() const |
96 | { |
97 | return m_validator.bottom(); |
98 | } |
99 | |
100 | void QQuickSpinBoxValidator1::setMinimumValue(qreal min) |
101 | { |
102 | if (min != m_validator.bottom()) { |
103 | m_validator.setBottom(min); |
104 | emit minimumValueChanged(); |
105 | if (m_initialized) |
106 | setValue(m_value); |
107 | } |
108 | } |
109 | |
110 | qreal QQuickSpinBoxValidator1::maximumValue() const |
111 | { |
112 | return m_validator.top(); |
113 | } |
114 | |
115 | void QQuickSpinBoxValidator1::setMaximumValue(qreal max) |
116 | { |
117 | if (max != m_validator.top()) { |
118 | m_validator.setTop(max); |
119 | emit maximumValueChanged(); |
120 | if (m_initialized) |
121 | setValue(m_value); |
122 | } |
123 | } |
124 | |
125 | int QQuickSpinBoxValidator1::decimals() const |
126 | { |
127 | return m_validator.decimals(); |
128 | } |
129 | |
130 | void QQuickSpinBoxValidator1::setDecimals(int decimals) |
131 | { |
132 | if (decimals != m_validator.decimals()) { |
133 | m_validator.setDecimals(decimals); |
134 | emit decimalsChanged(); |
135 | if (m_initialized) |
136 | setValue(m_value); |
137 | } |
138 | } |
139 | |
140 | qreal QQuickSpinBoxValidator1::stepSize() const |
141 | { |
142 | return m_step; |
143 | } |
144 | |
145 | void QQuickSpinBoxValidator1::setStepSize(qreal step) |
146 | { |
147 | if (m_step != step) { |
148 | m_step = step; |
149 | emit stepSizeChanged(); |
150 | } |
151 | } |
152 | |
153 | QString QQuickSpinBoxValidator1::prefix() const |
154 | { |
155 | return m_prefix; |
156 | } |
157 | |
158 | void QQuickSpinBoxValidator1::setPrefix(const QString &prefix) |
159 | { |
160 | if (m_prefix != prefix) { |
161 | m_prefix = prefix; |
162 | emit prefixChanged(); |
163 | } |
164 | } |
165 | |
166 | QString QQuickSpinBoxValidator1::suffix() const |
167 | { |
168 | return m_suffix; |
169 | } |
170 | |
171 | void QQuickSpinBoxValidator1::setSuffix(const QString &suffix) |
172 | { |
173 | if (m_suffix != suffix) { |
174 | m_suffix = suffix; |
175 | emit suffixChanged(); |
176 | } |
177 | } |
178 | |
179 | void QQuickSpinBoxValidator1::fixup(QString &input) const |
180 | { |
181 | input = textFromValue(value: m_value).remove(c: locale().groupSeparator()); |
182 | } |
183 | |
184 | QValidator::State QQuickSpinBoxValidator1::validate(QString &input, int &pos) const |
185 | { |
186 | if (pos > 0 && pos < input.length()) { |
187 | if (input.at(i: pos - 1) == locale().groupSeparator()) |
188 | return QValidator::Invalid; |
189 | if (input.at(i: pos - 1) == locale().decimalPoint() && m_validator.decimals() == 0) |
190 | return QValidator::Invalid; |
191 | } |
192 | |
193 | if (!m_prefix.isEmpty() && !input.startsWith(s: m_prefix)) { |
194 | input.prepend(s: m_prefix); |
195 | pos += m_prefix.length(); |
196 | } |
197 | |
198 | if (!m_suffix.isEmpty() && !input.endsWith(s: m_suffix)) |
199 | input.append(s: m_suffix); |
200 | |
201 | QString value = input.mid(position: m_prefix.length(), n: input.length() - m_prefix.length() - m_suffix.length()); |
202 | int valuePos = pos - m_prefix.length(); |
203 | QValidator::State state = m_validator.validate(value, valuePos); |
204 | input = m_prefix + value + m_suffix; |
205 | pos = m_prefix.length() + valuePos; |
206 | |
207 | if (state == QValidator::Acceptable || state == QValidator::Intermediate) { |
208 | bool ok = false; |
209 | qreal val = locale().toDouble(s: value, ok: &ok); |
210 | if (ok) { |
211 | if (state == QValidator::Acceptable || |
212 | (state == QValidator::Intermediate && val >= 0 && val <= m_validator.top()) || |
213 | (state == QValidator::Intermediate && val < 0 && val >= m_validator.bottom())) { |
214 | const_cast<QQuickSpinBoxValidator1 *>(this)->setValue(val); |
215 | if (input != textFromValue(value: val)) |
216 | state = QValidator::Intermediate; |
217 | } else if (val < m_validator.bottom() || val > m_validator.top()) { |
218 | return QValidator::Invalid; |
219 | } |
220 | } |
221 | } |
222 | return state; |
223 | } |
224 | |
225 | void QQuickSpinBoxValidator1::componentComplete() |
226 | { |
227 | m_initialized = true; |
228 | setValue(m_value); |
229 | } |
230 | |
231 | void QQuickSpinBoxValidator1::increment() |
232 | { |
233 | setValue(m_value + m_step); |
234 | } |
235 | |
236 | void QQuickSpinBoxValidator1::decrement() |
237 | { |
238 | setValue(m_value - m_step); |
239 | } |
240 | |
241 | QString QQuickSpinBoxValidator1::textFromValue(qreal value) const |
242 | { |
243 | return m_prefix + locale().toString(i: value, f: 'f', prec: m_validator.decimals()) + m_suffix; |
244 | } |
245 | |
246 | QT_END_NAMESPACE |
247 | |
248 | #endif // QT_CONFIG(validator) |
249 | |