delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / plasma / applets / folderview / proxymodel.cpp
blob7d9b23a3ff8e1499998036f30bd6280d69587d4f
1 /*
2 * Copyright © 2008 Fredrik Höglund <fredrik@kde.org>
3 * Copyright © 2008 Rafael Fernández López <ereslibre@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "proxymodel.h"
23 #include <KDirModel>
24 #include <KStringHandler>
27 ProxyModel::ProxyModel(QObject *parent)
28 : QSortFilterProxyModel(parent), m_sortDirsFirst(true)
30 setSupportedDragActions(Qt::CopyAction | Qt::MoveAction | Qt::LinkAction);
33 ProxyModel::~ProxyModel()
37 void ProxyModel::setFilterMode(FilterMode filterMode)
39 m_filterMode = filterMode;
40 invalidateFilter();
43 ProxyModel::FilterMode ProxyModel::filterMode() const
45 return m_filterMode;
48 void ProxyModel::setMimeTypeFilterList(const QStringList &mimeList)
50 m_mimeList = mimeList;
51 invalidateFilter();
54 const QStringList &ProxyModel::mimeTypeFilterList() const
56 return m_mimeList;
59 void ProxyModel::setSortDirectoriesFirst(bool enable)
61 m_sortDirsFirst = enable;
64 bool ProxyModel::sortDirectoriesFirst() const
66 return m_sortDirsFirst;
69 QModelIndex ProxyModel::indexForUrl(const KUrl &url) const
71 const KDirModel *dirModel = static_cast<KDirModel*>(sourceModel());
72 return mapFromSource(dirModel->indexForUrl(url));
75 KFileItem ProxyModel::itemForIndex(const QModelIndex &index) const
77 const KDirModel *dirModel = static_cast<KDirModel*>(sourceModel());
78 return dirModel->itemForIndex(mapToSource(index));
81 bool ProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
83 const KDirModel *dirModel = static_cast<KDirModel*>(sourceModel());
84 const KFileItem item1 = dirModel->itemForIndex(left);
85 const KFileItem item2 = dirModel->itemForIndex(right);
87 // Sort directories first
88 if (m_sortDirsFirst) {
89 if (item1.isDir() && !item2.isDir()) {
90 return true;
92 if (!item1.isDir() && item2.isDir()) {
93 return false;
97 const QString name1 = dirModel->data(left).toString();
98 const QString name2 = dirModel->data(right).toString();
100 return KStringHandler::naturalCompare(name1, name2, Qt::CaseInsensitive) < 0;
103 ProxyModel::FilterMode ProxyModel::filterModeFromInt(int filterMode)
105 switch (filterMode) {
106 case 0:
107 return ProxyModel::NoFilter;
108 case 1:
109 return ProxyModel::FilterShowMatches;
110 default:
111 return ProxyModel::FilterHideMatches;
115 bool ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
117 const KDirModel *dirModel = static_cast<KDirModel*>(sourceModel());
118 const KFileItem item = dirModel->itemForIndex(dirModel->index(sourceRow, KDirModel::Name, sourceParent));
120 bool invertResult = false;
121 switch (m_filterMode) {
122 case NoFilter:
123 return true;
124 case FilterHideMatches:
125 invertResult = true; // fall through
126 case FilterShowMatches: {
127 // Mime type check
128 bool ret = m_mimeList.contains(item.determineMimeType()->name());
129 if (!ret) {
130 return invertResult ? true : false;
132 // Pattern check
133 const QString regExpOrig = filterRegExp().pattern();
134 const QStringList regExps = regExpOrig.split(' ');
135 foreach (const QString &regExpStr, regExps) {
136 QRegExp regExp(regExpStr);
137 regExp.setPatternSyntax(QRegExp::Wildcard);
138 regExp.setCaseSensitivity(Qt::CaseInsensitive);
140 if (regExp.indexIn(item.name()) != -1) {
141 return invertResult ? false : true;
144 break;
148 return invertResult ? true : false;