Class: PuzzleWidget

Inherits:
Qt::Widget show all
Defined in:
ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb,
ext/ruby/qtruby/examples/draganddrop/puzzle/puzzlewidget.rb

Instance Method Summary collapse

Methods inherited from Qt::Widget

#raise

Methods inherited from Qt::Base

#%, #&, #*, #**, #+, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #QCOMPARE, #QEXPECT_FAIL, #QFAIL, #QSKIP, #QTEST, #QVERIFY, #QVERIFY2, #QWARN, #^, ancestors, #is_a?, #methods, private_slots, #protected_methods, #public_methods, q_classinfo, q_signal, q_slot, signals, #singleton_methods, slots, #|, #~

Constructor Details

#initialize(parent = nil) ⇒ PuzzleWidget

Returns a new instance of PuzzleWidget.



31
32
33
34
35
36
37
38
39
40
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 31

def initialize(parent = nil)
    super(parent)
    setAcceptDrops(true)
    setMinimumSize(400, 400)
    setMaximumSize(400, 400)

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

Instance Method Details

#clearObject



42
43
44
45
46
47
48
49
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 42

def clear()
    @pieceLocations.clear()
    @piecePixmaps.clear()
    @pieceRects.clear()
    @highlightedRect = Qt::Rect.new()
    @inPlace = 0
    update()
end

#dragEnterEvent(event) ⇒ Object



51
52
53
54
55
56
57
# File 'ext/ruby/qtruby/examples/itemviews/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) ⇒ Object



59
60
61
62
63
64
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 59

def dragLeaveEvent(event)
    updateRect = @highlightedRect
    @highlightedRect = Qt::Rect.new()
    update(updateRect)
    event.accept()
end

#dragMoveEvent(event) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/ruby/qtruby/examples/itemviews/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())
puts "dragMoveEvent event.dropAction = Qt::MoveAction"
        event.dropAction = Qt::MoveAction
        event.accept()
    else
        @highlightedRect = Qt::Rect.new()
        event.ignore()
    end

    update(updateRect)
end

#dropEvent(event) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 84

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)

puts "dropEvent event.dropAction = Qt::MoveAction"
        event.dropAction = Qt::MoveAction
        event.accept()
p event.dropAction    
        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) ⇒ Object



118
119
120
121
122
123
124
125
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 118

def findPiece(pieceRect)
    (0...@pieceRects.size).each do |i|
        if pieceRect == @pieceRects[i]
            return i
        end
    end
    return -1
end

#mousePressEvent(event) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 127

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) == 0
        @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) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 172

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) ⇒ Object



189
190
191
# File 'ext/ruby/qtruby/examples/itemviews/puzzle/puzzlewidget.rb', line 189

def targetSquare(position)
    return Qt::Rect.new(position.x()/80 * 80, position.y()/80 * 80, 80, 80)
end