diff options
Diffstat (limited to 'src/pdfquick/PdfMultiPageView.qml')
-rw-r--r-- | src/pdfquick/PdfMultiPageView.qml | 73 |
1 files changed, 48 insertions, 25 deletions
diff --git a/src/pdfquick/PdfMultiPageView.qml b/src/pdfquick/PdfMultiPageView.qml index 0f62a229d..194d7866e 100644 --- a/src/pdfquick/PdfMultiPageView.qml +++ b/src/pdfquick/PdfMultiPageView.qml @@ -1,5 +1,8 @@ // Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +pragma ComponentBehavior: Bound + import QtQuick import QtQuick.Controls import QtQuick.Pdf @@ -56,8 +59,8 @@ Item { */ function selectAll() { const currentItem = tableView.itemAtCell(tableView.cellAtPos(root.width / 2, root.height / 2)) - if (currentItem) - currentItem.selection.selectAll() + const pdfSelection = currentItem?.selection as PdfSelection + pdfSelection?.selectAll() } /*! @@ -70,9 +73,9 @@ Item { */ function copySelectionToClipboard() { const currentItem = tableView.itemAtCell(tableView.cellAtPos(root.width / 2, root.height / 2)) - console.log(lcMPV, "currentItem", currentItem, "sel", currentItem.selection.text) - if (currentItem) - currentItem.selection.copyToClipboard() + const pdfSelection = currentItem?.selection as PdfSelection + console.log(lcMPV, "currentItem", currentItem, "sel", pdfSelection?.text) + pdfSelection?.copyToClipboard() } // -------------------------------- @@ -159,6 +162,13 @@ Item { \sa PdfPageNavigator::jump(), currentPage */ function goToLocation(page, location, zoom) { + if (tableView.rows === 0) { + // save this request for later + tableView.pendingRow = page + tableView.pendingLocation = location + tableView.pendingZoom = zoom + return + } if (zoom > 0) { pageNavigator.jumping = true // don't call pageNavigator.update() because we will jump() instead root.renderScale = zoom @@ -298,26 +308,35 @@ Item { property point jumpLocationMargin: Qt.point(10, 10) // px away from viewport edges anchors.fill: parent anchors.leftMargin: 2 - model: modelInUse && root.document ? root.document.pageCount : 0 - // workaround to make TableView do scheduleRebuildTable(RebuildOption::All) in cases when forceLayout() doesn't - property bool modelInUse: true - function rebuild() { - modelInUse = false - modelInUse = true - } - // end workaround + model: root.document ? root.document.pageCount : 0 rowSpacing: 6 property real rotationNorm: Math.round((360 + (root.pageRotation % 360)) % 360) property bool rot90: rotationNorm == 90 || rotationNorm == 270 onRot90Changed: forceLayout() onHeightChanged: forceLayout() onWidthChanged: forceLayout() - property size firstPagePointSize: document?.status === PdfDocument.Ready ? document.pagePointSize(0) : Qt.size(1, 1) - property real pageHolderWidth: Math.max(root.width, ((rot90 ? document?.maxPageHeight : document?.maxPageWidth) ?? 0) * root.renderScale) - columnWidthProvider: function(col) { return document ? pageHolderWidth + vscroll.width + 2 : 0 } - rowHeightProvider: function(row) { return (rot90 ? document.pagePointSize(row).width : document.pagePointSize(row).height) * root.renderScale } + property size firstPagePointSize: root.document?.status === PdfDocument.Ready ? root.document.pagePointSize(0) : Qt.size(1, 1) + property real pageHolderWidth: Math.max(root.width, ((rot90 ? root.document?.maxPageHeight : root.document?.maxPageWidth) ?? 0) * root.renderScale) + columnWidthProvider: function(col) { return root.document ? pageHolderWidth + vscroll.width + 2 : 0 } + rowHeightProvider: function(row) { return (rot90 ? root.document.pagePointSize(row).width : root.document.pagePointSize(row).height) * root.renderScale } + + // delayed-jump feature in case the user called goToPage() or goToLocation() too early + property int pendingRow: -1 + property point pendingLocation + property real pendingZoom: -1 + onRowsChanged: { + if (rows > 0 && tableView.pendingRow >= 0) { + console.log(lcMPV, "initiating delayed jump to page", tableView.pendingRow, "loc", tableView.pendingLocation, "zoom", tableView.pendingZoom) + root.goToLocation(tableView.pendingRow, tableView.pendingLocation, tableView.pendingZoom) + tableView.pendingRow = -1 + tableView.pendingLocation = Qt.point(-1, -1) + tableView.pendingZoom = -1 + } + } + delegate: Rectangle { id: pageHolder + required property int index color: tableView.debug ? "beige" : "transparent" Text { visible: tableView.debug @@ -332,12 +351,12 @@ Item { height: image.height rotation: root.pageRotation anchors.centerIn: pinch.active ? undefined : parent - property size pagePointSize: document.pagePointSize(index) + property size pagePointSize: root.document.pagePointSize(pageHolder.index) property real pageScale: image.paintedWidth / pagePointSize.width PdfPageImage { id: image document: root.document - currentFrame: index + currentFrame: pageHolder.index asynchronous: true fillMode: Image.PreserveAspectFit width: paper.pagePointSize.width * root.renderScale @@ -351,7 +370,7 @@ Item { searchHighlights.update() } onStatusChanged: { - if (index === pageNavigator.currentPage) + if (pageHolder.index === pageNavigator.currentPage) root.currentPageRenderingStatus = status } } @@ -367,7 +386,7 @@ Item { id: searchHighlights function update() { // paths could be a binding, but we need to be able to "kick" it sometimes - paths = searchModel.boundingPolygonsOnPage(index) + paths = searchModel.boundingPolygonsOnPage(pageHolder.index) } } } @@ -388,7 +407,7 @@ Item { } Shape { anchors.fill: parent - visible: image.status === Image.Ready && searchModel.currentPage === index + visible: image.status === Image.Ready && searchModel.currentPage === pageHolder.index ShapePath { strokeWidth: style.currentSearchResultStrokeWidth strokeColor: style.currentSearchResultStrokeColor @@ -415,12 +434,16 @@ Item { const centroidInFlickable = tableView.mapFromItem(paper, pinch.centroid.position.x, pinch.centroid.position.y) const newSourceWidth = image.sourceSize.width * paper.scale const ratio = newSourceWidth / image.sourceSize.width - console.log(lcMPV, "pinch ended on page", index, "with centroid", pinch.centroid.position, centroidInPoints, "wrt flickable", centroidInFlickable, + console.log(lcMPV, "pinch ended on page", pageHolder.index, + "with scale", paper.scale.toFixed(3), "ratio", ratio.toFixed(3), + "centroid", pinch.centroid.position, centroidInPoints, + "wrt flickable", centroidInFlickable, "page at", pageHolder.x.toFixed(2), pageHolder.y.toFixed(2), "contentX/Y were", tableView.contentX.toFixed(2), tableView.contentY.toFixed(2)) if (ratio > 1.1 || ratio < 0.9) { const centroidOnPage = Qt.point(centroidInPoints.x * root.renderScale * ratio, centroidInPoints.y * root.renderScale * ratio) paper.scale = 1 + pinch.persistentScale = 1 paper.x = 0 paper.y = 0 root.renderScale *= ratio @@ -528,8 +551,8 @@ Item { // and don't force layout either, because positionViewAtCell() will do that if (pageNavigator.jumping) return - // make TableView rebuild from scratch, because otherwise it doesn't know the delegates are changing size - tableView.rebuild() + // page size changed: TableView needs to redo layout to avoid overlapping delegates or gaps between them + tableView.forceLayout() const cell = tableView.cellAtPos(root.width / 2, root.height / 2) const currentItem = tableView.itemAtCell(cell) if (currentItem) { |