File indexing completed on 2024-05-19 05:51:35

0001 /*
0002  * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com>
0003  * SPDX-FileCopyrightText: 2021-2022 Rohan Asokan <rohan.asokan@Students.iiit.ac.in>
0004  *
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QObject>
0011 
0012 class QalculateEngine;
0013 
0014 class InputManager : public QObject
0015 {
0016     Q_OBJECT
0017     Q_PROPERTY(QString expression READ expression NOTIFY expressionChanged)
0018     Q_PROPERTY(QString result READ result NOTIFY resultChanged)
0019     Q_PROPERTY(QString binaryResult READ binaryResult NOTIFY binaryResultChanged)
0020     Q_PROPERTY(QString hexResult READ hexResult NOTIFY hexResultChanged)
0021     Q_PROPERTY(bool moveFromResult READ moveFromResult NOTIFY resultChanged)
0022     Q_PROPERTY(int cursorPosition READ getCursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
0023     Q_PROPERTY(bool binaryMode READ binaryMode WRITE setBinaryMode)
0024     Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged)
0025     Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged)
0026     Q_PROPERTY(int historyIndex READ historyIndex WRITE setHistoryIndex)
0027 
0028 public:
0029     static InputManager *inst();
0030     const QString &expression() const;
0031     const QString &result() const;
0032     const QString &binaryResult() const;
0033     const QString &hexResult() const;
0034     bool moveFromResult() const;
0035     int getCursorPosition() const;
0036     void setCursorPosition(int position);
0037     Q_INVOKABLE int idealCursorPosition(int position, int arrow = 0) const;
0038     Q_INVOKABLE void append(const QString &subexpression);
0039     Q_INVOKABLE void backspace();
0040     Q_INVOKABLE void equal();
0041     Q_INVOKABLE void clear(bool save = true);
0042     Q_INVOKABLE void setHistoryIndex(const int &index);
0043     int historyIndex() const;
0044     Q_INVOKABLE void fromHistory(const QString &result);
0045     void store();
0046     Q_INVOKABLE void undo();
0047     Q_INVOKABLE void redo();
0048     bool canUndo();
0049     bool canRedo();
0050     void setBinaryMode(bool active);
0051     bool binaryMode();
0052     QString formatNumbers(const QString &text);
0053     void replaceWithSuperscript(QString &text);
0054     void addNumberSeparators(QString &number);
0055     void calculate(bool exact = false, const int minExp = -1);
0056 
0057 Q_SIGNALS:
0058     void expressionChanged();
0059     void resultChanged();
0060     void binaryResultChanged();
0061     void hexResultChanged();
0062     void cursorPositionChanged();
0063     void canUndoChanged();
0064     void canRedoChanged();
0065 
0066 private:
0067     InputManager();
0068     bool m_moveFromResult = false; // clear expression on none operator input
0069     int m_inputPosition;
0070     QString m_input;
0071     QString m_output;
0072     QString m_expression;
0073     QString m_result;
0074     QString m_binaryResult;
0075     QString m_hexResult;
0076     bool m_isBinaryMode = false; // Changes the parser based on this variable
0077     QString m_groupSeparator;
0078     QString m_decimalPoint;
0079     std::vector<QString> m_undoStack;
0080     size_t m_undoPos = 0;
0081     QalculateEngine *m_engine;
0082     bool m_isApproximate;
0083     int m_historyIndex;
0084 };