Enhancement: add a tweak to provide the sort value for undefined numbers.

This commit is contained in:
Charles Haley 2021-12-12 13:38:03 +00:00 committed by Kovid Goyal
parent 519f06b4af
commit 5d9c5ebd58
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 37 additions and 3 deletions

View File

@ -594,3 +594,16 @@ skip_network_check = False
# Sets the width of the tab stop in the template editor in "average characters".
# For example, a value of 1 results in a space with the width of one average character.
template_editor_tab_stop_width = 4
#: Value for undefined numbers when sorting
# Sets the value to use for undefined numbers when sorting.
# For example, the value -10 sorts undefined numbers as if they were set to -10.
# Use 'maximum' for the largest possible number. Use 'minimum' for the smallest
# possible number. Quotes are optional if entering a number.
# Examples:
# value_for_undefined_numbers_when_sorting = -100
# value_for_undefined_numbers_when_sorting = '2'
# value_for_undefined_numbers_when_sorting = -0.01
# value_for_undefined_numbers_when_sorting = 'minimum'
# value_for_undefined_numbers_when_sorting = 'maximum'
value_for_undefined_numbers_when_sorting = 0

View File

@ -25,8 +25,26 @@ def bool_sort_key(bools_are_tristate):
return (lambda x:{True: 1, False: 2, None: 3}.get(x, 3)) if bools_are_tristate else lambda x:{True: 1, False: 2, None: 2}.get(x, 2)
def _get_sort_value_for_undefined_numbers():
t = tweaks['value_for_undefined_numbers_when_sorting']
try:
if t == 'minimum':
return float('-inf')
if t == 'maximum':
return float('inf')
return float(t)
except:
print('***** Bad value in undefined sort number tweak', t)
return 0
sort_value_for_undefined_numbers = _get_sort_value_for_undefined_numbers()
def numeric_sort_key(x):
return x or 0
# It isn't clear whether this function can ever be called with a non-numeric
# argument, but we check just in case
return x if type(x) in (int, float) else sort_value_for_undefined_numbers
IDENTITY = lambda x: x
@ -59,7 +77,7 @@ class Field:
self._default_sort_key = b''
if dt in {'int', 'float', 'rating'}:
self._default_sort_key = 0
self._default_sort_key = sort_value_for_undefined_numbers
self._sort_key = numeric_sort_key
elif dt == 'bool':
self._default_sort_key = None
@ -262,7 +280,7 @@ class CompositeField(OneToOneField):
val = val[:(-2 if p > 1 else -1)].strip()
val = atof(val) * p
except (TypeError, AttributeError, ValueError, KeyError):
val = 0.0
val = sort_value_for_undefined_numbers
return val
def date_sort_key(self, val):

View File

@ -374,6 +374,9 @@ class TweaksView(QListView):
self.setAlternatingRowColors(True)
self.setSpacing(5)
self.setVerticalScrollMode(QAbstractItemView.ScrollMode.ScrollPerPixel)
# On windows (at least) the automatic scroll bar appearing hides part
# of the last line in the list view. Force it always on.
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
self.setMinimumWidth(300)
def currentChanged(self, cur, prev):