#include "piecesmodel.h"
#include <QIcon>
#include <QMimeData>
PiecesModel::PiecesModel(int pieceSize, QObject *parent)
: QAbstractListModel(parent), m_PieceSize(pieceSize)
{
}
// override function of abstract list model
// read data by data role of the current model index
QVariant PiecesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
// get the image of small piece from the list model
if (role == Qt::DecorationRole)
return QIcon(pixmaps.value(index.row()).scaled(m_PieceSize, m_PieceSize,
Qt::KeepAspectRatio, Qt::SmoothTransformation));
// get the image current rows
else if (role == Qt::UserRole)
return pixmaps.value(index.row());
// get the corrent position
else if (role == Qt::UserRole + 1)
return locations.value(index.row());
return QVariant();
}
// insert one piece image into list model
void PiecesModel::addPiece(const QPixmap &pixmap, const QPoint &location)
{
int row;
if (int(2.0 * qrand() / (RAND_MAX + 1.0)) == 1)
row = 0;
else
row = pixmaps.size();
beginInsertRows(QModelIndex(), row, row);
pixmaps.insert(row, pixmap);
locations.insert(row, location);
endInsertRows();
}
Qt::ItemFlags PiecesModel::flags(const QModelIndex &index) const
{
// set the every piece image could be draged
if (index.isValid())
return (QAbstractListModel::flags(index) | Qt::ItemIsDragEnabled);
return Qt::ItemIsDropEnabled;
}
// remove the data from the list model
bool PiecesModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (parent.isValid())
return false;
if (row >= pixmaps.size() || row + count <= 0)
return false;
int beginRow = qMax(0, row);
int endRow = qMin(row + count - 1, pixmaps.size() - 1);
beginRemoveRows(parent, beginRow, endRow);
// do the remove all piece images from data model
while (beginRow <= endRow)
{
pixmaps.removeAt(beginRow);
locations.removeAt(beginRow);
++beginRow;
}
endRemoveRows();
return true;
}
// override funcution of QAbstractListModel
QStringList PiecesModel::mimeTypes() const
{
QStringList types;
types << "image/x-puzzle-piece";
return types;
}
QMimeData *PiecesModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (QModelIndex index, indexes)
{
if (index.isValid())
{
// wirte the image and corrent position into Mime Data
QPixmap pixmap = qvariant_cast<QPixmap>(data(index, Qt::UserRole));
QPoint location = data(index, Qt::UserRole+1).toPoint();
stream << pixmap << location;
}
}
// set the all images into mime data
mimeData->setData("image/x-puzzle-piece", encodedData);
return mimeData;
}
// override function of QAbstractListModel
// Handles the data supplied by a drag and drop operation that ended
// with the given action.
bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent)
{
if (!data->hasFormat("image/x-puzzle-piece"))
return false;
if (action == Qt::IgnoreAction)
return true;
if (column > 0)
return false;
int endRow;
if (!parent.isValid())
{
if (row < 0)
endRow = pixmaps.size();
else
endRow = qMin(row, pixmaps.size());
} else
{
endRow = parent.row();
}
// Returns the data stored in the object in the format described
// by the MIME type specified by mimeType.
QByteArray encodedData = data->data("image/x-puzzle-piece");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
while (!stream.atEnd())
{
QPixmap pixmap;
QPoint location;
// read the pixmap & the image position from the Droped mime data
stream >> pixmap >> location;
// add the current pixmap and the position data
beginInsertRows(QModelIndex(), endRow, endRow);
pixmaps.insert(endRow, pixmap);
locations.insert(endRow, location);
endInsertRows();
++endRow;
}
return true;
}
// override function of QAbstractListModel
// Returns the number of rows under the given parent.
// When the parent is valid it means that rowCount is returning the number of children of parent.
int PiecesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return pixmaps.size();
}
// override function of QAbstractListModel
// Returns the drop actions supported by this model.
Qt::DropActions PiecesModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
void PiecesModel::addPieces(const QPixmap& pixmap)
{
// clear the image pieces list
beginRemoveRows(QModelIndex(), 0, 24);
pixmaps.clear();
locations.clear();
endRemoveRows();
// split the image into a martix: 5 x 5
// there will have 25 piece images
for (int y = 0; y < 5; ++y)
{
for (int x = 0; x < 5; ++x)
{
// split out one piece image
QPixmap pieceImage = pixmap.copy(x*m_PieceSize, y*m_PieceSize, m_PieceSize, m_PieceSize);
// add this piece image into image pieces list, into data model
// every piece image has only one correct position, this used for check the result
addPiece(pieceImage, QPoint(x, y));
}
}
}