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

This commit is contained in:
Kovid Goyal 2011-02-08 15:46:02 -07:00
parent e8033decd5
commit bdd690df12
9 changed files with 50 additions and 31 deletions

View File

@ -32,9 +32,10 @@ series_index_auto_increment = 'next'
# Should the completion separator be append # Should the completion separator be append
# to the end of the completed text to # 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 # Can be either True or False
completer_append_separator = False authors_completer_append_separator = False
# The algorithm used to copy author to author_sort # The algorithm used to copy author to author_sort

View File

@ -9,7 +9,6 @@ __docformat__ = 'restructuredtext en'
from PyQt4.Qt import QLineEdit, QAbstractListModel, Qt, \ from PyQt4.Qt import QLineEdit, QAbstractListModel, Qt, \
QApplication, QCompleter QApplication, QCompleter
from calibre.utils.config import tweaks
from calibre.utils.icu import sort_key, lower from calibre.utils.icu import sort_key, lower
from calibre.gui2 import NONE from calibre.gui2 import NONE
from calibre.gui2.widgets import EnComboBox from calibre.gui2.widgets import EnComboBox
@ -55,6 +54,8 @@ class MultiCompleteLineEdit(QLineEdit):
self.sep = ',' self.sep = ','
self.space_before_sep = False self.space_before_sep = False
self.add_separator = True
self.original_cursor_pos = None
self._model = CompleteModel(parent=self) self._model = CompleteModel(parent=self)
self._completer = c = QCompleter(self._model, self) self._completer = c = QCompleter(self._model, self)
@ -82,6 +83,9 @@ class MultiCompleteLineEdit(QLineEdit):
def set_space_before_sep(self, space_before): def set_space_before_sep(self, space_before):
self.space_before_sep = space_before self.space_before_sep = space_before
def set_add_separator(self, what):
self.add_separator = bool(what)
# }}} # }}}
def item_entered(self, idx): def item_entered(self, idx):
@ -93,7 +97,7 @@ class MultiCompleteLineEdit(QLineEdit):
def update_completions(self): def update_completions(self):
' Update the list of completions ' ' Update the list of completions '
cpos = self.cursorPosition() self.original_cursor_pos = cpos = self.cursorPosition()
text = unicode(self.text()) text = unicode(self.text())
prefix = text[:cpos] prefix = text[:cpos]
self.current_prefix = prefix self.current_prefix = prefix
@ -103,38 +107,38 @@ class MultiCompleteLineEdit(QLineEdit):
self._completer.setCompletionPrefix(complete_prefix) self._completer.setCompletionPrefix(complete_prefix)
def get_completed_text(self, text): def get_completed_text(self, text):
''' 'Get completed text in before and after parts'
Get completed text from current cursor position and the completion
text
'''
if self.sep is None: if self.sep is None:
return -1, text return text, ''
else: else:
cursor_pos = self.original_cursor_pos
if cursor_pos is None:
cursor_pos = self.cursorPosition() cursor_pos = self.cursorPosition()
before_text = unicode(self.text())[:cursor_pos] self.original_cursor_pos = None
after_text = unicode(self.text())[cursor_pos:] # Split text
prefix_len = len(before_text.split(self.sep)[-1].lstrip()) curtext = unicode(self.text())
if tweaks['completer_append_separator']: before_text = curtext[:cursor_pos]
prefix_len = len(before_text.split(self.sep)[-1].lstrip()) after_text = curtext[cursor_pos:].rstrip()
completed_text = before_text[:cursor_pos - prefix_len] + text + self.sep + ' ' + after_text # Remove the completion prefix from the before text
prefix_len = prefix_len - len(self.sep) - 1 before_text = self.sep.join(before_text.split(self.sep)[:-1]).rstrip()
if prefix_len < 0: if before_text:
prefix_len = 0 # 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: else:
prefix_len = len(before_text.split(self.sep)[-1].lstrip()) completed_text = text
completed_text = before_text[:cursor_pos - prefix_len] + text + after_text return before_text + completed_text, after_text
return prefix_len, completed_text
def completion_selected(self, text): def completion_selected(self, text):
prefix_len, ctext = self.get_completed_text(unicode(text)) before_text, after_text = self.get_completed_text(unicode(text))
if self.sep is None: self.setText(before_text + after_text)
self.setText(ctext) self.setCursorPosition(len(before_text))
self.setCursorPosition(len(ctext))
else:
cursor_pos = self.cursorPosition()
self.setText(ctext)
self.setCursorPosition(cursor_pos - prefix_len + len(text))
@dynamic_property @dynamic_property
def all_items(self): def all_items(self):
@ -164,6 +168,9 @@ class MultiCompleteComboBox(EnComboBox):
def set_space_before_sep(self, space_before): def set_space_before_sep(self, space_before):
self.lineEdit().set_space_before_sep(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__': if __name__ == '__main__':

View File

@ -19,6 +19,7 @@ from calibre.ptempfile import PersistentTemporaryFile
from calibre.gui2.convert import Widget from calibre.gui2.convert import Widget
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.library.comments import comments_to_html from calibre.library.comments import comments_to_html
from calibre.utils.config import tweaks
def create_opf_file(db, book_id): def create_opf_file(db, book_id):
mi = db.get_metadata(book_id, index_is_id=True) 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])) all_authors.sort(key=lambda x : sort_key(x[1]))
self.author.set_separator('&') self.author.set_separator('&')
self.author.set_space_before_sep(True) 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()) self.author.update_items_cache(self.db.all_author_names())
for i in all_authors: for i in all_authors:

