]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinsortfilterproxymodel.cpp
70256f15612ccab55f66012dcebcea336173195e
[dolphin.git] / src / views / dolphinsortfilterproxymodel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Dominic Battre <dominic@battre.de> *
4 * Copyright (C) 2006 by Martin Pool <mbp@canonical.com> *
5 * Copyright (C) 2007 by Rafael Fernández López <ereslibre@kde.org> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "dolphinsortfilterproxymodel.h"
24
25 DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
26 KDirSortFilterProxyModel(parent),
27 m_sorting(DolphinView::SortByName),
28 m_sortOrder(Qt::AscendingOrder)
29 {
30 }
31
32 DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
33 {
34 }
35
36 void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
37 {
38 m_sorting = sorting;
39 KDirSortFilterProxyModel::sort(static_cast<int>(m_sorting), m_sortOrder);
40 }
41
42 void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
43 {
44 m_sortOrder = sortOrder;
45 KDirSortFilterProxyModel::sort(static_cast<int>(m_sorting), m_sortOrder);
46 }
47
48 void DolphinSortFilterProxyModel::setSortFoldersFirst(bool foldersFirst)
49 {
50 if (foldersFirst != sortFoldersFirst()) {
51 KDirSortFilterProxyModel::setSortFoldersFirst(foldersFirst);
52
53 // We need to make sure that the files and folders are really resorted.
54 // Without the following two lines, QSortFilterProxyModel::sort(int column, Qt::SortOrder order)
55 // would do nothing because neither the column nor the sort order have changed.
56 // TODO: remove this hack if we find a better way to force the ProxyModel to re-sort the data.
57 const Qt::SortOrder tmpSortOrder = (m_sortOrder == Qt::AscendingOrder ? Qt::DescendingOrder : Qt::AscendingOrder);
58 KDirSortFilterProxyModel::sort(static_cast<int>(m_sorting), tmpSortOrder);
59
60 // Now comes the real sorting with the old column and sort order
61 KDirSortFilterProxyModel::sort(static_cast<int>(m_sorting), m_sortOrder);
62 }
63 }
64
65 void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder)
66 {
67 m_sorting = sortingForColumn(column);
68 m_sortOrder = sortOrder;
69
70 emit sortingRoleChanged();
71 KDirSortFilterProxyModel::sort(static_cast<int>(m_sorting), sortOrder);
72 }
73
74 DolphinView::Sorting DolphinSortFilterProxyModel::sortingForColumn(int column)
75 {
76 Q_ASSERT(column >= 0);
77 Q_ASSERT(column <= DolphinView::MaxSortingEnum);
78 return static_cast<DolphinView::Sorting>(column);
79 }
80
81 #include "dolphinsortfilterproxymodel.moc"