File indexing completed on 2024-05-12 16:44:01

0001 /*
0002     SPDX-FileCopyrightText: 2002-2017 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 KMYMONEYCALCULATOR_H
0008 #define KMYMONEYCALCULATOR_H
0009 
0010 // ----------------------------------------------------------------------------
0011 // QT Includes
0012 
0013 #include <QFrame>
0014 
0015 // ----------------------------------------------------------------------------
0016 // KDE Includes
0017 
0018 // ----------------------------------------------------------------------------
0019 // Project Includes
0020 
0021 #include "kmm_widgets_export.h"
0022 
0023 /**
0024   *@author Thomas Baumgart
0025   */
0026 
0027 /**
0028   * This class implements a simple electronic calculator with the
0029   * ability of addition, subtraction, multiplication and division
0030   * and percentage calculation. Memory locations are not available.
0031   *
0032   * The first operand and operation can be loaded from an external
0033   * source to switch from an edit-widget to the calculator widget
0034   * without having the user to re-type the data. See setInitialValues()
0035   * for details.
0036   */
0037 class KMyMoneyCalculatorPrivate;
0038 class KMM_WIDGETS_EXPORT KMyMoneyCalculator : public QFrame
0039 {
0040     Q_OBJECT
0041     Q_DISABLE_COPY(KMyMoneyCalculator)
0042 
0043 public:
0044     explicit KMyMoneyCalculator(QWidget* parent = nullptr);
0045     ~KMyMoneyCalculator();
0046 
0047     /**
0048       * This methods is used to extract the result of the last
0049       * calculation. The fractional part is separated from the
0050       * integral part by the character setup using setComma().
0051       *
0052       * @return QString representing the result of the
0053       *         last operation
0054       */
0055     QString result() const;
0056 
0057     /**
0058       * This method is used to set the character to be used
0059       * as the separator between the integer and fractional part
0060       * of an operand. Upon creation of the object, m_comma is
0061       * set to the current locale setting of KDE's decimalSymbol.
0062       *
0063       * @param ch QChar representing the character to be used
0064       */
0065     void setComma(const QChar ch);
0066 
0067     /**
0068       * This method is used to preset the first operand and start
0069       * execution of an operation. This method is currently used
0070       * by AmountEdit. If @p ev is 0, then no operation will be
0071       * started.
0072       *
0073       * @param value reference to QString representing the operands value
0074       * @param ev    pointer to QKeyEvent representing the operation's key
0075       */
0076     void setInitialValues(const QString& value, QKeyEvent* ev);
0077 
0078 Q_SIGNALS:
0079     /**
0080       * This signal is emitted, when a new result is available
0081       */
0082     void signalResultAvailable();
0083 
0084     /**
0085       * This signal is emitted, when the user pressed the ESC key
0086       */
0087     void signalQuit();
0088 
0089 protected:
0090     void keyPressEvent(QKeyEvent* ev) override;
0091 
0092     /**
0093       * This method is used to transform a double into a QString
0094       * and removing any trailing 0's and decimal separators
0095       *
0096       * @param val reference to double value to be converted
0097       * @return QString object containing the converted value
0098       */
0099     QString normalizeString(const double& val);
0100 
0101 protected Q_SLOTS:
0102     /**
0103       * This method appends the digit represented by the parameter
0104       * to the current operand
0105       *
0106       * @param button integer value of the digit to be added in the
0107       *               range [0..9]
0108       */
0109     void digitClicked(int button);
0110 
0111     /**
0112       * This methods starts the operation contained in the parameter
0113       *
0114       * @param button The Qt::Keycode for the button pressed or clicked
0115       */
0116     void calculationClicked(int button);
0117 
0118     /**
0119       * This method appends a period (comma) to initialize the fractional
0120       * part of an operand. The period is only appended once.
0121       */
0122     void commaClicked();
0123 
0124     /**
0125       * This method reverses the sign of the current operand
0126       */
0127     void plusminusClicked();
0128 
0129     /**
0130       * This method clears the current operand
0131       */
0132     void clearClicked();
0133 
0134     /**
0135       * This method clears all registers
0136       */
0137     void clearAllClicked();
0138 
0139     /**
0140       * This method executes the percent operation
0141       */
0142     void percentClicked();
0143 
0144 private:
0145     KMyMoneyCalculatorPrivate * const d_ptr;
0146     Q_DECLARE_PRIVATE(KMyMoneyCalculator)
0147 };
0148 
0149 #endif