1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "puzzlewidget.h"
|
---|
44 |
|
---|
45 | PuzzleWidget::PuzzleWidget(QWidget *parent)
|
---|
46 | : QWidget(parent)
|
---|
47 | {
|
---|
48 | setAcceptDrops(true);
|
---|
49 | setMinimumSize(400, 400);
|
---|
50 | setMaximumSize(400, 400);
|
---|
51 | }
|
---|
52 |
|
---|
53 | void PuzzleWidget::clear()
|
---|
54 | {
|
---|
55 | pieceLocations.clear();
|
---|
56 | piecePixmaps.clear();
|
---|
57 | pieceRects.clear();
|
---|
58 | highlightedRect = QRect();
|
---|
59 | inPlace = 0;
|
---|
60 | update();
|
---|
61 | }
|
---|
62 |
|
---|
63 | void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
|
---|
64 | {
|
---|
65 | if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
|
---|
66 | event->accept();
|
---|
67 | else
|
---|
68 | event->ignore();
|
---|
69 | }
|
---|
70 |
|
---|
71 | void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event)
|
---|
72 | {
|
---|
73 | QRect updateRect = highlightedRect;
|
---|
74 | highlightedRect = QRect();
|
---|
75 | update(updateRect);
|
---|
76 | event->accept();
|
---|
77 | }
|
---|
78 |
|
---|
79 | void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
|
---|
80 | {
|
---|
81 | QRect updateRect = highlightedRect.unite(targetSquare(event->pos()));
|
---|
82 |
|
---|
83 | if (event->mimeData()->hasFormat("image/x-puzzle-piece")
|
---|
84 | && findPiece(targetSquare(event->pos())) == -1) {
|
---|
85 |
|
---|
86 | highlightedRect = targetSquare(event->pos());
|
---|
87 | event->setDropAction(Qt::MoveAction);
|
---|
88 | event->accept();
|
---|
89 | } else {
|
---|
90 | highlightedRect = QRect();
|
---|
91 | event->ignore();
|
---|
92 | }
|
---|
93 |
|
---|
94 | update(updateRect);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void PuzzleWidget::dropEvent(QDropEvent *event)
|
---|
98 | {
|
---|
99 | if (event->mimeData()->hasFormat("image/x-puzzle-piece")
|
---|
100 | && findPiece(targetSquare(event->pos())) == -1) {
|
---|
101 |
|
---|
102 | QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
|
---|
103 | QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
---|
104 | QRect square = targetSquare(event->pos());
|
---|
105 | QPixmap pixmap;
|
---|
106 | QPoint location;
|
---|
107 | dataStream >> pixmap >> location;
|
---|
108 |
|
---|
109 | pieceLocations.append(location);
|
---|
110 | piecePixmaps.append(pixmap);
|
---|
111 | pieceRects.append(square);
|
---|
112 |
|
---|
113 | highlightedRect = QRect();
|
---|
114 | update(square);
|
---|
115 |
|
---|
116 | event->setDropAction(Qt::MoveAction);
|
---|
117 | event->accept();
|
---|
118 |
|
---|
119 | if (location == QPoint(square.x()/80, square.y()/80)) {
|
---|
120 | inPlace++;
|
---|
121 | if (inPlace == 25)
|
---|
122 | emit puzzleCompleted();
|
---|
123 | }
|
---|
124 | } else {
|
---|
125 | highlightedRect = QRect();
|
---|
126 | event->ignore();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | int PuzzleWidget::findPiece(const QRect &pieceRect) const
|
---|
131 | {
|
---|
132 | for (int i = 0; i < pieceRects.size(); ++i) {
|
---|
133 | if (pieceRect == pieceRects[i]) {
|
---|
134 | return i;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | return -1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void PuzzleWidget::mousePressEvent(QMouseEvent *event)
|
---|
141 | {
|
---|
142 | QRect square = targetSquare(event->pos());
|
---|
143 | int found = findPiece(square);
|
---|
144 |
|
---|
145 | if (found == -1)
|
---|
146 | return;
|
---|
147 |
|
---|
148 | QPoint location = pieceLocations[found];
|
---|
149 | QPixmap pixmap = piecePixmaps[found];
|
---|
150 | pieceLocations.removeAt(found);
|
---|
151 | piecePixmaps.removeAt(found);
|
---|
152 | pieceRects.removeAt(found);
|
---|
153 |
|
---|
154 | if (location == QPoint(square.x()/80, square.y()/80))
|
---|
155 | inPlace--;
|
---|
156 |
|
---|
157 | update(square);
|
---|
158 |
|
---|
159 | QByteArray itemData;
|
---|
160 | QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
---|
161 |
|
---|
162 | dataStream << pixmap << location;
|
---|
163 |
|
---|
164 | QMimeData *mimeData = new QMimeData;
|
---|
165 | mimeData->setData("image/x-puzzle-piece", itemData);
|
---|
166 |
|
---|
167 | QDrag *drag = new QDrag(this);
|
---|
168 | drag->setMimeData(mimeData);
|
---|
169 | drag->setHotSpot(event->pos() - square.topLeft());
|
---|
170 | drag->setPixmap(pixmap);
|
---|
171 |
|
---|
172 | if (!(drag->exec(Qt::MoveAction) == Qt::MoveAction)) {
|
---|
173 | pieceLocations.insert(found, location);
|
---|
174 | piecePixmaps.insert(found, pixmap);
|
---|
175 | pieceRects.insert(found, square);
|
---|
176 | update(targetSquare(event->pos()));
|
---|
177 |
|
---|
178 | if (location == QPoint(square.x()/80, square.y()/80))
|
---|
179 | inPlace++;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | void PuzzleWidget::paintEvent(QPaintEvent *event)
|
---|
184 | {
|
---|
185 | QPainter painter;
|
---|
186 | painter.begin(this);
|
---|
187 | painter.fillRect(event->rect(), Qt::white);
|
---|
188 |
|
---|
189 | if (highlightedRect.isValid()) {
|
---|
190 | painter.setBrush(QColor("#ffcccc"));
|
---|
191 | painter.setPen(Qt::NoPen);
|
---|
192 | painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
|
---|
193 | }
|
---|
194 |
|
---|
195 | for (int i = 0; i < pieceRects.size(); ++i) {
|
---|
196 | painter.drawPixmap(pieceRects[i], piecePixmaps[i]);
|
---|
197 | }
|
---|
198 | painter.end();
|
---|
199 | }
|
---|
200 |
|
---|
201 | const QRect PuzzleWidget::targetSquare(const QPoint &position) const
|
---|
202 | {
|
---|
203 | return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80);
|
---|
204 | }
|
---|