[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdelibs/kfile
From:       Peter Penz <peter.penz () gmx ! at>
Date:       2010-04-19 6:33:48
Message-ID: 20100419063348.7F650AC89D () svn ! kde ! org
[Download RAW message or body]

SVN commit 1116304 by ppenz:

Allow scrolling through the directories with the mouse wheel. Thanks to Todd for the patch!

http://reviewboard.kde.org/r/2330/

 M  +85 -1     kurlnavigatorbutton.cpp  
 M  +6 -0      kurlnavigatorbutton_p.h  


--- trunk/KDE/kdelibs/kfile/kurlnavigatorbutton.cpp #1116303:1116304
@@ -41,10 +41,12 @@
     KUrlButton(parent),
     m_hoverArrow(false),
     m_pendingTextChange(false),
+    m_wheelSteps(0),
     m_url(url),
     m_subDir(),
     m_popupDelay(0),
-    m_listJob(0)
+    m_listJob(0),
+    m_subDirNames()
 {
     setAcceptDrops(true);
     setUrl(url);
@@ -318,6 +320,17 @@
     }
 }
 
+void KUrlNavigatorButton::wheelEvent(QWheelEvent* event)
+{
+    if (event->orientation() == Qt::Vertical) {
+        m_wheelSteps = event->delta() / 120;
+        startCycleJob();
+        event->accept();
+    } else {
+        KUrlButton::wheelEvent(event);
+    }
+}
+
 void KUrlNavigatorButton::startPopupDelay()
 {
     if (!m_popupDelay->isActive() && (m_listJob == 0)) {
@@ -348,6 +361,19 @@
     connect(m_listJob, SIGNAL(result(KJob*)), this, SLOT(listJobFinished(KJob*)));
 }
 
+void KUrlNavigatorButton::startCycleJob()
+{
+    if (m_listJob != 0) {
+        return;
+    }
+
+    m_listJob = KIO::listDir(m_url.upUrl(), KIO::HideProgressInfo, false /*no hidden files*/);
+    m_subDirNames.clear(); // just to be ++safe
+    connect(m_listJob, SIGNAL(entries(KIO::Job*, const KIO::UDSEntryList &)),
+            this, SLOT(namesList(KIO::Job*, const KIO::UDSEntryList&)));
+    connect(m_listJob, SIGNAL(result(KJob*)), this, SLOT(cycleJobFinished(KJob*)));
+}
+
 void KUrlNavigatorButton::entriesList(KIO::Job* job, const KIO::UDSEntryList& entries)
 {
     if (job != m_listJob) {
@@ -368,6 +394,22 @@
     }
 }
 
+void KUrlNavigatorButton::namesList(KIO::Job* job, const KIO::UDSEntryList& entries)
+{
+    if (job != m_listJob) {
+        return;
+    }
+
+    foreach (const KIO::UDSEntry& entry, entries) {
+        if (entry.isDir()) {
+            const QString name = entry.stringValue(KIO::UDSEntry::UDS_NAME);
+            if ((name != QLatin1String(".")) && (name != QLatin1String(".."))) {
+                m_subDirNames.append(name);
+            }
+        }
+    }
+}
+
 void KUrlNavigatorButton::urlsDropped(QAction* action, QDropEvent* event)
 {
     const int result = action->data().toInt();
@@ -472,6 +514,48 @@
     setDisplayHintEnabled(PopupActiveHint, false);
 }
 
+/**
+ * Helper function for cycleJobFinished
+ */
+static bool naturalLessThanStr(const QString& s1, const QString& s2)
+{
+    return KStringHandler::naturalCompare(s1, s2, Qt::CaseInsensitive) < 0;
+}
+
+void KUrlNavigatorButton::cycleJobFinished(KJob* job)
+{
+    if (job != m_listJob) {
+        return;
+    }
+
+    m_listJob = 0;
+    if (job->error() || m_subDirNames.isEmpty()) {
+        // clear listing
+        return;
+    }
+
+    qSort(m_subDirNames.begin(), m_subDirNames.end(), naturalLessThanStr);
+
+    const int selectedIndex = m_subDirNames.indexOf(m_url.fileName());
+
+    if (selectedIndex > -1) {
+        const int targetIndex = selectedIndex - m_wheelSteps;
+        KUrl url = m_url.upUrl();
+        if (targetIndex <= 0) {
+            url.addPath(m_subDirNames.first());
+        }
+        else if (targetIndex >= m_subDirNames.count()) {
+            url.addPath(m_subDirNames.last());
+        }
+        else {
+            url.addPath(m_subDirNames[targetIndex]);
+        }
+        emit clicked(url, Qt::LeftButton);
+    }
+    m_subDirNames.clear();
+}
+
+
 int KUrlNavigatorButton::arrowWidth() const
 {
     // if there isn't arrow then return 0
--- trunk/KDE/kdelibs/kfile/kurlnavigatorbutton_p.h #1116303:1116304
@@ -98,13 +98,17 @@
     virtual void mousePressEvent(QMouseEvent* event);
     virtual void mouseReleaseEvent(QMouseEvent* event);
     virtual void mouseMoveEvent(QMouseEvent* event);
+    virtual void wheelEvent(QWheelEvent* event);
 
 private Q_SLOTS:
     void startPopupDelay();
     void stopPopupDelay();
     void startListJob();
+    void startCycleJob();
     void entriesList(KIO::Job* job, const KIO::UDSEntryList& entries);
+    void namesList(KIO::Job* job, const KIO::UDSEntryList& entries);
     void listJobFinished(KJob* job);
+    void cycleJobFinished(KJob* job);
     void urlsDropped(QAction* action, QDropEvent* event);
     void statFinished(KJob*);
 
@@ -117,12 +121,14 @@
 private:
     bool m_hoverArrow;
     bool m_pendingTextChange;
+    int m_wheelSteps;
     KUrl m_url;
     QString m_subDir;
     QTimer* m_popupDelay;
     KIO::Job* m_listJob;
     /// pair of name and display name
     QList<QPair<QString,QString> > m_subDirs;
+    QStringList m_subDirNames;
 
     static QPointer<KUrlNavigatorMenu> m_dirsMenu;
 };
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic