CalculatorWidget.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "CalculatorWidget.h"
  27. #include <AK/Assertions.h>
  28. #include <LibGUI/Button.h>
  29. #include <LibGUI/Label.h>
  30. #include <LibGUI/TextBox.h>
  31. #include <LibGfx/Font.h>
  32. #include <LibGfx/Palette.h>
  33. CalculatorWidget::CalculatorWidget()
  34. {
  35. set_fill_with_background_color(true);
  36. m_entry = add<GUI::TextBox>();
  37. m_entry->set_relative_rect(5, 5, 244, 26);
  38. m_entry->set_text_alignment(Gfx::TextAlignment::CenterRight);
  39. m_entry->set_font(Gfx::Font::default_fixed_width_font());
  40. m_label = add<GUI::Label>();
  41. m_label->set_relative_rect(12, 42, 27, 27);
  42. m_label->set_frame_shadow(Gfx::FrameShadow::Sunken);
  43. m_label->set_frame_shape(Gfx::FrameShape::Container);
  44. m_label->set_frame_thickness(2);
  45. auto label_palette = m_label->palette();
  46. label_palette.set_color(Gfx::ColorRole::WindowText, Color::Red);
  47. m_label->set_palette(label_palette);
  48. update_display();
  49. for (int i = 0; i < 10; i++) {
  50. m_digit_button[i] = add<GUI::Button>();
  51. auto& button = *m_digit_button[i];
  52. int p = i ? i + 2 : 0;
  53. int x = 55 + (p % 3) * 39;
  54. int y = 177 - (p / 3) * 33;
  55. button.move_to(x, y);
  56. add_digit_button(button, i);
  57. }
  58. m_mem_add_button = add<GUI::Button>();
  59. m_mem_add_button->move_to(9, 177);
  60. m_mem_add_button->set_text("M+");
  61. add_operation_button(*m_mem_add_button, Calculator::Operation::MemAdd);
  62. m_mem_save_button = add<GUI::Button>();
  63. m_mem_save_button->move_to(9, 144);
  64. m_mem_save_button->set_text("MS");
  65. add_operation_button(*m_mem_save_button, Calculator::Operation::MemSave);
  66. m_mem_recall_button = add<GUI::Button>();
  67. m_mem_recall_button->move_to(9, 111);
  68. m_mem_recall_button->set_text("MR");
  69. add_operation_button(*m_mem_recall_button, Calculator::Operation::MemRecall);
  70. m_mem_clear_button = add<GUI::Button>();
  71. m_mem_clear_button->move_to(9, 78);
  72. m_mem_clear_button->set_text("MC");
  73. add_operation_button(*m_mem_clear_button, Calculator::Operation::MemClear);
  74. m_clear_button = add<GUI::Button>();
  75. m_clear_button->set_text("C");
  76. m_clear_button->on_click = [this](auto) {
  77. m_keypad.set_value(0.0);
  78. m_calculator.clear_operation();
  79. update_display();
  80. };
  81. add_button(*m_clear_button, Color::Red);
  82. m_clear_button->set_relative_rect(187, 40, 60, 28);
  83. m_clear_error_button = add<GUI::Button>();
  84. m_clear_error_button->set_text("CE");
  85. m_clear_error_button->on_click = [this](auto) {
  86. m_keypad.set_value(0.0);
  87. update_display();
  88. };
  89. add_button(*m_clear_error_button, Color::Red);
  90. m_clear_error_button->set_relative_rect(124, 40, 59, 28);
  91. m_backspace_button = add<GUI::Button>();
  92. m_backspace_button->set_text("Backspace");
  93. m_backspace_button->on_click = [this](auto) {
  94. m_keypad.type_backspace();
  95. update_display();
  96. };
  97. add_button(*m_backspace_button, Color::Red);
  98. m_backspace_button->set_relative_rect(55, 40, 65, 28);
  99. m_decimal_point_button = add<GUI::Button>();
  100. m_decimal_point_button->move_to(133, 177);
  101. m_decimal_point_button->set_text(".");
  102. m_decimal_point_button->on_click = [this](auto) {
  103. m_keypad.type_decimal_point();
  104. update_display();
  105. };
  106. add_button(*m_decimal_point_button, Color::Blue);
  107. m_sign_button = add<GUI::Button>();
  108. m_sign_button->move_to(94, 177);
  109. m_sign_button->set_text("+/-");
  110. add_operation_button(*m_sign_button, Calculator::Operation::ToggleSign, Color::Blue);
  111. m_add_button = add<GUI::Button>();
  112. m_add_button->move_to(172, 177);
  113. m_add_button->set_text("+");
  114. add_operation_button(*m_add_button, Calculator::Operation::Add);
  115. m_subtract_button = add<GUI::Button>();
  116. m_subtract_button->move_to(172, 144);
  117. m_subtract_button->set_text("-");
  118. add_operation_button(*m_subtract_button, Calculator::Operation::Subtract);
  119. m_multiply_button = add<GUI::Button>();
  120. m_multiply_button->move_to(172, 111);
  121. m_multiply_button->set_text("*");
  122. add_operation_button(*m_multiply_button, Calculator::Operation::Multiply);
  123. m_divide_button = add<GUI::Button>();
  124. m_divide_button->move_to(172, 78);
  125. m_divide_button->set_text("/");
  126. add_operation_button(*m_divide_button, Calculator::Operation::Divide);
  127. m_sqrt_button = add<GUI::Button>();
  128. m_sqrt_button->move_to(211, 78);
  129. m_sqrt_button->set_text("sqrt");
  130. add_operation_button(*m_sqrt_button, Calculator::Operation::Sqrt, Color::Blue);
  131. m_inverse_button = add<GUI::Button>();
  132. m_inverse_button->move_to(211, 144);
  133. m_inverse_button->set_text("1/x");
  134. add_operation_button(*m_inverse_button, Calculator::Operation::Inverse, Color::Blue);
  135. m_percent_button = add<GUI::Button>();
  136. m_percent_button->move_to(211, 111);
  137. m_percent_button->set_text("%");
  138. add_operation_button(*m_percent_button, Calculator::Operation::Percent, Color::Blue);
  139. m_equals_button = add<GUI::Button>();
  140. m_equals_button->move_to(211, 177);
  141. m_equals_button->set_text("=");
  142. m_equals_button->on_click = [this](auto) {
  143. double argument = m_keypad.value();
  144. double res = m_calculator.finish_operation(argument);
  145. m_keypad.set_value(res);
  146. update_display();
  147. };
  148. add_button(*m_equals_button, Color::Red);
  149. }
  150. CalculatorWidget::~CalculatorWidget()
  151. {
  152. }
  153. void CalculatorWidget::add_operation_button(GUI::Button& button, Calculator::Operation operation, Color text_color)
  154. {
  155. add_button(button, text_color);
  156. button.on_click = [this, operation](auto) {
  157. double argument = m_keypad.value();
  158. double res = m_calculator.begin_operation(operation, argument);
  159. m_keypad.set_value(res);
  160. update_display();
  161. };
  162. }
  163. void CalculatorWidget::add_digit_button(GUI::Button& button, int digit)
  164. {
  165. add_button(button, Color::Blue);
  166. button.set_text(String::number(digit));
  167. button.on_click = [this, digit](auto) {
  168. m_keypad.type_digit(digit);
  169. update_display();
  170. };
  171. }
  172. void CalculatorWidget::add_button(GUI::Button& button, Color text_color)
  173. {
  174. button.resize(35, 28);
  175. auto palette = button.palette();
  176. palette.set_color(Gfx::ColorRole::ButtonText, text_color);
  177. button.set_palette(palette);
  178. }
  179. void CalculatorWidget::update_display()
  180. {
  181. m_entry->set_text(m_keypad.to_string());
  182. if (m_calculator.has_error())
  183. m_label->set_text("E");
  184. else
  185. m_label->set_text("");
  186. }
  187. void CalculatorWidget::keydown_event(GUI::KeyEvent& event)
  188. {
  189. //Clear button selection when we are typing
  190. m_equals_button->set_focus(true);
  191. m_equals_button->set_focus(false);
  192. if (event.key() == KeyCode::Key_Return) {
  193. m_keypad.set_value(m_calculator.finish_operation(m_keypad.value()));
  194. } else if (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) {
  195. m_keypad.type_digit(atoi(event.text().characters()));
  196. } else if (event.key() == KeyCode::Key_Period) {
  197. m_keypad.type_decimal_point();
  198. } else if (event.key() == KeyCode::Key_Escape) {
  199. m_keypad.set_value(0.0);
  200. m_calculator.clear_operation();
  201. } else if (event.key() == KeyCode::Key_Backspace) {
  202. m_keypad.type_backspace();
  203. } else {
  204. Calculator::Operation operation;
  205. switch (event.key()) {
  206. case KeyCode::Key_Plus:
  207. operation = Calculator::Operation::Add;
  208. break;
  209. case KeyCode::Key_Minus:
  210. operation = Calculator::Operation::Subtract;
  211. break;
  212. case KeyCode::Key_Asterisk:
  213. operation = Calculator::Operation::Multiply;
  214. break;
  215. case KeyCode::Key_Slash:
  216. operation = Calculator::Operation::Divide;
  217. break;
  218. case KeyCode::Key_Percent:
  219. operation = Calculator::Operation::Percent;
  220. break;
  221. default:
  222. return;
  223. }
  224. m_keypad.set_value(m_calculator.begin_operation(operation, m_keypad.value()));
  225. }
  226. update_display();
  227. }