]> cloud.milkyroute.net Git - dolphin.git/commitdiff
A small step in trying to make urlnavigator useable in kdelibs for the file dialog:
authorDavid Faure <faure@kde.org>
Mon, 26 Mar 2007 20:32:50 +0000 (20:32 +0000)
committerDavid Faure <faure@kde.org>
Mon, 26 Mar 2007 20:32:50 +0000 (20:32 +0000)
 remove dependency on DolphinSettings::bookmarkManager().

svn path=/trunk/KDE/kdebase/apps/; revision=646880

src/bookmarkselector.cpp
src/bookmarkselector.h
src/dolphinsettings.cpp
src/dolphinview.cpp
src/treeviewsidebarpage.cpp
src/urlnavigator.cpp
src/urlnavigator.h

index 73950434d6717bdaad75651b0d47999ace0ac523..84ec0fc4ee5606519a8e9e691a8cdae7d2c59e50 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "bookmarkselector.h"
 
-#include "dolphinsettings.h"
 #include "urlnavigator.h"
 
 #include <assert.h>
 #include <QPainter>
 #include <QPixmap>
 
-BookmarkSelector::BookmarkSelector(UrlNavigator* parent) :
+BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager) :
     UrlButton(parent),
-    m_selectedIndex(0),
-    m_urlNavigator(parent)
+    m_selectedAddress(),
+    m_urlNavigator(parent),
+    m_bookmarkManager(bookmarkManager)
 {
     setFocusPolicy(Qt::NoFocus);
 
     m_bookmarksMenu = new KMenu(this);
 
-    KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
+    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);
-        action->setData(i);
         m_bookmarksMenu->addAction(action);
-        if (i == m_selectedIndex) {
+        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());
@@ -73,12 +75,13 @@ BookmarkSelector::~BookmarkSelector()
 
 void BookmarkSelector::updateSelection(const KUrl& url)
 {
-    m_selectedIndex = baseBookmarkIndex(url);
-    if (m_selectedIndex >= 0) {
-        KBookmark bookmark = DolphinSettings::instance().bookmark(m_selectedIndex);
+    KBookmark bookmark = baseBookmark(m_bookmarkManager, url);
+    if (!bookmark.isNull()) {
+        m_selectedAddress = bookmark.address();
         setIcon(SmallIcon(bookmark.icon()));
     }
     else {
+        m_selectedAddress = QString();
         // No bookmark has been found which matches to the given Url. Show
         // a generic folder icon as pixmap for indication:
         setIcon(SmallIcon("folder"));
@@ -87,7 +90,7 @@ void BookmarkSelector::updateSelection(const KUrl& url)
 
 KBookmark BookmarkSelector::selectedBookmark() const
 {
-    return DolphinSettings::instance().bookmark(m_selectedIndex);
+    return m_bookmarkManager->findByAddress(m_selectedAddress);
 }
 
 QSize BookmarkSelector::sizeHint() const
@@ -96,10 +99,32 @@ QSize BookmarkSelector::sizeHint() const
     return QSize(height, height);
 }
 
-KBookmark BookmarkSelector::baseBookmark(const KUrl& url)
+KBookmark BookmarkSelector::baseBookmark(KBookmarkManager* bookmarkManager, const KUrl& url)
 {
-    const int index = baseBookmarkIndex(url);
-    return DolphinSettings::instance().bookmark(index);
+    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*/)
@@ -153,41 +178,12 @@ void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
 void BookmarkSelector::activateBookmark(QAction* action)
 {
     assert(action != 0);
-    m_selectedIndex = action->data().toInt();
+    m_selectedAddress = action->data().toString();
 
     const KBookmark bookmark = selectedBookmark();
     setPixmap(SmallIcon(bookmark.icon()));
     emit bookmarkActivated(bookmark.url());
 }
 
-int BookmarkSelector::baseBookmarkIndex(const KUrl& url)
-{
-    int index = -1;  // return value
-
-    KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
-    KBookmark bookmark = root.first();
-
-    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) {
-                index = i;
-                maxLength = length;
-            }
-        }
-        bookmark = root.next(bookmark);
-        ++i;
-    }
-
-    return index;
-}
-
 #include "bookmarkselector.moc"
 
index 334f25ba379c0004c1ca18d3c3e69cbe522bcdb8..a92f186ecf9186da1f20ca5ae04d00912f29e243 100644 (file)
@@ -44,7 +44,7 @@ public:
      * @param parent Parent widget where the bookmark selector
      *               is embedded into.
      */
-    BookmarkSelector(UrlNavigator* parent);
+    BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager);
 
     virtual ~BookmarkSelector();
 
@@ -57,12 +57,6 @@ public:
      */
     void updateSelection(const KUrl& url);
 
-    /**
-     * Returns the index of the selected bookmark. To get
-     * the bookmark, use BookmarkSelector::selectedBookmark().
-     */
-    int selectedIndex() const { return m_selectedIndex; }
-
     /** Returns the selected bookmark. */
     KBookmark selectedBookmark() const;
 
@@ -83,7 +77,7 @@ public:
      *
      * The base URL will be '/home/peter/Documents'.
      */
