From de568f87fd709f13d08a35448f2fd99badfb2ef7 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 22 Sep 2022 16:54:46 +0200 Subject: [PATCH] Calculator: Add a Rounding menu This menu has three actions. Each one of them enable a different level of rounding: to 0, 2 or 4 digits. --- .../Calculator/CalculatorWidget.cpp | 6 ++++++ .../Calculator/CalculatorWidget.h | 2 ++ Userland/Applications/Calculator/Keypad.cpp | 9 ++++++--- Userland/Applications/Calculator/Keypad.h | 4 ++++ Userland/Applications/Calculator/main.cpp | 19 +++++++++++++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index cfe111fe387..a2f1c53666c 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -218,3 +218,9 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event) update_display(); } + +void CalculatorWidget::set_rounding_length(unsigned rounding_threshold) +{ + m_keypad.set_rounding_length(rounding_threshold); + update_display(); +} diff --git a/Userland/Applications/Calculator/CalculatorWidget.h b/Userland/Applications/Calculator/CalculatorWidget.h index 07845cd3df8..6415af4277d 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.h +++ b/Userland/Applications/Calculator/CalculatorWidget.h @@ -21,6 +21,8 @@ public: String get_entry(); void set_entry(Crypto::BigFraction); + void set_rounding_length(unsigned); + private: CalculatorWidget(); void add_operation_button(GUI::Button&, Calculator::Operation); diff --git a/Userland/Applications/Calculator/Keypad.cpp b/Userland/Applications/Calculator/Keypad.cpp index b94fd79e2d8..e1624d7a0da 100644 --- a/Userland/Applications/Calculator/Keypad.cpp +++ b/Userland/Applications/Calculator/Keypad.cpp @@ -113,10 +113,8 @@ void Keypad::set_to_0() String Keypad::to_string() const { - // TODO: Implement custom rounding length in the calculator. - constexpr auto maximum_precision = 6; if (m_state == State::External) - return m_internal_value.to_string(maximum_precision); + return m_internal_value.to_string(m_displayed_fraction_length); StringBuilder builder; @@ -136,3 +134,8 @@ String Keypad::to_string() const return builder.to_string(); } + +void Keypad::set_rounding_length(unsigned rounding_threshold) +{ + m_displayed_fraction_length = rounding_threshold; +} diff --git a/Userland/Applications/Calculator/Keypad.h b/Userland/Applications/Calculator/Keypad.h index 0410e31b510..b71b9214da1 100644 --- a/Userland/Applications/Calculator/Keypad.h +++ b/Userland/Applications/Calculator/Keypad.h @@ -29,6 +29,8 @@ public: void set_value(Crypto::BigFraction); void set_to_0(); + void set_rounding_length(unsigned); + String to_string() const; private: @@ -46,6 +48,8 @@ private: mutable Crypto::BigFraction m_internal_value {}; + unsigned m_displayed_fraction_length { 0 }; + enum class State { External, TypingInteger, diff --git a/Userland/Applications/Calculator/main.cpp b/Userland/Applications/Calculator/main.cpp index 0e6bf8c2f82..c2076ccaad3 100644 --- a/Userland/Applications/Calculator/main.cpp +++ b/Userland/Applications/Calculator/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -16,8 +17,6 @@ #include #include #include -#include -#include ErrorOr serenity_main(Main::Arguments arguments) { @@ -70,6 +69,22 @@ ErrorOr serenity_main(Main::Arguments arguments) widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power }); })); + auto& round_menu = window->add_menu("&Round"); + GUI::ActionGroup preview_actions; + + static constexpr auto rounding_modes = Array { 0, 2, 4 }; + + for (auto const rounding_mode : rounding_modes) { + auto round_action = GUI::Action::create_checkable(String::formatted("To &{} digits", rounding_mode), [&widget, rounding_mode](auto&) { + widget->set_rounding_length(rounding_mode); + }); + + preview_actions.add_action(*round_action); + round_menu.add_action(*round_action); + } + + preview_actions.set_exclusive(true); + auto& help_menu = window->add_menu("&Help"); help_menu.add_action(GUI::CommonActions::make_about_action("Calculator", app_icon, window));