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

Instance Method Details

#border_size?Boolean

#

border_size?

#

Returns:

  • (Boolean)


469
470
471
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 469

def border_size?
  2
end

#box?Boolean

#

box?

#

Returns:

  • (Boolean)


622
623
624
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 622

def box?
  @box
end

#create_the_gridObject

#

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_resultsObject

#

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_boxObject

#

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_labelObject 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_mannerObject

#

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 do_style_all_buttons_in_a_uniform_manner
  return_all_buttons?.each {|this_button|
    this_button.remove_background
    this_button.on_hover(:lightgreen)
  }
end

#handle_CSS_rulesObject

#

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?

#

Returns:

  • (Boolean)


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.

#

Returns:

  • (Boolean)


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_eventsObject

#

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) {|widget, 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?

#

Returns:

  • (Boolean)


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?

#

Returns:

  • (Boolean)


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?

#

Returns:

  • (Boolean)


462
463
464
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 462

def padding?
  12
end

#populate_the_gridObject

#

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(return_button1)
  @grid.left(return_button2)
  @grid.left(return_button3)
  @grid.left(return_button_plus)
  @grid.right(return_button_square)
  @grid.left(return_button4)
  @grid.left(return_button5)
  @grid.left(return_button6)
  @grid.left(return_button_minus)
  @grid.right(return_button_percentage_sign)
  @grid.left(return_button7)
  @grid.left(return_button8)
  @grid.left(return_button9)
  @grid.right(return_button_multiply)
  @grid.left(return_button0)
  @grid.left(return_button_clear)
  @grid.left(return_button_dot)
  @grid.left(return_button_divide)
  @grid.left(return_button_equal)
end

#reset_the_shared_moduleObject

#

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_button0Object 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 return_button0
  button0 = button('0')
  button0.width_height(80,80)
  button0.on_clicked {
    input_is(0)
  }
  button0
end

#return_button1Object 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 return_button1
  button1 = button('1')
  button1.width_height(80,80)
  button1.on_clicked {
    input_is(1)
  }
  button1
end

#return_button2Object 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 return_button2
  button2 = button('2')
  button2.width_height(80,80)
  button2.on_clicked {
    input_is(2)
  }
  button2
end

#return_button3Object 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 return_button3
  button3 = button('3')
  button3.width_height(80,80)
  button3.on_clicked {
    input_is(3)
  }
  button3
end

#return_button4Object 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 return_button4
  button4 = button('4')
  button4.width_height(80,80)
  button4.on_clicked {
    input_is(4)
  }
  return button4
end

#return_button5Object 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 return_button5
  button5 = button('5')
  button5.width_height(80,80)
  button5.on_clicked {
    input_is(5)
  }
  button5
end

#return_button6Object 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 return_button6
  button6 = button('6')
  button6.width_height(80,80)
  button6.on_clicked {
    input_is(6)
  }
  button6
end

#return_button7Object 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 return_button7
  button7 = button('7')
  button7.width_height(80,80)
  button7.on_clicked {
    input_is(7)
  }
  button7
end

#return_button8Object 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 return_button8
  button8 = button('8')
  button8.width_height(80,80)
  button8.on_clicked {
    input_is(8)
  }
  button8
end

#return_button9Object 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 return_button9
  button9 = button('9')
  button9.width_height(80,80)
  button9.on_clicked {
    input_is(9)
  }
  button9
end

#return_button_clearObject Also known as: button_clear?

#

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 return_button_clear
  # ======================================================================= #
  # === button_clear
  # ======================================================================= #
  button_clear = button('_clear')
  button_clear.width_height(80,80)
  button_clear.on_clicked {
    do_clear_the_main_label
  }
  button_clear.hint = 'This button will clear the display.'
  button_clear
end

#return_button_divideObject

#

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 return_button_divide
  button_divide = button('/')
  button_divide.width_height(80,80)
  button_divide.hint = return_hint_for_the_division_button
  button_divide.on_clicked {
    # ===================================================================== #
    # We will only allow one '/' to be appended.
    # ===================================================================== #
    unless last_char? == '/'
      @label_results.append(button_divide.text?)
    end
  }
  return button_divide
end

#return_button_dotObject 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 return_button_dot
  button_dot = button('.')
  button_dot.width_height(80,80)
  button_dot.hint = return_hint_for_the_dot_button
  button_dot.on_clicked {
    input_is(button_dot.text?)
  }
  return button_dot
end

#return_button_equalObject 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 return_button_equal
  button_equal = button('=') {
    :do_evaluate_the_current_text
  }
  button_equal.width_height(80,80)
  return button_equal
end

#return_button_minusObject 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 return_button_minus
  button_minus = button('-')
  button_minus.width_height(80,80)
  button_minus.on_clicked {
    unless last_char? == button_minus.text?
      input_is(button_minus.text?)
    end
  }
  button_minus
end

#return_button_multiplyObject

#

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 return_button_multiply
  button_multiply = button('*')
  button_multiply.width_height(80,80)
  button_multiply.on_clicked {
    unless last_char? == '*'
      input_is('*')
    end
  }
  button_multiply
end

#return_button_percentage_signObject 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 return_button_percentage_sign
  # ======================================================================= #
  # === %
  # ======================================================================= #
  button_percentage_sign = button(PERCENTAGE_SIGN)
  button_percentage_sign.width_height(80,80)
  button_percentage_sign.on_clicked {
    input_is(button_percentage_sign.text?)
  }
  button_percentage_sign
end

#return_button_plusObject 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 return_button_plus
  button_plus = button('+')
  button_plus.width_height(80,80)
  button_plus.on_clicked {
    unless last_char? == button_plus.text?
      input_is(button_plus.text?)
    end
  }
  button_plus
end

#return_button_squareObject 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 return_button_square
  button_square = button('sqrt')
  button_square.hint = 'This will square the input, e. g. 2 ** 2 aka 2².'
  button_square.width_height(80,80)
  button_square.on_clicked {
    do_square_the_result
  }
  button_square
end

#return_hint_for_the_division_buttonObject

#

return_hint_for_the_division_button

#


141
142
143
144
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 141

def return_hint_for_the_division_button
  "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_buttonObject

#

return_hint_for_the_dot_button

#


149
150
151
152
# File 'lib/roebe/gui/shared_code/calculator/calculator_module.rb', line 149

def return_hint_for_the_dot_button
  "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.label_widget.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