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"
32 #include <QColorDialog>
33 #include <QFileDialog>
34 #include <QJsonDocument>
35 #include <QJsonObject>
38 #include <QMessageBox>
40 #include "base/3rdparty/expected.hpp"
41 #include "base/global.h"
42 #include "base/logger.h"
43 #include "base/path.h"
44 #include "base/profile.h"
45 #include "base/utils/fs.h"
46 #include "base/utils/io.h"
47 #include "uithemecommon.h"
50 #include "ui_uithemedialog.h"
52 #define SETTINGS_KEY(name) u"GUI/UIThemeDialog/" name
58 return specialFolderLocation(SpecialFolder::Config
) / Path(u
"themes/default"_s
);
61 Path
defaultIconPath(const QString
&iconID
, [[maybe_unused
]] const ColorMode colorMode
)
63 return Path(u
":icons"_s
) / Path(iconID
+ u
".svg");
67 class ColorWidget final
: public QFrame
69 Q_DISABLE_COPY_MOVE(ColorWidget
)
72 explicit ColorWidget(const QColor
¤tColor
, const QColor
&defaultColor
, QWidget
*parent
= nullptr)
74 , m_defaultColor
{defaultColor
}
76 setObjectName(u
"colorWidget"_s
);
77 setFrameShape(QFrame::Box
);
78 setFrameShadow(QFrame::Plain
);
80 setCurrentColor(currentColor
);
83 QColor
currentColor() const
85 return m_currentColor
;
89 void mouseDoubleClickEvent([[maybe_unused
]] QMouseEvent
*event
) override
94 void contextMenuEvent([[maybe_unused
]] QContextMenuEvent
*event
) override
96 QMenu
*menu
= new QMenu(this);
97 menu
->setAttribute(Qt::WA_DeleteOnClose
);
99 menu
->addAction(tr("Edit..."), this, &ColorWidget::showColorDialog
);
100 menu
->addAction(tr("Reset"), this, &ColorWidget::resetColor
);
102 menu
->popup(QCursor::pos());
105 void setCurrentColor(const QColor
&color
)
107 if (m_currentColor
== color
)
110 m_currentColor
= color
;
111 applyColor(m_currentColor
);
116 setCurrentColor(m_defaultColor
);
119 void applyColor(const QColor
&color
)
121 setStyleSheet(u
"#colorWidget { background-color: %1; }"_s
.arg(color
.name()));
124 void showColorDialog()
126 auto *dialog
= new QColorDialog(m_currentColor
, this);
127 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
128 connect(dialog
, &QDialog::accepted
, this, [this, dialog
]
130 setCurrentColor(dialog
->currentColor());
136 const QColor m_defaultColor
;
137 QColor m_currentColor
;
140 class IconWidget final
: public QLabel
142 Q_DISABLE_COPY_MOVE(IconWidget
)
145 explicit IconWidget(const Path
¤tPath
, const Path
&defaultPath
, QWidget
*parent
= nullptr)
147 , m_defaultPath
{defaultPath
}
149 setObjectName(u
"iconWidget"_s
);
150 setAlignment(Qt::AlignCenter
);
152 setCurrentPath(currentPath
);
155 Path
currentPath() const
157 return m_currentPath
;
161 void mouseDoubleClickEvent([[maybe_unused
]] QMouseEvent
*event
) override
166 void contextMenuEvent([[maybe_unused
]] QContextMenuEvent
*event
) override
168 QMenu
*menu
= new QMenu(this);
169 menu
->setAttribute(Qt::WA_DeleteOnClose
);
171 menu
->addAction(tr("Browse..."), this, &IconWidget::showFileDialog
);
172 menu
->addAction(tr("Reset"), this, &IconWidget::resetIcon
);
174 menu
->popup(QCursor::pos());
177 void setCurrentPath(const Path
&path
)
179 if (m_currentPath
== path
)
182 m_currentPath
= path
;
183 showIcon(m_currentPath
);
188 setCurrentPath(m_defaultPath
);
191 void showIcon(const Path
&iconPath
)
193 const QIcon icon
{iconPath
.data()};
194 setPixmap(icon
.pixmap(Utils::Gui::smallIconSize()));
197 void showFileDialog()
199 auto *dialog
= new QFileDialog(this, tr("Select icon")
200 , QDir::homePath(), (tr("Supported image files") + u
" (*.svg *.png)"));
201 dialog
->setFileMode(QFileDialog::ExistingFile
);
202 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
203 connect(dialog
, &QDialog::accepted
, this, [this, dialog
]
205 const Path iconPath
{dialog
->selectedFiles().value(0)};
206 setCurrentPath(iconPath
);
212 const Path m_defaultPath
;
216 UIThemeDialog::UIThemeDialog(QWidget
*parent
)
218 , m_ui
{new Ui::UIThemeDialog
}
219 , m_storeDialogSize
{SETTINGS_KEY(u
"Size"_s
)}
223 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &QDialog::accept
);
224 connect(m_ui
->buttonBox
, &QDialogButtonBox::rejected
, this, &QDialog::reject
);
229 if (const QSize dialogSize
= m_storeDialogSize
; dialogSize
.isValid())
233 UIThemeDialog::~UIThemeDialog()
235 m_storeDialogSize
= size();
239 void UIThemeDialog::accept()
243 bool hasError
= false;
251 QMessageBox::critical(this, tr("UI Theme Configuration.")
252 , tr("The UI Theme changes could not be fully applied. The details can be found in the Log."));
256 void UIThemeDialog::loadColors()
258 const QHash
<QString
, UIThemeColor
> defaultColors
= defaultUIThemeColors();
259 const QList
<QString
> colorIDs
= std::invoke([](auto &&list
) { list
.sort(); return list
; }, defaultColors
.keys());
261 for (const QString
&id
: colorIDs
)
263 m_ui
->colorsLayout
->addWidget(new QLabel(id
), row
, 0);
265 const UIThemeColor
&defaultColor
= defaultColors
.value(id
);
267 auto *lightColorWidget
= new ColorWidget(m_defaultThemeSource
.getColor(id
, ColorMode::Light
), defaultColor
.light
, this);
268 m_lightColorWidgets
.insert(id
, lightColorWidget
);
269 m_ui
->colorsLayout
->addWidget(lightColorWidget
, row
, 2);
271 auto *darkColorWidget
= new ColorWidget(m_defaultThemeSource
.getColor(id
, ColorMode::Dark
), defaultColor
.dark
, this);
272 m_darkColorWidgets
.insert(id
, darkColorWidget
);
273 m_ui
->colorsLayout
->addWidget(darkColorWidget
, row
, 4);
279 void UIThemeDialog::loadIcons()
281 const QSet
<QString
> defaultIcons
= defaultUIThemeIcons();
282 const QList
<QString
> iconIDs
= std::invoke([](auto &&list
) { list
.sort(); return list
; }
283 , QList
<QString
>(defaultIcons
.cbegin(), defaultIcons
.cend()));
285 for (const QString
&id
: iconIDs
)
287 m_ui
->iconsLayout
->addWidget(new QLabel(id
), row
, 0);
289 auto *lightIconWidget
= new IconWidget(m_defaultThemeSource
.getIconPath(id
, ColorMode::Light
)
290 , defaultIconPath(id
, ColorMode::Light
), this);
291 m_lightIconWidgets
.insert(id
, lightIconWidget
);
292 m_ui
->iconsLayout
->addWidget(lightIconWidget
, row
, 2);
294 auto *darkIconWidget
= new IconWidget(m_defaultThemeSource
.getIconPath(id
, ColorMode::Dark
)
295 , defaultIconPath(id
, ColorMode::Dark
), this);
296 m_darkIconWidgets
.insert(id
, darkIconWidget
);
297 m_ui
->iconsLayout
->addWidget(darkIconWidget
, row
, 4);
303 bool UIThemeDialog::storeColors()
305 QJsonObject userConfig
;
306 userConfig
.insert(u
"version", 2);
308 const QHash
<QString
, UIThemeColor
> defaultColors
= defaultUIThemeColors();
309 const auto addColorOverrides
= [this, &defaultColors
, &userConfig
](const ColorMode colorMode
)
311 const QHash
<QString
, ColorWidget
*> &colorWidgets
= (colorMode
== ColorMode::Light
)
312 ? m_lightColorWidgets
: m_darkColorWidgets
;
315 for (auto it
= colorWidgets
.cbegin(); it
!= colorWidgets
.cend(); ++it
)
317 const QString
&colorID
= it
.key();
318 const QColor
&defaultColor
= (colorMode
== ColorMode::Light
)
319 ? defaultColors
.value(colorID
).light
: defaultColors
.value(colorID
).dark
;
320 const QColor
&color
= it
.value()->currentColor();
321 if (color
!= defaultColor
)
322 colors
.insert(it
.key(), color
.name());
325 if (!colors
.isEmpty())
326 userConfig
.insert(((colorMode
== ColorMode::Light
) ? KEY_COLORS_LIGHT
: KEY_COLORS_DARK
), colors
);
329 addColorOverrides(ColorMode::Light
);
330 addColorOverrides(ColorMode::Dark
);
332 const QByteArray configData
= QJsonDocument(userConfig
).toJson();
333 const nonstd::expected
<void, QString
> result
= Utils::IO::saveToFile((userConfigPath() / Path(CONFIG_FILE_NAME
)), configData
);
336 const QString error
= tr("Couldn't save UI Theme configuration. Reason: %1").arg(result
.error());
337 LogMsg(error
, Log::WARNING
);
344 bool UIThemeDialog::storeIcons()
346 bool hasError
= false;
348 const auto updateIcons
= [this, &hasError
](const ColorMode colorMode
)
350 const QHash
<QString
, IconWidget
*> &iconWidgets
= (colorMode
== ColorMode::Light
)
351 ? m_lightIconWidgets
: m_darkIconWidgets
;
352 const Path subdirPath
= (colorMode
== ColorMode::Light
)
353 ? Path(u
"icons/light"_s
) : Path(u
"icons/dark"_s
);
355 for (auto it
= iconWidgets
.cbegin(); it
!= iconWidgets
.cend(); ++it
)
357 const QString
&id
= it
.key();
358 const Path
&path
= it
.value()->currentPath();
359 if (path
== m_defaultThemeSource
.getIconPath(id
, colorMode
))
362 const Path
&userIconPathBase
= userConfigPath() / subdirPath
/ Path(id
);
364 if (const Path oldIconPath
= userIconPathBase
+ u
".svg"
365 ; path
.exists() && !Utils::Fs::removeFile(oldIconPath
))
367 const QString error
= tr("Couldn't remove icon file. File: %1.").arg(oldIconPath
.toString());
368 LogMsg(error
, Log::WARNING
);
373 if (const Path oldIconPath
= userIconPathBase
+ u
".png"
374 ; path
.exists() && !Utils::Fs::removeFile(oldIconPath
))
376 const QString error
= tr("Couldn't remove icon file. File: %1.").arg(oldIconPath
.toString());
377 LogMsg(error
, Log::WARNING
);
382 if (const Path targetPath
= userIconPathBase
+ path
.extension()
383 ; !Utils::Fs::copyFile(path
, targetPath
))
385 const QString error
= tr("Couldn't copy icon file. Source: %1. Destination: %2.")
386 .arg(path
.toString(), targetPath
.toString());
387 LogMsg(error
, Log::WARNING
);
393 updateIcons(ColorMode::Light
);
394 updateIcons(ColorMode::Dark
);