GrabBag/BeltTearingApp/models/ImageInfoModel.cpp

52 lines
1.6 KiB
C++

#include "ImageInfoModel.h"
#include <QFileInfo>
#include <QImageReader>
ImageInfoModel::ImageInfoModel(QObject* parent)
: QAbstractListModel(parent) {}
int ImageInfoModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid()) return 0;
return m_items.size();
}
QVariant ImageInfoModel::data(const QModelIndex &index, int role) const {
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size())
return {};
const ImageInfo &info = m_items.at(index.row());
switch (role) {
case Qt::DisplayRole: return info.name;
case PathRole: return info.path;
case SizeRole: return static_cast<qlonglong>(info.sizeBytes);
case DimensionsRole: return QVariant::fromValue(info.dimensions);
default: return {};
}
}
QHash<int, QByteArray> ImageInfoModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "display";
roles[PathRole] = "path";
roles[SizeRole] = "size";
roles[DimensionsRole] = "dimensions";
return roles;
}
void ImageInfoModel::setFiles(const QStringList& filePaths) {
beginResetModel();
m_items.clear();
m_items.reserve(filePaths.size());
for (const QString &p : filePaths) {
QFileInfo fi(p);
ImageInfo info;
info.path = fi.absoluteFilePath();
info.name = fi.fileName();
info.sizeBytes = fi.size();
QImageReader reader(p);
info.dimensions = reader.size();
m_items.push_back(info);
}
endResetModel();
}