File indexing completed on 2024-05-12 16:02:27

0001 /* This file is part of the KDE project
0002  *
0003  * SPDX-FileCopyrightText: 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch>
0004  * SPDX-FileCopyrightText: 2021 Deif Lou <ginoba@gmail.com>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KISINTPARSESPINBOX_H
0010 #define KISINTPARSESPINBOX_H
0011 
0012 #include <QSpinBox>
0013 #include <QScopedPointer>
0014 
0015 #include <kritawidgetutils_export.h>
0016 
0017 template <typename SpinBoxTypeTP, typename BaseSpinBoxTypeTP>
0018 class KisParseSpinBoxPrivate;
0019 
0020 /**
0021  * @brief The KisIntParseSpinBox class is a cleverer SpinBox, able to parse arithmetic expressions.
0022  *
0023  * Use this spinbox instead of the basic one from Qt if you want it to be able to parse arithmetic expressions.
0024  */
0025 class KRITAWIDGETUTILS_EXPORT KisIntParseSpinBox : public QSpinBox
0026 {
0027     Q_OBJECT
0028 public:
0029     KisIntParseSpinBox(QWidget *parent = 0);
0030     ~KisIntParseSpinBox() override;
0031 
0032     /**
0033      * @brief This is a reimplementation of @ref QSpinBox::stepBy that
0034      * uses @ref setValue
0035      * @param steps Number of steps that the value should change
0036      */
0037     void stepBy(int steps) override;
0038     /**
0039      * @brief Set the value of the spinbox
0040      * 
0041      * This reimplementation also tries to clear the current expression and
0042      * warning message whenever possible. This will happen when the new value
0043      * is different from the current one and the line edit has not the focus
0044      * or it is read-only. One can force the reset also by passing true to the
0045      * @p overwriteExpression parameter.
0046      * 
0047      * @param value The new value
0048      * @param overwriteExpression Get if the expression in the edit field
0049      * (and the warning message) should be reset to reflect the new value.
0050      * The default is false so that if the user is editing the expression
0051      * it won't be disrupted by any default call to this function
0052      */
0053     void setValue(int value, bool overwriteExpression = false);
0054     /**
0055      * @brief Get if the last expression entered is a valid one
0056      * @retval true if the last expression entered is valid
0057      * @retval false otherwise
0058      */
0059     bool isLastValid() const;
0060     /**
0061      * @brief This virtual function is similar to cleanText(). But child classes
0062      * may reimplement it to further process ("clean up") the expression.
0063      * @return The processed expression
0064      */
0065     virtual QString veryCleanText() const;
0066 
0067 Q_SIGNALS:
0068     /**
0069      * @brief signal emitted when the last parsed expression is not valid.
0070      */
0071     void errorWhileParsing(const QString &expr) const;
0072     /**
0073      * @brief signal emitted when the last parsed expression is valid and
0074      * the expression before was not valid.
0075      */
0076     void noMoreParsingError() const;
0077 
0078 protected:
0079     QValidator::State validate(QString &input, int &pos) const override;
0080     int valueFromText(const QString &text) const override;
0081     QString textFromValue(int value) const override;
0082 
0083 private:
0084     template <typename SpinBoxTypeTP, typename BaseSpinBoxTypeTP>
0085     friend class KisParseSpinBoxPrivate;
0086     QScopedPointer<KisParseSpinBoxPrivate<KisIntParseSpinBox, QSpinBox>> d;
0087 };
0088 
0089 #endif // KISINTPARSESPINBOX_H