1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 12:17:35 +00:00

Calculator: Port interface to GML (#4742)

This commit is contained in:
Glenford Williams 2021-01-03 16:27:24 -05:00 committed by GitHub
parent bf45a28a89
commit 7dc52e04fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 306 additions and 90 deletions

View file

@ -1,8 +1,10 @@
compile_gml(CalculatorWindow.gml CalculatorGML.h calculator_gml)
set(SOURCES set(SOURCES
main.cpp main.cpp
Calculator.cpp Calculator.cpp
CalculatorWidget.cpp CalculatorWidget.cpp
Keypad.cpp Keypad.cpp
CalculatorGML.h
) )
serenity_app(Calculator ICON app-calculator) serenity_app(Calculator ICON app-calculator)

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org> * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2021 Glenford Williams <gw_dev@outlook.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -25,6 +26,7 @@
*/ */
#include "CalculatorWidget.h" #include "CalculatorWidget.h"
#include "Applications/Calculator/CalculatorGML.h"
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <LibGUI/Button.h> #include <LibGUI/Button.h>
#include <LibGUI/Label.h> #include <LibGUI/Label.h>
@ -35,150 +37,100 @@
CalculatorWidget::CalculatorWidget() CalculatorWidget::CalculatorWidget()
{ {
set_fill_with_background_color(true); load_from_gml(calculator_gml);
m_entry = add<GUI::TextBox>(); m_entry = *find_descendant_of_type_named<GUI::TextBox>("entry_textbox");
m_entry->set_relative_rect(5, 5, 244, 26); m_entry->set_relative_rect(5, 5, 244, 26);
m_entry->set_text_alignment(Gfx::TextAlignment::CenterRight); m_entry->set_text_alignment(Gfx::TextAlignment::CenterRight);
m_entry->set_font(Gfx::FontDatabase::default_fixed_width_font()); m_entry->set_font(Gfx::FontDatabase::default_fixed_width_font());
m_label = add<GUI::Label>(); m_label = *find_descendant_of_type_named<GUI::Label>("label");
m_label->set_relative_rect(12, 42, 27, 27);
m_label->set_frame_shadow(Gfx::FrameShadow::Sunken); m_label->set_frame_shadow(Gfx::FrameShadow::Sunken);
m_label->set_frame_shape(Gfx::FrameShape::Container); m_label->set_frame_shape(Gfx::FrameShape::Container);
m_label->set_frame_thickness(2); m_label->set_frame_thickness(2);
auto label_palette = m_label->palette();
label_palette.set_color(Gfx::ColorRole::WindowText, Color::Red);
m_label->set_palette(label_palette);
update_display();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
m_digit_button[i] = add<GUI::Button>(); m_digit_button[i] = *find_descendant_of_type_named<GUI::Button>(String::formatted("{}_button", i));
auto& button = *m_digit_button[i]; add_digit_button(*m_digit_button[i], i);
int p = i ? i + 2 : 0;
int x = 55 + (p % 3) * 39;
int y = 177 - (p / 3) * 33;
button.move_to(x, y);
add_digit_button(button, i);
} }
m_mem_add_button = add<GUI::Button>(); m_mem_add_button = *find_descendant_of_type_named<GUI::Button>("mem_add_button");
m_mem_add_button->move_to(9, 177);
m_mem_add_button->set_text("M+");
add_operation_button(*m_mem_add_button, Calculator::Operation::MemAdd); add_operation_button(*m_mem_add_button, Calculator::Operation::MemAdd);
m_mem_save_button = add<GUI::Button>(); m_mem_save_button = *find_descendant_of_type_named<GUI::Button>("mem_save_button");
m_mem_save_button->move_to(9, 144);
m_mem_save_button->set_text("MS");
add_operation_button(*m_mem_save_button, Calculator::Operation::MemSave); add_operation_button(*m_mem_save_button, Calculator::Operation::MemSave);
m_mem_recall_button = add<GUI::Button>(); m_mem_recall_button = *find_descendant_of_type_named<GUI::Button>("mem_recall_button");
m_mem_recall_button->move_to(9, 111);
m_mem_recall_button->set_text("MR");
add_operation_button(*m_mem_recall_button, Calculator::Operation::MemRecall); add_operation_button(*m_mem_recall_button, Calculator::Operation::MemRecall);
m_mem_clear_button = add<GUI::Button>(); m_mem_clear_button = *find_descendant_of_type_named<GUI::Button>("mem_clear_button");
m_mem_clear_button->move_to(9, 78);
m_mem_clear_button->set_text("MC");
add_operation_button(*m_mem_clear_button, Calculator::Operation::MemClear); add_operation_button(*m_mem_clear_button, Calculator::Operation::MemClear);
m_clear_button = add<GUI::Button>(); m_clear_button = *find_descendant_of_type_named<GUI::Button>("clear_button");
m_clear_button->set_text("C");
m_clear_button->on_click = [this](auto) { m_clear_button->on_click = [this](auto) {
m_keypad.set_value(0.0); m_keypad.set_value(0.0);
m_calculator.clear_operation(); m_calculator.clear_operation();
update_display(); update_display();
}; };
add_button(*m_clear_button, Color::Red);
m_clear_button->set_relative_rect(187, 40, 60, 28);
m_clear_error_button = add<GUI::Button>(); m_clear_error_button = *find_descendant_of_type_named<GUI::Button>("clear_error_button");
m_clear_error_button->set_text("CE");
m_clear_error_button->on_click = [this](auto) { m_clear_error_button->on_click = [this](auto) {
m_keypad.set_value(0.0); m_keypad.set_value(0.0);
update_display(); update_display();
}; };
add_button(*m_clear_error_button, Color::Red);
m_clear_error_button->set_relative_rect(124, 40, 59, 28);
m_backspace_button = add<GUI::Button>(); m_backspace_button = *find_descendant_of_type_named<GUI::Button>("backspace_button");
m_backspace_button->set_text("Backspace");
m_backspace_button->on_click = [this](auto) { m_backspace_button->on_click = [this](auto) {
m_keypad.type_backspace(); m_keypad.type_backspace();
update_display(); update_display();
}; };
add_button(*m_backspace_button, Color::Red);
m_backspace_button->set_relative_rect(55, 40, 65, 28);
m_decimal_point_button = add<GUI::Button>(); m_decimal_point_button = *find_descendant_of_type_named<GUI::Button>("decimal_button");
m_decimal_point_button->move_to(133, 177);
m_decimal_point_button->set_text(".");
m_decimal_point_button->on_click = [this](auto) { m_decimal_point_button->on_click = [this](auto) {
m_keypad.type_decimal_point(); m_keypad.type_decimal_point();
update_display(); update_display();
}; };
add_button(*m_decimal_point_button, Color::Blue);
m_sign_button = add<GUI::Button>(); m_sign_button = *find_descendant_of_type_named<GUI::Button>("sign_button");
m_sign_button->move_to(94, 177); add_operation_button(*m_sign_button, Calculator::Operation::ToggleSign);
m_sign_button->set_text("+/-");
add_operation_button(*m_sign_button, Calculator::Operation::ToggleSign, Color::Blue);
m_add_button = add<GUI::Button>(); m_add_button = *find_descendant_of_type_named<GUI::Button>("add_button");
m_add_button->move_to(172, 177);
m_add_button->set_text("+");
add_operation_button(*m_add_button, Calculator::Operation::Add); add_operation_button(*m_add_button, Calculator::Operation::Add);
m_subtract_button = add<GUI::Button>(); m_subtract_button = *find_descendant_of_type_named<GUI::Button>("subtract_button");
m_subtract_button->move_to(172, 144);
m_subtract_button->set_text("-");
add_operation_button(*m_subtract_button, Calculator::Operation::Subtract); add_operation_button(*m_subtract_button, Calculator::Operation::Subtract);
m_multiply_button = add<GUI::Button>(); m_multiply_button = *find_descendant_of_type_named<GUI::Button>("multiply_button");
m_multiply_button->move_to(172, 111);
m_multiply_button->set_text("*");
add_operation_button(*m_multiply_button, Calculator::Operation::Multiply); add_operation_button(*m_multiply_button, Calculator::Operation::Multiply);
m_divide_button = add<GUI::Button>(); m_divide_button = *find_descendant_of_type_named<GUI::Button>("divide_button");
m_divide_button->move_to(172, 78);
m_divide_button->set_text("/");
add_operation_button(*m_divide_button, Calculator::Operation::Divide); add_operation_button(*m_divide_button, Calculator::Operation::Divide);
m_sqrt_button = add<GUI::Button>(); m_sqrt_button = *find_descendant_of_type_named<GUI::Button>("sqrt_button");
m_sqrt_button->move_to(211, 78); add_operation_button(*m_sqrt_button, Calculator::Operation::Sqrt);
m_sqrt_button->set_text("sqrt");
add_operation_button(*m_sqrt_button, Calculator::Operation::Sqrt, Color::Blue);
m_inverse_button = add<GUI::Button>(); m_inverse_button = *find_descendant_of_type_named<GUI::Button>("inverse_button");
m_inverse_button->move_to(211, 144); add_operation_button(*m_inverse_button, Calculator::Operation::Inverse);
m_inverse_button->set_text("1/x");
add_operation_button(*m_inverse_button, Calculator::Operation::Inverse, Color::Blue);
m_percent_button = add<GUI::Button>(); m_percent_button = *find_descendant_of_type_named<GUI::Button>("mod_button");
m_percent_button->move_to(211, 111); add_operation_button(*m_percent_button, Calculator::Operation::Percent);
m_percent_button->set_text("%");
add_operation_button(*m_percent_button, Calculator::Operation::Percent, Color::Blue);
m_equals_button = add<GUI::Button>(); m_equals_button = *find_descendant_of_type_named<GUI::Button>("equal_button");
m_equals_button->move_to(211, 177);
m_equals_button->set_text("=");
m_equals_button->on_click = [this](auto) { m_equals_button->on_click = [this](auto) {
double argument = m_keypad.value(); double argument = m_keypad.value();
double res = m_calculator.finish_operation(argument); double res = m_calculator.finish_operation(argument);
m_keypad.set_value(res); m_keypad.set_value(res);
update_display(); update_display();
}; };
add_button(*m_equals_button, Color::Red);
} }
CalculatorWidget::~CalculatorWidget() CalculatorWidget::~CalculatorWidget()
{ {
} }
void CalculatorWidget::add_operation_button(GUI::Button& button, Calculator::Operation operation, Color text_color) void CalculatorWidget::add_operation_button(GUI::Button& button, Calculator::Operation operation)
{ {
add_button(button, text_color);
button.on_click = [this, operation](auto) { button.on_click = [this, operation](auto) {
double argument = m_keypad.value(); double argument = m_keypad.value();
double res = m_calculator.begin_operation(operation, argument); double res = m_calculator.begin_operation(operation, argument);
@ -189,22 +141,12 @@ void CalculatorWidget::add_operation_button(GUI::Button& button, Calculator::Ope
void CalculatorWidget::add_digit_button(GUI::Button& button, int digit) void CalculatorWidget::add_digit_button(GUI::Button& button, int digit)
{ {
add_button(button, Color::Blue);
button.set_text(String::number(digit));
button.on_click = [this, digit](auto) { button.on_click = [this, digit](auto) {
m_keypad.type_digit(digit); m_keypad.type_digit(digit);
update_display(); update_display();
}; };
} }
void CalculatorWidget::add_button(GUI::Button& button, Color text_color)
{
button.resize(35, 28);
auto palette = button.palette();
palette.set_color(Gfx::ColorRole::ButtonText, text_color);
button.set_palette(palette);
}
void CalculatorWidget::update_display() void CalculatorWidget::update_display()
{ {
m_entry->set_text(m_keypad.to_string()); m_entry->set_text(m_keypad.to_string());

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org> * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2021 Glenford Williams <gw_dev@outlook.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -38,9 +39,8 @@ public:
private: private:
CalculatorWidget(); CalculatorWidget();
void add_operation_button(GUI::Button&, Calculator::Operation, Color text_color = Color::Red); void add_operation_button(GUI::Button&, Calculator::Operation);
void add_digit_button(GUI::Button&, int digit); void add_digit_button(GUI::Button&, int digit);
void add_button(GUI::Button&, Color text_color);
void update_display(); void update_display();

View file

@ -0,0 +1,272 @@
@GUI::Widget {
fixed_width: 254
fixed_height: 213
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [10, 0, 10, 0]
}
@GUI::TextBox {
name: "entry_textbox"
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
}
@GUI::Label {
name: "label"
fixed_width: 35
fixed_height: 27
}
@GUI::Widget {
fixed_width: 5
}
@GUI::Button {
name: "backspace_button"
text: "Backspace"
fixed_width: 65
fixed_height: 28
foreground_color: "brown"
}
@GUI::Button {
name: "clear_error_button"
text: "CE"
fixed_width: 55
fixed_height: 28
foreground_color: "brown"
}
@GUI::Button {
name: "clear_button"
text: "C"
fixed_width: 60
fixed_height: 28
foreground_color: "brown"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
}
@GUI::Button {
name: "mem_clear_button"
text: "MC"
fixed_width: 35
fixed_height: 28
foreground_color: "red"
}
@GUI::Widget {
fixed_width: 5
}
@GUI::Button {
name: "7_button"
text: "7"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "8_button"
text: "8"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "9_button"
text: "9"
fixed_width: 35
fixed_height: 28
}
@GUI::Button {
name: "divide_button"
text: "/"
fixed_width: 35
fixed_height: 28
}
@GUI::Button {
name: "sqrt_button"
text: "sqrt"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {}
@GUI::Button {
name: "mem_recall_button"
text: "MR"
fixed_width: 35
fixed_height: 28
foreground_color: "red"
}
@GUI::Widget {
fixed_width: 5
}
@GUI::Button {
name: "4_button"
text: "4"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "5_button"
text: "5"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "6_button"
text: "6"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "multiply_button"
text: "*"
fixed_width: 35
fixed_height: 28
}
@GUI::Button {
name: "mod_button"
text: "%"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {}
@GUI::Button {
name: "mem_save_button"
text: "MS"
fixed_width: 35
fixed_height: 28
foreground_color: "red"
}
@GUI::Widget {
fixed_width: 5
}
@GUI::Button {
name: "1_button"
text: "1"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "2_button"
text: "2"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "3_button"
text: "3"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "subtract_button"
text: "-"
fixed_width: 35
fixed_height: 28
}
@GUI::Button {
name: "inverse_button"
text: "1/x"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {}
@GUI::Button {
name: "mem_add_button"
text: "M+"
fixed_width: 35
fixed_height: 28
foreground_color: "red"
}
@GUI::Widget {
fixed_width: 5
}
@GUI::Button {
name: "0_button"
text: "0"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "sign_button"
text: "+/-"
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "decimal_button"
text: "."
fixed_width: 35
fixed_height: 28
foreground_color: "blue"
}
@GUI::Button {
name: "add_button"
text: "+"
fixed_width: 35
fixed_height: 28
}
@GUI::Button {
name: "equal_button"
text: "="
fixed_width: 35
fixed_height: 28
foreground_color: "red"
}
}
}