Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickpdfdocument.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
5#include <private/qpdffile_p.h>
6#include <QtCore/qmetatype.h>
7#include <QtCore/qstandardpaths.h>
8#include <QtQml/qqmlcontext.h>
9#include <QtQml/qqmlengine.h>
10#include <QtQuick/qquickitem.h>
11#include <QtQml/qqmlfile.h>
12
14
18
28/*
29 Constructs a PDF document.
30*/
35
40{
41 delete m_carrierFile;
42};
43
45{
46 m_doc = static_cast<QPdfDocument *>(qmlExtendedObject(this));
47 Q_ASSERT(m_doc);
48 connect(m_doc, &QPdfDocument::passwordChanged, this, [this]() -> void {
49 if (resolvedSource().isValid())
51 });
52 connect(m_doc, &QPdfDocument::statusChanged, this, [this] (QPdfDocument::Status status) {
54 if (status == QPdfDocument::Status::Ready)
56 });
58 emit m_doc->passwordRequired();
59}
60
69{
70 if (m_source == source)
71 return;
72
73 m_source = source;
74 m_maxPageWidthHeight = QSizeF();
75 if (m_carrierFile)
76 m_carrierFile->deleteLater();
77 m_carrierFile = nullptr;
79 const QQmlContext *context = qmlContext(this);
80 m_resolvedSource = context ? context->resolvedUrl(source) : source;
81 if (m_resolvedSource.isValid())
82 m_doc->load(QQmlFile::urlToLocalFileOrQrc(m_resolvedSource));
83}
84
94{
95 switch (m_doc->error()) {
97 return tr("no error");
98 break;
100 break;
102 return tr("data not yet available");
103 break;
105 return tr("file not found");
106 break;
108 return tr("invalid file format");
109 break;
111 return tr("incorrect password");
112 break;
114 return tr("unsupported security scheme");
115 break;
116 }
117 return tr("unknown error");
118}
119
149{
150 updateMaxPageSize();
151 return m_maxPageWidthHeight.width();
152}
153
155{
156 updateMaxPageSize();
157 return m_maxPageWidthHeight.height();
158}
159
160QPdfDocument *QQuickPdfDocument::document() const
161{
162 return m_doc;
163}
164
170QPdfFile *QQuickPdfDocument::carrierFile()
171{
172 if (!m_carrierFile)
173 m_carrierFile = new QPdfFile(m_doc);
174 return m_carrierFile;
175}
176
177void QQuickPdfDocument::updateMaxPageSize() const
178{
179 if (m_maxPageWidthHeight.isValid())
180 return;
181 qreal w = 0;
182 qreal h = 0;
183 const int count = m_doc->pageCount();
184 for (int i = 0; i < count; ++i) {
185 auto size = m_doc->pagePointSize(i);
186 w = qMax(w, size.width());
187 h = qMax(w, size.height());
188 }
189 m_maxPageWidthHeight = QSizeF(w, h);
190}
191
270
271#include "moc_qquickpdfdocument_p.cpp"
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
The QPdfDocument class loads a PDF document and renders pages from it.
Error error() const
Returns the type of error if \l status is Error, or NoError if there is no error.
void passwordChanged()
Status
This enum describes the current status of the document.
void passwordRequired()
int pageCount
This property holds the number of pages in the loaded document or 0 if no document is loaded.
Error load(const QString &fileName)
Loads the document contents from fileName.
void statusChanged(QPdfDocument::Status status)
Q_INVOKABLE QSizeF pagePointSize(int page) const
Returns the size of page page in points (1/72 of an inch).
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
QUrl resolvedSource() const
void setSource(QUrl source)
\qmlproperty url PdfDocument::source
QQuickPdfDocument(QObject *parent=nullptr)
A representation of a PDF document.
void classBegin() override
Invoked after class creation, but before any properties have been set.
\inmodule QtCore
Definition qsize.h:208
constexpr bool isValid() const noexcept
Returns true if both the width and height are equal to or greater than 0; otherwise returns false.
Definition qsize.h:329
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:332
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:335
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qurl.h:94
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
Definition qurl.cpp:1882
Combined button and popup list for selecting options.
static void * context
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLsizei GLchar * source
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
QObject * qmlExtendedObject(QObject *object)
Definition qqml.cpp:129
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define tr(X)
#define emit
double qreal
Definition qtypes.h:187