Class: Cosmos::TelemetryChooser

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

Overview

Provides dropdowns to select from all the defined Targets, Packets, and Items. Callbacks can be added when any of the dropdown values are changed as well as when the select button is pressed. Provides an option to orient the dropdowns horizontally or vertically.

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, orientation = Qt::Horizontal, choose_item = true, select_button = true, support_latest = false, fill = false) ⇒ TelemetryChooser

Returns a new instance of TelemetryChooser.

Parameters:

  • parent (Qt::Widget)

    Parent of this widget

  • orientation (Integer) (defaults to: Qt::Horizontal)

    Must be Qt::Horizontal or Qt::Vertical

  • choose_item (Boolean) (defaults to: true)

    Whether to allow choosing items. Choosing targets and packets are alway enabled.

  • select_button (Boolean) (defaults to: true)

    Whether to display the "Select" button. You can disable the button if you just want to implement the dropdown change callbacks.

  • support_latest (Boolean) (defaults to: false)

    Whether to add LATEST to the list of packets



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 55

def initialize(
  parent,
  orientation = Qt::Horizontal,
  choose_item = true,
  select_button = true,
  support_latest = false,
  fill = false
)

  super(parent)
  if orientation == Qt::Horizontal
    # Horizontal Frame for overall widget
    @overall_frame = Qt::HBoxLayout.new(self)
  else
    # Vertical Frame for overall widget
    @overall_frame = Qt::VBoxLayout.new(self)
  end
  @overall_frame.setContentsMargins(0,0,0,0)

  # Target Selection
  @target_layout = Qt::HBoxLayout.new
  @target_label = Qt::Label.new('Target:')
  @target_label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed) if fill
  @target_layout.addWidget(@target_label)
  @target_layout.addStretch unless fill
  @target_combobox = Qt::ComboBox.new
  # This allows the comboboxes to automatically adjust when we change the contents - awesome!
  @target_combobox.setSizeAdjustPolicy(Qt::ComboBox::AdjustToContents)
  @target_combobox.connect(SIGNAL('activated(int)')) do
    update_packets()
    @target_changed_callback.call(target_name()) if @target_changed_callback
  end
  @target_layout.addWidget(@target_combobox)
  @overall_frame.addLayout(@target_layout)

  # Packet Selection
  @packet_layout = Qt::HBoxLayout.new
  @packet_label = Qt::Label.new('Packet:')
  @packet_label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed) if fill
  @packet_layout.addWidget(@packet_label)
  @packet_layout.addStretch unless fill
  @packet_combobox = Qt::ComboBox.new
  @packet_combobox.setSizeAdjustPolicy(Qt::ComboBox::AdjustToContents)
  @packet_combobox.connect(SIGNAL('activated(int)')) do
    update_items()
    @packet_changed_callback.call(target_name(), packet_name()) if @packet_changed_callback
  end
  @packet_layout.addWidget(@packet_combobox)
  @overall_frame.addLayout(@packet_layout)

  if choose_item
    # Item Selection
    @item_layout = Qt::HBoxLayout.new
    @item_label = Qt::Label.new('Item:')
    @item_label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed) if fill
    @item_layout.addWidget(@item_label)
    @item_layout.addStretch unless fill
    @item_combobox = Qt::ComboBox.new
    @item_combobox.setSizeAdjustPolicy(Qt::ComboBox::AdjustToContents)
    @item_combobox.connect(SIGNAL('activated(int)')) do
      @item_changed_callback.call(target_name(), packet_name(), item_name()) if @item_changed_callback
    end
    @item_layout.addWidget(@item_combobox)
    @overall_frame.addLayout(@item_layout)
  end

  if select_button
    # Select Button
    #@overall_frame.addStretch()
    @select_button = Qt::PushButton.new('Select')
    @select_button.connect(SIGNAL('clicked()')) do
      @select_button_callback.call(target_name(), packet_name(), item_name()) if @select_button_callback
    end
    @overall_frame.addWidget(@select_button)
  end

  # Initialize instance variables
  @target_changed_callback = nil
  @packet_changed_callback = nil
  @item_changed_callback = nil
  @select_button_callback = nil
  @support_latest = support_latest
  update()
