-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstylesheetloader.cpp
More file actions
210 lines (173 loc) · 5.14 KB
/
stylesheetloader.cpp
File metadata and controls
210 lines (173 loc) · 5.14 KB
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
#include "stylesheetloader.h"
#include <QtCore/QDir>
#include <QtCore/QSet>
#include <QtCore/QSettings>
#include <QtGui/QApplication>
#include "filesystemwatcher.h"
using namespace Utils;
namespace {
const char * const STYLESHEET_SUFFIX = "qss";
}
/*!
* \class Utils::StyleSheetLoader
* \brief Can find, load and reload automatically upon change a Qt Style Sheet
*/
StyleSheetLoader::StyleSheetLoader()
: QObject(0),
m_oldStyleSheet(qApp->styleSheet()),
m_fileWatcher(new FileSystemWatcher(this))
{
connect(m_fileWatcher, SIGNAL(pathChanged(QString)),
this, SLOT(reload()));
restoreSettings();
}
StyleSheetLoader::~StyleSheetLoader()
{
}
//! Implements the Singleton pattern
StyleSheetLoader *StyleSheetLoader::instance()
{
static StyleSheetLoader loader;
return &loader;
}
//! WORKAROUND: Use it to prevent focus rectangle to collide with button label
/*! This function is provided as central point for temporary fix related to
* focus rectangle which covers some letters with vertical lines on left/right
* side (B {Back, Browse}, N {Next}, l {Cancel}). Qt has poor support for
* style sheets, a lot of bugs reported are not fixed even alter few years.
* Until that changes or we change font to another with "better" metrics the
* button text is extended by whitespaces.
*/
QString StyleSheetLoader::fixButtonText(const QString &text)
{
return QString(" %1 ").arg(text);
}
//! Query if a global style sheet is set at the moment
bool StyleSheetLoader::isStyleSheetSet() const
{
return !qApp->styleSheet().isEmpty();
}
//! Select the default style sheet
void StyleSheetLoader::setDefaultName(const QString &name)
{
m_defaultName = name;
}
//! Query the default style sheet
QString StyleSheetLoader::defaultName() const
{
return m_defaultName;
}
//! Set paths to search for style sheets
void StyleSheetLoader::setPaths(const QStringList &paths)
{
m_paths = QSet<QString>::fromList(paths);
findFiles();
}
//! Query paths searched for style sheets
QStringList StyleSheetLoader::paths() const
{
return m_paths.values();
}
//! Query names of available style sheets
QStringList StyleSheetLoader::names() const
{
QStringList names = m_namePathMap.keys();
names.sort();
return names;
}
//! Query name of the active style sheet
QString StyleSheetLoader::activeName() const
{
return m_activeName;
}
//! Load style sheet given by its name
void StyleSheetLoader::load(const QString &name)
{
m_activeName = name;
reload();
}
//! Unload active style sheet
void StyleSheetLoader::unload()
{
stopWatcher();
qApp->setStyleSheet(QString());
}
//! Reload active style sheet
void StyleSheetLoader::reload()
{
findFiles();
unload();
if (m_activeName.isEmpty()) {
qApp->setStyleSheet(m_oldStyleSheet);
}
else {
qApp->setStyleSheet(QLatin1String("file:///")
+ m_namePathMap[m_activeName]);
startWatcherOnActiveFile();
}
}
void StyleSheetLoader::restoreSettings()
{
QSettings settings;
settings.beginGroup("StyleSheetLoader");
m_activeName =
settings.value(QLatin1String("CurrentStyleSheet")).toString();
settings.endGroup();
}
void StyleSheetLoader::saveSettings()
{
QSettings settings;
settings.beginGroup(QLatin1String("StyleSheetLoader"));
settings.setValue(QLatin1String("CurrentStyleSheet"), m_activeName);
settings.endGroup();
}
void StyleSheetLoader::findFiles()
{
m_namePathMap.clear();
QFileInfoList infoList;
foreach (const QString &path, m_paths) {
QDir dir(path);
if (!dir.exists()) {
continue;
}
// Search for files in registered paths
infoList << dir.entryInfoList(
QStringList() << QString("*.%1").arg(STYLESHEET_SUFFIX),
QDir::Files);
// Search for files in subfolders in registered paths - not recursively
QFileInfoList dirInfoList = dir.entryInfoList(
QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QFileInfo &dirInfo, dirInfoList) {
QFileInfo info(QString("%1/%2.%3").arg(dirInfo.absoluteFilePath())
.arg(dirInfo.completeBaseName()).arg(STYLESHEET_SUFFIX));
if (info.exists()) {
infoList << info;
}
}
}
// Update map
foreach (const QFileInfo &info, infoList) {
m_namePathMap[info.completeBaseName()] = info.absoluteFilePath();
}
// Update internal variables if needed
if (!m_namePathMap.keys().contains(m_defaultName)) {
m_defaultName.clear();
}
if (!m_namePathMap.keys().contains(m_activeName)) {
m_activeName = m_defaultName;
}
saveSettings();
}
void StyleSheetLoader::startWatcherOnActiveFile()
{
Q_ASSERT(!m_activeName.isEmpty());
Q_ASSERT(m_namePathMap.keys().contains(m_activeName));
stopWatcher();
if (!m_fileWatcher->paths().contains(m_namePathMap[m_activeName]))
m_fileWatcher->addPath(m_namePathMap[m_activeName]);
}
void StyleSheetLoader::stopWatcher()
{
if (m_fileWatcher->paths().contains(m_namePathMap[m_activeName]))
m_fileWatcher->removePath(m_namePathMap[m_activeName]);
}