Class: TreeModel
- Defined in:
- ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb,
ext/ruby/qtruby/examples/itemviews/simpletreemodel/treemodel.rb
Overview
Provides a simple tree model to show how to create and use hierarchical models.
Instance Method Summary collapse
- #columnCount(parent) ⇒ Object
- #data(index, role) ⇒ Object
- #flags(index) ⇒ Object
- #headerData(section, orientation, role) ⇒ Object
- #index(row, column, parent) ⇒ Object
-
#initialize(data, parent = nil) ⇒ TreeModel
constructor
A new instance of TreeModel.
- #parent(index) ⇒ Object
- #rowCount(parent) ⇒ Object
- #setupModelData(lines, parent) ⇒ Object
Constructor Details
#initialize(data, parent = nil) ⇒ TreeModel
Returns a new instance of TreeModel.
32 33 34 35 36 37 38 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 32 def initialize(data, parent = nil) super(parent) rootData = [] rootData << Qt::Variant.new("Title") << Qt::Variant.new("Summary") @rootItem = TreeItem.new(rootData) setupModelData(data.to_s.split("\n"), @rootItem) end |
Instance Method Details
#columnCount(parent) ⇒ Object
40 41 42 43 44 45 46 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 40 def columnCount(parent) if parent.valid? return parent.internalPointer.columnCount else return @rootItem.columnCount end end |
#data(index, role) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 48 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 Qt::Variant.new(item.data(index.column)) end |
#flags(index) ⇒ Object
61 62 63 64 65 66 67 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 61 def flags(index) if !index.valid? return Qt::ItemIsEnabled end return Qt::ItemIsEnabled | Qt::ItemIsSelectable end |
#headerData(section, orientation, role) ⇒ Object
69 70 71 72 73 74 75 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 69 def headerData(section, orientation, role = Qt::DisplayRole) if orientation == Qt::Horizontal && role == Qt::DisplayRole return @rootItem.data(section) end return Qt::Variant.new end |
#index(row, column, parent) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 77 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 92 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
107 108 109 110 111 112 113 114 115 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 107 def rowCount(parent) if !parent.valid? parentItem = @rootItem else parentItem = parent.internalPointer end return parentItem.childCount end |
#setupModelData(lines, parent) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'ext/ruby/qtruby/examples/itemviews/sortingmodel/treemodel.rb', line 117 def setupModelData(lines, parent) parents = [] indentations = [] parents << parent indentations << 0 number = 0 while number < lines.length position = 0 while position < lines[number].length if lines[number][position, 1] != " " break end position += 1 end lineData = lines[number][position, lines[number].length].strip if !lineData.empty? # Read the column data from the rest of the line. columnStrings = lineData.split("\t").delete_if {|item| item == ""} columnData = [] for column in 0...columnStrings.length columnData << columnStrings[column] end if position > indentations.last # The last child of the current parent is now the parent.new # unless the current parent has no children. if parents.last.childCount > 0 parents << parents.last.child(parents.last.childCount - 1) indentations << position end else while position < indentations.last && parents.length > 0 parents.pop indentations.pop end end # Append a item.new to the current parent's list of children. parents.last.appendChild(TreeItem.new(columnData, parents.last)) end number += 1 end end |