summaryrefslogtreecommitdiff
path: root/src/pdfquick
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2022-02-18 09:14:30 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2022-04-06 06:41:07 +0200
commitd3942c4af590d3830e118bacd64310a899a4c74a (patch)
tree5ff3fada71d9d8c9d151a6b3ba3d864f83d5c5f7 /src/pdfquick
parent3aaa33d1bf82394c4744ad777f7e3763984411c9 (diff)
downloadqtwebengine-d3942c4af590d3830e118bacd64310a899a4c74a.tar.gz
Use QML_EXTENDED to "inherit" QPdfDocument props, enums and signals
In QML we want to treat the status values like Error and Ready as if the enum were declared in QQuickPdfDocument, even though it's really from the non-QML type QPdfDocument. This is needed because QQuickPdfDocument doesn't inherit QPdfDocument (9968e2578f96081d2a242340620fcb2b96d9a1d3 could've done direct inheritance, but we sometimes value separation of QML API from C++ API). So we now de-duplicate other properties and signals too. It's as if from QML's perspective, PdfDocument inherits everything from QPdfDocument (including the properties pageCount, password and status), although in C++ it's not like that. Make all properties FINAL because we don't expect anybody to subclass QQuickPdfDocument. Fixes: QTBUG-100839 Change-Id: Idbb0d620443020d7168cba8f090d1f344a9b3296 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/pdfquick')
-rw-r--r--src/pdfquick/qquickpdfdocument.cpp57
-rw-r--r--src/pdfquick/qquickpdfdocument_p.h69
-rw-r--r--src/pdfquick/qquickpdflinkmodel.cpp2
-rw-r--r--src/pdfquick/qquickpdfpageimage.cpp11
-rw-r--r--src/pdfquick/qquickpdfsearchmodel.cpp9
-rw-r--r--src/pdfquick/qquickpdfselection.cpp14
6 files changed, 77 insertions, 85 deletions
diff --git a/src/pdfquick/qquickpdfdocument.cpp b/src/pdfquick/qquickpdfdocument.cpp
index 6cb57d76d..f7c6df75f 100644
--- a/src/pdfquick/qquickpdfdocument.cpp
+++ b/src/pdfquick/qquickpdfdocument.cpp
@@ -39,6 +39,7 @@
#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>
@@ -65,14 +66,6 @@ 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);
}
/*!
@@ -80,10 +73,21 @@ QQuickPdfDocument::QQuickPdfDocument(QObject *parent)
*/
QQuickPdfDocument::~QQuickPdfDocument() = default;
-void QQuickPdfDocument::componentComplete()
+void QQuickPdfDocument::classBegin()
{
- if (m_doc.error() == QPdfDocument::IncorrectPasswordError)
- emit passwordRequired();
+ 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();
}
/*!
@@ -104,9 +108,9 @@ void QQuickPdfDocument::setSource(QUrl source)
const QQmlContext *context = qmlContext(this);
m_resolvedSource = context ? context->resolvedUrl(source) : source;
if (source.scheme() == QLatin1String("qrc"))
- m_doc.load(QLatin1Char(':') + m_resolvedSource.path());
+ m_doc->load(QLatin1Char(':') + m_resolvedSource.path());
else
- m_doc.load(m_resolvedSource.toLocalFile());
+ m_doc->load(m_resolvedSource.toLocalFile());
}
/*!
@@ -119,7 +123,7 @@ void QQuickPdfDocument::setSource(QUrl source)
*/
QString QQuickPdfDocument::error() const
{
- switch (m_doc.error()) {
+ switch (m_doc->error()) {
case QPdfDocument::NoError:
return tr("no error");
break;
@@ -151,14 +155,6 @@ QString QQuickPdfDocument::error() const
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 (resolvedSource().isValid() && resolvedSource().isLocalFile())
- m_doc.load(resolvedSource().path());
-}
/*!
\qmlproperty int PdfDocument::pageCount
@@ -181,21 +177,26 @@ void QQuickPdfDocument::setPassword(const QString &password)
*/
QSizeF QQuickPdfDocument::pagePointSize(int page) const
{
- return m_doc.pageSize(page);
+ 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
Returns a QPdfFile instance that can carry this document down into
@@ -204,17 +205,17 @@ qreal QQuickPdfDocument::maxPageHeight() const
QPdfFile *QQuickPdfDocument::carrierFile()
{
if (!m_carrierFile)
- m_carrierFile = new QPdfFile(&m_doc);
+ 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);
w = qMax(w, size.width());
diff --git a/src/pdfquick/qquickpdfdocument_p.h b/src/pdfquick/qquickpdfdocument_p.h
index ca5be7b16..48280468e 100644
--- a/src/pdfquick/qquickpdfdocument_p.h
+++ b/src/pdfquick/qquickpdfdocument_p.h
@@ -68,52 +68,44 @@ class Q_PDFQUICK_EXPORT QQuickPdfDocument : public QObject, public QQmlParserSta
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
- Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
- Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged FINAL)
- Q_PROPERTY(qreal maxPageWidth READ maxPageWidth NOTIFY metaDataChanged)
- Q_PROPERTY(qreal maxPageHeight READ maxPageHeight NOTIFY metaDataChanged)
- Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged FINAL)
- Q_PROPERTY(QPdfDocument::Status status READ status NOTIFY statusChanged FINAL)
- Q_PROPERTY(QString error READ error NOTIFY statusChanged FINAL)
-
- Q_PROPERTY(QString title READ title NOTIFY metaDataChanged)
- Q_PROPERTY(QString subject READ subject NOTIFY metaDataChanged)
- Q_PROPERTY(QString author READ author NOTIFY metaDataChanged)
- Q_PROPERTY(QString keywords READ keywords NOTIFY metaDataChanged)
- Q_PROPERTY(QString producer READ producer NOTIFY metaDataChanged)
- Q_PROPERTY(QString creator READ creator NOTIFY metaDataChanged)
- Q_PROPERTY(QDateTime creationDate READ creationDate NOTIFY metaDataChanged)
- Q_PROPERTY(QDateTime modificationDate READ modificationDate NOTIFY metaDataChanged)
+ Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged FINAL)
+ Q_PROPERTY(qreal maxPageWidth READ maxPageWidth NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(qreal maxPageHeight READ maxPageHeight NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QString error READ error NOTIFY errorChanged FINAL)
+
+ Q_PROPERTY(QString title READ title NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QString subject READ subject NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QString author READ author NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QString keywords READ keywords NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QString producer READ producer NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QString creator READ creator NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QDateTime creationDate READ creationDate NOTIFY metaDataChanged FINAL)
+ Q_PROPERTY(QDateTime modificationDate READ modificationDate NOTIFY metaDataChanged FINAL)
QML_NAMED_ELEMENT(PdfDocument)
+ QML_EXTENDED(QPdfDocument)
QML_ADDED_IN_VERSION(5, 15)
public:
explicit QQuickPdfDocument(QObject *parent = nullptr);
~QQuickPdfDocument() override;
- void classBegin() override {}
- void componentComplete() override;
+ void classBegin() override;
+ void componentComplete() override {}
QUrl source() const { return m_source; }
void setSource(QUrl source);
QUrl resolvedSource() const { return m_resolvedSource; }
- int pageCount() const { return m_doc.pageCount(); }
- QPdfDocument::Status status() const { return m_doc.status(); }
-
QString error() const;
- QString password() const { return m_doc.password(); }
- void setPassword(const QString &password);
-
- QString title() { return m_doc.metaData(QPdfDocument::Title).toString(); }
- QString author() { return m_doc.metaData(QPdfDocument::Author).toString(); }
- QString subject() { return m_doc.metaData(QPdfDocument::Subject).toString(); }
- QString keywords() { return m_doc.metaData(QPdfDocument::Keywords).toString(); }
- QString producer() { return m_doc.metaData(QPdfDocument::Producer).toString(); }
- QString creator() { return m_doc.metaData(QPdfDocument::Creator).toString(); }
- QDateTime creationDate() { return m_doc.metaData(QPdfDocument::CreationDate).toDateTime(); }
- QDateTime modificationDate() { return m_doc.metaData(QPdfDocument::ModificationDate).toDateTime(); }
+ QString title() { return m_doc->metaData(QPdfDocument::Title).toString(); }
+ QString author() { return m_doc->metaData(QPdfDocument::Author).toString(); }
+ QString subject() { return m_doc->metaData(QPdfDocument::Subject).toString(); }
+ QString keywords() { return m_doc->metaData(QPdfDocument::Keywords).toString(); }
+ QString producer() { return m_doc->metaData(QPdfDocument::Producer).toString(); }
+ QString creator() { return m_doc->metaData(QPdfDocument::Creator).toString(); }
+ QDateTime creationDate() { return m_doc->metaData(QPdfDocument::CreationDate).toDateTime(); }
+ QDateTime modificationDate() { return m_doc->metaData(QPdfDocument::ModificationDate).toDateTime(); }
Q_INVOKABLE QSizeF pagePointSize(int page) const;
qreal maxPageWidth() const;
@@ -121,23 +113,20 @@ public:
Q_SIGNALS:
void sourceChanged();
- void passwordChanged();
- void passwordRequired();
- void statusChanged();
- void pageCountChanged();
+ void errorChanged();
void metaDataChanged();
private:
- QPdfDocument &document() { return m_doc; }
+ QPdfDocument *document() const;
QPdfFile *carrierFile();
- void updateMaxPageSize();
+ void updateMaxPageSize() const;
private:
QUrl m_source;
QUrl m_resolvedSource;
- QPdfDocument m_doc;
+ QPdfDocument *m_doc = nullptr;
QPdfFile *m_carrierFile = nullptr;
- QSizeF m_maxPageWidthHeight;
+ mutable QSizeF m_maxPageWidthHeight;
friend class QQuickPdfLinkModel;
friend class QQuickPdfPageImage;
diff --git a/src/pdfquick/qquickpdflinkmodel.cpp b/src/pdfquick/qquickpdflinkmodel.cpp
index 00fe2c06f..f38864591 100644
--- a/src/pdfquick/qquickpdflinkmodel.cpp
+++ b/src/pdfquick/qquickpdflinkmodel.cpp
@@ -128,7 +128,7 @@ void QQuickPdfLinkModel::setDocument(QQuickPdfDocument *document)
return;
m_quickDocument = document;
if (document)
- QPdfLinkModel::setDocument(&document->m_doc);
+ QPdfLinkModel::setDocument(document->document());
}
/*!
diff --git a/src/pdfquick/qquickpdfpageimage.cpp b/src/pdfquick/qquickpdfpageimage.cpp
index 18513fba4..60853454f 100644
--- a/src/pdfquick/qquickpdfpageimage.cpp
+++ b/src/pdfquick/qquickpdfpageimage.cpp
@@ -86,11 +86,11 @@ void QQuickPdfPageImage::setDocument(QQuickPdfDocument *document)
return;
if (d->doc)
- disconnect(d->doc, &QQuickPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
+ disconnect(d->doc->document(), &QPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
d->doc = document;
if (document) {
- connect(document, &QQuickPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
- if (document->status() == QPdfDocument::Status::Ready)
+ connect(document->document(), &QPdfDocument::statusChanged, this, &QQuickPdfPageImage::documentStatusChanged);
+ if (document->document()->status() == QPdfDocument::Status::Ready)
setSource(document->resolvedSource()); // calls load()
}
emit documentChanged();
@@ -147,8 +147,9 @@ void QQuickPdfPageImage::load()
void QQuickPdfPageImage::documentStatusChanged()
{
Q_D(QQuickPdfPageImage);
- qCDebug(qLcImg) << "document status" << d->doc->status();
- if (d->doc->status() == QPdfDocument::Status::Ready)
+ const auto status = d->doc->document()->status();
+ qCDebug(qLcImg) << "document status" << status;
+ if (status == QPdfDocument::Status::Ready)
setSource(d->doc->resolvedSource()); // calls load()
}
diff --git a/src/pdfquick/qquickpdfsearchmodel.cpp b/src/pdfquick/qquickpdfsearchmodel.cpp
index b5d9bc67d..40875cd21 100644
--- a/src/pdfquick/qquickpdfsearchmodel.cpp
+++ b/src/pdfquick/qquickpdfsearchmodel.cpp
@@ -79,7 +79,7 @@ void QQuickPdfSearchModel::setDocument(QQuickPdfDocument *document)
return;
m_quickDocument = document;
- QPdfSearchModel::setDocument(&document->m_doc);
+ QPdfSearchModel::setDocument(document->document());
}
/*!
@@ -213,7 +213,7 @@ QList<QPolygonF> QQuickPdfSearchModel::currentPageBoundingPolygons() const
*/
QList<QPolygonF> QQuickPdfSearchModel::boundingPolygonsOnPage(int page)
{
- if (!document() || searchString().isEmpty() || page < 0 || page > document()->pageCount())
+ if (!document() || searchString().isEmpty() || page < 0 || page > document()->document()->pageCount())
return {};
updatePage(page);
@@ -239,9 +239,10 @@ void QQuickPdfSearchModel::setCurrentPage(int currentPage)
if (m_currentPage == currentPage)
return;
+ const auto pageCount = document()->document()->pageCount();
if (currentPage < 0)
- currentPage = document()->pageCount() - 1;
- else if (currentPage >= document()->pageCount())
+ currentPage = pageCount - 1;
+ else if (currentPage >= pageCount)
currentPage = 0;
m_currentPage = currentPage;
diff --git a/src/pdfquick/qquickpdfselection.cpp b/src/pdfquick/qquickpdfselection.cpp
index 0f75c44ac..5dc3c2f5d 100644
--- a/src/pdfquick/qquickpdfselection.cpp
+++ b/src/pdfquick/qquickpdfselection.cpp
@@ -171,7 +171,7 @@ void QQuickPdfSelection::clear()
void QQuickPdfSelection::selectAll()
{
- QPdfSelection sel = m_document->m_doc.getAllText(m_page);
+ QPdfSelection sel = m_document->document()->getAllText(m_page);
if (sel.text() != m_text) {
m_text = sel.text();
if (QGuiApplication::clipboard()->supportsSelection())
@@ -211,14 +211,14 @@ void QQuickPdfSelection::keyReleaseEvent(QKeyEvent *ev)
i = 0;
else
i += 1; // don't select the space before the word
- auto sel = m_document->m_doc.getSelectionAtIndex(m_page, i, m_text.length() + m_fromCharIndex - i);
+ auto sel = m_document->document()->getSelectionAtIndex(m_page, i, m_text.length() + m_fromCharIndex - i);
update(sel);
QGuiApplication::inputMethod()->update(Qt::ImAnchorRectangle);
} else if (ev == QKeySequence::SelectNextWord) {
int i = allText.indexOf(WordDelimiter, m_toCharIndex);
if (i < 0)
i = allText.length(); // go to the end of m_textAfter
- auto sel = m_document->m_doc.getSelectionAtIndex(m_page, m_fromCharIndex, m_text.length() + i - m_toCharIndex);
+ auto sel = m_document->document()->getSelectionAtIndex(m_page, m_fromCharIndex, m_text.length() + i - m_toCharIndex);
update(sel);
QGuiApplication::inputMethod()->update(Qt::ImCursorRectangle);
} else if (ev == QKeySequence::Copy) {
@@ -234,7 +234,7 @@ void QQuickPdfSelection::inputMethodEvent(QInputMethodEvent *event)
qCDebug(qLcIm) << "QInputMethodEvent::Cursor: moved to" << attr.start << "len" << attr.length;
break;
case QInputMethodEvent::Selection: {
- auto sel = m_document->m_doc.getSelectionAtIndex(m_page, attr.start, attr.length);
+ auto sel = m_document->document()->getSelectionAtIndex(m_page, attr.start, attr.length);
update(sel);
qCDebug(qLcIm) << "QInputMethodEvent::Selection: from" << attr.start << "len" << attr.length
<< "result:" << m_fromCharIndex << "->" << m_toCharIndex << sel.boundingRectangle();
@@ -259,7 +259,7 @@ QVariant QQuickPdfSelection::inputMethodQuery(Qt::InputMethodQuery query, const
if (m_hitPoint == argument.toPointF())
return inputMethodQuery(query);
m_hitPoint = argument.toPointF();
- auto tp = m_document->m_doc.d->hitTest(m_page, m_hitPoint / m_renderScale);
+ auto tp = m_document->document()->d->hitTest(m_page, m_hitPoint / m_renderScale);
qCDebug(qLcIm) << "ImCursorPosition hit testing in px" << m_hitPoint << "pt" << (m_hitPoint / m_renderScale)
<< "got char index" << tp.charIndex << "@" << tp.position << "pt," << tp.position * m_renderScale << "px";
if (tp.charIndex >= 0) {
@@ -343,7 +343,7 @@ QVariant QQuickPdfSelection::inputMethodQuery(Qt::InputMethodQuery query) const
const QString &QQuickPdfSelection::pageText() const
{
if (m_pageTextDirty) {
- m_pageText = m_document->m_doc.getAllText(m_page).text();
+ m_pageText = m_document->document()->getAllText(m_page).text();
m_pageTextDirty = false;
}
return m_pageText;
@@ -504,7 +504,7 @@ void QQuickPdfSelection::updateResults()
{
if (!m_document)
return;
- QPdfSelection sel = m_document->document().getSelection(m_page,
+ QPdfSelection sel = m_document->document()->getSelection(m_page,
m_fromPoint / m_renderScale, m_toPoint / m_renderScale);
update(sel, true);
}