Module: Roebe::GUI::CalculatorModule
- Included in:
- Calculator
- Defined in:
- lib/roebe/gui/shared_code/calculator/calculator_module.rb
Overview
Roebe::GUI::CalculatorModule
Constant Summary collapse
- TITLE =
#
Roebe::GUI::CalculatorModule::TITLE
#
'Calculator'
- WIDTH =
#
WIDTH
#
'20% or minimum 200px'
- HEIGHT =
#
HEIGHT
#
'18% or minimum 180px'
- USE_THIS_FONT =
#
USE_THIS_FONT
#
:dejavu_condensed_18
- HASH_DESIGNATED_KEY_COMBINATIONS =
#
Roebe::GUI::CalculatorModule::HASH_DESIGNATED_KEY_COMBINATIONS
#
{ 'ctrl+=': "input_is('=')", # This one currently does not work. 'alt+1': 'input_is(1)', 'alt+2': 'input_is(2)', 'alt+3': 'input_is(3)', 'alt+4': 'input_is(4)', 'alt+5': 'input_is(5)', 'alt+6': 'input_is(6)', 'alt+7': 'input_is(7)', 'alt+8': 'input_is(8)', 'alt+9': 'input_is(9)', 'alt+0': 'input_is(0)', '1': 'input_is(1)', '2': 'input_is(2)', '3': 'input_is(3)', '4': 'input_is(4)', '5': 'input_is(5)', '6': 'input_is(6)', '7': 'input_is(7)', '8': 'input_is(8)', '9': 'input_is(9)', '0': 'input_is(0)', '.': "input_is('.')", 'alt++': "input_is('+')", 'alt+-': "input_is('-')", 'alt+/': "input_is('/')", 'alt+*': "input_is('/*')" }
- PERCENTAGE_SIGN =
#
PERCENTAGE_SIGN
#
'%'
Instance Method Summary collapse
-
#border_size? ⇒ Boolean
# === border_size? ========================================================================= #.
-
#box? ⇒ Boolean
# === box? ========================================================================= #.
-
#create_the_grid ⇒ Object
# === create_the_grid.
-
#create_the_label_results ⇒ Object
# === create_the_label_results.
-
#create_the_outer_box ⇒ Object
# === create_the_outer_box ========================================================================= #.
-
#do_clear_the_main_label ⇒ Object
(also: #do_clear, #clear_content)
# === do_clear_the_main_label.
-
#do_evaluate_the_current_text(i = main_text? ) ⇒ Object
(also: #do_the_calculation)
# === do_evaluate_the_current_text ========================================================================= #.
-
#do_perform_percentage_evaluation(i = main_entry_text? ) ⇒ Object
# === do_perform_percentage_evaluation ========================================================================= #.
-
#do_square_the_result(i = main_text? ) ⇒ Object
# === do_square_the_result ========================================================================= #.
-
#do_style_all_buttons_in_a_uniform_manner ⇒ Object
# === do_style_all_buttons_in_a_uniform_manner ========================================================================= #.
-
#handle_CSS_rules ⇒ Object
# === handle_CSS_rules (CSS tag) ========================================================================= #.
-
#input_is(i = 1, main_text = main_text? ) ⇒ Object
# === input_is (input tag) ========================================================================= #.
-
#label_results? ⇒ Boolean
# === label_results? ========================================================================= #.
-
#last_char? ⇒ Boolean
# === last_char?.
-
#let_the_label_respond_to_key_press_events ⇒ Object
# === let_the_label_respond_to_key_press_events ========================================================================= #.
-
#main_entry? ⇒ Boolean
(also: #label_results)
# === main_entry? ========================================================================= #.
-
#main_text? ⇒ Boolean
(also: #string?, #main_entry_text?)
# === main_text? ========================================================================= #.
-
#padding? ⇒ Boolean
# === padding? ========================================================================= #.
-
#populate_the_grid ⇒ Object
# === populate_the_grid ========================================================================= #.
-
#reset_the_shared_module ⇒ Object
# === reset_the_shared_module ========================================================================= #.
-
#return_button0 ⇒ Object
(also: #return0)
# === return_button0 ========================================================================= #.
-
#return_button1 ⇒ Object
(also: #return1)
# === return_button1 ========================================================================= #.
-
#return_button2 ⇒ Object
(also: #return2)
# === return_button2 ========================================================================= #.
-
#return_button3 ⇒ Object
(also: #return3)
# === return_button3 ========================================================================= #.
-
#return_button4 ⇒ Object
(also: #return4)
# === return_button4 ========================================================================= #.
-
#return_button5 ⇒ Object
(also: #return5)
# === return_button5 ========================================================================= #.
-
#return_button6 ⇒ Object
(also: #return6)
# === return_button6 ========================================================================= #.
-
#return_button7 ⇒ Object
(also: #return7)
# === return_button7 ========================================================================= #.
-
#return_button8 ⇒ Object
(also: #return8)
# === return_button8 ========================================================================= #.
-
#return_button9 ⇒ Object
(also: #return9)
# === return_button9 ========================================================================= #.
-
#return_button_clear ⇒ Object
(also: #button_clear?)
# === return_button_clear ========================================================================= #.
-
#return_button_divide ⇒ Object
# === return_button_divide ========================================================================= #.
-
#return_button_dot ⇒ Object
(also: #return_dot)
# === return_button_dot ========================================================================= #.
-
#return_button_equal ⇒ Object
(also: #return_equal)
# === return_button_equal ========================================================================= #.
-
#return_button_minus ⇒ Object
(also: #return_minus)
# === return_button_minus ========================================================================= #.
-
#return_button_multiply ⇒ Object
# === return_button_multiply ========================================================================= #.
-
#return_button_percentage_sign ⇒ Object
(also: #return_percentage)
# === return_button_percentage_sign ========================================================================= #.
-
#return_button_plus ⇒ Object
(also: #return_plus)
# === return_button_plus ========================================================================= #.
-
#return_button_square ⇒ Object
(also: #return_square)
# === return_button_square ========================================================================= #.
-
#return_hint_for_the_division_button ⇒ Object
# === return_hint_for_the_division_button ========================================================================= #.
-
#return_hint_for_the_dot_button ⇒ Object
# === return_hint_for_the_dot_button ========================================================================= #.
-
#return_the_frame_containing_the_primary_box(box = @box) ⇒ Object
# === return_the_frame_containing_the_primary_box ========================================================================= #.
-
#set_main_text(i) ⇒ Object
(also: #string=, #update_entry, #set_this_as_the_new_result)
# === set_main_text ========================================================================= #.
Instance Method Details
#border_size? ⇒ Boolean
#
border_size?
#
469 470 471 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 469 def border_size? 2 end |
#box? ⇒ Boolean
#
box?
#
622 623 624 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 622 def box? @box end |
#create_the_grid ⇒ Object
#
create_the_grid
This is the base grid - it will be populated by another method.
#
631 632 633 634 635 636 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 631 def create_the_grid # ======================================================================= # # Grid layout where all the buttons will reside # ======================================================================= # @grid = create_grid('5x4') end |
#create_the_label_results ⇒ Object
#
create_the_label_results
This method will create @label_results.
#
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 575 def create_the_label_results # ======================================================================= # # === @label_results # # Create the label where the equation results will appear, on top of # the gtk-grid layout that we are using. # ======================================================================= # @label_results = right_aligned_label @label_results.make_bold @label_results.bblack2 @label_results.yellow_background @label_results.hint = 'This label will show the results of our '\ 'calculation.' @label_results.pad8px @label_results.make_selectable @label_results.enable_all_events @label_results.on_enter { do_the_calculation } end |
#create_the_outer_box ⇒ Object
#
create_the_outer_box
#
599 600 601 602 603 604 605 606 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 599 def create_the_outer_box # ======================================================================= # # Define the outer box next, called @box. # ======================================================================= # @box = create_vbox {{ spacing: 12 }} @box.set_border_width(38) @box.set_homogeneous(false) end |
#do_clear_the_main_label ⇒ Object Also known as: do_clear, clear_content
#
do_clear_the_main_label
This method can be used to clear the main label, where the result shows up.
#
539 540 541 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 539 def do_clear_the_main_label label_results?.clear end |
#do_evaluate_the_current_text(i = main_text? ) ⇒ Object Also known as: do_the_calculation
#
do_evaluate_the_current_text
#
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 677 def do_evaluate_the_current_text( i = main_text? ) # ======================================================================= # # Divisions will be more accurate. # ======================================================================= # if i.include? '/' first_char = i[0, 1] first_char = first_char.to_f.to_s i[0, 1] = first_char end begin result = eval(i) rescue Exception result = '' end set_main_text(result) end |
#do_perform_percentage_evaluation(i = main_entry_text? ) ⇒ Object
#
do_perform_percentage_evaluation
#
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 157 def do_perform_percentage_evaluation( i = main_entry_text? ) use_this_as_the_split_token = i.gsub(/\d/,'') if i.include?(use_this_as_the_split_token) # ===================================================================== # # For instance, "600*15", which should be 90, as 90 is 15% of 600. # ===================================================================== # splitted = i.split(use_this_as_the_split_token) a = splitted.first.to_f percentage_value = splitted.last.to_f case use_this_as_the_split_token # ===================================================================== # # === * # ===================================================================== # when '*' result = ( a * percentage_value / 100.0 ) # ===================================================================== # # === / # ===================================================================== # when '/' result = ( a / (percentage_value / 100.0) ) # ===================================================================== # # === + # ===================================================================== # when '+' # This is "34+7" which means to calculate at 107.0 result = ( a * ( (100+percentage_value) / 100.0) ) # ===================================================================== # # === - # ===================================================================== # when '-' # This is "79-30" which means the result will be 55.3 result = ( a * ( (100-percentage_value) / 100.0) ) end set_this_as_the_new_result(result) else e 'Can not evaluate the following input: '+tomato(i) end end |
#do_square_the_result(i = main_text? ) ⇒ Object
#
do_square_the_result
#
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 96 def do_square_the_result( i = main_text? ) begin result = eval(i) if result.nil? e 'Got a nil-result. Input was: '+i.to_s else result = Math.sqrt(result) set_main_text(result.to_s) end rescue Exception => error pp error end end |
#do_style_all_buttons_in_a_uniform_manner ⇒ Object
#
do_style_all_buttons_in_a_uniform_manner
#
563 564 565 566 567 568 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 563 def .each {|| .remove_background .on_hover(:lightgreen) } end |
#handle_CSS_rules ⇒ Object
#
handle_CSS_rules (CSS tag)
#
44 45 46 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 44 def handle_CSS_rules use_gtk_paradise_project_css_file end |
#input_is(i = 1, main_text = main_text? ) ⇒ Object
#
input_is (input tag)
#
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 476 def input_is( i = 1, main_text = main_text? ) # ======================================================================= # # Next sanitize the names "plus", "minus", "asterisk", and "slash". # ======================================================================= # case i # ======================================================================= # # === plus # ======================================================================= # when 'plus' i = '+' # ======================================================================= # # === minus # ======================================================================= # when 'minus' i = '-' # ======================================================================= # # === asterisk # ======================================================================= # when 'asterisk','x' i = '*' # ======================================================================= # # === slash # ======================================================================= # when 'slash' i = '/' # ======================================================================= # # === period # ======================================================================= # when 'period' i = '.' end case i when PERCENTAGE_SIGN do_perform_percentage_evaluation(main_entry_text?) else # ===================================================================== # # We will only allow one '.' to be appended. # ===================================================================== # case i when '.' n_dots_in_the_string = main_text.count('.') if n_dots_in_the_string < 1 unless last_char? == '.' @label_results.append(i) end end else @label_results.append(i.to_s) end # @label_results.reposition_the_cursor_to_the_right end # main_entry?.cursor_is_on_the_right end |
#label_results? ⇒ Boolean
#
label_results?
#
556 557 558 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 556 def label_results? @label_results end |
#last_char? ⇒ Boolean
#
last_char?
This method will return the last character of the main-text.
#
549 550 551 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 549 def last_char? main_text?[-1,1] end |
#let_the_label_respond_to_key_press_events ⇒ Object
#
let_the_label_respond_to_key_press_events
#
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 115 def let_the_label_respond_to_key_press_events @label_results.signal_connect(:key_press_event) {|, event| _ = Gdk::Keyval.to_name(event.keyval) case _ when '*', 'x', '/', '-', '+', '.', 'plus', 'minus', 'asterisk', 'slash', 'period', /[0-9]/i input_is(_) else # Ignore these. end } if use_gtk3? end |
#main_entry? ⇒ Boolean Also known as: label_results
#
main_entry?
#
291 292 293 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 291 def main_entry? @label_results end |
#main_text? ⇒ Boolean Also known as: string?, main_entry_text?
#
main_text?
#
88 89 90 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 88 def main_text? @label_results.text? end |
#padding? ⇒ Boolean
#
padding?
#
462 463 464 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 462 def padding? 12 end |
#populate_the_grid ⇒ Object
#
populate_the_grid
#
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 641 def populate_the_grid @grid.left() @grid.left() @grid.left() @grid.left() @grid.right() @grid.left() @grid.left() @grid.left() @grid.left() @grid.right() @grid.left() @grid.left() @grid.left() @grid.right() @grid.left() @grid.left() @grid.left() @grid.left() @grid.left() end |
#reset_the_shared_module ⇒ Object
#
reset_the_shared_module
#
298 299 300 301 302 303 304 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 298 def reset_the_shared_module if defined?(@internal_hash) else @internal_hash = {} end @internal_hash[:buttons] = [] end |
#return_button0 ⇒ Object Also known as: return0
#
return_button0
#
345 346 347 348 349 350 351 352 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 345 def = ('0') .width_height(80,80) .on_clicked { input_is(0) } end |
#return_button1 ⇒ Object Also known as: return1
#
return_button1
#
309 310 311 312 313 314 315 316 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 309 def = ('1') .width_height(80,80) .on_clicked { input_is(1) } end |
#return_button2 ⇒ Object Also known as: return2
#
return_button2
#
321 322 323 324 325 326 327 328 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 321 def = ('2') .width_height(80,80) .on_clicked { input_is(2) } end |
#return_button3 ⇒ Object Also known as: return3
#
return_button3
#
396 397 398 399 400 401 402 403 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 396 def = ('3') .width_height(80,80) .on_clicked { input_is(3) } end |
#return_button4 ⇒ Object Also known as: return4
#
return_button4
#
233 234 235 236 237 238 239 240 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 233 def = ('4') .width_height(80,80) .on_clicked { input_is(4) } return end |
#return_button5 ⇒ Object Also known as: return5
#
return_button5
#
333 334 335 336 337 338 339 340 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 333 def = ('5') .width_height(80,80) .on_clicked { input_is(5) } end |
#return_button6 ⇒ Object Also known as: return6
#
return_button6
#
263 264 265 266 267 268 269 270 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 263 def = ('6') .width_height(80,80) .on_clicked { input_is(6) } end |
#return_button7 ⇒ Object Also known as: return7
#
return_button7
#
422 423 424 425 426 427 428 429 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 422 def = ('7') .width_height(80,80) .on_clicked { input_is(7) } end |
#return_button8 ⇒ Object Also known as: return8
#
return_button8
#
357 358 359 360 361 362 363 364 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 357 def = ('8') .width_height(80,80) .on_clicked { input_is(8) } end |
#return_button9 ⇒ Object Also known as: return9
#
return_button9
#
221 222 223 224 225 226 227 228 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 221 def = ('9') .width_height(80,80) .on_clicked { input_is(9) } end |
#return_button_clear ⇒ Object Also known as:
#
return_button_clear
#
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 275 def # ======================================================================= # # === button_clear # ======================================================================= # = ('_clear') .width_height(80,80) .on_clicked { do_clear_the_main_label } .hint = 'This button will clear the display.' end |
#return_button_divide ⇒ Object
#
return_button_divide
#
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 245 def = ('/') .width_height(80,80) .hint = .on_clicked { # ===================================================================== # # We will only allow one '/' to be appended. # ===================================================================== # unless last_char? == '/' @label_results.append(.text?) end } return end |
#return_button_dot ⇒ Object Also known as: return_dot
#
return_button_dot
#
449 450 451 452 453 454 455 456 457 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 449 def = ('.') .width_height(80,80) .hint = .on_clicked { input_is(.text?) } return end |
#return_button_equal ⇒ Object Also known as: return_equal
#
return_button_equal
#
666 667 668 669 670 671 672 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 666 def = ('=') { :do_evaluate_the_current_text } .width_height(80,80) return end |
#return_button_minus ⇒ Object Also known as: return_minus
#
return_button_minus
#
369 370 371 372 373 374 375 376 377 378 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 369 def = ('-') .width_height(80,80) .on_clicked { unless last_char? == .text? input_is(.text?) end } end |
#return_button_multiply ⇒ Object
#
return_button_multiply
#
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 207 def = ('*') .width_height(80,80) .on_clicked { unless last_char? == '*' input_is('*') end } end |
#return_button_percentage_sign ⇒ Object Also known as: return_percentage
#
return_button_percentage_sign
#
434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 434 def # ======================================================================= # # === % # ======================================================================= # = (PERCENTAGE_SIGN) .width_height(80,80) .on_clicked { input_is(.text?) } end |
#return_button_plus ⇒ Object Also known as: return_plus
#
return_button_plus
#
408 409 410 411 412 413 414 415 416 417 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 408 def = ('+') .width_height(80,80) .on_clicked { unless last_char? == .text? input_is(.text?) end } end |
#return_button_square ⇒ Object Also known as: return_square
#
return_button_square
#
383 384 385 386 387 388 389 390 391 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 383 def = ('sqrt') .hint = 'This will square the input, e. g. 2 ** 2 aka 2².' .width_height(80,80) .on_clicked { do_square_the_result } end |
#return_hint_for_the_division_button ⇒ Object
#
return_hint_for_the_division_button
#
141 142 143 144 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 141 def "This button can be used to <b>divide</b> two values, such as 6 / 3.\n\n"\ "In Unicode this would be the following token: ÷" end |
#return_hint_for_the_dot_button ⇒ Object
#
return_hint_for_the_dot_button
#
149 150 151 152 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 149 def "This button will simply append a '.' character, unless "\ "the last character already is '.'." end |
#return_the_frame_containing_the_primary_box(box = @box) ⇒ Object
#
return_the_frame_containing_the_primary_box
#
611 612 613 614 615 616 617 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 611 def return_the_frame_containing_the_primary_box(box = @box) frame = create_frame frame.set_text(" #{title?} ") frame..use_this_font = :hack_30 frame.add(box) return frame end |
#set_main_text(i) ⇒ Object Also known as: string=, update_entry, set_this_as_the_new_result
#
set_main_text
#
699 700 701 |
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 699 def set_main_text(i) @label_results.set_text(i.to_s) end |