diff --git a/src/plasma/private/theme_p.h b/src/plasma/private/theme_p.h --- a/src/plasma/private/theme_p.h +++ b/src/plasma/private/theme_p.h @@ -58,7 +58,7 @@ Q_DECLARE_FLAGS(CacheTypes, CacheType) Q_DECLARE_OPERATORS_FOR_FLAGS(CacheTypes) -class ThemePrivate : public QObject +class ThemePrivate : public QObject, public QSharedData { Q_OBJECT @@ -107,9 +107,7 @@ #endif //Ref counting of ThemePrivate instances static ThemePrivate *globalTheme; - static QAtomicInt globalThemeRefCount; static QHash themes; - static QHash themesRefCount; QString themeName; KPluginInfo pluginInfo; diff --git a/src/plasma/private/theme_p.cpp b/src/plasma/private/theme_p.cpp --- a/src/plasma/private/theme_p.cpp +++ b/src/plasma/private/theme_p.cpp @@ -47,9 +47,7 @@ #endif ThemePrivate *ThemePrivate::globalTheme = nullptr; -QAtomicInt ThemePrivate::globalThemeRefCount = QAtomicInt(); QHash ThemePrivate::themes = QHash(); -QHash ThemePrivate::themesRefCount = QHash(); ThemePrivate::ThemePrivate(QObject *parent) : QObject(parent), diff --git a/src/plasma/svg.cpp b/src/plasma/svg.cpp --- a/src/plasma/svg.cpp +++ b/src/plasma/svg.cpp @@ -310,13 +310,14 @@ if (elementsWithSizeHints.isEmpty()) { // Fetch all size hinted element ids from the theme's rect cache // and store them locally. - QRegExp sizeHintedKeyExpr(CACHE_ID_NATURAL_SIZE(QStringLiteral("(\\d+)-(\\d+)-(.+)"), status, ratio)); + static const QRegularExpression sizeHintedKeyExpr(CACHE_ID_NATURAL_SIZE(QStringLiteral("$(\\d+)-(\\d+)-(.+)^"), status, ratio)); foreach (const QString &key, cacheAndColorsTheme()->listCachedRectKeys(path)) { - if (sizeHintedKeyExpr.exactMatch(key)) { - QString baseElementId = sizeHintedKeyExpr.cap(3); - QSize sizeHint(sizeHintedKeyExpr.cap(1).toInt(), - sizeHintedKeyExpr.cap(2).toInt()); + const auto match = sizeHintedKeyExpr.match(key); + if (match.hasMatch()) { + QString baseElementId = match.captured(3); + QSize sizeHint(match.capturedRef(1).toInt(), + match.capturedRef(2).toInt()); if (sizeHint.isValid()) { elementsWithSizeHints.insertMulti(baseElementId, sizeHint); @@ -333,12 +334,12 @@ // Look at the size hinted elements and try to find the smallest one with an // identical aspect ratio. if (s.isValid() && !elementId.isEmpty()) { - QList elementSizeHints = elementsWithSizeHints.values(elementId); + const QList elementSizeHints = elementsWithSizeHints.values(elementId); if (!elementSizeHints.isEmpty()) { QSize bestFit(-1, -1); - Q_FOREACH (QSize hint, elementSizeHints) { + for (const QSize &hint : elementSizeHints) { if (hint.width() >= s.width() * ratio && hint.height() >= s.height() * ratio && (!bestFit.isValid() || @@ -368,11 +369,7 @@ return QPixmap(); } - QString id = cachePath(path, size); - - if (!actualElementId.isEmpty()) { - id.append(actualElementId); - } + const QString id = cachePath(path, size) + actualElementId; //qCDebug(LOG_PLASMA) << "id is " << id; diff --git a/src/plasma/theme.cpp b/src/plasma/theme.cpp --- a/src/plasma/theme.cpp +++ b/src/plasma/theme.cpp @@ -54,7 +54,7 @@ if (!ThemePrivate::globalTheme) { ThemePrivate::globalTheme = new ThemePrivate; } - ThemePrivate::globalThemeRefCount.ref(); + ThemePrivate::globalTheme->ref.ref(); d = ThemePrivate::globalTheme; d->settingsChanged(false); @@ -70,13 +70,13 @@ Theme::Theme(const QString &themeName, QObject *parent) : QObject(parent) { - if (!ThemePrivate::themes.contains(themeName)) { - ThemePrivate::themes[themeName] = new ThemePrivate; - ThemePrivate::themesRefCount[themeName] = QAtomicInt(); + auto& priv = ThemePrivate::themes[themeName]; + if (!priv) { + priv = new ThemePrivate; } - ThemePrivate::themesRefCount[themeName].ref(); - d = ThemePrivate::themes[themeName]; + priv->ref.ref(); + d = priv; // turn off caching so we don't accidentally trigger unnecessary disk activity at this point bool useCache = d->cacheTheme; @@ -94,18 +94,15 @@ Theme::~Theme() { if (d == ThemePrivate::globalTheme) { - if (!ThemePrivate::globalThemeRefCount.deref()) { + if (!d->ref.deref()) { disconnect(ThemePrivate::globalTheme, nullptr, this, nullptr); delete ThemePrivate::globalTheme; ThemePrivate::globalTheme = nullptr; d = nullptr; } } else { - if (!ThemePrivate::themesRefCount[d->themeName].deref()) { - ThemePrivate *themePrivate = ThemePrivate::themes[d->themeName]; - ThemePrivate::themes.remove(d->themeName); - ThemePrivate::themesRefCount.remove(d->themeName); - delete themePrivate; + if (!d->ref.deref()) { + delete ThemePrivate::themes.take(d->themeName); } } } @@ -118,18 +115,16 @@ if (d != ThemePrivate::globalTheme) { disconnect(QCoreApplication::instance(), nullptr, d, nullptr); - if (!ThemePrivate::themesRefCount[d->themeName].deref()) { - ThemePrivate *themePrivate = ThemePrivate::themes[d->themeName]; - ThemePrivate::themes.remove(d->themeName); - ThemePrivate::themesRefCount.remove(d->themeName); - delete themePrivate; + if (!d->ref.deref()) { + delete ThemePrivate::themes.take(d->themeName); } - if (!ThemePrivate::themes.contains(themeName)) { - ThemePrivate::themes[themeName] = new ThemePrivate; - ThemePrivate::themesRefCount[themeName] = QAtomicInt(); + + auto& priv = ThemePrivate::themes[themeName]; + if (!priv) { + priv = new ThemePrivate; } - ThemePrivate::themesRefCount[themeName].ref(); - d = ThemePrivate::themes[themeName]; + priv->ref.ref(); + d = priv; if (QCoreApplication::instance()) { connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, d, &ThemePrivate::onAppExitCleanup); @@ -300,8 +295,9 @@ if (d->useCache()) { const QString id = d->keysToCache.value(key); - if (d->pixmapsToCache.contains(id)) { - pix = d->pixmapsToCache.value(id); + const auto it = d->pixmapsToCache.constFind(id); + if (it != d->pixmapsToCache.constEnd()) { + pix = *it; return !pix.isNull(); } @@ -325,14 +321,9 @@ void Theme::insertIntoCache(const QString &key, const QPixmap &pix, const QString &id) { if (d->useCache()) { - d->pixmapsToCache.insert(id, pix); - - if (d->idsToCache.contains(id)) { - d->keysToCache.remove(d->idsToCache[id]); - } - - d->keysToCache.insert(key, id); - d->idsToCache.insert(id, key); + d->pixmapsToCache[id] = pix; + d->keysToCache[key] = id; + d->idsToCache[id] = key; //always start timer in d->pixmapSaveTimer's thread QMetaObject::invokeMethod(d->pixmapSaveTimer, "start", Qt::QueuedConnection);