Debian Codegraph @

src/librssguard/miscellaneous/iconfactory.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "miscellaneous/iconfactory.h"
4
5 #include "miscellaneous/application.h"
6 #include "miscellaneous/settings.h"
7
8 #include <QBuffer>
9 #include <QPainter>
10
11 IconFactory::IconFactory(QObject* parent) : QObject(parent) {}
12
13 IconFactory::~IconFactory() {
14 qDebugNN << LOGSEC_GUI << "Destroying IconFactory instance.";
15 }
16
17 QIcon IconFactory::generateIcon(const QColor& color) {
18 QPixmap pxm(64, 64);
19
20 pxm.fill(Qt::GlobalColor::transparent);
21
22 QPainter paint(&pxm);
23
24 paint.setBrush(color);
25 paint.setPen(Qt::GlobalColor::transparent);
26 paint.drawEllipse(pxm.rect().marginsRemoved(QMargins(2, 2, 2, 2)));
27
28 return pxm;
29 }
30
31 QIcon IconFactory::fromByteArray(QByteArray array) {
32 if (array.isEmpty()) {
33 return {};
34 }
35
36 array = QByteArray::fromBase64(array);
37 QIcon icon;
38 QBuffer buffer(&array);
39
40 buffer.open(QIODevice::OpenModeFlag::ReadOnly);
41 QDataStream in(&buffer);
42
43 in.setVersion(QDataStream::Version::Qt_4_7);
44 in >> icon;
45 buffer.close();
46 return icon;
47 }
48
49 QByteArray IconFactory::toByteArray(const QIcon& icon) {
50 if (icon.isNull()) {
51 return {};
52 }
53
54 QByteArray array;
55 QBuffer buffer(&array);
56
57 buffer.open(QIODevice::OpenModeFlag::WriteOnly);
58 QDataStream out(&buffer);
59
60 out.setVersion(QDataStream::Version::Qt_4_7);
61 out << icon;
62 buffer.close();
63 return array.toBase64();
64 }
65
66 QIcon IconFactory::fromTheme(const QString& name, const QString& fallback) {
67 QIcon original = QIcon::fromTheme(name);
68
69 return (original.isNull() && !fallback.isEmpty()) ? QIcon::fromTheme(fallback) : original;
70 }
71
72 QPixmap IconFactory::miscPixmap(const QString& name) {
73 return QPixmap(QString(APP_THEME_PATH) + QDir::separator() + "misc" + QDir::separator() + name + ".png");
74 }
75
76 QIcon IconFactory::miscIcon(const QString& name) {
77 return QIcon(QString(APP_THEME_PATH) + QDir::separator() + "misc" + QDir::separator() + name + ".png");
78 }
79
80 void IconFactory::setupSearchPaths() {
81 auto paths = QIcon::themeSearchPaths();
82
83 paths << APP_THEME_PATH << qApp->userDataFolder() + QDir::separator() + APP_LOCAL_ICON_THEME_FOLDER
84 << qApp->applicationDirPath() + QDir::separator() + APP_LOCAL_ICON_THEME_FOLDER;
85
86 QIcon::setThemeSearchPaths(paths);
87 qDebugNN << LOGSEC_GUI << "Available icon theme paths: " << paths;
88 }
89
90 void IconFactory::setCurrentIconTheme(const QString& theme_name) {
91 qApp->settings()->setValue(GROUP(GUI), GUI::IconTheme, theme_name);
92 }
93
94 QString IconFactory::currentIconTheme() const {
95 return qApp->settings()->value(GROUP(GUI), SETTING(GUI::IconTheme)).toString();
96 }
97
98 void IconFactory::loadCurrentIconTheme() {
99 const QStringList installed_themes = installedIconThemes();
100 const QString theme_name_from_settings = qApp->settings()->value(GROUP(GUI), SETTING(GUI::IconTheme)).toString();
101
102 if (QIcon::themeName() == theme_name_from_settings) {
103 qDebugNN << LOGSEC_GUI << "Icon theme" << QUOTE_W_SPACE(theme_name_from_settings) << "already loaded.";
104 return;
105 }
106
107 // Display list of installed themes.
108 qDebugNN << LOGSEC_GUI << "Installed icon themes are: "
109 << QStringList(installed_themes)
110 .replaceInStrings(QRegularExpression(QSL("^|$")), QSL("\'"))
111 .replaceInStrings(QRegularExpression(QSL("^\\'$")), QSL("\'\'"))
112 .join(QSL(", "));
113
114 if (installed_themes.contains(theme_name_from_settings)) {
115 // Desired icon theme is installed and can be loaded.
116 #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
117 if (theme_name_from_settings.isEmpty()) {
118 qDebugNN << LOGSEC_GUI << "Loading default system icon theme.";
119 }
120 else {
121 qDebugNN << LOGSEC_GUI << "Loading icon theme" << QUOTE_W_SPACE_DOT(theme_name_from_settings);
122 QIcon::setThemeName(theme_name_from_settings);
123 }
124 #else
125 qDebugNN << LOGSEC_GUI << "Loading icon theme" << QUOTE_W_SPACE_DOT(theme_name_from_settings);
126 QIcon::setThemeName(theme_name_from_settings);
127 #endif
128 }
129 else {
130 // Desired icon theme is not currently available.
131 // Activate "default" or "no" icon theme instead.
132 #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
133 qWarningNN << "Icon theme" << QUOTE_W_SPACE(theme_name_from_settings)
134 << "cannot be loaded because it is not installed. Activating \"no\" icon theme.";
135 #else
136 qWarningNN << "Icon theme" << QUOTE_W_SPACE(theme_name_from_settings)
137 << "cannot be loaded because it is not installed. Activating \"no\" icon theme.";
138 QIcon::setThemeName(QSL(APP_NO_THEME));
139 #endif
140 }
141 }
142
143 QStringList IconFactory::installedIconThemes() const {
144 QStringList icon_theme_names = {QSL(APP_NO_THEME)};
145
146 // Iterate all directories with icon themes.
147 QStringList icon_themes_paths = QIcon::themeSearchPaths();
148 QStringList filters_index;
149
150 filters_index.append(QSL("index.theme"));
151 icon_themes_paths.removeDuplicates();
152
153 for (const QString& icon_path : icon_themes_paths) {
154 const QDir icon_dir(icon_path);
155 auto icon_paths =
156 icon_dir.entryInfoList(QDir::Filter::Dirs | QDir::Filter::NoDotAndDotDot | QDir::Filter::Readable |
157 QDir::Filter::CaseSensitive | QDir::Filter::NoSymLinks,
158 QDir::SortFlag::Time);
159
160 // Iterate all icon themes in this directory.
161 for (const QFileInfo& icon_theme_path : std::as_const(icon_paths)) {
162 QDir icon_theme_dir = QDir(icon_theme_path.absoluteFilePath());
163
164 if (icon_theme_dir.exists(filters_index.at(0))) {
165 icon_theme_names << icon_theme_dir.dirName();
166 }
167 }
168 }
169
170 icon_theme_names.removeDuplicates();
171 return icon_theme_names;
172 }