Combine all the column filter related widgets
[qBittorrent.git] / src / gui / uithemedialog.cpp
blob54f8f86d0d49d3ffefe1f55a53df4d3c93416799
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "uithemedialog.h"
31 #include <QColor>
32 #include <QColorDialog>
33 #include <QFile>
34 #include <QFileDialog>
35 #include <QJsonDocument>
36 #include <QJsonObject>
37 #include <QLabel>
38 #include <QMenu>
39 #include <QMessageBox>
41 #include "base/3rdparty/expected.hpp"
42 #include "base/global.h"
43 #include "base/logger.h"
44 #include "base/path.h"
45 #include "base/profile.h"
46 #include "base/utils/fs.h"
47 #include "base/utils/io.h"
48 #include "uithemecommon.h"
49 #include "utils.h"
51 #include "ui_uithemedialog.h"
53 #define SETTINGS_KEY(name) u"GUI/UIThemeDialog/" name
55 namespace
57 Path userConfigPath()
59 return specialFolderLocation(SpecialFolder::Config) / Path(u"themes/default"_qs);
62 Path defaultIconPath(const QString &iconID, [[maybe_unused]] const ColorMode colorMode)
64 return Path(u":icons"_qs) / Path(iconID + u".svg");
68 class ColorWidget final : public QFrame
70 Q_DISABLE_COPY_MOVE(ColorWidget)
72 public:
73 explicit ColorWidget(const QColor &currentColor, const QColor &defaultColor, QWidget *parent = nullptr)
74 : QFrame(parent)
75 , m_defaultColor {defaultColor}
77 setObjectName(u"colorWidget"_qs);
78 setFrameShape(QFrame::Box);
79 setFrameShadow(QFrame::Plain);
81 setCurrentColor(currentColor);
84 QColor currentColor() const
86 return m_currentColor;
89 private:
90 void mouseDoubleClickEvent([[maybe_unused]] QMouseEvent *event) override
92 showColorDialog();
95 void contextMenuEvent([[maybe_unused]] QContextMenuEvent *event) override
97 QMenu *menu = new QMenu(this);
98 menu->setAttribute(Qt::WA_DeleteOnClose);
100 menu->addAction(tr("Edit..."), this, &ColorWidget::showColorDialog);
101 menu->addAction(tr("Reset"), this, &ColorWidget::resetColor);
103 menu->popup(QCursor::pos());
106 void setCurrentColor(const QColor &color)
108 if (m_currentColor == color)
109 return;
111 m_currentColor = color;
112 applyColor(m_currentColor);
115 void resetColor()
117 setCurrentColor(m_defaultColor);
120 void applyColor(const QColor &color)
122 setStyleSheet(u"#colorWidget { background-color: %1; }"_qs.arg(color.name()));
125 void showColorDialog()
127 auto dialog = new QColorDialog(m_currentColor, this);
128 dialog->setAttribute(Qt::WA_DeleteOnClose);
129 connect(dialog, &QDialog::accepted, this, [this, dialog]
131 setCurrentColor(dialog->currentColor());
134 dialog->open();
137 const QColor m_defaultColor;
138 QColor m_currentColor;
141 class IconWidget final : public QLabel
143 Q_DISABLE_COPY_MOVE(IconWidget)
145 public:
146 explicit IconWidget(const Path &currentPath, const Path &defaultPath, QWidget *parent = nullptr)
147 : QLabel(parent)
148 , m_defaultPath {defaultPath}
150 setObjectName(u"iconWidget"_qs);
151 setAlignment(Qt::AlignCenter);
153 setCurrentPath(currentPath);
156 Path currentPath() const
158 return m_currentPath;
161 private:
162 void mouseDoubleClickEvent([[maybe_unused]] QMouseEvent *event) override
164 showFileDialog();
167 void contextMenuEvent([[maybe_unused]] QContextMenuEvent *event) override
169 QMenu *menu = new QMenu(this);
170 menu->setAttribute(Qt::WA_DeleteOnClose);
172 menu->addAction(tr("Browse..."), this, &IconWidget::showFileDialog);
173 menu->addAction(tr("Reset"), this, &IconWidget::resetIcon);
175 menu->popup(QCursor::pos());
178 void setCurrentPath(const Path &path)
180 if (m_currentPath == path)
181 return;
183 m_currentPath = path;
184 showIcon(m_currentPath);
187 void resetIcon()
189 setCurrentPath(m_defaultPath);
192 void showIcon(const Path &iconPath)
194 const QIcon icon {iconPath.data()};
195 setPixmap(icon.pixmap(Utils::Gui::smallIconSize()));
198 void showFileDialog()
200 auto *dialog = new QFileDialog(this, tr("Select icon")
201 , QDir::homePath(), (tr("Supported image files") + u" (*.svg *.png)"));
202 dialog->setFileMode(QFileDialog::ExistingFile);
203 dialog->setAttribute(Qt::WA_DeleteOnClose);
204 connect(dialog, &QDialog::accepted, this, [this, dialog]
206 const Path iconPath {dialog->selectedFiles().value(0)};
207 setCurrentPath(iconPath);
210 dialog->open();
213 const Path m_defaultPath;
214 Path m_currentPath;
217 UIThemeDialog::UIThemeDialog(QWidget *parent)
218 : QDialog(parent)
219 , m_ui {new Ui::UIThemeDialog}
220 , m_storeDialogSize {SETTINGS_KEY(u"Size"_qs)}
222 m_ui->setupUi(this);
224 loadColors();
225 loadIcons();
227 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
228 resize(dialogSize);
231 UIThemeDialog::~UIThemeDialog()
233 m_storeDialogSize = size();
234 delete m_ui;
237 void UIThemeDialog::accept()
239 QDialog::accept();
241 bool hasError = false;
242 if (!storeColors())
243 hasError = true;
244 if (!storeIcons())
245 hasError = true;
247 if (hasError)
249 QMessageBox::critical(this, tr("UI Theme Configuration.")
250 , tr("The UI Theme changes could not be fully applied. The details can be found in the Log."));
254 void UIThemeDialog::loadColors()
256 const QHash<QString, UIThemeColor> defaultColors = defaultUIThemeColors();
257 const QList<QString> colorIDs = std::invoke([](auto &&list) { list.sort(); return list; }, defaultColors.keys());
258 int row = 2;
259 for (const QString &id : colorIDs)
261 m_ui->colorsLayout->addWidget(new QLabel(id), row, 0);
263 const UIThemeColor &defaultColor = defaultColors.value(id);
265 auto *lightColorWidget = new ColorWidget(m_defaultThemeSource.getColor(id, ColorMode::Light), defaultColor.light, this);
266 m_lightColorWidgets.insert(id, lightColorWidget);
267 m_ui->colorsLayout->addWidget(lightColorWidget, row, 2);
269 auto *darkColorWidget = new ColorWidget(m_defaultThemeSource.getColor(id, ColorMode::Dark), defaultColor.dark, this);
270 m_darkColorWidgets.insert(id, darkColorWidget);
271 m_ui->colorsLayout->addWidget(darkColorWidget, row, 4);
273 ++row;
277 void UIThemeDialog::loadIcons()
279 const QSet<QString> defaultIcons = defaultUIThemeIcons();
280 const QList<QString> iconIDs = std::invoke([](auto &&list) { list.sort(); return list; }
281 , QList<QString>(defaultIcons.cbegin(), defaultIcons.cend()));
282 int row = 2;
283 for (const QString &id : iconIDs)
285 m_ui->iconsLayout->addWidget(new QLabel(id), row, 0);
287 auto *lightIconWidget = new IconWidget(m_defaultThemeSource.getIconPath(id, ColorMode::Light)
288 , defaultIconPath(id, ColorMode::Light), this);
289 m_lightIconWidgets.insert(id, lightIconWidget);
290 m_ui->iconsLayout->addWidget(lightIconWidget, row, 2);
292 auto *darkIconWidget = new IconWidget(m_defaultThemeSource.getIconPath(id, ColorMode::Dark)
293 , defaultIconPath(id, ColorMode::Dark), this);
294 m_darkIconWidgets.insert(id, darkIconWidget);
295 m_ui->iconsLayout->addWidget(darkIconWidget, row, 4);
297 ++row;
301 bool UIThemeDialog::storeColors()
303 QJsonObject userConfig;
304 userConfig.insert(u"version", 2);
306 const QHash<QString, UIThemeColor> defaultColors = defaultUIThemeColors();
307 const auto addColorOverrides = [this, &defaultColors, &userConfig](const ColorMode colorMode)
309 const QHash<QString, ColorWidget *> &colorWidgets = (colorMode == ColorMode::Light)
310 ? m_lightColorWidgets : m_darkColorWidgets;
312 QJsonObject colors;
313 for (auto it = colorWidgets.cbegin(); it != colorWidgets.cend(); ++it)
315 const QString &colorID = it.key();
316 const QColor &defaultColor = (colorMode == ColorMode::Light)
317 ? defaultColors.value(colorID).light : defaultColors.value(colorID).dark;
318 const QColor &color = it.value()->currentColor();
319 if (color != defaultColor)
320 colors.insert(it.key(), color.name());
323 if (!colors.isEmpty())
324 userConfig.insert(((colorMode == ColorMode::Light) ? KEY_COLORS_LIGHT : KEY_COLORS_DARK), colors);
327 addColorOverrides(ColorMode::Light);
328 addColorOverrides(ColorMode::Dark);
330 const QByteArray configData = QJsonDocument(userConfig).toJson();
331 const nonstd::expected<void, QString> result = Utils::IO::saveToFile((userConfigPath() / Path(CONFIG_FILE_NAME)), configData);
332 if (!result)
334 const QString error = tr("Couldn't save UI Theme configuration. Reason: %1").arg(result.error());
335 LogMsg(error, Log::WARNING);
336 return false;
339 return true;
342 bool UIThemeDialog::storeIcons()
344 bool hasError = false;
346 const auto updateIcons = [this, &hasError](const ColorMode colorMode)
348 const QHash<QString, IconWidget *> &iconWidgets = (colorMode == ColorMode::Light)
349 ? m_lightIconWidgets : m_darkIconWidgets;
350 const Path subdirPath = (colorMode == ColorMode::Light)
351 ? Path(u"icons/light"_qs) : Path(u"icons/dark"_qs);
353 for (auto it = iconWidgets.cbegin(); it != iconWidgets.cend(); ++it)
355 const QString &id = it.key();
356 const Path &path = it.value()->currentPath();
357 if (path == m_defaultThemeSource.getIconPath(id, colorMode))
358 continue;
360 const Path &userIconPathBase = userConfigPath() / subdirPath / Path(id);
362 if (const Path oldIconPath = userIconPathBase + u".svg"
363 ; path.exists() && !Utils::Fs::removeFile(oldIconPath))
365 const QString error = tr("Couldn't remove icon file. File: %1.").arg(oldIconPath.toString());
366 LogMsg(error, Log::WARNING);
367 hasError = true;
368 continue;
371 if (const Path oldIconPath = userIconPathBase + u".png"
372 ; path.exists() && !Utils::Fs::removeFile(oldIconPath))
374 const QString error = tr("Couldn't remove icon file. File: %1.").arg(oldIconPath.toString());
375 LogMsg(error, Log::WARNING);
376 hasError = true;
377 continue;
380 if (const Path targetPath = userIconPathBase + path.extension()
381 ; !Utils::Fs::copyFile(path, targetPath))
383 const QString error = tr("Couldn't copy icon file. Source: %1. Destination: %2.")
384 .arg(path.toString(), targetPath.toString());
385 LogMsg(error, Log::WARNING);
386 hasError = true;
391 updateIcons(ColorMode::Light);
392 updateIcons(ColorMode::Dark);
394 return !hasError;