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

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 basically consist of a class for the GUI (here), a class for
0019   the display (dlabel.h), and one for the mathematics core
0020   (kcalc_core.h).
0021 
0022   When for example '+' is pressed, one sends the contents of the
0023   Display and the '+' to the core via "core.Plus(DISPLAY_AMOUNT)".
0024   This only updates the core. To bring the changes to the display,
0025   use afterwards "UpdateDisplay(true)".
0026 
0027   "UpdateDisplay(true)" means that the amount to be displayed should
0028   be taken from the core (get the result of some operation that was
0029   performed), "UpdateDisplay(false)" has already the information, what
0030   to be display (e.g. user is typing in a number).  Note that in the
0031   last case the core does not know the number typed in until some
0032   operation button is pressed, e.g. "core.Plus(display_number)".
0033  */
0034 
0035 #include "kcalc_button.h"
0036 #include "kcalc_const_button.h"
0037 #include "kcalc_core.h"
0038 
0039 #include "ui_colors.h"
0040 #include "ui_constants.h"
0041 #include "ui_fonts.h"
0042 #include "ui_general.h"
0043 #include "ui_kcalc.h"
0044 
0045 #include <array>
0046 #include <kxmlguiwindow.h>
0047 
0048 class General : public QWidget, public Ui::General
0049 {
0050     Q_OBJECT
0051 public:
0052     explicit General(QWidget *parent)
0053         : QWidget(parent)
0054     {
0055         setupUi(this);
0056     }
0057 };
0058 
0059 class Fonts : public QWidget, public Ui::Fonts
0060 {
0061     Q_OBJECT
0062 public:
0063     explicit Fonts(QWidget *parent)
0064         : QWidget(parent)
0065     {
0066         setupUi(this);
0067     }
0068 };
0069 
0070 class Constants : public QWidget, public Ui::Constants
0071 {
0072     Q_OBJECT
0073 public:
0074     explicit Constants(QWidget *parent)
0075         : QWidget(parent)
0076     {
0077         setupUi(this);
0078     }
0079 };
0080 
0081 class Colors : public QWidget, public Ui::Colors
0082 {
0083     Q_OBJECT
0084 public:
0085     explicit Colors(QWidget *parent)
0086         : QWidget(parent)
0087     {
0088         setupUi(this);
0089     }
0090 };
0091 
0092 class KCalculator : public KXmlGuiWindow, private Ui::KCalculator
0093 {
0094     Q_OBJECT
0095 
0096 public:
0097     explicit KCalculator(QWidget *parent = nullptr);
0098     ~KCalculator() override;
0099 
0100 Q_SIGNALS:
0101     void switchMode(ButtonModeFlags, bool);
0102     void switchShowAccels(bool);
0103 
0104 public:
0105     enum UpdateFlag { UPDATE_FROM_CORE = 1, UPDATE_STORE_RESULT = 2 };
0106 
0107     Q_DECLARE_FLAGS(UpdateFlags, UpdateFlag)
0108 
0109 protected:
0110     void resizeEvent(QResizeEvent* event) override;
0111     
0112 private:
0113     bool eventFilter(QObject *o, QEvent *e) override;
0114     bool event(QEvent *e) override;
0115     void updateGeometry();
0116     void setupMainActions();
0117     void setupKeys();
0118     void setupNumberKeys();
0119     void setupRightKeypad();
0120     void setupNumericKeypad();
0121     void setupLogicKeys();
0122     void setupScientificKeys();
0123     void setupStatisticKeys();
0124     void setupConstantsKeys();
0125     void setupMiscKeys();
0126     void keyPressEvent(QKeyEvent *e) override;
0127     void keyReleaseEvent(QKeyEvent *e) override;
0128     void setPrecision();
0129     void setAngle();
0130     void setBase();
0131     
0132     void setBaseFont(const QFont &font);
0133     const QFont &baseFont() const;
0134     bool isMinimumSize();
0135     void forceResizeEvent();
0136     void setLeftPadLayoutActive(bool active);
0137     void setBitsetLayoutActive(bool active);
0138 
0139     void updateDisplay(UpdateFlags flags);
0140     void updateHistoryWithFunction(CalcEngine::Operation);
0141     KCalcStatusBar *statusBar();
0142 
0143     // button sets
0144     void showMemButtons(bool toggled);
0145     void showStatButtons(bool toggled);
0146     void showScienceButtons(bool toggled);
0147     void showLogicButtons(bool toggled);
0148 
0149     KCalcConstMenu *createConstantsMenu();
0150 
0151 protected Q_SLOTS:
0152     void changeButtonNames();
0153     void updateSettings();
0154     void setColors();
0155     void setFonts();
0156     void EnterEqual(CalcEngine::Repeat allow_repeat = CalcEngine::REPEAT_ALLOW);
0157     void showSettings();
0158 
0159     // Mode
0160     void slotSetSimpleMode();
0161     void slotSetScienceMode();
0162     void slotSetStatisticMode();
0163     void slotSetNumeralMode();
0164 
0165     void slotHistoryshow(bool toggled);
0166     void slotConstantsShow(bool toggled);
0167     void slotBitsetshow(bool toggled);
0168     void slotAngleSelected(QAbstractButton *button);
0169     void slotBaseSelected(QAbstractButton *button);
0170     void slotNumberclicked(QAbstractButton *button);
0171     void slotEEclicked();
0172     void slotShifttoggled(bool myboolean);
0173     void slotMemRecallclicked();
0174     void slotMemStoreclicked();
0175     void slotSinclicked();
0176     void slotPlusMinusclicked();
0177     void slotMemPlusMinusclicked();
0178     void slotCosclicked();
0179     void slotReciclicked();
0180     void slotTanclicked();
0181     void slotFactorialclicked();
0182     void slotLogclicked();
0183     void slotSquareclicked();
0184     void slotCubeclicked();
0185     void slotLnclicked();
0186     void slotPowerclicked();
0187     void slotMemClearclicked();
0188     void slotClearclicked();
0189     void slotAllClearclicked();
0190     void slotParenOpenclicked();
0191     void slotParenCloseclicked();
0192     void slotANDclicked();
0193     void slotMultiplicationclicked();
0194     void slotDivisionclicked();
0195     void slotORclicked();
0196     void slotXORclicked();
0197     void slotPlusclicked();
0198     void slotMinusclicked();
0199     void slotLeftShiftclicked();
0200     void slotRightShiftclicked();
0201     void slotPeriodclicked();
0202     void slotEqualclicked();
0203     void slotPercentclicked();
0204     void slotNegateclicked();
0205     void slotModclicked();
0206     void slotStatNumclicked();
0207     void slotStatMeanclicked();
0208     void slotStatStdDevclicked();
0209     void slotStatMedianclicked();
0210     void slotStatDataInputclicked();
0211     void slotStatClearDataclicked();
0212     void slotHyptoggled(bool flag);
0213     void slotConstclicked(int);
0214     void slotBackspaceclicked();
0215 
0216     void slotConstantToDisplay(const science_constant &const_chosen);
0217     void slotChooseScientificConst0(const science_constant &);
0218     void slotChooseScientificConst1(const science_constant &);
0219     void slotChooseScientificConst2(const science_constant &);
0220     void slotChooseScientificConst3(const science_constant &);
0221     void slotChooseScientificConst4(const science_constant &);
0222     void slotChooseScientificConst5(const science_constant &);
0223 
0224     void slotBitsetChanged(quint64);
0225     void slotUpdateBitset(const KNumber &);
0226 
0227     void slotBaseModeAmountChanged(const KNumber &number);
0228 
0229     void slotPaste();
0230 
0231 private:
0232     enum StatusField { ShiftField = 0, BaseField, AngleField, MemField };
0233 
0234     enum AngleMode { DegMode = 0, RadMode, GradMode };
0235 
0236     enum BaseMode { BinMode = 2, OctMode = 8, DecMode = 10, HexMode = 16 };
0237 
0238 private:
0239     bool shift_mode_ = false;
0240     bool hyp_mode_ = false;
0241     bool update_history_window_ = false;
0242     KNumber memory_num_;
0243 
0244     int angle_mode_; // angle modes for trigonometric values
0245 
0246     KCalcConstMenu *constants_menu_ = nullptr;
0247 
0248     Constants *constants_ = nullptr; // this is the dialog for configuring const buttons
0249 
0250     QButtonGroup *angle_choose_group_ = nullptr;
0251     QButtonGroup *base_choose_group_ = nullptr;
0252     // num_button_group_: 0-9 = digits, 0xA-0xF = hex-keys
0253     QButtonGroup *num_button_group_ = nullptr;
0254 
0255     QList<QAbstractButton *> logic_buttons_;
0256     QList<QAbstractButton *> scientific_buttons_;
0257     QList<QAbstractButton *> stat_buttons_;
0258     QList<QAbstractButton *> const_buttons_;
0259 
0260     std::array<QLabel *, 4> base_conversion_labels_;
0261 
0262     KToggleAction *action_history_show_ = nullptr;
0263     KToggleAction *action_bitset_show_ = nullptr;
0264     KToggleAction *action_constants_show_ = nullptr;
0265 
0266     KToggleAction *action_mode_simple_ = nullptr;
0267     KToggleAction *action_mode_science_ = nullptr;
0268     KToggleAction *action_mode_statistic_ = nullptr;
0269     KToggleAction *action_mode_numeral_ = nullptr;
0270 
0271     QList<QAbstractButton *> function_button_list_;
0272     QList<QAbstractButton *> stat_button_list_;
0273     QList<QAbstractButton *> mem_button_list_;
0274     QList<QAbstractButton *> operation_button_list_;
0275 
0276     QFont baseFont_;
0277     bool is_still_in_launch_ = true; // necessary for startup at minimum size
0278 
0279     CalcEngine core;
0280 };
0281 
0282 Q_DECLARE_OPERATORS_FOR_FLAGS(KCalculator::UpdateFlags)
0283