File indexing completed on 2024-04-28 09:45:27

0001 /*
0002     SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com>
0003     SPDX-FileCopyrightText: 1996-2000 Bernd Johannes Wuebben <wuebben@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "knumber.h"
0011 #include <QFrame>
0012 #include <QList>
0013 
0014 class CalcEngine;
0015 class QTimer;
0016 class QStyleOptionFrame;
0017 
0018 #define NUM_STATUS_TEXT 4
0019 
0020 /*
0021   This class provides a pocket calculator display.  The display has
0022   implicitely two major modes: One is for editing and one is purely
0023   for displaying.
0024 
0025   When one uses "setAmount", the given amount is displayed, and the
0026   amount which was possibly typed in before is lost. At the same time
0027   this new value can not be modified.
0028 
0029   On the other hand, "addNewChar" adds a new digit to the amount that
0030   is being typed in. If "setAmount" was used before, the display is
0031   cleared and a new input starts.
0032 
0033   TODO: Check overflows, number of digits and such...
0034 */
0035 
0036 enum NumBase { NB_BINARY = 2, NB_OCTAL = 8, NB_DECIMAL = 10, NB_HEX = 16 };
0037 
0038 class KCalcDisplay : public QFrame
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     explicit KCalcDisplay(QWidget *parent = nullptr);
0044     ~KCalcDisplay() override;
0045 
0046     enum Event {
0047         EventReset, // resets display
0048         EventClear, // if no error reset display
0049         EventError,
0050         EventChangeSign
0051     };
0052 
0053     bool sendEvent(Event event);
0054     void deleteLastDigit();
0055     const KNumber &getAmount() const;
0056     void newCharacter(const QChar new_char);
0057     bool setAmount(const KNumber &new_amount);
0058     int setBase(NumBase new_base);
0059     void setBeep(bool flag);
0060     void setGroupDigits(bool flag);
0061     void setTwosComplement(bool flag);
0062     void setBinaryGrouping(int digits);
0063     void setOctalGrouping(int digits);
0064     void setHexadecimalGrouping(int digits);
0065     void setFixedPrecision(int precision);
0066     void setPrecision(int precision);
0067     void setText(const QString &string);
0068     void setFont(const QFont &font);
0069     const QFont &baseFont() const;
0070     QString formatDecimalNumber(QString string);
0071     QString groupDigits(const QString &displayString, int numDigits);
0072     QString text() const;
0073     void updateDisplay();
0074     void setStatusText(int i, const QString &text);
0075     QSize sizeHint() const override;
0076 
0077     void changeSettings();
0078     void enterDigit(int data);
0079     void updateFromCore(const CalcEngine &core, bool store_result_in_history = false);
0080 
0081 public Q_SLOTS:
0082     void slotCut();
0083     void slotCopy();
0084     void slotPaste(bool bClipboard = true);
0085 
0086 Q_SIGNALS:
0087     void clicked();
0088     void changedText(const QString &);
0089     void changedAmount(const KNumber &);
0090 
0091 protected:
0092     void mousePressEvent(QMouseEvent *) override;
0093     void paintEvent(QPaintEvent *p) override;
0094     void resizeEvent(QResizeEvent* event) override;
0095 
0096 private:
0097     bool changeSign();
0098     void invertColors();
0099     void initStyleOption(QStyleOptionFrame *option) const override;
0100     void updateFont();
0101 
0102 private Q_SLOTS:
0103     void slotSelectionTimedOut();
0104     void slotDisplaySelected();
0105     void slotHistoryBack();
0106     void slotHistoryForward();
0107 
0108 private:
0109     QString text_;
0110     bool beep_;
0111     bool groupdigits_;
0112     bool twoscomplement_;
0113     int binaryGrouping_;
0114     int octalGrouping_;
0115     int hexadecimalGrouping_;
0116     int button_;
0117     bool lit_;
0118     NumBase num_base_;
0119 
0120     int precision_;
0121     int fixed_precision_; // "-1" = no fixed_precision
0122 
0123     KNumber display_amount_;
0124 
0125     QFont baseFont_;
0126 
0127     QList<KNumber> history_list_;
0128     int history_index_;
0129 
0130     // only used for input of new numbers
0131     bool eestate_;
0132     bool period_;
0133     bool neg_sign_;
0134     QString str_int_;
0135     QString str_int_exp_;
0136     QString str_status_[NUM_STATUS_TEXT];
0137 
0138     QTimer *const selection_timer_;
0139 };
0140