end

Instance Attribute Details

#item_changed_callbackObject

Callback called when the item is changed - call(target_name, packet_name, item_name)



33
34
35
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 33

def item_changed_callback
  @item_changed_callback
end

#item_labelObject

Label for the item



45
46
47
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 45

def item_label
  @item_label
end

#packet_changed_callbackObject

Callback called when the packet is changed - call(target_name, packet_name)



30
31
32
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 30

def packet_changed_callback
  @packet_changed_callback
end

#packet_labelObject

Label for the packet



42
43
44
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 42

def packet_label
  @packet_label
end

#select_button_callbackObject

Callback called when the select button is pressed - call(target_name, packet_name, item_name)



36
37
38
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 36

def select_button_callback
  @select_button_callback
end

#target_changed_callbackObject

Callback called when the target is changed - call(target_name)



27
28
29
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 27

def target_changed_callback
  @target_changed_callback
end

#target_labelObject

Label for the target



39
40
41
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 39

def target_label
  @target_label
end

Instance Method Details

#button_text=(button_text) ⇒ Object

Set the text of the button



156
157
158
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 156

def button_text=(button_text)
  @select_button.setText(button_text)
end

#item_nameObject

Returns the selected item name



171
172
173
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 171

def item_name
  @item_combobox.text
end

#item_namesObject

Returns the list of all item names in the item combobox



190
191
192
193
194
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 190

def item_names
  item_names_array = []
  @item_combobox.each {|item_text, item_data| item_names_array << item_text}
  item_names_array
end

#packet_nameObject

Returns the selected packet name



166
167
168
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 166

def packet_name
  @packet_combobox.text
end

#packet_namesObject

Returns the list of all packet names in the packet combobox



183
184
185
186
187
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 183

def packet_names
  packet_names_array = []
  @packet_combobox.each {|item_text, item_data| packet_names_array << item_text}
  packet_names_array
end

#set_item(target_name, packet_name, item_name) ⇒ Object

Sets the current item



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 228

def set_item(target_name, packet_name, item_name)
  # Set the desired packet
  set_packet(target_name, packet_name)

  # Select desired item
  index = 0
  found = false
  @item_combobox.each do |item_text, item_data|
    if item_name.upcase == item_text.upcase
      found = true
      break
    end
    index += 1
  end
  Kernel.raise "TelemetryChooser unknown item_name #{item_name}" unless found
  @item_combobox.setCurrentIndex(index)
end

#set_packet(target_name, packet_name) ⇒ Object

Sets the current packet



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 197

def set_packet(target_name, packet_name)
  # Select desired target
  index = 0
  found = false
  @target_combobox.each do |item_text, item_data|
    if target_name.upcase == item_text.upcase
      found = true
      break
    end
    index += 1
  end
  Kernel.raise "TelemetryChooser unknown target_name #{target_name}" unless found
  @target_combobox.setCurrentIndex(index)
  update_packets()

  # Select desired packet
  index = 0
  found = false
  @packet_combobox.each do |item_text, item_data|
    if packet_name.upcase == item_text.upcase
      found = true
      break
    end
    index += 1
  end
  Kernel.raise "TelemetryChooser unknown packet_name #{packet_name}" unless found
  @packet_combobox.setCurrentIndex(index)
  update_items()
end

#target_nameObject

Returns the selected target name



161
162
163
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 161

def target_name
  @target_combobox.text
end

#target_namesObject

Returns the list of all target names in the target combobox



176
177
178
179
180
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 176

def target_names
  target_names_array = []
  @target_combobox.each {|item_text, item_data| target_names_array << item_text}
  target_names_array
end

#updateObject

Update items



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cosmos/gui/choosers/telemetry_chooser.rb', line 141

def update
  current_target_name = target_name()
  current_packet_name = packet_name()
  current_item_name = item_name()
  update_targets()
  if current_target_name and current_packet_name and current_item_name
    begin
      set_item(current_target_name, current_packet_name, current_item_name)
    rescue Exception
      # Oh well - Tried to keep the same item
    end
  end
end