Changeset View
Changeset View
Standalone View
Standalone View
src/plasma/theme.cpp
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Line(s) | |||||
49 | { | 49 | { | ||
50 | 50 | | |||
51 | Theme::Theme(QObject *parent) | 51 | Theme::Theme(QObject *parent) | ||
52 | : QObject(parent) | 52 | : QObject(parent) | ||
53 | { | 53 | { | ||
54 | if (!ThemePrivate::globalTheme) { | 54 | if (!ThemePrivate::globalTheme) { | ||
55 | ThemePrivate::globalTheme = new ThemePrivate; | 55 | ThemePrivate::globalTheme = new ThemePrivate; | ||
56 | } | 56 | } | ||
57 | ThemePrivate::globalThemeRefCount.ref(); | 57 | ThemePrivate::globalTheme->ref.ref(); | ||
58 | d = ThemePrivate::globalTheme; | 58 | d = ThemePrivate::globalTheme; | ||
59 | 59 | | |||
60 | d->settingsChanged(false); | 60 | d->settingsChanged(false); | ||
61 | if (QCoreApplication::instance()) { | 61 | if (QCoreApplication::instance()) { | ||
62 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, | 62 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, | ||
63 | d, &ThemePrivate::onAppExitCleanup); | 63 | d, &ThemePrivate::onAppExitCleanup); | ||
64 | } | 64 | } | ||
65 | connect(d, &ThemePrivate::themeChanged, this, &Theme::themeChanged); | 65 | connect(d, &ThemePrivate::themeChanged, this, &Theme::themeChanged); | ||
66 | connect(d, &ThemePrivate::defaultFontChanged, this, &Theme::defaultFontChanged); | 66 | connect(d, &ThemePrivate::defaultFontChanged, this, &Theme::defaultFontChanged); | ||
67 | connect(d, &ThemePrivate::smallestFontChanged, this, &Theme::smallestFontChanged); | 67 | connect(d, &ThemePrivate::smallestFontChanged, this, &Theme::smallestFontChanged); | ||
68 | } | 68 | } | ||
69 | 69 | | |||
70 | Theme::Theme(const QString &themeName, QObject *parent) | 70 | Theme::Theme(const QString &themeName, QObject *parent) | ||
71 | : QObject(parent) | 71 | : QObject(parent) | ||
72 | { | 72 | { | ||
73 | if (!ThemePrivate::themes.contains(themeName)) { | 73 | auto& priv = ThemePrivate::themes[themeName]; | ||
74 | ThemePrivate::themes[themeName] = new ThemePrivate; | 74 | if (!priv) { | ||
75 | ThemePrivate::themesRefCount[themeName] = QAtomicInt(); | 75 | priv = new ThemePrivate; | ||
76 | } | 76 | } | ||
77 | 77 | | |||
78 | ThemePrivate::themesRefCount[themeName].ref(); | 78 | priv->ref.ref(); | ||
79 | d = ThemePrivate::themes[themeName]; | 79 | d = priv; | ||
80 | 80 | | |||
81 | // turn off caching so we don't accidentally trigger unnecessary disk activity at this point | 81 | // turn off caching so we don't accidentally trigger unnecessary disk activity at this point | ||
82 | bool useCache = d->cacheTheme; | 82 | bool useCache = d->cacheTheme; | ||
83 | d->cacheTheme = false; | 83 | d->cacheTheme = false; | ||
84 | d->setThemeName(themeName, false, false); | 84 | d->setThemeName(themeName, false, false); | ||
85 | d->cacheTheme = useCache; | 85 | d->cacheTheme = useCache; | ||
86 | d->fixedName = true; | 86 | d->fixedName = true; | ||
87 | if (QCoreApplication::instance()) { | 87 | if (QCoreApplication::instance()) { | ||
88 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, | 88 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, | ||
89 | d, &ThemePrivate::onAppExitCleanup); | 89 | d, &ThemePrivate::onAppExitCleanup); | ||
90 | } | 90 | } | ||
91 | connect(d, &ThemePrivate::themeChanged, this, &Theme::themeChanged); | 91 | connect(d, &ThemePrivate::themeChanged, this, &Theme::themeChanged); | ||
92 | } | 92 | } | ||
93 | 93 | | |||
94 | Theme::~Theme() | 94 | Theme::~Theme() | ||
95 | { | 95 | { | ||
96 | if (d == ThemePrivate::globalTheme) { | 96 | if (d == ThemePrivate::globalTheme) { | ||
97 | if (!ThemePrivate::globalThemeRefCount.deref()) { | 97 | if (!d->ref.deref()) { | ||
98 | disconnect(ThemePrivate::globalTheme, nullptr, this, nullptr); | 98 | disconnect(ThemePrivate::globalTheme, nullptr, this, nullptr); | ||
99 | delete ThemePrivate::globalTheme; | 99 | delete ThemePrivate::globalTheme; | ||
100 | ThemePrivate::globalTheme = nullptr; | 100 | ThemePrivate::globalTheme = nullptr; | ||
101 | d = nullptr; | 101 | d = nullptr; | ||
102 | } | 102 | } | ||
103 | } else { | 103 | } else { | ||
104 | if (!ThemePrivate::themesRefCount[d->themeName].deref()) { | 104 | if (!d->ref.deref()) { | ||
broulik: Can we clean up all of this custom refcounting by using a `QSharedPointer`? | |||||
apol: Implemented with QSharedData. | |||||
105 | ThemePrivate *themePrivate = ThemePrivate::themes[d->themeName]; | 105 | delete ThemePrivate::themes.take(d->themeName); | ||
106 | ThemePrivate::themes.remove(d->themeName); | | |||
107 | ThemePrivate::themesRefCount.remove(d->themeName); | | |||
108 | delete themePrivate; | | |||
109 | } | 106 | } | ||
110 | } | 107 | } | ||
111 | } | 108 | } | ||
112 | 109 | | |||
113 | void Theme::setThemeName(const QString &themeName) | 110 | void Theme::setThemeName(const QString &themeName) | ||
114 | { | 111 | { | ||
115 | if (d->themeName == themeName) { | 112 | if (d->themeName == themeName) { | ||
116 | return; | 113 | return; | ||
117 | } | 114 | } | ||
118 | 115 | | |||
119 | if (d != ThemePrivate::globalTheme) { | 116 | if (d != ThemePrivate::globalTheme) { | ||
120 | disconnect(QCoreApplication::instance(), nullptr, d, nullptr); | 117 | disconnect(QCoreApplication::instance(), nullptr, d, nullptr); | ||
121 | if (!ThemePrivate::themesRefCount[d->themeName].deref()) { | 118 | if (!d->ref.deref()) { | ||
122 | ThemePrivate *themePrivate = ThemePrivate::themes[d->themeName]; | 119 | delete ThemePrivate::themes.take(d->themeName); | ||
123 | ThemePrivate::themes.remove(d->themeName); | | |||
124 | ThemePrivate::themesRefCount.remove(d->themeName); | | |||
125 | delete themePrivate; | | |||
126 | } | | |||
127 | if (!ThemePrivate::themes.contains(themeName)) { | | |||
128 | ThemePrivate::themes[themeName] = new ThemePrivate; | | |||
129 | ThemePrivate::themesRefCount[themeName] = QAtomicInt(); | | |||
130 | } | 120 | } | ||
131 | ThemePrivate::themesRefCount[themeName].ref(); | 121 | | ||
132 | d = ThemePrivate::themes[themeName]; | 122 | auto& priv = ThemePrivate::themes[themeName]; | ||
123 | if (!priv) { | ||||
124 | priv = new ThemePrivate; | ||||
125 | } | ||||
126 | priv->ref.ref(); | ||||
127 | d = priv; | ||||
133 | if (QCoreApplication::instance()) { | 128 | if (QCoreApplication::instance()) { | ||
134 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, | 129 | connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, | ||
135 | d, &ThemePrivate::onAppExitCleanup); | 130 | d, &ThemePrivate::onAppExitCleanup); | ||
136 | } | 131 | } | ||
137 | connect(d, &ThemePrivate::themeChanged, this, &Theme::themeChanged); | 132 | connect(d, &ThemePrivate::themeChanged, this, &Theme::themeChanged); | ||
138 | } | 133 | } | ||
139 | 134 | | |||
140 | d->setThemeName(themeName, true, true); | 135 | d->setThemeName(themeName, true, true); | ||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Line(s) | |||||
295 | bool Theme::findInCache(const QString &key, QPixmap &pix, unsigned int lastModified) | 290 | bool Theme::findInCache(const QString &key, QPixmap &pix, unsigned int lastModified) | ||
296 | { | 291 | { | ||
297 | if (lastModified != 0 && d->useCache() && lastModified > uint(d->pixmapCache->lastModifiedTime().toSecsSinceEpoch())) { | 292 | if (lastModified != 0 && d->useCache() && lastModified > uint(d->pixmapCache->lastModifiedTime().toSecsSinceEpoch())) { | ||
298 | return false; | 293 | return false; | ||
299 | } | 294 | } | ||
300 | 295 | | |||
301 | if (d->useCache()) { | 296 | if (d->useCache()) { | ||
302 | const QString id = d->keysToCache.value(key); | 297 | const QString id = d->keysToCache.value(key); | ||
303 | if (d->pixmapsToCache.contains(id)) { | 298 | const auto it = d->pixmapsToCache.constFind(id); | ||
304 | pix = d->pixmapsToCache.value(id); | 299 | if (it != d->pixmapsToCache.constEnd()) { | ||
300 | pix = *it; | ||||
305 | return !pix.isNull(); | 301 | return !pix.isNull(); | ||
306 | } | 302 | } | ||
307 | 303 | | |||
308 | QPixmap temp; | 304 | QPixmap temp; | ||
309 | if (d->pixmapCache->findPixmap(key, &temp) && !temp.isNull()) { | 305 | if (d->pixmapCache->findPixmap(key, &temp) && !temp.isNull()) { | ||
310 | pix = temp; | 306 | pix = temp; | ||
311 | return true; | 307 | return true; | ||
312 | } | 308 | } | ||
313 | } | 309 | } | ||
314 | 310 | | |||
315 | return false; | 311 | return false; | ||
316 | } | 312 | } | ||
317 | 313 | | |||
318 | void Theme::insertIntoCache(const QString &key, const QPixmap &pix) | 314 | void Theme::insertIntoCache(const QString &key, const QPixmap &pix) | ||
319 | { | 315 | { | ||
320 | if (d->useCache()) { | 316 | if (d->useCache()) { | ||
321 | d->pixmapCache->insertPixmap(key, pix); | 317 | d->pixmapCache->insertPixmap(key, pix); | ||
322 | } | 318 | } | ||
323 | } | 319 | } | ||
324 | 320 | | |||
325 | void Theme::insertIntoCache(const QString &key, const QPixmap &pix, const QString &id) | 321 | void Theme::insertIntoCache(const QString &key, const QPixmap &pix, const QString &id) | ||
326 | { | 322 | { | ||
327 | if (d->useCache()) { | 323 | if (d->useCache()) { | ||
328 | d->pixmapsToCache.insert(id, pix); | 324 | d->pixmapsToCache[id] = pix; | ||
So this code removes the item if already existing and then inserts it again? This thing is a QHash so insert will just override the existing one anyway, no need to remove it first. broulik: So this code removes the item if already existing and then inserts it again? This thing is a… | |||||
329 | 325 | d->keysToCache[key] = id; | |||
330 | if (d->idsToCache.contains(id)) { | 326 | d->idsToCache[id] = key; | ||
331 | d->keysToCache.remove(d->idsToCache[id]); | | |||
332 | } | | |||
333 | | ||||
334 | d->keysToCache.insert(key, id); | | |||
335 | d->idsToCache.insert(id, key); | | |||
336 | 327 | | |||
337 | //always start timer in d->pixmapSaveTimer's thread | 328 | //always start timer in d->pixmapSaveTimer's thread | ||
338 | QMetaObject::invokeMethod(d->pixmapSaveTimer, "start", Qt::QueuedConnection); | 329 | QMetaObject::invokeMethod(d->pixmapSaveTimer, "start", Qt::QueuedConnection); | ||
339 | } | 330 | } | ||
340 | } | 331 | } | ||
341 | 332 | | |||
342 | bool Theme::findInRectsCache(const QString &image, const QString &element, QRectF &rect) const | 333 | bool Theme::findInRectsCache(const QString &image, const QString &element, QRectF &rect) const | ||
343 | { | 334 | { | ||
▲ Show 20 Lines • Show All 177 Lines • Show Last 20 Lines |
Can we clean up all of this custom refcounting by using a QSharedPointer?