summaryrefslogtreecommitdiffstats
path: root/src/pdf
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2019-11-28 11:51:07 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2020-02-03 23:43:49 +0100
commita8e4ad7726f1aa52624a0367558650cd4d899c79 (patch)
treefdef891d5e8b9e59dcb2965c1a5c08333c381607 /src/pdf
parentb700f65011eaecefc60f6a4760547ecfb5542e34 (diff)
PdfPageView: Add zoom-to-fit and zoom-to-width features
Change-Id: I40b92000a4def105d22a3bd10d0544b0b0f0fe1e Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/pdf')
-rw-r--r--src/pdf/quick/qml/PdfPageView.qml57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/pdf/quick/qml/PdfPageView.qml b/src/pdf/quick/qml/PdfPageView.qml
index 556cf1b7a..e20ebd1b4 100644
--- a/src/pdf/quick/qml/PdfPageView.qml
+++ b/src/pdf/quick/qml/PdfPageView.qml
@@ -56,6 +56,51 @@ Rectangle {
property real __pageScale: image.paintedWidth / document.pagePointSize(image.currentFrame).width
+ function resetScale() {
+ image.sourceSize.width = 0
+ image.sourceSize.height = 0
+ paper.x = 0
+ paper.y = 0
+ paper.scale = 1
+ }
+
+ function scaleToWidth(width, height) {
+ var halfRotation = Math.abs(paper.rotation % 180)
+ image.sourceSize = Qt.size((halfRotation > 45 && halfRotation < 135) ? height : width, 0)
+ paper.x = 0
+ paper.y = 0
+ image.centerInSize = Qt.size(width, height)
+ image.centerOnLoad = true
+ image.vCenterOnLoad = (halfRotation > 45 && halfRotation < 135)
+ paper.scale = 1
+ }
+
+ function scaleToPage(width, height) {
+ var windowAspect = width / height
+ var halfRotation = Math.abs(paper.rotation % 180)
+ var pagePointSize = document.pagePointSize(image.currentFrame)
+ if (halfRotation > 45 && halfRotation < 135) {
+ // rotated 90 or 270ยบ
+ var pageAspect = pagePointSize.height / pagePointSize.width
+ if (windowAspect > pageAspect) {
+ image.sourceSize = Qt.size(height, 0)
+ } else {
+ image.sourceSize = Qt.size(0, width)
+ }
+ } else {
+ var pageAspect = pagePointSize.width / pagePointSize.height
+ if (windowAspect > pageAspect) {
+ image.sourceSize = Qt.size(0, height)
+ } else {
+ image.sourceSize = Qt.size(width, 0)
+ }
+ }
+ image.centerInSize = Qt.size(width, height)
+ image.centerOnLoad = true
+ image.vCenterOnLoad = true
+ paper.scale = 1
+ }
+
PdfSelection {
id: selection
document: paper.document
@@ -79,13 +124,23 @@ Rectangle {
source: document.status === PdfDocument.Ready ? document.source : ""
asynchronous: true
fillMode: Image.PreserveAspectFit
+ property bool centerOnLoad: false
+ property bool vCenterOnLoad: false
+ property size centerInSize
+ onStatusChanged:
+ if (status == Image.Ready && centerOnLoad) {
+ paper.x = (centerInSize.width - image.implicitWidth) / 2
+ paper.y = vCenterOnLoad ? (centerInSize.height - image.implicitHeight) / 2 : 0
+ centerOnLoad = false
+ vCenterOnLoad = false
+ }
}
function reRenderIfNecessary() {
var newSourceWidth = image.sourceSize.width * paper.scale
var ratio = newSourceWidth / image.sourceSize.width
if (ratio > 1.1 || ratio < 0.9) {
image.sourceSize.width = newSourceWidth
- image.sourceSize.height = 1
+ image.sourceSize.height = 0
paper.scale = 1
}
}