From bdd690df127cba1f785f6bd3da2c5d649d75a5f6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 8 Feb 2011 15:46:02 -0700 Subject: [PATCH] Completion: Restore adding of comma at end after completion for tags type fields. Add a tweak to control if an & is added after completion for author type fields --- resources/default_tweaks.py | 5 +- src/calibre/gui2/complete.py | 65 ++++++++++++--------- src/calibre/gui2/convert/metadata.py | 2 + src/calibre/gui2/dialogs/add_empty_book.py | 2 + src/calibre/gui2/dialogs/metadata_bulk.py | 1 + src/calibre/gui2/dialogs/metadata_single.py | 1 + src/calibre/gui2/dialogs/search.py | 2 + src/calibre/gui2/library/delegates.py | 2 + src/calibre/gui2/metadata/basic_widgets.py | 1 + 9 files changed, 50 insertions(+), 31 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 893c8b6b6a..81088da520 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -32,9 +32,10 @@ series_index_auto_increment = 'next' # Should the completion separator be append # to the end of the completed text to -# automatically begin a new completion operation. +# automatically begin a new completion operation +# for authors. # Can be either True or False -completer_append_separator = False +authors_completer_append_separator = False # The algorithm used to copy author to author_sort diff --git a/src/calibre/gui2/complete.py b/src/calibre/gui2/complete.py index 58020f924a..2eb97b128d 100644 --- a/src/calibre/gui2/complete.py +++ b/src/calibre/gui2/complete.py @@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en' from PyQt4.Qt import QLineEdit, QAbstractListModel, Qt, \ QApplication, QCompleter -from calibre.utils.config import tweaks from calibre.utils.icu import sort_key, lower from calibre.gui2 import NONE from calibre.gui2.widgets import EnComboBox @@ -55,6 +54,8 @@ class MultiCompleteLineEdit(QLineEdit): self.sep = ',' self.space_before_sep = False + self.add_separator = True + self.original_cursor_pos = None self._model = CompleteModel(parent=self) self._completer = c = QCompleter(self._model, self) @@ -82,6 +83,9 @@ class MultiCompleteLineEdit(QLineEdit): def set_space_before_sep(self, space_before): self.space_before_sep = space_before + def set_add_separator(self, what): + self.add_separator = bool(what) + # }}} def item_entered(self, idx): @@ -93,7 +97,7 @@ class MultiCompleteLineEdit(QLineEdit): def update_completions(self): ' Update the list of completions ' - cpos = self.cursorPosition() + self.original_cursor_pos = cpos = self.cursorPosition() text = unicode(self.text()) prefix = text[:cpos] self.current_prefix = prefix @@ -103,38 +107,38 @@ class MultiCompleteLineEdit(QLineEdit): self._completer.setCompletionPrefix(complete_prefix) def get_completed_text(self, text): - ''' - Get completed text from current cursor position and the completion - text - ''' + 'Get completed text in before and after parts' if self.sep is None: - return -1, text + return text, '' else: - cursor_pos = self.cursorPosition() - before_text = unicode(self.text())[:cursor_pos] - after_text = unicode(self.text())[cursor_pos:] - prefix_len = len(before_text.split(self.sep)[-1].lstrip()) - if tweaks['completer_append_separator']: - prefix_len = len(before_text.split(self.sep)[-1].lstrip()) - completed_text = before_text[:cursor_pos - prefix_len] + text + self.sep + ' ' + after_text - prefix_len = prefix_len - len(self.sep) - 1 - if prefix_len < 0: - prefix_len = 0 + cursor_pos = self.original_cursor_pos + if cursor_pos is None: + cursor_pos = self.cursorPosition() + self.original_cursor_pos = None + # Split text + curtext = unicode(self.text()) + before_text = curtext[:cursor_pos] + after_text = curtext[cursor_pos:].rstrip() + # Remove the completion prefix from the before text + before_text = self.sep.join(before_text.split(self.sep)[:-1]).rstrip() + if before_text: + # Add the separator to the end of before_text + if self.space_before_sep: + before_text += ' ' + before_text += self.sep + ' ' + if self.add_separator or after_text: + # Add separator to the end of completed text + if self.space_before_sep: + text = text.rstrip() + ' ' + completed_text = text + self.sep + ' ' else: - prefix_len = len(before_text.split(self.sep)[-1].lstrip()) - completed_text = before_text[:cursor_pos - prefix_len] + text + after_text - return prefix_len, completed_text - + completed_text = text + return before_text + completed_text, after_text def completion_selected(self, text): - prefix_len, ctext = self.get_completed_text(unicode(text)) - if self.sep is None: - self.setText(ctext) - self.setCursorPosition(len(ctext)) - else: - cursor_pos = self.cursorPosition() - self.setText(ctext) - self.setCursorPosition(cursor_pos - prefix_len + len(text)) + before_text, after_text = self.get_completed_text(unicode(text)) + self.setText(before_text + after_text) + self.setCursorPosition(len(before_text)) @dynamic_property def all_items(self): @@ -164,6 +168,9 @@ class MultiCompleteComboBox(EnComboBox): def set_space_before_sep(self, space_before): self.lineEdit().set_space_before_sep(space_before) + def set_add_separator(self, what): + self.lineEdit().set_add_separator(what) + if __name__ == '__main__': diff --git a/src/calibre/gui2/convert/metadata.py b/src/calibre/gui2/convert/metadata.py index 81274f25a8..95dd7623c9 100644 --- a/src/calibre/gui2/convert/metadata.py +++ b/src/calibre/gui2/convert/metadata.py @@ -19,6 +19,7 @@ from calibre.ptempfile import PersistentTemporaryFile from calibre.gui2.convert import Widget from calibre.utils.icu import sort_key from calibre.library.comments import comments_to_html +from calibre.utils.config import tweaks def create_opf_file(db, book_id): mi = db.get_metadata(book_id, index_is_id=True) @@ -108,6 +109,7 @@ class MetadataWidget(Widget, Ui_Form): all_authors.sort(key=lambda x : sort_key(x[1])) self.author.set_separator('&') self.author.set_space_before_sep(True) + self.author.set_add_separator(tweaks['authors_completer_append_separator']) self.author.update_items_cache(self.db.all_author_names()) for i in all_authors: diff --git a/src/calibre/gui2/dialogs/add_empty_book.py b/src/calibre/gui2/dialogs/add_empty_book.py index 9e5fb07308..d4990e14d4 100644 --- a/src/calibre/gui2/dialogs/add_empty_book.py +++ b/src/calibre/gui2/dialogs/add_empty_book.py @@ -9,6 +9,7 @@ from PyQt4.Qt import QDialog, QGridLayout, QLabel, QDialogButtonBox, \ from calibre.ebooks.metadata import authors_to_string, string_to_authors from calibre.utils.icu import sort_key from calibre.gui2.complete import MultiCompleteComboBox +from calibre.utils.config import tweaks class AddEmptyBookDialog(QDialog): @@ -69,6 +70,7 @@ class AddEmptyBookDialog(QDialog): self.authors_combo.set_separator('&') self.authors_combo.set_space_before_sep(True) + self.authors_combo.set_add_separator(tweaks['authors_completer_append_separator']) self.authors_combo.update_items_cache(db.all_author_names()) @property diff --git a/src/calibre/gui2/dialogs/metadata_bulk.py b/src/calibre/gui2/dialogs/metadata_bulk.py index e355144544..9ad61d515b 100644 --- a/src/calibre/gui2/dialogs/metadata_bulk.py +++ b/src/calibre/gui2/dialogs/metadata_bulk.py @@ -781,6 +781,7 @@ class MetadataBulkDialog(ResizableDialog, Ui_MetadataBulkDialog): self.authors.set_separator('&') self.authors.set_space_before_sep(True) + self.authors.set_add_separator(tweaks['authors_completer_append_separator']) self.authors.update_items_cache(self.db.all_author_names()) def initialize_series(self): diff --git a/src/calibre/gui2/dialogs/metadata_single.py b/src/calibre/gui2/dialogs/metadata_single.py index 52d263fe36..d95c905f42 100644 --- a/src/calibre/gui2/dialogs/metadata_single.py +++ b/src/calibre/gui2/dialogs/metadata_single.py @@ -735,6 +735,7 @@ class MetadataSingleDialog(ResizableDialog, Ui_MetadataSingleDialog): self.authors.set_separator('&') self.authors.set_space_before_sep(True) + self.authors.set_add_separator(tweaks['authors_completer_append_separator']) self.authors.update_items_cache(self.db.all_author_names()) def initialize_series(self): diff --git a/src/calibre/gui2/dialogs/search.py b/src/calibre/gui2/dialogs/search.py index 9c91446f3c..b4976e2657 100644 --- a/src/calibre/gui2/dialogs/search.py +++ b/src/calibre/gui2/dialogs/search.py @@ -9,6 +9,7 @@ from calibre.gui2.dialogs.search_ui import Ui_Dialog from calibre.library.caches import CONTAINS_MATCH, EQUALS_MATCH from calibre.gui2 import gprefs from calibre.utils.icu import sort_key +from calibre.utils.config import tweaks box_values = {} @@ -31,6 +32,7 @@ class SearchDialog(QDialog, Ui_Dialog): self.authors_box.setEditText('') self.authors_box.set_separator('&') self.authors_box.set_space_before_sep(True) + self.authors_box.set_add_separator(tweaks['authors_completer_append_separator']) self.authors_box.update_items_cache(db.all_author_names()) all_series = db.all_series() diff --git a/src/calibre/gui2/library/delegates.py b/src/calibre/gui2/library/delegates.py index fed2e42470..87da6818eb 100644 --- a/src/calibre/gui2/library/delegates.py +++ b/src/calibre/gui2/library/delegates.py @@ -177,6 +177,8 @@ class CompleteDelegate(QStyledItemDelegate): # {{{ editor = MultiCompleteLineEdit(parent) editor.set_separator(self.sep) editor.set_space_before_sep(self.space_before_sep) + if self.sep == '&': + editor.set_add_separator(tweaks['authors_completer_append_separator']) if not index.model().is_custom_column(col): all_items = getattr(self.db, self.items_func_name)() else: diff --git a/src/calibre/gui2/metadata/basic_widgets.py b/src/calibre/gui2/metadata/basic_widgets.py index f9058fc333..a135176daf 100644 --- a/src/calibre/gui2/metadata/basic_widgets.py +++ b/src/calibre/gui2/metadata/basic_widgets.py @@ -177,6 +177,7 @@ class AuthorsEdit(MultiCompleteComboBox): self.set_separator('&') self.set_space_before_sep(True) + self.set_add_separator(tweaks['authors_completer_append_separator']) self.update_items_cache(db.all_author_names()) au = db.authors(id_, index_is_id=True)