diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2023-07-04 21:04:15 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-08-06 20:25:27 +0000 |
commit | fb47deb9203ca2d43c108075245945fd099be854 (patch) | |
tree | 507b674fb4137e04b9365cb1725ad88941906f71 | |
parent | c9367cb556449999fbfeb7c47e34369177438e6e (diff) |
Fix PDF page label search: don't trim in QPdfDocument::pageIndexForLabel()
In the 6.6 header review it was suggested that the caller should do the
trimming if necessary; avoiding it here is more efficient. So far the
only caller is QPdfPageSelector (although we may need to implement the
same feature in the Quick PDF examples at some point).
Typing page labels into QPdfPageSelector didn't work because
QPdfPageSelector::validate() was not overridden. Now we reuse
pageIndexForLabel() for that too: if the label is found, it's valid; if
the label is not found, we just assume the user is not done typing yet.
There's no QPdfDocument function to do a substring search for page
labels, or to return the whole list of them either. It seems they
can only be gotten one-at-a-time via the FPDF_GetPageLabel function.
Now it actually works in the widget-based example
qtwebengine/examples/pdfwidgets/pdfviewer/pdfviewer: if you load a
document that has some Roman-numeral-numbered preface pages and type
e.g. " iii" into the spinbox, it jumps to page iii.
Fixes: QTBUG-115476
Change-Id: I070338effee1bbf916581c5ba964367424b786b2
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit c439786616ade9436a599806e67199ffa93f9c91)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/pdf/qpdfdocument.cpp | 3 | ||||
-rw-r--r-- | src/pdfwidgets/qpdfpageselector.cpp | 8 | ||||
-rw-r--r-- | src/pdfwidgets/qpdfpageselector.h | 1 |
3 files changed, 9 insertions, 3 deletions
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp index 695b30c59..e2309be9c 100644 --- a/src/pdf/qpdfdocument.cpp +++ b/src/pdf/qpdfdocument.cpp @@ -831,9 +831,8 @@ QString QPdfDocument::pageLabel(int page) */ int QPdfDocument::pageIndexForLabel(const QString &label) { - const auto trimmed = label.trimmed(); for (int i = 0; i < d->pageCount; ++i) { - if (pageLabel(i) == trimmed) + if (pageLabel(i) == label) return i; } return -1; diff --git a/src/pdfwidgets/qpdfpageselector.cpp b/src/pdfwidgets/qpdfpageselector.cpp index 72ab28355..7d7e7b11e 100644 --- a/src/pdfwidgets/qpdfpageselector.cpp +++ b/src/pdfwidgets/qpdfpageselector.cpp @@ -88,7 +88,7 @@ int QPdfPageSelector::valueFromText(const QString &text) const if (d->document.isNull()) return 0; - return d->document->pageIndexForLabel(text); + return d->document->pageIndexForLabel(text.trimmed()); } QString QPdfPageSelector::textFromValue(int value) const @@ -100,6 +100,12 @@ QString QPdfPageSelector::textFromValue(int value) const return d->document->pageLabel(value); } +QValidator::State QPdfPageSelector::validate(QString &text, int &pos) const +{ + Q_UNUSED(pos); + return valueFromText(text) >= 0 ? QValidator::Acceptable : QValidator::Intermediate; +} + QT_END_NAMESPACE #include "moc_qpdfpageselector.cpp" diff --git a/src/pdfwidgets/qpdfpageselector.h b/src/pdfwidgets/qpdfpageselector.h index bc2e352f7..f529690e8 100644 --- a/src/pdfwidgets/qpdfpageselector.h +++ b/src/pdfwidgets/qpdfpageselector.h @@ -33,6 +33,7 @@ Q_SIGNALS: protected: int valueFromText(const QString &text) const override; QString textFromValue(int value) const override; + QValidator::State validate(QString &text, int &pos) const override; private: Q_DECLARE_PRIVATE(QPdfPageSelector) |