Enhancement #2027727: Accented characters in tag browser find

This commit is contained in:
Charles Haley 2023-07-14 14:15:11 +01:00
parent 34d96c98dc
commit befbcb112f
2 changed files with 33 additions and 16 deletions

View File

@ -1249,13 +1249,21 @@ class TagsModel(QAbstractItemModel): # {{{
else: else:
use_exact_match = False use_exact_match = False
filter_by = self.filter_categories_by filter_by = self.filter_categories_by
if prefs['use_primary_find_in_search']:
final_equals = lambda x, y: primary_strcmp(x, y) == 0
final_contains = primary_contains
else:
final_equals = lambda x, y: strcmp(x, y) == 0
final_contains = lambda filt, txt: contains(filt, icu_lower(txt))
for category in data.keys(): for category in data.keys():
if use_exact_match: if use_exact_match:
data[category] = [t for t in data[category] data[category] = [t for t in data[category]
if lower(t.name) == filter_by] if final_equals(t.name, filter_by)]
else: else:
data[category] = [t for t in data[category] data[category] = [t for t in data[category]
if lower(t.name).find(filter_by) >= 0] if final_contains(filter_by, t.name)]
# Build a dict of the keys that have data. # Build a dict of the keys that have data.
# Always add user categories so that the constructed hierarchy works. # Always add user categories so that the constructed hierarchy works.
@ -1843,12 +1851,13 @@ class TagsModel(QAbstractItemModel): # {{{
self.path_found = None self.path_found = None
if start_path is None: if start_path is None:
start_path = [] start_path = []
if prefs['use_primary_find_in_search']: if prefs['use_primary_find_in_search']:
final_strcmp = primary_strcmp final_equals = lambda x, y: primary_strcmp(x, y) == 0
final_contains = primary_contains final_contains = primary_contains
else: else:
final_strcmp = strcmp final_equals = lambda x, y: strcmp(x, y) == 0
final_contains = contains final_contains = lambda filt, txt: contains(filt, icu_lower(txt))
def process_tag(depth, tag_index, tag_item, start_path): def process_tag(depth, tag_index, tag_item, start_path):
path = self.path_for_index(tag_index) path = self.path_for_index(tag_index)
@ -1858,8 +1867,8 @@ class TagsModel(QAbstractItemModel): # {{{
if tag is None: if tag is None:
return False return False
name = tag.original_name name = tag.original_name
if (equals_match and final_strcmp(name, txt) == 0) or \ if ((equals_match and final_equals(name, txt)) or
(not equals_match and final_contains(txt, name)): (not equals_match and final_contains(txt, name))):
self.path_found = path self.path_found = path
return True return True
for i,c in enumerate(tag_item.children): for i,c in enumerate(tag_item.children):

View File

@ -577,15 +577,23 @@ class TagBrowserBar(QWidget): # {{{
self.item_search.initialize('tag_browser_search') self.item_search.initialize('tag_browser_search')
self.item_search.completer().setCaseSensitivity(Qt.CaseSensitivity.CaseSensitive) self.item_search.completer().setCaseSensitivity(Qt.CaseSensitivity.CaseSensitive)
self.item_search.setToolTip( self.item_search.setToolTip(
'<p>' +_( _('<p>'
'Search for items. If the text begins with equals (=) the search is ' 'Search for items in the Tag browser. If the search text begins '
'exact match, otherwise it is "contains" finding items containing ' 'with an equals sign (=) then the search is "equals", otherwise '
'the text anywhere in the item name. Both exact and contains ' 'it is "contains". Both the equals and contains searches ignore '
'searches ignore case. You can limit the search to particular ' 'case. If the preference <em>Preferences / Searching / Unaccented '
'categories using syntax similar to search. For example, ' 'characters match accented characters ...</em> is checked then a '
'tags:foo will find foo in any tag, but not in authors etc. Entering ' '<em>Character variant search</em> is used, where characters '
'*foo will collapse all categories then showing only those categories ' 'match regardless of accents, and punctuation is significant. See '
'with items containing the text "foo"') + '</p>') '<em>The search interface</em> in the calibre manual for more explanation.'
'</p><p>'
'You can limit the search to particular categories using syntax '
"similar to calibre's <em>Search</em>. For example, tags:foo will "
'find foo in tags but not in authors etc.'
'</p><p>'
'Entering *foo will collapse all categories before doing the '
'search.'
'</p>'))
ac = QAction(parent) ac = QAction(parent)
parent.addAction(ac) parent.addAction(ac)
parent.keyboard.register_shortcut('tag browser find box', parent.keyboard.register_shortcut('tag browser find box',