experimental devicePixelRatio in Plasma::Svg

Conflicts:
	src/declarativeimports/core/framesvgitem.cpp
This commit is contained in:
Marco Martin 2014-01-28 15:15:55 +01:00
parent 8c65f60de5
commit 17332257d7
7 changed files with 60 additions and 12 deletions

View File

@ -82,6 +82,7 @@ void FrameSvgItem::setImagePath(const QString &path)
m_frameSvg->setImagePath(path);
m_frameSvg->setElementPrefix(m_prefix);
updateDevicePixelRatio();
if (implicitWidth() <= 0) {
setImplicitWidth(m_frameSvg->marginSize(Plasma::Types::LeftMargin) + m_frameSvg->marginSize(Plasma::Types::RightMargin));
@ -228,6 +229,12 @@ void FrameSvgItem::componentComplete()
}
void FrameSvgItem::updateDevicePixelRatio()
{
m_frameSvg->resize();
m_frameSvg->resize(m_frameSvg->size() * 5);
}
} // Plasma namespace
#include "framesvgitem.moc"

View File

@ -23,6 +23,8 @@
#include <Plasma/FrameSvg>
#include "units.h"
namespace Plasma {
class FrameSvg;
@ -155,11 +157,13 @@ Q_SIGNALS:
private Q_SLOTS:
void doUpdate();
void updateDevicePixelRatio();
private:
Plasma::FrameSvg *m_frameSvg;
FrameSvgItemMargins *m_margins;
QString m_prefix;
Units m_units;
};
}

View File

@ -19,6 +19,8 @@
#include "svgitem.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include "QDebug"
@ -68,10 +70,10 @@ QSizeF SvgItem::naturalSize() const
if (!m_svg) {
return QSizeF();
} else if (!m_elementID.isEmpty()) {
return m_svg.data()->elementSize(m_elementID);
return m_svg.data()->elementSize(m_elementID) * floor(m_units.devicePixelRatio());
}
return m_svg.data()->size();
return m_svg.data()->size() * floor(m_units.devicePixelRatio());
}

View File

@ -21,6 +21,8 @@
#include <QQuickPaintedItem>
#include "units.h"
namespace Plasma {
class Svg;
@ -104,6 +106,7 @@ private:
QWeakPointer<Plasma::Svg> m_svg;
QString m_elementID;
bool m_smooth;
Units m_units;
};
}

View File

@ -107,6 +107,7 @@ public:
QSizeF naturalSize;
QChar styleCrc;
unsigned int lastModified;
qreal devicePixelRatio;
bool multipleImages : 1;
bool themed : 1;
bool applyColors : 1;

View File

