From 8d2eb02da8598830f21e3da0741bead83c9a9d72 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Wed, 23 Jun 2021 11:40:26 +0100 Subject: [PATCH] Enhancement 1933210: Sort Tag browser categories alphabetically. Done with a new tweak. --- resources/default_tweaks.py | 20 +++++++++++++++++--- src/calibre/gui2/tag_browser/model.py | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 1bbc8cd68d..f45493004d 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -133,10 +133,24 @@ categories_collapsed_popularity_template = r'{first.count:d} - {last.count:d}' # the Tag browser. Items are named using their lookup name, and will be sorted # using the number supplied. The lookup name '*' stands for all names that # otherwise do not appear. Two names with the same value will be sorted -# using the default order; the one used when the dict is empty. -# Example: tag_browser_category_order = {'series':1, 'tags':2, '*':3} -# resulting in the order series, tags, then everything else in default order. +# using the default order, the one specified by tag_browser_category_default_sort. +# Example: +# tag_browser_category_order = {'series':1, 'tags':2, '*':3} +# +# results in the order series, tags, then everything else in default order. +# The tweak tag_browser_category_default_sort specifies the sort order before +# applying the category order from the dict. The allowed values are: +# tag_browser_category_default_sort = 'default' # The calibre default order +# tag_browser_category_default_sort = 'display_name' # Sort by the display name of the category +# tag_browser_category_default_sort = 'lookup_name' # Sort by the lookup name of the category +# +# In addition and if the category default sort is not 'default' you can specify +# whether the sort is ascending or descending. This is ignored if the sort is 'default'. +# tag_browser_category_default_sort_direction = 'ascending' +# tag_browser_category_default_sort_direction = 'descending' tag_browser_category_order = {'*':1} +tag_browser_category_default_sort = 'default' +tag_browser_category_default_sort_direction = 'ascending' #: Specify columns to sort the booklist by on startup diff --git a/src/calibre/gui2/tag_browser/model.py b/src/calibre/gui2/tag_browser/model.py index 4f38e57d2b..7a427d58f8 100644 --- a/src/calibre/gui2/tag_browser/model.py +++ b/src/calibre/gui2/tag_browser/model.py @@ -1141,6 +1141,22 @@ class TagsModel(QAbstractItemModel): # {{{ self.categories[category] = tb_categories[category]['name'] # Now build the list of fields in display order + order = tweaks.get('tag_browser_category_default_sort', None) + if order not in ('default', 'display_name', 'lookup_name'): + print('Tweak tag_browser_category_default_sort is not valid. Ignored') + order = 'default' + if order == 'default': + self.row_map = self.categories.keys() + else: + def key_func(val): + if order == 'display_name': + return icu_lower(self.db.field_metadata[val]['name']) + return icu_lower(val[1:] if val.startswith('#') or val.startswith('@') else val) + direction = tweaks.get('tag_browser_category_default_sort_direction', None) + if direction not in ('ascending', 'descending'): + print('Tweak tag_browser_category_default_sort_direction is not valid. Ignored') + direction = 'ascending' + self.row_map = sorted(self.categories, key=key_func, reverse=direction == 'descending') try: order = tweaks['tag_browser_category_order'] if not isinstance(order, dict): @@ -1149,7 +1165,7 @@ class TagsModel(QAbstractItemModel): # {{{ print('Tweak tag_browser_category_order is not valid. Ignored') order = {'*': 100} defvalue = order.get('*', 100) - self.row_map = sorted(self.categories, key=lambda x: order.get(x, defvalue)) + self.row_map = sorted(self.row_map, key=lambda x: order.get(x, defvalue)) return data def set_categories_filter(self, txt):