summaryrefslogtreecommitdiffstats
path: root/src/pdf/qpdflink.cpp
blob: 0c28670864c762a4234d0af6fe11595c763c22a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// 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 "qpdflink.h"
#include "qpdflink_p.h"
#include "qpdflinkmodel_p.h"
#include <QGuiApplication>
#include <QDebug>

QT_BEGIN_NAMESPACE

/*!
    \class QPdfLink
    \since 6.4
    \inmodule QtPdf

    \brief The QPdfLink class defines a link between a region on a page
    (such as a hyperlink or a search result) and a destination
    (page, location on the page, and zoom level at which to view it).
*/

/*!
    Constructs an invalid Destination.

    \sa valid
*/
QPdfLink::QPdfLink() :
    QPdfLink(new QPdfLinkPrivate()) { }

QPdfLink::QPdfLink(int page, QPointF location, qreal zoom)
  : QPdfLink(new QPdfLinkPrivate(page, location, zoom))
{
}

QPdfLink::QPdfLink(int page, QList<QRectF> rects,
                                   QString contextBefore, QString contextAfter)
    : QPdfLink(new QPdfLinkPrivate(page, std::move(rects),
                                                   std::move(contextBefore),
                                                   std::move(contextAfter)))
{
}

QPdfLink::QPdfLink(QPdfLinkPrivate *d) : d(d) {}

QPdfLink::~QPdfLink() = default;
QPdfLink::QPdfLink(const QPdfLink &other) noexcept = default;
QPdfLink::QPdfLink(QPdfLink &&other) noexcept = default;
QPdfLink &QPdfLink::operator=(const QPdfLink &other) = default;

/*!
    \property QPdfLink::valid

    This property holds whether the link is valid.
*/
bool QPdfLink::isValid() const
{
    return d->page >= 0;
}

/*!
    \property QPdfLink::page

    This property holds the page number.
    If the link is a search result, it is the page number on which the result is found;
    if the link is a hyperlink, it is the destination page number.
*/
int QPdfLink::page() const
{
    return d->page;
}

/*!
    \property QPdfLink::location

    This property holds the location on the \l page, in units of points.
    If the link is a search result, it is the location where the result is found;
    if the link is a hyperlink, it is the destination location.
*/
QPointF QPdfLink::location() const
{
    return d->location;
}

/*!
    \property QPdfLink::zoom

    This property holds the suggested magnification level, where 1.0 means default scale
    (1 pixel = 1 point). If the link is a search result, this value is not used.
*/
qreal QPdfLink::zoom() const
{
    return d->zoom;
}

/*!
    \property QPdfLink::url

    This property holds the destination URL if the link is an external hyperlink;
    otherwise, it's empty.
*/
QUrl QPdfLink::url() const
{
    return d->url;
}

/*!
    \property QPdfLink::contextBefore

    This property holds adjacent text found on the page before the search string.
    If the link is a hyperlink, this string is empty.

    \sa QPdfSearchModel::resultsOnPage(), QPdfSearchModel::resultAtIndex()
*/
QString QPdfLink::contextBefore() const
{
    return d->contextBefore;
}

/*!
    \property QPdfLink::contextAfter

    This property holds adjacent text found on the page after the search string.
    If the link is a hyperlink, this string is empty.

    \sa QPdfSearchModel::resultsOnPage(), QPdfSearchModel::resultAtIndex()
*/
QString QPdfLink::contextAfter() const
{
    return d->contextAfter;
}

/*!
    \property QPdfLink::rectangles

    This property holds the region (set of rectangles) occupied by the link or
    search result on the page where it was found. If the text wraps around to
    multiple lines on the page, there may be multiple rectangles:

    \image wrapping-search-result.png

    \sa QPdfSearchModel::resultsOnPage(), QPdfSearchModel::resultAtIndex()
*/
QList<QRectF> QPdfLink::rectangles() const
{
    return d->rects;
}

/*!
    Returns a translated representation for display.

    \sa copyToClipboard()
*/
QString QPdfLink::toString() const
{
    if (d->page <= 0)
         return d->url.toString();
    return QPdfLinkModel::tr("Page %1 location %2, %3 zoom %4")
        .arg(d->page).arg(d->location.x(), 0, 'f', 1).arg(d->location.y(), 0, 'f', 1)
        .arg(d->zoom, 0, 'f', 0);
}

/*!
    Copies the toString() representation of the link to the
    \l {QGuiApplication::clipboard()}{system clipboard} depending on the \a mode given.
*/
void QPdfLink::copyToClipboard(QClipboard::Mode mode) const
{
    QGuiApplication::clipboard()->setText(toString(), mode);
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QPdfLink &link)
{
    QDebugStateSaver saver(dbg);
    dbg.nospace();
    dbg << "QPdfLink(page=" << link.page()
        << " location=" << link.location()
        << " zoom=" << link.zoom()
        << " contextBefore=" << link.contextBefore()
        << " contextAfter=" << link.contextAfter()
        << " rects=" << link.rectangles();
    dbg << ')';
    return dbg;
}
#endif

QT_END_NAMESPACE

#include "moc_qpdflink.cpp"