-
Notifications
You must be signed in to change notification settings - Fork 6
/
timelineimagegenerator.cpp
225 lines (183 loc) · 8.4 KB
/
timelineimagegenerator.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
#include "timelineimagegenerator.h"
#include "globals.h"
#include <QDebug>
#include <QPainter>
#include <QFontMetrics>
#include <QApplication>
#include <QClipboard>
#include <QDate>
#include <QMimeData>
#include <QPainterPath>
namespace Yate {
TimelineImageGenerator::TimelineImageGenerator(QString path, NightInfo &night, QString host, QSet<QString> squad, QObject *parent)
: QObject{parent}, path_(path), night_(night), host_(host), squad_(squad)
{
}
QImage* TimelineImageGenerator::generateImage()
{
qDebug() << "Generating timeline image.";
int summaryHeight = 100;
int footerHeight = 60;
int runNumHeight = 80;
int numberOfRuns = 0;
int maxLogSize = 0;
int runWidth = 680;
int evtHeight = 200;
int evtLeftOffset = -70;
for(int i = 0; i < night_.runs().size(); i++) {
auto &set = night_.run(i);
if (set.getNumberOfCaps() || set.getNumberOfKills()) {
numberOfRuns++;
maxLogSize = qMax(maxLogSize, set.eventLog().size());
}
}
int imageWidth = runWidth * numberOfRuns;
int imageHeight = evtHeight * maxLogSize + runNumHeight + summaryHeight + footerHeight;
static QMap<LogEventType, QVector<QString>> logEvtText{
{LogEventType::NightBegin, {"Night Started", ":/timeline/NightBegin.png"}},
{LogEventType::DayBegin, {"Day Started", ":/timeline/DayBegin.png"}},
{LogEventType::TeralystSpawn, {"Teralyst Emerging", ":/timeline/EidolonSpawn.png"}},
{LogEventType::LimbBreak, {"Limb Break", ":/timeline/LimbBreak.png"}},
{LogEventType::EidolonCapture, {"Eidolon Captured", ":/timeline/EidolonCapture.png"}},
{LogEventType::EidolonKill, {"Eidolon Killed", ":/timeline/EidolonKill.png"}},
{LogEventType::LootDrop, {"Loot Dropped", ":/timeline/LootDrop.png"}},
{LogEventType::ShrineEnable, {"Shrine Enabled", ":/timeline/ShrineEnable.png"}},
{LogEventType::ShardInsert, {"Shard Inserted", ":/timeline/ShardInsert.png"}},
{LogEventType::ShardRemove, {"Shard Removed", ":/timeline/ShardRemove.png"}},
{LogEventType::ShrineDisable, {"Shrine Disabled", ":/timeline/ShrineDisable.png"}},
{LogEventType::EidolonSpawn, {"Eidolon Spawned", ":/timeline/EidolonSpawn.png"}},
{LogEventType::EidolonTeleport, {"Eidolon Teleported", ":/timeline/EidolonTeleport.png"}},
{LogEventType::HostJoin, {"Host Joinned", ":/timeline/HostJoin.png"}},
{LogEventType::SquadJoin, {"Member Joined", ":/timeline/SquadJoin.png"}},
{LogEventType::HostUnload, {"Host Exit", ":/timeline/HostUnload.png"}},
{LogEventType::EidolonDespawn, {"Eidolons Despawning", ":/timeline/EidolonDespawn.png"}},
{LogEventType::Invalid, {"Invalid", ":/timeline/Invalid.png"}}
};
QImage *pix = new QImage(imageWidth, imageHeight, QImage::Format_ARGB32_Premultiplied);
QPainter painter(pix);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::TextAntialiasing, true);
painter.setPen(QPen(QColor(SUMMARY_COLOR_TITLE), 3));
painter.setBrush(QColor(SUMMARY_COLOR_TITLE));
QFont font = painter.font();
font.setFamily("Roboto Mono");
font.setPointSize(30);
QFontMetrics fm(font);
painter.setFont(font);
painter.fillRect(0, 0, pix->width(), pix->height(), QColor(SUMMARY_COLOR_BG));
int topOffset = 0;
int fw, fh;
int spaceWidth = QFontMetrics(painter.font()).horizontalAdvance(" ");
QString text;
text = "Squad";
font.setBold(true);
fm = QFontMetrics(font);
painter.setFont(font);
int xpos = 5;
int ypos = topOffset + summaryHeight / 2 + 4;
painter.setPen(QPen(QColor(SUMMARY_COLOR_TITLE), 2));
painter.drawText(xpos, ypos, text);
xpos += fm.horizontalAdvance(text) + 2 * spaceWidth;
QStringList squadList;
squadList.append(host_);
for(auto &m: squad_) {
if (m.trimmed() != "") {
squadList.append(m);
}
}
text = squadList.join(", ");
QString nightRes = night_.getNightResult();
if (nightRes.size()) {
text = text + " (" + nightRes + ")";
}
painter.setPen(QPen(QColor(SUMMARY_COLOR_LIMBS), 2));
painter.drawText(xpos, ypos, text);
painter.setPen(QPen(QColor(SUMMARY_COLOR_AVERAGE), 2));
QString txtAvg = night_.getAverage();
text = txtAvg;
painter.drawText(imageWidth - fm.horizontalAdvance(text) - 5, ypos, text);
painter.setPen(QPen(QColor(SUMMARY_COLOR_TITLE), 2));
text = "Average";
painter.drawText(imageWidth - fm.horizontalAdvance(txtAvg) - spaceWidth - fm.horizontalAdvance(text) - 5, ypos, text);
topOffset += summaryHeight;
font.setBold(false);
painter.setFont(font);
painter.setPen(QPen(QColor(SUMMARY_COLOR_LIMBS), 3));
painter.drawLine(0, topOffset, imageWidth, topOffset);
int validRunCounter = 0;
for(int i = 0; i < night_.runs().size(); i++) {
auto &set = night_.run(i);
if (set.getNumberOfCaps() || set.getNumberOfKills()) {
auto logEvts = set.eventLog();
if (validRunCounter) {
painter.setPen(QPen(QColor(SUMMARY_COLOR_LIMBS), 3));
painter.drawLine(validRunCounter * runWidth, summaryHeight, validRunCounter * runWidth, imageHeight - footerHeight);
}
text = "Run #" + QString::number(validRunCounter + 1);
fm = QFontMetrics(font);
fw = fm.horizontalAdvance(text);
painter.drawText(validRunCounter * runWidth + (runWidth / 2.0) - (fw / 2.0) + evtLeftOffset, summaryHeight + runNumHeight/2.0 + 15, text);
painter.setPen(QPen(QColor(TIMELINE_COLOR_ELEMENT), 6));
int tlLineX = validRunCounter * runWidth + (runWidth / 2.0) + evtLeftOffset;
int tlLinyYStart = summaryHeight + runNumHeight + (0.7 * evtHeight / 2.0);
int tlLinyYEnd = summaryHeight + (logEvts.size() - 1) * evtHeight + runNumHeight;
painter.drawLine(tlLineX, tlLinyYStart, tlLineX, tlLinyYEnd);
painter.setPen(QPen(QColor(SUMMARY_COLOR_LIMBS), 3));
if (logEvts.size()) {
for(int j = 0; j < logEvts.size(); j++) {
QImage tlImg(logEvtText[logEvts[j].type()][1]);
tlImg = tlImg.scaled(0.7 * evtHeight, 0.7 * evtHeight);
int x = validRunCounter * runWidth + (runWidth / 2.0) - (tlImg.width() / 2.0) + evtLeftOffset;
int y = summaryHeight + j * evtHeight + runNumHeight;
painter.drawImage(x, y, tlImg);
auto timestamp = logEvts[j].timestamp();
float progressTime = timestamp - set.startTimestamp();
QString progressStr = HuntInfo::timestampToProgressString(progressTime);
auto evtName = logEvtText[logEvts[j].type()][0];
text = progressStr;
fm = QFontMetrics(font);
fw = fm.horizontalAdvance(text);
painter.drawText(x - fw - 15, y + (tlImg.height() / 2.0) + 15, text);
text = evtName;
painter.drawText(x + tlImg.width() + 15, y + (tlImg.height() / 2.0) + 15, text);
}
}
validRunCounter++;
}
}
topOffset += evtHeight * maxLogSize + runNumHeight;
painter.setPen(QPen(QColor(SUMMARY_COLOR_LIMBS), 2));
font.setPointSize(20);
painter.setFont(font);
QString ver = QCoreApplication::applicationVersion();
if (ver.count(".") == 3) {
auto verDiv = ver.split(".");
verDiv.pop_back();
ver = verDiv.join(".");
}
text = "Generated by YATE " + ver + " (" + QDate::currentDate().toString("MMM dd, yyyy") + ")";
fm = QFontMetrics(font);
fw = fm.horizontalAdvance(text);
fh = fm.height();
topOffset += footerHeight - fh/2;
painter.drawLine(0, imageHeight - footerHeight, imageWidth, imageHeight - footerHeight);
painter.drawText(imageWidth/2 - fw/2, topOffset, text);
qDebug() << "Timeline image generated.";
return pix;
}
void TimelineImageGenerator::exportImage()
{
auto img = generateImage();
qDebug() << "Saving image to " << path_;
img->save(path_);
delete img;
emit exportFinished(true);
}
void TimelineImageGenerator::generateAndEmit()
{
auto img = generateImage();
qDebug() << "Emitting generated image.";
emit generateFinished(*img);
delete img;
}
}