-
Notifications
You must be signed in to change notification settings - Fork 238
/
SearchFilter.py
122 lines (86 loc) · 3.14 KB
/
SearchFilter.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class SearchFilter:
Widget = None
target = None
rgx = None
def __init__(self, Widget, target):
self.Widget = Widget
self.target = target
self.rgx = QRegExp(target, Qt.CaseInsensitive, QRegExp.Wildcard)
def search_treewidget(self):
path = self.target.split("/")
cnt = self.Widget.topLevelItemCount()
root = []
for i in range(0, cnt):
root.append(self.Widget.topLevelItem(i))
for item in path:
flag = 0
for r in root:
if r.text(0) == item:
flag = 1
childCount = r.childCount()
newroot = []
for ch in range(0, childCount):
newroot.append(r.child(ch))
# expand this item
r.setExpanded(1)
r.setSelected(1)
else:
r.setHidden(1)
if flag == 0:
for i in range(0, cnt):
self.Widget.topLevelItem(i).setHidden(1)
break
del root[:]
root = newroot[:]
def filter_treewidget(self):
cnt = self.Widget.topLevelItemCount()
root = []
for i in range(0, cnt):
root.append(self.Widget.topLevelItem(i))
# set all qtreewidgetitems to be hidden
it = QTreeWidgetItemIterator(self.Widget)
while it.value():
item = it.value()
item.setHidden(1)
it = it.__iadd__(1)
result = []
for r in root:
del result[:]
self.findChild(r, result)
for i in result:
# expand this item
i.setExpanded(1)
i.setSelected(1)
parent = i
while parent != None:
parent.setHidden(0)
parent.setExpanded(1)
parent = parent.parent()
def findChild(self, root, result):
childCount = root.childCount()
if childCount == 0:
print root.text(0)
if root.text(0).contains(self.rgx):
result.append(root)
return
else:
for ch in range(0, childCount):
self.findChild(root.child(ch), result)
def search_listwidget(self):
total = self.Widget.count()
for i in range(0, total):
item = self.Widget.item(i)
if item.text() == self.target:
item.setHidden(0)
else:
item.setHidden(1)
def filter_listwidget(self):
total = self.Widget.count()
for i in range(0, total):
item = self.Widget.item(i)
if item.text().contains(self.rgx):
item.setHidden(0)
else:
item.setHidden(1)