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