qBittorrent
uithememanager.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2019 Prince Gupta <[email protected]>
4  *
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.
9  *
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.
14  *
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,
18  * USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL". If
25  * you modify file(s), you may extend this exception to your version of the
26  * file(s), but you are not obligated to do so. If you do not wish to do so,
27  * delete this exception statement from your version.
28  */
29 
30 #pragma once
31 
32 #include <QtGlobal>
33 #include <QColor>
34 #include <QHash>
35 #include <QIcon>
36 #include <QObject>
37 #include <QString>
38 
40 {
41 public:
42  virtual ~UIThemeSource() = default;
43 
44  virtual QByteArray readStyleSheet() = 0;
45  virtual QByteArray readConfig() = 0;
46  virtual QString iconPath(const QString &iconId) const = 0;
47 };
48 
49 class UIThemeManager : public QObject
50 {
51  Q_OBJECT
52  Q_DISABLE_COPY_MOVE(UIThemeManager)
53 
54 public:
55  static void initInstance();
56  static void freeInstance();
57  static UIThemeManager *instance();
58 
59  QString getIconPath(const QString &iconId) const;
60  QIcon getIcon(const QString &iconId, const QString &fallback = {}) const;
61  QIcon getFlagIcon(const QString &countryIsoCode) const;
62 
63  QColor getColor(const QString &id, const QColor &defaultColor) const;
64 
65 #ifndef Q_OS_MACOS
66  QIcon getSystrayIcon() const;
67 #endif
68 
69 private:
70  UIThemeManager(); // singleton class
71  QString getIconPathFromResources(const QString &iconId, const QString &fallback = {}) const;
73  void applyPalette() const;
74  void applyStyleSheet() const;
75 
77  const bool m_useCustomTheme;
78  std::unique_ptr<UIThemeSource> m_themeSource;
79  QHash<QString, QColor> m_colors;
80  mutable QHash<QString, QIcon> m_iconCache;
81  mutable QHash<QString, QIcon> m_flagCache;
82 #if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
83  const bool m_useSystemTheme;
84 #endif
85 };
void applyPalette() const
QHash< QString, QIcon > m_flagCache
QString getIconPath(const QString &iconId) const
std::unique_ptr< UIThemeSource > m_themeSource
QString getIconPathFromResources(const QString &iconId, const QString &fallback={}) const
QIcon getSystrayIcon() const
static UIThemeManager * m_instance
static UIThemeManager * instance()
static void freeInstance()
QColor getColor(const QString &id, const QColor &defaultColor) const
QHash< QString, QColor > m_colors
void applyStyleSheet() const
static void initInstance()
QIcon getFlagIcon(const QString &countryIsoCode) const
QIcon getIcon(const QString &iconId, const QString &fallback={}) const
const bool m_useCustomTheme
void loadColorsFromJSONConfig()
QHash< QString, QIcon > m_iconCache
virtual QByteArray readConfig()=0
virtual QByteArray readStyleSheet()=0
virtual QString iconPath(const QString &iconId) const =0
virtual ~UIThemeSource()=default