-
Notifications
You must be signed in to change notification settings - Fork 1
/
advancementtabdock.cpp
193 lines (172 loc) · 6.96 KB
/
advancementtabdock.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "advancementtabdock.h"
#include "ui_advancementtabdock.h"
#include "inventoryitem.h"
#include "globalhelpers.h"
#include "platforms/windows_specific.h"
#include <QDirIterator>
#include <QJsonDocument>
#include <QJsonObject>
AdvancementTabDock::AdvancementTabDock(QWidget *parent) :
QDockWidget(parent), ui(new Ui::AdvancementTabDock) {
ui->setupUi(this);
hide();
connect(this, &QDockWidget::visibilityChanged, this,
[this](const bool visible) {
if (visible && m_pendingLoad) {
m_pendingLoad = false;
loadAdvancements();
}
});
connect(ui->reloadBtn, &QToolButton::clicked, this,
&AdvancementTabDock::loadAdvancements);
connect(this, &QDockWidget::topLevelChanged, [ = ](bool floating) {
if (floating) {
Windows::setDarkFrameIfDarkMode(this);
}
});
}
AdvancementTabDock::~AdvancementTabDock() {
delete ui;
}
void AdvancementTabDock::loadAdvancements() {
const static QMap<QString,
AdvancemDisplayInfo::FrameType> stringToFrameType = {
{ "task", AdvancemDisplayInfo::FrameType::Task },
{ "goal", AdvancemDisplayInfo::FrameType::Goal },
{ "challenge", AdvancemDisplayInfo::FrameType::Challenge },
};
if (isHidden()) {
m_pendingLoad = true;
return;
}
#ifndef QT_NO_CURSOR
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
#endif
// Read all showable advancements
QDir &&dir = QDir::current();
const QString &&dirPath = dir.path();
dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
dir.setNameFilters({ "*.json"_QL1 });
QDirIterator it(dir, QDirIterator::Subdirectories);
std::map<QString, AdvancemDisplayInfo> advancements;
QVector<QString> rootAdvancements;
while (it.hasNext()) {
const QString &&path = it.next();
const auto &&finfo = it.fileInfo();
if (finfo.isFile()) {
if (Glhp::pathToFileType(dirPath, path) == CodeFile::Advancement) {
QJsonObject obj;
QFile file(path);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QByteArray &&data = file.readAll();
const auto &&doc = QJsonDocument::fromJson(data);
if (!doc.isNull() && doc.isObject()) {
obj = doc.object();
}
file.close();
}
if (!obj.isEmpty()) {
AdvancemDisplayInfo advancemInfo;
if (obj.contains("display")) {
const auto &&display = obj["display"].toObject();
if (display.contains("icon")) {
const auto &&icon = display["icon"].toObject();
if (icon.contains("item")) {
const auto &&item = icon["item"].toString();
if (!item.isEmpty()) {
advancemInfo.displayIcon =
InventoryItem{ item }.getPixmap();
} else {
qWarning() << "No item in" <<
Glhp::toNamespacedID(dirPath, path);
continue;
}
}
}
if (display.contains("title")) {
advancemInfo.title = display["title"];
} else {
qWarning() << "No title in" << Glhp::toNamespacedID(
dirPath, path);
continue;
}
if (display.contains("description")) {
advancemInfo.description = display["description"];
} else {
qWarning() << "No description in" <<
Glhp::toNamespacedID(dirPath, path);
continue;
}
advancemInfo.backgroundPath =
display.value("background").toString();
advancemInfo.frameType =
stringToFrameType.value(
display.value("frame").toString("task"));
advancemInfo.hidden = display.value("hidden").toBool();
} else {
continue;
}
advancemInfo.parent = obj.value("parent").toString();
if (!obj.contains("criteria")) {
qWarning() << "No criteria in" << Glhp::toNamespacedID(
dirPath, path);
continue;
}
const auto &&id = Glhp::toNamespacedID(dirPath, path);
advancements[id] = std::move(advancemInfo);
}
}
}
}
// Build advancement trees from the list
QMap<QString, AdvancemDisplayInfo *> advancementRefs;
for (auto &[key, value]: advancements) {
advancementRefs[key] = &value;
}
for (auto it = advancementRefs.cbegin(); it != advancementRefs.cend();
++it) {
const auto &parent = it.value()->parent;
if (parent.isEmpty()) {
rootAdvancements += it.key();
} else if (advancementRefs.contains(parent)) {
advancementRefs.value(parent)->children.insert(advancements.extract(
it.key()));
}
}
// Populate advancement tabs
for (const auto &id: qAsConst(rootAdvancements)) {
if (m_tabs.contains(id)) {
m_tabs.value(id)->setRootAdvancement(std::move(advancements.at(id)),
id);
} else {
auto *tab = new AdvancementTab();
tab->setRootAdvancement(std::move(advancements.at(id)), id);
connect(tab, &AdvancementTab::openFileRequested,
this, &AdvancementTabDock::openFileRequested);
ui->tabWidget->addTab(tab, id);
m_tabs[id] = tab;
}
}
for (auto it = m_tabs.begin(); it != m_tabs.end();) {
if (!rootAdvancements.contains(it.key())) {
ui->tabWidget->removeTab(ui->tabWidget->indexOf(it.value()));
it.value()->deleteLater();
it = m_tabs.erase(it);
} else {
++it;
}
}
#ifndef QT_NO_CURSOR
QGuiApplication::restoreOverrideCursor();
#endif
}
void AdvancementTabDock::changeEvent(QEvent *e) {
QDockWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}