@ -129,14 +129,15 @@ bool SharedSvgRenderer::load(
}
#define QLSEP QLatin1Char('_')
#define CACHE_ID_WITH_SIZE(size, id) QString::number(int(size.width())) % QLSEP % QString::number(int(size.height())) % QLSEP % id
#define CACHE_ID_NATURAL_SIZE(id) QLatin1Literal("Natural") % QLSEP % id
#define CACHE_ID_WITH_SIZE(size, id, devicePixelRatio) QString::number(int(size.width())) % QLSEP % QString::number(int(size.height())) % QLSEP % id % QLSEP % QString::number(int(devicePixelRatio))
#define CACHE_ID_NATURAL_SIZE(id, devicePixelRatio) QLatin1Literal("Natural") % QLSEP % id % QLSEP % QString::number(int(devicePixelRatio))
SvgPrivate::SvgPrivate(Svg *svg)
: q(svg),
renderer(0),
styleCrc(0),
lastModified(0),
devicePixelRatio(2.0),
multipleImages(false),
themed(false),
applyColors(false),
@ -155,16 +156,16 @@ SvgPrivate::~SvgPrivate()
QString SvgPrivate::cacheId(const QString &elementId)
{
if (size.isValid() && size != naturalSize) {
return CACHE_ID_WITH_SIZE(size, elementId);
return CACHE_ID_WITH_SIZE(size, elementId, devicePixelRatio);
} else {
return CACHE_ID_NATURAL_SIZE(elementId);
return CACHE_ID_NATURAL_SIZE(elementId, devicePixelRatio);
}
}
//This function is meant for the pixmap cache
QString SvgPrivate::cachePath(const QString &path, const QSize &size)
{
return CACHE_ID_WITH_SIZE(size, path);
return CACHE_ID_WITH_SIZE(size, path, devicePixelRatio);
}
bool SvgPrivate::setImagePath(const QString &imagePath)
@ -221,7 +222,7 @@ bool SvgPrivate::setImagePath(const QString &imagePath)
naturalSize = rect.size();
} else {
createRenderer();
naturalSize = renderer->defaultSize();
naturalSize = renderer->defaultSize() * devicePixelRatio;
//qDebug() << "natural size for" << path << "from renderer is" << naturalSize;
cacheAndColorsTheme()->insertIntoRectsCache(path, "_Natural", QRectF(QPointF(0,0), naturalSize));
//qDebug() << "natural size for" << path << "from cache is" << naturalSize;
@ -271,7 +272,7 @@ QPixmap SvgPrivate::findInCache(const QString &elementId, const QSizeF &s)
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("(\\d+)-(\\d+)-(.+)"));
QRegExp sizeHintedKeyExpr(CACHE_ID_NATURAL_SIZE("(\\d+)-(\\d+)-(.+)", devicePixelRatio));
foreach (const QString &key, cacheAndColorsTheme()->listCachedRectKeys(path)) {
if (sizeHintedKeyExpr.exactMatch(key)) {
@ -438,7 +439,7 @@ void SvgPrivate::createRenderer()
const QString &elementId = i.key();
const QRectF &elementRect = i.value();
const QString cacheId = CACHE_ID_NATURAL_SIZE(elementId);
const QString cacheId = CACHE_ID_NATURAL_SIZE(elementId, devicePixelRatio);
localRectCache.insert(cacheId, elementRect);
cacheAndColorsTheme()->insertIntoRectsCache(path, cacheId, elementRect);
}
@ -513,12 +514,12 @@ QRectF SvgPrivate::findAndCacheElementRect(const QString &elementId)
QRectF elementRect = renderer->elementExists(elementId) ?
renderer->matrixForElement(elementId).map(renderer->boundsOnElement(elementId)).boundingRect() :
QRectF();
naturalSize = renderer->defaultSize();
naturalSize = renderer->defaultSize() * devicePixelRatio;
qreal dx = size.width() / naturalSize.width();
qreal dy = size.height() / naturalSize.height();
elementRect = QRectF(elementRect.x() * dx, elementRect.y() * dy,
elementRect.width() * dx, elementRect.height() * dy);
elementRect.width() * dx * devicePixelRatio, elementRect.height() * dy * devicePixelRatio);
cacheAndColorsTheme()->insertIntoRectsCache(path, id, elementRect);
return elementRect;
@ -659,6 +660,23 @@ Svg::~Svg()
delete d;
}
void Svg::setDevicePixelRatio(qreal ratio)
{
//be completely integer for now
if (floor(d->devicePixelRatio) == floor(ratio)) {
return;
}
d->devicePixelRatio = floor(ratio);
emit repaintNeeded();
emit sizeChanged();
}
qreal Svg::devicePixelRatio()
{
return d->devicePixelRatio;
}
QPixmap Svg::pixmap(const QString &elementID)
{
if (elementID.isNull() || d->multipleImages) {

View File

@ -80,6 +80,19 @@ class PLASMA_EXPORT Svg : public QObject
explicit Svg(QObject *parent = 0);
~Svg();
/**
* Set the device pixel ratio for the Svg. This is the ratio between
* image pixels and device-independent pixels.
* The default value is 1.0.
* Setting it to something more, will make all the elements of this svg appear bigger.
*/
void setDevicePixelRatio(qreal ratio);
/**
* @return the device pixel ratio for this Svg.
*/
qreal devicePixelRatio();
/**
* Returns a pixmap of the SVG represented by this object.
*