Edit Book: Search and replace panel: Add arrows to open the list of recently used search and replace expressions

This commit is contained in:
Kovid Goyal 2014-07-12 21:00:21 +05:30
parent 97b192eb7b
commit 4166178747
3 changed files with 41 additions and 15 deletions

View File

@ -376,16 +376,21 @@ class LineEdit(QLineEdit, LineEditECM):
class EditWithComplete(EnComboBox): class EditWithComplete(EnComboBox):
def __init__(self, *args): def __init__(self, *args, **kwargs):
EnComboBox.__init__(self, *args) 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.setCompleter(None)
self.eat_focus_out = True self.eat_focus_out = True
self.installEventFilter(self) self.installEventFilter(self)
# Interface {{{ # Interface {{{
def showPopup(self): def showPopup(self):
orig = self.disable_popup
self.disable_popup = False
try:
self.lineEdit().complete(show_all=True) self.lineEdit().complete(show_all=True)
finally:
self.disable_popup = orig
def update_items_cache(self, complete_items): def update_items_cache(self, complete_items):
self.lineEdit().update_items_cache(complete_items) self.lineEdit().update_items_cache(complete_items)
@ -412,7 +417,15 @@ class EditWithComplete(EnComboBox):
self.lineEdit().all_items = val self.lineEdit().all_items = val
return property(fget=fget, fset=fset) 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): def text(self):
return unicode(self.lineEdit().text()) return unicode(self.lineEdit().text())

View File

@ -21,7 +21,7 @@ import regex
from calibre import prepare_string_for_xml from calibre import prepare_string_for_xml
from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files, choose_save_file 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.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 import tprefs, editors, current_container
from calibre.gui2.tweak_book.widgets import Dialog, BusyCursor from calibre.gui2.tweak_book.widgets import Dialog, BusyCursor
@ -37,19 +37,19 @@ class PushButton(QPushButton):
QPushButton.__init__(self, text, parent) QPushButton.__init__(self, text, parent)
self.clicked.connect(lambda : parent.search_triggered.emit(action)) self.clicked.connect(lambda : parent.search_triggered.emit(action))
class HistoryLineEdit(HistoryLineEdit2): class HistoryBox(HistoryComboBox):
max_history_items = 100 max_history_items = 100
save_search = pyqtSignal() save_search = pyqtSignal()
show_saved_searches = pyqtSignal() show_saved_searches = pyqtSignal()
def __init__(self, parent, clear_msg): def __init__(self, parent, clear_msg):
HistoryLineEdit2.__init__(self, parent) HistoryComboBox.__init__(self, parent)
self.disable_popup = tprefs['disable_completion_popup_for_search'] self.disable_popup = tprefs['disable_completion_popup_for_search']
self.clear_msg = clear_msg self.clear_msg = clear_msg
def contextMenuEvent(self, event): def contextMenuEvent(self, event):
menu = self.createStandardContextMenu() menu = self.lineEdit().createStandardContextMenu()
menu.addSeparator() menu.addSeparator()
menu.addAction(self.clear_msg, self.clear_history) menu.addAction(self.clear_msg, self.clear_history)
menu.addAction((_('Enable completion based on search history') if self.disable_popup else _( 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:')) self.fl = fl = QLabel(_('&Find:'))
fl.setAlignment(Qt.AlignRight | Qt.AlignCenter) 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.save_search.connect(self.save_search)
ft.show_saved_searches.connect(self.show_saved_searches) ft.show_saved_searches.connect(self.show_saved_searches)
ft.initialize('tweak_book_find_edit') 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) fl.setBuddy(ft)
l.addWidget(fl, 0, 0) l.addWidget(fl, 0, 0)
l.addWidget(ft, 0, 1) l.addWidget(ft, 0, 1)
self.rl = rl = QLabel(_('&Replace:')) self.rl = rl = QLabel(_('&Replace:'))
rl.setAlignment(Qt.AlignRight | Qt.AlignCenter) 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.save_search.connect(self.save_search)
rt.show_saved_searches.connect(self.show_saved_searches) rt.show_saved_searches.connect(self.show_saved_searches)
rt.initialize('tweak_book_replace_edit') rt.initialize('tweak_book_replace_edit')

View File

@ -8,15 +8,15 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from PyQt4.Qt import QPushButton, QPixmap, QIcon, QColor, Qt, QColorDialog, pyqtSignal 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 from calibre.gui2.widgets import history
class HistoryLineEdit2(LineEdit): class HistoryMixin(object):
max_history_items = None max_history_items = None
def __init__(self, parent=None, completer_widget=None, sort_func=lambda x:None): def __init__(self, *args, **kwargs):
LineEdit.__init__(self, parent=parent, completer_widget=completer_widget, sort_func=sort_func) pass
@property @property
def store_name(self): def store_name(self):
@ -28,7 +28,10 @@ class HistoryLineEdit2(LineEdit):
self.set_separator(None) self.set_separator(None)
self.update_items_cache(self.history) self.update_items_cache(self.history)
self.setText('') self.setText('')
try:
self.editingFinished.connect(self.save_history) self.editingFinished.connect(self.save_history)
except AttributeError:
self.lineEdit().editingFinished.connect(self.save_history)
def save_history(self): def save_history(self):
ct = unicode(self.text()) ct = unicode(self.text())
@ -48,6 +51,16 @@ class HistoryLineEdit2(LineEdit):
history.set(self.store_name, self.history) history.set(self.store_name, self.history)
self.update_items_cache(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): class ColorButton(QPushButton):
color_changed = pyqtSignal(object) color_changed = pyqtSignal(object)