-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcelexport.cpp
More file actions
373 lines (368 loc) · 20.1 KB
/
Copy pathexcelexport.cpp
File metadata and controls
373 lines (368 loc) · 20.1 KB
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
/////////////////////////////////////////////////////////////////////////////
// Name: excelexport.cpp
// Purpose: Export collection as Excel worksheet
// Author: Jan Buchholz
// Created: 2026-05-07
// Changed: 2026-05-21
/////////////////////////////////////////////////////////////////////////////
#include "excelexport.h"
#include "appsettings.h"
#include "conversions.hpp"
#include "lookupkeys.h"
#include "globals.h"
#include "models/headertexts.h"
#include "models/movietreemodel.h"
#include "models/seriestreemodel.h"
#include "models/homevideotreemodel.h"
#include "models/musicvideotreemodel.h"
#include "models/musictreemodel.h"
#include "xlsxdocument.h"
#include "xlsxworkbook.h"
#include "xlsxformat.h"
ExcelExport::ExcelExport() {
AppSettings::AppPreferences settings = AppSettings::getSettings();
// ---Export item limits---
int max_cast = (settings.limit_actors) ? settings.max_actors : DEF_NO_LIMIT;
int max_directors = (settings.limit_directors) ? settings.max_directors : DEF_NO_LIMIT;
int max_studios = (settings.limit_studios) ? settings.max_studios : DEF_NO_LIMIT;
int max_genres = (settings.limit_genres) ? settings.max_genres : DEF_NO_LIMIT;
// ---FieldMaps---
movieFields = {
{LookUpKeys::title, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::originaltitle, [](auto& d){ return toQString(d.originalTitle); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::runtime, [](auto& d){ return int64ToRuntimeMinutes(d.runtime); }},
{LookUpKeys::resolution, [](auto& d){ return resolutionValue(d.width, d.height); }},
{LookUpKeys::cast, [max_cast](auto& d){ return joinList(d.actors, max_cast); }},
{LookUpKeys::directors, [max_directors](auto& d){ return joinList(d.directors, max_directors); }},
{LookUpKeys::genres, [max_genres](auto& d){ return joinList(d.genres, max_genres); }},
{LookUpKeys::studios, [max_studios](auto& d){ return joinList(d.studios, max_studios); }},
{LookUpKeys::container, [](auto& d){ return toQString(d.container); }},
{LookUpKeys::videocodec, [](auto& d){ return toQString(d.videoCodec); }},
{LookUpKeys::audiocodec, [](auto& d){ return toQString(d.audioCodec); }},
{LookUpKeys::bitrate, [](auto& d){ return int32ToBitrate(d.bitrate); }},
{LookUpKeys::imdbid, [](auto& d){ return toQString(d.imDbId); }},
{LookUpKeys::filesize, [](auto& d){ return int64ToFileSize(d.fileSize); }},
{LookUpKeys::filename, [](auto& d){ return toQString(d.fileName); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
seriesFields = {
{LookUpKeys::series, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::cast, [max_cast](auto& d){ return joinList(d.actors, max_cast); }},
{LookUpKeys::directors, [max_directors](auto& d){ return joinList(d.directors, max_directors); }},
{LookUpKeys::genres, [max_genres](auto& d){ return joinList(d.genres, max_genres); }},
{LookUpKeys::studios, [max_studios](auto& d){ return joinList(d.studios, max_studios); }},
{LookUpKeys::imdbid, [](auto& d){ return toQString(d.imDbId); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
seasonFields = {
{LookUpKeys::season, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
episodeFields = {
{LookUpKeys::episode, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::runtime, [](auto& d){ return int64ToRuntimeMinutes(d.runtime); }},
{LookUpKeys::resolution, [](auto& d){ return resolutionValue(d.width, d.height); }},
{LookUpKeys::cast, [max_cast](auto& d){ return joinList(d.actors, max_cast); }},
{LookUpKeys::directors, [max_directors](auto& d){ return joinList(d.directors, max_directors); }},
{LookUpKeys::container, [](auto& d){ return toQString(d.container); }},
{LookUpKeys::videocodec, [](auto& d){ return toQString(d.videoCodec); }},
{LookUpKeys::audiocodec, [](auto& d){ return toQString(d.audioCodec); }},
{LookUpKeys::bitrate, [](auto& d){ return int32ToBitrate(d.bitrate); }},
{LookUpKeys::imdbid, [](auto& d){ return toQString(d.imDbId); }},
{LookUpKeys::filesize, [](auto& d){ return int64ToFileSize(d.fileSize); }},
{LookUpKeys::filename, [](auto& d){ return toQString(d.fileName); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
homeVideoFields = {
{LookUpKeys::title, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::runtime, [](auto& d){ return int64ToRuntimeMinutes(d.runtime); }},
{LookUpKeys::resolution, [](auto& d){ return resolutionValue(d.width, d.height); }},
{LookUpKeys::people, [max_cast](auto& d){ return joinList(d.people, max_cast); }},
{LookUpKeys::genres, [max_genres](auto& d){ return joinList(d.genres, max_genres); }},
{LookUpKeys::container, [](auto& d){ return toQString(d.container); }},
{LookUpKeys::videocodec, [](auto& d){ return toQString(d.videoCodec); }},
{LookUpKeys::audiocodec, [](auto& d){ return toQString(d.audioCodec); }},
{LookUpKeys::bitrate, [](auto& d){ return int32ToBitrate(d.bitrate); }},
{LookUpKeys::filesize, [](auto& d){ return int64ToFileSize(d.fileSize); }},
{LookUpKeys::filename, [](auto& d){ return toQString(d.fileName); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
musicVideoFields = {
{LookUpKeys::title, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::runtime, [](auto& d){ return int64ToRuntimeMinutes(d.runtime); }},
{LookUpKeys::resolution, [](auto& d){ return resolutionValue(d.width, d.height); }},
{LookUpKeys::artists, [max_cast](auto& d){ return joinList(d.artists, max_cast); }},
{LookUpKeys::genres, [max_genres](auto& d){ return joinList(d.genres, max_genres); }},
{LookUpKeys::container, [](auto& d){ return toQString(d.container); }},
{LookUpKeys::videocodec, [](auto& d){ return toQString(d.videoCodec); }},
{LookUpKeys::audiocodec, [](auto& d){ return toQString(d.audioCodec); }},
{LookUpKeys::bitrate, [](auto& d){ return int32ToBitrate(d.bitrate); }},
{LookUpKeys::filesize, [](auto& d){ return int64ToFileSize(d.fileSize); }},
{LookUpKeys::filename, [](auto& d){ return toQString(d.fileName); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
albumFields = {
{LookUpKeys::album, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::albumartist, [](auto& d){ return toQString(d.albumArtist); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::runtime, [](auto& d){ return int64ToRuntimeMinutes(d.runtime); }},
{LookUpKeys::genres, [max_genres](auto& d){ return joinList(d.genres, max_genres); }},
{LookUpKeys::musicbrainzid, [](auto& d){ return toQString(d.musicBrainzId); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
audioFields = {
{LookUpKeys::track, [](auto& d){ return toQString(d.name); }},
{LookUpKeys::year, [](auto& d){ return int32ToYear(d.productionYear); }},
{LookUpKeys::runtime, [](auto& d){ return int64ToRuntimeMinutes(d.runtime); }},
{LookUpKeys::albumartist, [](auto& d){ return toQString(d.albumArtist); }},
{LookUpKeys::genres, [max_genres](auto& d){ return joinList(d.genres, max_genres); }},
{LookUpKeys::container, [](auto& d){ return toQString(d.container); }},
{LookUpKeys::audiocodec, [](auto& d){ return toQString(d.audioCodec); }},
{LookUpKeys::bitrate, [](auto& d){ return int32ToBitrate(d.bitrate); }},
{LookUpKeys::filesize, [](auto& d){ return int64ToFileSize(d.fileSize); }},
{LookUpKeys::filename, [](auto& d){ return toQString(d.fileName); }},
{LookUpKeys::addedat, [](auto& d){ return int64ToDateISO(d.addedAt); }},
};
}
bool ExcelExport::doExport(QAbstractItemModel* model, EmbyCollection collection, QString fileName) {
int row = 1;
QVector<ExportHeaders> cols = createHeaders(collection.type);
QXlsx::Document xlsx;
xlsx.addSheet(collection.name);
QXlsx::Format fmt;
// ---Cell formats---
QXlsx::Format headerFmt;
headerFmt.setFontBold(true);
headerFmt.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
headerFmt.setPatternBackgroundColor(DEF_HEADER_COLOR);
QXlsx::Format defaultFmtL;
defaultFmtL.setFontBold(false);
defaultFmtL.setHorizontalAlignment(QXlsx::Format::AlignLeft);
defaultFmtL.setPatternBackgroundColor(DEF_DEFAULT_COLOR);
defaultFmtL.setTextWrap(true);
QXlsx::Format seriesFmtL;
seriesFmtL.setFontBold(false);
seriesFmtL.setHorizontalAlignment(QXlsx::Format::AlignLeft);
seriesFmtL.setPatternBackgroundColor(DEF_SERIES_COLOR);
seriesFmtL.setTextWrap(true);
QXlsx::Format seasonFmtL;
seasonFmtL.setFontBold(false);
seasonFmtL.setHorizontalAlignment(QXlsx::Format::AlignLeft);
seasonFmtL.setPatternBackgroundColor(DEF_SEASON_COLOR);
seasonFmtL.setTextWrap(true);
QXlsx::Format albumFmtL;
albumFmtL.setFontBold(false);
albumFmtL.setHorizontalAlignment(QXlsx::Format::AlignLeft);
albumFmtL.setPatternBackgroundColor(DEF_ALBUM_COLOR);
albumFmtL.setTextWrap(true);
QXlsx::Format defaultFmtR;
defaultFmtR.setFontBold(false);
defaultFmtR.setHorizontalAlignment(QXlsx::Format::AlignRight);
defaultFmtR.setPatternBackgroundColor(DEF_DEFAULT_COLOR);
defaultFmtR.setTextWrap(true);
QXlsx::Format seriesFmtR;
seriesFmtR.setFontBold(false);
seriesFmtR.setHorizontalAlignment(QXlsx::Format::AlignRight);
seriesFmtR.setPatternBackgroundColor(DEF_SERIES_COLOR);
seriesFmtR.setTextWrap(true);
QXlsx::Format seasonFmtR;
seasonFmtR.setFontBold(false);
seasonFmtR.setHorizontalAlignment(QXlsx::Format::AlignRight);
seasonFmtR.setPatternBackgroundColor(DEF_SEASON_COLOR);
seasonFmtR.setTextWrap(true);
QXlsx::Format albumFmtR;
albumFmtR.setFontBold(false);
albumFmtR.setHorizontalAlignment(QXlsx::Format::AlignRight);
albumFmtR.setPatternBackgroundColor(DEF_ALBUM_COLOR);
albumFmtR.setTextWrap(true);
// ---Write column header---
for (int i = 0; i < cols.size(); ++i) {
xlsx.setColumnWidth(i + 1, cols[i].headerWidth);
xlsx.write(row, i + 1, cols[i].headerText, headerFmt);
}
// ---Movie Model---
if (auto movieModel = qobject_cast<const MovieTreeModel*>(model)) {
const std::vector<MovieTreeModel::MovieNode*>* movieNodes = movieModel->rootNodes();
for (auto& movieNode : *movieNodes) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? defaultFmtL : defaultFmtR;
QString data = getFieldValueForExport(movieFields, movieNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
}
}
// --Series Model---
else if (auto seriesModel = qobject_cast<const SeriesTreeModel*>(model)) {
const std::vector<SeriesTreeModel::SeriesNode*>* seriesNodes = seriesModel->rootNodes();
for (auto& seriesNode : *seriesNodes) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? seriesFmtL : seriesFmtR;
QString data = getFieldValueForExport(seriesFields, seriesNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
for (auto& seasonNode : seriesNode->seasons) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? seasonFmtL : seasonFmtR;
QString data = getFieldValueForExport(seasonFields, seasonNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
for (auto& episodeNode : seasonNode->episodes) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? defaultFmtL : defaultFmtR;
QString data = getFieldValueForExport(episodeFields, episodeNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
}
}
}
}
// ---HomeVideos Model---
else if (auto homeVideoModel = qobject_cast<const HomeVideoTreeModel*>(model)) {
const std::vector<HomeVideoTreeModel::HomeVideoNode*>* videoNodes = homeVideoModel->rootNodes();
for (auto& videoNode : *videoNodes) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? defaultFmtL : defaultFmtR;
QString data = getFieldValueForExport(homeVideoFields, videoNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
}
}
// ---MusicVideos Model---
else if (auto musicVideoModel = qobject_cast<const MusicVideoTreeModel*>(model)) {
const std::vector<MusicVideoTreeModel::MusicVideoNode*>* videoNodes = musicVideoModel->rootNodes();
for (auto& videoNode : *videoNodes) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? defaultFmtL : defaultFmtR;
QString data = getFieldValueForExport(musicVideoFields, videoNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
}
}
// ---Music Model---
else if (auto musicModel = qobject_cast<const MusicTreeModel*>(model)) {
const std::vector<MusicTreeModel::AlbumNode*>* albumNodes = musicModel->rootNodes();
for (auto& albumNode : *albumNodes) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? albumFmtL : albumFmtR;
QString data = getFieldValueForExport(albumFields, albumNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
for (auto& trackNode : albumNode->tracks) {
++row;
for (int i = 0; i < cols.size(); ++i) {
fmt = (cols[i].align == l) ? defaultFmtL : defaultFmtR;
QString data = getFieldValueForExport(audioFields, trackNode->data, cols[i].lookupKey);
xlsx.write(row, i + 1, data, fmt);
}
}
}
}
else return false;
return xlsx.saveAs(fileName);
}
// ---Header definitions---
QVector<ExcelExport::ExportHeaders> ExcelExport::createHeaders(QString collectionType) {
if (collectionType == CollectionMovies) {
return {
{LookUpKeys::title, c_txt_title, 50, l},
{LookUpKeys::originaltitle, c_txt_originaltitle, 50, l},
{LookUpKeys::year, c_txt_year, 10, r},
{LookUpKeys::runtime, c_txt_runtime, 10, r},
{LookUpKeys::resolution, c_txt_resolution, 10, r},
{LookUpKeys::cast, c_txt_cast, 90, l},
{LookUpKeys::directors, c_txt_directors, 50, l},
{LookUpKeys::studios, c_txt_studios, 50, l},
{LookUpKeys::genres, c_txt_genres, 50, l},
{LookUpKeys::container, c_txt_container, 10, l},
{LookUpKeys::videocodec, c_txt_videocodec, 15, l},
{LookUpKeys::audiocodec, c_txt_audiocodec, 15, l},
{LookUpKeys::bitrate, c_txt_bitrate, 15, r},
{LookUpKeys::imdbid, c_txt_imdbid, 15, l},
{LookUpKeys::filesize, c_txt_filesize, 15, r},
{LookUpKeys::filename, c_txt_filename, 80, l},
{LookUpKeys::addedat, c_txt_addedat, 15, r},
};
} else if (collectionType == CollectionSeries) {
return {
{LookUpKeys::series, c_txt_series, 50, l},
{LookUpKeys::season, c_txt_season, 20, l},
{LookUpKeys::episode, c_txt_episode, 50, l},
{LookUpKeys::year, c_txt_year, 10, r},
{LookUpKeys::runtime, c_txt_runtime, 10, r},
{LookUpKeys::resolution, c_txt_resolution, 10, r},
{LookUpKeys::cast, c_txt_cast, 90, l},
{LookUpKeys::directors, c_txt_directors, 50, l},
{LookUpKeys::studios, c_txt_studios, 50, l},
{LookUpKeys::genres, c_txt_genres, 50, l},
{LookUpKeys::container, c_txt_container, 10, l},
{LookUpKeys::videocodec, c_txt_videocodec, 15, l},
{LookUpKeys::audiocodec, c_txt_audiocodec, 10, l},
{LookUpKeys::bitrate, c_txt_bitrate, 15, r},
{LookUpKeys::imdbid, c_txt_imdbid, 15, l},
{LookUpKeys::filesize, c_txt_filesize, 15, r},
{LookUpKeys::filename, c_txt_filename, 80, l},
{LookUpKeys::addedat, c_txt_addedat, 15, r},
};
} else if (collectionType == CollectionHomeVideos) {
return {
{LookUpKeys::title, c_txt_title, 50, l},
{LookUpKeys::year, c_txt_year, 10, r},
{LookUpKeys::runtime, c_txt_runtime, 10, r},
{LookUpKeys::resolution, c_txt_resolution, 10, r},
{LookUpKeys::people, c_txt_people, 50, l},
{LookUpKeys::genres, c_txt_genres, 50, l},
{LookUpKeys::container, c_txt_container, 10, l},
{LookUpKeys::videocodec, c_txt_videocodec, 15, l},
{LookUpKeys::audiocodec, c_txt_audiocodec, 15, l},
{LookUpKeys::bitrate, c_txt_bitrate, 15, r},
{LookUpKeys::filesize, c_txt_filesize, 15, r},
{LookUpKeys::filename, c_txt_filename, 80, l},
{LookUpKeys::addedat, c_txt_addedat, 15, r},
};
} else if (collectionType == CollectionMusicVideos) {
return {
{LookUpKeys::title, c_txt_title, 50, l},
{LookUpKeys::year, c_txt_year, 10, r},
{LookUpKeys::runtime, c_txt_runtime, 10, r},
{LookUpKeys::resolution, c_txt_resolution, 10, r},
{LookUpKeys::artists, c_txt_artists, 70, l},
{LookUpKeys::genres, c_txt_genres, 50, l},
{LookUpKeys::container, c_txt_container, 10, l},
{LookUpKeys::videocodec, c_txt_videocodec, 15, l},
{LookUpKeys::audiocodec, c_txt_audiocodec, 15, l},
{LookUpKeys::bitrate, c_txt_bitrate, 15, r},
{LookUpKeys::filesize, c_txt_filesize, 15, r},
{LookUpKeys::filename, c_txt_filename, 80, l},
{LookUpKeys::addedat, c_txt_addedat, 15, r},
};
} else if (collectionType == CollectionMusic) {
return {
{LookUpKeys::album, c_txt_album, 50, l},
{LookUpKeys::track, c_txt_track, 50, l},
{LookUpKeys::albumartist, c_txt_albumartist, 50, l},
{LookUpKeys::year, c_txt_year, 10, r},
{LookUpKeys::runtime, c_txt_runtime, 10, r},
{LookUpKeys::genres, c_txt_genres, 50, l},
{LookUpKeys::container, c_txt_container, 10, l},
{LookUpKeys::audiocodec, c_txt_audiocodec, 15, l},
{LookUpKeys::bitrate, c_txt_bitrate, 15, r},
{LookUpKeys::filesize, c_txt_filesize, 15, r},
{LookUpKeys::filename, c_txt_filename, 50, l},
{LookUpKeys::addedat, c_txt_addedat, 15, r},
};
};
return {};
}