]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinsortfilterproxymodel.cpp
900f2fae6d51bf17a8a99c8a23d49c91d8748d6f
[dolphin.git] / src / dolphinsortfilterproxymodel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 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 #include <config-nepomuk.h>
26
27 #include "dolphinmodel.h"
28
29 #include <kfileitem.h>
30 #include <kdatetime.h>
31 #include <klocale.h>
32 #include <kstringhandler.h>
33
34 static DolphinView::Sorting sortingTypeTable[] =
35 {
36 DolphinView::SortByName, // DolphinModel::Name
37 DolphinView::SortBySize, // DolphinModel::Size
38 DolphinView::SortByDate, // DolphinModel::ModifiedTime
39 DolphinView::SortByPermissions, // DolphinModel::Permissions
40 DolphinView::SortByOwner, // DolphinModel::Owner
41 DolphinView::SortByGroup, // DolphinModel::Group
42 DolphinView::SortByType // DolphinModel::Type
43 };
44
45 DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject* parent) :
46 KDirSortFilterProxyModel(parent),
47 m_sorting(DolphinView::SortByName),
48 m_sortOrder(Qt::AscendingOrder)
49 {
50 }
51
52 DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
53 {
54 }
55
56 void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting)
57 {
58 m_sorting = sorting;
59
60 // change the sorting column by keeping the current sort order
61 KDirSortFilterProxyModel::sort((int) m_sorting, m_sortOrder);
62 }
63
64 void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder)
65 {
66 m_sortOrder = sortOrder;
67
68 // change the sort order by keeping the current column
69 KDirSortFilterProxyModel::sort((int) m_sorting, m_sortOrder);
70 }
71
72 void DolphinSortFilterProxyModel::setSortFoldersFirst(bool foldersFirst)
73 {
74 if (foldersFirst != sortFoldersFirst()) {
75 KDirSortFilterProxyModel::setSortFoldersFirst(foldersFirst);
76
77 // We need to make sure that the files and folders are really resorted.
78 // Without the following two lines, QSortFilterProxyModel::sort(int column, Qt::SortOrder order)
79 // would do nothing because neither the column nor the sort order have changed.
80 // TODO: remove this hack if we find a better way to force the ProxyModel to re-sort the data.
81 Qt::SortOrder tmpSortOrder = (m_sortOrder == Qt::AscendingOrder ? Qt::DescendingOrder : Qt::AscendingOrder);
82 KDirSortFilterProxyModel::sort((int) m_sorting, tmpSortOrder);
83
84 // Now comes the real sorting with the old column and sort order
85 KDirSortFilterProxyModel::sort((int) m_sorting, m_sortOrder);
86 }
87 }
88
89 void DolphinSortFilterProxyModel::sort(int column, Qt::SortOrder sortOrder)
90 {
91 m_sorting = sortingForColumn(column);
92 m_sortOrder = sortOrder;
93
94 emit sortingRoleChanged();
95 KDirSortFilterProxyModel::sort((int) m_sorting, sortOrder);
96 }
97
98 DolphinView::Sorting DolphinSortFilterProxyModel::sortingForColumn(int column)
99 {
100 Q_ASSERT(column >= 0);
101 Q_ASSERT(column < static_cast<int>(sizeof(sortingTypeTable) / sizeof(DolphinView::Sorting)));
102 return sortingTypeTable[column];
103 }
104
105 bool DolphinSortFilterProxyModel::subSortLessThan(const QModelIndex& left,
106 const QModelIndex& right) const
107 {
108 // switch (left.column()) {
109 // case DolphinView::Revision:
110 // return left > right;
111 // ...
112 return KDirSortFilterProxyModel::subSortLessThan(left, right);
113 }
114
115 bool DolphinSortFilterProxyModel::isDirectoryOrHidden(const KFileItem& left,
116 const KFileItem& right,
117 bool& result) const
118 {
119 bool isDirectoryOrHidden = true;
120
121 const bool isLessThan = (sortOrder() == Qt::AscendingOrder);
122 if (left.isDir() && !right.isDir()) {
123 result = isLessThan;
124 } else if (!left.isDir() && right.isDir()) {
125 result = !isLessThan;
126 } else if (left.isHidden() && !right.isHidden()) {
127 result = isLessThan;
128 } else if (!left.isHidden() && right.isHidden()) {
129 result = !isLessThan;
130 } else {
131 isDirectoryOrHidden = false;
132 }
133
134 return isDirectoryOrHidden;
135 }
136
137 #include "dolphinsortfilterproxymodel.moc"