Skip to content

Commit

Permalink
[office] Add internal page shifts for goto links in PDF documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcaliste committed Sep 20, 2015
1 parent ef16ac4 commit ec0c503
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pdf/pdfcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ void PDFCanvas::setDocument(PDFDocument* doc)
}
}

qreal PDFCanvas::pagePosition(int index) const
QRectF PDFCanvas::pageRectangle(int index) const
{
if( d->pages.count() == 0 )
return 0.f;
return QRectF();

return d->pages.value( index ).rect.y();
return d->pages.value( index ).rect;
}

int PDFCanvas::currentPage() const
Expand Down
2 changes: 1 addition & 1 deletion pdf/pdfcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PDFCanvas : public QQuickItem
PDFCanvas(QQuickItem* parent = 0);
~PDFCanvas();

Q_INVOKABLE qreal pagePosition( int index ) const;
Q_INVOKABLE QRectF pageRectangle( int index ) const;

QQuickItem *flickable() const;
void setFlickable(QQuickItem *f);
Expand Down
7 changes: 6 additions & 1 deletion pdf/pdflinkarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ void PDFLinkArea::mouseReleaseEvent(QMouseEvent* event)
} else if (url.isRelative() && url.hasQuery()) {
QUrlQuery query = QUrlQuery(url);
if (query.hasQueryItem("page")) {
emit gotoClicked(query.queryItemValue("page").toInt());
bool ok;
double top = query.queryItemValue("top").toFloat(&ok);
if (!ok) top = -1.;
double left = query.queryItemValue("left").toFloat(&ok);
if (!ok) left = -1.;
emit gotoClicked(query.queryItemValue("page").toInt(), top, left);
} else {
emit clicked();
}
Expand Down
2 changes: 1 addition & 1 deletion pdf/pdflinkarea.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PDFLinkArea : public QQuickItem
void clicked();
void doubleClicked();
void linkClicked(QUrl linkTarget);
void gotoClicked(int page);
void gotoClicked(int page, qreal top, qreal left);

void canvasChanged();

Expand Down
14 changes: 12 additions & 2 deletions pdf/pdfrenderthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QMutex>
#include <QDebug>
#include <QCoreApplication>
#include <QUrlQuery>

#include <poppler-qt5.h>

Expand Down Expand Up @@ -105,8 +106,17 @@ class PDFRenderThreadPrivate
if (gotoLink->isExternal())
break;
QRectF linkArea = link->linkArea();
QString linkPage = QString("?page=%1").arg(gotoLink->destination().pageNumber());
linkTargets.insert( i, QPair< QRectF, QUrl >{ linkArea, linkPage } );
QUrl linkURL = QUrl("?");
QUrlQuery query = QUrlQuery();
query.addQueryItem("page", QString::number(gotoLink->destination().pageNumber()));
if (gotoLink->destination().isChangeLeft()) {
query.addQueryItem("left", QString::number(gotoLink->destination().left()));
}
if (gotoLink->destination().isChangeTop()) {
query.addQueryItem("top", QString::number(gotoLink->destination().top()));
}
linkURL.setQuery(query);
linkTargets.insert( i, QPair< QRectF, QUrl >{ linkArea, linkURL } );
break;
}
default:
Expand Down
13 changes: 10 additions & 3 deletions plugin/PDFView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ SilicaFlickable {

canvas: pdfCanvas;
onLinkClicked: Qt.openUrlExternally(linkTarget);
onGotoClicked: base.goToPage(page - 1)
onGotoClicked: base.goToPage(page - 1, top, left,
Theme.paddingLarge, Theme.paddingLarge)
onClicked: base.clicked();
}
}
Expand Down Expand Up @@ -179,7 +180,13 @@ SilicaFlickable {
VerticalScrollDecorator { color: Theme.highlightDimmerColor; }
]

function goToPage(pageNumber) {
base.contentY = pdfCanvas.pagePosition( pageNumber );
function goToPage(pageNumber, top, left, topSpacing, leftSpacing) {
var rect = pdfCanvas.pageRectangle( pageNumber );
// Adjust horizontal position if required.
if (left !== undefined && left >= 0.) {
base.contentX = rect.x + left * rect.width - ( leftSpacing !== undefined ? leftSpacing : 0.)
}
// Adjust vertical position.
base.contentY = rect.y + (top === undefined ? 0. : top * rect.height) - ( topSpacing !== undefined ? topSpacing : 0.);
}
}

0 comments on commit ec0c503

Please sign in to comment.