PdfPageView.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. pragma ComponentBehavior: Bound
  4. import QtQuick
  5. import QtQuick.Pdf
  6. import QtQuick.Shapes
  7. /*!
  8. \qmltype PdfPageView
  9. \inqmlmodule QtQuick.Pdf
  10. \brief A PDF viewer component to show one page a time.
  11. PdfPageView provides a PDF viewer component that shows one whole page at a
  12. time, without scrolling. It supports selecting text and copying it to the
  13. clipboard, zooming in and out, clicking an internal link to jump to another
  14. section in the document, rotating the view, and searching for text.
  15. The implementation is a QML assembly of smaller building blocks that are
  16. available separately. In case you want to make changes in your own version
  17. of this component, you can copy the QML, which is installed into the
  18. \c QtQuick/Pdf/qml module directory, and modify it as needed.
  19. \sa PdfScrollablePageView, PdfMultiPageView, PdfStyle
  20. */
  21. Rectangle {
  22. /*!
  23. \qmlproperty PdfDocument PdfPageView::document
  24. A PdfDocument object with a valid \c source URL is required:
  25. \snippet pdfpageview.qml 0
  26. */
  27. required property PdfDocument document
  28. /*!
  29. \qmlproperty int PdfPageView::status
  30. This property holds the \l {QtQuick::Image::status}{rendering status} of
  31. the \l {currentPage}{current page}.
  32. */
  33. property alias status: image.status
  34. /*!
  35. \qmlproperty PdfDocument PdfPageView::selectedText
  36. The selected text.
  37. */
  38. property alias selectedText: selection.text
  39. /*!
  40. \qmlmethod void PdfPageView::selectAll()
  41. Selects all the text on the \l {currentPage}{current page}, and makes it
  42. available as the system \l {QClipboard::Selection}{selection} on systems
  43. that support that feature.
  44. \sa copySelectionToClipboard()
  45. */
  46. function selectAll() {
  47. selection.selectAll()
  48. }
  49. /*!
  50. \qmlmethod void PdfPageView::copySelectionToClipboard()
  51. Copies the selected text (if any) to the
  52. \l {QClipboard::Clipboard}{system clipboard}.
  53. \sa selectAll()
  54. */
  55. function copySelectionToClipboard() {
  56. selection.copyToClipboard()
  57. }
  58. // --------------------------------
  59. // page navigation
  60. /*!
  61. \qmlproperty int PdfPageView::currentPage
  62. \readonly
  63. This property holds the zero-based page number of the page visible in the
  64. scrollable view. If there is no current page, it holds -1.
  65. This property is read-only, and is typically used in a binding (or
  66. \c onCurrentPageChanged script) to update the part of the user interface
  67. that shows the current page number, such as a \l SpinBox.
  68. \sa PdfPageNavigator::currentPage
  69. */
  70. property alias currentPage: pageNavigator.currentPage
  71. /*!
  72. \qmlproperty bool PdfPageView::backEnabled
  73. \readonly
  74. This property indicates if it is possible to go back in the navigation
  75. history to a previous-viewed page.
  76. \sa PdfPageNavigator::backAvailable, back()
  77. */
  78. property alias backEnabled: pageNavigator.backAvailable
  79. /*!
  80. \qmlproperty bool PdfPageView::forwardEnabled
  81. \readonly
  82. This property indicates if it is possible to go to next location in the
  83. navigation history.
  84. \sa PdfPageNavigator::forwardAvailable, forward()
  85. */
  86. property alias forwardEnabled: pageNavigator.forwardAvailable
  87. /*!
  88. \qmlmethod void PdfPageView::back()
  89. Scrolls the view back to the previous page that the user visited most
  90. recently; or does nothing if there is no previous location on the
  91. navigation stack.
  92. \sa PdfPageNavigator::back(), currentPage, backEnabled
  93. */
  94. function back() { pageNavigator.back() }
  95. /*!
  96. \qmlmethod void PdfPageView::forward()
  97. Scrolls the view to the page that the user was viewing when the back()
  98. method was called; or does nothing if there is no "next" location on the
  99. navigation stack.
  100. \sa PdfPageNavigator::forward(), currentPage
  101. */
  102. function forward() { pageNavigator.forward() }
  103. /*!
  104. \qmlmethod void PdfPageView::goToPage(int page)
  105. Changes the view to the \a page, if possible.
  106. \sa PdfPageNavigator::jump(), currentPage
  107. */
  108. function goToPage(page) { goToLocation(page, Qt.point(0, 0), 0) }
  109. /*!
  110. \qmlmethod void PdfPageView::goToLocation(int page, point location, real zoom)
  111. Scrolls the view to the \a location on the \a page, if possible,
  112. and sets the \a zoom level.
  113. \sa PdfPageNavigator::jump(), currentPage
  114. */
  115. function goToLocation(page, location, zoom) {
  116. if (zoom > 0)
  117. root.renderScale = zoom
  118. pageNavigator.jump(page, location, zoom)
  119. }
  120. // --------------------------------
  121. // page scaling
  122. /*!
  123. \qmlproperty bool PdfPageView::zoomEnabled
  124. This property holds whether the user can use the pinch gesture or
  125. Control + mouse wheel to zoom. The default is \c true.
  126. When the user zooms the page, the size of PdfPageView changes.
  127. */
  128. property bool zoomEnabled: true
  129. /*!
  130. \qmlproperty real PdfPageView::renderScale
  131. This property holds the ratio of pixels to points. The default is \c 1,
  132. meaning one point (1/72 of an inch) equals 1 logical pixel.
  133. */
  134. property real renderScale: 1
  135. /*!
  136. \qmlproperty size PdfPageView::sourceSize
  137. This property holds the scaled width and height of the full-frame image.
  138. \sa {QtQuick::Image::sourceSize}{Image.sourceSize}
  139. */
  140. property alias sourceSize: image.sourceSize
  141. /*!
  142. \qmlmethod void PdfPageView::resetScale()
  143. Sets \l renderScale back to its default value of \c 1.
  144. */
  145. function resetScale() {
  146. image.sourceSize.width = 0
  147. image.sourceSize.height = 0
  148. root.scale = 1
  149. }
  150. /*!
  151. \qmlmethod void PdfPageView::scaleToWidth(real width, real height)
  152. Sets \l renderScale such that the width of the first page will fit into a
  153. viewport with the given \a width and \a height. If the page is not rotated,
  154. it will be scaled so that its width fits \a width. If it is rotated +/- 90
  155. degrees, it will be scaled so that its width fits \a height.
  156. */
  157. function scaleToWidth(width, height) {
  158. const halfRotation = Math.abs(root.rotation % 180)
  159. image.sourceSize = Qt.size((halfRotation > 45 && halfRotation < 135) ? height : width, 0)
  160. image.centerInSize = Qt.size(width, height)
  161. image.centerOnLoad = true
  162. image.vCenterOnLoad = (halfRotation > 45 && halfRotation < 135)
  163. root.scale = 1
  164. }
  165. /*!
  166. \qmlmethod void PdfPageView::scaleToPage(real width, real height)
  167. Sets \l renderScale such that the whole first page will fit into a viewport
  168. with the given \a width and \a height. The resulting \l renderScale depends
  169. on page rotation: the page will fit into the viewport at a larger size if it
  170. is first rotated to have a matching aspect ratio.
  171. */
  172. function scaleToPage(width, height) {
  173. const windowAspect = width / height
  174. const halfRotation = Math.abs(root.rotation % 180)
  175. const pagePointSize = document.pagePointSize(pageNavigator.currentPage)
  176. const pageAspect = pagePointSize.height / pagePointSize.width
  177. if (halfRotation > 45 && halfRotation < 135) {
  178. // rotated 90 or 270º
  179. if (windowAspect > pageAspect) {
  180. image.sourceSize = Qt.size(height, 0)
  181. } else {
  182. image.sourceSize = Qt.size(0, width)
  183. }
  184. } else {
  185. if (windowAspect > pageAspect) {
  186. image.sourceSize = Qt.size(0, height)
  187. } else {
  188. image.sourceSize = Qt.size(width, 0)
  189. }
  190. }
  191. image.centerInSize = Qt.size(width, height)
  192. image.centerOnLoad = true
  193. image.vCenterOnLoad = true
  194. root.scale = 1
  195. }
  196. // --------------------------------
  197. // text search
  198. /*!
  199. \qmlproperty PdfSearchModel PdfPageView::searchModel
  200. This property holds a PdfSearchModel containing the list of search results
  201. for a given \l searchString.
  202. \sa PdfSearchModel
  203. */
  204. property alias searchModel: searchModel
  205. /*!
  206. \qmlproperty string PdfPageView::searchString
  207. This property holds the search string that the user may choose to search
  208. for. It is typically used in a binding to the \c text property of a
  209. TextField.
  210. \sa searchModel
  211. */
  212. property alias searchString: searchModel.searchString
  213. /*!
  214. \qmlmethod void PdfPageView::searchBack()
  215. Decrements the
  216. \l{PdfSearchModel::currentResult}{searchModel's current result}
  217. so that the view will jump to the previous search result.
  218. */
  219. function searchBack() { --searchModel.currentResult }
  220. /*!
  221. \qmlmethod void PdfPageView::searchForward()
  222. Increments the
  223. \l{PdfSearchModel::currentResult}{searchModel's current result}
  224. so that the view will jump to the next search result.
  225. */
  226. function searchForward() { ++searchModel.currentResult }
  227. // --------------------------------
  228. // implementation
  229. id: root
  230. width: image.width
  231. height: image.height
  232. PdfSelection {
  233. id: selection
  234. document: root.document
  235. page: pageNavigator.currentPage
  236. from: Qt.point(textSelectionDrag.centroid.pressPosition.x / image.pageScale, textSelectionDrag.centroid.pressPosition.y / image.pageScale)
  237. to: Qt.point(textSelectionDrag.centroid.position.x / image.pageScale, textSelectionDrag.centroid.position.y / image.pageScale)
  238. hold: !textSelectionDrag.active && !tapHandler.pressed
  239. }
  240. PdfSearchModel {
  241. id: searchModel
  242. document: root.document === undefined ? null : root.document
  243. onCurrentPageChanged: root.goToPage(currentPage)
  244. }
  245. PdfPageNavigator {
  246. id: pageNavigator
  247. onCurrentPageChanged: searchModel.currentPage = currentPage
  248. onCurrentZoomChanged: root.renderScale = currentZoom
  249. property url documentSource: root.document.source
  250. onDocumentSourceChanged: {
  251. pageNavigator.clear()
  252. root.goToPage(0)
  253. }
  254. }
  255. PdfPageImage {
  256. id: image
  257. document: root.document
  258. currentFrame: pageNavigator.currentPage
  259. asynchronous: true
  260. fillMode: Image.PreserveAspectFit
  261. property bool centerOnLoad: false
  262. property bool vCenterOnLoad: false
  263. property size centerInSize
  264. property real pageScale: image.paintedWidth / document.pagePointSize(pageNavigator.currentPage).width
  265. function reRenderIfNecessary() {
  266. const newSourceWidth = image.sourceSize.width * root.scale * Screen.devicePixelRatio
  267. const ratio = newSourceWidth / image.sourceSize.width
  268. if (ratio > 1.1 || ratio < 0.9) {
  269. image.sourceSize.width = newSourceWidth
  270. image.sourceSize.height = 0
  271. root.scale = 1
  272. }
  273. }
  274. onStatusChanged:
  275. if (status == Image.Ready && centerOnLoad) {
  276. root.x = (centerInSize.width - image.implicitWidth) / 2
  277. root.y = vCenterOnLoad ? (centerInSize.height - image.implicitHeight) / 2 : 0
  278. centerOnLoad = false
  279. vCenterOnLoad = false
  280. }
  281. }
  282. onRenderScaleChanged: {
  283. image.sourceSize.width = document.pagePointSize(pageNavigator.currentPage).width * renderScale
  284. image.sourceSize.height = 0
  285. root.scale = 1
  286. }
  287. Shape {
  288. anchors.fill: parent
  289. opacity: 0.25
  290. visible: image.status === Image.Ready
  291. ShapePath {
  292. strokeWidth: 1
  293. strokeColor: "cyan"
  294. fillColor: "steelblue"
  295. scale: Qt.size(image.pageScale, image.pageScale)
  296. PathMultiline {
  297. paths: searchModel.currentPageBoundingPolygons
  298. }
  299. }
  300. ShapePath {
  301. strokeWidth: 1
  302. strokeColor: "orange"
  303. fillColor: "cyan"
  304. scale: Qt.size(image.pageScale, image.pageScale)
  305. PathMultiline {
  306. paths: searchModel.currentResultBoundingPolygons
  307. }
  308. }
  309. ShapePath {
  310. fillColor: "orange"
  311. scale: Qt.size(image.pageScale, image.pageScale)
  312. PathMultiline {
  313. paths: selection.geometry
  314. }
  315. }
  316. }
  317. Repeater {
  318. model: PdfLinkModel {
  319. id: linkModel
  320. document: root.document
  321. page: pageNavigator.currentPage
  322. }
  323. delegate: PdfLinkDelegate {
  324. x: rectangle.x * image.pageScale
  325. y: rectangle.y * image.pageScale
  326. width: rectangle.width * image.pageScale
  327. height: rectangle.height * image.pageScale
  328. visible: image.status === Image.Ready
  329. onTapped:
  330. (link) => {
  331. if (link.page >= 0)
  332. pageNavigator.jump(link)
  333. else
  334. Qt.openUrlExternally(url)
  335. }
  336. }
  337. }
  338. PinchHandler {
  339. id: pinch
  340. enabled: root.zoomEnabled && root.scale * root.renderScale <= 10 && root.scale * root.renderScale >= 0.1
  341. minimumScale: 0.1
  342. maximumScale: 10
  343. minimumRotation: 0
  344. maximumRotation: 0
  345. onActiveChanged: if (!active) image.reRenderIfNecessary()
  346. grabPermissions: PinchHandler.TakeOverForbidden // don't allow takeover if pinch has started
  347. }
  348. WheelHandler {
  349. enabled: pinch.enabled
  350. acceptedModifiers: Qt.ControlModifier
  351. property: "scale"
  352. onActiveChanged: if (!active) image.reRenderIfNecessary()
  353. }
  354. DragHandler {
  355. id: textSelectionDrag
  356. acceptedDevices: PointerDevice.Mouse | PointerDevice.Stylus
  357. target: null
  358. }
  359. TapHandler {
  360. id: tapHandler
  361. acceptedDevices: PointerDevice.Mouse | PointerDevice.Stylus
  362. }
  363. }