Enhancement 1933210: Sort Tag browser categories alphabetically. Done with a new tweak.

This commit is contained in:
Charles Haley 2021-06-23 11:40:26 +01:00
parent ea522df226
commit 8d2eb02da8
2 changed files with 34 additions and 4 deletions

View File

@ -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 # 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 # 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 # 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. # using the default order, the one specified by tag_browser_category_default_sort.
# Example: tag_browser_category_order = {'series':1, 'tags':2, '*':3} # Example:
# resulting in the order series, tags, then everything else in default order. # 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_order = {'*':1}
tag_browser_category_default_sort = 'default'
tag_browser_category_default_sort_direction = 'ascending'
#: Specify columns to sort the booklist by on startup #: Specify columns to sort the booklist by on startup

View File

@ -1141,6 +1141,22 @@ class TagsModel(QAbstractItemModel): # {{{
self.categories[category] = tb_categories[category]['name'] self.categories[category] = tb_categories[category]['name']
# Now build the list of fields in display order # 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: try:
order = tweaks['tag_browser_category_order'] order = tweaks['tag_browser_category_order']
if not isinstance(order, dict): if not isinstance(order, dict):
@ -1149,7 +1165,7 @@ class TagsModel(QAbstractItemModel): # {{{
print('Tweak tag_browser_category_order is not valid. Ignored') print('Tweak tag_browser_category_order is not valid. Ignored')
order = {'*': 100} order = {'*': 100}
defvalue = order.get('*', 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 return data
def set_categories_filter(self, txt): def set_categories_filter(self, txt):