summaryrefslogtreecommitdiff
path: root/src/pdf
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2023-02-06 15:50:13 +0100
committerShawn Rutledge <shawn.rutledge@qt.io>2023-03-17 12:35:30 +0100
commitf36f0d5202fee3b778290dd68d1c30911b75b5b4 (patch)
treee7177927488ec3695e55c2363877bc6668ff8524 /src/pdf
parent76b01c4aeab77c4ab4e052d31141e010b9a73111 (diff)
downloadqtwebengine-f36f0d5202fee3b778290dd68d1c30911b75b5b4.tar.gz
Make QPdfLinkModel public and use in QPdfView
Now you can click links on pages in PDF documents in QPdfView. Task-number: QTBUG-77511 Fixes: QTBUG-102760 Change-Id: I348dcfd906be088aa3fcbe53aec4628b521cc2f3 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'src/pdf')
-rw-r--r--src/pdf/CMakeLists.txt2
-rw-r--r--src/pdf/qpdflinkmodel.cpp42
-rw-r--r--src/pdf/qpdflinkmodel.h65
-rw-r--r--src/pdf/qpdflinkmodel_p.h53
-rw-r--r--src/pdf/qpdflinkmodel_p_p.h44
5 files changed, 105 insertions, 101 deletions
diff --git a/src/pdf/CMakeLists.txt b/src/pdf/CMakeLists.txt
index 7ca252e29..e85270075 100644
--- a/src/pdf/CMakeLists.txt
+++ b/src/pdf/CMakeLists.txt
@@ -22,7 +22,7 @@ qt_internal_add_module(Pdf
qpdfdocumentrenderoptions.h
qpdffile.cpp qpdffile_p.h
qpdflink.cpp qpdflink.h qpdflink_p.h
- qpdflinkmodel.cpp qpdflinkmodel_p.h qpdflinkmodel_p_p.h
+ qpdflinkmodel.cpp qpdflinkmodel.h qpdflinkmodel_p.h
qpdfpagenavigator.cpp qpdfpagenavigator.h
qpdfpagerenderer.cpp qpdfpagerenderer.h
qpdfsearchmodel.cpp qpdfsearchmodel.h qpdfsearchmodel_p.h
diff --git a/src/pdf/qpdflinkmodel.cpp b/src/pdf/qpdflinkmodel.cpp
index c98a8723e..5fae579d3 100644
--- a/src/pdf/qpdflinkmodel.cpp
+++ b/src/pdf/qpdflinkmodel.cpp
@@ -2,8 +2,8 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qpdflink_p.h"
+#include "qpdflinkmodel.h"
#include "qpdflinkmodel_p.h"
-#include "qpdflinkmodel_p_p.h"
#include "qpdfdocument_p.h"
#include "third_party/pdfium/public/fpdf_doc.h"
@@ -16,9 +16,9 @@ QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(qLcLink, "qt.pdf.links")
-/*! \internal
+/*!
\class QPdfLinkModel
- \since 5.15
+ \since 6.6
\inmodule QtPdf
\inherits QAbstractListModel
@@ -28,7 +28,7 @@ Q_LOGGING_CATEGORY(qLcLink, "qt.pdf.links")
This is used in PDF viewers to implement the hyperlink mechanism.
*/
-/*! \internal
+/*!
\enum QPdfLinkModel::Role
\value Link A QPdfLink object.
@@ -40,7 +40,7 @@ Q_LOGGING_CATEGORY(qLcLink, "qt.pdf.links")
\omitvalue NRoles
*/
-/*! \internal
+/*!
Constructs a new link model with parent object \a parent.
*/
QPdfLinkModel::QPdfLinkModel(QObject *parent)
@@ -51,7 +51,7 @@ QPdfLinkModel::QPdfLinkModel(QObject *parent)
m_roleNames.insert(r, QByteArray(rolesMetaEnum.valueToKey(r)).toLower());
}
-/*! \internal
+/*!
Destroys the model.
*/
QPdfLinkModel::~QPdfLinkModel() {}
@@ -61,7 +61,7 @@ QHash<int, QByteArray> QPdfLinkModel::roleNames() const
return m_roleNames;
}
-/*! \internal
+/*!
\reimp
*/
int QPdfLinkModel::rowCount(const QModelIndex &parent) const
@@ -71,7 +71,7 @@ int QPdfLinkModel::rowCount(const QModelIndex &parent) const
return d->links.size();
}
-/*! \internal
+/*!
\reimp
*/
QVariant QPdfLinkModel::data(const QModelIndex &index, int role) const
@@ -99,9 +99,9 @@ QVariant QPdfLinkModel::data(const QModelIndex &index, int role) const
return QVariant();
}
-/*! \internal
+/*!
\property QPdfLinkModel::document
- \brief the document to load links from
+ \brief The document to load links from.
*/
QPdfDocument *QPdfLinkModel::document() const
{
@@ -125,9 +125,9 @@ void QPdfLinkModel::setDocument(QPdfDocument *document)
d->update();
}
-/*! \internal
+/*!
\property QPdfLinkModel::page
- \brief the page to load links from
+ \brief The page to load links from.
*/
int QPdfLinkModel::page() const
{
@@ -146,6 +146,24 @@ void QPdfLinkModel::setPage(int page)
d->update();
}
+/*!
+ Returns a \l {QPdfLink::isValid()}{valid} link if found under the \a point
+ (given in units of points, 1/72 of an inch), or an invalid link if it is
+ not found. In other words, this function is useful for picking, to handle
+ mouse click or hover.
+*/
+QPdfLink QPdfLinkModel::linkAt(const QPointF &point) const
+{
+ Q_D(const QPdfLinkModel);
+ for (const auto &link : std::as_const(d->links)) {
+ for (const auto &rect : link.rectangles()) {
+ if (rect.contains(point))
+ return link;
+ }
+ }
+ return {};
+}
+
QPdfLinkModelPrivate::QPdfLinkModelPrivate() : QAbstractItemModelPrivate()
{
}
diff --git a/src/pdf/qpdflinkmodel.h b/src/pdf/qpdflinkmodel.h
new file mode 100644
index 000000000..73cc23d1f
--- /dev/null
+++ b/src/pdf/qpdflinkmodel.h
@@ -0,0 +1,65 @@
+// Copyright (C) 2020 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
+
+#ifndef QPDFLINKMODEL_H
+#define QPDFLINKMODEL_H
+
+#include <QtPdf/qtpdfglobal.h>
+#include <QtPdf/qpdfdocument.h>
+#include <QtPdf/qpdflink.h>
+
+#include <QtCore/QAbstractListModel>
+
+QT_BEGIN_NAMESPACE
+
+class QPdfLinkModelPrivate;
+
+class Q_PDF_EXPORT QPdfLinkModel : public QAbstractListModel
+{
+ Q_OBJECT
+ Q_PROPERTY(QPdfDocument *document READ document WRITE setDocument NOTIFY documentChanged)
+ Q_PROPERTY(int page READ page WRITE setPage NOTIFY pageChanged)
+
+public:
+ enum class Role : int {
+ Link = Qt::UserRole,
+ Rectangle,
+ Url,
+ Page,
+ Location,
+ Zoom,
+ NRoles
+ };
+ Q_ENUM(Role)
+ explicit QPdfLinkModel(QObject *parent = nullptr);
+ ~QPdfLinkModel();
+
+ QPdfDocument *document() const;
+
+ QHash<int, QByteArray> roleNames() const override;
+ int rowCount(const QModelIndex &parent) const override;
+ QVariant data(const QModelIndex &index, int role) const override;
+
+ int page() const;
+
+ QPdfLink linkAt(const QPointF &point) const;
+
+public Q_SLOTS:
+ void setDocument(QPdfDocument *document);
+ void setPage(int page);
+
+Q_SIGNALS:
+ void documentChanged();
+ void pageChanged(int page);
+
+private Q_SLOTS:
+ void onStatusChanged(QPdfDocument::Status status);
+
+private:
+ QHash<int, QByteArray> m_roleNames;
+ Q_DECLARE_PRIVATE(QPdfLinkModel)
+};
+
+QT_END_NAMESPACE
+
+#endif // QPDFLINKMODEL_H
diff --git a/src/pdf/qpdflinkmodel_p.h b/src/pdf/qpdflinkmodel_p.h
index 3251d4e9a..99bd5f173 100644
--- a/src/pdf/qpdflinkmodel_p.h
+++ b/src/pdf/qpdflinkmodel_p.h
@@ -15,58 +15,23 @@
// We mean it.
//
-#include "qtpdfglobal.h"
-#include "qpdfdocument.h"
-
-#include <QObject>
-#include <QAbstractListModel>
+#include "qpdflinkmodel.h"
+#include <private/qabstractitemmodel_p.h>
QT_BEGIN_NAMESPACE
-class QPdfLinkModelPrivate;
-
-class Q_PDF_EXPORT QPdfLinkModel : public QAbstractListModel
+class QPdfLinkModelPrivate: public QAbstractItemModelPrivate
{
- Q_OBJECT
- Q_PROPERTY(QPdfDocument *document READ document WRITE setDocument NOTIFY documentChanged)
- Q_PROPERTY(int page READ page WRITE setPage NOTIFY pageChanged)
+ Q_DECLARE_PUBLIC(QPdfLinkModel)
public:
- enum class Role : int {
- Link = Qt::UserRole,
- Rectangle,
- Url,
- Page,
- Location,
- Zoom,
- NRoles
- };
- Q_ENUM(Role)
- explicit QPdfLinkModel(QObject *parent = nullptr);
- ~QPdfLinkModel();
-
- QPdfDocument *document() const;
-
- QHash<int, QByteArray> roleNames() const override;
- int rowCount(const QModelIndex &parent) const override;
- QVariant data(const QModelIndex &index, int role) const override;
-
- int page() const;
-
-public Q_SLOTS:
- void setDocument(QPdfDocument *document);
- void setPage(int page);
-
-Q_SIGNALS:
- void documentChanged();
- void pageChanged(int page);
+ QPdfLinkModelPrivate();
-private Q_SLOTS:
- void onStatusChanged(QPdfDocument::Status status);
+ void update();
-private:
- QHash<int, QByteArray> m_roleNames;
- Q_DECLARE_PRIVATE(QPdfLinkModel)
+ QPdfDocument *document = nullptr;
+ QList<QPdfLink> links;
+ int page = 0;
};
QT_END_NAMESPACE
diff --git a/src/pdf/qpdflinkmodel_p_p.h b/src/pdf/qpdflinkmodel_p_p.h
deleted file mode 100644
index ba553d41f..000000000
--- a/src/pdf/qpdflinkmodel_p_p.h
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright (C) 2020 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
-
-#ifndef QPDFLINKMODEL_P_P_H
-#define QPDFLINKMODEL_P_P_H
-
-//
-// W A R N I N G
-// -------------
-//
-// This file is not part of the Qt API. It exists purely as an
-// implementation detail. This header file may change from version to
-// version without notice, or even be removed.
-//
-// We mean it.
-//
-
-#include "qpdflinkmodel_p.h"
-#include "qpdflink.h"
-#include <private/qabstractitemmodel_p.h>
-
-#include "third_party/pdfium/public/fpdfview.h"
-
-#include <QUrl>
-
-QT_BEGIN_NAMESPACE
-
-class QPdfLinkModelPrivate: public QAbstractItemModelPrivate
-{
- Q_DECLARE_PUBLIC(QPdfLinkModel)
-
-public:
- QPdfLinkModelPrivate();
-
- void update();
-
- QPdfDocument *document = nullptr;
- QList<QPdfLink> links;
- int page = 0;
-};
-
-QT_END_NAMESPACE
-
-#endif // QPDFLINKMODEL_P_P_H