From a9d3d9a81ab282298088bf0753c1c77f0ec21afe Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Fri, 20 Nov 2015 14:26:16 +0100 Subject: [PATCH] make event filters static to decrease installed filters on qApp since seems there are so many event filters installed on the QApplication installed that may give performanc issues, try to use separate, singleton watchers for them to decrease the amount of eventfilters called REVIEW:126113 --- src/declarativeimports/core/units.cpp | 38 ++++++++++++++++++++------- src/declarativeimports/core/units.h | 19 +++++++++++--- src/plasma/private/svg_p.h | 1 + src/plasma/private/theme_p.cpp | 1 + src/plasma/private/theme_p.h | 1 + src/plasma/svg.cpp | 11 +++----- 6 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/declarativeimports/core/units.cpp b/src/declarativeimports/core/units.cpp index 4e2adae8f..fa34772c3 100644 --- a/src/declarativeimports/core/units.cpp +++ b/src/declarativeimports/core/units.cpp @@ -37,6 +37,28 @@ const QString plasmarc = QStringLiteral("plasmarc"); const QString groupName = QStringLiteral("Units"); const int defaultLongDuration = 120; + +SharedAppFilter::SharedAppFilter(QObject *parent) + : QObject(parent) +{ + QCoreApplication::instance()->installEventFilter(this); +} + +SharedAppFilter::~SharedAppFilter() +{} + +bool SharedAppFilter::eventFilter(QObject *watched, QEvent *event) +{ + if (watched == QCoreApplication::instance()) { + if (event->type() == QEvent::ApplicationFontChange) { + emit fontChanged(); + } + } + return QObject::eventFilter(watched, event); +} + +SharedAppFilter *Units::s_sharedAppFilter = nullptr; + Units::Units(QObject *parent) : QObject(parent), m_gridUnit(-1), @@ -45,12 +67,16 @@ Units::Units(QObject *parent) m_largeSpacing(-1), m_longDuration(defaultLongDuration) // default base value for animations { + if (!s_sharedAppFilter) { + s_sharedAppFilter = new SharedAppFilter(); + } + m_iconSizes = new QQmlPropertyMap(this); updateDevicePixelRatio(); // also updates icon sizes updateSpacing(); // updates gridUnit and *Spacing properties connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, &Units::iconLoaderSettingsChanged); - QCoreApplication::instance()->installEventFilter(this); + QObject::connect(s_sharedAppFilter, SIGNAL(fontChanged()), this, SLOT(updateSpacing())); const QString configFile = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + plasmarc; KDirWatch::self()->addFile(configFile); @@ -236,13 +262,5 @@ int Units::shortDuration() const return m_longDuration / 5; } -bool Units::eventFilter(QObject *watched, QEvent *event) -{ - if (watched == QCoreApplication::instance()) { - if (event->type() == QEvent::ApplicationFontChange || event->type() == QEvent::FontChange) { - updateSpacing(); - } - } - return QObject::eventFilter(watched, event); -} +#include "moc_units.cpp" diff --git a/src/declarativeimports/core/units.h b/src/declarativeimports/core/units.h index fa2256e4c..c8c057bf8 100644 --- a/src/declarativeimports/core/units.h +++ b/src/declarativeimports/core/units.h @@ -28,6 +28,20 @@ class QQuickItem; +class SharedAppFilter : public QObject +{ + Q_OBJECT +public: + SharedAppFilter(QObject *parent = 0); + ~SharedAppFilter(); + +Q_SIGNALS: + void fontChanged(); + +protected: + bool eventFilter(QObject *watched, QEvent *event); +}; + /** * @class Units * @short Expose sizes to QML @@ -106,8 +120,6 @@ public: Units(QObject *parent = 0); ~Units(); - bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE; - /** * @return pixel value for a grid Unit. Depends on DPI and font size. */ @@ -166,10 +178,10 @@ Q_SIGNALS: private Q_SLOTS: void iconLoaderSettingsChanged(); void settingsFileChanged(const QString &file); + void updateSpacing(); private: void updateDevicePixelRatio(); - void updateSpacing(); void updatePlasmaRCSettings(); /** * @return The dpi-adjusted size for a given icon size @@ -181,6 +193,7 @@ private: qreal m_dpi; QQmlPropertyMap *m_iconSizes; + static SharedAppFilter *s_sharedAppFilter; int m_smallSpacing; int m_largeSpacing; diff --git a/src/plasma/private/svg_p.h b/src/plasma/private/svg_p.h index 597465ebe..1d1000dc9 100644 --- a/src/plasma/private/svg_p.h +++ b/src/plasma/private/svg_p.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace Plasma { diff --git a/src/plasma/private/theme_p.cpp b/src/plasma/private/theme_p.cpp index f34634392..7bf81d423 100644 --- a/src/plasma/private/theme_p.cpp +++ b/src/plasma/private/theme_p.cpp @@ -363,6 +363,7 @@ void ThemePrivate::colorsChanged() buttonColorScheme = KColorScheme(QPalette::Active, KColorScheme::Button, colors); viewColorScheme = KColorScheme(QPalette::Active, KColorScheme::View, colors); scheduleThemeChangeNotification(PixmapCache); + emit applicationPaletteChange(); } void ThemePrivate::scheduleThemeChangeNotification(CacheTypes caches) diff --git a/src/plasma/private/theme_p.h b/src/plasma/private/theme_p.h index 5b8f71c01..ae4d0aa7e 100644 --- a/src/plasma/private/theme_p.h +++ b/src/plasma/private/theme_p.h @@ -95,6 +95,7 @@ Q_SIGNALS: void themeChanged(); void defaultFontChanged(); void smallestFontChanged(); + void applicationPaletteChange(); public: static const char defaultTheme[]; diff --git a/src/plasma/svg.cpp b/src/plasma/svg.cpp index bcceaf7ec..ad03f4187 100644 --- a/src/plasma/svg.cpp +++ b/src/plasma/svg.cpp @@ -44,6 +44,7 @@ namespace Plasma { + SharedSvgRenderer::SharedSvgRenderer(QObject *parent) : QSvgRenderer(parent) { @@ -579,20 +580,15 @@ void SvgPrivate::checkColorHints() // a colorscheme if (qApp) { if (usesColors && (!themed || !actualTheme()->colorScheme())) { - qApp->installEventFilter(q); + QObject::connect(actualTheme()->d, SIGNAL(applicationPaletteChange()), q, SLOT(colorsChanged())); } else { - qApp->removeEventFilter(q); + QObject::disconnect(actualTheme()->d, SIGNAL(applicationPaletteChange()), q, SLOT(colorsChanged())); } } } bool Svg::eventFilter(QObject *watched, QEvent *event) { - if (watched == QCoreApplication::instance()) { - if (event->type() == QEvent::ApplicationPaletteChange) { - d->colorsChanged(); - } - } return QObject::eventFilter(watched, event); } @@ -978,4 +974,5 @@ Theme *Svg::theme() const } // Plasma namespace +#include "private/moc_svg_p.cpp" #include "moc_svg.cpp"