1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtPDF module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquickpdfdocument_p.h"
#include <private/qpdffile_p.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qstandardpaths.h>
#include <QtQml/qqmlcontext.h>
#include <QtQml/qqmlengine.h>
#include <QtQuick/qquickitem.h>
QT_BEGIN_NAMESPACE
/*!
\qmltype PdfDocument
//! \instantiates QQuickPdfDocument
\inqmlmodule QtQuick.Pdf
\ingroup pdf
\brief A representation of a PDF document.
\since 5.15
PdfDocument provides access to PDF document meta-information.
It is not necessary for rendering, as it is enough to use an
\l Image with source set to the URL of the PDF.
*/
/*
Constructs a PDF document.
*/
QQuickPdfDocument::QQuickPdfDocument(QObject *parent)
: QObject(parent)
{
}
/*!
\internal
*/
QQuickPdfDocument::~QQuickPdfDocument() = default;
void QQuickPdfDocument::classBegin()
{
m_doc = static_cast<QPdfDocument *>(qmlExtendedObject(this));
Q_ASSERT(m_doc);
connect(m_doc, &QPdfDocument::passwordChanged, this, [this]() {
if (resolvedSource().isValid() && resolvedSource().isLocalFile())
m_doc->load(resolvedSource().path());
});
connect(m_doc, &QPdfDocument::statusChanged, this, [this] (QPdfDocument::Status status) {
emit errorChanged();
if (status == QPdfDocument::Ready)
emit metaDataChanged();
});
if (m_doc->error() == QPdfDocument::IncorrectPasswordError)
emit m_doc->passwordRequired();
}
/*!
\qmlproperty url PdfDocument::source
This property holds a URL pointing to the PDF file to be loaded.
\note At this time, only local filesystem URLs are supported.
*/
void QQuickPdfDocument::setSource(QUrl source)
{
if (m_source == source)
return;
m_source = source;
m_maxPageWidthHeight = QSizeF();
m_carrierFile->deleteLater();
m_carrierFile = nullptr;
emit sourceChanged();
const QQmlContext *context = qmlContext(this);
m_resolvedSource = context ? context->resolvedUrl(source) : source;
if (source.scheme() == QLatin1String("qrc"))
m_doc->load(QLatin1Char(':') + m_resolvedSource.path());
else
m_doc->load(m_resolvedSource.toLocalFile());
}
/*!
\qmlproperty string PdfDocument::error
This property holds a translated string representation of the current
error, if any.
\sa status
*/
QString QQuickPdfDocument::error() const
{
switch (m_doc->error()) {
case QPdfDocument::NoError:
return tr("no error");
break;
case QPdfDocument::UnknownError:
break;
case QPdfDocument::DataNotYetAvailableError:
return tr("data not yet available");
break;
case QPdfDocument::FileNotFoundError:
return tr("file not found");
break;
case QPdfDocument::InvalidFileFormatError:
return tr("invalid file format");
break;
case QPdfDocument::IncorrectPasswordError:
return tr("incorrect password");
break;
case QPdfDocument::UnsupportedSecuritySchemeError:
return tr("unsupported security scheme");
break;
}
return tr("unknown error");
}
/*!
\qmlproperty string PdfDocument::password
This property holds the document password. If the passwordRequired()
signal is emitted, the UI should prompt the user and then set this
property so that document opening can continue.
*/
/*!
\qmlproperty int PdfDocument::pageCount
This property holds the number of pages the PDF contains.
*/
/*!
\qmlsignal PdfDocument::passwordRequired()
This signal is emitted when the PDF requires a password in order to open.
The UI in a typical PDF viewer should prompt the user for the password
and then set the password property when the user has provided it.
*/
/*!
\qmlmethod size PdfDocument::pagePointSize(int page)
Returns the size of the given \a page in points.
*/
QSizeF QQuickPdfDocument::pagePointSize(int page) const
{
return m_doc->pageSize(page);
}
qreal QQuickPdfDocument::maxPageWidth() const
{
updateMaxPageSize();
return m_maxPageWidthHeight.width();
}
qreal QQuickPdfDocument::maxPageHeight() const
{
updateMaxPageSize();
return m_maxPageWidthHeight.height();
}
QPdfDocument *QQuickPdfDocument::document() const
{
return m_doc;
}
/*!
\internal
Returns a QPdfFile instance that can carry this document down into
QPdfIOHandler::load(QIODevice *). It should not be used for other purposes.
*/
QPdfFile *QQuickPdfDocument::carrierFile()
{
if (!m_carrierFile)
m_carrierFile = new QPdfFile(m_doc);
return m_carrierFile;
}
void QQuickPdfDocument::updateMaxPageSize() const
{
if (m_maxPageWidthHeight.isValid())
return;
qreal w = 0;
qreal h = 0;
const int count = m_doc->pageCount();
for (int i = 0; i < count; ++i) {
auto size = pagePointSize(i);
w = qMax(w, size.width());
h = qMax(w, size.height());
}
m_maxPageWidthHeight = QSizeF(w, h);
}
/*!
\qmlproperty real PdfDocument::maxPageWidth
This property holds the width of the widest page in the document, in points.
*/
/*!
\qmlproperty real PdfDocument::maxPageHeight
This property holds the height of the tallest page in the document, in points.
*/
/*!
\qmlproperty string PdfDocument::title
This property holds the document's title. A typical viewer UI can bind this
to the \c Window.title property.
*/
/*!
\qmlproperty string PdfDocument::author
This property holds the name of the person who created the document.
*/
/*!
\qmlproperty string PdfDocument::subject
This property holds the subject of the document.
*/
/*!
\qmlproperty string PdfDocument::keywords
This property holds the keywords associated with the document.
*/
/*!
\qmlproperty string PdfDocument::creator
If the document was converted to PDF from another format, this property
holds the name of the software that created the original document.
*/
/*!
\qmlproperty string PdfDocument::producer
If the document was converted to PDF from another format, this property
holds the name of the software that converted it to PDF.
*/
/*!
\qmlproperty date PdfDocument::creationDate
This property holds the date and time the document was created.
*/
/*!
\qmlproperty date PdfDocument::modificationDate
This property holds the date and time the document was most recently
modified.
*/
/*!
\qmlproperty enum PdfDocument::status
This property tells the current status of the document. The possible values are:
\value PdfDocument.Null The initial status after the document has been created or after it has been closed.
\value PdfDocument.Loading The status after load() has been called and before the document is fully loaded.
\value PdfDocument.Ready The status when the document is fully loaded and its data can be accessed.
\value PdfDocument.Unloading The status after close() has been called on an open document.
At this point the document is still valid and all its data can be accessed.
\value PdfDocument.Error The status after Loading, if loading has failed.
*/
QT_END_NAMESPACE
|