-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.cpp
152 lines (124 loc) · 3.78 KB
/
puzzle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "puzzle.h"
#include <QtWidgets>
Puzzle::Puzzle(int size, QWidget *parent):
QWidget (parent), imageSize (size)
{
setAcceptDrops (true);
setMinimumSize (imageSize, imageSize);
setMaximumSize (imageSize, imageSize);
}
void Puzzle::clear()
{
pieces.clear ();
rectangleBlanc = QRect ();
inPlace = 0;
update ();
}
int Puzzle::longPiece() const
{
return imageSize/5;
}
int Puzzle::longImage() const
{
return imageSize;
}
int Puzzle::findPiece(const QRect &pieceRect) const
{
for (int i = 0; i < pieces.size (); ++i) {
if (pieces.at (i).rectangle == pieceRect)
return i;
}
return -1;
}
const QRect Puzzle::targetSquare(const QPoint &pos)
{
return QRect (pos / longPiece ()*longPiece (), QSize (longPiece (), longPiece ()));
}
void Puzzle::mousePressEvent(QMouseEvent *event)
{
QRect rect = targetSquare (event->pos ());
int indice = findPiece (rect);
if (indice == -1) return;
Piece piece = pieces.takeAt (indice);
if (piece.location == rect.topLeft ()/longPiece ())
inPlace--;
update (rect);
QByteArray encodeur;
QDataStream stream (&encodeur, QIODevice::WriteOnly);
stream << piece.pixmap << piece.location;
QMimeData *mime = new QMimeData;
mime->setData ("image/x-image-piece", encodeur);
QDrag *drag = new QDrag(this);
drag->setMimeData (mime);
drag->setHotSpot (event->pos () - rect.topLeft ());
drag->setPixmap (piece.pixmap);
if (drag->exec (Qt::MoveAction) == Qt::IgnoreAction) {
pieces.insert (indice, piece);
update (targetSquare (event->pos ()));
if (piece.location == QPoint (rect.x ()/longPiece (), rect.y ()/longPiece ()))
inPlace++;
}
}
void Puzzle::paintEvent(QPaintEvent *event)
{
QPainter crayon (this);
crayon.fillRect (event->rect (), Qt::white);
if (rectangleBlanc.isValid ()) {
crayon.setBrush (QColor ("#ffcccc"));
crayon.setPen (Qt::NoPen);
crayon.drawRect (rectangleBlanc.adjusted (0, 0, -1, -1));
}
for (const Piece &piece : pieces) {
crayon.drawPixmap (piece.rectangle, piece.pixmap);
}
}
void Puzzle::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData ()->hasFormat ("image/x-image-piece"))
event->accept ();
else
event->ignore ();
}
void Puzzle::dragMoveEvent(QDragMoveEvent *event)
{
QRect nouveauRect = rectangleBlanc.united (targetSquare (event->pos ()));
if (event->mimeData ()->hasFormat ("image/x-image-piece") && findPiece (targetSquare (event->pos ())) == -1) {
rectangleBlanc = targetSquare (event->pos ());
event->setDropAction (Qt::MoveAction);
event->accept ();
} else {
rectangleBlanc = QRect ();
event->ignore ();
}
update (nouveauRect);
}
void Puzzle::dragLeaveEvent(QDragLeaveEvent *event)
{
QRect rect = rectangleBlanc;
rectangleBlanc = QRect ();
update (rect);
event->accept ();
}
void Puzzle::dropEvent(QDropEvent *event)
{
if (event->mimeData ()->hasFormat ("image/x-image-piece") && findPiece (targetSquare (event->pos ())) == -1) {
QByteArray encodeur = event->mimeData ()->data ("image/x-image-piece");
QDataStream stream (&encodeur, QIODevice::ReadOnly);
Piece piece;
piece.rectangle = targetSquare (event->pos ());
stream >> piece.pixmap >> piece.location;
pieces.append (piece);
rectangleBlanc = QRect ();
update (piece.rectangle);
event->setDropAction (Qt::MoveAction);
event->accept ();
if (piece.location == piece.rectangle.topLeft ()/longPiece ()) {
inPlace++;
if (inPlace == 25)
emit puzzleComplet ();
}
} else {
rectangleBlanc = QRect ();
event->ignore ();
}
}