Make the ArtModel::loadData function a constructor for ArtPiece
This commit is contained in:
parent
aec3ffa4b3
commit
4216387c14
2 changed files with 34 additions and 32 deletions
|
@ -38,14 +38,13 @@ ArtModel::ArtModel(const QDir &definitionDirectory, const QDir &assetDirectory,
|
||||||
|
|
||||||
beginInsertRows(QModelIndex(), m_artPieces.size(), m_artPieces.size() + 1);
|
beginInsertRows(QModelIndex(), m_artPieces.size(), m_artPieces.size() + 1);
|
||||||
|
|
||||||
m_artPieces.push_back(ArtPiece{});
|
m_artPieces.resize(m_artPieces.size());
|
||||||
|
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::function<ArtPiece(const PieceInformation &info)> loadPiece = [this](const PieceInformation &info) -> ArtPiece {
|
const std::function<ArtPiece(const PieceInformation &info)> loadPiece = [this](const PieceInformation &info) -> ArtPiece {
|
||||||
ArtPiece p;
|
ArtPiece p(info.definition, info.asset);
|
||||||
loadData(p, info.definition, info.asset);
|
|
||||||
|
|
||||||
p.image.load(p.filename);
|
p.image.load(p.filename);
|
||||||
p.thumbnail = QPixmap::fromImage(p.image).scaled(100, 100, Qt::AspectRatioMode::KeepAspectRatio).toImage();
|
p.thumbnail = QPixmap::fromImage(p.image).scaled(100, 100, Qt::AspectRatioMode::KeepAspectRatio).toImage();
|
||||||
|
@ -121,31 +120,6 @@ QVariant ArtModel::headerData(int section, Qt::Orientation orientation, int role
|
||||||
return QAbstractTableModel::headerData(section, orientation, role);
|
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);
|
|
||||||
|
|
||||||
const QJsonDocument artJson = QJsonDocument::fromJson(artFile.readAll());
|
|
||||||
|
|
||||||
if (artJson[QStringLiteral("date")].toString().contains(QStringLiteral("-"))) {
|
|
||||||
piece.date = QDate::fromString(artJson[QStringLiteral("date")].toString(), QStringLiteral("yyyy-MM-dd"));
|
|
||||||
} else {
|
|
||||||
piece.date = QDate::fromString(artJson[QStringLiteral("date")].toString(), QStringLiteral("yyyy"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (artJson.object().contains(QStringLiteral("title"))) {
|
|
||||||
piece.title = artJson.object()[QStringLiteral("title")].toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (artJson.object().contains(QStringLiteral("alt_text"))) {
|
|
||||||
piece.hasAltText = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ArtModel::pieceFinished(const int row)
|
void ArtModel::pieceFinished(const int row)
|
||||||
{
|
{
|
||||||
m_artPieces[row] = piecesFuture->resultAt(row);
|
m_artPieces[row] = piecesFuture->resultAt(row);
|
||||||
|
@ -161,3 +135,28 @@ void ArtModel::finished()
|
||||||
|
|
||||||
Q_EMIT dataChanged(index(0, 0), index(m_artPieces.size(), 0));
|
Q_EMIT dataChanged(index(0, 0), index(m_artPieces.size(), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArtPiece::ArtPiece(const QString &filename, const QString &assetFilename)
|
||||||
|
{
|
||||||
|
jsonFilename = filename + ".json";
|
||||||
|
this->filename = assetFilename;
|
||||||
|
|
||||||
|
QFile artFile(jsonFilename);
|
||||||
|
artFile.open(QFile::ReadOnly);
|
||||||
|
|
||||||
|
const QJsonDocument artJson = QJsonDocument::fromJson(artFile.readAll());
|
||||||
|
|
||||||
|
if (artJson[QStringLiteral("date")].toString().contains(QStringLiteral("-"))) {
|
||||||
|
date = QDate::fromString(artJson[QStringLiteral("date")].toString(), QStringLiteral("yyyy-MM-dd"));
|
||||||
|
} else {
|
||||||
|
date = QDate::fromString(artJson[QStringLiteral("date")].toString(), QStringLiteral("yyyy"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (artJson.object().contains(QStringLiteral("title"))) {
|
||||||
|
title = artJson.object()[QStringLiteral("title")].toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (artJson.object().contains(QStringLiteral("alt_text"))) {
|
||||||
|
hasAltText = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -10,7 +10,12 @@
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
struct ArtPiece {
|
class ArtPiece
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ArtPiece() = default;
|
||||||
|
ArtPiece(const QString &filename, const QString &assetFilename);
|
||||||
|
|
||||||
QString filename, jsonFilename;
|
QString filename, jsonFilename;
|
||||||
QString title;
|
QString title;
|
||||||
QJsonObject object;
|
QJsonObject object;
|
||||||
|
@ -34,12 +39,10 @@ public:
|
||||||
[[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
[[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadData(ArtPiece &piece, const QString &filename, const QString &assetFilename);
|
|
||||||
|
|
||||||
void pieceFinished(int index);
|
void pieceFinished(int index);
|
||||||
void finished();
|
void finished();
|
||||||
|
|
||||||
QFutureWatcher<ArtPiece> *piecesFuture;
|
QFutureWatcher<ArtPiece> *piecesFuture;
|
||||||
|
|
||||||
QList<ArtPiece> m_artPieces;
|
QVector<ArtPiece> m_artPieces;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue