diff options
author | Lucas CHOLLET <lucas.chollet@free.fr> | 2022-01-16 22:45:09 +0100 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-10-03 15:12:47 -0400 |
commit | 2ff773a6ba4b8ab81bf1f2eacc180a666a2ea204 (patch) | |
tree | f27d65c9fd62c10b57aa4d2e4384999b7f23b8c8 | |
parent | e3b22c395d7bef1897284f88ae0bacc77a8659a9 (diff) | |
download | serenity-2ff773a6ba4b8ab81bf1f2eacc180a666a2ea204.zip |
Calculator: Add a Shrinking action
This action allow the user to shrink a number to a finite number of
decimal.
-rw-r--r-- | Userland/Applications/Calculator/CalculatorWidget.cpp | 6 | ||||
-rw-r--r-- | Userland/Applications/Calculator/CalculatorWidget.h | 1 | ||||
-rw-r--r-- | Userland/Applications/Calculator/Keypad.cpp | 5 | ||||
-rw-r--r-- | Userland/Applications/Calculator/Keypad.h | 1 | ||||
-rw-r--r-- | Userland/Applications/Calculator/RoundingDialog.cpp | 8 | ||||
-rw-r--r-- | Userland/Applications/Calculator/RoundingDialog.h | 4 | ||||
-rw-r--r-- | Userland/Applications/Calculator/main.cpp | 14 |
7 files changed, 32 insertions, 7 deletions
diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index 10717da2f1..4835797214 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -217,6 +217,12 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event) update_display(); } +void CalculatorWidget::shrink(unsigned shrink_threshold) +{ + m_keypad.shrink(shrink_threshold); + update_display(); +} + unsigned CalculatorWidget::rounding_length() const { return m_keypad.rounding_length(); diff --git a/Userland/Applications/Calculator/CalculatorWidget.h b/Userland/Applications/Calculator/CalculatorWidget.h index b74242fc5f..556dadc22b 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.h +++ b/Userland/Applications/Calculator/CalculatorWidget.h @@ -22,6 +22,7 @@ public: String get_entry(); void set_entry(Crypto::BigFraction); + void shrink(unsigned); unsigned rounding_length() const; void set_rounding_length(unsigned); diff --git a/Userland/Applications/Calculator/Keypad.cpp b/Userland/Applications/Calculator/Keypad.cpp index 55b685e6fd..c261800043 100644 --- a/Userland/Applications/Calculator/Keypad.cpp +++ b/Userland/Applications/Calculator/Keypad.cpp @@ -145,3 +145,8 @@ unsigned Keypad::rounding_length() const { return m_displayed_fraction_length; } + +void Keypad::shrink(unsigned shrink_threshold) +{ + m_internal_value = m_internal_value.rounded(shrink_threshold); +} diff --git a/Userland/Applications/Calculator/Keypad.h b/Userland/Applications/Calculator/Keypad.h index b658f5e540..ce57847284 100644 --- a/Userland/Applications/Calculator/Keypad.h +++ b/Userland/Applications/Calculator/Keypad.h @@ -29,6 +29,7 @@ public: void set_value(Crypto::BigFraction); void set_to_0(); + void shrink(unsigned); void set_rounding_length(unsigned); unsigned rounding_length() const; diff --git a/Userland/Applications/Calculator/RoundingDialog.cpp b/Userland/Applications/Calculator/RoundingDialog.cpp index b968b42cd7..4492404f06 100644 --- a/Userland/Applications/Calculator/RoundingDialog.cpp +++ b/Userland/Applications/Calculator/RoundingDialog.cpp @@ -11,9 +11,9 @@ #include <LibGUI/SpinBox.h> #include <LibGUI/TextEditor.h> -RoundingDialog::ExecResult RoundingDialog::show(GUI::Window* parent_window, unsigned& rounding_value) +RoundingDialog::ExecResult RoundingDialog::show(GUI::Window* parent_window, StringView title, unsigned& rounding_value) { - auto dialog = RoundingDialog::construct(parent_window); + auto dialog = RoundingDialog::construct(parent_window, title); if (parent_window) { dialog->set_icon(parent_window->icon()); @@ -32,12 +32,12 @@ RoundingDialog::ExecResult RoundingDialog::show(GUI::Window* parent_window, unsi return GUI::Dialog::ExecResult::OK; } -RoundingDialog::RoundingDialog(GUI::Window* parent_window) +RoundingDialog::RoundingDialog(GUI::Window* parent_window, StringView title) : Dialog(parent_window) { resize(m_dialog_length, m_dialog_height); set_resizable(false); - set_title("Choose custom rounding"); + set_title(title); auto& main_widget = set_main_widget<GUI::Widget>(); diff --git a/Userland/Applications/Calculator/RoundingDialog.h b/Userland/Applications/Calculator/RoundingDialog.h index 24fa8990c3..62828c5f03 100644 --- a/Userland/Applications/Calculator/RoundingDialog.h +++ b/Userland/Applications/Calculator/RoundingDialog.h @@ -13,10 +13,10 @@ class RoundingDialog : public GUI::Dialog { C_OBJECT(RoundingDialog); public: - static ExecResult show(GUI::Window* parent_window, unsigned& rounding_value); + static ExecResult show(GUI::Window* parent_window, StringView title, unsigned& rounding_value); private: - RoundingDialog(GUI::Window* parent_window); + RoundingDialog(GUI::Window* parent_window, StringView title); virtual ~RoundingDialog() override = default; RefPtr<GUI::SpinBox> m_rounding_spinbox; diff --git a/Userland/Applications/Calculator/main.cpp b/Userland/Applications/Calculator/main.cpp index 3a8750b338..4b6b7802e9 100644 --- a/Userland/Applications/Calculator/main.cpp +++ b/Userland/Applications/Calculator/main.cpp @@ -92,7 +92,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto round_custom = GUI::Action::create_checkable(String::formatted(format, 0), [&](auto& action) { unsigned custom_rounding_length = widget->rounding_length(); - if (RoundingDialog::show(window, custom_rounding_length) == GUI::Dialog::ExecResult::OK) { + if (RoundingDialog::show(window, "Choose custom rounding"sv, custom_rounding_length) == GUI::Dialog::ExecResult::OK) { action.set_text(String::formatted(format, custom_rounding_length)); widget->set_rounding_length(custom_rounding_length); last_rounding_mode.clear(); @@ -102,9 +102,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) widget->set_rounding_custom(round_custom, format); + auto shrink_action = GUI::Action::create("&Shrink...", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-cut.png"sv)), [&](auto&) { + unsigned shrink_length = widget->rounding_length(); + + if (RoundingDialog::show(window, "Choose shrinking length"sv, shrink_length) == GUI::Dialog::ExecResult::OK) { + round_custom->set_checked(true); + round_custom->set_text(String::formatted(format, shrink_length)); + widget->set_rounding_length(shrink_length); + widget->shrink(shrink_length); + } + }); + preview_actions.add_action(*round_custom); preview_actions.set_exclusive(true); round_menu.add_action(*round_custom); + round_menu.add_action(*shrink_action); round_menu.action_at(last_rounding_mode.value())->activate(); |