Class: PiecesList

Inherits:
Qt::ListWidget
  • Object
show all
Defined in:
ext/ruby/qtruby/examples/draganddrop/puzzle/pieceslist.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ PiecesList

Returns a new instance of PiecesList.



29
30
31
32
33
34
35
36
37
# File 'ext/ruby/qtruby/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

Instance Method Details

#addPiece(pixmap, location) ⇒ Object



65
66
67
68
69
70
71
72
# File 'ext/ruby/qtruby/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) ⇒ Object



39
40
41
42
43
44
45
46
# File 'ext/ruby/qtruby/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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/ruby/qtruby/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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/ruby/qtruby/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