Class: Cosmos::IntegerChooser

Inherits:
Qt::Widget show all
Defined in:
lib/cosmos/gui/choosers/integer_chooser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, label_text, initial_value, minimum_value = nil, maximum_value = nil, field_width = 20, fill = false) ⇒ IntegerChooser

Returns a new instance of IntegerChooser.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cosmos/gui/choosers/integer_chooser.rb', line 20

def initialize(
  parent, label_text, initial_value,
  minimum_value = nil, maximum_value = nil, field_width = 20, fill = false
)
  super(parent)
  @minimum_value = minimum_value
  @maximum_value = maximum_value

  layout = Qt::HBoxLayout.new(self)
  layout.setContentsMargins(0,0,0,0)

  @integer_label = Qt::Label.new(label_text)
  @integer_label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed) if fill
  layout.addWidget(@integer_label)
  layout.addStretch() unless fill
  @integer_value = Qt::LineEdit.new(initial_value.to_s)
  @integer_value.setMinimumWidth(field_width)
  if minimum_value or maximum_value
    validator = Qt::IntValidator.new(@integer_value)
    validator.setBottom(minimum_value) if minimum_value
    validator.setTop(minimum_value) if maximum_value
    @integer_value.setValidator(validator)
  end
  @callback_in_progress = false
  @integer_value.connect(SIGNAL('editingFinished()')) do
    unless @callback_in_progress # Prevent double fire on loss of focus
      begin
        @callback_in_progress = true
        @sel_command_callback.call(string(), value()) if @sel_command_callback
      ensure
        @callback_in_progress = false
      end
    end
  end
  layout.addWidget(@integer_value)
  setLayout(layout)

  @sel_command_callback = nil
end

Instance Attribute Details

#sel_command_callbackObject

Callback called when the value changes



18
19
20
# File 'lib/cosmos/gui/choosers/integer_chooser.rb', line 18

def sel_command_callback
  @sel_command_callback
end

Instance Method Details

#stringObject

Returns the value as a string



61
62
63
# File 'lib/cosmos/gui/choosers/integer_chooser.rb', line 61

def string
  @integer_value.text
end

#valueObject

Returns the value as an integer



66
67
68
69
70
71
# File 'lib/cosmos/gui/choosers/integer_chooser.rb', line 66

def value
  integer_value = @integer_value.text.to_i
  integer_value = @minimum_value if @minimum_value and integer_value < @minimum_value
  integer_value = @maximum_value if @maximum_value and integer_value > @maximum_value
  integer_value
end

#value=(new_value) ⇒ Object

Sets the value



74
75
76
# File 'lib/cosmos/gui/choosers/integer_chooser.rb', line 74

def value=(new_value)
  @integer_value.setText(new_value.to_s)
end