class ActiveItemModel
Public Class Methods
new(collection, columns=nil)
click to toggle source
Calls superclass method
# 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
Public Instance Methods
[](row)
click to toggle source
# 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="")
click to toggle source
# 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)
click to toggle source
# File ext/ruby/qtruby/rails_support/active_item_model.rb, line 117 def column(name) @keys.index name end
columnCount(parent)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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