Class: Cosmos::CmdSenderItemDelegate

Inherits:
Qt::StyledItemDelegate
  • Object
show all
Defined in:
lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ CmdSenderItemDelegate

Returns a new instance of CmdSenderItemDelegate.



14
15
16
17
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb', line 14

def initialize(parent)
  @table = parent
  super(parent)
end

Instance Method Details

#createEditor(parent, option, index) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb', line 19

def createEditor(parent, option, index)
  packet_item, _, _ = CmdSender.param_widgets[index.row]
  if index.column == 1 and packet_item and packet_item.states
    combo = Qt::ComboBox.new(parent)
    sorted_states = packet_item.states.sort {|a, b| a[1] <=> b[1]}
    items = sorted_states.map {|state_name, state_value| state_name}
    items << CmdSender::MANUALLY
    combo.addItems(items)
    combo.setCurrentText(@table.item(index.row, index.column).text.to_s)
    combo.setMaxVisibleItems(6)
    connect(combo, SIGNAL('activated(int)')) do
      emit commitData(combo)
      CmdSender.table.closeEditor(combo, 0)
    end
    return combo
  else
    return super(parent, option, index)
  end
end

#paint(painter, option, index) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb', line 39

def paint(painter, option, index)
  packet_item, _, _ = CmdSender.param_widgets[index.row]
  if index.column == 1 and packet_item and packet_item.states
    # This code simply draws the current combo box text inside a button to
    # give the user an idea that they have to click it to activate it
    opt = Qt::StyleOptionButton.new
    opt.rect = option.rect
    opt.text = @table.item(index.row, index.column).text.to_s
    Qt::Application.style.drawControl(Qt::Style::CE_PushButton, opt, painter)
    opt.dispose
  else
    super(painter, option, index)
  end
end

#setEditorData(editor, index) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb', line 62

def setEditorData(editor, index)
  if Qt::ComboBox === editor
    v = index.data(Qt::EditRole)
    combo_index = editor.findText(v.toString)
    if combo_index >= 0
      editor.setCurrentIndex(combo_index)
    else
      editor.setCurrentIndex(0)
    end
  else
    super(editor, index)
  end
end

#setModelData(editor, model, index) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/cosmos/tools/cmd_sender/cmd_sender_item_delegate.rb', line 54

def setModelData(editor, model, index)
  if Qt::ComboBox === editor
    model.setData(index, Qt::Variant.new(editor.currentText), Qt::EditRole)
  else
    super(editor, model, index)
  end
end