diff options
Diffstat (limited to 'src/widgets/widgets/qspinbox.cpp')
-rw-r--r-- | src/widgets/widgets/qspinbox.cpp | 94 |
1 files changed, 30 insertions, 64 deletions
diff --git a/src/widgets/widgets/qspinbox.cpp b/src/widgets/widgets/qspinbox.cpp index 6ac2e1cec2..73946d54a9 100644 --- a/src/widgets/widgets/qspinbox.cpp +++ b/src/widgets/widgets/qspinbox.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <private/qabstractspinbox_p.h> #include <qspinbox.h> @@ -51,6 +15,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + //#define QSPINBOX_QSBDEBUG #ifdef QSPINBOX_QSBDEBUG # define QSBDEBUG qDebug @@ -166,12 +132,9 @@ public: for a custom spin box that allows the user to enter icon sizes (e.g., "32 x 32"): - \snippet widgets/icons/iconsizespinbox.cpp 1 + \snippet code/src_gui_widgets_qspinbox.cpp 8 \codeline - \snippet widgets/icons/iconsizespinbox.cpp 2 - - See the \l{widgets/icons}{Icons} example for the full source - code. + \snippet code/src_gui_widgets_qspinbox.cpp 9 \sa QDoubleSpinBox, QDateTimeEdit, QSlider, {Spin Boxes Example} */ @@ -518,7 +481,7 @@ QString QSpinBox::textFromValue(int value) const QString str; if (d->displayIntegerBase != 10) { - const QLatin1String prefix = value < 0 ? QLatin1String("-") : QLatin1String(); + const auto prefix = value < 0 ? "-"_L1 : ""_L1; str = prefix + QString::number(qAbs(value), d->displayIntegerBase); } else { str = locale().toString(value); @@ -977,9 +940,8 @@ void QDoubleSpinBox::setDecimals(int decimals) This virtual function is used by the spin box whenever it needs to display the given \a value. The default implementation returns a string containing \a value printed using QWidget::locale().toString(\a value, - QLatin1Char('f'), decimals()) and will remove the thousand - separator unless setGroupSeparatorShown() is set. Reimplementations may - return anything. + \c u'f', decimals()) and will remove the thousand separator unless + setGroupSeparatorShown() is set. Reimplementations may return anything. Note: QDoubleSpinBox does not call this function for specialValueText() and that neither prefix() nor suffix() should @@ -1127,11 +1089,11 @@ QVariant QSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, int num = min; if (max != min && (copy.isEmpty() - || (min < 0 && copy == QLatin1String("-")) - || (max >= 0 && copy == QLatin1String("+")))) { + || (min < 0 && copy == "-"_L1) + || (max >= 0 && copy == "+"_L1))) { state = QValidator::Intermediate; QSBDEBUG() << __FILE__ << __LINE__<< "num is set to" << num; - } else if (copy.startsWith(QLatin1Char('-')) && min >= 0) { + } else if (copy.startsWith(u'-') && min >= 0) { state = QValidator::Invalid; // special-case -0 will be interpreted as 0 and thus not be invalid with a range from 0-100 } else { bool ok = false; @@ -1281,24 +1243,25 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, const bool minus = min <= 0; const QString group(locale.groupSeparator()); - const uint groupUcs = (group.size() > 1 && group.at(0).isHighSurrogate() - ? QChar::surrogateToUcs4(group.at(0), group.at(1)) - : group.at(0).unicode()); + const uint groupUcs = (group.isEmpty() ? 0 : + (group.size() > 1 && group.at(0).isHighSurrogate() + ? QChar::surrogateToUcs4(group.at(0), group.at(1)) + : group.at(0).unicode())); switch (len) { case 0: state = max != min ? QValidator::Intermediate : QValidator::Invalid; goto end; case 1: if (copy.at(0) == locale.decimalPoint() - || (plus && copy.at(0) == QLatin1Char('+')) - || (minus && copy.at(0) == QLatin1Char('-'))) { + || (plus && copy.at(0) == u'+') + || (minus && copy.at(0) == u'-')) { state = QValidator::Intermediate; goto end; } break; case 2: if (copy.at(1) == locale.decimalPoint() - && ((plus && copy.at(0) == QLatin1Char('+')) || (minus && copy.at(0) == QLatin1Char('-')))) { + && ((plus && copy.at(0) == u'+') || (minus && copy.at(0) == u'-'))) { state = QValidator::Intermediate; goto end; } @@ -1306,7 +1269,7 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, default: break; } - if (copy.at(0) == locale.groupSeparator()) { + if (groupUcs && copy.startsWith(group)) { QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid"; state = QValidator::Invalid; goto end; @@ -1322,8 +1285,9 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, state = QValidator::Invalid; goto end; } - for (int i=dec + 1; i<copy.size(); ++i) { - if (copy.at(i).isSpace() || copy.at(i) == locale.groupSeparator()) { + for (int i = dec + 1; i < copy.size(); ++i) { + if (copy.at(i).isSpace() + || (groupUcs && QStringView{copy}.sliced(i).startsWith(group))) { QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid"; state = QValidator::Invalid; goto end; @@ -1331,10 +1295,11 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, } } else { const QChar last = copy.back(); - const bool groupEnd = copy.endsWith(group); + const bool groupEnd = groupUcs && copy.endsWith(group); const QStringView head(copy.constData(), groupEnd ? len - group.size() : len - 1); const QChar secondLast = head.back(); - if ((groupEnd || last.isSpace()) && (head.endsWith(group) || secondLast.isSpace())) { + if ((groupEnd || last.isSpace()) + && ((groupUcs && head.endsWith(group)) || secondLast.isSpace())) { state = QValidator::Invalid; QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid"; goto end; @@ -1353,7 +1318,7 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, if (!ok) { if (QChar::isPrint(groupUcs)) { - if (max < 1000 && min > -1000 && copy.contains(group)) { + if (max < 1000 && min > -1000 && groupUcs && copy.contains(group)) { state = QValidator::Invalid; QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid"; goto end; @@ -1361,7 +1326,7 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, const int len = copy.size(); for (int i = 0; i < len - 1;) { - if (QStringView(copy).mid(i).startsWith(group)) { + if (groupUcs && QStringView{copy}.sliced(i).startsWith(group)) { if (QStringView(copy).mid(i + group.size()).startsWith(group)) { QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid"; state = QValidator::Invalid; @@ -1374,7 +1339,8 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos, } QString copy2 = copy; - copy2.remove(group); + if (groupUcs) + copy2.remove(group); num = locale.toDouble(copy2, &ok); QSBDEBUG() << group << num << copy2 << ok; |