@@ -67,24 +67,46 @@ def __init__(self, toolbox):
6767 self .delegate = SettingDelegate ()
6868 self .tree .setItemDelegateForColumn (1 , self .delegate )
6969
70- self .searchBox .textChanged .connect (self .fillTree )
70+ self .searchBox .textChanged .connect (self .textChanged )
7171
7272 self .fillTree ()
7373
7474 self .tree .expanded .connect (self .adjustColumns )
7575
76+ def textChanged (self ):
77+ text = unicode (self .searchBox .text ().lower ())
78+ self ._filterItem (self .model .invisibleRootItem (), text )
79+ if text :
80+ self .tree .expandAll ()
81+ else :
82+ self .tree .collapseAll ()
83+
84+ def _filterItem (self , item , text ):
85+ if item .hasChildren ():
86+ show = False
87+ for i in xrange (item .rowCount ()):
88+ child = item .child (i )
89+ showChild = self ._filterItem (child , text )
90+ show = (showChild or show )
91+ self .tree .setRowHidden (item .row (), item .index ().parent (), not show )
92+ return show
93+
94+ elif isinstance (item , QStandardItem ):
95+ hide = bool (text ) and (text not in item .text ().lower ())
96+ self .tree .setRowHidden (item .row (), item .index ().parent (), hide )
97+ return not hide
98+
7699 def fillTree (self ):
100+ self .fillTreeUsingProviders ()
101+
102+ def fillTreeUsingProviders (self ):
77103 self .items = {}
78104 self .model .clear ()
79105 self .model .setHorizontalHeaderLabels ([self .tr ('Setting' ),
80106 self .tr ('Value' )])
81107
82- text = unicode (self .searchBox .text ())
83108 settings = ProcessingConfig .getSettings ()
84109
85- # Let's check if menu's children have any matches vs search wildcard
86- isAnyMenuChildMatchesWildcard = False
87-
88110 rootItem = self .model .invisibleRootItem ()
89111
90112 """
@@ -99,25 +121,18 @@ def fillTree(self):
99121 emptyItem = QStandardItem ()
100122 emptyItem .setEditable (False )
101123
102- isAnyMenuChildMatchesWildcard = False
103-
124+ rootItem .insertRow (0 , [groupItem , emptyItem ])
104125 # add menu item only if it has any search matches
105126 for setting in settings [group ]:
106127 if setting .hidden or setting .name .startswith ("MENU_" ):
107128 continue
108129
109- if text == '' or text .lower () in setting .description .lower ():
110- labelItem = QStandardItem (setting .description )
111- labelItem .setIcon (icon )
112- labelItem .setEditable (False )
113- self .items [setting ] = SettingItem (setting )
114- groupItem .insertRow (0 , [labelItem , self .items [setting ]])
115-
116- isAnyMenuChildMatchesWildcard = True
130+ labelItem = QStandardItem (setting .description )
131+ labelItem .setIcon (icon )
132+ labelItem .setEditable (False )
133+ self .items [setting ] = SettingItem (setting )
134+ groupItem .insertRow (0 , [labelItem , self .items [setting ]])
117135
118- # If menu have any search matches with children - show it
119- if isAnyMenuChildMatchesWildcard :
120- rootItem .insertRow (0 , [groupItem , emptyItem ])
121136
122137 """
123138 Filter 'Providers' items
@@ -129,9 +144,7 @@ def fillTree(self):
129144 emptyItem = QStandardItem ()
130145 emptyItem .setEditable (False )
131146
132- # Let's check if menu's children have any matches vs search wildcard
133- isAnyMenuChildMatchesWildcard = False
134-
147+ rootItem .insertRow (0 , [providersItem , emptyItem ])
135148 for group in settings .keys ():
136149 if group in priorityKeys :
137150 continue
@@ -141,29 +154,20 @@ def fillTree(self):
141154 groupItem .setIcon (icon )
142155 groupItem .setEditable (False )
143156
144- isAnyChildMatchesWildcard = False
145-
146157 for setting in settings [group ]:
147158 if setting .hidden :
148159 continue
149160
150- if text == '' or text .lower () in setting .description .lower ():
151- labelItem = QStandardItem (setting .description )
152- labelItem .setIcon (icon )
153- labelItem .setEditable (False )
154- self .items [setting ] = SettingItem (setting )
155- groupItem .insertRow (0 , [labelItem , self .items [setting ]])
156- isAnyChildMatchesWildcard = True
157- isAnyMenuChildMatchesWildcard = True
161+ labelItem = QStandardItem (setting .description )
162+ labelItem .setIcon (icon )
163+ labelItem .setEditable (False )
164+ self .items [setting ] = SettingItem (setting )
165+ groupItem .insertRow (0 , [labelItem , self .items [setting ]])
158166
159167 emptyItem = QStandardItem ()
160168 emptyItem .setEditable (False )
169+ providersItem .appendRow ([groupItem , emptyItem ])
161170
162- if isAnyChildMatchesWildcard or text .lower () in group .lower ():
163- providersItem .appendRow ([groupItem , emptyItem ])
164-
165- if isAnyMenuChildMatchesWildcard :
166- rootItem .insertRow (0 , [providersItem , emptyItem ])
167171
168172 """
169173 Filter 'Menus' items
@@ -175,47 +179,33 @@ def fillTree(self):
175179 emptyItem = QStandardItem ()
176180 emptyItem .setEditable (False )
177181
178- # Let's check if menu's children have any matches vs search wildcard
179- isAnyMenuChildMatchesWildcard = False
182+ rootItem .insertRow (0 , [menusItem , emptyItem ])
180183
181184 providers = Processing .providers
182185 for provider in providers :
183- groupItem = QStandardItem (provider .getDescription ())
186+ providerDescription = provider .getDescription ()
187+ groupItem = QStandardItem (providerDescription )
184188 icon = provider .getIcon ()
185189 groupItem .setIcon (icon )
186190 groupItem .setEditable (False )
187191
188- # Let's check if provider's children have any matches vs search wildcard
189- isAnyChildMatchesWildcard = False
190-
191192 for alg in provider .algs :
192193 labelItem = QStandardItem (alg .name )
193194 labelItem .setIcon (icon )
194195 labelItem .setEditable (False )
195- setting = ProcessingConfig .settings ["MENU_" + alg .commandLineName ()]
196+ try :
197+ setting = ProcessingConfig .settings ["MENU_" + alg .commandLineName ()]
198+ except :
199+ continue
196200 self .items [setting ] = SettingItem (setting )
197201
198- if text == '' or text .lower () in provider .getDescription ().lower ():
199- groupItem .insertRow (0 , [labelItem , self .items [setting ]])
200- # remember that provider have at least one child matching search wildcard
201- isAnyChildMatchesWildcard = True
202- isAnyMenuChildMatchesWildcard = True
202+ groupItem .insertRow (0 , [labelItem , self .items [setting ]])
203203
204204 emptyItem = QStandardItem ()
205205 emptyItem .setEditable (False )
206206
207- # Add provider to the list only if it have any children-matches
208- if isAnyChildMatchesWildcard :
209- menusItem .appendRow ([groupItem , emptyItem ])
207+ menusItem .appendRow ([groupItem , emptyItem ])
210208
211- # add menu item only if it has any search matches
212- if isAnyMenuChildMatchesWildcard :
213- rootItem .insertRow (0 , [menusItem , emptyItem ])
214-
215- # we need to expand our trees if any searching is performing
216- needExpandTree = bool (text != "" )
217- if needExpandTree :
218- self .tree .expandAll ()
219209
220210 self .tree .sortByColumn (0 , Qt .AscendingOrder )
221211 self .adjustColumns ()
0 commit comments