From 85bc196887fbed01187e3830cd28c2500cb01cb2 Mon Sep 17 00:00:00 2001 From: David Faure Date: Mon, 26 Mar 2007 23:04:43 +0000 Subject: [PATCH] Ported to KBookmarkManager::closestBookmark - which even solves another TODO about code duplication. Improved urlnavigator API to avoid exposing the full QList (api independent from implementation now). svn path=/trunk/KDE/kdebase/apps/; revision=646954 --- src/bookmarkselector.cpp | 30 +----------------------------- src/bookmarkselector.h | 16 ---------------- src/bookmarkssidebarpage.cpp | 29 ++++------------------------- src/dolphinmainwindow.cpp | 3 +-- src/dolphinview.cpp | 18 +++++------------- src/dolphinview.h | 8 -------- src/treeviewsidebarpage.cpp | 5 +++-- src/urlnavigator.cpp | 10 +++++++--- src/urlnavigator.h | 13 ++++++------- src/urlnavigatorbutton.h | 2 +- 10 files changed, 28 insertions(+), 106 deletions(-) diff --git a/src/bookmarkselector.cpp b/src/bookmarkselector.cpp index 84ec0fc4e..1e4aaadc9 100644 --- a/src/bookmarkselector.cpp +++ b/src/bookmarkselector.cpp @@ -75,7 +75,7 @@ BookmarkSelector::~BookmarkSelector() void BookmarkSelector::updateSelection(const KUrl& url) { - KBookmark bookmark = baseBookmark(m_bookmarkManager, url); + KBookmark bookmark = m_bookmarkManager->root().closestBookmark(url); if (!bookmark.isNull()) { m_selectedAddress = bookmark.address(); setIcon(SmallIcon(bookmark.icon())); @@ -99,34 +99,6 @@ QSize BookmarkSelector::sizeHint() const return QSize(height, height); } -KBookmark BookmarkSelector::baseBookmark(KBookmarkManager* bookmarkManager, const KUrl& url) -{ - const KBookmarkGroup root = bookmarkManager->root(); - KBookmark bookmark = root.first(); - KBookmark foundBookmark; - - int maxLength = 0; - - // Search the bookmark which is equal to the Url or at least is a parent Url. - // If there are more than one possible parent Url candidates, choose the bookmark - // which covers the bigger range of the Url. - int i = 0; - while (!bookmark.isNull()) { - const KUrl bookmarkUrl = bookmark.url(); - if (bookmarkUrl.isParentOf(url)) { - const int length = bookmarkUrl.prettyUrl().length(); - if (length > maxLength) { - foundBookmark = bookmark; - maxLength = length; - } - } - bookmark = root.next(bookmark); - ++i; - } - - return foundBookmark; -} - void BookmarkSelector::paintEvent(QPaintEvent* /*event*/) { QPainter painter(this); diff --git a/src/bookmarkselector.h b/src/bookmarkselector.h index a92f186ec..a5f0d434e 100644 --- a/src/bookmarkselector.h +++ b/src/bookmarkselector.h @@ -63,22 +63,6 @@ public: /** @see QWidget::sizeHint() */ virtual QSize sizeHint() const; - /** - * Returns the base bookmark for the URL \a url. - * The base bookmark is defined as bookmark which is equal to - * the URL or at least is a parent URL. If there are more than - * one possible parent URL candidates, the bookmark which covers - * the bigger range of the URL is returned. - * - * Example: the url is '/home/peter/Documents/Music. Available - * bookmarks are: - * - /home/peter - * - /home/peter/Documents - * - * The base URL will be '/home/peter/Documents'. - */ - static KBookmark baseBookmark(KBookmarkManager* bookmarkManager, const KUrl& url); - signals: /** * Is send when a bookmark has been activated by the user. diff --git a/src/bookmarkssidebarpage.cpp b/src/bookmarkssidebarpage.cpp index 677d0bd06..b68fd58e5 100644 --- a/src/bookmarkssidebarpage.cpp +++ b/src/bookmarkssidebarpage.cpp @@ -196,41 +196,20 @@ void BookmarksSidebarPage::slotContextMenuRequested(Q3ListBoxItem* item, void BookmarksSidebarPage::adjustSelection(const KUrl& url) { - // TODO (remarked in dolphin/TODO): the following code is quite equal - // to BookmarkSelector::updateSelection(). - KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root(); - KBookmark bookmark = root.first(); - - int maxLength = 0; - int selectedIndex = -1; - - // Search the bookmark which is equal to the Url or at least is a parent Url. - // If there are more than one possible parent Url candidates, choose the bookmark - // which covers the bigger range of the Url. - int i = 0; - while (!bookmark.isNull()) { - const KUrl bookmarkUrl = bookmark.url(); - if (bookmarkUrl.isParentOf(url)) { - const int length = bookmarkUrl.prettyUrl().length(); - if (length > maxLength) { - selectedIndex = i; - maxLength = length; - } - } - bookmark = root.next(bookmark); - ++i; - } + KBookmark bookmark = root.closestBookmark(url); const bool block = m_bookmarksList->signalsBlocked(); m_bookmarksList->blockSignals(true); - if (selectedIndex < 0) { + if (bookmark.isNull()) { // no bookmark matches, hence deactivate any selection const int currentIndex = m_bookmarksList->index(m_bookmarksList->selectedItem()); m_bookmarksList->setSelected(currentIndex, false); } else { // select the bookmark which is part of the current Url + // TODO when porting to QListWidget, use the address as item data? + int selectedIndex = bookmark.address().mid(1).toInt(); // convert "/5" to 5. m_bookmarksList->setSelected(selectedIndex, true); } m_bookmarksList->blockSignals(block); diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index ebf8a3f82..e86ad13fa 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -1348,11 +1348,10 @@ void DolphinMainWindow::setupDockWidgets() void DolphinMainWindow::updateHistory() { int index = 0; - const QList list = m_activeView->urlHistory(index); QAction* backAction = actionCollection()->action("go_back"); if (backAction != 0) { - backAction->setEnabled(index < static_cast(list.count()) - 1); + backAction->setEnabled(index < m_activeView->urlNavigator()->historySize() - 1); } QAction* forwardAction = actionCollection()->action("go_forward"); diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 23c91b9f4..17bbd608d 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -454,11 +454,6 @@ void DolphinView::setUrlEditable(bool editable) m_urlNavigator->editUrl(editable); } -const QList DolphinView::urlHistory(int& index) const -{ - return m_urlNavigator->history(index); -} - bool DolphinView::hasSelection() const { return itemView()->selectionModel()->hasSelection(); @@ -771,15 +766,12 @@ void DolphinView::showPreview(const KFileItem* item, const QPixmap& pixmap) void DolphinView::restoreContentsPos() { - int index = 0; - const QList history = urlHistory(index); - if (!history.isEmpty()) { + UrlNavigator::HistoryElem historyItem = m_urlNavigator->currentHistoryItem(); + if (!historyItem.url().isEmpty()) { QAbstractItemView* view = itemView(); - // TODO: view->setCurrentItem(history[index].currentFileName()); - - const UrlNavigator::HistoryElem& it = history[index]; - view->horizontalScrollBar()->setValue(it.contentsX()); - view->verticalScrollBar()->setValue(it.contentsY()); + // TODO: view->setCurrentItem(historyItem.currentFileName()); + view->horizontalScrollBar()->setValue(historyItem.contentsX()); + view->verticalScrollBar()->setValue(historyItem.contentsY()); } } diff --git a/src/dolphinview.h b/src/dolphinview.h index 0bec4824a..14c8372d3 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -211,14 +211,6 @@ public: */ void setUrlEditable(bool editable); - /** - * Returns the complete URL history. The index 0 indicates the oldest - * history element. - * @param index Output parameter which indicates the current - * index of the location. - */ - const QList urlHistory(int& index) const; - /** Returns true, if at least one item is selected. */ bool hasSelection() const; diff --git a/src/treeviewsidebarpage.cpp b/src/treeviewsidebarpage.cpp index aef455c1e..523e21a78 100644 --- a/src/treeviewsidebarpage.cpp +++ b/src/treeviewsidebarpage.cpp @@ -19,7 +19,7 @@ #include "treeviewsidebarpage.h" -#include "bookmarkselector.h" +#include "kbookmarkmanager.h" #include "dolphinmainwindow.h" #include "dolphinsortfilterproxymodel.h" #include "dolphinview.h" @@ -99,7 +99,8 @@ void TreeViewSidebarPage::setUrl(const KUrl& url) m_url = url; // adjust the root of the tree to the base bookmark - const KUrl baseUrl = BookmarkSelector::baseBookmark(DolphinSettings::instance().bookmarkManager(), url).url(); + KBookmarkManager* bookmarkManager = DolphinSettings::instance().bookmarkManager(); + const KUrl baseUrl = bookmarkManager->root().closestBookmark(url).url(); if (m_dirLister->url() != baseUrl) { m_dirLister->stop(); m_dirLister->openUrl(baseUrl); diff --git a/src/urlnavigator.cpp b/src/urlnavigator.cpp index ca3b1789c..aa4c94c69 100644 --- a/src/urlnavigator.cpp +++ b/src/urlnavigator.cpp @@ -166,10 +166,14 @@ KUrl UrlNavigator::url(int index) const return newurl; } -const QList& UrlNavigator::history(int& index) const +UrlNavigator::HistoryElem UrlNavigator::currentHistoryItem() const { - index = m_historyIndex; - return m_history; + return m_history[m_historyIndex]; +} + +int UrlNavigator::historySize() const +{ + return m_history.count(); } void UrlNavigator::goBack() diff --git a/src/urlnavigator.h b/src/urlnavigator.h index aa46709aa..8a1d2512d 100644 --- a/src/urlnavigator.h +++ b/src/urlnavigator.h @@ -58,10 +58,7 @@ class ProtocolCombo; * * The URL navigator also remembers the URL history and allows to go * back and forward within this history. -*/ - -//typedef QList UrlStack; - + */ class UrlNavigator : public QWidget { Q_OBJECT @@ -107,13 +104,16 @@ public: /** Returns the portion of the current active URL up to the button at index. */ KUrl url(int index) const; + /** Returns the amount of items in the history */ + int historySize() const; + /** - * Returns the complete URL history. The index 0 indicates the oldest + * Returns one item out of the history. The index 0 indicates the oldest * history element. * @param index Output parameter which indicates the current * index of the location. */ - const QList& history(int& index) const; + HistoryElem currentHistoryItem() const; /** * Goes back one step in the URL history. The signals @@ -315,7 +315,6 @@ private: QLineEdit* m_host; QLinkedList m_navButtons; QWidget* m_filler; - //UrlStack m_urls; }; #endif diff --git a/src/urlnavigatorbutton.h b/src/urlnavigatorbutton.h index 55e74c46b..335001be2 100644 --- a/src/urlnavigatorbutton.h +++ b/src/urlnavigatorbutton.h @@ -22,7 +22,7 @@ #define URLNAVIGATORBUTTON_H #include -#include +#include "urlbutton.h" class KJob; class KUrl; -- 2.47.3