class PiecesModel

Constants

RAND_MAX

Public Class Methods

new(parent) click to toggle source
Calls superclass method
# File examples/itemviews/puzzle/piecesmodel.rb, line 30
def initialize(parent)
    super(parent)
        @locations = []
        @pixmaps = []
end

Public Instance Methods

addPiece(pixmap, location) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 53
def addPiece(pixmap, location)
        if (2.0*rand(RAND_MAX)/(RAND_MAX+1.0)).to_i == 1
        row = 0
    else
        row = @pixmaps.size
        end

    beginInsertRows(Qt::ModelIndex.new, row, row)
    @pixmaps[row] = pixmap
    @locations[row] = location
    endInsertRows()
end
data(index, role) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 36
def data(index, role)
    if !index.valid?
        return Qt::Variant.new
        end

    if role == Qt::DecorationRole
        return qVariantFromValue(Qt::Icon.new(@pixmaps[index.row].scaled(60, 60,
                         Qt::KeepAspectRatio, Qt::SmoothTransformation)))
    elsif role == Qt::UserRole
        return qVariantFromValue(@pixmaps[index.row])
    elsif role == Qt::UserRole + 1
        return qVariantFromValue(@locations[index.row])
        end

    return Qt::Variant.new
end
dropMimeData(data, action, row, column, parent) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 123
def dropMimeData(data, action, row, column, parent)
    if !data.hasFormat("image/x-puzzle-piece")
        return false
        end

    if action == Qt::IgnoreAction
        return true
        end

    if column > 0
        return false
        end

    if !parent.valid? && row < 0
        endRow = @pixmaps.size
    elsif !parent.valid?
        endRow = [row, pixmaps.size()].min
    else
        endRow = parent.row
        end

    encodedData = data.data("image/x-puzzle-piece")
    stream = Qt::DataStream.new(encodedData, Qt::IODevice::ReadOnly)

    while !stream.atEnd
        pixmap = Qt::Pixmap.new
        location = Qt::Point.new
        stream >> pixmap >> location

        beginInsertRows(Qt::ModelIndex.new, endRow, endRow)
        @pixmaps.insert(endRow, pixmap)
        @locations.insert(endRow, location)
        endInsertRows()

        endRow += 1
    end

    return true
end
flags(index) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 66
def flags(index)
    if index.valid?
        return (Qt::ItemIsEnabled | Qt::ItemIsSelectable |
                Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled)
    end

    return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled
end
mimeData(indexes) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 105
def mimeData(indexes)
    mimeData = Qt::MimeData.new
    encodedData = Qt::ByteArray.new

    stream = Qt::DataStream.new(encodedData, Qt::IODevice::WriteOnly)

    indexes.each do |index|
        if index.valid?
            pixmap = qVariantValue(Qt::Pixmap, data(index, Qt::UserRole))
            location = data(index, Qt::UserRole+1).toPoint
            stream << pixmap << location
        end
    end

    mimeData.setData("image/x-puzzle-piece", encodedData)
    return mimeData
end
mimeTypes() click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 99
def mimeTypes()
    types = []
    types << "image/x-puzzle-piece"
    return types
end
removeRows(row, count, parent) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 75
def removeRows(row, count, parent)
    if parent.valid?
        return false
        end

    if row >= @pixmaps.size() || row + count <= 0
        return false
        end

    beginRow = [0, row].max
    endRow = [row + count - 1, @pixmaps.size() - 1].min

    beginRemoveRows(parent, beginRow, endRow)

    while beginRow <= endRow
        @pixmaps.delete(beginRow)
        @locations.delete(beginRow)
        beginRow += 1
    end

    endRemoveRows()
    return true
end
rowCount(parent) click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 163
def rowCount(parent)
    if parent.valid?
        return 0
    else
        return @pixmaps.size
        end
end
supportedDropActions() click to toggle source
# File examples/itemviews/puzzle/piecesmodel.rb, line 171
def supportedDropActions
    return Qt::CopyAction | Qt::MoveAction
end