-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstylestorage.cpp
More file actions
289 lines (248 loc) · 7.08 KB
/
stylestorage.cpp
File metadata and controls
289 lines (248 loc) · 7.08 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "stylestorage.h"
#include "imagemanager.h"
#include <QDir>
#include <QFile>
#include <QWidget>
#include <QVariant>
#include <QFileInfo>
#include <QApplication>
#include <QtXml>
#define FOLDER_DEFAULT "images"
#define IMAGES_FOLDER_PATH "%IMAGES_PATH%"
#define STYLEVALUES_FILE "stylevalues.xml"
QHash<QString, StyleStorage *> StyleStorage::FStaticStorages;
QHash<QObject *, StyleStorage *> StyleStorage::FObjectStorage;
QStringList StyleStorage::_systemStyleSuffixes;
struct StyleStorage::StyleUpdateParams
{
QString key;
int index;
};
StyleStorage::StyleStorage(const QString &AStorage, const QString &ASubStorage, QObject *AParent) : FileStorage(AStorage,ASubStorage,AParent)
{
FStyleValuesLoaded = false;
connect(this,SIGNAL(storageChanged()),SLOT(onStorageChanged()));
}
StyleStorage::~StyleStorage()
{
foreach(QObject *object, FUpdateParams.keys()) {
removeObject(object); }
}
QString StyleStorage::getStyle(const QString &AKey, int AIndex) const
{
QFile file(fileFullName(AKey, AIndex));
if (file.open(QFile::ReadOnly))
{
QString folder = fileOption(AKey, STYLE_STORAGE_OPTION_IMAGES_FOLDER);
if (folder.isEmpty())
folder = FOLDER_DEFAULT;
folder = QFileInfo(file.fileName()).absoluteDir().absoluteFilePath(folder);
QString resultingStyle = QString::fromUtf8(file.readAll());
foreach (QString suffix, systemStyleSuffixes())
{
QString sFileName = fileFullName(AKey, AIndex, suffix);
QFile sFile(sFileName);
if (sFile.open(QFile::ReadOnly))
{
resultingStyle += "\n";
resultingStyle += QString::fromUtf8(sFile.readAll());
}
}
return resultingStyle.replace(IMAGES_FOLDER_PATH,folder);;
}
return QString::null;
}
void StyleStorage::insertAutoStyle(QObject *AObject, const QString &AKey, int AIndex)
{
StyleStorage *oldStorage = FObjectStorage.value(AObject);
if (oldStorage!=NULL && oldStorage!=this)
oldStorage->removeAutoStyle(AObject);
if (AObject && !AKey.isEmpty())
{
StyleUpdateParams *params;
if (oldStorage != this)
{
params = new StyleUpdateParams;
FObjectStorage.insert(AObject,this);
FUpdateParams.insert(AObject,params);
}
else
{
params = FUpdateParams.value(AObject);
}
params->key = AKey;
params->index = AIndex;
updateObject(AObject);
connect(AObject,SIGNAL(destroyed(QObject *)),SLOT(onObjectDestroyed(QObject *)));
}
else if (AObject != NULL)
{
removeAutoStyle(AObject);
}
}
void StyleStorage::removeAutoStyle(QObject *AObject)
{
if (FUpdateParams.contains(AObject))
{
AObject->setProperty("styleSheet",QString());
removeObject(AObject);
disconnect(AObject,SIGNAL(destroyed(QObject *)),this,SLOT(onObjectDestroyed(QObject *)));
}
}
QString StyleStorage::fileFullName(const QString AKey, int AIndex) const
{
return FileStorage::fileFullName(AKey, AIndex);
}
QString StyleStorage::fileFullName(const QString AKey, int AIndex, const QString & suffix) const
{
QString filename = fileFullName(AKey, AIndex);
QFileInfo finfo(filename);
return finfo.absoluteDir().absolutePath() + "/" +finfo.baseName() + suffix + "." + finfo.completeSuffix();
}
QVariant StyleStorage::getStyleValue(const QString & AKey)
{
if (!FStyleValuesLoaded)
loadStyleValues();
QStringList keys;
foreach(QString suffix, systemStyleSuffixes())
{
keys += AKey + suffix;
}
keys += AKey;
foreach(QString key, keys)
{
if (FStyleValues.contains(key))
return FStyleValues.value(key);
}
return QVariant();
}
QColor StyleStorage::getStyleColor(const QString & AKey)
{
return getStyleValue(AKey).value<QColor>();
}
int StyleStorage::getStyleInt(const QString & AKey, int ADefaultValue)
{
QVariant value = getStyleValue(AKey);
bool ok = false;
int intValue = value.toInt(&ok);
return ok ? intValue : ADefaultValue;
}
qreal StyleStorage::getStyleReal(const QString & AKey, qreal ADefaultValue)
{
QVariant value = getStyleValue(AKey);
bool ok = false;
qreal realValue = value.toReal(&ok);
return ok ? realValue : ADefaultValue;
}
bool StyleStorage::getStyleBool(const QString & AKey)
{
return getStyleValue(AKey).toBool();
}
void StyleStorage::previewReset()
{
onStorageChanged();
emit stylePreviewReset();
}
void StyleStorage::previewStyle(const QString &AStyleSheet, const QString &AKey, int AIndex)
{
for (QHash<QObject *, StyleUpdateParams *>::iterator it=FUpdateParams.begin(); it!=FUpdateParams.end(); it++)
{
if (it.value()->key==AKey && it.value()->index==AIndex)
it.key()->setProperty("styleSheet", AStyleSheet);
}
}
StyleStorage *StyleStorage::staticStorage(const QString &AStorage)
{
StyleStorage *styleStorage = FStaticStorages.value(AStorage,NULL);
if (!styleStorage)
{
styleStorage = new StyleStorage(AStorage,STORAGE_SHARED_DIR,qApp);
FStaticStorages.insert(AStorage,styleStorage);
}
return styleStorage;
}
void StyleStorage::updateStyle(QObject * object)
{
if (QWidget * w = qobject_cast<QWidget*>(object))
{
w->setStyleSheet(w->styleSheet());
}
else
{
object->setProperty("styleSheet", object->property("styleSheet"));
}
}
QStringList StyleStorage::systemStyleSuffixes()
{
if (_systemStyleSuffixes.isEmpty())
{
#if defined Q_WS_MAC
_systemStyleSuffixes += "_mac";
#elif defined Q_WS_WIN
_systemStyleSuffixes += "_win";
#elif defined Q_WS_X11
_systemStyleSuffixes += "_x11";
#elif defined Q_WS_S60
_systemStyleSuffixes += "_s60";
#endif
}
return _systemStyleSuffixes;
}
void StyleStorage::loadStyleValues()
{
if (FStyleValuesLoaded)
return;
QStringList dirs = subStorageDirs(storage(), subStorage());
if (!dirs.count())
return;
QString dir = dirs.value(0);
QFile f(QString("%1/%2").arg(dir).arg(STYLEVALUES_FILE));
if (!f.open(QFile::ReadOnly))
return;
QDomDocument doc;
doc.setContent(f.readAll());
f.close();
QDomElement valueEl = doc.documentElement().firstChildElement("stylevalue");
QString key, type, value;
// TODO: add more types
while (!valueEl.isNull())
{
key = valueEl.attribute("key");
type = valueEl.attribute("type");
value = valueEl.attribute("value");
if (type == "color")
{
// parsing color
QColor c = ImageManager::resolveColor(value);
FStyleValues.insert(key, c);
}
else
{
// save as common variant
FStyleValues.insert(key, value);
}
valueEl = valueEl.nextSiblingElement("stylevalue");
}
FStyleValuesLoaded = true;
}
void StyleStorage::updateObject(QObject *AObject)
{
StyleUpdateParams *params = FUpdateParams.value(AObject);
QString style = getStyle(params->key,params->index);
AObject->setProperty("styleSheet",style);
}
void StyleStorage::removeObject(QObject *AObject)
{
FObjectStorage.remove(AObject);
StyleUpdateParams *params = FUpdateParams.take(AObject);
delete params;
}
void StyleStorage::onStorageChanged()
{
for (QHash<QObject *, StyleUpdateParams *>::iterator it=FUpdateParams.begin(); it!=FUpdateParams.end(); it++)
updateObject(it.key());
}
void StyleStorage::onObjectDestroyed(QObject *AObject)
{
removeObject(AObject);
}