Better use of Qt APIs in Plasma::Theme

Summary:
Try not to access hashes repeatedly.
QRegExp -> QRegularExpression.

Reviewers: #plasma, #frameworks, fvogt, mart

Reviewed By: #plasma, mart

Subscribers: bruns, tcanabrava, fvogt, broulik, kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D22147
This commit is contained in:
Aleix Pol 2019-06-30 12:48:02 +02:00
parent 97272465a8
commit 6c692309ed
4 changed files with 33 additions and 49 deletions

View File

@ -47,9 +47,7 @@ EffectWatcher *ThemePrivate::s_backgroundContrastEffectWatcher = nullptr;
#endif
ThemePrivate *ThemePrivate::globalTheme = nullptr;
QAtomicInt ThemePrivate::globalThemeRefCount = QAtomicInt();
QHash<QString, ThemePrivate *> ThemePrivate::themes = QHash<QString, ThemePrivate *>();
QHash<QString, QAtomicInt> ThemePrivate::themesRefCount = QHash<QString, QAtomicInt>();
ThemePrivate::ThemePrivate(QObject *parent)
: QObject(parent),

View File

@ -58,7 +58,7 @@ enum CacheType {
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 @@ public:
#endif
//Ref counting of ThemePrivate instances
static ThemePrivate *globalTheme;
static QAtomicInt globalThemeRefCount;
static QHash<QString, ThemePrivate *> themes;
static QHash<QString, QAtomicInt> themesRefCount;
QString themeName;
KPluginInfo pluginInfo;

View File

@ -310,13 +310,14 @@ QPixmap SvgPrivate::findInCache(const QString &elementId, qreal ratio, const QSi
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 @@ QPixmap SvgPrivate::findInCache(const QString &elementId, qreal ratio, const QSi
// Look at the size hinted elements and try to find the smallest one with an
// identical aspect ratio.
if (s.isValid() && !elementId.isEmpty()) {
QList<QSize> elementSizeHints = elementsWithSizeHints.values(elementId);
const QList<QSize> 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 @@ QPixmap SvgPrivate::findInCache(const QString &elementId, qreal ratio, const QSi
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;

View File

@ -54,7 +54,7 @@ Theme::Theme(QObject *parent)
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(QObject *parent)
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(const QString &themeName, QObject *parent)
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 @@ void Theme::setThemeName(const QString &themeName)
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 @@ bool Theme::findInCache(const QString &key, QPixmap &pix, unsigned int lastModif
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)
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);