1 // vim: set tabstop=4 shiftwidth=4 expandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2012 Aurélien Gâteau <agateau@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
19
20 */
21 // Self
22 #include "kindproxymodel.h"
23
24 // Local
25
26 // KF
27 #include <KDirModel>
28 #include <KFileItem>
29
30 // Qt
31
32 namespace Gwenview
33 {
34 struct KindProxyModelPrivate {
35 MimeTypeUtils::Kinds mKindFilter;
36 };
37
KindProxyModel(QObject * parent)38 KindProxyModel::KindProxyModel(QObject *parent)
39 : QSortFilterProxyModel(parent)
40 , d(new KindProxyModelPrivate)
41 {
42 }
43
~KindProxyModel()44 KindProxyModel::~KindProxyModel()
45 {
46 delete d;
47 }
48
setKindFilter(MimeTypeUtils::Kinds filter)49 void KindProxyModel::setKindFilter(MimeTypeUtils::Kinds filter)
50 {
51 if (d->mKindFilter != filter) {
52 d->mKindFilter = filter;
53 invalidateFilter();
54 }
55 }
56
kindFilter() const57 MimeTypeUtils::Kinds KindProxyModel::kindFilter() const
58 {
59 return d->mKindFilter;
60 }
61
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent) const62 bool KindProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
63 {
64 if (d->mKindFilter == MimeTypeUtils::Kinds()) {
65 return true;
66 }
67 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
68 KFileItem fileItem = index.data(KDirModel::FileItemRole).value<KFileItem>();
69 if (fileItem.isNull()) {
70 return false;
71 }
72 MimeTypeUtils::Kinds kind = MimeTypeUtils::fileItemKind(fileItem);
73 return d->mKindFilter & kind;
74 }
75
76 } // namespace
77