File indexing completed on 2024-05-05 17:18:59

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGCALCULATOREDIT_H
0007 #define SKGCALCULATOREDIT_H
0008 /** @file
0009  * A QLineEdit with calculator included.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 
0014 #include "skgbasegui_export.h"
0015 #include <qlineedit.h>
0016 /**
0017  * This file is a QLineEdit with calculator included
0018  */
0019 class SKGBASEGUI_EXPORT SKGCalculatorEdit : public QLineEdit
0020 {
0021     Q_OBJECT
0022     /**
0023      * Value of the calculator editor
0024      */
0025     Q_PROPERTY(double value READ value WRITE setValue USER true NOTIFY modified)
0026 
0027     /**
0028      * Mode of the calculator editor
0029      */
0030     Q_PROPERTY(SKGCalculatorEdit::Mode mode READ mode WRITE setMode NOTIFY modified)
0031 
0032     /**
0033      * Sign of the calculator editor
0034      */
0035     Q_PROPERTY(double sign READ sign NOTIFY modified)
0036 
0037     /**
0038      * To know if the text is valid
0039      */
0040     Q_PROPERTY(bool valid READ valid NOTIFY modified)
0041 
0042 public:
0043     /**
0044      * This enumerate defines type of calculator
0045      */
0046     enum Mode {CALCULATOR, /**< simple calculator */
0047                EXPRESSION /**< expression is evaluated */
0048               };
0049     /**
0050      * Mode of the calculator editor
0051      */
0052     Q_ENUM(Mode)
0053 
0054     /**
0055      * Default Constructor
0056      * @param iParent the parent
0057      */
0058     explicit SKGCalculatorEdit(QWidget* iParent);
0059 
0060     /**
0061      * Default Destructor
0062      */
0063     ~SKGCalculatorEdit() override;
0064 
0065     /**
0066      * Get the mode
0067      * @return the mode
0068      */
0069     virtual Mode mode() const;
0070 
0071 
0072     /**
0073      * Set the mode
0074      * @param iMode the mode
0075      */
0076     virtual void setMode(Mode iMode);
0077 
0078     /**
0079      * Get the value
0080      * @return the value
0081      */
0082     virtual double value();
0083 
0084     /**
0085      * Set the value
0086      * @param iValue the value
0087      */
0088     virtual void setValue(double iValue);
0089 
0090     /**
0091      * Set the text
0092      * @param iText the text
0093      */
0094     virtual void setText(const QString& iText);
0095 
0096     /**
0097      * Get the sign of the value.
0098      * 1 if the value is write like "+5"
0099      * -1 if the value is write like "-5"
0100      * 0 if the value is write like "5"
0101      * @return the sign
0102      */
0103     virtual int sign() const;
0104 
0105     /**
0106      * To know if the text is a valid value
0107      * @return validation
0108      */
0109     virtual bool valid();
0110 
0111     /**
0112      * To formula
0113      * @return the formula
0114      */
0115     virtual QString formula();
0116 
0117     /**
0118      * Add a parameter
0119      * @param iParameter the parameter name
0120      * @param iValue the value
0121      */
0122     virtual void addParameterValue(const QString& iParameter, double iValue);
0123 
0124 Q_SIGNALS:
0125     /**
0126      * This signal is launched when the object is modified
0127      */
0128     void modified();
0129 
0130 protected:
0131     /**
0132      * This event handler, for event event, can be reimplemented in a subclass to receive key press events for the widget.
0133      * @param iEvent the event
0134      */
0135     void keyPressEvent(QKeyEvent* iEvent) override;
0136 
0137     /**
0138      * This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) for the widget. The events is passed in the event parameter.
0139      * @param iEvent the event
0140      */
0141     void focusOutEvent(QFocusEvent* iEvent) override;
0142 
0143 private:
0144     Q_DISABLE_COPY(SKGCalculatorEdit)
0145 
0146     void keyPressEvent(int key);
0147     double getEvaluatedValue(bool& iOk);
0148     double m_lastValue;
0149     int m_lastOperator;
0150     Mode m_currentMode;
0151 
0152     QColor m_fontColor;
0153     QMap<QString, double> m_parameters;
0154     QString m_formula;
0155 };
0156 
0157 #endif  // SKGCALCULATOREDIT_H