File indexing completed on 2024-04-14 05:42:53

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 class Constants;
0011 class QButtonGroup;
0012 class QLabel;
0013 class KToggleAction;
0014 class KCalcConstMenu;
0015 class KCalcStatusBar;
0016 
0017 /*
0018  * Kcalc consist of a class for the GUI (here), and a few
0019  * implementation classes:
0020  *
0021  * - KNumber: holds a given number, integer or float, it implements
0022  *   all operations for a given KNumber (cos, ln, +, & and such).
0023  * - KCalcToken: class to hold "tokens" such as operations (+,-,×,..),
0024  *   KNumbers, parentheses or functions (ln, cos, ...) to be processed
0025  *   by the engine.
0026  * - KCalcParser: once an input string (such as "5cos(4.6)+1") can be committed
0027  *   for calculation, this class converts it to their respective KCalcToken
0028  *   objects and puts them into a Queue.
0029  * - CalcEngine: is able to process a given KCalToken Queue to produce a final
0030  *   result, based on precedence levels of each operation.
0031  *
0032  * Once the user has finished entering the desired expression to calculate
0033  * (signaling this by clicking "equal"), the string hold by KCalcInputDisplay is
0034  * sent to KCalcParser, the resulting KCalToken Queue is then sent to CalcEngine
0035  * and finally, the result is shown in KCalcDisplay.
0036  */
0037 
0038 #include "kcalc_button.h"
0039 #include "kcalc_const_button.h"
0040 #include "kcalc_core.h"
0041 #include "kcalc_parser.h"
0042 #include "kcalc_token.h"
0043 
0044 #include "ui_colors.h"
0045 #include "ui_constants.h"
0046 #include "ui_fonts.h"
0047 #include "ui_general.h"
0048 #include "ui_kcalc.h"
0049 
0050 #include <QQueue>
0051 #include <array>
0052 #include <kxmlguiwindow.h>
0053 
0054 class General : public QWidget, public Ui::General
0055 {
0056     Q_OBJECT
0057 public:
0058     explicit General(QWidget *parent)
0059         : QWidget(parent)
0060     {
0061         setupUi(this);
0062     }
0063 };
0064 
0065 class Fonts : public QWidget, public Ui::Fonts
0066 {
0067     Q_OBJECT
0068 public:
0069     explicit Fonts(QWidget *parent)
0070         : QWidget(parent)
0071     {
0072         setupUi(this);
0073     }
0074 };
0075 
0076 class Constants : public QWidget, public Ui::Constants
0077 {
0078     Q_OBJECT
0079 public:
0080     explicit Constants(QWidget *parent)
0081         : QWidget(parent)
0082     {
0083         setupUi(this);
0084     }
0085 };
0086 
0087 class Colors : public QWidget, public Ui::Colors
0088 {
0089     Q_OBJECT
0090 public:
0091     explicit Colors(QWidget *parent)
0092         : QWidget(parent)
0093     {
0094         setupUi(this);
0095     }
0096 };
0097 
0098 class KCalculator : public KXmlGuiWindow, private Ui::KCalculator
0099 {
0100     Q_OBJECT
0101 
0102 public:
0103     explicit KCalculator(QWidget *parent = nullptr);
0104     ~KCalculator() override;
0105 
0106 Q_SIGNALS:
0107     void switchMode(ButtonModeFlags, bool);
0108     void switchShowAccels(bool);
0109 
0110 public:
0111     enum UpdateFlag { UPDATE_FROM_CORE = 1, UPDATE_STORE_RESULT = 2, UPDATE_MALFORMED_EXPRESSION = 4, UPDATE_CLEAR = 8 };
0112 
0113     Q_DECLARE_FLAGS(UpdateFlags, UpdateFlag)
0114 
0115 protected:
0116     void resizeEvent(QResizeEvent* event) override;
0117     
0118 private:
0119     bool eventFilter(QObject *o, QEvent *e) override;
0120     bool event(QEvent *e) override;
0121     void updateGeometry();
0122     void setupMainActions();
0123     void setupKeys();
0124     void setupNumberKeys();
0125     void setupRightKeypad();
0126     void setupNumericKeypad();
0127     void setupLogicKeys();
0128     void setupScientificKeys();
0129     void setupStatisticKeys();
0130     void setupConstantsKeys();
0131     void setupMiscKeys();
0132     void keyPressEvent(QKeyEvent *e) override;
0133     void keyReleaseEvent(QKeyEvent *e) override;
0134     void setPrecision();
0135     void setAngle();
0136     void setBase();
0137     
0138     void setBaseFont(const QFont &font);
0139     const QFont &baseFont() const;
0140     bool isMinimumSize();
0141     void forceResizeEvent();
0142     void setLeftPadLayoutActive(bool active);
0143     void setBitsetLayoutActive(bool active);
0144 
0145     void updateDisplay(UpdateFlags flags);
0146     void insertToInputDisplay(KCalcToken::TokenCode token);
0147     void insertToInputDisplay(const QString &token);
0148     KCalcStatusBar *statusBar();
0149 
0150     // button sets
0151     void showMemButtons(bool toggled);
0152     void showStatButtons(bool toggled);
0153     void showScienceButtons(bool toggled);
0154     void showLogicButtons(bool toggled);
0155 
0156     KCalcConstMenu *createConstantsMenu();
0157 
0158 protected Q_SLOTS:
0159     void changeButtonNames();
0160     void updateSettings();
0161     void setColors();
0162     void setFonts();
0163     void updateResultDisplay();
0164     void showSettings();
0165 
0166     // Mode
0167     void slotSetSimpleMode();
0168     void slotSetScienceMode();
0169     void slotSetStatisticMode();
0170     void slotSetNumeralMode();
0171 
0172     void slotHistoryshow(bool toggled);
0173     void slotConstantsShow(bool toggled);
0174     void slotBitsetshow(bool toggled);
0175     void slotAngleSelected(QAbstractButton *button, bool checked);
0176     void slotBaseSelected(QAbstractButton *button);
0177     void slotNumberclicked(QAbstractButton *button);
0178     void slotEEclicked();
0179     void slotShifttoggled(bool myboolean);
0180     void slotMemRecallclicked();
0181     void slotMemStoreclicked();
0182     void slotSinclicked();
0183     void slotPlusMinusclicked();
0184     void slotMemPlusMinusclicked();
0185     void slotCosclicked();
0186     void slotReciclicked();
0187     void slotTanclicked();
0188     void slotFactorialclicked();
0189     void slotLogclicked();
0190     void slotSquareclicked();
0191     void slotCubeclicked();
0192     void slotLnclicked();
0193     void slotPowerclicked();
0194     void slotMemClearclicked();
0195     void slotClearclicked();
0196     void slotAllClearclicked();
0197     void slotParenOpenclicked();
0198     void slotParenCloseclicked();
0199     void slotANDclicked();
0200     void slotMultiplicationclicked();
0201     void slotDivisionclicked();
0202     void slotORclicked();
0203     void slotXORclicked();
0204     void slotPlusclicked();
0205     void slotMinusclicked();
0206     void slotLeftShiftclicked();
0207     void slotRightShiftclicked();
0208     void slotPeriodclicked();
0209     void slotEqualclicked();
0210     void slotPercentclicked();
0211     void slotNegateclicked();
0212     void slotModclicked();
0213     void slotStatNumclicked();
0214     void slotStatMeanclicked();
0215     void slotStatStdDevclicked();
0216     void slotStatMedianclicked();
0217     void slotStatDataInputclicked();
0218     void slotStatClearDataclicked();
0219     void slotHyptoggled(bool flag);
0220     void slotConstclicked(int);
0221     void slotBackspaceclicked();
0222 
0223     void slotConstantToDisplay(const science_constant &const_chosen);
0224     void slotChooseScientificConst0(const science_constant &);
0225     void slotChooseScientificConst1(const science_constant &);
0226     void slotChooseScientificConst2(const science_constant &);
0227     void slotChooseScientificConst3(const science_constant &);
0228     void slotChooseScientificConst4(const science_constant &);
0229     void slotChooseScientificConst5(const science_constant &);
0230 
0231     void slotBitsetChanged(quint64);
0232     void slotUpdateBitset(const KNumber &);
0233 
0234     void slotBaseModeAmountChanged(const KNumber &number);
0235 
0236     void slotPaste();
0237 
0238 private:
0239     enum StatusField { ShiftField = 0, BaseField, AngleField, MemField };
0240 
0241     enum AngleMode { DegMode = 0, RadMode, GradMode };
0242 
0243     enum BaseMode { BinMode = 2, OctMode = 8, DecMode = 10, HexMode = 16 };
0244 
0245 private:
0246     int commit_Input_();
0247     void commit_Result_(bool toHistory = true);
0248     int load_Constants_(const QString &filePath);
0249 
0250     bool shift_mode_ = false;
0251     bool hyp_mode_ = false;
0252     bool update_history_window_ = false;
0253     QQueue<KCalcToken> token_Queue_;
0254     KNumber memory_num_;
0255 
0256     int angle_mode_; // angle modes for trigonometric values
0257     int base_mode_;
0258 
0259     bool parsing_failure_, calculation_failure_;
0260     int input_error_index_;
0261     int calculation_error_token_index_;
0262     void inline handle_Parsing_Error_();
0263     void inline handle_Calculation_Error_();
0264 
0265     KCalcConstMenu *constants_menu_ = nullptr;
0266 
0267     Constants *constants_ = nullptr; // this is the dialog for configuring const buttons
0268 
0269     QButtonGroup *angle_choose_group_ = nullptr;
0270     QButtonGroup *base_choose_group_ = nullptr;
0271     // num_button_group_: 0-9 = digits, 0xA-0xF = hex-keys
0272     QButtonGroup *num_button_group_ = nullptr;
0273 
0274     QList<QAbstractButton *> logic_buttons_;
0275     QList<QAbstractButton *> scientific_buttons_;
0276     QList<QAbstractButton *> stat_buttons_;
0277     QList<QAbstractButton *> const_buttons_;
0278 
0279     std::array<QLabel *, 4> base_conversion_labels_;
0280 
0281     KToggleAction *action_history_show_ = nullptr;
0282     KToggleAction *action_bitset_show_ = nullptr;
0283     KToggleAction *action_constants_show_ = nullptr;
0284 
0285     KToggleAction *action_mode_simple_ = nullptr;
0286     KToggleAction *action_mode_science_ = nullptr;
0287     KToggleAction *action_mode_statistic_ = nullptr;
0288     KToggleAction *action_mode_numeral_ = nullptr;
0289 
0290     QList<QAbstractButton *> function_button_list_;
0291     QList<QAbstractButton *> stat_button_list_;
0292     QList<QAbstractButton *> mem_button_list_;
0293     QList<QAbstractButton *> operation_button_list_;
0294 
0295     QFont baseFont_;
0296     bool is_still_in_launch_ = true; // necessary for startup at minimum size
0297 
0298     CalcEngine core;
0299     KCalcParser parser;
0300 };
0301 
0302 Q_DECLARE_OPERATORS_FOR_FLAGS(KCalculator::UpdateFlags)
0303