-
Notifications
You must be signed in to change notification settings - Fork 2
/
credits.cpp
82 lines (70 loc) · 2.03 KB
/
credits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "credits.h"
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <QDir>
Credits::Credits(QObject *parent) : QObject(parent)
{
QFile source("assets/credits/credits.json");
if (source.open(QIODevice::ReadOnly))
{
const auto list = QJsonDocument::fromJson(source.readAll()).array();
for (const auto& personValue : list)
{
CreditPerson* person = new CreditPerson(this);
QJsonObject data = personValue.toObject();
QString category = data["category"].toString();
person->initialize(data);
if (!categories.contains(category))
{
categories.push_back(category);
persons.insert(category, {person});
}
else
persons[category].push_back(person);
}
}
}
unsigned short Credits::categoryCount(const QString& category) const
{
return persons[category].size();
}
CreditPerson* Credits::person(const QString& category, unsigned short index) const
{
if (persons[category].count() > index)
return persons[category][index];
return nullptr;
}
bool Credits::categoryHasArtwork(const QString& category) const
{
for (auto* person : persons[category])
{
if (person->hasAssets())
return true;
}
return false;
}
CreditPerson::CreditPerson(QObject* parent) : QObject(parent)
{
}
static QString getCreditResourcePath(const QString& filename)
{
const auto currentDir = QDir::currentPath();
const auto path = currentDir + '/' + filename;
if (QFile::exists(path))
return ("file:/" + path);
return ("qrc:/" + filename);
}
void CreditPerson::initialize(const QJsonObject& data)
{
auto currentDir = QDir::currentPath();
name = data["name"].toString();
url = data["url"].toString();
if (data["avatar"].isUndefined())
avatar = getCreditResourcePath("assets/credits/no-avatar.png");
else
avatar = getCreditResourcePath("assets/credits/" + data["avatar"].toString());
for (const QString& filename : data["files"].toVariant().toStringList())
assets << getCreditResourcePath(filename);
}