mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Edit book: Allow the search expression history to remember very short terms and also preserve leading and trailing whitespace
Fixes #1836559 [leading and trailing spaces are trimmed from old search and replace values in e-book editor](https://bugs.launchpad.net/calibre/+bug/1836559)
This commit is contained in:
parent
e06d268dad
commit
ae02e93b22
@ -30,14 +30,15 @@ def containsq(x, prefix):
|
||||
|
||||
class CompleteModel(QAbstractListModel): # {{{
|
||||
|
||||
def __init__(self, parent=None, sort_func=sort_key):
|
||||
def __init__(self, parent=None, sort_func=sort_key, strip_completion_entries=True):
|
||||
QAbstractListModel.__init__(self, parent)
|
||||
self.strip_completion_entries = strip_completion_entries
|
||||
self.sort_func = sort_func
|
||||
self.all_items = self.current_items = ()
|
||||
self.current_prefix = ''
|
||||
|
||||
def set_items(self, items):
|
||||
items = [unicode_type(x.strip()) for x in items]
|
||||
items = [unicode_type(x).strip() if self.strip_completion_entries else unicode_type(x) for x in items]
|
||||
items = [x for x in items if x]
|
||||
items = tuple(sorted(items, key=self.sort_func))
|
||||
self.beginResetModel()
|
||||
@ -84,7 +85,7 @@ class Completer(QListView): # {{{
|
||||
item_selected = pyqtSignal(object)
|
||||
relayout_needed = pyqtSignal()
|
||||
|
||||
def __init__(self, completer_widget, max_visible_items=7, sort_func=sort_key):
|
||||
def __init__(self, completer_widget, max_visible_items=7, sort_func=sort_key, strip_completion_entries=True):
|
||||
QListView.__init__(self)
|
||||
self.disable_popup = False
|
||||
self.completer_widget = weakref.ref(completer_widget)
|
||||
@ -95,7 +96,7 @@ class Completer(QListView): # {{{
|
||||
self.setSelectionBehavior(self.SelectRows)
|
||||
self.setSelectionMode(self.SingleSelection)
|
||||
self.setAlternatingRowColors(True)
|
||||
self.setModel(CompleteModel(self, sort_func=sort_func))
|
||||
self.setModel(CompleteModel(self, sort_func=sort_func, strip_completion_entries=strip_completion_entries))
|
||||
self.setMouseTracking(True)
|
||||
self.entered.connect(self.item_entered)
|
||||
self.activated.connect(self.item_chosen)
|
||||
@ -301,7 +302,7 @@ class LineEdit(QLineEdit, LineEditECM):
|
||||
'''
|
||||
item_selected = pyqtSignal(object)
|
||||
|
||||
def __init__(self, parent=None, completer_widget=None, sort_func=sort_key):
|
||||
def __init__(self, parent=None, completer_widget=None, sort_func=sort_key, strip_completion_entries=True):
|
||||
QLineEdit.__init__(self, parent)
|
||||
|
||||
self.sep = ','
|
||||
@ -311,7 +312,7 @@ class LineEdit(QLineEdit, LineEditECM):
|
||||
completer_widget = (self if completer_widget is None else
|
||||
completer_widget)
|
||||
|
||||
self.mcompleter = Completer(completer_widget, sort_func=sort_func)
|
||||
self.mcompleter = Completer(completer_widget, sort_func=sort_func, strip_completion_entries=strip_completion_entries)
|
||||
self.mcompleter.item_selected.connect(self.completion_selected,
|
||||
type=Qt.QueuedConnection)
|
||||
self.mcompleter.relayout_needed.connect(self.relayout)
|
||||
@ -436,7 +437,9 @@ class EditWithComplete(EnComboBox):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
EnComboBox.__init__(self, *args)
|
||||
self.setLineEdit(LineEdit(self, completer_widget=self, sort_func=kwargs.get('sort_func', sort_key)))
|
||||
self.setLineEdit(LineEdit(
|
||||
self, completer_widget=self, sort_func=kwargs.get('sort_func', sort_key),
|
||||
strip_completion_entries=kwargs.get('strip_completion_entries', False)))
|
||||
self.lineEdit().item_selected.connect(self.item_selected)
|
||||
self.setCompleter(None)
|
||||
self.eat_focus_out = True
|
||||
|
@ -89,9 +89,10 @@ class HistoryBox(HistoryComboBox):
|
||||
max_history_items = 100
|
||||
save_search = pyqtSignal()
|
||||
show_saved_searches = pyqtSignal()
|
||||
min_history_entry_length = 1
|
||||
|
||||
def __init__(self, parent, clear_msg):
|
||||
HistoryComboBox.__init__(self, parent)
|
||||
HistoryComboBox.__init__(self, parent, strip_completion_entries=False)
|
||||
self.disable_popup = tprefs['disable_completion_popup_for_search']
|
||||
self.clear_msg = clear_msg
|
||||
self.ignore_snip_expansion = False
|
||||
|
@ -24,6 +24,7 @@ from polyglot.builtins import unicode_type
|
||||
class HistoryMixin(object):
|
||||
|
||||
max_history_items = None
|
||||
min_history_entry_length = 3
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
@ -45,7 +46,7 @@ class HistoryMixin(object):
|
||||
|
||||
def save_history(self):
|
||||
ct = unicode_type(self.text())
|
||||
if len(ct) > 2:
|
||||
if len(ct) >= self.min_history_entry_length:
|
||||
try:
|
||||
self.history.remove(ct)
|
||||
except ValueError:
|
||||
@ -70,8 +71,8 @@ class HistoryLineEdit2(LineEdit, HistoryMixin):
|
||||
|
||||
class HistoryComboBox(EditWithComplete, HistoryMixin):
|
||||
|
||||
def __init__(self, parent=None):
|
||||
EditWithComplete.__init__(self, parent, sort_func=lambda x:b'')
|
||||
def __init__(self, parent=None, strip_completion_entries=True):
|
||||
EditWithComplete.__init__(self, parent, sort_func=lambda x:b'', strip_completion_entries=strip_completion_entries)
|
||||
|
||||
|
||||
class ColorButton(QPushButton):
|
||||
|
Loading…
x
Reference in New Issue
Block a user