summaryrefslogtreecommitdiffstats
path: root/src/pdfwidgets/qpdfpageselector.cpp
blob: 66b781ed38780f8003ac2b602e0c7defed619708 (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
// 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>

#include <QtWidgets/qboxlayout.h>

using namespace Qt::StringLiterals;

QT_BEGIN_NAMESPACE

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

    QPdfPageSelector is a widget 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)
    : QWidget(parent), d_ptr(new QPdfPageSelectorPrivate)
{
    Q_D(QPdfPageSelector);
    d->spinBox = new QPdfPageSelectorSpinBox(this);
    d->spinBox->setObjectName(u"_q_spinBox"_s);
    auto vlay = new QVBoxLayout(this);
    vlay->setContentsMargins({});
    vlay->addWidget(d->spinBox);

    connect(d->spinBox, &QPdfPageSelectorSpinBox::_q_documentChanged,
            this, &QPdfPageSelector::documentChanged);
    connect(d->spinBox, &QSpinBox::valueChanged, this, &QPdfPageSelector::currentPageChanged);
    connect(d->spinBox, &QSpinBox::textChanged, this, &QPdfPageSelector::currentPageLabelChanged);
}

/*!
    Destroys the page selector.
*/
QPdfPageSelector::~QPdfPageSelector()
    = default;

/*!
    \property QPdfPageSelector::document

    This property holds the document to be viewed.
*/

void QPdfPageSelector::setDocument(QPdfDocument *doc)
{
    Q_D(QPdfPageSelector);
    d->spinBox->setDocument(doc);
}

QPdfDocument *QPdfPageSelector::document() const
{
    Q_D(const QPdfPageSelector);
    return d->spinBox->document();
}

/*!
    \property QPdfPageSelector::currentPage

    This property holds the index (\c{0}-based) of the current page in the
    document.
*/

int QPdfPageSelector::currentPage() const
{
    Q_D(const QPdfPageSelector);
    return d->spinBox->value();
}

void QPdfPageSelector::setCurrentPage(int index)
{
    Q_D(QPdfPageSelector);
    d->spinBox->setValue(index);
}

/*!
    \property QPdfPageSelector::currentPageLabel

    This property holds the page label corresponding to the current page
    in the document.

    This is the text presented to the user.

    \sa QPdfDocument::pageLabel()
*/

QString QPdfPageSelector::currentPageLabel() const
{
    Q_D(const QPdfPageSelector);
    return d->spinBox->text();
}

//
// QPdfPageSelectorSpinBox:
//

void QPdfPageSelectorSpinBox::documentStatusChanged()
{
    if (m_document && m_document->status() == QPdfDocument::Status::Ready) {
        setMaximum(m_document->pageCount());
        setValue(0);
    }
}

void QPdfPageSelectorSpinBox::setDocument(QPdfDocument *document)
{
    if (m_document == document)
        return;

    if (m_document)
        disconnect(m_documentStatusChangedConnection);

    m_document = document;
    emit _q_documentChanged(document);

    if (m_document) {
        m_documentStatusChangedConnection =
                connect(m_document.get(), &QPdfDocument::statusChanged,
                        this, &QPdfPageSelectorSpinBox::documentStatusChanged);
    }

    documentStatusChanged();
}

QPdfPageSelectorSpinBox::QPdfPageSelectorSpinBox(QWidget *parent)
    : QSpinBox(parent)
{
}

QPdfPageSelectorSpinBox::~QPdfPageSelectorSpinBox()
    = default;

int QPdfPageSelectorSpinBox::valueFromText(const QString &text) const
{
    if (!m_document)
        return 0;

    return m_document->pageIndexForLabel(text.trimmed());
}

QString QPdfPageSelectorSpinBox::textFromValue(int value) const
{
    if (!m_document)
        return {};

    return m_document->pageLabel(value);
}

QValidator::State QPdfPageSelectorSpinBox::validate(QString &text, int &pos) const
{
    Q_UNUSED(pos);
    return valueFromText(text) >= 0 ? QValidator::Acceptable : QValidator::Intermediate;
}

QT_END_NAMESPACE

#include "moc_qpdfpageselector_p.cpp"
#include "moc_qpdfpageselector.cpp"