From 78c312a0f33b15b34aed8ea7cf9e9c344dea1f87 Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Mon, 2 Apr 2007 17:32:49 +0000 Subject: [PATCH] Make use of KFilePlaces instead of the bookmark system in the URL navigator. Yay! svn path=/trunk/KDE/kdebase/apps/; revision=649452 --- src/bookmarkselector.cpp | 106 ++++++++++++++++++++++++--------------- src/bookmarkselector.h | 24 +++++---- src/dolphinview.cpp | 3 +- src/urlnavigator.cpp | 44 ++++++++-------- src/urlnavigator.h | 4 +- 5 files changed, 106 insertions(+), 75 deletions(-) diff --git a/src/bookmarkselector.cpp b/src/bookmarkselector.cpp index 77fa5b7ac2..10ce0f2b3c 100644 --- a/src/bookmarkselector.cpp +++ b/src/bookmarkselector.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include @@ -33,65 +33,88 @@ #include #include -BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager) : +BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KFilePlacesModel* placesModel) : UrlButton(parent), - m_selectedAddress(), + m_selectedItem(-1), m_urlNavigator(parent), - m_bookmarkManager(bookmarkManager) + m_placesModel(placesModel) { setFocusPolicy(Qt::NoFocus); - m_bookmarksMenu = new KMenu(this); + m_placesMenu = new KMenu(this); - KBookmarkGroup root = m_bookmarkManager->root(); - KBookmark bookmark = root.first(); - int i = 0; - while (!bookmark.isNull()) { - QAction* action = new QAction(MainBarIcon(bookmark.icon()), - bookmark.text(), - this); - m_bookmarksMenu->addAction(action); - QString address = QChar('/'); - address += QString::number(i); - action->setData(address); - if (address == m_selectedAddress) { - QPixmap pixmap = SmallIcon(bookmark.icon()); - setIcon(QIcon(pixmap)); - setIconSize(pixmap.size()); - setMinimumWidth(pixmap.width() + 2); - } - bookmark = root.next(bookmark); - ++i; - } + updateMenu(); - connect(m_bookmarksMenu, SIGNAL(triggered(QAction*)), - this, SLOT(activateBookmark(QAction*))); + connect(m_placesModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)), + this, SLOT(updateMenu())); + connect(m_placesModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), + this, SLOT(updateMenu())); + connect(m_placesMenu, SIGNAL(triggered(QAction*)), + this, SLOT(activatePlace(QAction*))); - setMenu(m_bookmarksMenu); + setMenu(m_placesMenu); } BookmarkSelector::~BookmarkSelector() { } +void BookmarkSelector::updateMenu() +{ + m_placesMenu->clear(); + + for (int i=0; irowCount(); ++i) { + QModelIndex index = m_placesModel->index(i, 0); + QAction* action = new QAction(m_placesModel->icon(index), + m_placesModel->text(index), + m_placesMenu); + m_placesMenu->addAction(action); + + action->setData(i); + + if (i == m_selectedItem) { + //QPixmap pixmap = SmallIcon(bookmark.icon()); + setIcon(m_placesModel->icon(index)); + //setIconSize(pixmap.size()); + //setMinimumWidth(pixmap.width() + 2); + } + } +} + void BookmarkSelector::updateSelection(const KUrl& url) { - KBookmark bookmark = m_bookmarkManager->root().closestBookmark(url); - if (!bookmark.isNull()) { - m_selectedAddress = bookmark.address(); - setIcon(KIcon(bookmark.icon())); + QModelIndex index = m_placesModel->closestItem(url); + + if (index.isValid()) { + m_selectedItem = index.row(); + setIcon(m_placesModel->icon(index)); } else { - m_selectedAddress = QString(); + m_selectedItem = -1; // No bookmark has been found which matches to the given Url. Show // a generic folder icon as pixmap for indication: setIcon(KIcon("folder")); } } -KBookmark BookmarkSelector::selectedBookmark() const +KUrl BookmarkSelector::selectedPlaceUrl() const { - return m_bookmarkManager->findByAddress(m_selectedAddress); + QModelIndex index = m_placesModel->index(m_selectedItem, 0); + + if (index.isValid()) + return m_placesModel->url(index); + else + return KUrl(); +} + +QString BookmarkSelector::selectedPlaceText() const +{ + QModelIndex index = m_placesModel->index(m_selectedItem, 0); + + if (index.isValid()) + return m_placesModel->text(index); + else + return QString(); } QSize BookmarkSelector::sizeHint() const @@ -148,14 +171,17 @@ void BookmarkSelector::paintEvent(QPaintEvent* /*event*/) painter.drawPixmap(x, y, pixmap); } -void BookmarkSelector::activateBookmark(QAction* action) +void BookmarkSelector::activatePlace(QAction* action) { assert(action != 0); - m_selectedAddress = action->data().toString(); + m_selectedItem = action->data().toInt(); - const KBookmark bookmark = selectedBookmark(); - setPixmap(SmallIcon(bookmark.icon())); - emit bookmarkActivated(bookmark.url()); + QModelIndex index = m_placesModel->index(m_selectedItem, 0); + + if (index.isValid()) { + setIcon(m_placesModel->icon(index)); + emit placeActivated(m_placesModel->url(index)); + } } #include "bookmarkselector.moc" diff --git a/src/bookmarkselector.h b/src/bookmarkselector.h index a5f0d434ef..bedfcb233f 100644 --- a/src/bookmarkselector.h +++ b/src/bookmarkselector.h @@ -20,12 +20,12 @@ #ifndef BOOKMARKSELECTOR_H #define BOOKMARKSELECTOR_H -#include #include +#include +class KFilePlacesModel; class UrlNavigator; class KMenu; -class KUrl; /** * @brief Allows to select a bookmark from a popup menu. @@ -44,7 +44,7 @@ public: * @param parent Parent widget where the bookmark selector * is embedded into. */ - BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager); + BookmarkSelector(UrlNavigator* parent, KFilePlacesModel* placesModel); virtual ~BookmarkSelector(); @@ -58,7 +58,9 @@ public: void updateSelection(const KUrl& url); /** Returns the selected bookmark. */ - KBookmark selectedBookmark() const; + KUrl selectedPlaceUrl() const; + /** Returns the selected bookmark. */ + QString selectedPlaceText() const; /** @see QWidget::sizeHint() */ virtual QSize sizeHint() const; @@ -66,9 +68,9 @@ public: signals: /** * Is send when a bookmark has been activated by the user. - * @param url URL of the selected bookmark. + * @param url URL of the selected place. */ - void bookmarkActivated(const KUrl& url); + void placeActivated(const KUrl& url); protected: /** @@ -82,13 +84,15 @@ private slots: * Updates the selected index and the icon to the bookmark * which is indicated by the triggered action \a action. */ - void activateBookmark(QAction* action); + void activatePlace(QAction* action); + + void updateMenu(); private: - QString m_selectedAddress; + int m_selectedItem; UrlNavigator* m_urlNavigator; - KMenu* m_bookmarksMenu; - KBookmarkManager* m_bookmarkManager; + KMenu* m_placesMenu; + KFilePlacesModel* m_placesModel; }; #endif diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index bb84d32c01..402a673f04 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -96,7 +97,7 @@ DolphinView::DolphinView(DolphinMainWindow* mainWindow, connect(clipboard, SIGNAL(dataChanged()), this, SLOT(updateCutItems())); - m_urlNavigator = new UrlNavigator(DolphinSettings::instance().bookmarkManager(), url, this); + m_urlNavigator = new UrlNavigator(new KFilePlacesModel(this), url, this); m_urlNavigator->setUrlEditable(DolphinSettings::instance().generalSettings()->editableUrl()); m_urlNavigator->setHomeUrl(DolphinSettings::instance().generalSettings()->homeUrl()); m_urlNavigator->setShowHiddenFiles(showHiddenFiles); diff --git a/src/urlnavigator.cpp b/src/urlnavigator.cpp index 514e4b22b1..1e8f06b6e3 100644 --- a/src/urlnavigator.cpp +++ b/src/urlnavigator.cpp @@ -98,7 +98,7 @@ HistoryElem::~HistoryElem() class UrlNavigator::Private { public: - Private(UrlNavigator* q, KBookmarkManager* bookmarkManager); + Private(UrlNavigator* q, KFilePlacesModel* placesModel); void slotReturnPressed(const QString&); void slotRemoteHostActivated(); @@ -162,7 +162,7 @@ public: }; -UrlNavigator::Private::Private(UrlNavigator* q, KBookmarkManager* bookmarkManager) +UrlNavigator::Private::Private(UrlNavigator* q, KFilePlacesModel* placesModel) : m_active(true), m_showHiddenFiles(false), @@ -189,8 +189,8 @@ UrlNavigator::Private::Private(UrlNavigator* q, KBookmarkManager* bookmarkManage q, SLOT(switchView())); // initialize the bookmark selector - m_bookmarkSelector = new BookmarkSelector(q, bookmarkManager); - connect(m_bookmarkSelector, SIGNAL(bookmarkActivated(const KUrl&)), + m_bookmarkSelector = new BookmarkSelector(q, placesModel); + connect(m_bookmarkSelector, SIGNAL(placeActivated(const KUrl&)), q, SLOT(setUrl(const KUrl&))); // initialize the path box of the traditional view @@ -403,39 +403,39 @@ void UrlNavigator::Private::updateContent() m_filler->show(); // get the data from the currently selected bookmark - KBookmark bookmark = m_bookmarkSelector->selectedBookmark(); + KUrl placeUrl = m_bookmarkSelector->selectedPlaceUrl(); - QString bookmarkPath; - if (bookmark.isNull()) { - // No bookmark is a part of the current Url. - // The following code tries to guess the bookmark + QString placePath; + if (!placeUrl.isValid()) { + // No place is a part of the current Url. + // The following code tries to guess the place // path. E. g. "fish://root@192.168.0.2/var/lib" writes - // "fish://root@192.168.0.2" to 'bookmarkPath', which leads to the + // "fish://root@192.168.0.2" to 'placePath', which leads to the // navigation indication 'Custom Path > var > lib". int idx = path.indexOf(QString("//")); idx = path.indexOf("/", (idx < 0) ? 0 : idx + 2); - bookmarkPath = (idx < 0) ? path : path.left(idx); + placePath = (idx < 0) ? path : path.left(idx); } else { - bookmarkPath = bookmark.url().pathOrUrl(); + placePath = placeUrl.pathOrUrl(); } - const uint len = bookmarkPath.length(); + const uint len = placePath.length(); // calculate the start point for the URL navigator buttons by counting - // the slashs inside the bookmark URL + // the slashs inside the place URL int slashCount = 0; for (uint i = 0; i < len; ++i) { - if (bookmarkPath.at(i) == QChar('/')) { + if (placePath.at(i) == QChar('/')) { ++slashCount; } } - if ((len > 0) && bookmarkPath.at(len - 1) == QChar('/')) { + if ((len > 0) && placePath.at(len - 1) == QChar('/')) { assert(slashCount > 0); --slashCount; } const KUrl currentUrl = q->url(); - if (!currentUrl.isLocalFile() && bookmark.isNull()) { + if (!currentUrl.isLocalFile() && !placeUrl.isValid()) { QString protocol = currentUrl.protocol(); if (!m_protocols) { deleteButtons(); @@ -511,9 +511,9 @@ void UrlNavigator::Private::updateButtons(const QString& path, int startIndex) QString text; if (isFirstButton) { // the first URL navigator button should get the name of the - // bookmark instead of the directory name - const KBookmark bookmark = m_bookmarkSelector->selectedBookmark(); - text = bookmark.text(); + // place instead of the directory name + const KUrl placeUrl = m_bookmarkSelector->selectedPlaceUrl(); + text = m_bookmarkSelector->selectedPlaceText(); if (text.isEmpty()) { if (currentUrl.isLocalFile()) { text = i18n("Custom Path"); @@ -576,11 +576,11 @@ void UrlNavigator::Private::deleteButtons() //// -UrlNavigator::UrlNavigator(KBookmarkManager* bookmarkManager, +UrlNavigator::UrlNavigator(KFilePlacesModel* placesModel, const KUrl& url, QWidget* parent) : QWidget(parent), - d( new Private(this, bookmarkManager) ) + d( new Private(this, placesModel) ) { d->m_history.prepend(HistoryElem(url)); diff --git a/src/urlnavigator.h b/src/urlnavigator.h index cec3a0a9d8..af77fa8e39 100644 --- a/src/urlnavigator.h +++ b/src/urlnavigator.h @@ -25,7 +25,7 @@ #include #include -class KBookmarkManager; +class KFilePlacesModel; class QMouseEvent; /** @@ -50,7 +50,7 @@ class UrlNavigator : public QWidget Q_OBJECT public: - UrlNavigator(KBookmarkManager* bookmarkManager, const KUrl& url, QWidget* parent); + UrlNavigator(KFilePlacesModel* placesModel, const KUrl& url, QWidget* parent); virtual ~UrlNavigator(); /** Returns the current active URL. */