Class: Canis::ButtonGroup

Inherits:
Object show all
Defined in:
lib/canis/core/widgets/rwidget.rb

Overview

This is not a visual class or a widget. This class allows us to attach several RadioButtons to it, so it can maintain which one is the selected one. It also allows for assigning of commands to be executed whenever a button is pressed, akin to binding to the fire of the button, except that one would not have to bind to each button, but only once here.

Added on 2014-04-30

Examples:

group = ButtonGroup.new
group.add(r1).add(r2).add(r3)
# change the color of +somelabel+ to the color specified by the value of clicked radio.
group.command(somelabel) do |grp, label| label.color(grp.value); end

Since:

  • 1.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeButtonGroup

Returns a new instance of ButtonGroup.

Since:

  • 1.2.0



3503
3504
3505
3506
# File 'lib/canis/core/widgets/rwidget.rb', line 3503

def initialize 
  @elements = []
  @hash = {}
end

Instance Attribute Details

#elementsObject (readonly)

Array of buttons that have been added.

Since:

  • 1.2.0



3499
3500
3501
# File 'lib/canis/core/widgets/rwidget.rb', line 3499

def elements
  @elements
end

#valueObject (readonly)

the value of the radio button that is selected. To get the button itself, use selection.

Since:

  • 1.2.0



3502
3503
3504
# File 'lib/canis/core/widgets/rwidget.rb', line 3502

def value
  @value
end

Instance Method Details

#add(e) ⇒ Object

Since:

  • 1.2.0



3508
3509
3510
3511
3512
3513
# File 'lib/canis/core/widgets/rwidget.rb', line 3508

def add e
  @elements << e
  @hash[e.value] = e
  e.variable(self)
  self
end

#command(*args, &block) ⇒ Object

install trigger to call whenever a value is updated

Since:

  • 1.2.0



3536
3537
3538
3539
3540
3541
# File 'lib/canis/core/widgets/rwidget.rb', line 3536

def command *args, &block
  @commands ||= []
  @args ||= []
  @commands << block
  @args << args
end

#get_value(name = nil) ⇒ Object

returns the value of the selected button NOTE: This is used by RadioButton class for backward compat with Variable. User programs should use value()

Since:

  • 1.2.0



3569
3570
3571
# File 'lib/canis/core/widgets/rwidget.rb', line 3569

def get_value name=nil
  @value
end

#remove(e) ⇒ Object

Since:

  • 1.2.0



3514
3515
3516
3517
3518
# File 'lib/canis/core/widgets/rwidget.rb', line 3514

def remove e
  @elements.delete e
  @hash.delete e.value
  self
end

#select(button) ⇒ Object

select the given button or value. This may be called by user programs to programmatically select a button

Since:

  • 1.2.0



3544
3545
3546
3547
3548
3549
3550
3551
# File 'lib/canis/core/widgets/rwidget.rb', line 3544

def select button
  if button.is_a? String
    ;
  else
    button = button.value
  end
  set_value button
end

#selected?(val) ⇒ true or false

Returns for wether the given value or button is the selected one.

Parameters:

  • +value+ (String, RadioButton)

    of a button, or Button itself to check if selected.

Returns:

  • (true or false)

    for wether the given value or button is the selected one

Since:

  • 1.2.0



3527
3528
3529
3530
3531
3532
3533
# File 'lib/canis/core/widgets/rwidget.rb', line 3527

def selected? val
  if val.is_a? String
    @value == val
  else
    @hash[@value] == val
  end
end

#selectionObject

Returns the radiobutton that is selected.

Returns:

  • the radiobutton that is selected

Since:

  • 1.2.0



3521
3522
3523
# File 'lib/canis/core/widgets/rwidget.rb', line 3521

def selection
  @hash[@value]
end

#set_value(value, name = nil) ⇒ Object

when a radio button is pressed, it calls set_value giving the value of that radio. it also gives the name (optionally) since Variables can also be passed and be used across groups. Here, since a button group is only for one group, so we discard name. This is used by RadioButton class for backward compat with Variable.

Parameters:

  • value (String)

    (text) of radio button that is selected

Since:

  • 1.2.0



3559
3560
3561
3562
3563
3564
3565
# File 'lib/canis/core/widgets/rwidget.rb', line 3559

def set_value value, name=nil
  @value = value
  return unless @commands
  @commands.each_with_index do |comm, ix|
    comm.call(self, *@args[ix]) unless comm.nil?
  end
end