summaryrefslogtreecommitdiffstats
path: root/src/pdf/qpdfpagenavigator.cpp
blob: e077e21848fa914be1c76ab867ec59878c6a47a8 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// 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 "qpdfpagenavigator.h"
#include "qpdfdocument.h"
#include "qpdflink_p.h"

#include <QtCore/qloggingcategory.h>
#include <QtCore/qpointer.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(qLcNav, "qt.pdf.pagenavigator")

struct QPdfPageNavigatorPrivate
{
    QPdfPageNavigator *q = nullptr;

    QList<QExplicitlySharedDataPointer<QPdfLinkPrivate>> pageHistory;
    int currentHistoryIndex = 0;
    bool changing = false;
};

/*!
    \class QPdfPageNavigator
    \since 6.4
    \inmodule QtPdf
    \brief Navigation history within a PDF document.

    The QPdfPageNavigator class remembers which destinations the user
    has visited in a PDF document, and provides the ability to traverse
    backward and forward. It is used to implement back and forward actions
    similar to the back and forward buttons in a web browser.

    \sa QPdfDocument
*/

/*!
    Constructs a page navigation stack with parent object \a parent.
*/
QPdfPageNavigator::QPdfPageNavigator(QObject *parent)
    : QObject(parent), d(new QPdfPageNavigatorPrivate)
{
    d->q = this;
    clear();
}

/*!
    Destroys the page navigation stack.
*/
QPdfPageNavigator::~QPdfPageNavigator()
{
}

/*!
    Goes back to the page, location and zoom level that was being viewed before
    back() was called, and then emits the \l jumped() signal.

    If a new destination was pushed since the last time \l back() was called,
    the forward() function does nothing, because there is a branch in the
    timeline which causes the "future" to be lost.
*/
void QPdfPageNavigator::forward()
{
    if (d->currentHistoryIndex >= d->pageHistory.size() - 1)
        return;
    const bool backAvailableWas = backAvailable();
    const bool forwardAvailableWas = forwardAvailable();
    QPointF currentLocationWas = currentLocation();
    qreal currentZoomWas = currentZoom();
    ++d->currentHistoryIndex;
    d->changing = true;
    emit jumped(currentLink());
    if (currentZoomWas != currentZoom())
        emit currentZoomChanged(currentZoom());
    emit currentPageChanged(currentPage());
    if (currentLocationWas != currentLocation())
        emit currentLocationChanged(currentLocation());
    if (!backAvailableWas)
        emit backAvailableChanged(backAvailable());
    if (forwardAvailableWas != forwardAvailable())
        emit forwardAvailableChanged(forwardAvailable());
    d->changing = false;
    qCDebug(qLcNav) << "forward: index" << d->currentHistoryIndex << "page" << currentPage()
                    << "@" << currentLocation() << "zoom" << currentZoom();
}

/*!
    Pops the stack, updates the \l currentPage, \l currentLocation and
    \l currentZoom properties to the most-recently-viewed destination, and then
    emits the \l jumped() signal.
*/
void QPdfPageNavigator::back()
{
    if (d->currentHistoryIndex <= 0)
        return;
    const bool backAvailableWas = backAvailable();
    const bool forwardAvailableWas = forwardAvailable();
    QPointF currentLocationWas = currentLocation();
    qreal currentZoomWas = currentZoom();
    --d->currentHistoryIndex;
    d->changing = true;
    emit jumped(currentLink());
    if (currentZoomWas != currentZoom())
        emit currentZoomChanged(currentZoom());
    emit currentPageChanged(currentPage());
    if (currentLocationWas != currentLocation())
        emit currentLocationChanged(currentLocation());
    if (backAvailableWas != backAvailable())
        emit backAvailableChanged(backAvailable());
    if (!forwardAvailableWas)
        emit forwardAvailableChanged(forwardAvailable());
    d->changing = false;
    qCDebug(qLcNav) << "back: index" << d->currentHistoryIndex << "page" << currentPage()
                    << "@" << currentLocation() << "zoom" << currentZoom();
}
/*!
    \property QPdfPageNavigator::currentPage

    This property holds the current page that is being viewed.
    The default is \c 0.
*/
int QPdfPageNavigator::currentPage() const
{
    if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
        return -1; // only until ctor or clear() runs
    return d->pageHistory.at(d->currentHistoryIndex)->page;
}

