1 /*
2     SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com>
3     SPDX-FileCopyrightText: 1996-2000 Bernd Johannes Wuebben <wuebben@kde.org>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #pragma once
9 
10 #include "knumber.h"
11 #include <QFrame>
12 #include <QVector>
13 
14 class CalcEngine;
15 class QTimer;
16 class QStyleOptionFrame;
17 
18 #define NUM_STATUS_TEXT 4
19 
20 /*
21   This class provides a pocket calculator display.  The display has
22   implicitely two major modes: One is for editing and one is purely
23   for displaying.
24 
25   When one uses "setAmount", the given amount is displayed, and the
26   amount which was possibly typed in before is lost. At the same time
27   this new value can not be modified.
28 
29   On the other hand, "addNewChar" adds a new digit to the amount that
30   is being typed in. If "setAmount" was used before, the display is
31   cleared and a new input starts.
32 
33   TODO: Check overflows, number of digits and such...
34 */
35 
36 enum NumBase { NB_BINARY = 2, NB_OCTAL = 8, NB_DECIMAL = 10, NB_HEX = 16 };
37 
38 class KCalcDisplay : public QFrame
39 {
40     Q_OBJECT
41 
42 public:
43     explicit KCalcDisplay(QWidget *parent = nullptr);
44     ~KCalcDisplay() override;
45 
46     enum Event {
47         EventReset, // resets display
48         EventClear, // if no error reset display
49         EventError,
50         EventChangeSign
51     };
52 
53     bool sendEvent(Event event);
54     void deleteLastDigit();
55     const KNumber &getAmount() const;
56     void newCharacter(const QChar new_char);
57     bool setAmount(const KNumber &new_amount);
58     int setBase(NumBase new_base);
59     void setBeep(bool flag);
60     void setGroupDigits(bool flag);
61     void setTwosComplement(bool flag);
62     void setBinaryGrouping(int digits);
63     void setOctalGrouping(int digits);
64     void setHexadecimalGrouping(int digits);
65     void setFixedPrecision(int precision);
66     void setPrecision(int precision);
67     void setText(const QString &string);
68     QString formatDecimalNumber(QString string);
69     QString groupDigits(const QString &displayString, int numDigits);
70     QString text() const;
71     void updateDisplay();
72     void setStatusText(int i, const QString &text);
73     QSize sizeHint() const override;
74 
75     void changeSettings();
76     void enterDigit(int data);
77     void updateFromCore(const CalcEngine &core, bool store_result_in_history = false);
78 
79 public Q_SLOTS:
80     void slotCut();
81     void slotCopy();
82     void slotPaste(bool bClipboard = true);
83 
84 Q_SIGNALS:
85     void clicked();
86     void changedText(const QString &);
87     void changedAmount(const KNumber &);
88 
89 protected:
90     void mousePressEvent(QMouseEvent *) override;
91     void paintEvent(QPaintEvent *p) override;
92 
93 private:
94     bool changeSign();
95     void invertColors();
96     void initStyleOption(QStyleOptionFrame *option) const;
97 
98 private Q_SLOTS:
99     void slotSelectionTimedOut();
100     void slotDisplaySelected();
101     void slotHistoryBack();
102     void slotHistoryForward();
103 
104 private:
105     QString text_;
106     bool beep_;
107     bool groupdigits_;
108     bool twoscomplement_;
109     int binaryGrouping_;
110     int octalGrouping_;
111     int hexadecimalGrouping_;
112     int button_;
113     bool lit_;
114     NumBase num_base_;
115 
116     int precision_;
117     int fixed_precision_; // "-1" = no fixed_precision
118 
119     KNumber display_amount_;
120 
121     QVector<KNumber> history_list_;
122     int history_index_;
123 
124     // only used for input of new numbers
125     bool eestate_;
126     bool period_;
127     bool neg_sign_;
128     QString str_int_;
129     QString str_int_exp_;
130     QString str_status_[NUM_STATUS_TEXT];
131 
132     QTimer *const selection_timer_;
133 };
134 
135