-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortfuzzyfilterproxymodel.cpp
More file actions
106 lines (86 loc) · 2.88 KB
/
Copy pathsortfuzzyfilterproxymodel.cpp
File metadata and controls
106 lines (86 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
---
Copyright (C) 2011 Tomáš Frýda <fryda.tomas@gmail.com>
*/
#include "sortfuzzyfilterproxymodel.h"
#include <QModelIndex>
#include <QString>
#include <QStringList>
SortFuzzyFilterProxyModel::SortFuzzyFilterProxyModel(QObject *parent) :
QSortFilterProxyModel(parent),
m_fuzzyfilter("")
{
}
void SortFuzzyFilterProxyModel::setFuzzyFilterString(const QString &pattern)
{
m_fuzzyfilter = pattern;
invalidateFilter();
QSortFilterProxyModel::sort(0);
}
bool SortFuzzyFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QString _left = sourceModel()->data(left).toString();
QString _right = sourceModel()->data(right).toString();
if (m_fuzzyfilter=="")
return QSortFilterProxyModel::lessThan(left,right);
return getPoints(_left)<getPoints(_right);
}
int SortFuzzyFilterProxyModel::getPoints(const QString &str) const
{
if (m_fuzzyfilter=="")
{
return 0;
}
QStringList _list;
if (m_fuzzyfilter.indexOf("_")>=0)
{
_list = str.split(QRegExp("[/\\-\\\\:\\.]"));
}
else
{
_list = str.split(QRegExp("[/\\-\\\\_:\\.]"));
}
unsigned _index = 0;
int _word = 0;
unsigned _points = 0;
foreach (QChar c, m_fuzzyfilter) {
bool _s = true;
while (_word<_list.count() && _s)
{
if (_list.at(_word).at(_index) == c)
{
_s=false;
_index++;
}
else
{
_points++;
_word++;
_index = 0;
}
}
if (_word>=_list.count() && _s)
return 10000;
}
return _points;
}
bool SortFuzzyFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex _index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QModelIndex _index1 = sourceModel()->index(sourceRow, 1, sourceParent);
QString _first = sourceModel()->data(_index0).toString();
QString _second = sourceModel()->data(_index1).toString();
return (getPoints(_first)<3) || (getPoints(_second)<50);
}