From 3e3a504d3c64b5e89a810a922cc5daff268b34c6 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Thu, 8 Mar 2012 23:26:44 +0100 Subject: [PATCH] Allow the renaming of bookmark "parent" items (i.e. file names) Patch heavily inspired on a patch by Constantin Serban-Radoi --- core/bookmarkmanager.cpp | 24 ++++++++++++++++++++++++ core/bookmarkmanager.h | 13 +++++++++++++ ui/bookmarklist.cpp | 27 +++++++++++++++++++-------- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/core/bookmarkmanager.cpp b/core/bookmarkmanager.cpp index 07c9dbf09..2d9c9c013 100644 --- a/core/bookmarkmanager.cpp +++ b/core/bookmarkmanager.cpp @@ -362,6 +362,30 @@ void BookmarkManager::renameBookmark( KBookmark* bm, const QString& newName) d->manager->emitChanged( thebg ); } +void BookmarkManager::renameBookmark( const KUrl& referurl, const QString& newName ) +{ + if ( !referurl.isValid() ) + return; + + KBookmarkGroup thebg; + QHash::iterator it = d->bookmarkFind( referurl, false, &thebg ); + Q_ASSERT ( it != d->knownFiles.end() ); + if ( it == d->knownFiles.end() ) + return; + + thebg.setFullText( newName ); + d->manager->emitChanged( thebg ); +} + +QString BookmarkManager::titleForUrl( const KUrl& referurl ) const +{ + KBookmarkGroup thebg; + QHash::iterator it = d->bookmarkFind( referurl, false, &thebg ); + Q_ASSERT( it != d->knownFiles.end() ); + + return thebg.fullText(); +} + int BookmarkManager::removeBookmark( const KUrl& referurl, const KBookmark& bm ) { if ( !referurl.isValid() || bm.isNull() || bm.isGroup() || bm.isSeparator() ) diff --git a/core/bookmarkmanager.h b/core/bookmarkmanager.h index 03bd1eb9c..21bf34665 100644 --- a/core/bookmarkmanager.h +++ b/core/bookmarkmanager.h @@ -101,6 +101,19 @@ class OKULAR_EXPORT BookmarkManager : public QObject */ void renameBookmark( KBookmark* bm, const QString& newName ); + /** + * Renames the top-level bookmark for the @p referurl specified with + * the @p newName specified. + * @since 0.15 (KDE 4.9) + */ + void renameBookmark( const KUrl& referurl, const QString& newName ); + + /** + * Returns title for the @p referurl + * @since 0.15 (KDE 4.9) + */ + QString titleForUrl( const KUrl& referurl ) const; + /** * Returns whether the given @p page is bookmarked. */ diff --git a/ui/bookmarklist.cpp b/ui/bookmarklist.cpp index 4e4c7327d..034afa4a4 100644 --- a/ui/bookmarklist.cpp +++ b/ui/bookmarklist.cpp @@ -94,10 +94,11 @@ class BookmarkItem : public QTreeWidgetItem class FileItem : public QTreeWidgetItem { public: - FileItem( const KUrl & url, QTreeWidget *tree ) + FileItem( const KUrl & url, QTreeWidget *tree, Okular::Document *document ) : QTreeWidgetItem( tree, FileItemType ) { - const QString fileString = url.isLocalFile() ? url.toLocalFile() : url.prettyUrl(); + setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable ); + const QString fileString = document->bookmarkManager()->titleForUrl( url ); setText( 0, fileString ); setData( 0, UrlRole, qVariantFromValue( url ) ); } @@ -219,11 +220,18 @@ void BookmarkList::slotExecuted( QTreeWidgetItem * item ) void BookmarkList::slotChanged( QTreeWidgetItem * item ) { BookmarkItem* bmItem = dynamic_cast( item ); - if ( !bmItem || !bmItem->viewport().isValid() ) - return; + if ( bmItem && bmItem->viewport().isValid() ) + { + bmItem->bookmark().setFullText( bmItem->text( 0 ) ); + m_document->bookmarkManager()->save(); + } - bmItem->bookmark().setFullText( bmItem->text( 0 ) ); - m_document->bookmarkManager()->save(); + FileItem* fItem = dynamic_cast( item ); + if ( fItem ) + { + m_document->bookmarkManager()->renameBookmark( m_document->currentDocument(), fItem->text( 0 ) ); + m_document->bookmarkManager()->save(); + } } void BookmarkList::slotContextMenu( const QPoint& p ) @@ -271,6 +279,7 @@ void BookmarkList::contextMenuForFileItem( const QPoint& p, FileItem* fItem ) QAction * open = 0; if ( !thisdoc ) open = menu.addAction( i18nc( "Opens the selected document", "Open Document" ) ); + QAction * editbm = menu.addAction( KIcon( "edit-rename" ), i18n( "Rename Bookmark" ) ); QAction * removebm = menu.addAction( KIcon( "list-remove" ), i18n( "Remove Bookmarks" ) ); QAction * res = menu.exec( QCursor::pos() ); if ( !res ) @@ -281,6 +290,8 @@ void BookmarkList::contextMenuForFileItem( const QPoint& p, FileItem* fItem ) Okular::GotoAction action( itemurl.pathOrUrl(), Okular::DocumentViewport() ); m_document->processAction( &action ); } + else if ( res == editbm ) + m_tree->editItem( fItem, 0 ); else if ( res == removebm ) { KBookmark::List list; @@ -358,7 +369,7 @@ void BookmarkList::rebuildTree( bool filter ) QList subitems = createItems( url, m_document->bookmarkManager()->bookmarks( url ) ); if ( !subitems.isEmpty() ) { - FileItem * item = new FileItem( url, m_tree ); + FileItem * item = new FileItem( url, m_tree, m_document ); item->addChildren( subitems ); if ( !currenturlitem && url == m_document->currentDocument() ) { @@ -426,7 +437,7 @@ void BookmarkList::selectiveUrlUpdate( const KUrl& url, QTreeWidgetItem*& item ) } else { - item = new FileItem( url, m_tree ); + item = new FileItem( url, m_tree, m_document ); fileitem_created = true; } if ( m_document->isOpened() && url == m_document->currentDocument() )