mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
Make rename search into edit search
This commit is contained in:
parent
3d9e2fe6ad
commit
0f42a3a53a
@ -4,8 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
from PyQt5.Qt import (
|
from PyQt5.Qt import (
|
||||||
QFormLayout, QIcon, QInputDialog, QLabel, QLineEdit, QListWidget, Qt,
|
QFormLayout, QIcon, QLabel, QLineEdit, QListWidget, Qt, QVBoxLayout
|
||||||
QVBoxLayout
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from calibre import prepare_string_for_xml
|
from calibre import prepare_string_for_xml
|
||||||
@ -23,8 +22,10 @@ def commit_searches(searches):
|
|||||||
|
|
||||||
class AddSavedSearch(Dialog):
|
class AddSavedSearch(Dialog):
|
||||||
|
|
||||||
def __init__(self, parent=None, search=None, commit_changes=True):
|
def __init__(self, parent=None, search=None, commit_changes=True, label=None, validate=None):
|
||||||
self.initial_search = search
|
self.initial_search = search
|
||||||
|
self.validate = validate
|
||||||
|
self.label = label
|
||||||
self.commit_changes = commit_changes
|
self.commit_changes = commit_changes
|
||||||
Dialog.__init__(
|
Dialog.__init__(
|
||||||
self, _('Add a new Saved search'), 'add-saved-search', parent)
|
self, _('Add a new Saved search'), 'add-saved-search', parent)
|
||||||
@ -39,7 +40,7 @@ class AddSavedSearch(Dialog):
|
|||||||
self.l = l = QFormLayout(self)
|
self.l = l = QFormLayout(self)
|
||||||
l.setFieldGrowthPolicy(l.AllNonFixedFieldsGrow)
|
l.setFieldGrowthPolicy(l.AllNonFixedFieldsGrow)
|
||||||
|
|
||||||
self.la = la = QLabel(_(
|
self.la = la = QLabel(self.label or _(
|
||||||
'You can create a <i>Saved search</i>, for frequently used searches here.'
|
'You can create a <i>Saved search</i>, for frequently used searches here.'
|
||||||
' The search will be visible under <i>Searches</i> in the Tag browser,'
|
' The search will be visible under <i>Searches</i> in the Tag browser,'
|
||||||
' using the name that you specify here.'))
|
' using the name that you specify here.'))
|
||||||
@ -60,7 +61,6 @@ class AddSavedSearch(Dialog):
|
|||||||
l.addRow(self.bb)
|
l.addRow(self.bb)
|
||||||
|
|
||||||
def accept(self):
|
def accept(self):
|
||||||
Dialog.accept(self)
|
|
||||||
name = self.sname.text().strip()
|
name = self.sname.text().strip()
|
||||||
if not name:
|
if not name:
|
||||||
return error_dialog(
|
return error_dialog(
|
||||||
@ -76,6 +76,11 @@ class AddSavedSearch(Dialog):
|
|||||||
_('You must specify a search expression for the Saved search'),
|
_('You must specify a search expression for the Saved search'),
|
||||||
show=True)
|
show=True)
|
||||||
self.accepted_data = name, expression
|
self.accepted_data = name, expression
|
||||||
|
if self.validate is not None:
|
||||||
|
err = self.validate(name, expression)
|
||||||
|
if err:
|
||||||
|
return error_dialog(self, _('Invalid saved search'), err, show=True)
|
||||||
|
Dialog.accept(self)
|
||||||
if self.commit_changes:
|
if self.commit_changes:
|
||||||
if icu_lower(name) in self.search_names:
|
if icu_lower(name) in self.search_names:
|
||||||
self.searches.pop(self.search_names[icu_lower(name)], None)
|
self.searches.pop(self.search_names[icu_lower(name)], None)
|
||||||
@ -102,11 +107,12 @@ class SavedSearchEditor(Dialog):
|
|||||||
b.setIcon(QIcon(I('minus.png')))
|
b.setIcon(QIcon(I('minus.png')))
|
||||||
b.clicked.connect(self.del_search)
|
b.clicked.connect(self.del_search)
|
||||||
|
|
||||||
b = self.bb.addButton(_('Re&name search'), self.bb.ActionRole)
|
b = self.bb.addButton(_('&Edit search'), self.bb.ActionRole)
|
||||||
b.setIcon(QIcon(I('modified.png')))
|
b.setIcon(QIcon(I('modified.png')))
|
||||||
b.clicked.connect(self.rename_search)
|
b.clicked.connect(self.edit_search)
|
||||||
|
|
||||||
self.slist = QListWidget(self)
|
self.slist = QListWidget(self)
|
||||||
|
self.slist.setAlternatingRowColors(True)
|
||||||
self.searches = {name: db.saved_search_lookup(name) for name in db.saved_search_names()}
|
self.searches = {name: db.saved_search_lookup(name) for name in db.saved_search_names()}
|
||||||
self.populate_search_list()
|
self.populate_search_list()
|
||||||
if self.initial_search is not None and self.initial_search in self.searches:
|
if self.initial_search is not None and self.initial_search in self.searches:
|
||||||
@ -166,13 +172,26 @@ class SavedSearchEditor(Dialog):
|
|||||||
self.slist.takeItem(self.slist.currentRow())
|
self.slist.takeItem(self.slist.currentRow())
|
||||||
del self.searches[n]
|
del self.searches[n]
|
||||||
|
|
||||||
def rename_search(self):
|
def edit_search(self):
|
||||||
n = self.current_search_name
|
n = self.current_search_name
|
||||||
if n:
|
if not n:
|
||||||
text, ok = QInputDialog.getText(self, _('Rename saved search'), _('&New name:'))
|
return
|
||||||
if ok and text:
|
d = AddSavedSearch(parent=self, commit_changes=False, label=_('Edit the name and/or expression below.'), validate=self.validate_edit)
|
||||||
self.slist.currentItem().setText(text)
|
d.setWindowTitle(_('Edit saved search'))
|
||||||
self.searches[text] = self.searches.pop(n)
|
d.sname.setText(n)
|
||||||
|
d.search.setText(self.searches[n])
|
||||||
|
if d.exec_() != d.Accepted:
|
||||||
|
return
|
||||||
|
name, expression = d.accepted_data
|
||||||
|
self.slist.currentItem().setText(name)
|
||||||
|
del self.searches[n]
|
||||||
|
self.searches[name] = expression
|
||||||
|
self.current_index_changed(self.slist.currentItem())
|
||||||
|
|
||||||
|
def validate_edit(self, name, expression):
|
||||||
|
q = self.current_search_name
|
||||||
|
if icu_lower(name) in {icu_lower(n) for n in self.searches if n != q}:
|
||||||
|
return _('A saved search with the name {} already exists. Choose another name').format(name)
|
||||||
|
|
||||||
def select_search(self, name):
|
def select_search(self, name):
|
||||||
items = self.slist.findItems(name, Qt.MatchFixedString | Qt.MatchCaseSensitive)
|
items = self.slist.findItems(name, Qt.MatchFixedString | Qt.MatchCaseSensitive)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user