Class: ActiveTableModel

Inherits:
Qt::AbstractTableModel
  • Object
show all
Defined in:
ext/ruby/qtruby/rails_support/active_table_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, columns = nil) ⇒ ActiveTableModel

Returns a new instance of ActiveTableModel.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 22

def initialize(collection, columns=nil)
    super()
    @collection = collection
    if columns
        if columns.kind_of? Hash
            @keys=columns.keys
            @labels=columns.values
        else
            @keys=columns
        end
    else
        @keys = build_keys([], @collection.first.attributes)
    end
    @labels||=@keys.collect { |k| k.humanize.gsub(/\./, ' ') }
end

Instance Method Details

#[](row) ⇒ Object



57
58
59
60
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 57

def [](row)
    row = row.row if row.is_a?Qt::ModelIndex
    @collection[row]
end

#build_keys(keys, attrs, prefix = "") ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 38

def build_keys(keys, attrs, prefix="")
    attrs.inject(keys) do |cols, a|
        if a[1].respond_to? :attributes
            build_keys(cols, a[1].attributes, prefix + a[0] + ".")
        else
            cols << prefix + a[0]
        end
    end
end

#column(name) ⇒ Object



62
63
64
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 62

def column(name)
    @keys.index name
end

#columnCount(parent) ⇒ Object



52
53
54
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 52

def columnCount(parent)
    @keys.size
end

#data(index, role = Qt::DisplayRole) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 66

def data(index, role=Qt::DisplayRole)
    invalid = Qt::Variant.new
    return invalid unless role == Qt::DisplayRole or role == Qt::EditRole
    item = @collection[index.row]
    return invalid if item.nil?
    raise "invalid column #{index.column}" if (index.column < 0 ||
        index.column >= @keys.size)
    value = eval("item.attributes['%s']" % @keys[index.column].gsub(/\./, "'].attributes['"))
    return Qt::Variant.new(value)
end

#flags(index) ⇒ Object



89
90
91
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 89

def flags(index)
    return Qt::ItemIsEditable | super(index)
end

#headerData(section, orientation, role = Qt::DisplayRole) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 77

def headerData(section, orientation, role=Qt::DisplayRole)
    invalid = Qt::Variant.new
    return invalid unless role == Qt::DisplayRole
    v = case orientation
    when Qt::Horizontal
        @labels[section]
    else
        section
    end
    return Qt::Variant.new(v)
end

#rowCount(parent) ⇒ Object



48
49
50
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 48

def rowCount(parent)
    @collection.size
end

#setData(index, variant, role = Qt::EditRole) ⇒ Object



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
# File 'ext/ruby/qtruby/rails_support/active_table_model.rb', line 93

def setData(index, variant, role=Qt::EditRole)
    if index.valid? and role == Qt::EditRole
        att = @keys[index.column]
        # Don't allow the primary key to be changed
        if att == 'id'
            return false
        end

        item = @collection[index.row]
        raise "invalid column #{index.column}" if (index.column < 0 ||
            index.column >= @keys.size)
        value = variant.value

        if value.class.name == "Qt::Date"
            value = Date.new(value.year, value.month, value.day)
        elsif value.class.name == "Qt::Time"
            value = Time.new(value.hour, value.min, value.sec)
        end

        eval("item['%s'] = value" % att.gsub(/\./, "']['"))
        item.save
        emit dataChanged(index, index)
        return true
    else
        return false
    end
end