119 lines
No EOL
3 KiB
C++
119 lines
No EOL
3 KiB
C++
#include "ArtModel.h"
|
|
|
|
#include <QDirIterator>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QIcon>
|
|
#include <QImage>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QPixmap>
|
|
|
|
ArtModel::ArtModel(const QString& definitionDirectory, const QString& assetDirectory) : QAbstractTableModel() {
|
|
QDirIterator it(definitionDirectory);
|
|
while (it.hasNext()) {
|
|
QFileInfo info(it.next());
|
|
if(!info.isFile()) {
|
|
continue;
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), m_artPieces.size(), m_artPieces.size() + 1);
|
|
|
|
ArtPiece p;
|
|
loadData(p, QString("%1/%2").arg(definitionDirectory).arg(info.baseName()), QString("%1/%2").arg(assetDirectory).arg(info.baseName()));
|
|
|
|
m_artPieces.push_back(p);
|
|
|
|
endInsertRows();
|
|
}
|
|
|
|
std::sort(m_artPieces.begin(), m_artPieces.end(), [](ArtPiece& a, ArtPiece& b)
|
|
{
|
|
return a.date > b.date;
|
|
});
|
|
|
|
dataChanged(index(0, 0), index(m_artPieces.size(), 0));
|
|
}
|
|
|
|
int ArtModel::rowCount(const QModelIndex &parent) const {
|
|
return m_artPieces.size();
|
|
}
|
|
|
|
int ArtModel::columnCount(const QModelIndex &parent) const {
|
|
return 4;
|
|
}
|
|
|
|
QVariant ArtModel::data(const QModelIndex &index, int role) const {
|
|
if (!index.isValid())
|
|
return {};
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
switch(index.column()) {
|
|
case 0:
|
|
return m_artPieces[index.row()].filename;
|
|
case 1:
|
|
return {};
|
|
case 2:
|
|
return m_artPieces[index.row()].title;
|
|
case 3:
|
|
return m_artPieces[index.row()].hasAltText;
|
|
}
|
|
} else if (role == Qt::UserRole) {
|
|
return m_artPieces[index.row()].object;
|
|
} else if (role == Qt::DecorationRole) {
|
|
switch(index.column()) {
|
|
case 1:
|
|
{
|
|
QImage image;
|
|
image.load(m_artPieces[index.row()].filename);
|
|
|
|
return QPixmap::fromImage(image).scaled(100, 100, Qt::AspectRatioMode::KeepAspectRatio);
|
|
}
|
|
break;
|
|
case 3:
|
|
return m_artPieces[index.row()].hasAltText ? QIcon::fromTheme("emblem-checked") : QIcon::fromTheme("emblem-error");
|
|
}
|
|
} else if (role == Qt::UserRole + 1) {
|
|
return m_artPieces[index.row()].jsonFilename;
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
QVariant ArtModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
if (orientation == Qt::Orientation::Horizontal && role == Qt::DisplayRole) {
|
|
switch(section) {
|
|
case 0:
|
|
return "Filename";
|
|
case 1:
|
|
return "Image";
|
|
case 2:
|
|
return "Title";
|
|
case 3:
|
|
return "Has Alt Text";
|
|
}
|
|
}
|
|
|
|
return QAbstractTableModel::headerData(section, orientation, role);
|
|
}
|
|
|
|
|
|
void ArtModel::loadData(ArtPiece& piece, const QString& filename, const QString& assetFilename) {
|
|
piece.jsonFilename = filename + ".json";
|
|
piece.filename = assetFilename;
|
|
|
|
QFile artFile(piece.jsonFilename);
|
|
artFile.open(QFile::ReadOnly);
|
|
QJsonDocument artJson = QJsonDocument::fromJson(artFile.readAll());
|
|
|
|
if(artJson["date"].toString().contains("-")) {
|
|
piece.date = QDate::fromString(artJson["date"].toString(), "yyyy-MM-dd");
|
|
} else {
|
|
piece.date = QDate::fromString(artJson["date"].toString(), "yyyy");
|
|
}
|
|
|
|
if(artJson.object().contains("alt_text")) {
|
|
piece.hasAltText = true;
|
|
}
|
|
} |