-    static KBookmark baseBookmark(const KUrl& url);
+    static KBookmark baseBookmark(KBookmarkManager* bookmarkManager, const KUrl& url);
 
 signals:
     /**
@@ -107,13 +101,10 @@ private slots:
     void activateBookmark(QAction* action);
 
 private:
-    static int baseBookmarkIndex(const KUrl& url);
-
-private:
-    int m_selectedIndex;
+    QString m_selectedAddress;
     UrlNavigator* m_urlNavigator;
     KMenu* m_bookmarksMenu;
-
+    KBookmarkManager* m_bookmarkManager;
 };
 
 #endif
index 7e3388524878ceb175deae3698954bd856bceafb..837a5f4d1c76e48753cb354db812ebb957ad3891 100644 (file)
@@ -47,18 +47,7 @@ DolphinSettings& DolphinSettings::instance()
 
 KBookmark DolphinSettings::bookmark(int index) const
 {
-    int i = 0;
-    KBookmarkGroup root = bookmarkManager()->root();
-    KBookmark bookmark = root.first();
-    while (!bookmark.isNull()) {
-        if (i == index) {
-            return bookmark;
-        }
-        ++i;
-        bookmark = root.next(bookmark);
-    }
-
-    return KBookmark();
+    return bookmarkManager()->findByAddress( QString('/')+QString::number(index) );
 }
 
 KBookmarkManager* DolphinSettings::bookmarkManager() const
index 311ca36b4508c1db2ec37daae2be552712d89f59..23c91b9f45f4289d349af9c9722da44051371fb7 100644 (file)
@@ -51,6 +51,7 @@
 #include "renamedialog.h"
 #include "urlnavigator.h"
 #include "viewproperties.h"
+#include "dolphinsettings.h"
 
 DolphinView::DolphinView(DolphinMainWindow* mainWindow,
                          QWidget* parent,
@@ -90,7 +91,7 @@ DolphinView::DolphinView(DolphinMainWindow* mainWindow,
     connect(clipboard, SIGNAL(dataChanged()),
             this, SLOT(updateCutItems()));
 
-    m_urlNavigator = new UrlNavigator(url, this);
+    m_urlNavigator = new UrlNavigator(DolphinSettings::instance().bookmarkManager(), url, this);
     m_urlNavigator->setShowHiddenFiles(showHiddenFiles);
     connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
             this, SLOT(loadDirectory(const KUrl&)));
index 19feb58745b5290a86dbf3dae6581723af4d8d1a..aef455c1e35f2efae9e64beed8c6f8b6013fabf0 100644 (file)
@@ -34,6 +34,7 @@
 #include <QItemSelectionModel>
 #include <QTreeView>
 #include <QVBoxLayout>
+#include "dolphinsettings.h"
 
 // TODO: currently when using a proxy model the strange effect occurs
 // that items get duplicated. Activate the following define to have the proxy
@@ -98,7 +99,7 @@ void TreeViewSidebarPage::setUrl(const KUrl& url)
     m_url = url;
 
     // adjust the root of the tree to the base bookmark
-    const KUrl baseUrl = BookmarkSelector::baseBookmark(url).url();
+    const KUrl baseUrl = BookmarkSelector::baseBookmark(DolphinSettings::instance().bookmarkManager(), url).url();
     if (m_dirLister->url() != baseUrl) {
         m_dirLister->stop();
         m_dirLister->openUrl(baseUrl);
index 1a49b386b580007187fbebc99f08bbbea4a00c83..ca3b1789cbf26ffe3a954d0b4054d2b78df41b90 100644 (file)
@@ -65,7 +65,8 @@ UrlNavigator::HistoryElem::~HistoryElem()
 {
 }
 
-UrlNavigator::UrlNavigator(const KUrl& url,
+UrlNavigator::UrlNavigator(KBookmarkManager* bookmarkManager,
+                           const KUrl& url,
                            QWidget* parent) :
     QWidget(parent),
     m_active(true),
@@ -101,7 +102,7 @@ UrlNavigator::UrlNavigator(const KUrl& url,
     }
 
     // initialize the bookmark selector
-    m_bookmarkSelector = new BookmarkSelector(this);
+    m_bookmarkSelector = new BookmarkSelector(this, bookmarkManager);
     connect(m_bookmarkSelector, SIGNAL(bookmarkActivated(const KUrl&)),
             this, SLOT(setUrl(const KUrl&)));
 
@@ -532,7 +533,6 @@ void UrlNavigator::updateContent()
 
         // get the data from the currently selected bookmark
         KBookmark bookmark = m_bookmarkSelector->selectedBookmark();
-        //int bookmarkIndex = m_bookmarkSelector->selectedIndex();
 
         QString bookmarkPath;
         if (bookmark.isNull()) {
index 5e0d7f836feb9c3cf1596b33d7b24eda9922e930..aa46709aace8601011b7c055ffa3872de9055188 100644 (file)
@@ -27,6 +27,7 @@
 #include <QList>
 #include <QLinkedList>
 
+class KBookmarkManager;
 class QHBoxLayout;
 class QLabel;
 class QLineEdit;
@@ -97,7 +98,7 @@ public:
         int m_contentsY;
     };
 
-    UrlNavigator(const KUrl& url, QWidget* parent);
+    UrlNavigator(KBookmarkManager* bookmarkManager, const KUrl& url, QWidget* parent);
     virtual ~UrlNavigator();
 
     /** Returns the current active URL. */