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(minw: m_ImageSize, minh: m_ImageSize); |
64 | setMaximumSize(maxw: m_ImageSize, maxh: 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(mimetype: 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(r: targetSquare(position: event->pos())); |
94 | |
95 | if (event->mimeData()->hasFormat(mimetype: PiecesList::puzzleMimeType()) |
96 | && findPiece(pieceRect: targetSquare(position: event->pos())) == -1) { |
97 | |
98 | highlightedRect = targetSquare(position: event->pos()); |
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(mimetype: PiecesList::puzzleMimeType()) |
112 | && findPiece(pieceRect: targetSquare(position: event->pos())) == -1) { |
113 | |
114 | QByteArray pieceData = event->mimeData()->data(mimetype: PiecesList::puzzleMimeType()); |
115 | QDataStream dataStream(&pieceData, QIODevice::ReadOnly); |
116 | Piece piece; |
117 | piece.rect = targetSquare(position: event->pos()); |
118 | dataStream >> piece.pixmap >> piece.location; |
119 | |
120 | pieces.append(t: 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(position: event->pos()); |
151 | const int found = findPiece(pieceRect: square); |
152 | |
153 | if (found == -1) |
154 | return; |
155 | |
156 | Piece piece = pieces.takeAt(i: 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(mimetype: PiecesList::puzzleMimeType(), data: itemData); |
170 | |
171 | QDrag *drag = new QDrag(this); |
172 | drag->setMimeData(mimeData); |
173 | drag->setHotSpot(event->pos() - square.topLeft()); |
174 | drag->setPixmap(piece.pixmap); |
175 | |
176 | if (drag->exec(supportedActions: Qt::MoveAction) != Qt::MoveAction) { |
177 | pieces.insert(i: found, t: piece); |
178 | update(targetSquare(position: event->pos())); |
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(r: event->rect(), c: Qt::white); |
189 | |
190 | if (highlightedRect.isValid()) { |
191 | painter.setBrush(QColor("#ffcccc" )); |
192 | painter.setPen(Qt::NoPen); |
193 | painter.drawRect(r: highlightedRect.adjusted(xp1: 0, yp1: 0, xp2: -1, yp2: -1)); |
194 | } |
195 | |
196 | for (const Piece &piece : pieces) |
197 | painter.drawPixmap(r: piece.rect, pm: 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 | |