Class: Cosmos::FloatChooser

Inherits:
Qt::Widget show all
Defined in:
lib/cosmos/gui/choosers/float_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) ⇒ FloatChooser

Returns a new instance of FloatChooser.



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
59
60
# File 'lib/cosmos/gui/choosers/float_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
  @field_width   = field_width

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

  @float_label = Qt::Label.new(label_text)
  @float_label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed) if fill
  layout.addWidget(@float_label)
  layout.addStretch unless fill
  @float_value = Qt::LineEdit.new(initial_value.to_s)
  @float_value.setMinimumWidth(field_width)
  if minimum_value or maximum_value
    validator = Qt::DoubleValidator.new(@float_value)
    validator.setBottom(minimum_value) if minimum_value
    validator.setTop(maximum_value) if maximum_value
    validator.setNotation(Qt::DoubleValidator::StandardNotation)
    @float_value.setValidator(validator)
  end
  @callback_in_progress = false
  @float_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(@float_value)
  setLayout(layout)

  @sel_command_callback = nil
end

Instance Attribute Details

#sel_command_callbackObject

Callback for a new value entered into the text field



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

def sel_command_callback
  @sel_command_callback
end

Instance Method Details

#stringObject

Returns the value as a string



63
64
65
# File 'lib/cosmos/gui/choosers/float_chooser.rb', line 63

def string
  @float_value.text
end

#valueObject

Returns the value as a float



68
69
70
71
72
73
# File 'lib/cosmos/gui/choosers/float_chooser.rb', line 68

def value
  float_value = @float_value.text.to_f
  float_value = @minimum_value if @minimum_value and float_value < @minimum_value
  float_value = @maximum_value if @maximum_value and float_value > @maximum_value
  float_value
end

#value=(new_value) ⇒ Object

Sets the value of the text field



76
77
78
# File 'lib/cosmos/gui/choosers/float_chooser.rb', line 76

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