From 8efb57c22df66d52c1abf1a21f1ce605485d749c Mon Sep 17 00:00:00 2001 From: Matej Knopp Date: Tue, 20 Jan 2015 16:21:41 +0100 Subject: [PATCH] use different cached images for different device pixel ratios Signed-off-by: Matej Knopp --- src/widgets/styles/qfusionstyle.cpp | 30 +++++++++++++++++++----------- src/widgets/styles/qstyle_p.h | 9 +++++---- src/widgets/styles/qstylehelper.cpp | 4 +++- src/widgets/styles/qstylehelper_p.h | 2 +- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/widgets/styles/qfusionstyle.cpp b/src/widgets/styles/qfusionstyle.cpp index ed7b4ab..8f42e71 100644 --- a/src/widgets/styles/qfusionstyle.cpp +++ b/src/widgets/styles/qfusionstyle.cpp @@ -63,6 +63,14 @@ QT_BEGIN_NAMESPACE +static QWindow *qWidgetWindow(const QWidget *widget) +{ + if (widget && widget->window()) + return widget->window()->windowHandle(); + else + return 0; +} + using namespace QStyleHelper; enum Direction { @@ -1240,13 +1248,13 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio painter->save(); // Draws the header in tables. if (const QStyleOptionHeader *header = qstyleoption_cast(option)) { - QString pixmapName = QStyleHelper::uniqueName(QLatin1String("headersection"), option, option->rect.size()); + QString pixmapName = QStyleHelper::uniqueName(QLatin1String("headersection"), option, option->rect.size(), qWidgetWindow(widget)); pixmapName += QString::number(- int(header->position)); pixmapName += QString::number(- int(header->orientation)); QPixmap cache; if (!QPixmapCache::find(pixmapName, cache)) { - cache = styleCachePixmap(rect.size()); + cache = styleCachePixmap(rect.size(), qWidgetWindow(widget)); cache.fill(Qt::transparent); QRect pixmapRect(0, 0, rect.width(), rect.height()); QPainter cachePainter(&cache); @@ -1982,10 +1990,10 @@ void QFusionStyle::drawComplexControl(ComplexControl control, const QStyleOption case CC_SpinBox: if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast(option)) { QPixmap cache; - QString pixmapName = QStyleHelper::uniqueName(QLatin1String("spinbox"), spinBox, spinBox->rect.size()); + QString pixmapName = QStyleHelper::uniqueName(QLatin1String("spinbox"), spinBox, spinBox->rect.size(), qWidgetWindow(widget)); if (!QPixmapCache::find(pixmapName, cache)) { - cache = styleCachePixmap(spinBox->rect.size()); + cache = styleCachePixmap(spinBox->rect.size(), qWidgetWindow(widget)); cache.fill(Qt::transparent); QRect pixmapRect(0, 0, spinBox->rect.width(), spinBox->rect.height()); @@ -2705,7 +2713,7 @@ void QFusionStyle::drawComplexControl(ComplexControl control, const QStyleOption bool sunken = comboBox->state & State_On; // play dead, if combobox has no items bool isEnabled = (comboBox->state & State_Enabled); QPixmap cache; - QString pixmapName = QStyleHelper::uniqueName(QLatin1String("combobox"), option, comboBox->rect.size()); + QString pixmapName = QStyleHelper::uniqueName(QLatin1String("combobox"), option, comboBox->rect.size(), qWidgetWindow(widget)); if (sunken) pixmapName += QLatin1String("-sunken"); if (comboBox->editable) @@ -2714,7 +2722,7 @@ void QFusionStyle::drawComplexControl(ComplexControl control, const QStyleOption pixmapName += QLatin1String("-enabled"); if (!QPixmapCache::find(pixmapName, cache)) { - cache = styleCachePixmap(comboBox->rect.size()); + cache = styleCachePixmap(comboBox->rect.size(), qWidgetWindow(widget)); cache.fill(Qt::transparent); QPainter cachePainter(&cache); QRect pixmapRect(0, 0, comboBox->rect.width(), comboBox->rect.height()); @@ -2818,12 +2826,12 @@ void QFusionStyle::drawComplexControl(ComplexControl control, const QStyleOption grooveColor.setHsv(buttonColor.hue(), qMin(255, (int)(buttonColor.saturation())), qMin(255, (int)(buttonColor.value()*0.9))); - QString groovePixmapName = QStyleHelper::uniqueName(QLatin1String("slider_groove"), option, groove.size()); + QString groovePixmapName = QStyleHelper::uniqueName(QLatin1String("slider_groove"), option, groove.size(), qWidgetWindow(widget)); QRect pixmapRect(0, 0, groove.width(), groove.height()); // draw background groove if (!QPixmapCache::find(groovePixmapName, cache)) { - cache = styleCachePixmap(pixmapRect.size()); + cache = styleCachePixmap(pixmapRect.size(), qWidgetWindow(widget)); cache.fill(Qt::transparent); QPainter groovePainter(&cache); groovePainter.setRenderHint(QPainter::Antialiasing, true); @@ -2851,7 +2859,7 @@ void QFusionStyle::drawComplexControl(ComplexControl control, const QStyleOption QRect clipRect; groovePixmapName += QLatin1String("_blue"); if (!QPixmapCache::find(groovePixmapName, cache)) { - cache = styleCachePixmap(pixmapRect.size()); + cache = styleCachePixmap(pixmapRect.size(), qWidgetWindow(widget)); cache.fill(Qt::transparent); QPainter groovePainter(&cache); QLinearGradient gradient; @@ -2956,9 +2964,9 @@ void QFusionStyle::drawComplexControl(ComplexControl control, const QStyleOption } // draw handle if ((option->subControls & SC_SliderHandle) ) { - QString handlePixmapName = QStyleHelper::uniqueName(QLatin1String("slider_handle"), option, handle.size()); + QString handlePixmapName = QStyleHelper::uniqueName(QLatin1String("slider_handle"), option, handle.size(), qWidgetWindow(widget)); if (!QPixmapCache::find(handlePixmapName, cache)) { - cache = styleCachePixmap(handle.size()); + cache = styleCachePixmap(handle.size(), qWidgetWindow(widget)); cache.fill(Qt::transparent); QRect pixmapRect(0, 0, handle.width(), handle.height()); QPainter handlePainter(&cache); diff --git a/src/widgets/styles/qstyle_p.h b/src/widgets/styles/qstyle_p.h index 572d3d9..40f8cde 100644 --- a/src/widgets/styles/qstyle_p.h +++ b/src/widgets/styles/qstyle_p.h @@ -36,6 +36,7 @@ #include "private/qobject_p.h" #include +#include #include QT_BEGIN_NAMESPACE @@ -65,17 +66,17 @@ public: QStyle *proxyStyle; }; -inline QImage styleCacheImage(const QSize &size) +inline QImage styleCacheImage(const QSize &size, const QWindow *window = 0) { - const qreal pixelRatio = qApp->devicePixelRatio(); + const qreal pixelRatio = window ? window->devicePixelRatio() : qApp->devicePixelRatio(); QImage cacheImage = QImage(size * pixelRatio, QImage::Format_ARGB32_Premultiplied); cacheImage.setDevicePixelRatio(pixelRatio); return cacheImage; } -inline QPixmap styleCachePixmap(const QSize &size) +inline QPixmap styleCachePixmap(const QSize &size, const QWindow *window = 0) { - const qreal pixelRatio = qApp->devicePixelRatio(); + const qreal pixelRatio = window ? window->devicePixelRatio() : qApp->devicePixelRatio(); QPixmap cachePixmap = QPixmap(size * pixelRatio); cachePixmap.setDevicePixelRatio(pixelRatio); return cachePixmap; diff --git a/src/widgets/styles/qstylehelper.cpp b/src/widgets/styles/qstylehelper.cpp index 63cd0a0..07124fa 100644 --- a/src/widgets/styles/qstylehelper.cpp +++ b/src/widgets/styles/qstylehelper.cpp @@ -50,7 +50,7 @@ Q_GUI_EXPORT int qt_defaultDpiX(); namespace QStyleHelper { -QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size) +QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size, const QWindow *window) { const QStyleOptionComplex *complexOption = qstyleoption_cast(option); QString tmp = key % HexString(option->state) @@ -67,6 +67,8 @@ QString uniqueName(const QString &key, const QStyleOption *option, const QSize & % QLatin1Char(spinBox->frame ? '1' : '0'); ; } #endif // QT_NO_SPINBOX + if (window) + tmp = tmp % HexString(window->devicePixelRatio()); return tmp; } diff --git a/src/widgets/styles/qstylehelper_p.h b/src/widgets/styles/qstylehelper_p.h index a4ab295..f62a315 100644 --- a/src/widgets/styles/qstylehelper_p.h +++ b/src/widgets/styles/qstylehelper_p.h @@ -64,7 +64,7 @@ class QWindow; namespace QStyleHelper { - QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size); + QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size, const QWindow *window = 0); qreal dpiScaled(qreal value); #ifndef QT_NO_DIAL qreal angle(const QPointF &p1, const QPointF &p2); -- 1.9.3 (Apple Git-50)