From 288a5f859312d5db20213c3a2a7b255a4a363e26 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 6 Feb 2022 22:10:35 +0000 Subject: [PATCH] Add a tweak to change the behavior of the tab character in completing combo boxes --- resources/default_tweaks.py | 15 ++++++++++----- src/calibre/gui2/complete2.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index ceca0a6680..9b921e4ffa 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -38,11 +38,9 @@ series_index_auto_increment = 'next' use_series_auto_increment_tweak_when_importing = False #: Add separator after completing an author name -# Should the completion separator be append -# to the end of the completed text to -# automatically begin a new completion operation -# for authors. -# Can be either True or False +# Set this if the completion separator should be appended to the end of the +# completed text to automatically begin a new completion operation for authors. +# It can be either True or False authors_completer_append_separator = False #: Author sort name algorithm @@ -502,7 +500,14 @@ default_tweak_format = None # completions you will now have to press Tab to select one before pressing # Enter. Which technique you prefer will depend on the state of metadata in # your library and your personal editing style. +# +# If preselect_first_completion is False and you want Tab to accept what you +# typed instead of the first completion then set tab_accepts_uncompleted_text +# to True. If you do this then to select from the completions you must press +# the Down or Up arrow keys. The tweak tab_accepts_uncompleted_text is ignored +# if preselect_first_completion is True preselect_first_completion = False +tab_accepts_uncompleted_text = False #: Completion mode when editing authors/tags/series/etc. # By default, when completing items, calibre will show you all the candidates diff --git a/src/calibre/gui2/complete2.py b/src/calibre/gui2/complete2.py index c5923c076e..afac58d160 100644 --- a/src/calibre/gui2/complete2.py +++ b/src/calibre/gui2/complete2.py @@ -82,6 +82,7 @@ class CompleteModel(QAbstractListModel): # {{{ class Completer(QListView): # {{{ item_selected = pyqtSignal(object) + apply_current_text = pyqtSignal() relayout_needed = pyqtSignal() def __init__(self, completer_widget, max_visible_items=7, sort_func=sort_key, strip_completion_entries=True): @@ -101,6 +102,8 @@ class Completer(QListView): # {{{ self.pressed.connect(self.item_chosen) self.installEventFilter(self) self.setFocusPolicy(Qt.FocusPolicy.NoFocus) + self.tab_accepts_uncompleted_text = (tweaks['tab_accepts_uncompleted_text'] and + not tweaks['preselect_first_completion']) def hide(self): self.setCurrentIndex(QModelIndex()) @@ -232,6 +235,9 @@ class Completer(QListView): # {{{ if idx.isValid(): self.item_chosen(idx) self.hide() + elif self.tab_accepts_uncompleted_text: + self.hide() + self.apply_current_text.emit() elif self.model().rowCount() > 0: self.next_match() e.accept() @@ -306,6 +312,8 @@ class LineEdit(QLineEdit, LineEditECM): 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.ConnectionType.QueuedConnection) + self.mcompleter.apply_current_text.connect(self.apply_current_text, + type=Qt.ConnectionType.QueuedConnection) self.mcompleter.relayout_needed.connect(self.relayout) self.mcompleter.setFocusProxy(completer_widget) self.textEdited.connect(self.text_edited) @@ -425,6 +433,14 @@ class LineEdit(QLineEdit, LineEditECM): self.setCursorPosition(len(before_text)) self.item_selected.emit(text) + def apply_current_text(self): + if self.sep is not None: + txt = str(self.text()) + sep_pos = txt.rfind(self.sep) + if sep_pos: + ntxt = txt[sep_pos+1:].strip() + self.completion_selected(ntxt) + class EditWithComplete(EnComboBox):