class PiecesList

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/draganddrop/puzzle/pieceslist.rb, line 29
def initialize(parent = nil)
    super(parent)
    setDragEnabled(true)
    setViewMode(Qt::ListView::IconMode)
    setIconSize(Qt::Size.new(60, 60))
    setSpacing(10)
    setAcceptDrops(true)
    setDropIndicatorShown(true)
end

Public Instance Methods

addPiece(pixmap, location) click to toggle source
# File examples/draganddrop/puzzle/pieceslist.rb, line 65
def addPiece(pixmap, location)
    pieceItem = Qt::ListWidgetItem.new(self)
    pieceItem.icon = Qt::Icon.new(pixmap)
    pieceItem.setData(Qt::UserRole, qVariantFromValue(pixmap))
    pieceItem.setData(Qt::UserRole.to_i + 1, Qt::Variant.new(location))
    pieceItem.setFlags( Qt::ItemIsEnabled.to_i | Qt::ItemIsSelectable.to_i |
                        Qt::ItemIsDragEnabled.to_i )
end
dragMoveEvent(event) click to toggle source
# File examples/draganddrop/puzzle/pieceslist.rb, line 39
def dragMoveEvent(event)
    if event.mimeData().hasFormat("image/x-puzzle-piece")
        event.dropAction = Qt::MoveAction
        event.accept()
    else
        event.ignore()
    end
end
dropEvent(event) click to toggle source
# File examples/draganddrop/puzzle/pieceslist.rb, line 48
def dropEvent(event)
    if event.mimeData().hasFormat("image/x-puzzle-piece")
        pieceData = event.mimeData().data("image/x-puzzle-piece")
        dataStream = Qt::DataStream.new(pieceData, Qt::IODevice::ReadOnly.to_i)
        pixmap = Qt::Pixmap.new
        location = Qt::Point.new
        dataStream >> pixmap >> location

        addPiece(pixmap, location)

        event.dropAction = Qt::MoveAction
        event.accept()
    else
        event.ignore()
            end
end
startDrag(supportedActions) click to toggle source
# File examples/draganddrop/puzzle/pieceslist.rb, line 74
def startDrag(supportedActions)
    item = currentItem()
    itemData = Qt::ByteArray.new("")
    dataStream = Qt::DataStream.new(itemData, Qt::IODevice::WriteOnly.to_i)
    pixmap = qVariantValue(Qt::Pixmap, item.data(Qt::UserRole))
    location = item.data(Qt::UserRole+1).toPoint()

    dataStream << pixmap << location

    mimeData = Qt::MimeData.new
    mimeData.setData("image/x-puzzle-piece", itemData)

    drag = Qt::Drag.new(self)
    drag.mimeData = mimeData
    drag.hotSpot = Qt::Point.new(pixmap.width/2, pixmap.height/2)
    drag.pixmap = pixmap

    if drag.start(Qt::MoveAction.to_i) == Qt::MoveAction
        takeItem(row(item)).dispose
    end
end