diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2016-03-08 19:38:03 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2016-03-08 20:50:56 -0800 |
commit | 44deaf6c5176df3ee8f1277db8ddba2fe3c45cfc (patch) | |
tree | b2584fcba45587315d649a87714a49d614266cc2 | |
parent | 298235eec78541b1051e6bb1411d98ec98dcebf3 (diff) | |
download | subsurface-44deaf6c5176df3ee8f1277db8ddba2fe3c45cfc.tar.gz |
QMLProfile: account for device pixel ratio when transforming the profile
This is turning into the never ending story. The documentation is not helpful,
so this is the result of a ton of trial and error - and I worry that the next
device I try this on will once again break things.
I think the scaling may finally be correct. Take the viewport rectangles
of the profile and the painter, create their ratio, multiply in the device
pixel ratio and the scene size with a little margin added around it.
But the other magic (especially the shifting of the profile on the
painter) makes absolutely no sense to me.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | qt-mobile/qml/ThemeTest.qml | 3 | ||||
-rw-r--r-- | qt-mobile/qmlprofile.cpp | 37 |
2 files changed, 35 insertions, 5 deletions
diff --git a/qt-mobile/qml/ThemeTest.qml b/qt-mobile/qml/ThemeTest.qml index ed6631e46..e30615da8 100644 --- a/qt-mobile/qml/ThemeTest.qml +++ b/qt-mobile/qml/ThemeTest.qml @@ -1,6 +1,7 @@ import QtQuick 2.5 import QtQuick.Controls 1.4 import QtQuick.Layouts 1.1 +import QtQuick.Window 2.2 import org.kde.plasma.mobilecomponents 0.2 as MobileComponents MobileComponents.Page { @@ -61,7 +62,7 @@ MobileComponents.Page { text: "Units.devicePixelRatio:" } MobileComponents.Label { - text: MobileComponents.Units.devicePixelRatio + text: Screen.devicePixelRatio } MobileComponents.Heading { diff --git a/qt-mobile/qmlprofile.cpp b/qt-mobile/qmlprofile.cpp index e382e4e07..7725786f0 100644 --- a/qt-mobile/qmlprofile.cpp +++ b/qt-mobile/qmlprofile.cpp @@ -27,13 +27,42 @@ QMLProfile::~QMLProfile() void QMLProfile::paint(QPainter *painter) { // let's look at the intended size of the content and scale our scene accordingly - QRect rect = m_profileWidget->contentsRect(); + QRect painterRect = painter->viewport(); + QRect profileRect = m_profileWidget->viewport()->rect(); + qDebug() << "profile viewport and painter viewport" << profileRect << painterRect; qreal sceneSize = 104; // that should give us 2% margin all around (100x100 scene) - qreal sx = rect.width() / sceneSize; - qreal sy = rect.height() / sceneSize; - QTransform profileTransform; + qreal dprComp = devicePixelRatio() * painterRect.width() / profileRect.width(); + qreal sx = painterRect.width() / sceneSize / dprComp; + qreal sy = painterRect.height() / sceneSize / dprComp; + + // next figure out the weird magic by which we need to shift the painter so the profile is shown + int dpr = rint(devicePixelRatio()); + qreal magicShiftFactor = (dpr == 2 ? 0.25 : (dpr == 3 ? 0.33 : 0.0)); + + // now set up the transformations scale the profile and + // shift the painter (taking its existing transformation into account) + QTransform profileTransform = QTransform(); profileTransform.scale(sx, sy); + QTransform painterTransform = painter->transform(); + painterTransform.translate(-painterRect.width() * magicShiftFactor ,-painterRect.height() * magicShiftFactor); + + // some debugging messages to help adjust this in case the magic above is insufficient + QMLManager::instance()->appendTextToLog(QString("dpr %1 profile viewport %2 %3 painter viewport %4 %5").arg(dpr).arg(profileRect.width()).arg(profileRect.height()) + .arg(painterRect.width()).arg(painterRect.height())); + QMLManager::instance()->appendTextToLog(QString("profile matrix %1 %2 %3 %4 %5 %6 %7 %8 %9").arg(profileTransform.m11()).arg(profileTransform.m12()).arg(profileTransform.m13()) + .arg(profileTransform.m21()).arg(profileTransform.m22()).arg(profileTransform.m23()) + .arg(profileTransform.m31()).arg(profileTransform.m32()).arg(profileTransform.m33())); + QMLManager::instance()->appendTextToLog(QString("painter matrix %1 %2 %3 %4 %5 %6 %7 %8 %9").arg(painterTransform.m11()).arg(painterTransform.m12()).arg(painterTransform.m13()) + .arg(painterTransform.m21()).arg(painterTransform.m22()).arg(painterTransform.m23()) + .arg(painterTransform.m31()).arg(painterTransform.m32()).arg(painterTransform.m33())); + qDebug() << "profile scaled by" << profileTransform.m11() << profileTransform.m22() << "and translated by" << profileTransform.m31() << profileTransform.m32(); + qDebug() << "exist profile transform" << m_profileWidget->transform() << "painter transform" << painter->transform(); + + // apply the transformation + painter->setTransform(painterTransform); m_profileWidget->setTransform(profileTransform); + + // finally, render the profile m_profileWidget->render(painter); } |