From 0b6a4d94945a975390b2574e6aff2568ebb7f061 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 10 Feb 2020 10:49:33 +0100 Subject: PdfSearchModel: be QALM and find search results on all pages It's a QAbstractListModel, so now PdfMultiPageView has a ListView in a left-side Drawer showing all results found so far. - In PdfMultiPageView, multiple pages exist at once, so it makes sense to use the same searchmodel for all. - It's faster and saves memory. - Search results on each page can be cached. - It's possible to show search results in a ListView or QListView. Change-Id: I66fba6975954a09a4d23262be87ff8cc25ee7478 Reviewed-by: Shawn Rutledge --- .../pdf/pdfviewer/resources/go-down-search.svg | 13 +++ examples/pdf/pdfviewer/resources/go-up-search.svg | 8 ++ examples/pdf/pdfviewer/viewer.qml | 119 +++++++++++++++------ examples/pdf/pdfviewer/viewer.qrc | 2 + 4 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 examples/pdf/pdfviewer/resources/go-down-search.svg create mode 100644 examples/pdf/pdfviewer/resources/go-up-search.svg (limited to 'examples/pdf/pdfviewer') diff --git a/examples/pdf/pdfviewer/resources/go-down-search.svg b/examples/pdf/pdfviewer/resources/go-down-search.svg new file mode 100644 index 000000000..ae17ab93b --- /dev/null +++ b/examples/pdf/pdfviewer/resources/go-down-search.svg @@ -0,0 +1,13 @@ + + + + + + diff --git a/examples/pdf/pdfviewer/resources/go-up-search.svg b/examples/pdf/pdfviewer/resources/go-up-search.svg new file mode 100644 index 000000000..5cc155873 --- /dev/null +++ b/examples/pdf/pdfviewer/resources/go-up-search.svg @@ -0,0 +1,8 @@ + + + + diff --git a/examples/pdf/pdfviewer/viewer.qml b/examples/pdf/pdfviewer/viewer.qml index 095bc3985..e94642c5e 100644 --- a/examples/pdf/pdfviewer/viewer.qml +++ b/examples/pdf/pdfviewer/viewer.qml @@ -57,7 +57,7 @@ import Qt.labs.platform 1.1 as Platform ApplicationWindow { id: root - width: 1280 + width: 800 height: 1024 color: "lightgrey" title: document.title @@ -140,14 +140,15 @@ ApplicationWindow { from: 1 to: document.pageCount editable: true - onValueChanged: pageView.goToPage(value - 1) + value: pageView.currentPage + 1 + onValueModified: pageView.goToPage(value - 1) Shortcut { sequence: StandardKey.MoveToPreviousPage - onActivated: currentPageSB.value-- + onActivated: pageView.goToPage(currentPageSB.value - 2) } Shortcut { sequence: StandardKey.MoveToNextPage - onActivated: currentPageSB.value++ + onActivated: pageView.goToPage(currentPageSB.value) } } ToolButton { @@ -168,30 +169,6 @@ ApplicationWindow { onTriggered: pageView.copySelectionToClipboard() } } - TextField { - id: searchField - placeholderText: "search" - Layout.minimumWidth: 200 - Layout.fillWidth: true - Image { - visible: searchField.text !== "" - source: "resources/edit-clear.svg" - anchors { - right: parent.right - top: parent.top - bottom: parent.bottom - margins: 3 - rightMargin: 5 - } - TapHandler { - onTapped: searchField.clear() - } - } - } - Shortcut { - sequence: StandardKey.Find - onActivated: searchField.forceActiveFocus() - } Shortcut { sequence: StandardKey.Quit onActivated: Qt.quit() @@ -223,9 +200,7 @@ ApplicationWindow { PdfPageView { id: pageView - // TODO should work but ends up being NaN in QQuickSpinBoxPrivate::setValue() (?!) -// onCurrentPageChanged: currentPageSB.value = pageView.currrentPage + 1 - onCurrentPageReallyChanged: currentPageSB.value = page + 1 + x: searchDrawer.position * searchDrawer.width // TODO binding gets broken during centering document: PdfDocument { id: document onStatusChanged: if (status === PdfDocument.Error) errorDialog.open() @@ -233,6 +208,88 @@ ApplicationWindow { searchString: searchField.text } + Drawer { + id: searchDrawer + edge: Qt.LeftEdge + modal: false + width: searchLayout.implicitWidth + y: root.header.height + height: root.contentItem.height + dim: false + Shortcut { + sequence: StandardKey.Find + onActivated: { + searchDrawer.open() + searchField.forceActiveFocus() + } + } + ColumnLayout { + id: searchLayout + anchors.fill: parent + anchors.margins: 2 + RowLayout { + ToolButton { + action: Action { + icon.source: "resources/go-up-search.svg" + shortcut: StandardKey.FindPrevious + onTriggered: pageView.searchBack() + } + ToolTip.visible: enabled && hovered + ToolTip.delay: 2000 + ToolTip.text: "find previous" + } + TextField { + id: searchField + placeholderText: "search" + Layout.minimumWidth: 200 + Layout.fillWidth: true + Image { + visible: searchField.text !== "" + source: "resources/edit-clear.svg" + anchors { + right: parent.right + top: parent.top + bottom: parent.bottom + margins: 3 + rightMargin: 5 + } + TapHandler { + onTapped: searchField.clear() + } + } + } + ToolButton { + action: Action { + icon.source: "resources/go-down-search.svg" + shortcut: StandardKey.FindNext + onTriggered: pageView.searchForward() + } + ToolTip.visible: enabled && hovered + ToolTip.delay: 2000 + ToolTip.text: "find next" + } + } + ListView { + id: searchResultsList + ColumnLayout.fillWidth: true + ColumnLayout.fillHeight: true + clip: true + model: pageView.searchModel + ScrollBar.vertical: ScrollBar { } + delegate: ItemDelegate { + width: parent ? parent.width : 0 + text: "page " + (page + 1) + ": " + context + highlighted: ListView.isCurrentItem + onClicked: { + searchResultsList.currentIndex = index + pageView.goToLocation(page, location, 0) + pageView.searchModel.currentResult = indexOnPage + } + } + } + } + } + footer: Label { property size implicitPointSize: document.pagePointSize(pageView.currentPage) text: "page " + (pageView.currentPage + 1) + " of " + pageView.pageCount + diff --git a/examples/pdf/pdfviewer/viewer.qrc b/examples/pdf/pdfviewer/viewer.qrc index fa3561caf..9698a2689 100644 --- a/examples/pdf/pdfviewer/viewer.qrc +++ b/examples/pdf/pdfviewer/viewer.qrc @@ -3,8 +3,10 @@ viewer.qml resources/edit-clear.svg resources/edit-copy.svg + resources/go-down-search.svg resources/go-next-view-page.svg resources/go-previous-view-page.svg + resources/go-up-search.svg resources/rotate-left.svg resources/rotate-right.svg resources/zoom-in.svg -- cgit v1.2.3