]> cloud.milkyroute.net Git - dolphin.git/commitdiff
-Use QSortFilterProxyModel for filtering instead of KDirList::setFilterName
authorHolger Freyther <holger+kde@freyther.de>
Sun, 24 Dec 2006 00:03:40 +0000 (00:03 +0000)
committerHolger Freyther <holger+kde@freyther.de>
Sun, 24 Dec 2006 00:03:40 +0000 (00:03 +0000)
-Map DolphinView::Sorting to a column of the KDirModel and sort this column
-Reimplement QAbstractItemModel::sort(int,SortOrder) to keep track of the
 sorted column and order as dolphin uses this in the config dialogs. We will
 now work correctly if a view changes sorting.

svn path=/trunk/playground/utils/dolphin/; revision=616170

src/dolphinsortfilterproxymodel.cpp
src/dolphinsortfilterproxymodel.h
src/dolphinview.cpp

index 25bb1cf0d5b3de2945e34138f1d077264ad8cf14..fc79ec5694ce97bd8bb88cab10be3490b99e0c85 100644 (file)
 #include <kdirmodel.h>
 #include <kfileitem.h>
 
+static const int dolphin_map_size = 3;
+static int dolphin_view_to_dir_model_column[] = {
+    /* SortByName */ KDirModel::Name,
+    /* SortBySize */ KDirModel::Size,
+    /* SortByDate */ KDirModel::ModifiedTime 
+};
+
+static DolphinView::Sorting dir_model_column_to_dolphin_view[] = {
+    /* KDirModel::Name */ DolphinView::SortByName,
+    /* KDirModel::Size */ DolphinView::SortBySize,
+    /* KDirModel::ModifiedTime */ DolphinView::SortByDate
+};
+
+
 DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
-    QSortFilterProxyModel(parent),
-    m_sorting(DolphinView::SortByName),
-    m_sortOrder(Qt::Ascending)
+    QSortFilterProxyModel(parent)
 {
+    setDynamicSortFilter(true);
+
+    /*
+     * sort by the user visible string for now
+     */
+    setSortRole(Qt::DisplayRole);
+    setSortCaseSensitivity(Qt::CaseInsensitive);
+    sort(KDirModel::Name, Qt::Ascending);
 }
 
 DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
 {
 }
 
+/*
+ * Update the sort column by mapping DolpginView::Sorting to
+ * KDirModel::ModelColumns.
+ * We will keep the sortOrder
+ */
 void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
 {
-    if (sorting != m_sorting) {
-        m_sorting = sorting;
-        clear();
-    }
+    Q_ASSERT( static_cast<int>(sorting) >= 0 && static_cast<int>(sorting) <= dolphin_map_size );
+    sort(dolphin_view_to_dir_model_column[static_cast<int>(sorting)],
+         m_sortOrder );
+}
+
+/**
+ * @reimplemented, @internal
+ *
+ * If the view 'forces' sorting order to change we will
+ * notice now.
+ */
+void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder)
+{
+    m_sortOrder = sortOrder;
+    m_sorting = column >= 0 && column <= dolphin_map_size ?
+                dir_model_column_to_dolphin_view[column]  :
+                DolphinView::SortByName; 
+    QSortFilterProxyModel::sort(column,sortOrder);
 }
 
+/*
+ * change the sort order by keeping the current column
+ */
 void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
 {
-    if (sortOrder != m_sortOrder) {
-        m_sortOrder = sortOrder;
-        clear();
-    }
+    sort(dolphin_view_to_dir_model_column[m_sorting], sortOrder);
 }
 
 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();
+    /*
+     * We have set a SortRole and trust the ProxyModel to do
+     * the right thing for now
+     */
+    return QSortFilterProxyModel::lessThan(left,right);
 }
 
 #include "dolphinsortfilterproxymodel.moc"
index b400400f63a30910fc4c2988fbc8aaaac52fe5e2..e2c605dc72015e0b0ae20a3f2b36af4379b1da35 100644 (file)
@@ -41,6 +41,8 @@ public:
     void setSorting(DolphinView::Sorting sorting);
     DolphinView::Sorting sorting() const { return m_sorting; }
 
+    virtual void sort (int column, 
+                       Qt::SortOrder order = Qt::AscendingOrder);
     void setSortOrder(Qt::SortOrder sortOrder);
     Qt::SortOrder sortOrder() const { return m_sortOrder; }
 
index 1efe333844f269fc146e57735dd3ba1bf789f102..8fb16d1d5b66ba500d2c434cde56835bd17434be 100644 (file)
@@ -968,8 +968,20 @@ void DolphinView::slotChangeNameFilter(const QString& nameFilter)
     adjustedFilter.insert(0, '*');
     adjustedFilter.append('*');
 
+/*
+ * Use the ProxyModel to filter:
+ *   This code is #ifdefed as setNameFilter behaves
+ *   slightly different than the QSortFilterProxyModel
+ *   as it will not remove directories. I will ask
+ *   our beloved usability experts for input
+ *     -- z.
+ */
+#if 0
     m_dirLister->setNameFilter(adjustedFilter);
     m_dirLister->emitChanges();
+#else
+    m_proxyModel->setFilterRegExp( nameFilter );
+#endif
 }
 
 void DolphinView::applyModeToView()