summaryrefslogtreecommitdiffstats
path: root/src/pdf/qpdflink.cpp
blob: 94d4e741d3e16636a01d65d34f64349e82bd930e (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtPDF module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qpdflink.h"
#include "qpdflink_p.h"
#include "qpdflinkmodel_p.h"
#include <QGuiApplication>

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
{
    static const QString format = QPdfLinkModel::tr("page %1 location %2,%3 zoom %4");
    return d->page > 0 ? format.arg(QString::number(d->page),
                                    QString::number(d->location.x()),
                                    QString::number(d->location.y()),
                                    QString::number(d->zoom))
                       : d->url.toString();
}

/*!
    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);
}

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;
}

QT_END_NAMESPACE

#include "moc_qpdflink.cpp"