From bd7dbfec434d499d0c0131ce8108b5e9710f9b10 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Mon, 13 Dec 2021 12:52:47 +0000 Subject: [PATCH] Take 2 on the numeric sort and the tweaks scroll bar changes --- src/calibre/db/fields.py | 11 ++++------ src/calibre/gui2/preferences/tweaks.py | 30 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/calibre/db/fields.py b/src/calibre/db/fields.py index 293be9a70e..9fc05e0d7f 100644 --- a/src/calibre/db/fields.py +++ b/src/calibre/db/fields.py @@ -25,7 +25,7 @@ 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(): +def sort_value_for_undefined_numbers(): t = tweaks['value_for_undefined_numbers_when_sorting'] try: if t == 'minimum': @@ -38,13 +38,10 @@ def _get_sort_value_for_undefined_numbers(): return 0 -sort_value_for_undefined_numbers = _get_sort_value_for_undefined_numbers() - - def numeric_sort_key(x): # 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 + return x if type(x) in (int, float) else sort_value_for_undefined_numbers() IDENTITY = lambda x: x @@ -77,7 +74,7 @@ class Field: self._default_sort_key = b'' if dt in {'int', 'float', 'rating'}: - self._default_sort_key = sort_value_for_undefined_numbers + self._default_sort_key = sort_value_for_undefined_numbers() self._sort_key = numeric_sort_key elif dt == 'bool': self._default_sort_key = None @@ -280,7 +277,7 @@ class CompositeField(OneToOneField): val = val[:(-2 if p > 1 else -1)].strip() val = atof(val) * p except (TypeError, AttributeError, ValueError, KeyError): - val = sort_value_for_undefined_numbers + val = sort_value_for_undefined_numbers() return val def date_sort_key(self, val): diff --git a/src/calibre/gui2/preferences/tweaks.py b/src/calibre/gui2/preferences/tweaks.py index 93a2bfa1a4..23951d669d 100644 --- a/src/calibre/gui2/preferences/tweaks.py +++ b/src/calibre/gui2/preferences/tweaks.py @@ -6,13 +6,14 @@ import textwrap from collections import OrderedDict from functools import partial +from math import ceil, floor from operator import attrgetter from qt.core import ( QAbstractListModel, QApplication, QDialog, QDialogButtonBox, QFont, QGridLayout, QGroupBox, QIcon, QLabel, QListView, QMenu, QModelIndex, QPlainTextEdit, QComboBox, QPushButton, QSizePolicy, QSplitter, QStyle, QStyledItemDelegate, QAbstractItemView, QItemSelectionModel, - QStyleOptionViewItem, Qt, QVBoxLayout, QWidget, pyqtSignal + QStyleOptionViewItem, Qt, QVBoxLayout, QWidget, pyqtSignal, QTextDocument ) from calibre import isbytestring @@ -139,6 +140,11 @@ class Tweak: # {{{ # }}} +# This is the width of the tweaks list view. Add the vertical scrollbar and +# the margins to it. +tweaks_list_minimum_width = 280 + + class Tweaks(QAbstractListModel, AdaptSQP): # {{{ def __init__(self, parent=None): @@ -156,7 +162,17 @@ class Tweaks(QAbstractListModel, AdaptSQP): # {{{ except: return None if role == Qt.ItemDataRole.DisplayRole: - return textwrap.fill(tweak.name, 40) + # Compute the display length of the string then word wrap it so it + # fits in minimum_width. This seems much harder than it should be. + d = QTextDocument() + d.setDocumentMargin(0) + if tweak.is_customized: + d.setHtml('' + tweak.name + '