File indexing completed on 2024-05-12 16:43:57

0001 /*
0002     SPDX-FileCopyrightText: 2010-2019 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef AMOUNTEDIT_H
0008 #define AMOUNTEDIT_H
0009 
0010 #include "kmm_widgets_export.h"
0011 
0012 // ----------------------------------------------------------------------------
0013 // QT Includes
0014 
0015 #include <QLineEdit>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 // ----------------------------------------------------------------------------
0021 // Project Includes
0022 
0023 #include "mymoneymoney.h"
0024 
0025 class MyMoneySecurity;
0026 
0027 /**
0028   * This class represents a widget to enter monetary values.
0029   * It has an edit field and a button to select a popup
0030   * calculator. The result of the calculator (if used) is
0031   * stored in the edit field.
0032   *
0033   * @author Thomas Baumgart
0034   */
0035 class AmountEditPrivate;
0036 class KMM_WIDGETS_EXPORT AmountEdit : public QLineEdit
0037 {
0038     Q_OBJECT
0039     Q_DISABLE_COPY(AmountEdit)
0040 
0041     Q_PROPERTY(bool calculatorButtonVisibility READ isCalculatorButtonVisible WRITE setCalculatorButtonVisible)
0042     Q_PROPERTY(bool allowEmpty READ isEmptyAllowed WRITE setAllowEmpty)
0043     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
0044     Q_PROPERTY(MyMoneyMoney value READ value WRITE setValue DESIGNABLE false STORED false USER true)
0045     Q_PROPERTY(bool valid READ isValid DESIGNABLE false STORED false)
0046 
0047 protected Q_SLOTS:
0048     void theTextChanged(const QString & text);
0049     void slotCalculatorResult();
0050     void slotCalculatorOpen();
0051     void slotCalculatorClose();
0052 
0053 public:
0054     explicit AmountEdit(QWidget* parent = nullptr, const int prec = -2);
0055     explicit AmountEdit(const MyMoneySecurity& eq, QWidget* parent = nullptr);
0056     virtual ~AmountEdit();
0057 
0058     static AmountEdit* global();
0059 
0060     MyMoneyMoney value() const;
0061 
0062     void setValue(const MyMoneyMoney& value);
0063 
0064     bool isValid() const;
0065 
0066     /**
0067       * This method returns the value of the edit field in "numerator/denominator" format.
0068       * If you want to get the text of the edit field, use lineedit()->text() instead.
0069       */
0070     QString numericalText() const;
0071 
0072     /**
0073       * Set the number of fractional digits that should be shown
0074       *
0075       * @param prec number of fractional digits.
0076       *
0077       * @note should be used prior to calling setText()
0078       * @sa precision
0079       */
0080     void setPrecision(const int prec);
0081 
0082     /**
0083       * return the number of fractional digits
0084       * @sa setPrecision
0085       */
0086     int precision() const;
0087 
0088     /**
0089       * This method allows to modify the behavior of the widget
0090       * such that it accepts an empty value (all blank) or not.
0091       * The default is to not accept an empty input and to
0092       * convert an empty field into 0.00 upon loss of focus.
0093       *
0094       * @param allowed if @a true, empty input is allowed, if @a false
0095       *                empty input will be converted to 0.00
0096       */
0097     void setAllowEmpty(bool allowed = true);
0098 
0099     bool isEmptyAllowed() const;
0100 
0101     bool isCalculatorButtonVisible() const;
0102 
0103     /**
0104      * This allows to setup the standard precision (number of decimal places)
0105      * to be used when no other information is available. @a prec must be in
0106      * the range of 0..19. If never set, the default precision is 2.
0107      *
0108      * @sa standardPrecision
0109      */
0110     static void setStandardPrecision(int prec);
0111 
0112     /**
0113      * This returns the global selected standard precision
0114      *
0115      * @sa setStandardPrecision
0116      */
0117     static int standardPrecision();
0118 
0119 public Q_SLOTS:
0120     void resetText();
0121 
0122     void setText(const QString& txt);
0123 
0124     /**
0125       * This method allows to show/hide the calculator button of the widget.
0126       * The parameter @p show controls the behavior. Default is to show the
0127       * button.
0128       *
0129       * @param show if true, button is shown, if false it is hidden
0130       */
0131     void setCalculatorButtonVisible(const bool show);
0132 
0133 Q_SIGNALS:
0134     /**
0135       * This signal is sent, when the focus leaves this widget and
0136       * the amount has been changed by user during this focus possession.
0137       */
0138     void valueChanged(const QString& text);
0139 
0140     /**
0141      * This signal is emitted when the contents of the widget
0142      * changed and was validated. Use this in favor of textChanged()
0143      * in your application.
0144      */
0145     void validatedTextChanged(const QString& text);
0146 
0147 protected:
0148     /**
0149       * This method ensures that the text version contains a
0150       * fractional part.
0151       */
0152     void ensureFractionalPart();
0153 
0154     /**
0155      * Overridden to support calculator button.
0156      */
0157     virtual void resizeEvent(QResizeEvent* event) override;
0158 
0159     /**
0160      * Overridden to support full selection upon entry.
0161      */
0162     virtual void focusInEvent(QFocusEvent* event) override;
0163 
0164     /**
0165      * Overridden to support ensureFractionalPart().
0166      */
0167     virtual void focusOutEvent(QFocusEvent* event) override;
0168 
0169     /**
0170      * Overridden to support calculator button.
0171      */
0172     virtual void keyPressEvent(QKeyEvent* event) override;
0173 
0174 private:
0175     AmountEditPrivate * const d_ptr;
0176     Q_DECLARE_PRIVATE(AmountEdit)
0177 };
0178 
0179 #endif