-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
413 lines (338 loc) · 12.9 KB
/
mainwindow.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "classes/replay.h"
#include "sceneimageviewer.h"
#include <qfontdatabase.h>
#include "worker.h"
#include "version.h"
#include "preferencesdialog.h"
#include <QPainter>
#include <QTreeView>
#include <QStandardItemModel>
#include <QDir>
#include <QFileInfo>
#include <QDateTime>
#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMap>
#include <QThread>
#include <QStandardItem>
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <QLabel>
#include <QDebug>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QPushButton>
#include <QSettings>
#include <QMessageBox>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), ui(new Ui::MainWindow),
model(new QStandardItemModel(this)),
m_thread(nullptr),
m_worker(nullptr)
{
ui->setupUi(this);
QSettings settings("sgambe33", "wtplotter");
if (!settings.value("replayFolderPath").isNull()) {
populateReplayTreeView(ui->replayTreeView, settings.value("replayFolderPath").toString());
}
ui->splitter->setStretchFactor(0, 2);
ui->splitter->setStretchFactor(1, 3);
QPixmap img(":/map_images/unknownmap.png");
img = img.scaled(125, 125, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->mapImage->setPixmap(img);
QStatusBar* statusBar = ui->statusbar;
QWidget* buttonBox = new QWidget(this);
QHBoxLayout* layout = new QHBoxLayout(buttonBox);
layout->setContentsMargins(0, 0, 0, 0);
QPushButton* replayButton = new QPushButton("Replays", buttonBox);
QPushButton* plotterButton = new QPushButton("Plotter", buttonBox);
connect(replayButton, &QPushButton::clicked, [=] {
ui->stackedWidget->setCurrentIndex(0);
ui->replayTreeView->setDisabled(false);
plotterButton->setDisabled(false);
replayButton->setDisabled(true);
stopPlotter();
});
connect(plotterButton, &QPushButton::clicked, [=] {
ui->stackedWidget->setCurrentIndex(2);
ui->replayTreeView->setDisabled(true);
plotterButton->setDisabled(true);
replayButton->setDisabled(false);
startPlotter();
});
layout->addWidget(replayButton);
layout->addWidget(plotterButton);
statusBar->addPermanentWidget(buttonBox);
ui->replayTreeView->setDisabled(true);
plotterButton->setDisabled(true);
checkForUpdates();
startPlotter();
connect(ui->replayTreeView, &QTreeView::clicked, this, &MainWindow::onTreeItemClicked);
connect(ui->actionPreferences, &QAction::triggered, this, &MainWindow::openPreferencesDialog);
connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::openAboutDialog);
connect(ui->actionQuit, &QAction::triggered, qApp, &QCoreApplication::quit);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openPreferencesDialog()
{
PreferencesDialog dialog(this);
dialog.exec();
}
void MainWindow::openAboutDialog()
{
QString aboutText = R"(
<p>WT Plotter is a tool for reading War Thunder replays and record match development. This
project is developed by <strong>Sgambe33</strong> and is fully open source.You can find the
source code and contribute to the project on <a href='https://github.com/sgambe33/wt-plotter'>
GitHub</a>.</p>
<p>Thank you for using WT Plotter!</p>
)";
QMessageBox::about(this, "About WT Plotter", aboutText);
}
void MainWindow::setCustomFont(const QString& fontPath, QWidget* widget) {
int id = QFontDatabase::addApplicationFont(fontPath);
if (id != -1) {
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
QFont customFont(family);
widget->setFont(customFont);
for (auto child : widget->findChildren<QWidget*>()) {
child->setFont(customFont);
}
}
else {
qDebug() << "Failed to load font from" << fontPath;
}
}
void MainWindow::startPlotter() {
qDebug() << "Starting plotter...";
m_thread = new QThread();
m_worker = new Worker(ui->mappa);
connect(m_thread, &QThread::started, m_worker, &Worker::performTask);
connect(qApp, &QCoreApplication::aboutToQuit, this, &MainWindow::stopPlotter);
connect(m_thread, &QThread::finished, m_worker, &QObject::deleteLater);
connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater);
connect(m_worker, &Worker::updatePixmap, ui->mappa, &SceneImageViewer::setPixmap);
connect(m_worker, &Worker::updateStatusLabel, this, &MainWindow::updateStatusLabel);
connect(m_worker, &Worker::updateProgressBar, this, &MainWindow::updateProgressBar);
connect(m_worker, &Worker::changeStackedWidget, this, &MainWindow::changeStackedWidget);
m_worker->moveToThread(m_thread);
m_thread->start();
}
void MainWindow::updatePixmap(const QPixmap& pixmap)
{
ui->mappa->setPixmap(pixmap);
}
void MainWindow::changeStackedWidget(int index)
{
ui->stackedWidget->setCurrentIndex(index);
}
void MainWindow::updateProgressBar(double progress) {
ui->progressBar->setValue(progress);
}
void MainWindow::updateStatusLabel(QString msg) {
ui->statusLabel->setText(msg);
}
void MainWindow::stopPlotter() {
if (m_thread && m_worker) {
QMetaObject::invokeMethod(m_worker, "stopTimer", Qt::QueuedConnection);
m_thread->quit();
m_thread->wait();
m_thread = nullptr;
m_worker = nullptr;
qDebug() << "Killing plotter and thread...";
}
}
void MainWindow::populateReplayTreeView(QTreeView* replayTreeView, const QString& directoryPath)
{
model->setHorizontalHeaderLabels({ "File Name" });
QMap<QDate, QList<QFileInfo>> filesByDate;
QDir dir(directoryPath);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
dir.setNameFilters({ "*.wrpl" });
QFileInfoList fileInfoList = dir.entryInfoList();
for (const QFileInfo& fileInfo : fileInfoList)
{
QDate creationDate = fileInfo.birthTime().date();
filesByDate[creationDate].append(fileInfo);
}
for (auto it = filesByDate.cbegin(); it != filesByDate.cend(); ++it)
{
QStandardItem* dateItem = new QStandardItem(it.key().toString("yyyy-MM-dd"));
dateItem->setFlags(dateItem->flags() & ~Qt::ItemIsEditable);
for (const QFileInfo& fileInfo : it.value())
{
QStandardItem* fileNameItem = new QStandardItem(fileInfo.fileName());
fileNameItem->setData(fileInfo.absoluteFilePath(), Qt::UserRole);
fileNameItem->setFlags(fileNameItem->flags() & ~Qt::ItemIsEditable);
dateItem->appendRow({ fileNameItem });
}
model->appendRow(dateItem);
}
replayTreeView->setModel(model);
replayTreeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
void MainWindow::onTreeItemClicked(const QModelIndex& index)
{
QString filePath = index.data(Qt::UserRole).toString();
if (!filePath.isEmpty())
{
qDebug() << filePath;
executeCommand(filePath);
}
}
void MainWindow::executeCommand(const QString& filePath)
{
Replay rep = Replay::fromFile(filePath);
QPixmap mapPixmap(":/map_images/" + rep.getLevel() + "_tankmap_thumb.png");
if (mapPixmap.isNull()) {
mapPixmap = QPixmap(":/map_images/unknownmap.png");
}
ui->mapImage->setPixmap(mapPixmap);
ui->mapNameLabel->setText(QString("Map: ") + rep.getLevel());
QDateTime startTime = QDateTime::fromSecsSinceEpoch(rep.getStartTime());
QString formattedStartTime = startTime.toString("hh:mm:ss");
ui->startTimeLabel->setText(QString("Start time: ") + formattedStartTime);
ui->timePlayedLabel->setText(QString("Time played: ") + QString::number(rep.getTimePlayed()));
ui->resultLabel->setText(QString("Result: ") + rep.getStatus());
QList<Player> players = rep.getPlayers();
QList<Player> allies;
QList<Player> axis;
for (const Player& player : players)
{
if (player.getTeam() == 1)
{
allies.append(player);
}
else if (player.getTeam() == 2)
{
axis.append(player);
}
}
std::sort(allies.begin(), allies.end(), [](const Player& p1, const Player& p2)
{
return p1.getScore() > p2.getScore();
});
std::sort(axis.begin(), axis.end(), [](const Player& p1, const Player& p2)
{
return p1.getScore() > p2.getScore();
});
populateTeamTable(ui->alliesTable, allies);
populateTeamTable(ui->axisTable, axis);
}
void MainWindow::populateTeamTable(QTableWidget* table, const QList<Player>& players)
{
table->clear();
table->setRowCount(players.size());
table->setColumnCount(10);
QPixmap scorePixmap(":/icons/score.png");
QPixmap killsPixmap(":/icons/kills.png");
QPixmap groundKillsPixmap(":/icons/groundKills.png");
QPixmap navalKillsPixmap(":/icons/navalKills.png");
QPixmap assistsPixmap(":/icons/assists.png");
QPixmap capturedZonesPixmap(":/icons/capturedZones.png");
QPixmap aiKillsPixmap(":/icons/aiKills.png");
QPixmap awardDamagePixmap(":/icons/awardDamage.png");
QPixmap damageZonePixmap(":/icons/damageZone.png");
QPixmap deathsPixmap(":/icons/deaths.png");
table->setHorizontalHeaderItem(0, new QTableWidgetItem(QString("Name")));
table->setHorizontalHeaderItem(1, new QTableWidgetItem(QIcon(scorePixmap), ""));
table->setHorizontalHeaderItem(2, new QTableWidgetItem(QIcon(killsPixmap), ""));
table->setHorizontalHeaderItem(3, new QTableWidgetItem(QIcon(groundKillsPixmap), ""));
table->setHorizontalHeaderItem(4, new QTableWidgetItem(QIcon(navalKillsPixmap), ""));
table->setHorizontalHeaderItem(5, new QTableWidgetItem(QIcon(assistsPixmap), ""));
table->setHorizontalHeaderItem(6, new QTableWidgetItem(QIcon(capturedZonesPixmap), ""));
table->setHorizontalHeaderItem(7, new QTableWidgetItem(QIcon(aiKillsPixmap), ""));
table->setHorizontalHeaderItem(8, new QTableWidgetItem(QIcon(awardDamagePixmap), ""));
table->setHorizontalHeaderItem(9, new QTableWidgetItem(QIcon(damageZonePixmap), ""));
table->setHorizontalHeaderItem(10, new QTableWidgetItem(QIcon(deathsPixmap), ""));
for (int i = 0; i < players.size(); ++i)
{
const Player& player = players[i];
table->setItem(i, 0, new QTableWidgetItem(player.getClanTag() + " " + player.getName()));
QTableWidgetItem* scoreItem = new QTableWidgetItem(QString::number(player.getScore()));
scoreItem->setToolTip("Score");
table->setItem(i, 1, scoreItem);
QTableWidgetItem* killsItem = new QTableWidgetItem(QString::number(player.getKills()));
killsItem->setToolTip("Air kills");
table->setItem(i, 2, killsItem);
QTableWidgetItem* groundKillsItem = new QTableWidgetItem(QString::number(player.getGroundKills()));
groundKillsItem->setToolTip("Ground kills");
table->setItem(i, 3, groundKillsItem);
QTableWidgetItem* navalKillsItem = new QTableWidgetItem(QString::number(player.getNavalKills()));
navalKillsItem->setToolTip("Naval kills");
table->setItem(i, 4, navalKillsItem);
QTableWidgetItem* assistsItem = new QTableWidgetItem(QString::number(player.getAssists()));
assistsItem->setToolTip("Assists");
table->setItem(i, 5, assistsItem);
QTableWidgetItem* captureZoneItem = new QTableWidgetItem(QString::number(player.getCaptureZone()));
captureZoneItem->setToolTip("Captured zones");
table->setItem(i, 6, captureZoneItem);
QTableWidgetItem* aiKillsItem = new QTableWidgetItem(QString::number(player.getAiKills() + player.getAiGroundKills() + player.getAiNavalKills()));
aiKillsItem->setToolTip("AI kills");
table->setItem(i, 7, aiKillsItem);
QTableWidgetItem* awardDamageItem = new QTableWidgetItem(QString::number(player.getAwardDamage()));
awardDamageItem->setToolTip("Awarded damage");
table->setItem(i, 8, awardDamageItem);
QTableWidgetItem* damageZoneItem = new QTableWidgetItem(QString::number(player.getDamageZone()));
damageZoneItem->setToolTip("Bombing damage");
table->setItem(i, 9, damageZoneItem);
QTableWidgetItem* deathsItem = new QTableWidgetItem(QString::number(player.getDeaths()));
deathsItem->setToolTip("Deaths");
table->setItem(i, 10, deathsItem);
}
table->resizeColumnsToContents();
}
void MainWindow::checkForUpdates() {
QUrl url("https://raw.githubusercontent.com/Sgambe33/WT-Plotter/refs/heads/main/version.json");
QNetworkRequest request(url);
QNetworkAccessManager networkManager;
QNetworkReply* reply = networkManager.get(request);
QString appVersion = QString("%1.%2.%3")
.arg(APP_VERSION_MAJOR)
.arg(APP_VERSION_MINOR)
.arg(APP_VERSION_PATCH);
qDebug() << "Current app version:" << appVersion;
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() != QNetworkReply::NoError)
{
qWarning() << "Failed to fetch version information:" << reply->errorString();
reply->deleteLater();
return;
}
QByteArray responseData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);
reply->deleteLater();
if (!jsonDoc.isObject())
{
qWarning() << "Invalid JSON received.";
return;
}
QJsonObject jsonObj = jsonDoc.object();
QString latestVersion = jsonObj.value("version").toString();
if (latestVersion.isEmpty())
{
qWarning() << "Version key not found in JSON.";
return;
}
if (appVersion != latestVersion)
{
QMessageBox::warning(nullptr, "Update Required", R"(
<p>A new version of this app has been found. In order to keep shared data consistent, please update by
downloading the latest version <a href='https://github.com/Sgambe33/WT-Plotter/releases/latest'>
here</a>.</p><p>Thank you</p>)");
std::exit(0);
}
else
{
qInfo() << "You are using the latest version.";
}
}