From 8a7c2ad6540bfa679f4a64df670f318b99bce5b9 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sat, 13 Jan 2007 17:28:54 +0000 Subject: [PATCH] As requested by our usability expert Florian, add a Bookmarks menu with the bookmarks in the current document, if any. svn path=/trunk/playground/graphics/okular/; revision=622995 --- core/bookmarkmanager.cpp | 22 ++++++++++++++ core/bookmarkmanager.h | 9 ++++++ core/document.cpp | 1 - part.cpp | 65 ++++++++++++++++++++++++++++++++++++++++ part.h | 5 ++++ part.rc | 5 +++- 6 files changed, 105 insertions(+), 2 deletions(-) diff --git a/core/bookmarkmanager.cpp b/core/bookmarkmanager.cpp index 503721aa6..0211647f6 100644 --- a/core/bookmarkmanager.cpp +++ b/core/bookmarkmanager.cpp @@ -222,6 +222,28 @@ int BookmarkManager::removeBookmark( const KUrl& referurl, const KBookmark& bm ) return vp.pageNumber; } +QList< QAction * > BookmarkManager::actionsForUrl( const KUrl& url ) const +{ + QList< QAction * > ret; + KBookmarkGroup group = d->manager->root(); + for ( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) ) + { + if ( !bm.isGroup() || KUrl( bm.fullText() ) != url ) + continue; + + KBookmarkGroup group = bm.toGroup(); + for ( KBookmark b = group.first(); !b.isNull(); b = group.next( b ) ) + { + if ( b.isSeparator() || b.isGroup() ) + continue; + + ret.append( new KBookmarkAction( b, d, 0 ) ); + } + break; + } + return ret; +} + void BookmarkManager::setUrl( const KUrl& url ) { d->url = url; diff --git a/core/bookmarkmanager.h b/core/bookmarkmanager.h index 00a688f43..61671006f 100644 --- a/core/bookmarkmanager.h +++ b/core/bookmarkmanager.h @@ -14,6 +14,7 @@ #include "okular_export.h" +class QAction; class KUrl; namespace Okular { @@ -55,6 +56,14 @@ class OKULAR_EXPORT BookmarkManager : public QObject */ int removeBookmark( const KUrl& referurl, const KBookmark& bm ); + /** + * Returns a list of actions for the bookmarks of the specified @p url. + * + * @note the actions will have no parents, so you have to delete them + * yourself + */ + QList< QAction* > actionsForUrl( const KUrl& url ) const; + Q_SIGNALS: /** * The bookmark manager is requesting to open the specified @p url. diff --git a/core/document.cpp b/core/document.cpp index 52a4bbc7b..01df08844 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -575,7 +575,6 @@ Document::Document( QHash * generators ) : d( new Private( this, generators ) ) { d->m_bookmarkManager = new BookmarkManager( this ); - connect( d->m_bookmarkManager, SIGNAL( openUrl( const KUrl & ) ), this, SIGNAL( openUrl( const KUrl & ) ) ); } Document::~Document() diff --git a/part.cpp b/part.cpp index 1f1307cbe..da9e9dbd7 100644 --- a/part.cpp +++ b/part.cpp @@ -71,6 +71,7 @@ #include "ui/bookmarklist.h" #include "conf/preferencesdialog.h" #include "settings.h" +#include "core/bookmarkmanager.h" #include "core/document.h" #include "core/generator.h" #include "core/page.h" @@ -108,6 +109,7 @@ m_searchStarted(false), m_cliPresentation(false) connect( m_document, SIGNAL( linkPresentation() ), this, SLOT( slotShowPresentation() ) ); connect( m_document, SIGNAL( linkEndPresentation() ), this, SLOT( slotHidePresentation() ) ); connect( m_document, SIGNAL( openUrl(const KUrl &) ), this, SLOT( openUrlFromDocument(const KUrl &) ) ); + connect( m_document->bookmarkManager(), SIGNAL( openUrl(const KUrl &) ), this, SLOT( openUrlFromBookmarks(const KUrl &) ) ); connect( m_document, SIGNAL( close() ), this, SLOT( close() ) ); if ( parent && parent->metaObject()->indexOfSlot( SLOT( slotQuit() ) ) != -1 ) @@ -403,6 +405,8 @@ m_searchStarted(false), m_cliPresentation(false) Okular::Settings::setUseKTTSD( !offers.isEmpty() ); Okular::Settings::writeConfig(); + rebuildBookmarkMenu( false ); + // set our XML-UI resource file setXMLFile("part.rc"); // @@ -426,6 +430,8 @@ Part::~Part() if (m_tempfile) delete m_tempfile; + + qDeleteAll( m_bookmarkActions ); } @@ -451,6 +457,22 @@ void Part::openUrlFromDocument(const KUrl &url) openUrl(url); } +void Part::openUrlFromBookmarks(const KUrl &_url) +{ + KUrl url = _url; + Okular::DocumentViewport vp( _url.htmlRef() ); + if ( vp.isValid() ) + m_document->setNextDocumentViewport( vp ); + url.setHTMLRef( QString() ); + if ( m_document->currentDocument() == url ) + { + if ( vp.isValid() ) + m_document->setViewport( vp ); + } + else + openUrl( url ); +} + void Part::supportedMimetypes() { @@ -555,6 +577,14 @@ void Part::slotGeneratorPreferences( ) } +void Part::notifySetup( const QVector< Okular::Page * > & /*pages*/, bool documentChanged ) +{ + if ( !documentChanged ) + return; + + rebuildBookmarkMenu(); +} + void Part::notifyViewportChanged( bool /*smoothMove*/ ) { // update actions if the page is changed @@ -567,6 +597,14 @@ void Part::notifyViewportChanged( bool /*smoothMove*/ ) } } +void Part::notifyPageChanged( int /*page*/, int flags ) +{ + if ( !(flags & Okular::DocumentObserver::Bookmark ) ) + return; + + rebuildBookmarkMenu(); +} + void Part::goToPage(uint i) { @@ -1543,6 +1581,33 @@ bool Part::handleCompressed(KUrl & url, const QString &path, const KMimeType::Pt return true; } +void Part::rebuildBookmarkMenu( bool unplugActions ) +{ + if ( unplugActions ) + { + unplugActionList( "bookmarks_currentdocument" ); + qDeleteAll( m_bookmarkActions ); + m_bookmarkActions.clear(); + } + KUrl u = m_document->currentDocument(); + if ( u.isValid() ) + { + m_bookmarkActions = m_document->bookmarkManager()->actionsForUrl( u ); + } + if ( m_bookmarkActions.isEmpty() ) + { + QAction * a = new QAction( 0 ); + a->setText( i18n( "No Bookmarks" ) ); + a->setEnabled( false ); + m_bookmarkActions.append( a ); + } + for ( int i = 0; i < m_bookmarkActions.count(); ++i ) + { + actionCollection()->addAction( QString( "bookmark_action_%1" ).arg( i ), m_bookmarkActions.at(i) ); + } + plugActionList( "bookmarks_currentdocument", m_bookmarkActions ); +} + /* * BrowserExtension class diff --git a/part.h b/part.h index 4bf6ee433..05c914600 100644 --- a/part.h +++ b/part.h @@ -85,7 +85,9 @@ class Part : public KParts::ReadOnlyPart, public Okular::DocumentObserver, publi // inherited from DocumentObserver uint observerId() const { return PART_ID; } + void notifySetup( const QVector< Okular::Page * > &pages, bool documentChanged ); void notifyViewportChanged( bool smoothMove ); + void notifyPageChanged( int page, int flags ); static KAboutData* createAboutData(); @@ -119,6 +121,7 @@ class Part : public KParts::ReadOnlyPart, public Okular::DocumentObserver, publi protected slots: // connected to actions void openUrlFromDocument(const KUrl &url); + void openUrlFromBookmarks(const KUrl &url); void slotGoToPage(); void slotHistoryBack(); void slotHistoryNext(); @@ -158,6 +161,7 @@ class Part : public KParts::ReadOnlyPart, public Okular::DocumentObserver, publi void doPrint( KPrinter& printer ); void fillGenerators(); bool handleCompressed(KUrl & url, const QString &path, const KMimeType::Ptr mimetype); + void rebuildBookmarkMenu( bool unplugActions = true ); KTemporaryFile *m_tempfile; // the document @@ -222,6 +226,7 @@ class Part : public KParts::ReadOnlyPart, public Okular::DocumentObserver, publi QStringList m_supportedMimeTypes; KSelectAction * m_confGens; QList m_exportFormats; + QList m_bookmarkActions; bool m_cliPresentation; private slots: diff --git a/part.rc b/part.rc index 8761dfbe9..22a7170f9 100644 --- a/part.rc +++ b/part.rc @@ -1,5 +1,5 @@ - + &File @@ -47,6 +47,9 @@ + &Bookmarks + + &Tools -- GitLab