summaryrefslogtreecommitdiffstats
path: root/src/pdfwidgets/qpdfpageselector.cpp
blob: 72ab283555a2a24da53bfa68e45ba1eb6dd26e04 (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
// Copyright (C) 2023 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 "qpdfpageselector.h"
#include "qpdfpageselector_p.h"

#include <QPdfDocument>

QT_BEGIN_NAMESPACE

QPdfPageSelectorPrivate::QPdfPageSelectorPrivate(QPdfPageSelector *q)
    : q_ptr(q)
{
}

void QPdfPageSelectorPrivate::documentStatusChanged()
{
    Q_Q(QPdfPageSelector);
    if (!document.isNull() && document->status() == QPdfDocument::Status::Ready) {
        q->setMaximum(document->pageCount());
        q->setValue(0);
    }
}

/*!
    \class QPdfPageSelector
    \inmodule QtPdf
    \brief A QSpinBox for selecting a PDF page.

    QPdfPageSelector is a QSpinBox specialized for selecting a page label
    from a QPdfDocument.

    \sa QPdfDocument::pageLabel()
*/

/*!
    Constructs a PDF page selector with parent widget \a parent.
*/
QPdfPageSelector::QPdfPageSelector(QWidget *parent)
    : QSpinBox(parent)
    , d_ptr(new QPdfPageSelectorPrivate(this))
{
}

/*!
    Destroys the page selector.
*/
QPdfPageSelector::~QPdfPageSelector()
{
}

/*!
    \property QPdfPageSelector::document

    This property holds the document to be viewed.
*/
void QPdfPageSelector::setDocument(QPdfDocument *document)
{
    Q_D(QPdfPageSelector);

    if (d->document == document)
        return;

    if (d->document)
        disconnect(d->documentStatusChangedConnection);

    d->document = document;
    emit documentChanged(d->document);

    if (d->document)
        d->documentStatusChangedConnection =
                connect(d->document.data(), &QPdfDocument::statusChanged, this,
                        [d](){ d->documentStatusChanged(); });

    d->documentStatusChanged();
}

QPdfDocument *QPdfPageSelector::document() const
{
    Q_D(const QPdfPageSelector);

    return d->document;
}

int QPdfPageSelector::valueFromText(const QString &text) const
{
    Q_D(const QPdfPageSelector);
    if (d->document.isNull())
        return 0;

    return d->document->pageIndexForLabel(text);
}

QString QPdfPageSelector::textFromValue(int value) const
{
    Q_D(const QPdfPageSelector);
    if (d->document.isNull())
        return {};

    return d->document->pageLabel(value);
}

QT_END_NAMESPACE

#include "moc_qpdfpageselector.cpp"