From 37403a031ae8c3d83b0ecfc19150fd53b549b6b5 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sun, 17 May 2009 18:08:43 +0000 Subject: [PATCH] Small rework on the system used to pass parameters of the document: - on command line, properly read the URL ref ('#foobar' at the end), avoiding a KCmdLineArgs behaviour which encodes it as part of the file name - pass what was specified as ref or the page (as specified with -p/--page) to the kpart - make the okularpart take out the ref from the URL it opens and then: a) if it is a number, assume it is the destination page number b) otherwise, use that string as named destination CCMAIL: okular-devel@kde.org svn path=/trunk/KDE/kdegraphics/okular/; revision=969227 --- part.cpp | 24 +++++++++++++++++++++++- shell/main.cpp | 2 +- shell/shell.cpp | 37 ++++++++++++++++++++++++++----------- shell/shell.h | 4 ++-- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/part.cpp b/part.cpp index 4b1d9c13a..fc6de751f 100644 --- a/part.cpp +++ b/part.cpp @@ -994,8 +994,30 @@ bool Part::openFile() return true; } -bool Part::openUrl(const KUrl &url) +bool Part::openUrl(const KUrl &_url) { + KUrl url( _url ); + if ( url.hasHTMLRef() ) + { + const QString dest = url.htmlRef(); + bool ok = true; + const int page = dest.toInt( &ok ); + if ( ok ) + { + Okular::DocumentViewport vp( page - 1 ); + vp.rePos.enabled = true; + vp.rePos.normalizedX = 0; + vp.rePos.normalizedY = 0; + vp.rePos.pos = Okular::DocumentViewport::TopLeft; + m_document->setNextDocumentViewport( vp ); + } + else + { + m_document->setNextDocumentDestination( dest ); + } + url.setHTMLRef( QString() ); + } + // this calls in sequence the 'closeUrl' and 'openFile' methods bool openOk = KParts::ReadOnlyPart::openUrl( url ); diff --git a/shell/main.cpp b/shell/main.cpp index 25bb6c944..cc1ca36a3 100644 --- a/shell/main.cpp +++ b/shell/main.cpp @@ -72,7 +72,7 @@ int main(int argc, char** argv) { for (int i = 0; i < args->count(); ++i) { - Shell* widget = new Shell(args, args->url(i)); + Shell* widget = new Shell(args, i); widget->show(); } } diff --git a/shell/shell.cpp b/shell/shell.cpp index 89489fd04..f123e098c 100644 --- a/shell/shell.cpp +++ b/shell/shell.cpp @@ -46,10 +46,31 @@ // local includes #include "kdocumentviewer.h" -Shell::Shell(KCmdLineArgs* args, const KUrl &url) +Shell::Shell(KCmdLineArgs* args, int argIndex) : KParts::MainWindow(), m_args(args), m_menuBarWasShown(true), m_toolBarWasShown(true) { - m_openUrl = url; + if (m_args && argIndex != -1) + { + /* + Rationale for the small "cut-and-paste" work being done below: + KCmdLineArgs::makeURL() (used by ::url() encodes any # into the URL itself, + so we have to find it manually and build up the URL by taking its ref, + if any. + */ + KUrl url = m_args->url(argIndex); + const QString arg = m_args->arg(argIndex); + const int sharpPos = arg.lastIndexOf(QLatin1Char('#')); + if (sharpPos != -1) + { + url = KCmdLineArgs::makeURL(arg.left(sharpPos).toUtf8()); + url.setHTMLRef(arg.mid(sharpPos + 1)); + } + else if (!m_args->getOption("page").isEmpty()) + { + url.setHTMLRef(m_args->getOption("page")); + } + m_openUrl = url; + } init(); } @@ -105,13 +126,7 @@ void Shell::init() void Shell::delayedOpen() { - uint page = 0; - if (m_args && m_doc) - { - QString pageopt = m_args->getOption("page"); - page = pageopt.toUInt(); - } - openUrl(m_openUrl, page); + openUrl( m_openUrl ); } Shell::~Shell() @@ -122,13 +137,13 @@ Shell::~Shell() m_args->clear(); } -void Shell::openUrl( const KUrl & url, uint page ) +void Shell::openUrl( const KUrl & url ) { if ( m_part ) { if ( m_doc && m_args && m_args->isSet( "presentation" ) ) m_doc->startPresentation(); - bool openOk = page > 0 && m_doc ? m_doc->openDocument( url, page ) : m_part->openUrl( url ); + bool openOk = m_part->openUrl( url ); const bool isstdin = url.fileName( KUrl::ObeyTrailingSlash ) == QLatin1String( "-" ); if ( !isstdin ) { diff --git a/shell/shell.h b/shell/shell.h index 628aff1c0..850e7e37f 100644 --- a/shell/shell.h +++ b/shell/shell.h @@ -41,7 +41,7 @@ public: /** * Constructor */ - explicit Shell(KCmdLineArgs* args = 0, const KUrl &url = KUrl()); + explicit Shell(KCmdLineArgs* args = 0, int argIndex = -1); /** * Default Destructor @@ -77,7 +77,7 @@ private slots: void slotUpdateFullScreen(); void slotShowMenubar(); - void openUrl( const KUrl & url, uint page = 0 ); + void openUrl( const KUrl & url ); void delayedOpen(); signals: