0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include "piecesmodel.h"
|
|
45 |
|
|
46 |
PiecesModel::PiecesModel(QObject *parent)
|
|
47 |
: QAbstractListModel(parent)
|
|
48 |
{
|
|
49 |
}
|
|
50 |
|
|
51 |
QVariant PiecesModel::data(const QModelIndex &index, int role) const
|
|
52 |
{
|
|
53 |
if (!index.isValid())
|
|
54 |
return QVariant();
|
|
55 |
|
|
56 |
if (role == Qt::DecorationRole)
|
|
57 |
return QIcon(pixmaps.value(index.row()).scaled(60, 60,
|
|
58 |
Qt::KeepAspectRatio, Qt::SmoothTransformation));
|
|
59 |
else if (role == Qt::UserRole)
|
|
60 |
return pixmaps.value(index.row());
|
|
61 |
else if (role == Qt::UserRole + 1)
|
|
62 |
return locations.value(index.row());
|
|
63 |
|
|
64 |
return QVariant();
|
|
65 |
}
|
|
66 |
|
|
67 |
void PiecesModel::addPiece(const QPixmap &pixmap, const QPoint &location)
|
|
68 |
{
|
|
69 |
int row;
|
|
70 |
if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1)
|
|
71 |
row = 0;
|
|
72 |
else
|
|
73 |
row = pixmaps.size();
|
|
74 |
|
|
75 |
beginInsertRows(QModelIndex(), row, row);
|
|
76 |
pixmaps.insert(row, pixmap);
|
|
77 |
locations.insert(row, location);
|
|
78 |
endInsertRows();
|
|
79 |
}
|
|
80 |
|
|
81 |
Qt::ItemFlags PiecesModel::flags(const QModelIndex &index) const
|
|
82 |
{
|
|
83 |
if (index.isValid())
|
|
84 |
return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
|
|
85 |
|
|
86 |
return Qt::ItemIsDropEnabled;
|
|
87 |
}
|
|
88 |
|
|
89 |
bool PiecesModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
90 |
{
|
|
91 |
if (parent.isValid())
|
|
92 |
return false;
|
|
93 |
|
|
94 |
if (row >= pixmaps.size() || row + count <= 0)
|
|
95 |
return false;
|
|
96 |
|
|
97 |
int beginRow = qMax(0, row);
|
|
98 |
int endRow = qMin(row + count - 1, pixmaps.size() - 1);
|
|
99 |
|
|
100 |
beginRemoveRows(parent, beginRow, endRow);
|
|
101 |
|
|
102 |
while (beginRow <= endRow) {
|
|
103 |
pixmaps.removeAt(beginRow);
|
|
104 |
locations.removeAt(beginRow);
|
|
105 |
++beginRow;
|
|
106 |
}
|
|
107 |
|
|
108 |
endRemoveRows();
|
|
109 |
return true;
|
|
110 |
}
|
|
111 |
|
|
112 |
QStringList PiecesModel::mimeTypes() const
|
|
113 |
{
|
|
114 |
QStringList types;
|
|
115 |
types << "image/x-puzzle-piece";
|
|
116 |
return types;
|
|
117 |
}
|
|
118 |
|
|
119 |
QMimeData *PiecesModel::mimeData(const QModelIndexList &indexes) const
|
|
120 |
{
|
|
121 |
QMimeData *mimeData = new QMimeData();
|
|
122 |
QByteArray encodedData;
|
|
123 |
|
|
124 |
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
|
125 |
|
|
126 |
foreach (QModelIndex index, indexes) {
|
|
127 |
if (index.isValid()) {
|
|
128 |
QPixmap pixmap = qVariantValue<QPixmap>(data(index, Qt::UserRole));
|
|
129 |
QPoint location = data(index, Qt::UserRole+1).toPoint();
|
|
130 |
stream << pixmap << location;
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
mimeData->setData("image/x-puzzle-piece", encodedData);
|
|
135 |
return mimeData;
|
|
136 |
}
|
|
137 |
|
|
138 |
bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
|
|
139 |
int row, int column, const QModelIndex &parent)
|
|
140 |
{
|
|
141 |
if (!data->hasFormat("image/x-puzzle-piece"))
|
|
142 |
return false;
|
|
143 |
|
|
144 |
if (action == Qt::IgnoreAction)
|
|
145 |
return true;
|
|
146 |
|
|
147 |
if (column > 0)
|
|
148 |
return false;
|
|
149 |
|
|
150 |
int endRow;
|
|
151 |
|
|
152 |
if (!parent.isValid()) {
|
|
153 |
if (row < 0)
|
|
154 |
endRow = pixmaps.size();
|
|
155 |
else
|
|
156 |
endRow = qMin(row, pixmaps.size());
|
|
157 |
} else
|
|
158 |
endRow = parent.row();
|
|
159 |
|
|
160 |
QByteArray encodedData = data->data("image/x-puzzle-piece");
|
|
161 |
QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
|
162 |
|
|
163 |
while (!stream.atEnd()) {
|
|
164 |
QPixmap pixmap;
|
|
165 |
QPoint location;
|
|
166 |
stream >> pixmap >> location;
|
|
167 |
|
|
168 |
beginInsertRows(QModelIndex(), endRow, endRow);
|
|
169 |
pixmaps.insert(endRow, pixmap);
|
|
170 |
locations.insert(endRow, location);
|
|
171 |
endInsertRows();
|
|
172 |
|
|
173 |
++endRow;
|
|
174 |
}
|
|
175 |
|
|
176 |
return true;
|
|
177 |
}
|
|
178 |
|
|
179 |
int PiecesModel::rowCount(const QModelIndex &parent) const
|
|
180 |
{
|
|
181 |
if (parent.isValid())
|
|
182 |
return 0;
|
|
183 |
else
|
|
184 |
return pixmaps.size();
|
|
185 |
}
|
|
186 |
|
|
187 |
Qt::DropActions PiecesModel::supportedDropActions() const
|
|
188 |
{
|
|
189 |
return Qt::CopyAction | Qt::MoveAction;
|
|
190 |
}
|
|
191 |
|
|
192 |
void PiecesModel::addPieces(const QPixmap& pixmap)
|
|
193 |
{
|
|
194 |
beginRemoveRows(QModelIndex(), 0, 24);
|
|
195 |
pixmaps.clear();
|
|
196 |
locations.clear();
|
|
197 |
endRemoveRows();
|
|
198 |
for (int y = 0; y < 5; ++y) {
|
|
199 |
for (int x = 0; x < 5; ++x) {
|
|
200 |
QPixmap pieceImage = pixmap.copy(x*80, y*80, 80, 80);
|
|
201 |
addPiece(pieceImage, QPoint(x, y));
|
|
202 |
}
|
|
203 |
}
|
|
204 |
}
|