mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:17:44 +00:00
Calculator: Add a Shrinking action
This action allow the user to shrink a number to a finite number of decimal.
This commit is contained in:
parent
e3b22c395d
commit
2ff773a6ba
7 changed files with 32 additions and 7 deletions
|
@ -217,6 +217,12 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event)
|
||||||
update_display();
|
update_display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CalculatorWidget::shrink(unsigned shrink_threshold)
|
||||||
|
{
|
||||||
|
m_keypad.shrink(shrink_threshold);
|
||||||
|
update_display();
|
||||||
|
}
|
||||||
|
|
||||||
unsigned CalculatorWidget::rounding_length() const
|
unsigned CalculatorWidget::rounding_length() const
|
||||||
{
|
{
|
||||||
return m_keypad.rounding_length();
|
return m_keypad.rounding_length();
|
||||||
|
|
|
@ -22,6 +22,7 @@ public:
|
||||||
String get_entry();
|
String get_entry();
|
||||||
void set_entry(Crypto::BigFraction);
|
void set_entry(Crypto::BigFraction);
|
||||||
|
|
||||||
|
void shrink(unsigned);
|
||||||
unsigned rounding_length() const;
|
unsigned rounding_length() const;
|
||||||
void set_rounding_length(unsigned);
|
void set_rounding_length(unsigned);
|
||||||
|
|
||||||
|
|
|
@ -145,3 +145,8 @@ unsigned Keypad::rounding_length() const
|
||||||
{
|
{
|
||||||
return m_displayed_fraction_length;
|
return m_displayed_fraction_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Keypad::shrink(unsigned shrink_threshold)
|
||||||
|
{
|
||||||
|
m_internal_value = m_internal_value.rounded(shrink_threshold);
|
||||||
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ public:
|
||||||
void set_value(Crypto::BigFraction);
|
void set_value(Crypto::BigFraction);
|
||||||
void set_to_0();
|
void set_to_0();
|
||||||
|
|
||||||
|
void shrink(unsigned);
|
||||||
void set_rounding_length(unsigned);
|
void set_rounding_length(unsigned);
|
||||||
unsigned rounding_length() const;
|
unsigned rounding_length() const;
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,9 @@
|
||||||
#include <LibGUI/SpinBox.h>
|
#include <LibGUI/SpinBox.h>
|
||||||
#include <LibGUI/TextEditor.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) {
|
if (parent_window) {
|
||||||
dialog->set_icon(parent_window->icon());
|
dialog->set_icon(parent_window->icon());
|
||||||
|
@ -32,12 +32,12 @@ RoundingDialog::ExecResult RoundingDialog::show(GUI::Window* parent_window, unsi
|
||||||
return GUI::Dialog::ExecResult::OK;
|
return GUI::Dialog::ExecResult::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
RoundingDialog::RoundingDialog(GUI::Window* parent_window)
|
RoundingDialog::RoundingDialog(GUI::Window* parent_window, StringView title)
|
||||||
: Dialog(parent_window)
|
: Dialog(parent_window)
|
||||||
{
|
{
|
||||||
resize(m_dialog_length, m_dialog_height);
|
resize(m_dialog_length, m_dialog_height);
|
||||||
set_resizable(false);
|
set_resizable(false);
|
||||||
set_title("Choose custom rounding");
|
set_title(title);
|
||||||
|
|
||||||
auto& main_widget = set_main_widget<GUI::Widget>();
|
auto& main_widget = set_main_widget<GUI::Widget>();
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,10 @@ class RoundingDialog : public GUI::Dialog {
|
||||||
C_OBJECT(RoundingDialog);
|
C_OBJECT(RoundingDialog);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static ExecResult show(GUI::Window* parent_window, unsigned& rounding_value);
|
static ExecResult show(GUI::Window* parent_window, StringView title, unsigned& rounding_value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RoundingDialog(GUI::Window* parent_window);
|
RoundingDialog(GUI::Window* parent_window, StringView title);
|
||||||
virtual ~RoundingDialog() override = default;
|
virtual ~RoundingDialog() override = default;
|
||||||
|
|
||||||
RefPtr<GUI::SpinBox> m_rounding_spinbox;
|
RefPtr<GUI::SpinBox> m_rounding_spinbox;
|
||||||
|
|
|
@ -92,7 +92,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto round_custom = GUI::Action::create_checkable(String::formatted(format, 0), [&](auto& action) {
|
auto round_custom = GUI::Action::create_checkable(String::formatted(format, 0), [&](auto& action) {
|
||||||
unsigned custom_rounding_length = widget->rounding_length();
|
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));
|
action.set_text(String::formatted(format, custom_rounding_length));
|
||||||
widget->set_rounding_length(custom_rounding_length);
|
widget->set_rounding_length(custom_rounding_length);
|
||||||
last_rounding_mode.clear();
|
last_rounding_mode.clear();
|
||||||
|
@ -102,9 +102,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
widget->set_rounding_custom(round_custom, format);
|
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.add_action(*round_custom);
|
||||||
preview_actions.set_exclusive(true);
|
preview_actions.set_exclusive(true);
|
||||||
round_menu.add_action(*round_custom);
|
round_menu.add_action(*round_custom);
|
||||||
|
round_menu.add_action(*shrink_action);
|
||||||
|
|
||||||
round_menu.action_at(last_rounding_mode.value())->activate();
|
round_menu.action_at(last_rounding_mode.value())->activate();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue