From: Peter Penz Date: Thu, 14 Dec 2006 23:49:30 +0000 (+0000) Subject: Use QSortFilterProxyModel for sorting KFileItems (thanks to Fredrik for this hint... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/1547891be25c90446034f24d0487426e174c7849?ds=sidebyside Use QSortFilterProxyModel for sorting KFileItems (thanks to Fredrik for this hint!). TODO: the implementation does not work yet, as QSortFilerProxyModel::lessThan() is not invoked; I'm too tired currently for finding the root of this ;-) svn path=/trunk/playground/utils/dolphin/; revision=613747 --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3294672bb..7c50ce550 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 index 000000000..56eccc0b1 --- /dev/null +++ b/src/dolphinsortfilterproxymodel.cpp @@ -0,0 +1,72 @@ +/*************************************************************************** + * Copyright (C) 2006 by Peter Penz * + * Copyright (C) 2006 by Gregor Kališnik * + * * + * 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 +#include + +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(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 index 000000000..fd1005bfd --- /dev/null +++ b/src/dolphinsortfilterproxymodel.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (C) 2006 by Peter Penz * + * * + * 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 +#include + +/** + * @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 diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 1dba37ad5..c23cb2baa 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -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(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(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() diff --git a/src/dolphinview.h b/src/dolphinview.h index 6aa5967f6..42e54f802 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -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_