class PuzzleWidget

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 31
def initialize(parent = nil)
    super(parent)
    setAcceptDrops(true)
    setMinimumSize(400, 400)
    setMaximumSize(400, 400)

    @pieceLocations = []
    @piecePixmaps = []
    @pieceRects = []
end

Public Instance Methods

clear() click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 42
def clear()
    @pieceLocations.clear()
    @piecePixmaps.clear()
    @pieceRects.clear()
    @highlightedRect = Qt::Rect.new()
    @inPlace = 0
    update()
end
dragEnterEvent(event) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 51
def dragEnterEvent(event)
if event.mimeData.hasFormat("image/x-puzzle-piece")
        event.accept
else
        event.ignore
        end
end
dragLeaveEvent(event) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 59
def dragLeaveEvent(event)
    updateRect = @highlightedRect
    @highlightedRect = Qt::Rect.new()
    update(updateRect)
    event.accept()
end
dragMoveEvent(event) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 66
def dragMoveEvent(event)
    updateRect = @highlightedRect.unite(targetSquare(event.pos()))

    if event.mimeData().hasFormat("image/x-puzzle-piece") &&
        findPiece(targetSquare(event.pos)) == -1

        @highlightedRect = targetSquare(event.pos())
        event.dropAction = Qt::MoveAction
        event.accept()
    else
        @highlightedRect = Qt::Rect.new()
        event.ignore()
    end

    update(updateRect)
end
dropEvent(event) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 83
def dropEvent(event)
    if event.mimeData().hasFormat("image/x-puzzle-piece") &&
        findPiece(targetSquare(event.pos)) == -1

        pieceData = event.mimeData().data("image/x-puzzle-piece")
        dataStream = Qt::DataStream.new(pieceData, Qt::IODevice::ReadOnly.to_i)
        square = targetSquare(event.pos)
        pixmap = Qt::Pixmap.new
        location = Qt::Point.new
        dataStream >> pixmap >> location

        @pieceLocations.push location
        @piecePixmaps.push pixmap
        @pieceRects.push square

        @highlightedRect = Qt::Rect.new()
        update(square)

        event.dropAction = Qt::MoveAction
        event.accept()

        if location == Qt::Point.new(square.x()/80, square.y()/80)
            @inPlace += 1
            if @inPlace == 25
                emit puzzleCompleted()
            end
        end
    else
        @highlightedRect = Qt::Rect.new()
        event.ignore()
    end
end
findPiece(pieceRect) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 116
def findPiece(pieceRect)
    (0...@pieceRects.size).each do |i|
        if pieceRect == @pieceRects[i]
            return i
        end
    end
    return -1
end
mousePressEvent(event) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 125
def mousePressEvent(event)
    square = targetSquare(event.pos())
    found = findPiece(square)

    if found == -1
        return
    end

    location = @pieceLocations[found]
    pixmap = @piecePixmaps[found]
    @pieceLocations.delete_at(found)
    @piecePixmaps.delete_at(found)
    @pieceRects.delete_at(found)

    if location == Qt::Point.new(square.x()/80, square.y()/80)
        @inPlace -= 1
    end

    update(square)

    itemData = Qt::ByteArray.new("")
    dataStream = Qt::DataStream.new(itemData, Qt::IODevice::WriteOnly.to_i)

    dataStream << pixmap << location

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

    drag = Qt::Drag.new(self)
    drag.mimeData = mimeData
    drag.hotSpot = event.pos - square.topLeft()
    drag.pixmap = pixmap

    if !drag.start(Qt::MoveAction.to_i) == Qt::MoveAction
        @pieceLocations.insert(found, location)
        @piecePixmaps.insert(found, pixmap)
        @pieceRects.insert(found, square)
        update(targetSquare(event.pos()))

                    if (location == Qt::Point.new(square.x()/80, square.y()/80))
                            @inPlace += 1
                    end
    end
end
paintEvent(event) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 170
def paintEvent(event)
    painter = Qt::Painter.new
    painter.begin(self)
    painter.fillRect(event.rect(), Qt::Brush.new(Qt::white))

    if @highlightedRect.isValid()
        painter.brush = Qt::Brush.new(Qt::Color.new("#ffcccc"))
        painter.pen = Qt::NoPen
        painter.drawRect(@highlightedRect.adjusted(0, 0, -1, -1))
    end

    (0...@pieceRects.size).each do |i|
        painter.drawPixmap(@pieceRects[i], @piecePixmaps[i])
    end
    painter.end
end
targetSquare(position) click to toggle source
# File examples/draganddrop/puzzle/puzzlewidget.rb, line 187
 def targetSquare(position)
    return Qt::Rect.new(position.x()/80 * 80, position.y()/80 * 80, 80, 80)
end