File indexing completed on 2024-06-16 05:22:22

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 
0085 Q_SIGNALS:
0086     void clicked();
0087     void changedText(const QString &);
0088     void changedAmount(const KNumber &);
0089 
0090 protected:
0091     void mousePressEvent(QMouseEvent *) override;
0092     void paintEvent(QPaintEvent *p) override;
0093     void resizeEvent(QResizeEvent* event) override;
0094 
0095 private:
0096     bool changeSign();
0097     void invertColors();
0098     void initStyleOption(QStyleOptionFrame *option) const override;
0099     void updateFont();
0100 
0101 private Q_SLOTS:
0102     void slotSelectionTimedOut();
0103     void slotDisplaySelected();
0104     void slotHistoryBack();
0105     void slotHistoryForward();
0106 
0107 private:
0108     QString text_;
0109     bool beep_;
0110     bool groupdigits_;
0111     bool twoscomplement_;
0112     int binaryGrouping_;
0113     int octalGrouping_;
0114     int hexadecimalGrouping_;
0115     int button_;
0116     bool lit_;
0117     NumBase num_base_;
0118 
0119     int precision_;
0120     int fixed_precision_; // "-1" = no fixed_precision
0121 
0122     KNumber display_amount_;
0123 
0124     QFont baseFont_;
0125 
0126     QList<KNumber> history_list_;
0127     int history_index_;
0128 
0129     // only used for input of new numbers
0130     bool eestate_;
0131     bool period_;
0132     bool neg_sign_;
0133     QString str_int_;
0134     QString str_int_exp_;
0135     QString str_status_[NUM_STATUS_TEXT];
0136 
0137     QTimer *const selection_timer_;
0138 };
0139