mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-31 14:33:54 -04:00
Enhancement: add a tweak to provide the sort value for undefined numbers.
This commit is contained in:
parent
519f06b4af
commit
5d9c5ebd58
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user