-
Notifications
You must be signed in to change notification settings - Fork 4
/
Filter.qml
64 lines (48 loc) · 1.44 KB
/
Filter.qml
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
import QtQuick 1.0
Item {
id: component
property alias model: filterModel
property QtObject sourceModel: undefined
property string filter: ""
property string property: ""
Connections {
onFilterChanged: invalidateFilter()
onPropertyChanged: invalidateFilter()
onSourceModelChanged: invalidateFilter()
}
Component.onCompleted: invalidateFilter()
ListModel {
id: filterModel
}
// filters out all items of source model that does not match filter
function invalidateFilter() {
if (sourceModel === undefined)
return;
filterModel.clear();
if (!isFilteringPropertyOk())
return
var length = sourceModel.count
for (var i = 0; i < length; ++i) {
var item = sourceModel.get(i);
if (isAcceptedItem(item)) {
filterModel.append(item)
}
}
}
// returns true if item is accepted by filter
function isAcceptedItem(item) {
if (item[this.property] === undefined)
return false
if (item[this.property].match(this.filter) === null) {
return false
}
return true
}
// checks if it has any sence to process invalidating based on property
function isFilteringPropertyOk() {
if(this.property === undefined || this.property === "") {
return false
}
return true
}
}