diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2020-02-10 10:49:33 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2020-02-17 17:50:01 +0100 |
commit | 0b6a4d94945a975390b2574e6aff2568ebb7f061 (patch) | |
tree | 480d54e4146ee0d23dd3f2ce69877408162bb512 /examples | |
parent | e5a33355798d3277c631b0024f389cdca2f2c683 (diff) |
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 <shawn.rutledge@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/pdf/multipage/viewer.qml | 96 | ||||
-rw-r--r-- | examples/pdf/pdfviewer/resources/go-down-search.svg | 13 | ||||
-rw-r--r-- | examples/pdf/pdfviewer/resources/go-up-search.svg | 8 | ||||
-rw-r--r-- | examples/pdf/pdfviewer/viewer.qml | 119 | ||||
-rw-r--r-- | examples/pdf/pdfviewer/viewer.qrc | 2 |
5 files changed, 172 insertions, 66 deletions
diff --git a/examples/pdf/multipage/viewer.qml b/examples/pdf/multipage/viewer.qml index 3d9cd371a..8f102a3c1 100644 --- a/examples/pdf/multipage/viewer.qml +++ b/examples/pdf/multipage/viewer.qml @@ -230,6 +230,7 @@ ApplicationWindow { PdfMultiPageView { id: view anchors.fill: parent + anchors.leftMargin: searchDrawer.position * searchDrawer.width document: root.document searchString: searchField.text onCurrentPageChanged: currentPageSB.value = view.currentPage + 1 @@ -237,10 +238,11 @@ ApplicationWindow { Drawer { id: searchDrawer - edge: Qt.BottomEdge - x: 20 + edge: Qt.LeftEdge + modal: false width: searchLayout.implicitWidth - height: searchLayout.implicitHeight + y: root.header.height + height: view.height dim: false Shortcut { sequence: StandardKey.Find @@ -249,45 +251,69 @@ ApplicationWindow { searchField.forceActiveFocus() } } - RowLayout { + ColumnLayout { id: searchLayout - ToolButton { - action: Action { - icon.source: "resources/go-up-search.svg" - onTriggered: view.searchBack() + anchors.fill: parent + anchors.margins: 2 + RowLayout { + ToolButton { + action: Action { + icon.source: "resources/go-up-search.svg" + shortcut: StandardKey.FindPrevious + onTriggered: view.searchBack() + } + ToolTip.visible: enabled && hovered + ToolTip.delay: 2000 + ToolTip.text: "find previous" } - 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 + 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() + } } - TapHandler { - onTapped: searchField.clear() + } + ToolButton { + action: Action { + icon.source: "resources/go-down-search.svg" + shortcut: StandardKey.FindNext + onTriggered: view.searchForward() } + ToolTip.visible: enabled && hovered + ToolTip.delay: 2000 + ToolTip.text: "find next" } } - ToolButton { - action: Action { - icon.source: "resources/go-down-search.svg" - onTriggered: view.searchForward() + ListView { + id: searchResultsList + ColumnLayout.fillWidth: true + ColumnLayout.fillHeight: true + clip: true + model: view.searchModel + ScrollBar.vertical: ScrollBar { } + delegate: ItemDelegate { + width: parent ? parent.width : 0 + text: "page " + (page + 1) + ": " + context + highlighted: ListView.isCurrentItem + onClicked: { + searchResultsList.currentIndex = index + view.goToLocation(page, location, 0) + view.searchModel.currentResult = indexOnPage + } } - ToolTip.visible: enabled && hovered - ToolTip.delay: 2000 - ToolTip.text: "find next" } } } 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 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> + <defs id="defs3051"> + <style type="text/css" id="current-color-scheme"> + .ColorScheme-Text { + color:#232629; + } + </style> + </defs> + <path style="fill:currentColor;fill-opacity:1;stroke:none" + d="M 4.7070312 8 L 4 8.7070312 L 10.125 14.832031 L 12 16.707031 L 13.875 14.832031 L 20 8.7070312 L 19.292969 8 L 13.167969 14.125 L 12 15.292969 L 10.832031 14.125 L 4.7070312 8 z " + class="ColorScheme-Text" + /> +</svg> 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 @@ +<svg height="24" width="24" xmlns="http://www.w3.org/2000/svg"> + <style type="text/css" id="current-color-scheme"> + .ColorScheme-Text { + color:#232629; + } + </style> + <path d="M4.707 16L4 15.293l8-8 8 8-.707.707L12 8.707" class="ColorScheme-Text" fill="currentColor"/> +</svg> 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 @@ <file>viewer.qml</file> <file>resources/edit-clear.svg</file> <file>resources/edit-copy.svg</file> + <file>resources/go-down-search.svg</file> <file>resources/go-next-view-page.svg</file> <file>resources/go-previous-view-page.svg</file> + <file>resources/go-up-search.svg</file> <file>resources/rotate-left.svg</file> <file>resources/rotate-right.svg</file> <file>resources/zoom-in.svg</file> |