Files
QtGameMaker/models/spritesmodel.cpp
T

55 lines
1.2 KiB
C++
Raw Normal View History

2022-01-09 21:07:41 +01:00
#include "spritesmodel.h"
#include <QPixmap>
2022-01-10 21:49:06 +01:00
#include <QDebug>
2022-01-09 21:07:41 +01:00
2022-01-10 21:49:06 +01:00
SpritesModel::SpritesModel(const std::vector<QPixmap> &pixmaps, QObject *parent) :
QAbstractListModel{parent},
m_pixmaps{pixmaps}
2022-01-09 21:07:41 +01:00
{
}
int SpritesModel::rowCount(const QModelIndex &parent) const
{
2022-01-11 02:35:26 +01:00
Q_UNUSED(parent)
2022-01-10 21:49:06 +01:00
return m_pixmaps.size();
2022-01-09 21:07:41 +01:00
}
QVariant SpritesModel::data(const QModelIndex &index, int role) const
{
2022-01-10 21:49:06 +01:00
if (index.column() != 0)
return {};
2022-01-09 21:07:41 +01:00
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
return tr("image %0").arg(index.row());
case Qt::DecorationRole:
2022-01-10 21:49:06 +01:00
if (index.row() < 0 || index.row() >= m_pixmaps.size())
{
qWarning() << "invalid row" << index.row();
return {};
}
const auto &pixmap = m_pixmaps.at(index.row());
if (pixmap.isNull())
{
QPixmap pixmap{32, 32};
pixmap.fill(Qt::white);
return pixmap;
}
2022-01-09 21:07:41 +01:00
return pixmap;
}
return {};
}
2022-01-10 21:49:06 +01:00
const QPixmap &SpritesModel::pixmap(const QModelIndex &index) const
{
if (index.row() < 0 || index.row() >= m_pixmaps.size())
qFatal("index out of range");
return m_pixmaps.at(index.row());
}