]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kfileitemmodelfilter.cpp
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / kitemviews / private / kfileitemmodelfilter.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Janardhan Reddy *
3 * <annapareddyjanardhanreddy@gmail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program 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 *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "kfileitemmodelfilter.h"
22
23 #include <QRegularExpression>
24
25 #include <KFileItem>
26
27 KFileItemModelFilter::KFileItemModelFilter() :
28 m_useRegExp(false),
29 m_regExp(nullptr),
30 m_lowerCasePattern(),
31 m_pattern()
32 {
33 }
34
35 KFileItemModelFilter::~KFileItemModelFilter()
36 {
37 delete m_regExp;
38 m_regExp = nullptr;
39 }
40
41 void KFileItemModelFilter::setPattern(const QString& filter)
42 {
43 m_pattern = filter;
44 m_lowerCasePattern = filter.toLower();
45
46 if (filter.contains('*') || filter.contains('?') || filter.contains('[')) {
47 if (!m_regExp) {
48 m_regExp = new QRegularExpression();
49 m_regExp->setPatternOptions(QRegularExpression::CaseInsensitiveOption);
50 }
51 m_regExp->setPattern(QRegularExpression::wildcardToRegularExpression(filter));
52 m_useRegExp = m_regExp->isValid();
53 } else {
54 m_useRegExp = false;
55 }
56 }
57
58 QString KFileItemModelFilter::pattern() const
59 {
60 return m_pattern;
61 }
62
63 void KFileItemModelFilter::setMimeTypes(const QStringList& types)
64 {
65 m_mimeTypes = types;
66 }
67
68 QStringList KFileItemModelFilter::mimeTypes() const
69 {
70 return m_mimeTypes;
71 }
72
73 bool KFileItemModelFilter::hasSetFilters() const
74 {
75 return (!m_pattern.isEmpty() || !m_mimeTypes.isEmpty());
76 }
77
78
79 bool KFileItemModelFilter::matches(const KFileItem& item) const
80 {
81 const bool hasPatternFilter = !m_pattern.isEmpty();
82 const bool hasMimeTypesFilter = !m_mimeTypes.isEmpty();
83
84 // If no filter is set, return true.
85 if (!hasPatternFilter && !hasMimeTypesFilter) {
86 return true;
87 }
88
89 // If both filters are set, return true when both filters are matched
90 if (hasPatternFilter && hasMimeTypesFilter) {
91 return (matchesPattern(item) && matchesType(item));
92 }
93
94 // If only one filter is set, return true when that filter is matched
95 if (hasPatternFilter) {
96 return matchesPattern(item);
97 }
98
99 return matchesType(item);
100 }
101
102 bool KFileItemModelFilter::matchesPattern(const KFileItem& item) const
103 {
104 if (m_useRegExp) {
105 return m_regExp->match(item.text()).hasMatch();
106 } else {
107 return item.text().toLower().contains(m_lowerCasePattern);
108 }
109 }
110
111 bool KFileItemModelFilter::matchesType(const KFileItem& item) const
112 {
113 foreach (const QString& mimeType, m_mimeTypes) {
114 if (item.mimetype() == mimeType) {
115 return true;
116 }
117 }
118
119 return m_mimeTypes.isEmpty();
120 }