| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "piecesmodel.h" |
| 5 |
|
| 6 |
#include <QIcon> |
| 7 |
#include <QMimeData> |
| 8 |
#include <QRandomGenerator> |
| 9 |
|
| 10 |
PiecesModel::PiecesModel(int pieceSize, QObject *parent) |
| 11 |
: QAbstractListModel(parent), m_PieceSize(pieceSize) |
| 12 |
{ |
| 13 |
} |
| 14 |
|
| 15 |
QVariant PiecesModel::data(const QModelIndex &index, int role) const |
| 16 |
{ |
| 17 |
if (!index.isValid()) |
| 18 |
return QVariant(); |
| 19 |
|
| 20 |
if (role == Qt::DecorationRole) |
| 21 |
return QIcon(pixmaps.value(index.row()).scaled(m_PieceSize, m_PieceSize, |
| 22 |
Qt::KeepAspectRatio, Qt::SmoothTransformation)); |
| 23 |
else if (role == Qt::UserRole) |
| 24 |
return pixmaps.value(index.row()); |
| 25 |
else if (role == Qt::UserRole + 1) |
| 26 |
return locations.value(index.row()); |
| 27 |
|
| 28 |
return QVariant(); |
| 29 |
} |
| 30 |
|
| 31 |
void PiecesModel::addPiece(const QPixmap &pixmap, const QPoint &location) |
| 32 |
{ |
| 33 |
int row; |
| 34 |
if (QRandomGenerator::global()->bounded(2) == 1) |
| 35 |
row = 0; |
| 36 |
else |
| 37 |
row = pixmaps.size(); |
| 38 |
|
| 39 |
beginInsertRows(QModelIndex(), row, row); |
| 40 |
pixmaps.insert(row, pixmap); |
| 41 |
locations.insert(row, location); |
| 42 |
endInsertRows(); |
| 43 |
} |
| 44 |
|
| 45 |
Qt::ItemFlags PiecesModel::flags(const QModelIndex &index) const |
| 46 |
{ |
| 47 |
if (index.isValid()) |
| 48 |
return (QAbstractListModel::flags(index)|Qt::ItemIsDragEnabled); |
| 49 |
|
| 50 |
return Qt::ItemIsDropEnabled; |
| 51 |
} |
| 52 |
|
| 53 |
bool PiecesModel::removeRows(int row, int count, const QModelIndex &parent) |
| 54 |
{ |
| 55 |
if (parent.isValid()) |
| 56 |
return false; |
| 57 |
|
| 58 |
if (row >= pixmaps.size() || row + count <= 0) |
| 59 |
return false; |
| 60 |
|
| 61 |
int beginRow = qMax(0, row); |
| 62 |
int endRow = qMin(row + count - 1, pixmaps.size() - 1); |
| 63 |
|
| 64 |
beginRemoveRows(parent, beginRow, endRow); |
| 65 |
|
| 66 |
while (beginRow <= endRow) { |
| 67 |
pixmaps.removeAt(beginRow); |
| 68 |
locations.removeAt(beginRow); |
| 69 |
++beginRow; |
| 70 |
} |
| 71 |
|
| 72 |
endRemoveRows(); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
QStringList PiecesModel::mimeTypes() const |
| 77 |
{ |
| 78 |
QStringList types; |
| 79 |
types << "image/x-puzzle-piece"; |
| 80 |
return types; |
| 81 |
} |
| 82 |
|
| 83 |
QMimeData *PiecesModel::mimeData(const QModelIndexList &indexes) const |
| 84 |
{ |
| 85 |
QMimeData *mimeData = new QMimeData(); |
| 86 |
QByteArray encodedData; |
| 87 |
|
| 88 |
QDataStream stream(&encodedData, QDataStream::WriteOnly); |
| 89 |
|
| 90 |
for (const QModelIndex &index : indexes) { |
| 91 |
if (index.isValid()) { |
| 92 |
QPixmap pixmap = qvariant_cast<QPixmap>(data(index, Qt::UserRole)); |
| 93 |
QPoint location = data(index, Qt::UserRole+1).toPoint(); |
| 94 |
stream << pixmap << location; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
mimeData->setData("image/x-puzzle-piece", encodedData); |
| 99 |
return mimeData; |
| 100 |
} |
| 101 |
|
| 102 |
bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, |
| 103 |
int row, int column, const QModelIndex &parent) |
| 104 |
{ |
| 105 |
if (!data->hasFormat("image/x-puzzle-piece")) |
| 106 |
return false; |
| 107 |
|
| 108 |
if (action == Qt::IgnoreAction) |
| 109 |
return true; |
| 110 |
|
| 111 |
if (column > 0) |
| 112 |
return false; |
| 113 |
|
| 114 |
int endRow; |
| 115 |
|
| 116 |
if (!parent.isValid()) { |
| 117 |
if (row < 0) |
| 118 |
endRow = pixmaps.size(); |
| 119 |
else |
| 120 |
endRow = qMin(row, pixmaps.size()); |
| 121 |
} else { |
| 122 |
endRow = parent.row(); |
| 123 |
} |
| 124 |
|
| 125 |
QByteArray encodedData = data->data("image/x-puzzle-piece"); |
| 126 |
QDataStream stream(&encodedData, QDataStream::ReadOnly); |
| 127 |
|
| 128 |
while (!stream.atEnd()) { |
| 129 |
QPixmap pixmap; |
| 130 |
QPoint location; |
| 131 |
stream >> pixmap >> location; |
| 132 |
|
| 133 |
beginInsertRows(QModelIndex(), endRow, endRow); |
| 134 |
pixmaps.insert(endRow, pixmap); |
| 135 |
locations.insert(endRow, location); |
| 136 |
endInsertRows(); |
| 137 |
|
| 138 |
++endRow; |
| 139 |
} |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
int PiecesModel::rowCount(const QModelIndex &parent) const |
| 145 |
{ |
| 146 |
return parent.isValid() ? 0 : pixmaps.size(); |
| 147 |
} |
| 148 |
|
| 149 |
Qt::DropActions PiecesModel::supportedDropActions() const |
| 150 |
{ |
| 151 |
return Qt::CopyAction | Qt::MoveAction; |
| 152 |
} |
| 153 |
|
| 154 |
void PiecesModel::addPieces(const QPixmap &pixmap) |
| 155 |
{ |
| 156 |
if (!pixmaps.isEmpty()) { |
| 157 |
beginRemoveRows(QModelIndex(), 0, pixmaps.size() - 1); |
| 158 |
pixmaps.clear(); |
| 159 |
locations.clear(); |
| 160 |
endRemoveRows(); |
| 161 |
} |
| 162 |
for (int y = 0; y < 5; ++y) { |
| 163 |
for (int x = 0; x < 5; ++x) { |
| 164 |
QPixmap pieceImage = pixmap.copy(x*m_PieceSize, y*m_PieceSize, m_PieceSize, m_PieceSize); |
| 165 |
addPiece(pieceImage, QPoint(x, y)); |
| 166 |
} |
| 167 |
} |
| 168 |
} |