Introduce Units singleton

Every single (Frame)SvgItem would creates its own Units instance which in turn would
create a property map for icon sizes and lots of other stuff. Avoid this.

CHANGELOG: There is now a global "Units" instance reducing memory consumption and creation time of SVG items

Differential Revision: https://phabricator.kde.org/D4012
This commit is contained in:
Kai Uwe Broulik 2017-01-08 16:00:20 +01:00
parent d46a91ea57
commit 56773014e1
7 changed files with 24 additions and 9 deletions

View File

@ -59,8 +59,7 @@ void CoreBindingsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
Plasma::QuickTheme *theme = new Plasma::QuickTheme(engine);
context->setContextProperty(QStringLiteral("theme"), theme);
Units *units = new Units(context);
context->setContextProperty(QStringLiteral("units"), units);
context->setContextProperty(QStringLiteral("units"), &Units::instance());
if (!engine->rootContext()->contextObject()) {
KDeclarative::KDeclarative kdeclarative;

View File

@ -265,7 +265,7 @@ FrameSvgItem::FrameSvgItem(QQuickItem *parent)
m_fixedMargins->setFixed(true);
setFlag(ItemHasContents, true);
connect(m_frameSvg, SIGNAL(repaintNeeded()), this, SLOT(doUpdate()));
connect(&m_units, &Units::devicePixelRatioChanged, this, &FrameSvgItem::updateDevicePixelRatio);
connect(&Units::instance(), &Units::devicePixelRatioChanged, this, &FrameSvgItem::updateDevicePixelRatio);
connect(m_frameSvg, &Svg::fromCurrentThemeChanged, this, &FrameSvgItem::fromCurrentThemeChanged);
connect(m_frameSvg, &Svg::statusChanged, this, &FrameSvgItem::statusChanged);
}
@ -532,7 +532,7 @@ void FrameSvgItem::updateDevicePixelRatio()
} else {
m_frameSvg->setDevicePixelRatio(qMax<qreal>(1.0, floor(qApp->devicePixelRatio())));
}
m_frameSvg->setScaleFactor(qMax<qreal>(1.0, floor(m_units.devicePixelRatio())));
m_frameSvg->setScaleFactor(qMax<qreal>(1.0, floor(Units::instance().devicePixelRatio())));
m_textureChanged = true;
}

View File

@ -236,7 +236,6 @@ private:
FrameSvgItemMargins *m_margins;
FrameSvgItemMargins *m_fixedMargins;
QString m_prefix;
Units m_units;
bool m_textureChanged;
bool m_sizeChanged;
bool m_fastPath;

View File

@ -40,7 +40,7 @@ SvgItem::SvgItem(QQuickItem *parent)
m_textureChanged(false)
{
setFlag(QQuickItem::ItemHasContents, true);
connect(&m_units, &Units::devicePixelRatioChanged, this, &SvgItem::updateDevicePixelRatio);
connect(&Units::instance(), &Units::devicePixelRatioChanged, this, &SvgItem::updateDevicePixelRatio);
}
SvgItem::~SvgItem()
@ -197,7 +197,7 @@ void SvgItem::updateDevicePixelRatio()
} else {
m_svg.data()->setDevicePixelRatio(qMax<qreal>(1.0, floor(qApp->devicePixelRatio())));
}
m_svg.data()->setScaleFactor(qMax<qreal>(1.0, floor(m_units.devicePixelRatio())));
m_svg.data()->setScaleFactor(qMax<qreal>(1.0, floor(Units::instance().devicePixelRatio())));
}
}

View File

@ -106,7 +106,6 @@ private:
QString m_elementID;
bool m_smooth;
bool m_textureChanged;
Units m_units;
QImage m_image;
};
}

View File

@ -91,6 +91,13 @@ Units::Units(QObject *parent)
Units::~Units()
{
}
Units &Units::instance()
{
static Units units;
return units;
}
void Units::settingsFileChanged(const QString &file)

View File

@ -117,9 +117,14 @@ class Units : public QObject
public:
/// @cond INTERNAL_DOCS
Units(QObject *parent = 0);
~Units();
/**
* @return a reference to the global Units instance
* @since 5.31
*/
static Units &instance();
/**
* @return pixel value for a grid Unit. Depends on DPI and font size.
*/
@ -181,6 +186,12 @@ private Q_SLOTS:
void updateSpacing();
private:
Units(QObject *parent = 0);
Units(Units const&) = delete; // Copy construct
Units(Units&&) = delete; // Move construct
Units& operator=(Units const&) = delete; // Copy assign
Units& operator=(Units &&) = delete; // Move assign
void updateDevicePixelRatio();
void updatePlasmaRCSettings();
/**