Hi, I have finally made this to work as it should.
Brief description of what I did: I created a menu entry in contextMenuForFileItem to be able to edit the top-level bookmarks (fileItems). I made a function in BookmarkManager class to be able to rename a bookmark referenced by its KUrl, and used it in slotChanged. The greatest challenge was to get the *title* for the current item, and this is why I overloaded the currentTitle function. I am waiting for any feedback and I hope this patch can be pushed upstream. Yours sincerely, Constantin Serban-Radoi.
diff --git a/core/bookmarkmanager.cpp b/core/bookmarkmanager.cpp index 07c9dbf..f8fbb03 100644 --- a/core/bookmarkmanager.cpp +++ b/core/bookmarkmanager.cpp @@ -71,7 +71,7 @@ class BookmarkManager::Private : public KBookmarkOwner } virtual QString currentUrl() const; - virtual QString currentTitle() const; + virtual QString currentTitle( const KUrl& referurl ) const; virtual bool enableOption(BookmarkOption option) const; virtual void openBookmark( const KBookmark & bm, Qt::MouseButtons, Qt::KeyboardModifiers ); @@ -122,9 +122,13 @@ QString BookmarkManager::Private::currentUrl() const return url.prettyUrl(); } -QString BookmarkManager::Private::currentTitle() const +QString BookmarkManager::Private::currentTitle( const KUrl &referurl ) const { - return url.isLocalFile() ? url.toLocalFile() : url.prettyUrl(); + KBookmarkGroup thebg; + QHash<KUrl, QString>::iterator it = q->d->bookmarkFind( referurl, false, &thebg ); + Q_ASSERT( it != q->d->knownFiles.end() ); + + return thebg.fullText(); } bool BookmarkManager::Private::enableOption(BookmarkOption option) const @@ -362,6 +366,26 @@ 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<KUrl, QString>::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::currentTitle( const KUrl& referurl ) const +{ + return d->currentTitle( referurl ); +} + 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 03bd1eb..31cc975 100644 --- a/core/bookmarkmanager.h +++ b/core/bookmarkmanager.h @@ -102,6 +102,17 @@ 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. + */ + void renameBookmark( const KUrl& referurl, const QString& newName ); + + /** + * Returns current title for the @p referurl + */ + QString currentTitle( const KUrl& referurl ) const; + + /** * Returns whether the given @p page is bookmarked. */ bool isBookmarked( int page ) const; diff --git a/ui/bookmarklist.cpp b/ui/bookmarklist.cpp index 4e4c732..c13280a 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 ) - : QTreeWidgetItem( tree, FileItemType ) + FileItem( const KUrl & url, QTreeWidget *tree, Okular::Document *document ) + : QTreeWidgetItem( tree, FileItemType ), m_document( document ) { - const QString fileString = url.isLocalFile() ? url.toLocalFile() : url.prettyUrl(); + setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable ); + const QString fileString = m_document->bookmarkManager()->currentTitle( url ); setText( 0, fileString ); setData( 0, UrlRole, qVariantFromValue( url ) ); } @@ -113,6 +114,9 @@ class FileItem : public QTreeWidgetItem } return QTreeWidgetItem::data( column, role ); } + + private: + Okular::Document * m_document; }; @@ -220,7 +224,13 @@ void BookmarkList::slotChanged( QTreeWidgetItem * item ) { BookmarkItem* bmItem = dynamic_cast<BookmarkItem*>( item ); if ( !bmItem || !bmItem->viewport().isValid() ) - return; + + if ( FileItem* fItem = dynamic_cast<FileItem*>( item ) ) + { + m_document->bookmarkManager()->renameBookmark( m_document->currentDocument(), fItem->text( 0 ) ); + m_document->bookmarkManager()->save(); + return; + } bmItem->bookmark().setFullText( bmItem->text( 0 ) ); m_document->bookmarkManager()->save(); @@ -271,6 +281,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 +292,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 +371,7 @@ void BookmarkList::rebuildTree( bool filter ) QList<QTreeWidgetItem*> 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 +439,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() )
_______________________________________________ Okular-devel mailing list Okular-devel@kde.org https://mail.kde.org/mailman/listinfo/okular-devel