/*!
    \property QPdfPageNavigator::currentLocation

    This property holds the current location on the page that is being viewed
    (the location that was last given to jump() or update()). The default is
    \c {0, 0}.
*/
QPointF QPdfPageNavigator::currentLocation() const
{
    if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
        return QPointF();
    return d->pageHistory.at(d->currentHistoryIndex)->location;
}

/*!
    \property QPdfPageNavigator::currentZoom

    This property holds the magnification scale (1 logical pixel = 1 point)
    on the page that is being viewed. The default is \c 1.
*/
qreal QPdfPageNavigator::currentZoom() const
{
    if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
        return 1;
    return d->pageHistory.at(d->currentHistoryIndex)->zoom;
}

QPdfLink QPdfPageNavigator::currentLink() const
{
    if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
        return QPdfLink();
    return QPdfLink(d->pageHistory.at(d->currentHistoryIndex).data());
}

/*!
    Clear the history and restore \l currentPage, \l currentLocation and
    \l currentZoom to their default values.
*/
void QPdfPageNavigator::clear()
{
    d->pageHistory.clear();
    d->currentHistoryIndex = 0;
    // Begin with an implicit jump to page 0, so that
    // backAvailable() will become true after jump() is called one more time.
    d->pageHistory.append(QExplicitlySharedDataPointer<QPdfLinkPrivate>(new QPdfLinkPrivate(0, {}, 1)));
}

/*!
    Adds the given \a destination to the history of visited locations.

    In this case, PDF views respond to the \l jumped signal by scrolling to
    place \c destination.rectangles in the viewport, as opposed to placing
    \c destination.location in the viewport. So it's appropriate to call this
    method to jump to a search result from QPdfSearchModel (because the
    rectangles cover the region of text found). To jump to a hyperlink
    destination, call jump(page, location, zoom) instead, because in that
    case the QPdfLink object's \c rectangles cover the hyperlink origin
    location rather than the destination.
*/
void QPdfPageNavigator::jump(QPdfLink destination)
{
    const bool zoomChange = !qFuzzyCompare(destination.zoom(), currentZoom());
    const bool pageChange = (destination.page() != currentPage());
    const bool locationChange = (destination.location() != currentLocation());
    const bool backAvailableWas = backAvailable();
    const bool forwardAvailableWas = forwardAvailable();
    if (!d->changing) {
        if (d->currentHistoryIndex >= 0 && forwardAvailableWas)
            d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.size() - d->currentHistoryIndex - 1);
        d->pageHistory.append(destination.d);
        d->currentHistoryIndex = d->pageHistory.size() - 1;
    }
    if (zoomChange)
        emit currentZoomChanged(currentZoom());
    if (pageChange)
        emit currentPageChanged(currentPage());
    if (locationChange)
        emit currentLocationChanged(currentLocation());
    if (d->changing)
        return;
    if (backAvailableWas != backAvailable())
        emit backAvailableChanged(backAvailable());
    if (forwardAvailableWas != forwardAvailable())
        emit forwardAvailableChanged(forwardAvailable());
    emit jumped(currentLink());
    qCDebug(qLcNav) << "push: index" << d->currentHistoryIndex << destination << "-> history" <<
        [this]() {
            QStringList ret;
            for (auto d : d->pageHistory)
                ret << QString::number(d->page);
            return ret.join(QLatin1Char(','));
        }();
}

