diff options
author | Lucas CHOLLET <lucas.chollet@free.fr> | 2022-09-22 16:54:46 +0200 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-10-03 15:12:47 -0400 |
commit | de568f87fd709f13d08a35448f2fd99badfb2ef7 (patch) | |
tree | 821a7b73b266d72c9a593651355ec7369416ce73 /Userland | |
parent | 164094e161de3df98a9d169161cf639af9c09a56 (diff) | |
download | serenity-de568f87fd709f13d08a35448f2fd99badfb2ef7.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/Calculator/CalculatorWidget.cpp | 6 | ||||
-rw-r--r-- | Userland/Applications/Calculator/CalculatorWidget.h | 2 | ||||
-rw-r--r-- | Userland/Applications/Calculator/Keypad.cpp | 9 | ||||
-rw-r--r-- | Userland/Applications/Calculator/Keypad.h | 4 | ||||
-rw-r--r-- | 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 cfe111fe38..a2f1c53666 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 07845cd3df..6415af4277 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 b94fd79e2d..e1624d7a0d 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 0410e31b51..b71b9214da 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 0e6bf8c2f8..c2076ccaad 100644 --- a/Userland/Applications/Calculator/main.cpp +++ b/Userland/Applications/Calculator/main.cpp @@ -8,6 +8,7 @@ #include <LibCore/System.h> #include <LibCrypto/NumberTheory/ModularFunctions.h> #include <LibGUI/Action.h> +#include <LibGUI/ActionGroup.h> #include <LibGUI/Application.h> #include <LibGUI/Clipboard.h> #include <LibGUI/Icon.h> @@ -16,8 +17,6 @@ #include <LibGUI/Window.h> #include <LibGfx/Bitmap.h> #include <LibMain/Main.h> -#include <stdio.h> -#include <unistd.h> ErrorOr<int> serenity_main(Main::Arguments arguments) { @@ -70,6 +69,22 @@ ErrorOr<int> 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)); |