Qt 6.x
The Qt SDK
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*/
33{
34}
35
40
42{
43 m_doc = static_cast<QPdfDocument *>(qmlExtendedObject(this));
44 Q_ASSERT(m_doc);
45 connect(m_doc, &QPdfDocument::passwordChanged, this, [this]() -> void {
46 if (resolvedSource().isValid())
48 });
49 connect(m_doc, &QPdfDocument::statusChanged, this, [this] (QPdfDocument::Status status) {
51 if (status == QPdfDocument::Status::Ready)
53 });
55 emit m_doc->passwordRequired();
56}
57
66{
67 if (m_source == source)
68 return;
69
70 m_source = source;
71 m_maxPageWidthHeight = QSizeF();
72 m_carrierFile->deleteLater();
73 m_carrierFile = nullptr;
75 const QQmlContext *context = qmlContext(this);
76 m_resolvedSource = context ? context->resolvedUrl(source) : source;
77 if (m_resolvedSource.isValid())
78 m_doc->load(QQmlFile::urlToLocalFileOrQrc(m_resolvedSource));
79}
80
90{
91 switch (m_doc->error()) {
93 return tr("no error");
94 break;
96 break;
98 return tr("data not yet available");
99 break;
101 return tr("file not found");
102 break;
104 return tr("invalid file format");
105 break;
107 return tr("incorrect password");
108 break;
110 return tr("unsupported security scheme");
111 break;
112 }
113 return tr("unknown error");
114}
115
145{
146 updateMaxPageSize();
147 return m_maxPageWidthHeight.width();
148}
149
151{
152 updateMaxPageSize();
153 return m_maxPageWidthHeight.height();
154}
155
156QPdfDocument *QQuickPdfDocument::document() const
157{
158 return m_doc;
159}
160
166QPdfFile *QQuickPdfDocument::carrierFile()
167{
168 if (!m_carrierFile)
169 m_carrierFile = new QPdfFile(m_doc);
170 return m_carrierFile;
171}
172
173void QQuickPdfDocument::updateMaxPageSize() const
174{
175 if (m_maxPageWidthHeight.isValid())
176 return;
177 qreal w = 0;
178 qreal h = 0;
179 const int count = m_doc->pageCount();
180 for (int i = 0; i < count; ++i) {
181 auto size = m_doc->pagePointSize(i);
182 w = qMax(w, size.width());
183 h = qMax(w, size.height());
184 }
185 m_maxPageWidthHeight = QSizeF(w, h);
186}
187
266
267#include "moc_qquickpdfdocument_p.cpp"
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
void deleteLater()
\threadsafe
Definition qobject.cpp:2352
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 QFile.
Definition qqmlfile.cpp:643
QUrl resolvedSource() const
~QQuickPdfDocument() override
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:207
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:318
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:321
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\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:1874
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:71
QObject * qmlExtendedObject(QObject *object)
Definition qqml.cpp:125
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define tr(X)
#define emit
double qreal
Definition qtypes.h:92
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent