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 <QList>
13
14class CalcEngine;
15class QTimer;
16class 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
36enum NumBase { NB_BINARY = 2, NB_OCTAL = 8, NB_DECIMAL = 10, NB_HEX = 16 };
37
38class KCalcDisplay : public QFrame
39{
40 Q_OBJECT
41
42public:
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 void setFont(const QFont &font);
69 const QFont &baseFont() const;
70 QString formatDecimalNumber(QString string);
71 QString groupDigits(const QString &displayString, int numDigits);
72 QString text() const;
73 void updateDisplay();
74 void setStatusText(int i, const QString &text);
75 QSize sizeHint() const override;
76
77 void changeSettings();
78 void enterDigit(int data);
79 void updateFromCore(const CalcEngine &core, bool store_result_in_history = false);
80
81public Q_SLOTS:
82 void slotCut();
83 void slotCopy();
84 void slotPaste(bool bClipboard = true);
85
86Q_SIGNALS:
87 void clicked();
88 void changedText(const QString &);
89 void changedAmount(const KNumber &);
90
91protected:
92 void mousePressEvent(QMouseEvent *) override;
93 void paintEvent(QPaintEvent *p) override;
94 void resizeEvent(QResizeEvent* event) override;
95
96private:
97 bool changeSign();
98 void invertColors();
99 void initStyleOption(QStyleOptionFrame *option) const override;
100 void updateFont();
101
102private Q_SLOTS:
103 void slotSelectionTimedOut();
104 void slotDisplaySelected();
105 void slotHistoryBack();
106 void slotHistoryForward();
107
108private:
109 QString text_;
110 bool beep_;
111 bool groupdigits_;
112 bool twoscomplement_;
113 int binaryGrouping_;
114 int octalGrouping_;
115 int hexadecimalGrouping_;
116 int button_;
117 bool lit_;
118 NumBase num_base_;
119
120 int precision_;
121 int fixed_precision_; // "-1" = no fixed_precision
122
123 KNumber display_amount_;
124
125 QFont baseFont_;
126
127 QList<KNumber> history_list_;
128 int history_index_;
129
130 // only used for input of new numbers
131 bool eestate_;
132 bool period_;
133 bool neg_sign_;
134 QString str_int_;
135 QString str_int_exp_;
136 QString str_status_[NUM_STATUS_TEXT];
137
138 QTimer *const selection_timer_;
139};
140
141

source code of kcalc/kcalcdisplay.h