]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinsortfilterproxymodel.cpp
900f2fae6d51bf17a8a99c8a23d49c91d8748d6f
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> *
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. *
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. *
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 ***************************************************************************/
23 #include "dolphinsortfilterproxymodel.h"
25 #include <config-nepomuk.h>
27 #include "dolphinmodel.h"
29 #include <kfileitem.h>
30 #include <kdatetime.h>
32 #include <kstringhandler.h>
34 static DolphinView::Sorting sortingTypeTable
[] =
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
45 DolphinSortFilterProxyModel::DolphinSortFilterProxyModel(QObject
* parent
) :
46 KDirSortFilterProxyModel(parent
),
47 m_sorting(DolphinView::SortByName
),
48 m_sortOrder(Qt::AscendingOrder
)
52 DolphinSortFilterProxyModel::~DolphinSortFilterProxyModel()
56 void DolphinSortFilterProxyModel::setSorting(DolphinView::Sorting sorting
)
60 // change the sorting column by keeping the current sort order
61 KDirSortFilterProxyModel::sort((int) m_sorting
, m_sortOrder
);
64 void DolphinSortFilterProxyModel::setSortOrder(Qt::SortOrder sortOrder
)
66 m_sortOrder
= sortOrder
;
68 // change the sort order by keeping the current column
69 KDirSortFilterProxyModel::sort((int) m_sorting
, m_sortOrder
);
72 void DolphinSortFilterProxyModel::setSortFoldersFirst(bool foldersFirst
)
74 if (foldersFirst
!= sortFoldersFirst()) {
75 KDirSortFilterProxyModel::setSortFoldersFirst(foldersFirst
);
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
);
84 // Now comes the real sorting with the old column and sort order
85 KDirSortFilterProxyModel::sort((int) m_sorting
, m_sortOrder
);
89 void DolphinSortFilterProxyModel::sort(int column
, Qt::SortOrder sortOrder
)
91 m_sorting
= sortingForColumn(column
);
92 m_sortOrder
= sortOrder
;
94 emit
sortingRoleChanged();
95 KDirSortFilterProxyModel::sort((int) m_sorting
, sortOrder
);
98 DolphinView::Sorting
DolphinSortFilterProxyModel::sortingForColumn(int column
)
100 Q_ASSERT(column
>= 0);
101 Q_ASSERT(column
< static_cast<int>(sizeof(sortingTypeTable
) / sizeof(DolphinView::Sorting
)));
102 return sortingTypeTable
[column
];
105 bool DolphinSortFilterProxyModel::subSortLessThan(const QModelIndex
& left
,
106 const QModelIndex
& right
) const
108 // switch (left.column()) {
109 // case DolphinView::Revision:
110 // return left > right;
112 return KDirSortFilterProxyModel::subSortLessThan(left
, right
);
115 bool DolphinSortFilterProxyModel::isDirectoryOrHidden(const KFileItem
& left
,
116 const KFileItem
& right
,
119 bool isDirectoryOrHidden
= true;
121 const bool isLessThan
= (sortOrder() == Qt::AscendingOrder
);
122 if (left
.isDir() && !right
.isDir()) {
124 } else if (!left
.isDir() && right
.isDir()) {
125 result
= !isLessThan
;
126 } else if (left
.isHidden() && !right
.isHidden()) {
128 } else if (!left
.isHidden() && right
.isHidden()) {
129 result
= !isLessThan
;
131 isDirectoryOrHidden
= false;
134 return isDirectoryOrHidden
;
137 #include "dolphinsortfilterproxymodel.moc"