Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpdflink.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
4#include "qpdflink.h"
5#include "qpdflink_p.h"
7#include <QGuiApplication>
8#include <QDebug>
9
11
12/*!
13 \class QPdfLink
14 \since 6.4
15 \inmodule QtPdf
16
17 \brief The QPdfLink class defines a link between a region on a page
18 (such as a hyperlink or a search result) and a destination
19 (page, location on the page, and zoom level at which to view it).
20*/
21
22/*!
23 Constructs an invalid Destination.
24
25 \sa valid
26*/
27QPdfLink::QPdfLink() :
28 QPdfLink(new QPdfLinkPrivate()) { }
29
30QPdfLink::QPdfLink(int page, QPointF location, qreal zoom)
31 : QPdfLink(new QPdfLinkPrivate(page, location, zoom))
32{
33}
34
35QPdfLink::QPdfLink(int page, QList<QRectF> rects,
36 QString contextBefore, QString contextAfter)
37 : QPdfLink(new QPdfLinkPrivate(page, std::move(rects),
38 std::move(contextBefore),
39 std::move(contextAfter)))
40{
41}
42
43QPdfLink::QPdfLink(QPdfLinkPrivate *d) : d(d) {}
44
45QPdfLink::~QPdfLink() = default;
46QPdfLink::QPdfLink(const QPdfLink &other) noexcept = default;
47QPdfLink::QPdfLink(QPdfLink &&other) noexcept = default;
48QPdfLink &QPdfLink::operator=(const QPdfLink &other) = default;
49
50/*!
51 \property QPdfLink::valid
52
53 This property holds whether the link is valid.
54*/
55bool QPdfLink::isValid() const
56{
57 return d->page >= 0;
58}
59
60/*!
61 \property QPdfLink::page
62
63 This property holds the page number.
64 If the link is a search result, it is the page number on which the result is found;
65 if the link is a hyperlink, it is the destination page number.
66*/
67int QPdfLink::page() const
68{
69 return d->page;
70}
71
72/*!
73 \property QPdfLink::location
74
75 This property holds the location on the \l page, in units of points.
76 If the link is a search result, it is the location where the result is found;
77 if the link is a hyperlink, it is the destination location.
78*/
79QPointF QPdfLink::location() const
80{
81 return d->location;
82}
83
84/*!
85 \property QPdfLink::zoom
86
87 This property holds the suggested magnification level, where 1.0 means default scale
88 (1 pixel = 1 point). If the link is a search result, this value is not used.
89*/
90qreal QPdfLink::zoom() const
91{
92 return d->zoom;
93}
94
95/*!
96 \property QPdfLink::url
97
98 This property holds the destination URL if the link is an external hyperlink;
99 otherwise, it's empty.
100*/
101QUrl QPdfLink::url() const
102{
103 return d->url;
104}
105
106/*!
107 \property QPdfLink::contextBefore
108
109 This property holds adjacent text found on the page before the search string.
110 If the link is a hyperlink, this string is empty.
111
112 \sa QPdfSearchModel::resultsOnPage(), QPdfSearchModel::resultAtIndex()
113*/
114QString QPdfLink::contextBefore() const
115{
116 return d->contextBefore;
117}
118
119/*!
120 \property QPdfLink::contextAfter
121
122 This property holds adjacent text found on the page after the search string.
123 If the link is a hyperlink, this string is empty.
124
125 \sa QPdfSearchModel::resultsOnPage(), QPdfSearchModel::resultAtIndex()
126*/
127QString QPdfLink::contextAfter() const
128{
129 return d->contextAfter;
130}
131
132/*!
133 \property QPdfLink::rectangles
134
135 This property holds the region (set of rectangles) occupied by the link or
136 search result on the page where it was found. If the text wraps around to
137 multiple lines on the page, there may be multiple rectangles:
138
139 \image wrapping-search-result.png
140
141 \sa QPdfSearchModel::resultsOnPage(), QPdfSearchModel::resultAtIndex()
142*/
143QList<QRectF> QPdfLink::rectangles() const
144{
145 return d->rects;
146}
147
148/*!
149 Returns a translated representation for display.
150
151 \sa copyToClipboard()
152*/
153QString QPdfLink::toString() const
154{
155 if (d->page <= 0)
156 return d->url.toString();
157 return QPdfLinkModel::tr("Page %1 location %2, %3 zoom %4")
158 .arg(d->page).arg(d->location.x(), 0, 'f', 1).arg(d->location.y(), 0, 'f', 1)
159 .arg(d->zoom, 0, 'f', 0);
160}
161
162/*!
163 Copies the toString() representation of the link to the
164 \l {QGuiApplication::clipboard()}{system clipboard} depending on the \a mode given.
165*/
166void QPdfLink::copyToClipboard(QClipboard::Mode mode) const
167{
168 QGuiApplication::clipboard()->setText(toString(), mode);
169}
170
171#ifndef QT_NO_DEBUG_STREAM
172QDebug operator<<(QDebug dbg, const QPdfLink &link)
173{
174 QDebugStateSaver saver(dbg);
175 dbg.nospace();
176 dbg << "QPdfLink(page=" << link.page()
177 << " location=" << link.location()
178 << " zoom=" << link.zoom()
179 << " contextBefore=" << link.contextBefore()
180 << " contextAfter=" << link.contextAfter()
181 << " rects=" << link.rectangles();
182 dbg << ')';
183 return dbg;
184}
185#endif
186
187QT_END_NAMESPACE
188
189#include "moc_qpdflink.cpp"