Class: Cosmos::ComboboxChooser

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

Overview

This class implements the ComboboxChooser

Constant Summary collapse

COMBOBOX_BUTTON_WIDTH =

Width of the button in the combobox

30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, label_text, items, allow_user_entry: false, compact_combobox: true, color_chooser: false) ⇒ ComboboxChooser

Returns a new instance of ComboboxChooser.



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
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 24

def initialize(
  parent, label_text, items, # required
  allow_user_entry: false, compact_combobox: true, color_chooser: false
)
  super(parent)
  layout = Qt::HBoxLayout.new(self)
  layout.setContentsMargins(0,0,0,0)
  @combo_label = Qt::Label.new(label_text)
  @combo_label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed) unless compact_combobox
  layout.addWidget(@combo_label)
  @combo_value = Qt::ComboBox.new
  @combo_value.setEditable(allow_user_entry)
  string_items = items.map {|item| item.to_s}
  @combo_value.addItems(string_items)
  @color_chooser = color_chooser
  set_colors(string_items) if @color_chooser
  @combo_value.connect(SIGNAL('currentIndexChanged(int)')) {|index| handle_combobox_sel_command(index) }
  if items.length < 20
    @combo_value.setMaxVisibleItems(items.length)
  else
    @combo_value.setMaxVisibleItems(20)
  end
  layout.addStretch if compact_combobox
  layout.addWidget(@combo_value)

  setLayout(layout)
  @sel_command_callback = nil
end

Instance Attribute Details

#sel_command_callbackObject

Optional callback for when the combobox value changes



22
23
24
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 22

def sel_command_callback
  @sel_command_callback
end

Instance Method Details

#floatObject

Returns the selected item as a float



85
86
87
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 85

def float
  @combo_value.currentText.to_f if @combo_value.currentText
end

#handle_combobox_sel_command(index) ⇒ Object

Supports a callback on item changing



72
73
74
75
76
77
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 72

def handle_combobox_sel_command(index)
  if index >= 0
    @sel_command_callback.call(string()) if @sel_command_callback
  end
  0
end

#integerObject

Returns the selected item as an integer



80
81
82
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 80

def integer
  Integer(@combo_value.currentText)
end

#set_current(string) ⇒ Object



67
68
69
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 67

def set_current(string)
  @combo_value.setCurrentText(string.to_s)
end

#stringObject

Returns the selected item as a string



90
91
92
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 90

def string
  @combo_value.currentText
end

#symbolObject

Returns the selected item as a symbol



95
96
97
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 95

def symbol
  @combo_value.currentText.intern if @combo_value.currentText
end

#update_items(items, include_blank = true) ⇒ Object

Changes the items in the combobox and resizes it



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cosmos/gui/choosers/combobox_chooser.rb', line 54

def update_items(items, include_blank = true)
  @combo_value.clearItems
  @combo_value.addItem(' ') if include_blank
  string_items = items.map {|item| item.to_s}
  @combo_value.addItems(string_items)
  set_colors(string_items) if @color_chooser
  if items.length < 20
    @combo_value.setMaxVisibleItems(items.length)
  else
    @combo_value.setMaxVisibleItems(20)
  end
end