-
Notifications
You must be signed in to change notification settings - Fork 0
/
piecemodel.cpp
161 lines (132 loc) · 4.32 KB
/
piecemodel.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
153
154
155
156
157
158
159
160
161
#include "piecemodel.h"
#include <QIcon>
#include <QMimeData>
#include <QRandomGenerator>
PieceModel::PieceModel(int size, QObject *parent):
QAbstractListModel (parent), nbrPieces (size)
{
}
int PieceModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid () ? 0 : m_pixmaps.size ();
}
QVariant PieceModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid ())
return QVariant ();
if (role == Qt::DecorationRole)
return QIcon (m_pixmaps.value (index.row ()).scaled (nbrPieces, nbrPieces, Qt::KeepAspectRatio, Qt::SmoothTransformation));
else if (role == Qt::UserRole)
return m_pixmaps.value (index.row ());
else if (role == Qt::UserRole + 1)
return m_locations.value (index.row ());
return QVariant ();
}
QStringList PieceModel::mimeTypes() const
{
QStringList types;
types << "image/x-image-piece" ;
return types;
}
QMimeData *PieceModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mime = new QMimeData ();
QByteArray encodeur;
QDataStream stream (&encodeur, QIODevice::WriteOnly);
for (const QModelIndex &index : indexes) {
if (index.isValid ()) {
QPixmap pixmap = qvariant_cast<QPixmap> (data (index, Qt::UserRole));
QPoint location = data (index, Qt::UserRole+1).toPoint ();
stream << pixmap << location;
}
}
mime->setData ("image/x-image-piece", encodeur);
return mime;
}
bool PieceModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
//test d'abord les parametres s'ils sont pretes à l'insertion
if (!data->hasFormat ("image/x-image-piece")) return false;
if (action == Qt::IgnoreAction) return true;
if (column > 0) return false;
//prendre une ligne
int ligne;
if (!parent.isValid ()) {
if (row < 0)
ligne = m_pixmaps.size ();
else
ligne = qMin (row, m_pixmaps.size ());
} else {
ligne = parent.row ();
}
//on prend l'encodeur mime et on le lit
QByteArray encodeur = data->data ("image/x-image-piece");
QDataStream stream (&encodeur, QIODevice::ReadOnly);
//on insert les image dans n pixmap et point
while (!stream.atEnd ()) {
QPixmap pixmap; QPoint location;
stream >> pixmap >> location;
beginInsertRows (QModelIndex (), ligne, ligne);
m_pixmaps.insert (ligne, pixmap);
m_locations.insert (ligne, location);
endInsertRows ();
++ligne;
}
return true;
}
Qt::DropActions PieceModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
bool PieceModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (parent.isValid ())
return false;
if (row >= m_pixmaps.size () || row + count <= 0)
return false;
int beginRow = qMax (0, row);
int endRow = qMin (row + count - 1, m_pixmaps.size () - 1);
beginRemoveRows (parent, beginRow, endRow);
while (beginRow <= endRow) {
m_pixmaps.removeAt (beginRow);
m_locations.removeAt (beginRow);
++beginRow;
}
endRemoveRows ();
return true;
}
Qt::ItemFlags PieceModel::flags(const QModelIndex &index) const
{
if (index.isValid ())
return (QAbstractListModel::flags (index) | Qt::ItemIsDragEnabled);
return Qt::ItemIsDropEnabled;
}
void PieceModel::ajoutePiece(const QPixmap &pixmap, const QPoint &location)
{
int ligne;
if (QRandomGenerator::global ()->bounded (2) == 1)
ligne = 0;
else
ligne = m_pixmaps.size ();
beginInsertRows (QModelIndex (), ligne, ligne);
m_pixmaps.insert (ligne, pixmap);
m_locations.insert (ligne, location);
endInsertRows ();
}
void PieceModel::ajouteLesPieces(const QPixmap &pixmaps)
{
//on vide d'abord les donnees
if (!m_pixmaps.isEmpty ()) {
beginRemoveRows (QModelIndex(), 0, m_pixmaps.size () - 1);
m_pixmaps.clear ();
m_locations.clear ();
endRemoveRows ();
}
//completons les pieces
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
QPixmap piece = pixmaps.copy (j*nbrPieces, i*nbrPieces, nbrPieces, nbrPieces);
ajoutePiece (piece, QPoint (j, i));
}
}
}