/
theme_manager.cpp
524 lines (438 loc) · 16.8 KB
/
theme_manager.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
SPDX-FileCopyrightText: 2009-2020 Graeme Gott <graeme@gottcode.org>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "theme_manager.h"
#include "gzip.h"
#include "session.h"
#include "theme.h"
#include "theme_dialog.h"
#include "utils.h"
#include <QApplication>
#include <QDialogButtonBox>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QImageReader>
#include <QInputDialog>
#include <QListWidget>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QStandardPaths>
#include <QStyle>
#include <QTabWidget>
#include <QTemporaryFile>
#include <QUrl>
#include <QVBoxLayout>
//-----------------------------------------------------------------------------
namespace
{
class ThemeItem : public QListWidgetItem
{
public:
ThemeItem(const QIcon& icon, const QString& text, QListWidget* view)
: QListWidgetItem(icon, text, view)
{
}
bool operator<(const QListWidgetItem& other) const
{
return localeAwareSort(text(), other.text());
}
};
}
//-----------------------------------------------------------------------------
ThemeManager::ThemeManager(QSettings& settings, QWidget* parent)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
, m_settings(settings)
{
setWindowTitle(tr("Themes"));
m_tabs = new QTabWidget(this);
// Find view sizes
const int focush = style()->pixelMetric(QStyle::PM_FocusFrameHMargin);
const int focusv = style()->pixelMetric(QStyle::PM_FocusFrameVMargin);
const int frame = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
const int scrollbar = style()->pixelMetric(QStyle::PM_SliderThickness);
const QSize grid_size(259 + focush, 154 + focusv + (fontMetrics().height() * 2));
const QSize view_size((grid_size.width() + frame + focush) * 2 + scrollbar, (grid_size.height() + frame + focusv) * 2);
// Add default themes tab
QWidget* tab = new QWidget(this);
m_tabs->addTab(tab, tr("Default"));
// Add default themes list
m_default_themes = new QListWidget(tab);
m_default_themes->setSortingEnabled(true);
m_default_themes->setViewMode(QListView::IconMode);
m_default_themes->setIconSize(QSize(258, 153));
m_default_themes->setGridSize(grid_size);
m_default_themes->setMovement(QListView::Static);
m_default_themes->setResizeMode(QListView::Adjust);
m_default_themes->setSelectionMode(QAbstractItemView::SingleSelection);
m_default_themes->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_default_themes->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_default_themes->setMinimumSize(view_size);
m_default_themes->setWordWrap(true);
addItem("bitterskies", true, tr("Bitter Skies"));
addItem("enchantment", true, tr("Enchantment"));
addItem("gentleblues", true, tr("Gentle Blues"));
addItem("oldschool", true, tr("Old School"));
addItem("spacedreams", true, tr("Space Dreams"));
addItem("spygames", true, tr("Spy Games"));
addItem("tranquility", true, tr("Tranquility"));
addItem("writingdesk", true, tr("Writing Desk"));
// Add default control buttons
QPushButton* new_default_button = new QPushButton(tr("New"), tab);
new_default_button->setAutoDefault(false);
connect(new_default_button, &QPushButton::clicked, this, &ThemeManager::newTheme);
m_clone_default_button = new QPushButton(tr("Duplicate"), tab);
m_clone_default_button->setAutoDefault(false);
connect(m_clone_default_button, &QPushButton::clicked, this, &ThemeManager::cloneTheme);
// Lay out default themes tab
QGridLayout* default_layout = new QGridLayout(tab);
default_layout->setColumnStretch(0, 1);
default_layout->setRowStretch(2, 1);
default_layout->addWidget(m_default_themes, 0, 0, 3, 1);
default_layout->addWidget(new_default_button, 0, 1);
default_layout->addWidget(m_clone_default_button, 1, 1);
// Add themes tab
tab = new QWidget(this);
m_tabs->addTab(tab, tr("Custom"));
// Add themes list
m_themes = new QListWidget(tab);
m_themes->setSortingEnabled(true);
m_themes->setViewMode(QListView::IconMode);
m_themes->setIconSize(QSize(258, 153));
m_themes->setGridSize(grid_size);
m_themes->setMovement(QListView::Static);
m_themes->setResizeMode(QListView::Adjust);
m_themes->setSelectionMode(QAbstractItemView::SingleSelection);
m_themes->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_themes->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_themes->setMinimumSize(view_size);
m_themes->setWordWrap(true);
QDir dir(Theme::path(), "*.theme");
const QStringList themes = dir.entryList(QDir::Files, QDir::Name | QDir::IgnoreCase);
for (const QString& theme : themes) {
QString name = QSettings(dir.filePath(theme), QSettings::IniFormat).value("Name").toString();
if (!name.isEmpty()) {
addItem(QFileInfo(theme).completeBaseName(), false, name);
} else {
name = QUrl::fromPercentEncoding(QFileInfo(theme).completeBaseName().toUtf8());
QSettings(dir.filePath(theme), QSettings::IniFormat).setValue("Name", name);
const QString id = Theme::createId();
dir.rename(theme, id + ".theme");
dir.remove(QFileInfo(theme).completeBaseName() + ".png");
QStringList sessions = QDir(Session::path(), "*.session").entryList(QDir::Files);
sessions.prepend(QString());
for (const QString& file : std::as_const(sessions)) {
Session session(file);
if ((session.theme() == name) && (session.themeDefault() == false)) {
session.setTheme(id, false);
}
}
addItem(id, false, name);
}
}
// Add control buttons
QPushButton* new_button = new QPushButton(tr("New"), tab);
new_button->setAutoDefault(false);
connect(new_button, &QPushButton::clicked, this, &ThemeManager::newTheme);
m_clone_button = new QPushButton(tr("Duplicate"), tab);
m_clone_button->setAutoDefault(false);
m_clone_button->setEnabled(false);
connect(m_clone_button, &QPushButton::clicked, this, &ThemeManager::cloneTheme);
m_edit_button = new QPushButton(tr("Edit"), tab);
m_edit_button->setAutoDefault(false);
m_edit_button->setEnabled(false);
connect(m_edit_button, &QPushButton::clicked, this, &ThemeManager::editTheme);
m_remove_button = new QPushButton(tr("Delete"), tab);
m_remove_button->setAutoDefault(false);
m_remove_button->setEnabled(false);
connect(m_remove_button, &QPushButton::clicked, this, &ThemeManager::deleteTheme);
QPushButton* import_button = new QPushButton(tr("Import"), tab);
import_button->setAutoDefault(false);
connect(import_button, &QPushButton::clicked, this, &ThemeManager::importTheme);
m_export_button = new QPushButton(tr("Export"), tab);
m_export_button->setAutoDefault(false);
m_export_button->setEnabled(false);
connect(m_export_button, &QPushButton::clicked, this, &ThemeManager::exportTheme);
// Lay out custom themes tab
QGridLayout* custom_layout = new QGridLayout(tab);
custom_layout->setColumnStretch(0, 1);
custom_layout->setRowMinimumHeight(4, import_button->sizeHint().height());
custom_layout->setRowStretch(7, 1);
custom_layout->addWidget(m_themes, 0, 0, 8, 1);
custom_layout->addWidget(new_button, 0, 1);
custom_layout->addWidget(m_clone_button, 1, 1);
custom_layout->addWidget(m_edit_button, 2, 1);
custom_layout->addWidget(m_remove_button, 3, 1);
custom_layout->addWidget(import_button, 5, 1);
custom_layout->addWidget(m_export_button, 6, 1);
// Lay out dialog
QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this);
connect(buttons, &QDialogButtonBox::rejected, this, &ThemeManager::reject);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(m_tabs, 1);
layout->addSpacing(layout->contentsMargins().top());
layout->addWidget(buttons);
// Select theme
const QString theme = m_settings.value("ThemeManager/Theme").toString();
const bool is_default = m_settings.value("ThemeManager/ThemeDefault", false).toBool();
if (!selectItem(theme, is_default)) {
selectItem(Theme::defaultId(), true);
}
selectionChanged(is_default);
connect(m_default_themes, &QListWidget::currentItemChanged, this, &ThemeManager::currentThemeChanged);
connect(m_themes, &QListWidget::currentItemChanged, this, &ThemeManager::currentThemeChanged);
connect(m_themes, &QListWidget::itemActivated, this, &ThemeManager::editTheme);
// Restore size
resize(m_settings.value("ThemeManager/Size", sizeHint()).toSize());
}
//-----------------------------------------------------------------------------
void ThemeManager::hideEvent(QHideEvent* event)
{
m_settings.setValue("ThemeManager/Size", size());
QDialog::hideEvent(event);
}
//-----------------------------------------------------------------------------
void ThemeManager::newTheme()
{
Theme theme(QString(), false);
ThemeDialog dialog(theme, this);
dialog.setWindowTitle(ThemeDialog::tr("New Theme"));
if (dialog.exec() == QDialog::Rejected) {
return;
}
QListWidgetItem* item = addItem(theme.id(), false, theme.name());
m_themes->setCurrentItem(item);
m_tabs->setCurrentIndex(1);
}
//-----------------------------------------------------------------------------
void ThemeManager::editTheme()
{
QListWidgetItem* item = m_themes->currentItem();
if (!item) {
return;
}
Theme theme(item->data(Qt::UserRole).toString(), false);
ThemeDialog dialog(theme, this);
if (dialog.exec() == QDialog::Rejected) {
return;
}
item->setText(theme.name());
item->setIcon(QIcon(Theme::iconPath(theme.id(), false, devicePixelRatioF())));
Q_EMIT themeSelected(theme);
}
//-----------------------------------------------------------------------------
void ThemeManager::cloneTheme()
{
const bool is_default = m_tabs->currentIndex() == 0;
QListWidgetItem* item = (is_default ? m_default_themes : m_themes)->currentItem();
if (!item) {
return;
}
const QString id = Theme::clone(item->data(Qt::UserRole).toString(), is_default, item->text());
const QString name = QSettings(Theme::filePath(id, false), QSettings::IniFormat).value("Name").toString();
item = addItem(id, false, name);
m_themes->setCurrentItem(item);
m_tabs->setCurrentIndex(1);
}
//-----------------------------------------------------------------------------
void ThemeManager::deleteTheme()
{
QListWidgetItem* item = m_themes->currentItem();
if (!item || (QMessageBox::question(this,
tr("Question"),
tr("Delete theme '%1'?").arg(item->text()),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)) {
return;
}
const QString id = item->data(Qt::UserRole).toString();
QFile::remove(Theme::filePath(id));
Theme::removeIcon(id, false);
delete item;
item = nullptr;
// Handle deleting last custom theme
if (m_themes->count() == 0) {
selectItem(Theme::defaultId(), true);
}
}
//-----------------------------------------------------------------------------
void ThemeManager::importTheme()
{
// Find file to import
QSettings settings;
QString path = settings.value("ThemeManager/Location").toString();
if (path.isEmpty() || !QFile::exists(path)) {
path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
const QString filename = QFileDialog::getOpenFileName(this, tr("Import Theme"), path, tr("Themes (%1)").arg("*.fwtz *.theme"));
if (filename.isEmpty()) {
return;
}
settings.setValue("ThemeManager/Location", QFileInfo(filename).absolutePath());
const QString id = Theme::createId();
// Uncompress theme
const QString theme_filename = Theme::filePath(id);
const QByteArray theme = gunzip(filename);
{
QFile file(theme_filename);
if (file.open(QFile::WriteOnly)) {
file.write(theme);
file.close();
}
}
// Find theme name
QSettings theme_ini(theme_filename, QSettings::IniFormat);
QString name = theme_ini.value("Name", QFileInfo(filename).completeBaseName()).toString();
{
theme_ini.setValue("Name", id);
const QStringList values = splitStringAtLastNumber(name);
int count = values.at(1).toInt();
while (Theme::exists(name)) {
++count;
name = values.at(0) + QString::number(count);
}
theme_ini.setValue("Name", name);
}
// Extract and use background image
const QByteArray data = QByteArray::fromBase64(theme_ini.value("Data/Image").toByteArray());
const QString image_file = theme_ini.value("Background/ImageFile").toString();
theme_ini.remove("Background/ImageFile");
theme_ini.remove("Data/Image");
theme_ini.sync();
if (!data.isEmpty()) {
QTemporaryFile file(QDir::tempPath() + "/XXXXXX-" + image_file);
if (file.open()) {
file.write(data);
file.close();
}
Theme theme(id, false);
theme.setBackgroundImage(file.fileName());
theme.saveChanges();
}
theme_ini.sync();
theme_ini.remove("Background/Image");
QListWidgetItem* item = addItem(id, false, name);
m_themes->setCurrentItem(item);
}
//-----------------------------------------------------------------------------
void ThemeManager::exportTheme()
{
const QListWidgetItem* item = m_themes->currentItem();
if (!item) {
return;
}
// Find export file name
QSettings settings;
QString path = settings.value("ThemeManager/Location").toString();
if (path.isEmpty() || !QFile::exists(path)) {
path = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}
path = path + "/" + item->text() + ".fwtz";
QString filename = QFileDialog::getSaveFileName(this, tr("Export Theme"), path, tr("Themes (%1)").arg("*.fwtz"), 0, QFileDialog::DontResolveSymlinks);
if (filename.isEmpty()) {
return;
}
if (!filename.endsWith(".fwtz")) {
filename += ".fwtz";
}
settings.setValue("ThemeManager/Location", QFileInfo(filename).absolutePath());
// Copy theme
QFile::remove(filename);
QFile::copy(Theme::filePath(item->data(Qt::UserRole).toString()), filename);
// Store image in export file
{
QSettings theme_ini(filename, QSettings::IniFormat);
theme_ini.remove("Background/Image");
const QString image = theme_ini.value("Background/ImageFile").toString();
if (!image.isEmpty()) {
QFile file(Theme::path() + "/Images/" + image);
if (file.open(QFile::ReadOnly)) {
theme_ini.setValue("Data/Image", file.readAll().toBase64());
file.close();
}
}
}
// Compress theme
gzip(filename);
}
//-----------------------------------------------------------------------------
void ThemeManager::currentThemeChanged(const QListWidgetItem* current)
{
if (current) {
const bool is_default = current->listWidget() == m_default_themes;
if (is_default) {
m_themes->setCurrentIndex(m_themes->rootIndex());
} else {
m_default_themes->setCurrentIndex(m_default_themes->rootIndex());
}
selectionChanged(is_default);
const QString id = current->data(Qt::UserRole).toString();
m_settings.setValue("ThemeManager/Theme", id);
m_settings.setValue("ThemeManager/ThemeDefault", is_default);
Q_EMIT themeSelected(Theme(id, is_default));
}
}
//-----------------------------------------------------------------------------
QListWidgetItem* ThemeManager::addItem(const QString& id, bool is_default, const QString& name)
{
const qreal pixelratio = devicePixelRatioF();
const QString icon = Theme::iconPath(id, is_default, pixelratio);
if (!QFile::exists(icon) || QImageReader(icon).size() != (QSize(258, 153) * pixelratio)) {
Theme theme(id, is_default);
// Find load color in separate thread
QFuture<QColor> load_color;
if (!theme.isDefault() && (theme.loadColor() == theme.backgroundColor())) {
load_color = theme.calculateLoadColor();
}
// Generate preview
QRect foreground;
const QImage background = theme.render(QSize(1920, 1080), foreground, 0, pixelratio);
QImage icon;
theme.renderText(background, foreground, pixelratio, nullptr, &icon);
icon.save(Theme::iconPath(theme.id(), theme.isDefault(), pixelratio));
// Save load color
load_color.waitForFinished();
if (load_color.resultCount()) {
theme.setLoadColor(load_color.result());
theme.saveChanges();
}
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
QListWidgetItem* item = new ThemeItem(QIcon(icon), name, is_default ? m_default_themes : m_themes);
item->setToolTip(name);
item->setData(Qt::UserRole, id);
return item;
}
//-----------------------------------------------------------------------------
bool ThemeManager::selectItem(const QString& id, bool is_default)
{
QListWidget* view = m_themes;
QListWidget* other_view = m_default_themes;
if (is_default) {
std::swap(view, other_view);
}
const QAbstractItemModel* model = view->model();
const QModelIndexList items = model->match(model->index(0, 0, QModelIndex()),
Qt::UserRole, id, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive);
if (!items.isEmpty()) {
view->setCurrentRow(items.constFirst().row());
other_view->setCurrentIndex(other_view->rootIndex());
m_tabs->setCurrentIndex(!is_default);
return true;
} else {
return false;
}
}
//-----------------------------------------------------------------------------
void ThemeManager::selectionChanged(bool is_default)
{
m_clone_default_button->setEnabled(is_default);
m_clone_button->setDisabled(is_default);
m_edit_button->setDisabled(is_default);
m_remove_button->setDisabled(is_default);
m_export_button->setDisabled(is_default);
}
//-----------------------------------------------------------------------------