View File

@ -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.ebooks.metadata import authors_to_string, string_to_authors
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.gui2.complete import MultiCompleteComboBox from calibre.gui2.complete import MultiCompleteComboBox
from calibre.utils.config import tweaks
class AddEmptyBookDialog(QDialog): class AddEmptyBookDialog(QDialog):
@ -69,6 +70,7 @@ class AddEmptyBookDialog(QDialog):
self.authors_combo.set_separator('&') self.authors_combo.set_separator('&')
self.authors_combo.set_space_before_sep(True) 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()) self.authors_combo.update_items_cache(db.all_author_names())
@property @property

View File

@ -781,6 +781,7 @@ class MetadataBulkDialog(ResizableDialog, Ui_MetadataBulkDialog):
self.authors.set_separator('&') self.authors.set_separator('&')
self.authors.set_space_before_sep(True) 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()) self.authors.update_items_cache(self.db.all_author_names())
def initialize_series(self): def initialize_series(self):

View File

@ -735,6 +735,7 @@ class MetadataSingleDialog(ResizableDialog, Ui_MetadataSingleDialog):
self.authors.set_separator('&') self.authors.set_separator('&')
self.authors.set_space_before_sep(True) 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()) self.authors.update_items_cache(self.db.all_author_names())
def initialize_series(self): def initialize_series(self):

View File

@ -9,6 +9,7 @@ from calibre.gui2.dialogs.search_ui import Ui_Dialog
from calibre.library.caches import CONTAINS_MATCH, EQUALS_MATCH from calibre.library.caches import CONTAINS_MATCH, EQUALS_MATCH
from calibre.gui2 import gprefs from calibre.gui2 import gprefs
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.config import tweaks
box_values = {} box_values = {}
@ -31,6 +32,7 @@ class SearchDialog(QDialog, Ui_Dialog):
self.authors_box.setEditText('') self.authors_box.setEditText('')
self.authors_box.set_separator('&') self.authors_box.set_separator('&')
self.authors_box.set_space_before_sep(True) 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()) self.authors_box.update_items_cache(db.all_author_names())
all_series = db.all_series() all_series = db.all_series()

View File

@ -177,6 +177,8 @@ class CompleteDelegate(QStyledItemDelegate): # {{{
editor = MultiCompleteLineEdit(parent) editor = MultiCompleteLineEdit(parent)
editor.set_separator(self.sep) editor.set_separator(self.sep)
editor.set_space_before_sep(self.space_before_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): if not index.model().is_custom_column(col):
all_items = getattr(self.db, self.items_func_name)() all_items = getattr(self.db, self.items_func_name)()
else: else:

View File

@ -177,6 +177,7 @@ class AuthorsEdit(MultiCompleteComboBox):
self.set_separator('&') self.set_separator('&')
self.set_space_before_sep(True) self.set_space_before_sep(True)
self.set_add_separator(tweaks['authors_completer_append_separator'])
self.update_items_cache(db.all_author_names()) self.update_items_cache(db.all_author_names())
au = db.authors(id_, index_is_id=True) au = db.authors(id_, index_is_id=True)