From 4166178747f8fbc83bf3d548738b26a89806a9ad Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 12 Jul 2014 21:00:21 +0530 Subject: [PATCH] Edit Book: Search and replace panel: Add arrows to open the list of recently used search and replace expressions --- src/calibre/gui2/complete2.py | 19 ++++++++++++++++--- src/calibre/gui2/tweak_book/search.py | 14 +++++++------- src/calibre/gui2/widgets2.py | 23 ++++++++++++++++++----- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/calibre/gui2/complete2.py b/src/calibre/gui2/complete2.py index 34e7d576b7..4920d9cb99 100644 --- a/src/calibre/gui2/complete2.py +++ b/src/calibre/gui2/complete2.py @@ -376,16 +376,21 @@ class LineEdit(QLineEdit, LineEditECM): class EditWithComplete(EnComboBox): - def __init__(self, *args): + def __init__(self, *args, **kwargs): EnComboBox.__init__(self, *args) - self.setLineEdit(LineEdit(self, completer_widget=self)) + self.setLineEdit(LineEdit(self, completer_widget=self, sort_func=kwargs.get('sort_func', sort_key))) self.setCompleter(None) self.eat_focus_out = True self.installEventFilter(self) # Interface {{{ def showPopup(self): - self.lineEdit().complete(show_all=True) + orig = self.disable_popup + self.disable_popup = False + try: + self.lineEdit().complete(show_all=True) + finally: + self.disable_popup = orig def update_items_cache(self, complete_items): self.lineEdit().update_items_cache(complete_items) @@ -412,7 +417,15 @@ class EditWithComplete(EnComboBox): self.lineEdit().all_items = val return property(fget=fget, fset=fset) + @dynamic_property + def disable_popup(self): + def fget(self): + return self.lineEdit().disable_popup + def fset(self, val): + self.lineEdit().disable_popup = bool(val) + return property(fget=fget, fset=fset) # }}} + def text(self): return unicode(self.lineEdit().text()) diff --git a/src/calibre/gui2/tweak_book/search.py b/src/calibre/gui2/tweak_book/search.py index a61e143cd2..5ce492e9a4 100644 --- a/src/calibre/gui2/tweak_book/search.py +++ b/src/calibre/gui2/tweak_book/search.py @@ -21,7 +21,7 @@ import regex from calibre import prepare_string_for_xml from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files, choose_save_file from calibre.gui2.dialogs.message_box import MessageBox -from calibre.gui2.widgets2 import HistoryLineEdit2 +from calibre.gui2.widgets2 import HistoryComboBox from calibre.gui2.tweak_book import tprefs, editors, current_container from calibre.gui2.tweak_book.widgets import Dialog, BusyCursor @@ -37,19 +37,19 @@ class PushButton(QPushButton): QPushButton.__init__(self, text, parent) self.clicked.connect(lambda : parent.search_triggered.emit(action)) -class HistoryLineEdit(HistoryLineEdit2): +class HistoryBox(HistoryComboBox): max_history_items = 100 save_search = pyqtSignal() show_saved_searches = pyqtSignal() def __init__(self, parent, clear_msg): - HistoryLineEdit2.__init__(self, parent) + HistoryComboBox.__init__(self, parent) self.disable_popup = tprefs['disable_completion_popup_for_search'] self.clear_msg = clear_msg def contextMenuEvent(self, event): - menu = self.createStandardContextMenu() + menu = self.lineEdit().createStandardContextMenu() menu.addSeparator() menu.addAction(self.clear_msg, self.clear_history) menu.addAction((_('Enable completion based on search history') if self.disable_popup else _( @@ -179,18 +179,18 @@ class SearchWidget(QWidget): self.fl = fl = QLabel(_('&Find:')) fl.setAlignment(Qt.AlignRight | Qt.AlignCenter) - self.find_text = ft = HistoryLineEdit(self, _('Clear search history')) + self.find_text = ft = HistoryBox(self, _('Clear search history')) ft.save_search.connect(self.save_search) ft.show_saved_searches.connect(self.show_saved_searches) ft.initialize('tweak_book_find_edit') - ft.returnPressed.connect(lambda : self.search_triggered.emit('find')) + ft.lineEdit().returnPressed.connect(lambda : self.search_triggered.emit('find')) fl.setBuddy(ft) l.addWidget(fl, 0, 0) l.addWidget(ft, 0, 1) self.rl = rl = QLabel(_('&Replace:')) rl.setAlignment(Qt.AlignRight | Qt.AlignCenter) - self.replace_text = rt = HistoryLineEdit(self, _('Clear replace history')) + self.replace_text = rt = HistoryBox(self, _('Clear replace history')) rt.save_search.connect(self.save_search) rt.show_saved_searches.connect(self.show_saved_searches) rt.initialize('tweak_book_replace_edit') diff --git a/src/calibre/gui2/widgets2.py b/src/calibre/gui2/widgets2.py index ad4aaee495..aa5a911e57 100644 --- a/src/calibre/gui2/widgets2.py +++ b/src/calibre/gui2/widgets2.py @@ -8,15 +8,15 @@ __copyright__ = '2013, Kovid Goyal ' from PyQt4.Qt import QPushButton, QPixmap, QIcon, QColor, Qt, QColorDialog, pyqtSignal -from calibre.gui2.complete2 import LineEdit +from calibre.gui2.complete2 import LineEdit, EditWithComplete from calibre.gui2.widgets import history -class HistoryLineEdit2(LineEdit): +class HistoryMixin(object): max_history_items = None - def __init__(self, parent=None, completer_widget=None, sort_func=lambda x:None): - LineEdit.__init__(self, parent=parent, completer_widget=completer_widget, sort_func=sort_func) + def __init__(self, *args, **kwargs): + pass @property def store_name(self): @@ -28,7 +28,10 @@ class HistoryLineEdit2(LineEdit): self.set_separator(None) self.update_items_cache(self.history) self.setText('') - self.editingFinished.connect(self.save_history) + try: + self.editingFinished.connect(self.save_history) + except AttributeError: + self.lineEdit().editingFinished.connect(self.save_history) def save_history(self): ct = unicode(self.text()) @@ -48,6 +51,16 @@ class HistoryLineEdit2(LineEdit): history.set(self.store_name, self.history) self.update_items_cache(self.history) +class HistoryLineEdit2(LineEdit, HistoryMixin): + + def __init__(self, parent=None, completer_widget=None, sort_func=lambda x:None): + LineEdit.__init__(self, parent=parent, completer_widget=completer_widget, sort_func=sort_func) + +class HistoryComboBox(EditWithComplete, HistoryMixin): + + def __init__(self, parent=None): + EditWithComplete.__init__(self, parent, sort_func=lambda x:None) + class ColorButton(QPushButton): color_changed = pyqtSignal(object)