]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Use QSortFilterProxyModel for sorting KFileItems (thanks to Fredrik for this hint...
authorPeter Penz <peter.penz19@gmail.com>
Thu, 14 Dec 2006 23:49:30 +0000 (23:49 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 14 Dec 2006 23:49:30 +0000 (23:49 +0000)
svn path=/trunk/playground/utils/dolphin/; revision=613747

src/CMakeLists.txt
src/dolphinsortfilterproxymodel.cpp [new file with mode: 0644]
src/dolphinsortfilterproxymodel.h [new file with mode: 0644]
src/dolphinview.cpp
src/dolphinview.h

index 3294672bbb3f9f563890e26df344e1966303e55b..7c50ce550e4d7ead72d8fdb66eb9fe3a0e997a0c 100644 (file)
@@ -24,6 +24,7 @@ set(dolphin_SRCS
    bookmarkselector.cpp
    urlbutton.cpp
    dolphincontextmenu.cpp
+   dolphinsortfilterproxymodel.cpp
    undomanager.cpp
    progressindicator.cpp
    iconsviewsettingspage.cpp
diff --git a/src/dolphinsortfilterproxymodel.cpp b/src/dolphinsortfilterproxymodel.cpp
new file mode 100644 (file)
index 0000000..56eccc0
--- /dev/null
@@ -0,0 +1,72 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>                  *
+ *   Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net>          *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#include "dolphinsortfilterproxymodel.h"
+
+#include <kdirmodel.h>
+#include <kfileitem.h>
+
+DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
+    QSortFilterProxyModel(parent),
+    m_sorting(DolphinView::SortByName),
+    m_sortOrder(Qt::Ascending)
+{
+}
+
+DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
+{
+}
+
+void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
+{
+    if (sorting != m_sorting) {
+        m_sorting = sorting;
+        // TODO: how to trigger an update?
+    }
+}
+
+void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
+{
+    if (sortOrder != m_sortOrder) {
+        m_sortOrder = sortOrder;
+        // TODO: how to trigger an update?
+    }
+}
+
+bool DolphinSortFilterProxyModel::lessThan(const QModelIndex& left,
+                                           const QModelIndex& right) const
+{
+    // TODO: this is just a test implementation
+    KDirModel* model = static_cast<KDirModel*>(sourceModel());
+
+    KFileItem* leftItem = model->itemForIndex(left);
+    if (leftItem == 0) {
+        return true;
+    }
+
+    KFileItem* rightItem = model->itemForIndex(right);
+    if (rightItem == 0) {
+        return false;
+    }
+
+    return leftItem->name() > rightItem->name();
+}
+
+#include "dolphinsortfilterproxymodel.moc"
diff --git a/src/dolphinsortfilterproxymodel.h b/src/dolphinsortfilterproxymodel.h
new file mode 100644 (file)
index 0000000..fd1005b
--- /dev/null
@@ -0,0 +1,56 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>                  *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#ifndef DOLPHINSORTFILTERPROXYMODEL_H
+#define DOLPHINSORTFILTERPROXYMODEL_H
+
+#include <QSortFilterProxyModel>
+#include <dolphinview.h>
+
+/**
+ * @brief Acts as proxy model for KDirModel to sort and filter
+ *        KFileItems.
+ *
+ * TODO: implementation does not work yet (the method lessThan is
+ * not invoked)
+ */
+class DolphinSortFilterProxyModel : public QSortFilterProxyModel
+{
+    Q_OBJECT
+
+public:
+    DolphinSortFilterProxyModel(QObject* parent = 0);
+    virtual ~DolphinSortFilterProxyModel();
+
+    void setSorting(DolphinView::Sorting sorting);
+    DolphinView::Sorting sorting() const { return m_sorting; }
+
+    void setSortOrder(Qt::SortOrder sortOrder);
+    Qt::SortOrder sortOrder() const { return m_sortOrder; }
+
+protected:
+     virtual bool lessThan(const QModelIndex& left,
+                           const QModelIndex& right) const;
+
+private:
+    DolphinView::Sorting m_sorting;
+    Qt::SortOrder m_sortOrder;
+};
+
+#endif
index 1dba37ad511cf562d689a2135b620348cb21491b..c23cb2baa7197e8dbb7269c23876d2d909ede332 100644 (file)
@@ -39,6 +39,7 @@
 #include "dolphinstatusbar.h"
 #include "dolphinmainwindow.h"
 #include "dolphindirlister.h"
+#include "dolphinsortfilterproxymodel.h"
 #include "viewproperties.h"
 #include "dolphindetailsview.h"
 #include "dolphiniconsview.h"
@@ -57,12 +58,17 @@ DolphinView::DolphinView(DolphinMainWindow *mainWindow,
     m_refreshing(false),
     m_showProgress(false),
     m_mode(mode),
-    m_mainWindow(mainWindow),
-    m_statusBar(0),
     m_iconSize(0),
     m_folderCount(0),
     m_fileCount(0),
-    m_filterBar(0)
+    m_mainWindow(mainWindow),
+    m_topLayout(0),
+    m_urlNavigator(0),
+    m_iconsView(0),
+    m_filterBar(0),
+    m_statusBar(0),
+    m_dirLister(0),
+    m_proxyModel(0)
 {
     hide();
     setFocusPolicy(Qt::StrongFocus);
@@ -98,6 +104,11 @@ DolphinView::DolphinView(DolphinMainWindow *mainWindow,
 
     KDirModel* model = new KDirModel();
     model->setDirLister(m_dirLister);
+
+    m_proxyModel = new DolphinSortFilterProxyModel(this);
+    m_proxyModel->setSourceModel(model);
+    m_proxyModel->setDynamicSortFilter(true);
+
     m_iconsView->setModel(model);
 
     KFileItemDelegate* delegate = new KFileItemDelegate(this);
@@ -395,8 +406,7 @@ void DolphinView::setSorting(Sorting sorting)
         ViewProperties props(url());
         props.setSorting(sorting);
 
-        KDirModel* dirModel = static_cast<KDirModel*>(m_iconsView->model());
-        dirModel->sort(columnIndex(sorting), props.sortOrder());
+        m_proxyModel->setSorting(sorting);
 
         emit sortingChanged(sorting);
     }
@@ -404,10 +414,7 @@ void DolphinView::setSorting(Sorting sorting)
 
 DolphinView::Sorting DolphinView::sorting() const
 {
-    // TODO: instead of getting the sorting from the properties just fetch
-    // them from KDirModel, if such an interface will be offered (David?)
-    ViewProperties props(url());
-    return props.sorting();
+    return m_proxyModel->sorting();
 }
 
 void DolphinView::setSortOrder(Qt::SortOrder order)
@@ -416,8 +423,7 @@ void DolphinView::setSortOrder(Qt::SortOrder order)
         ViewProperties props(url());
         props.setSortOrder(order);
 
-        KDirModel* dirModel = static_cast<KDirModel*>(m_iconsView->model());
-        dirModel->sort(columnIndex(props.sorting()), order);
+        m_proxyModel->setSortOrder(order);
 
         emit sortOrderChanged(order);
     }
@@ -425,10 +431,7 @@ void DolphinView::setSortOrder(Qt::SortOrder order)
 
 Qt::SortOrder DolphinView::sortOrder() const
 {
-    // TODO: instead of getting the order from the properties just fetch
-    // them from KDirModel, if such an interface will be offered (David?)
-    ViewProperties props(url());
-    return props.sortOrder();
+    return m_proxyModel->sortOrder();
 }
 
 void DolphinView::goBack()
index 6aa5967f6b97c06efb29b42da6bbf4e72235fb34..42e54f802973fed61f217d29482ccaf22b05b979 100644 (file)
@@ -50,6 +50,7 @@ class DolphinDirLister;
 class DolphinStatusBar;
 class DolphinIconsView;
 class DolphinDetailsView;
+class DolphinSortFilterProxyModel;
 class ViewProperties;
 class KProgress;
 class FilterBar;
@@ -452,19 +453,20 @@ private:
     bool m_showProgress;
     Mode m_mode;
 
+    int m_iconSize;
+    int m_folderCount;
+    int m_fileCount;
+
     DolphinMainWindow* m_mainWindow;
     QVBoxLayout* m_topLayout;
     UrlNavigator* m_urlNavigator;
     DolphinIconsView* m_iconsView;
+    FilterBar *m_filterBar;
     DolphinStatusBar* m_statusBar;
 
-    int m_iconSize;
-    int m_folderCount;
-    int m_fileCount;
-
     DolphinDirLister* m_dirLister;
+    DolphinSortFilterProxyModel* m_proxyModel;
 
-    FilterBar *m_filterBar;
 };
 
 #endif // _DOLPHINVIEW_H_