diff options
Diffstat (limited to 'src/pdfquick/qquickpdfdocument.cpp')
-rw-r--r-- | src/pdfquick/qquickpdfdocument.cpp | 168 |
1 files changed, 67 insertions, 101 deletions
diff --git a/src/pdfquick/qquickpdfdocument.cpp b/src/pdfquick/qquickpdfdocument.cpp index 2f5360f37..9770900db 100644 --- a/src/pdfquick/qquickpdfdocument.cpp +++ b/src/pdfquick/qquickpdfdocument.cpp @@ -1,43 +1,14 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtPDF module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** 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 http://www.qt.io/terms-conditions. For further -** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qquickpdfdocument_p.h" -#include <QQuickItem> -#include <QQmlEngine> -#include <QStandardPaths> +#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> +#include <QtQml/qqmlfile.h> QT_BEGIN_NAMESPACE @@ -60,20 +31,31 @@ QT_BEGIN_NAMESPACE QQuickPdfDocument::QQuickPdfDocument(QObject *parent) : QObject(parent) { - connect(&m_doc, &QPdfDocument::passwordChanged, this, &QQuickPdfDocument::passwordChanged); - connect(&m_doc, &QPdfDocument::passwordRequired, this, &QQuickPdfDocument::passwordRequired); - connect(&m_doc, &QPdfDocument::statusChanged, [this] (QPdfDocument::Status status) { - emit statusChanged(); - if (status == QPdfDocument::Ready) - emit metaDataChanged(); - }); - connect(&m_doc, &QPdfDocument::pageCountChanged, this, &QQuickPdfDocument::pageCountChanged); } -void QQuickPdfDocument::componentComplete() +/*! + \internal +*/ +QQuickPdfDocument::~QQuickPdfDocument() { - if (m_doc.error() == QPdfDocument::IncorrectPasswordError) - emit passwordRequired(); + delete m_carrierFile; +}; + +void QQuickPdfDocument::classBegin() +{ + m_doc = static_cast<QPdfDocument *>(qmlExtendedObject(this)); + Q_ASSERT(m_doc); + connect(m_doc, &QPdfDocument::passwordChanged, this, [this]() -> void { + if (resolvedSource().isValid()) + m_doc->load(QQmlFile::urlToLocalFileOrQrc(resolvedSource())); + }); + connect(m_doc, &QPdfDocument::statusChanged, this, [this] (QPdfDocument::Status status) { + emit errorChanged(); + if (status == QPdfDocument::Status::Ready) + emit metaDataChanged(); + }); + if (m_doc->error() == QPdfDocument::Error::IncorrectPassword) + emit m_doc->passwordRequired(); } /*! @@ -90,11 +72,14 @@ void QQuickPdfDocument::setSource(QUrl source) m_source = source; m_maxPageWidthHeight = QSizeF(); + if (m_carrierFile) + m_carrierFile->deleteLater(); + m_carrierFile = nullptr; emit sourceChanged(); - if (source.scheme() == QLatin1String("qrc")) - m_doc.load(QLatin1Char(':') + source.path()); - else - m_doc.load(source.toLocalFile()); + const QQmlContext *context = qmlContext(this); + m_resolvedSource = context ? context->resolvedUrl(source) : source; + if (m_resolvedSource.isValid()) + m_doc->load(QQmlFile::urlToLocalFileOrQrc(m_resolvedSource)); } /*! @@ -107,25 +92,25 @@ void QQuickPdfDocument::setSource(QUrl source) */ QString QQuickPdfDocument::error() const { - switch (m_doc.error()) { - case QPdfDocument::NoError: + switch (m_doc->error()) { + case QPdfDocument::Error::None: return tr("no error"); break; - case QPdfDocument::UnknownError: + case QPdfDocument::Error::Unknown: break; - case QPdfDocument::DataNotYetAvailableError: + case QPdfDocument::Error::DataNotYetAvailable: return tr("data not yet available"); break; - case QPdfDocument::FileNotFoundError: + case QPdfDocument::Error::FileNotFound: return tr("file not found"); break; - case QPdfDocument::InvalidFileFormatError: + case QPdfDocument::Error::InvalidFileFormat: return tr("invalid file format"); break; - case QPdfDocument::IncorrectPasswordError: + case QPdfDocument::Error::IncorrectPassword: return tr("incorrect password"); break; - case QPdfDocument::UnsupportedSecuritySchemeError: + case QPdfDocument::Error::UnsupportedSecurityScheme: return tr("unsupported security scheme"); break; } @@ -133,20 +118,12 @@ QString QQuickPdfDocument::error() const } /*! - \qmlproperty bool PdfDocument::password + \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. */ -void QQuickPdfDocument::setPassword(const QString &password) -{ - if (m_doc.password() == password) - return; - m_doc.setPassword(password); - if (source().isValid() && source().isLocalFile()) - m_doc.load(source().path()); -} /*! \qmlproperty int PdfDocument::pageCount @@ -167,58 +144,45 @@ void QQuickPdfDocument::setPassword(const QString &password) 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 { - const_cast<QQuickPdfDocument *>(this)->updateMaxPageSize(); + updateMaxPageSize(); return m_maxPageWidthHeight.width(); } qreal QQuickPdfDocument::maxPageHeight() const { - const_cast<QQuickPdfDocument *>(this)->updateMaxPageSize(); + updateMaxPageSize(); return m_maxPageWidthHeight.height(); } +QPdfDocument *QQuickPdfDocument::document() const +{ + return m_doc; +} + /*! \internal - \qmlmethod size PdfDocument::heightSumBeforePage(int page) - - Returns the sum of the heights, in points, of all sets of \a facingPages - pages from 0 to the given \a page, exclusive. - - That is, if the pages were laid out end-to-end in adjacent sets of - \a facingPages, what would be the distance in points from the top of the - first page to the top of the given page. + Returns a QPdfFile instance that can carry this document down into + QPdfIOHandler::load(QIODevice *). It should not be used for other purposes. */ -// Workaround for lack of something analogous to ListView.positionViewAtIndex() in TableView -qreal QQuickPdfDocument::heightSumBeforePage(int page, qreal spacing, int facingPages) const +QPdfFile *QQuickPdfDocument::carrierFile() { - qreal ret = 0; - for (int i = 0; i < page; i+= facingPages) { - if (i + facingPages > page) - break; - qreal facingPagesHeight = 0; - for (int j = i; j < i + facingPages; ++j) - facingPagesHeight = qMax(facingPagesHeight, pagePointSize(j).height()); - ret += facingPagesHeight + spacing; - } - return ret; + if (!m_carrierFile) + m_carrierFile = new QPdfFile(m_doc); + return m_carrierFile; } -void QQuickPdfDocument::updateMaxPageSize() +void QQuickPdfDocument::updateMaxPageSize() const { if (m_maxPageWidthHeight.isValid()) return; qreal w = 0; qreal h = 0; - const int count = pageCount(); + const int count = m_doc->pageCount(); for (int i = 0; i < count; ++i) { - auto size = pagePointSize(i); + auto size = m_doc->pagePointSize(i); w = qMax(w, size.width()); h = qMax(w, size.height()); } @@ -277,13 +241,13 @@ void QQuickPdfDocument::updateMaxPageSize() */ /*! - \qmlproperty string PdfDocument::creationDate + \qmlproperty date PdfDocument::creationDate This property holds the date and time the document was created. */ /*! - \qmlproperty string PdfDocument::modificationDate + \qmlproperty date PdfDocument::modificationDate This property holds the date and time the document was most recently modified. @@ -303,3 +267,5 @@ void QQuickPdfDocument::updateMaxPageSize() */ QT_END_NAMESPACE + +#include "moc_qquickpdfdocument_p.cpp" |