-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgraphicseffectsstorage.cpp
More file actions
313 lines (294 loc) · 9.09 KB
/
graphicseffectsstorage.cpp
File metadata and controls
313 lines (294 loc) · 9.09 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "graphicseffectsstorage.h"
#include <QDomDocument>
#include <QGraphicsDropShadowEffect>
#include <QSet>
#include <QFile>
#include <QWidget>
#include <QApplication>
#include <QCheckBox>
#include <QRadioButton>
#include <QStyleOptionButton>
#include <QPainter>
static QColor parseColor(const QString & name)
{
QColor color;
if (QColor::isValidColor(name))
color.setNamedColor(name);
else
{
// trying to parse "#RRGGBBAA" color
if (name.length() == 9)
{
QString solidColor = name.left(7);
if (QColor::isValidColor(solidColor))
{
color.setNamedColor(solidColor);
int alpha = name.right(2).toInt(0, 16);
color.setAlpha(alpha);
}
}
}
return color;
}
class TextDropShadowEffect : public QGraphicsDropShadowEffect
{
public:
TextDropShadowEffect(QObject* parent) : QGraphicsDropShadowEffect(parent) { }
protected:
void draw(QPainter *painter)
{
if (parent())
{
if (qobject_cast<QCheckBox*>(parent()) || qobject_cast<QRadioButton*>(parent()))
{
QWidget * w = qobject_cast<QWidget*>(parent());
QStyle * style = w->style() ? w->style() : qApp->style();
QStyleOptionButton * opt = new QStyleOptionButton;
opt->rect = w->rect();
int indicatorWidth = style->subElementRect(QStyle::SE_CheckBoxIndicator, opt, w).width() + xOffset() + 2; // 2 is a magic namba!
painter->setClipRect(0, 0, indicatorWidth, w->height());
QPoint offset;
const QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset);
QTransform restoreTransform = painter->worldTransform();
painter->setWorldTransform(QTransform());
painter->drawPixmap(offset, pixmap);
painter->setWorldTransform(restoreTransform);
painter->setClipRect(indicatorWidth, 0, w->width() - indicatorWidth, w->height());
}
}
if (painter->isActive())
QGraphicsDropShadowEffect::draw(painter);
}
};
GraphicsEffectsStorage::GraphicsEffectsStorage(const QString &AStorage, const QString &ASubStorage, QObject *AParent) :
FileStorage(AStorage, ASubStorage, AParent)
{
}
GraphicsEffectsStorage::~GraphicsEffectsStorage()
{
}
bool GraphicsEffectsStorage::installGraphicsEffect(QWidget * widget, const QString & key)
{
QList<EffectMask> masks = keyMaskCache.values(key);
if (masks.isEmpty())
{
parseFile(key);
masks = keyMaskCache.values(key);
}
if (widget && !masks.isEmpty())
{
foreach (EffectMask mask, masks)
if (widetMatchesTheMask(widget, mask))
widget->setGraphicsEffect(effectForMask(mask, widget));
foreach (QObject * child, widget->children())
if (child->isWidgetType())
installGraphicsEffect(qobject_cast<QWidget*>(child), key);
return true;
}
return false;
}
bool GraphicsEffectsStorage::installGraphicsEffect(const QString & key)
{
QList<EffectMask> masks = keyMaskCache.values(key);
if (masks.isEmpty())
{
parseFile(key);
masks = keyMaskCache.values(key);
}
foreach (QWidget* widget, qApp->allWidgets())
foreach (EffectMask mask, masks)
if (widetMatchesTheMask(widget, mask))
widget->setGraphicsEffect(effectForMask(mask, widget));
return true;
}
bool GraphicsEffectsStorage::uninstallGraphicsEffect(QWidget * widget, const QString & key)
{
QList<EffectMask> masks = keyMaskCache.values(key);
if (masks.isEmpty())
{
parseFile(key);
masks = keyMaskCache.values(key);
}
if (widget && !masks.isEmpty())
{
foreach (EffectMask mask, masks)
if (widetMatchesTheMask(widget, mask))
widget->setGraphicsEffect(NULL);
foreach (QObject * child, widget->children())
if (child->isWidgetType())
uninstallGraphicsEffect(qobject_cast<QWidget*>(child), key);
return true;
}
return false;
}
bool GraphicsEffectsStorage::uninstallGraphicsEffect(const QString & key)
{
QList<EffectMask> masks = keyMaskCache.values(key);
if (masks.isEmpty())
{
parseFile(key);
masks = keyMaskCache.values(key);
}
foreach (QWidget* widget, qApp->allWidgets())
foreach (EffectMask mask, masks)
if (widetMatchesTheMask(widget, mask))
widget->setGraphicsEffect(NULL);
return true;
}
QList<QGraphicsEffect*> GraphicsEffectsStorage::getEffects(const QString & key)
{
QList<EffectMask> masks = keyMaskCache.values(key);
if (masks.isEmpty())
{
parseFile(key);
masks = keyMaskCache.values(key);
}
QList<QGraphicsEffect*> effects;
foreach (EffectMask mask, masks)
effects << effectForMask(mask, NULL);
return effects;
}
QGraphicsEffect * GraphicsEffectsStorage::getFirstEffect(const QString & key)
{
QList<QGraphicsEffect*> effects = getEffects(key);
QGraphicsEffect * effect = effects.isEmpty() ? NULL : effects.first();
if (effect)
{
effects.removeFirst();
qDeleteAll(effects);
}
return effect;
}
GraphicsEffectsStorage * GraphicsEffectsStorage::staticStorage(const QString & storage)
{
GraphicsEffectsStorage * _storage = staticStorages.value(storage, NULL);
if (!_storage)
{
_storage = new GraphicsEffectsStorage(storage, STORAGE_SHARED_DIR, qApp);
staticStorages.insert(storage, _storage);
}
return _storage;
}
void GraphicsEffectsStorage::parseFile(const QString & key)
{
if (!loadedKeysCache.contains(key))
{
QString fileKey = fileCacheKey(key);
if (!fileKey.isEmpty())
{
QString fileName = fileFullName(key);
if (!fileName.isEmpty())
{
QFile file(fileName);
if (file.open(QFile::ReadOnly))
{
QString s = QString::fromUtf8(file.readAll());
QDomDocument doc;
if (doc.setContent(s))
{
QDomElement root = doc.firstChildElement("graphics-effects");
if (!root.isNull())
{
QDomElement effect = root.firstChildElement("effect");
while (!effect.isNull())
{
EffectMask mask;
mask.key = key;
QDomElement classes = effect.firstChildElement("classes");
if (!classes.isNull())
{
QDomElement classEl = classes.firstChildElement("class");
while (!classEl.isNull())
{
mask.classNames.append(classEl.text());
classEl = classEl.nextSiblingElement("class");
}
}
QDomElement names = effect.firstChildElement("names");
if (!names.isNull())
{
QDomElement nameEl = names.firstChildElement("name");
while (!nameEl.isNull())
{
mask.objectNames.append(nameEl.text());
nameEl = nameEl.nextSiblingElement("name");
}
}
keyMaskCache.insert(key, mask);
QGraphicsEffect * newEffect = parseGraphicEffect(effect);
if (newEffect)
{
effectCache.insert(mask, newEffect);
}
effect = effect.nextSiblingElement("effect");
}
}
}
}
}
}
loadedKeysCache.insert(key);
}
}
QGraphicsEffect * GraphicsEffectsStorage::parseGraphicEffect(const QDomElement & element)
{
if (element.attribute("type") == "shadow")
{
TextDropShadowEffect * effect = new TextDropShadowEffect(NULL);
QDomElement color = element.firstChildElement("color");
if (!color.isNull())
{
effect->setColor(parseColor(color.text()));
}
QDomElement offset = element.firstChildElement("offset");
if (!offset.isNull())
{
effect->setXOffset(offset.attribute("x").toInt());
effect->setYOffset(offset.attribute("y").toInt());
}
QDomElement blur = element.firstChildElement("blur");
if (!blur.isNull())
{
// we won't need it i think...
//effect->setBlurRadius(blur.attribute("radius").toDouble());
}
return effect;
}
return NULL;
}
QGraphicsEffect * GraphicsEffectsStorage::copyEffect(const QGraphicsEffect * effect) const
{
// only QGraphicsDropShadowEffect for now
if (const QGraphicsDropShadowEffect* shadowEffect = qobject_cast<const QGraphicsDropShadowEffect*>(effect))
{
TextDropShadowEffect * copy = new TextDropShadowEffect(effect->parent());
copy->setOffset(shadowEffect->offset());
copy->setBlurRadius(shadowEffect->blurRadius());
copy->setColor(shadowEffect->color());
return copy;
}
return NULL;
}
QGraphicsEffect * GraphicsEffectsStorage::effectForMask(const GraphicsEffectsStorage::EffectMask & mask, QObject * parent) const
{
// NOTE: graphics effects work bad on Qt 4.8.0 (Mac OS X only)
// see https://bugreports.qt.nokia.com/browse/QTBUG-23205
#if (QT_VERSION >= 0x040800) && defined(Q_WS_MAC)
Q_UNUSED(mask)
Q_UNUSED(parent)
return NULL;
#else
QGraphicsEffect * effect = copyEffect(effectCache.value(mask, NULL));
effect->setParent(parent);
return effect;
#endif
}
bool GraphicsEffectsStorage::widetMatchesTheMask(QWidget* widget, const GraphicsEffectsStorage::EffectMask & mask) const
{
return (mask.objectNames.contains(widget->objectName()) || mask.classNames.contains(widget->metaObject()->className()));
}
// static vars
QMultiHash<QString, GraphicsEffectsStorage::EffectMask> GraphicsEffectsStorage::keyMaskCache;
QHash<GraphicsEffectsStorage::EffectMask, QGraphicsEffect*> GraphicsEffectsStorage::effectCache;
QHash<QString, GraphicsEffectsStorage *> GraphicsEffectsStorage::staticStorages;
QSet<QString> GraphicsEffectsStorage::loadedKeysCache;