Support '#page=<pagenum>' fragment to open at page <pagenum>

Support the '#page=<pagenum>' fragment to specify which page
to open the document at, in addition to the existing '#<pagenum>'
syntax.

For PDF, the '#page=<pagenum>' fragment is specified in RFC 8118,
section 3.

BUG: 406831
This commit is contained in:
Michael Weghorn 2020-09-09 13:52:30 +02:00 committed by Albert Astals Cid
parent 0fed884424
commit b696d6dbfb
2 changed files with 32 additions and 1 deletions

View file

@ -95,6 +95,7 @@ private slots:
void testAdditionalActionTriggers();
void testTypewriterAnnotTool();
void testJumpToPage();
void testOpenAtPage();
void testForwardBackwardNavigation();
void testTabletProximityBehavior();
void testOpenPrintPreview();
@ -1794,6 +1795,26 @@ void PartTest::testJumpToPage()
QCOMPARE(part.m_pageView->verticalScrollBar()->value(), pageWithSpaceTop - 4);
}
void PartTest::testOpenAtPage()
{
const QString testFile = QStringLiteral(KDESRCDIR "data/simple-multipage.pdf");
QUrl url = QUrl::fromLocalFile(testFile);
Okular::Part part(nullptr, nullptr, QVariantList());
const uint targetPageNumA = 25;
const uint expectedPageA = targetPageNumA - 1;
url.setFragment(QString::number(targetPageNumA));
part.openUrl(url);
QCOMPARE(part.m_document->currentPage(), expectedPageA);
// 'page=<pagenum>' param as specified in RFC 3778
const uint targetPageNumB = 15;
const uint expectedPageB = targetPageNumB - 1;
url.setFragment("page=" + QString::number(targetPageNumB));
part.openUrl(url);
QCOMPARE(part.m_document->currentPage(), expectedPageB);
}
void PartTest::testForwardBackwardNavigation()
{
const QString testFile = QStringLiteral(KDESRCDIR "data/simple-multipage.pdf");

View file

@ -1670,7 +1670,17 @@ bool Part::openUrl(const QUrl &_url, bool swapInsteadOfOpening)
if (url.hasFragment()) {
const QString dest = url.fragment(QUrl::FullyDecoded);
bool ok = true;
const int page = dest.toInt(&ok);
int page = dest.toInt(&ok);
if (!ok) {
const QStringList parameters = dest.split(QChar('&'));
for (const QString &parameter : parameters) {
if (parameter.startsWith(QStringLiteral("page="), Qt::CaseInsensitive)) {
page = dest.midRef(5).toInt(&ok);
}
}
}
if (ok) {
Okular::DocumentViewport vp(page - 1);
vp.rePos.enabled = true;