| 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 | |
| 53 | #include <QtWidgets> |
| 54 | |
| 55 | PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent) |
| 56 | : QWidget(parent), m_ImageSize(imageSize) |
| 57 | { |
| 58 | setAcceptDrops(true); |
| 59 | setMinimumSize(m_ImageSize, m_ImageSize); |
| 60 | setMaximumSize(m_ImageSize, m_ImageSize); |
| 61 | } |
| 62 | |
| 63 | void PuzzleWidget::clear() |
| 64 | { |
| 65 | pieces.clear(); |
| 66 | highlightedRect = QRect(); |
| 67 | inPlace = 0; |
| 68 | update(); |
| 69 | } |
| 70 | |
| 71 | void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event) |
| 72 | { |
| 73 | if (event->mimeData()->hasFormat("image/x-puzzle-piece" )) |
| 74 | event->accept(); |
| 75 | else |
| 76 | event->ignore(); |
| 77 | } |
| 78 | |
| 79 | void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event) |
| 80 | { |
| 81 | QRect updateRect = highlightedRect; |
| 82 | highlightedRect = QRect(); |
| 83 | update(updateRect); |
| 84 | event->accept(); |
| 85 | } |
| 86 | |
| 87 | void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event) |
| 88 | { |
| 89 | QRect updateRect = highlightedRect.united(targetSquare(event->position().toPoint())); |
| 90 | |
| 91 | if (event->mimeData()->hasFormat("image/x-puzzle-piece" ) |
| 92 | && findPiece(targetSquare(event->position().toPoint())) == -1) { |
| 93 | |
| 94 | highlightedRect = targetSquare(event->position().toPoint()); |
| 95 | event->setDropAction(Qt::MoveAction); |
| 96 | event->accept(); |
| 97 | } else { |
| 98 | highlightedRect = QRect(); |
| 99 | event->ignore(); |
| 100 | } |
| 101 | |
| 102 | update(updateRect); |
| 103 | } |
| 104 | |
| 105 | void PuzzleWidget::dropEvent(QDropEvent *event) |
| 106 | { |
| 107 | if (event->mimeData()->hasFormat("image/x-puzzle-piece" ) |
| 108 | && findPiece(targetSquare(event->position().toPoint())) == -1) { |
| 109 | |
| 110 | QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece" ); |
| 111 | QDataStream dataStream(&pieceData, QIODevice::ReadOnly); |
| 112 | Piece piece; |
| 113 | piece.rect = targetSquare(event->position().toPoint()); |
| 114 | dataStream >> piece.pixmap >> piece.location; |
| 115 | |
| 116 | pieces.append(piece); |
| 117 | |
| 118 | highlightedRect = QRect(); |
| 119 | update(piece.rect); |
| 120 | |
| 121 | event->setDropAction(Qt::MoveAction); |
| 122 | event->accept(); |
| 123 | |
| 124 | if (piece.location == piece.rect.topLeft() / pieceSize()) { |
| 125 | inPlace++; |
| 126 | if (inPlace == 25) |
| 127 | emit puzzleCompleted(); |
| 128 | } |
| 129 | } else { |
| 130 | highlightedRect = QRect(); |
| 131 | event->ignore(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | int PuzzleWidget::findPiece(const QRect &pieceRect) const |
| 136 | { |
| 137 | for (int i = 0, size = pieces.size(); i < size; ++i) { |
| 138 | if (pieces.at(i).rect == pieceRect) |
| 139 | return i; |
| 140 | } |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | void PuzzleWidget::mousePressEvent(QMouseEvent *event) |
| 145 | { |
| 146 | QRect square = targetSquare(event->position().toPoint()); |
| 147 | int found = findPiece(square); |
| 148 | |
| 149 | if (found == -1) |
| 150 | return; |
| 151 | |
| 152 | Piece piece = pieces.takeAt(found); |
| 153 | |
| 154 | if (piece.location == square.topLeft() / pieceSize()) |
| 155 | inPlace--; |
| 156 | |
| 157 | update(square); |
| 158 | |
| 159 | QByteArray itemData; |
| 160 | QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
| 161 | |
| 162 | dataStream << piece.pixmap << piece.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->position().toPoint() - square.topLeft()); |
| 170 | drag->setPixmap(piece.pixmap); |
| 171 | |
| 172 | if (drag->exec(Qt::MoveAction) == Qt::IgnoreAction) { |
| 173 | pieces.insert(found, piece); |
| 174 | update(targetSquare(event->position().toPoint())); |
| 175 | |
| 176 | if (piece.location == QPoint(square.x() / pieceSize(), square.y() / pieceSize())) |
| 177 | inPlace++; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void PuzzleWidget::paintEvent(QPaintEvent *event) |
| 182 | { |
| 183 | QPainter painter(this); |
| 184 | painter.fillRect(event->rect(), Qt::white); |
| 185 | |
| 186 | if (highlightedRect.isValid()) { |
| 187 | painter.setBrush(QColor("#ffcccc" )); |
| 188 | painter.setPen(Qt::NoPen); |
| 189 | painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1)); |
| 190 | } |
| 191 | |
| 192 | for (const Piece &piece : pieces) |
| 193 | painter.drawPixmap(piece.rect, piece.pixmap); |
| 194 | } |
| 195 | |
| 196 | const QRect PuzzleWidget::targetSquare(const QPoint &position) const |
| 197 | { |
| 198 | return QRect(position / pieceSize() * pieceSize(), |
| 199 | QSize(pieceSize(), pieceSize())); |
| 200 | } |
| 201 | |
| 202 | int PuzzleWidget::pieceSize() const |
| 203 | { |
| 204 | return m_ImageSize / 5; |
| 205 | } |
| 206 | |
| 207 | int PuzzleWidget::imageSize() const |
| 208 | { |
| 209 | return m_ImageSize; |
| 210 | } |
| 211 | |