mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Pre-fill search box with selected text when starting a search
This commit is contained in:
parent
d893799106
commit
bfba63b246
@ -313,6 +313,14 @@ class Boss(QObject):
|
||||
if ed.has_marked_text:
|
||||
self.gui.central.search_panel.set_where('selected-text')
|
||||
|
||||
def show_find(self):
|
||||
self.gui.central.show_find()
|
||||
ed = self.gui.central.current_editor
|
||||
if ed is not None:
|
||||
text = ed.selected_text
|
||||
if text and text.strip():
|
||||
self.gui.central.pre_fill_search(text)
|
||||
|
||||
def search(self, action, overrides=None):
|
||||
' Run a search/replace '
|
||||
sp = self.gui.central.search_panel
|
||||
|
@ -72,6 +72,10 @@ class TextEdit(QPlainTextEdit):
|
||||
self.document().setModified(bool(val))
|
||||
return property(fget=fget, fset=fset)
|
||||
|
||||
@property
|
||||
def selected_text(self):
|
||||
return unicode(self.textCursor().selectedText())
|
||||
|
||||
def sizeHint(self):
|
||||
return self.size_hint
|
||||
|
||||
|
@ -86,6 +86,10 @@ class Editor(QMainWindow):
|
||||
def redo(self):
|
||||
self.editor.redo()
|
||||
|
||||
@property
|
||||
def selected_text(self):
|
||||
return self.editor.selected_text
|
||||
|
||||
# Search and replace {{{
|
||||
def mark_selected_text(self):
|
||||
self.editor.mark_selected_text()
|
||||
|
@ -226,6 +226,12 @@ class SearchWidget(QWidget):
|
||||
def save_state(self):
|
||||
tprefs.set('find-widget-state', self.state)
|
||||
|
||||
def pre_fill(self, text):
|
||||
if self.mode == 'regex':
|
||||
text = regex.escape(text, special_only=True)
|
||||
self.find = text
|
||||
self.find_text.setSelection(0, len(text)+10)
|
||||
|
||||
# }}}
|
||||
|
||||
regex_cache = {}
|
||||
@ -251,6 +257,7 @@ class SearchPanel(QWidget):
|
||||
l.addWidget(self.widget)
|
||||
self.restore_state, self.save_state = self.widget.restore_state, self.widget.save_state
|
||||
self.widget.search_triggered.connect(self.search_triggered)
|
||||
self.pre_fill = self.widget.pre_fill
|
||||
|
||||
def hide_panel(self):
|
||||
self.setVisible(False)
|
||||
|
@ -113,6 +113,9 @@ class Central(QStackedWidget):
|
||||
def show_find(self):
|
||||
self.search_panel.show_panel()
|
||||
|
||||
def pre_fill_search(self, text):
|
||||
self.search_panel.pre_fill(text)
|
||||
|
||||
class Main(MainWindow):
|
||||
|
||||
APP_NAME = _('Tweak Book')
|
||||
@ -231,7 +234,7 @@ class Main(MainWindow):
|
||||
|
||||
# Search actions
|
||||
group = _('Search')
|
||||
self.action_find = reg('search.png', _('&Find/Replace'), self.central.show_find, 'find-replace', ('Ctrl+F',), _('Show the Find/Replace panel'))
|
||||
self.action_find = reg('search.png', _('&Find/Replace'), self.boss.show_find, 'find-replace', ('Ctrl+F',), _('Show the Find/Replace panel'))
|
||||
def sreg(name, text, action, overrides={}, keys=(), description=None, icon=None):
|
||||
return reg(icon, text, partial(self.boss.search, action, overrides), name, keys, description or text.replace('&', ''))
|
||||
self.action_find_next = sreg('find-next', _('Find &Next'),
|
||||
|
Loading…
x
Reference in New Issue
Block a user