class TreeModel

Provides a simple tree model to show how to create and use hierarchical models.

Public Class Methods

new(data, parent = nil) click to toggle source
Calls superclass method
# File examples/itemviews/simpletreemodel/treemodel.rb, line 30
def initialize(data, parent = nil)
    super(parent)
    rootData = []
    rootData << "Title" << "Summary"
    @rootItem = TreeItem.new(rootData)
    setupModelData(data.to_s.split("\n"), @rootItem)
end

Public Instance Methods

columnCount(parent) click to toggle source
# File examples/itemviews/simpletreemodel/treemodel.rb, line 38
def columnCount(parent)
    if parent.valid?
        return parent.internalPointer.columnCount
    else
        return @rootItem.columnCount
        end
end
data(index, role) click to toggle source
# File examples/itemviews/simpletreemodel/treemodel.rb, line 46
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 examples/itemviews/simpletreemodel/treemodel.rb, line 59
def flags(index)
    if !index.valid?
        return Qt::ItemIsEnabled
        end

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable
end
headerData(section, orientation, role) click to toggle source
# File examples/itemviews/simpletreemodel/treemodel.rb, line 67
def headerData(section, orientation, role)
    if orientation == Qt::Horizontal && role == Qt::DisplayRole
        return Qt::Variant.new(@rootItem.data(section))
        end

    return Qt::Variant.new
end
index(row, column, parent) click to toggle source
# File examples/itemviews/simpletreemodel/treemodel.rb, line 75
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 examples/itemviews/simpletreemodel/treemodel.rb, line 90
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 examples/itemviews/simpletreemodel/treemodel.rb, line 105
def rowCount(parent)
    if !parent.valid?
        parentItem = @rootItem
    else
        parentItem = parent.internalPointer
        end

    return parentItem.childCount
end
setupModelData(lines, parent) click to toggle source
# File examples/itemviews/simpletreemodel/treemodel.rb, line 115
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