source: trunk/examples/itemviews/puzzle/piecesmodel.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "piecesmodel.h"
44
45PiecesModel::PiecesModel(QObject *parent)
46 : QAbstractListModel(parent)
47{
48}
49
50QVariant PiecesModel::data(const QModelIndex &index, int role) const
51{
52 if (!index.isValid())
53 return QVariant();
54
55 if (role == Qt::DecorationRole)
56 return QIcon(pixmaps.value(index.row()).scaled(60, 60,
57 Qt::KeepAspectRatio, Qt::SmoothTransformation));
58 else if (role == Qt::UserRole)
59 return pixmaps.value(index.row());
60 else if (role == Qt::UserRole + 1)
61 return locations.value(index.row());
62
63 return QVariant();
64}
65
66void PiecesModel::addPiece(const QPixmap &pixmap, const QPoint &location)
67{
68 int row;
69 if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1)
70 row = 0;
71 else
72 row = pixmaps.size();
73
74 beginInsertRows(QModelIndex(), row, row);
75 pixmaps.insert(row, pixmap);
76 locations.insert(row, location);
77 endInsertRows();
78}
79
80Qt::ItemFlags PiecesModel::flags(const QModelIndex &index) const
81{
82 if (index.isValid())
83 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
84
85 return Qt::ItemIsDropEnabled;
86}
87
88bool PiecesModel::removeRows(int row, int count, const QModelIndex &parent)
89{
90 if (parent.isValid())
91 return false;
92
93 if (row >= pixmaps.size() || row + count <= 0)
94 return false;
95
96 int beginRow = qMax(0, row);
97 int endRow = qMin(row + count - 1, pixmaps.size() - 1);
98
99 beginRemoveRows(parent, beginRow, endRow);
100
101 while (beginRow <= endRow) {
102 pixmaps.removeAt(beginRow);
103 locations.removeAt(beginRow);
104 ++beginRow;
105 }
106
107 endRemoveRows();
108 return true;
109}
110
111QStringList PiecesModel::mimeTypes() const
112{
113 QStringList types;
114 types << "image/x-puzzle-piece";
115 return types;
116}
117
118QMimeData *PiecesModel::mimeData(const QModelIndexList &indexes) const
119{
120 QMimeData *mimeData = new QMimeData();
121 QByteArray encodedData;
122
123 QDataStream stream(&encodedData, QIODevice::WriteOnly);
124
125 foreach (QModelIndex index, indexes) {
126 if (index.isValid()) {
127 QPixmap pixmap = qVariantValue<QPixmap>(data(index, Qt::UserRole));
128 QPoint location = data(index, Qt::UserRole+1).toPoint();
129 stream << pixmap << location;
130 }
131 }
132
133 mimeData->setData("image/x-puzzle-piece", encodedData);
134 return mimeData;
135}
136
137bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
138 int row, int column, const QModelIndex &parent)
139{
140 if (!data->hasFormat("image/x-puzzle-piece"))
141 return false;
142
143 if (action == Qt::IgnoreAction)
144 return true;
145
146 if (column > 0)
147 return false;
148
149 int endRow;
150
151 if (!parent.isValid()) {
152 if (row < 0)
153 endRow = pixmaps.size();
154 else
155 endRow = qMin(row, pixmaps.size());
156 } else
157 endRow = parent.row();
158
159 QByteArray encodedData = data->data("image/x-puzzle-piece");
160 QDataStream stream(&encodedData, QIODevice::ReadOnly);
161
162 while (!stream.atEnd()) {
163 QPixmap pixmap;
164 QPoint location;
165 stream >> pixmap >> location;
166
167 beginInsertRows(QModelIndex(), endRow, endRow);
168 pixmaps.insert(endRow, pixmap);
169 locations.insert(endRow, location);
170 endInsertRows();
171
172 ++endRow;
173 }
174
175 return true;
176}
177
178int PiecesModel::rowCount(const QModelIndex &parent) const
179{
180 if (parent.isValid())
181 return 0;
182 else
183 return pixmaps.size();
184}
185
186Qt::DropActions PiecesModel::supportedDropActions() const
187{
188 return Qt::CopyAction | Qt::MoveAction;
189}
190
191void PiecesModel::addPieces(const QPixmap& pixmap)
192{
193 beginRemoveRows(QModelIndex(), 0, 24);
194 pixmaps.clear();
195 locations.clear();
196 endRemoveRows();
197 for (int y = 0; y < 5; ++y) {
198 for (int x = 0; x < 5; ++x) {
199 QPixmap pieceImage = pixmap.copy(x*80, y*80, 80, 80);
200 addPiece(pieceImage, QPoint(x, y));
201 }
202 }
203}
Note: See TracBrowser for help on using the repository browser.