Commit e9f24f3f authored by Peter Penz's avatar Peter Penz
Browse files

Use QSortFilterProxyModel for sorting KFileItems (thanks to Fredrik for this...

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
parent 8fdd2e90
......@@ -24,6 +24,7 @@ set(dolphin_SRCS
bookmarkselector.cpp
urlbutton.cpp
dolphincontextmenu.cpp
dolphinsortfilterproxymodel.cpp
undomanager.cpp
progressindicator.cpp
iconsviewsettingspage.cpp
......
/***************************************************************************
* 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"
/***************************************************************************
* 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
......@@ -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()
......
......@@ -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_
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment