diff --git a/src/calibre/gui2/tag_browser/model.py b/src/calibre/gui2/tag_browser/model.py index e08245f4cf..bfa10c1fcd 100644 --- a/src/calibre/gui2/tag_browser/model.py +++ b/src/calibre/gui2/tag_browser/model.py @@ -418,21 +418,24 @@ class TagsModel(QAbstractItemModel): # {{{ chardict[c][1] = idx # sort the ranges to facilitate detecting overlap - ranges = sorted([(v[0], v[1], c) for c,v in chardict.items()]) - - # Create a list of 'first letters' to use for each item in - # the category. The list is generated using the ranges. Overlaps - # are filled with the character that first occurs. - cl_list = list(repeat(None, len(data[key]))) - for t in ranges: - start = t[0] - c = t[2] - if cl_list[start] is None: - nc = c - else: - nc = cl_list[start] - for i in range(start, t[1]+1): - cl_list[i] = nc + if len(chardict) == 1 and ' ' in chardict: + # The category could not be partitioned. + collapse_model = 'disable' + else: + ranges = sorted([(v[0], v[1], c) for c,v in chardict.items()]) + # Create a list of 'first letters' to use for each item in + # the category. The list is generated using the ranges. Overlaps + # are filled with the character that first occurs. + cl_list = list(repeat(None, len(data[key]))) + for t in ranges: + start = t[0] + c = t[2] + if cl_list[start] is None: + nc = c + else: + nc = cl_list[start] + for i in range(start, t[1]+1): + cl_list[i] = nc for idx,tag in enumerate(data[key]): if clear_rating: @@ -448,13 +451,19 @@ class TagsModel(QAbstractItemModel): # {{{ else: d['last'] = data[key][cat_len-1] name = eval_formatter.safe_format(collapse_template, - d, 'TAG_VIEW', None) - sub_cat = self.create_node(parent=category, data = name, + d, '##TAG_VIEW##', None) + if name.startswith('##TAG_VIEW##'): + # Formatter threw an exception. Don't create subnode + node_parent = category + else: + sub_cat = self.create_node(parent=category, data = name, tooltip = None, temporary=True, category_icon = category_node.icon, category_key=category_node.category_key, icon_map=self.icon_state_map) - sub_cat.tag.is_searchable = False + sub_cat.tag.is_searchable = False + sub_cat.is_gst = is_gst + node_parent = sub_cat else: # by 'first letter' cl = cl_list[idx] if cl != collapse_letter: @@ -465,8 +474,8 @@ class TagsModel(QAbstractItemModel): # {{{ tooltip = None, temporary=True, category_key=category_node.category_key, icon_map=self.icon_state_map) - sub_cat.is_gst = is_gst - node_parent = sub_cat + sub_cat.is_gst = is_gst + node_parent = sub_cat else: node_parent = category