| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "puzzlewidget.h" | 
|---|
| 45 |  | 
|---|
| 46 | PuzzleWidget::PuzzleWidget(QWidget *parent) | 
|---|
| 47 | : QWidget(parent) | 
|---|
| 48 | { | 
|---|
| 49 | setAcceptDrops(true); | 
|---|
| 50 | setMinimumSize(400, 400); | 
|---|
| 51 | setMaximumSize(400, 400); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | void PuzzleWidget::clear() | 
|---|
| 55 | { | 
|---|
| 56 | pieceLocations.clear(); | 
|---|
| 57 | piecePixmaps.clear(); | 
|---|
| 58 | pieceRects.clear(); | 
|---|
| 59 | highlightedRect = QRect(); | 
|---|
| 60 | inPlace = 0; | 
|---|
| 61 | update(); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event) | 
|---|
| 65 | { | 
|---|
| 66 | if (event->mimeData()->hasFormat("image/x-puzzle-piece")) | 
|---|
| 67 | event->accept(); | 
|---|
| 68 | else | 
|---|
| 69 | event->ignore(); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event) | 
|---|
| 73 | { | 
|---|
| 74 | QRect updateRect = highlightedRect; | 
|---|
| 75 | highlightedRect = QRect(); | 
|---|
| 76 | update(updateRect); | 
|---|
| 77 | event->accept(); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event) | 
|---|
| 81 | { | 
|---|
| 82 | QRect updateRect = highlightedRect.unite(targetSquare(event->pos())); | 
|---|
| 83 |  | 
|---|
| 84 | if (event->mimeData()->hasFormat("image/x-puzzle-piece") | 
|---|
| 85 | && findPiece(targetSquare(event->pos())) == -1) { | 
|---|
| 86 |  | 
|---|
| 87 | highlightedRect = targetSquare(event->pos()); | 
|---|
| 88 | event->setDropAction(Qt::MoveAction); | 
|---|
| 89 | event->accept(); | 
|---|
| 90 | } else { | 
|---|
| 91 | highlightedRect = QRect(); | 
|---|
| 92 | event->ignore(); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | update(updateRect); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void PuzzleWidget::dropEvent(QDropEvent *event) | 
|---|
| 99 | { | 
|---|
| 100 | if (event->mimeData()->hasFormat("image/x-puzzle-piece") | 
|---|
| 101 | && findPiece(targetSquare(event->pos())) == -1) { | 
|---|
| 102 |  | 
|---|
| 103 | QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece"); | 
|---|
| 104 | QDataStream stream(&pieceData, QIODevice::ReadOnly); | 
|---|
| 105 | QRect square = targetSquare(event->pos()); | 
|---|
| 106 | QPixmap pixmap; | 
|---|
| 107 | QPoint location; | 
|---|
| 108 | stream >> pixmap >> location; | 
|---|
| 109 |  | 
|---|
| 110 | pieceLocations.append(location); | 
|---|
| 111 | piecePixmaps.append(pixmap); | 
|---|
| 112 | pieceRects.append(square); | 
|---|
| 113 |  | 
|---|
| 114 | highlightedRect = QRect(); | 
|---|
| 115 | update(square); | 
|---|
| 116 |  | 
|---|
| 117 | event->setDropAction(Qt::MoveAction); | 
|---|
| 118 | event->accept(); | 
|---|
| 119 |  | 
|---|
| 120 | if (location == QPoint(square.x()/80, square.y()/80)) { | 
|---|
| 121 | inPlace++; | 
|---|
| 122 | if (inPlace == 25) | 
|---|
| 123 | emit puzzleCompleted(); | 
|---|
| 124 | } | 
|---|
| 125 | } else { | 
|---|
| 126 | highlightedRect = QRect(); | 
|---|
| 127 | event->ignore(); | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | int PuzzleWidget::findPiece(const QRect &pieceRect) const | 
|---|
| 132 | { | 
|---|
| 133 | for (int i = 0; i < pieceRects.size(); ++i) { | 
|---|
| 134 | if (pieceRect == pieceRects[i]) { | 
|---|
| 135 | return i; | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 | return -1; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void PuzzleWidget::mousePressEvent(QMouseEvent *event) | 
|---|
| 142 | { | 
|---|
| 143 | QRect square = targetSquare(event->pos()); | 
|---|
| 144 | int found = findPiece(square); | 
|---|
| 145 |  | 
|---|
| 146 | if (found == -1) | 
|---|
| 147 | return; | 
|---|
| 148 |  | 
|---|
| 149 | QPoint location = pieceLocations[found]; | 
|---|
| 150 | QPixmap pixmap = piecePixmaps[found]; | 
|---|
| 151 | pieceLocations.removeAt(found); | 
|---|
| 152 | piecePixmaps.removeAt(found); | 
|---|
| 153 | pieceRects.removeAt(found); | 
|---|
| 154 |  | 
|---|
| 155 | if (location == QPoint(square.x()/80, square.y()/80)) | 
|---|
| 156 | inPlace--; | 
|---|
| 157 |  | 
|---|
| 158 | update(square); | 
|---|
| 159 |  | 
|---|
| 160 | QByteArray itemData; | 
|---|
| 161 | QDataStream dataStream(&itemData, QIODevice::WriteOnly); | 
|---|
| 162 |  | 
|---|
| 163 | dataStream << pixmap << location; | 
|---|
| 164 |  | 
|---|
| 165 | QMimeData *mimeData = new QMimeData; | 
|---|
| 166 | mimeData->setData("image/x-puzzle-piece", itemData); | 
|---|
| 167 |  | 
|---|
| 168 | QDrag *drag = new QDrag(this); | 
|---|
| 169 | drag->setMimeData(mimeData); | 
|---|
| 170 | drag->setHotSpot(event->pos() - square.topLeft()); | 
|---|
| 171 | drag->setPixmap(pixmap); | 
|---|
| 172 |  | 
|---|
| 173 | if (drag->start(Qt::MoveAction) == 0) { | 
|---|
| 174 | pieceLocations.insert(found, location); | 
|---|
| 175 | piecePixmaps.insert(found, pixmap); | 
|---|
| 176 | pieceRects.insert(found, square); | 
|---|
| 177 | update(targetSquare(event->pos())); | 
|---|
| 178 |  | 
|---|
| 179 | if (location == QPoint(square.x()/80, square.y()/80)) | 
|---|
| 180 | inPlace++; | 
|---|
| 181 | } | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | void PuzzleWidget::paintEvent(QPaintEvent *event) | 
|---|
| 185 | { | 
|---|
| 186 | QPainter painter; | 
|---|
| 187 | painter.begin(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 (int i = 0; i < pieceRects.size(); ++i) { | 
|---|
| 197 | painter.drawPixmap(pieceRects[i], piecePixmaps[i]); | 
|---|
| 198 | } | 
|---|
| 199 | painter.end(); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | const QRect PuzzleWidget::targetSquare(const QPoint &position) const | 
|---|
| 203 | { | 
|---|
| 204 | return QRect(position.x()/80 * 80, position.y()/80 * 80, 80, 80); | 
|---|
| 205 | } | 
|---|