Class: ActiveItemModel

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

Instance Method Summary collapse

Constructor Details

#initialize(collection, columns = nil) ⇒ ActiveItemModel

Returns a new instance of ActiveItemModel.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 87

def initialize(collection, columns=nil)
    super()
    @collection = collection
    @keys = build_keys([], @collection.first.attributes)
    @keys.inject(@labels = {}) do |labels, k| 
        labels[k] = k.humanize.gsub(/\./, ' ')
        labels 
    end

    @rootItem = TreeItem.new(@labels, @keys)
    @collection.each do |row|
        TreeItem.new(row, @keys, @rootItem)
    end
end

Instance Method Details

#[](row) ⇒ Object



112
113
114
115
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 112

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

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



102
103
104
105
106
107
108
109
110
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 102

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



117
118
119
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 117

def column(name)
    @keys.index name
end

#columnCount(parent) ⇒ Object



121
122
123
124
125
126
127
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 121

def columnCount(parent)
    if parent.valid?
        return parent.internalPointer.columnCount
    else
        return @rootItem.columnCount
    end
end

#data(index, role) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 129

def data(index, role)
    if !index.valid?
        return Qt::Variant.new
    end

    if role != Qt::DisplayRole
        return Qt::Variant.new
    end

    item = index.internalPointer
    return item.data(index.column)
end

#flags(index) ⇒ Object



177
178
179
180
181
182
183
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 177

def flags(index)
    if !index.valid?
        return Qt::ItemIsEnabled
    end

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable
end

#headerData(section, orientation, role) ⇒ Object



185
186
187
188
189
190
191
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 185

def headerData(section, orientation, role)
    if orientation == Qt::Horizontal && role == Qt::DisplayRole
        return Qt::Variant.new(@labels[@keys[section]])
    end

    return Qt::Variant.new
end

#index(row, column, parent) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 193

def index(row, column, parent)
    if !parent.valid?
        parentItem = @rootItem
    else
        parentItem = parent.internalPointer
    end

    @childItem = parentItem.child(row)
    if ! @childItem.nil?
        return createIndex(row, column, @childItem)
    else
        return Qt::ModelIndex.new
    end
end

#parent(index) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 208

def parent(index)
    if !index.valid?
        return Qt::ModelIndex.new
    end

    childItem = index.internalPointer
    parentItem = childItem.parent

    if parentItem == @rootItem
        return Qt::ModelIndex.new
    end

    return createIndex(parentItem.row, 0, parentItem)
end

#rowCount(parent) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 223

def rowCount(parent)
    if !parent.valid?
        parentItem = @rootItem
    else
        parentItem = parent.internalPointer
    end

    return parentItem.childCount
end

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'ext/ruby/qtruby/rails_support/active_item_model.rb', line 142

def setData(index, variant, role=Qt::EditRole)
    if index.valid? and role == Qt::EditRole
        raise "invalid column #{index.column}" if (index.column < 0 ||
            index.column >= @keys.size)

        att = @keys[index.column]
        item = index.internalPointer

        if ! item.itemData.has_key? att
            return false
        end

        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

        att.gsub!(/.*\.(.*)/, '\1')
        # Don't allow the primary key to be changed
        if att == 'id'
            return false
        end

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