Perceptual Color

extendeddoublevalidator.cpp
1// SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR MIT
3
4// Own headers
5// First the interface, which forces the header to be self-contained.
6#include "extendeddoublevalidator.h"
7// Second, the private implementation.
8#include "extendeddoublevalidator_p.h" // IWYU pragma: associated
9
10#include "constpropagatinguniquepointer.h"
11#include <qstringbuilder.h>
12class QObject;
13
14namespace PerceptualColor
15{
16/** @brief Default constructor
17 * @param parent pointer to the parent widget, if any */
18ExtendedDoubleValidator::ExtendedDoubleValidator(QObject *parent)
19 : QDoubleValidator(parent)
20 , d_pointer(new ExtendedDoubleValidatorPrivate)
21{
22}
23
24/** @brief Destructor */
25ExtendedDoubleValidator::~ExtendedDoubleValidator() noexcept
26{
27}
28
29// No documentation here (documentation of properties
30// and its getters are in the header)
31QString ExtendedDoubleValidator::prefix() const
32{
33 return d_pointer->m_prefix;
34}
35
36/** @brief Set the @ref prefix property. */
37void ExtendedDoubleValidator::setPrefix(const QString &prefix)
38{
39 if (prefix != d_pointer->m_prefix) {
40 d_pointer->m_prefix = prefix;
41 Q_EMIT prefixChanged(prefix);
42 }
43}
44
45/** @brief Set the @ref suffix property. */
46void ExtendedDoubleValidator::setSuffix(const QString &suffix)
47{
48 if (suffix != d_pointer->m_suffix) {
49 d_pointer->m_suffix = suffix;
50 Q_EMIT suffixChanged(suffix);
51 }
52}
53
54// No documentation here (documentation of properties
55// and its getters are in the header)
56QString ExtendedDoubleValidator::suffix() const
57{
58 return d_pointer->m_suffix;
59}
60
61QValidator::State ExtendedDoubleValidator::validate(QString &input, int &pos) const
62{
63 QString myInput = input;
64 int myPos = pos;
65
66 // IF (m_prefix.isEmpty && !m_prefix.isNull)
67 // THEN input.startsWith(m_prefix)
68 // → will be true IF !input.isEmpty
69 // → will be false IF input.isEmpty
70 // This is inconsistent. Therefore, we test is m_prefix is empty.
71 // If not, we do nothing.
72 // The same also applies to suffix.
73 // TODO Bug report, because Qt documentation says at
74 // https://doc.qt.io/qt-6/qstring.html#distinction-between-null-and-empty-strings
75 // (and at its Qt-5-counterpart):
76 // “All functions except isNull() treat null strings the same
77 // as empty strings.”
78 // This is apparently wrong (at least for Qt 5).
79 if (!d_pointer->m_prefix.isEmpty()) {
80 if (myInput.startsWith(d_pointer->m_prefix)) {
81 myInput.remove(0, d_pointer->m_prefix.size());
82 // In Qt6, QString::size() returns a qsizetype aka “long long int”.
83 // HACK We do a simple static_cast because a so long QString isn’t
84 // useful anyway.
85 myPos -= static_cast<int>(d_pointer->m_prefix.size());
86 } else {
87 return QValidator::State::Invalid;
88 }
89 }
90 if (!d_pointer->m_suffix.isEmpty()) {
91 if (myInput.endsWith(d_pointer->m_suffix)) {
92 myInput.chop(d_pointer->m_suffix.size());
93 } else {
94 return QValidator::State::Invalid;
95 }
96 }
97
98 QValidator::State result = QDoubleValidator::validate(myInput, myPos);
99 // Following the Qt documentation, QValidator::validate() is allowed
100 // and indented to make changes to the arguments passed by reference
101 // (“input” and “pos”). However, we use its child class QDoubleValidator.
102 // The documentation of QDoubleValidator states that the “pos” argument
103 // is not used. Therefore, write back only the “input” argument.
104 input = d_pointer->m_prefix + myInput + d_pointer->m_suffix;
105
106 return result;
107}
108
109} // namespace PerceptualColor
The namespace of this library.
virtual QValidator::State validate(QString &input, int &pos) const const override
void chop(qsizetype n)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:48:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.