/*!
    Adds the given destination, consisting of \a page, \a location, and \a zoom,
    to the history of visited locations.

    The \a zoom argument represents magnification (where \c 1 is the default
    scale, 1 logical pixel = 1 point). If \a zoom is not given or is \c 0,
    currentZoom keeps its existing value, and currentZoomChanged is not emitted.

    The \a location should be the same as QPdfLink::location() if the user is
    following a link; and since that is specified as the upper-left corner of
    the destination, it is best for consistency to always use the location
    visible in the upper-left corner of the viewport, in points.

    If forwardAvailable is \c true, calling this function represents a branch
    in the timeline which causes the "future" to be lost, and therefore
    forwardAvailable will change to \c false.
*/
void QPdfPageNavigator::jump(int page, const QPointF &location, qreal zoom)
{
    if (page == currentPage() && location == currentLocation() && zoom == currentZoom())
        return;
    if (qFuzzyIsNull(zoom))
        zoom = currentZoom();
    const bool zoomChange = !qFuzzyCompare(zoom, currentZoom());
    const bool pageChange = (page != currentPage());
    const bool locationChange = (location != currentLocation());
    const bool backAvailableWas = backAvailable();
    const bool forwardAvailableWas = forwardAvailable();
    if (!d->changing) {
        if (d->currentHistoryIndex >= 0 && forwardAvailableWas)
            d->pageHistory.remove(d->currentHistoryIndex + 1, d->pageHistory.size() - d->currentHistoryIndex - 1);
        d->pageHistory.append(QExplicitlySharedDataPointer<QPdfLinkPrivate>(new QPdfLinkPrivate(page, location, zoom)));
        d->currentHistoryIndex = d->pageHistory.size() - 1;
    }
    if (zoomChange)
        emit currentZoomChanged(currentZoom());
    if (pageChange)
        emit currentPageChanged(currentPage());
    if (locationChange)
        emit currentLocationChanged(currentLocation());
    if (d->changing)
        return;
    if (backAvailableWas != backAvailable())
        emit backAvailableChanged(backAvailable());
    if (forwardAvailableWas != forwardAvailable())
        emit forwardAvailableChanged(forwardAvailable());
    emit jumped(currentLink());
    qCDebug(qLcNav) << "push: index" << d->currentHistoryIndex << "page" << page
                    << "@" << location << "zoom" << zoom << "-> history" <<
        [this]() {
            QStringList ret;
            for (auto d : d->pageHistory)
                ret << QString::number(d->page);
            return ret.join(QLatin1Char(','));
        }();
}

/*!
    Modifies the current destination, consisting of \a page, \a location and \a zoom.

    This can be called periodically while the user is manually moving around
    the document, so that after back() is called, forward() will jump back to
    the most-recently-viewed destination rather than the destination that was
    last specified by push().

    The \c currentZoomChanged, \c currentPageChanged and \c currentLocationChanged
    signals will be emitted if the respective properties are actually changed.
    The \l jumped signal is not emitted, because this operation represents
    smooth movement rather than a navigational jump.
*/
void QPdfPageNavigator::update(int page, const QPointF &location, qreal zoom)
{
    if (d->currentHistoryIndex < 0 || d->currentHistoryIndex >= d->pageHistory.size())
        return;
    int currentPageWas = currentPage();
    QPointF currentLocationWas = currentLocation();
    qreal currentZoomWas = currentZoom();
    if (page == currentPageWas && location == currentLocationWas && zoom == currentZoomWas)
        return;
    d->pageHistory[d->currentHistoryIndex]->page = page;
    d->pageHistory[d->currentHistoryIndex]->location = location;
    d->pageHistory[d->currentHistoryIndex]->zoom = zoom;
    if (currentZoomWas != zoom)
        emit currentZoomChanged(currentZoom());
    if (currentPageWas != page)
        emit currentPageChanged(currentPage());
    if (currentLocationWas != location)
        emit currentLocationChanged(currentLocation());
    qCDebug(qLcNav) << "update: index" << d->currentHistoryIndex << "page" << page
                    << "@" << location << "zoom" << zoom << "-> history" <<
        [this]() {
            QStringList ret;
            for (auto d : d->pageHistory)
                ret << QString::number(d->page);
            return ret.join(QLatin1Char(','));
        }();
}

/*!
    \property QPdfPageNavigator::backAvailable
    \readonly

    Holds \c true if a \e back destination is available in the history:
    that is, if push() or forward() has been called.
*/
bool QPdfPageNavigator::backAvailable() const
{
    return d->currentHistoryIndex > 0;
}

/*!
    \property QPdfPageNavigator::forwardAvailable
    \readonly

    Holds \c true if a \e forward destination is available in the history:
    that is, if back() has been previously called.
*/
bool QPdfPageNavigator::forwardAvailable() const
{
    return d->currentHistoryIndex < d->pageHistory.size() - 1;
}

/*!
    \fn void QPdfPageNavigator::jumped(QPdfLink current)

    This signal is emitted when an abrupt jump occurs, to the \a current
    page index, location on the page, and zoom level; but \e not when simply
    scrolling through the document one page at a time. That is, jump(),
    forward() and back() emit this signal, but update() does not.

    If \c {current.rectangles.length > 0}, they are rectangles that cover
    a specific destination area: a search result that should be made
    visible; otherwise, \c {current.location} is the destination location on
    the \c page (a hyperlink destination, or during forward/back navigation).
*/

QT_END_NAMESPACE

#include "moc_qpdfpagenavigator.cpp"