File indexing completed on 2024-05-12 04:44:32

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 #ifndef EXTENDEDDOUBLEVALIDATOR_H
0005 #define EXTENDEDDOUBLEVALIDATOR_H
0006 
0007 #include "constpropagatinguniquepointer.h"
0008 #include <qglobal.h>
0009 #include <qstring.h>
0010 #include <qvalidator.h>
0011 class QObject;
0012 
0013 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0014 #include <qtmetamacros.h>
0015 #else
0016 #include <qobjectdefs.h>
0017 #endif
0018 
0019 namespace PerceptualColor
0020 {
0021 class ExtendedDoubleValidatorPrivate;
0022 
0023 /** @internal
0024  *
0025  *  @brief The @ref ExtendedDoubleValidator class provides range checking
0026  * of floating-point numbers with support for prefix and/or suffix.
0027  *
0028  * This class behaves exactly like its base class <tt>QDoubleValidator</tt>
0029  * with the difference that is allows to specify prefixes and/or suffixed
0030  * that are considered by @ref validate(). */
0031 class ExtendedDoubleValidator : public QDoubleValidator
0032 {
0033     Q_OBJECT
0034 
0035     /** @brief The prefix of the number.
0036      *
0037      * @sa @ref prefix()
0038      * @sa @ref setPrefix()
0039      * @sa @ref prefixChanged()
0040      * @sa @ref ExtendedDoubleValidatorPrivate::m_prefix */
0041     Q_PROPERTY(QString prefix READ prefix WRITE setPrefix NOTIFY prefixChanged)
0042 
0043     /** @brief The suffix of the number.
0044      *
0045      * @sa @ref suffix()
0046      * @sa @ref setSuffix()
0047      * @sa @ref suffixChanged()
0048      * @sa @ref ExtendedDoubleValidatorPrivate::m_suffix */
0049     Q_PROPERTY(QString suffix READ suffix WRITE setSuffix NOTIFY suffixChanged)
0050 
0051 public:
0052     Q_INVOKABLE explicit ExtendedDoubleValidator(QObject *parent = nullptr);
0053     virtual ~ExtendedDoubleValidator() noexcept override;
0054     /** @brief Getter for property @ref prefix
0055      *  @returns the property @ref prefix */
0056     [[nodiscard]] QString prefix() const;
0057     /** @brief Getter for property @ref suffix
0058      *  @returns the property @ref suffix */
0059     [[nodiscard]] QString suffix() const;
0060     /** @brief Validate input.
0061      *
0062      * @param input the input string
0063      * @param pos the cursor position
0064      * @returns
0065      * - An input that does not contain the @ref prefix() and @ref suffix()
0066      *   (if these are not empty) is always invalid.
0067      * - If an input contains correct prefix and suffix, the floating point
0068      *   portion is validated as <tt>QDoubleValidator</tt> does and
0069      *   the corresponding result is returned. */
0070     [[nodiscard]] virtual QValidator::State validate(QString &input, int &pos) const override;
0071 
0072 public Q_SLOTS:
0073     void setPrefix(const QString &prefix);
0074     void setSuffix(const QString &suffix);
0075 
0076 Q_SIGNALS:
0077     /** @brief Notify signal for property @ref prefix.
0078      *
0079      * @param prefix the new prefix */
0080     void prefixChanged(const QString &prefix);
0081     /** @brief Notify signal for property @ref suffix.
0082      *
0083      * @param suffix the new suffix */
0084     void suffixChanged(const QString &suffix);
0085 
0086 private:
0087     Q_DISABLE_COPY(ExtendedDoubleValidator)
0088 
0089     /** @internal
0090      *
0091      * @brief Declare the private implementation as friend class.
0092      *
0093      * This allows the private class to access the protected members and
0094      * functions of instances of <em>this</em> class. */
0095     friend class ExtendedDoubleValidatorPrivate;
0096     /** @brief Pointer to implementation (pimpl) */
0097     ConstPropagatingUniquePointer<ExtendedDoubleValidatorPrivate> d_pointer;
0098 
0099     /** @internal @brief Only for unit tests. */
0100     friend class TestExtendedDoubleValidator;
0101 };
0102 
0103 } // namespace PerceptualColor
0104 
0105 #endif // EXTENDEDDOUBLEVALIDATOR_H