Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpdfpageselector.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6
7#include <QPdfDocument>
8
9#include <QtWidgets/qboxlayout.h>
10
11using namespace Qt::StringLiterals;
12
13QT_BEGIN_NAMESPACE
14
15/*!
16 \class QPdfPageSelector
17 \inmodule QtPdf
18 \since 6.6
19 \brief A widget for selecting a PDF page.
20
21 QPdfPageSelector is a widget for selecting a page label from a
22 QPdfDocument.
23
24 \sa QPdfDocument::pageLabel()
25*/
26
27/*!
28 Constructs a PDF page selector with parent widget \a parent.
29*/
30QPdfPageSelector::QPdfPageSelector(QWidget *parent)
31 : QWidget(parent), d_ptr(new QPdfPageSelectorPrivate)
32{
33 Q_D(QPdfPageSelector);
34 d->spinBox = new QPdfPageSelectorSpinBox(this);
35 d->spinBox->setObjectName(u"_q_spinBox"_s);
36 auto vlay = new QVBoxLayout(this);
37 vlay->setContentsMargins({});
38 vlay->addWidget(d->spinBox);
39
40 connect(d->spinBox, &QPdfPageSelectorSpinBox::_q_documentChanged,
41 this, &QPdfPageSelector::documentChanged);
42 connect(d->spinBox, &QSpinBox::valueChanged, this, &QPdfPageSelector::currentPageChanged);
43 connect(d->spinBox, &QSpinBox::textChanged, this, &QPdfPageSelector::currentPageLabelChanged);
44}
45
46/*!
47 Destroys the page selector.
48*/
49QPdfPageSelector::~QPdfPageSelector()
50 = default;
51
52/*!
53 \property QPdfPageSelector::document
54
55 This property holds the document to be viewed.
56*/
57
58void QPdfPageSelector::setDocument(QPdfDocument *doc)
59{
60 Q_D(QPdfPageSelector);
61 d->spinBox->setDocument(doc);
62}
63
64QPdfDocument *QPdfPageSelector::document() const
65{
66 Q_D(const QPdfPageSelector);
67 return d->spinBox->document();
68}
69
70/*!
71 \property QPdfPageSelector::currentPage
72
73 This property holds the index (\c{0}-based) of the current page in the
74 document.
75*/
76
77int QPdfPageSelector::currentPage() const
78{
79 Q_D(const QPdfPageSelector);
80 return d->spinBox->value();
81}
82
83void QPdfPageSelector::setCurrentPage(int index)
84{
85 Q_D(QPdfPageSelector);
86 d->spinBox->setValue(index);
87}
88
89/*!
90 \property QPdfPageSelector::currentPageLabel
91
92 This property holds the page label corresponding to the current page
93 in the document.
94
95 This is the text presented to the user.
96
97 \sa QPdfDocument::pageLabel()
98*/
99
100QString QPdfPageSelector::currentPageLabel() const
101{
102 Q_D(const QPdfPageSelector);
103 return d->spinBox->text();
104}
105
106//
107// QPdfPageSelectorSpinBox:
108//
109
110void QPdfPageSelectorSpinBox::documentStatusChanged()
111{
112 if (m_document && m_document->status() == QPdfDocument::Status::Ready) {
113 setMaximum(m_document->pageCount());
114 setValue(0);
115 }
116}
117
118void QPdfPageSelectorSpinBox::setDocument(QPdfDocument *document)
119{
120 if (m_document == document)
121 return;
122
123 if (m_document)
124 disconnect(m_documentStatusChangedConnection);
125
126 m_document = document;
127 emit _q_documentChanged(document);
128
129 if (m_document) {
130 m_documentStatusChangedConnection =
131 connect(m_document.get(), &QPdfDocument::statusChanged,
132 this, &QPdfPageSelectorSpinBox::documentStatusChanged);
133 }
134
135 documentStatusChanged();
136}
137
138QPdfPageSelectorSpinBox::QPdfPageSelectorSpinBox(QWidget *parent)
139 : QSpinBox(parent)
140{
141}
142
144 = default;
145
146int QPdfPageSelectorSpinBox::valueFromText(const QString &text) const
147{
148 if (!m_document)
149 return 0;
150
151 return m_document->pageIndexForLabel(text.trimmed());
152}
153
155{
156 if (!m_document)
157 return {};
158
159 return m_document->pageLabel(value);
160}
161
162QValidator::State QPdfPageSelectorSpinBox::validate(QString &text, int &pos) const
163{
164 Q_UNUSED(pos);
165 return valueFromText(text) >= 0 ? QValidator::Acceptable : QValidator::Intermediate;
166}
167
168QT_END_NAMESPACE
169
170#include "moc_qpdfpageselector_p.cpp"
171#include "moc_qpdfpageselector.cpp"
QValidator::State validate(QString &text, int &pos) const override
This virtual function is called by the QAbstractSpinBox to determine whether input is valid.
int valueFromText(const QString &text) const override
This virtual function is used by the spin box whenever it needs to interpret text entered by the user...
void setDocument(QPdfDocument *document)
QString textFromValue(int value) const override
This virtual function is used by the spin box whenever it needs to display the given value.