Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickpdfpageimage.cpp
Go to the documentation of this file.
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
6#include <private/qpdffile_p.h>
7#include <private/qtpdfglobal_p.h>
8#include <QtQuick/private/qquickimage_p_p.h>
9
11
12Q_PDF_LOGGING_CATEGORY(qLcImg, "qt.pdf.image")
13
14/*!
15 \qmltype PdfPageImage
16//! \nativetype QQuickPdfPageImage
17 \inqmlmodule QtQuick.Pdf
18 \ingroup pdf
19 \inherits Image
20 \brief Displays one page from a PDF document.
21 \since 6.4
22
23 The PdfPageImage type is an Image specialized to render a page from a PDF document.
24*/
25
26class QQuickPdfPageImagePrivate: public QQuickImagePrivate
27{
28public:
29 QQuickPdfPageImagePrivate() : QQuickImagePrivate() {}
30
31 QQuickPdfDocument *doc = nullptr;
32};
33
34QQuickPdfPageImage::QQuickPdfPageImage(QQuickItem *parent)
35 : QQuickImage(*(new QQuickPdfPageImagePrivate), parent)
36{
37}
38
39/*!
40 \internal
41*/
42QQuickPdfPageImage::~QQuickPdfPageImage()
43{
44 Q_D(QQuickPdfPageImage);
45 // cancel any async rendering job that is running on my behalf
46 d->pendingPix->clear();
47}
48
49/*!
50 \qmlproperty PdfDocument PdfPageImage::document
51
52 This property holds the PDF document from which to render an image.
53*/
54void QQuickPdfPageImage::setDocument(QQuickPdfDocument *document)
55{
56 Q_D(QQuickPdfPageImage);
57 if (d->doc == document)
58 return;
59
60 if (d->doc)
61 disconnect(d->doc->document(), &QPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
62 d->doc = document;
63 if (document) {
64 connect(document->document(), &QPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
65 if (document->document()->status() == QPdfDocument::Status::Ready)
66 setSource(document->resolvedSource()); // calls load()
67 }
68 emit documentChanged();
69}
70
71QQuickPdfDocument *QQuickPdfPageImage::document() const
72{
73 Q_D(const QQuickPdfPageImage);
74 return d->doc;
75}
76
77void QQuickPdfPageImage::load()
78{
79 Q_D(QQuickPdfPageImage);
80 QUrl url = source();
81 if (!d->doc || !d->doc->carrierFile()) {
82 if (!url.isEmpty()) {
83 qmlWarning(this) << "document property not set: falling back to inefficient loading of " << url;
84 QQuickImageBase::load();
85 }
86 return;
87 }
88 if (url != d->doc->resolvedSource()) {
89 url = d->doc->resolvedSource();
90 qmlWarning(this) << "document and source properties in conflict: preferring document source " << url;
91 }
92 auto carrierFile = d->doc->carrierFile();
93 static int thisRequestProgress = -1;
94 static int thisRequestFinished = -1;
95 if (thisRequestProgress == -1) {
96 thisRequestProgress =
97 QQuickImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
98 thisRequestFinished =
99 QQuickImageBase::staticMetaObject.indexOfSlot("requestFinished()");
100 }
101 static QMetaMethod requestFinishedSlot = staticMetaObject.method(thisRequestFinished);
102
103 d->pendingPix->loadImageFromDevice(qmlEngine(this), carrierFile, url,
104 d->sourceClipRect.toRect(), d->sourcesize * d->devicePixelRatio,
105 QQuickImageProviderOptions(), d->currentFrame, d->frameCount);
106
107 qCDebug(qLcImg) << "loading page" << d->currentFrame << "of" << d->frameCount
108 << "from" << carrierFile->fileName() << "status" << d->pendingPix->status();
109
110 switch (d->pendingPix->status()) {
111 case QQuickPixmap::Ready:
112 requestFinishedSlot.invoke(this);
113 pixmapChange();
114 break;
115 case QQuickPixmap::Loading:
116 d->pendingPix->connectFinished(this, thisRequestFinished);
117 d->pendingPix->connectDownloadProgress(this, thisRequestProgress);
118 if (d->progress != 0.0) {
119 d->progress = 0.0;
120 emit progressChanged(d->progress);
121 }
122 if (d->status != Loading) {
123 d->status = Loading;
124 emit statusChanged(d->status);
125 }
126 break;
127 default:
128 qCDebug(qLcImg) << "unexpected status" << d->pendingPix->status();
129 break;
130 }
131}
132
133void QQuickPdfPageImage::documentStatusChanged()
134{
135 Q_D(QQuickPdfPageImage);
136 const auto status = d->doc->document()->status();
137 qCDebug(qLcImg) << "document status" << status;
138 if (status == QPdfDocument::Status::Ready)
139 setSource(d->doc->resolvedSource()); // calls load()
140}
141
142QT_END_NAMESPACE
143
144#include "moc_qquickpdfpageimage_p.cpp"