Make the save_to_disk preference use selected books when opening the template editor. This avoids spurious "must be in GUI" errors, and also provides more information to the user.

This commit is contained in:
Charles Haley 2023-08-16 14:45:48 +01:00
parent 3df98e0995
commit 797b579382

View File

@ -49,7 +49,18 @@ class SaveTemplate(QWidget, Ui_Form):
self.open_editor.clicked.connect(self.do_open_editor)
def do_open_editor(self):
t = TemplateDialog(self, self.opt_template.text(), fm=self.field_metadata)
# Try to get selected books
from calibre.gui2.ui import get_gui
db = get_gui().current_db
view = get_gui().library_view
rows = view.selectionModel().selectedRows()[0:10] # Maximum of 10 books
mi = [db.new_api.get_proxy_metadata(db.data.index_to_id(x.row())) for x in rows]
if not mi:
error_dialog(self, _('Must select books'),
_('One or more books must be selected so the template '
'editor can show the template results'), show=True)
return
t = TemplateDialog(self, self.opt_template.text(), fm=self.field_metadata, mi=mi)
t.setWindowTitle(_('Edit template'))
if t.exec():
self.opt_template.set_value(t.rule[1])
@ -64,6 +75,9 @@ class SaveTemplate(QWidget, Ui_Form):
custom fields, because they may or may not exist.
'''
tmpl = preprocess_template(self.opt_template.text())
# Allow PTM or GPM templates without checking
if tmpl.startswith(('program:', 'python:')):
return True
try:
t = validation_formatter.validate(tmpl)
if t.find(validation_formatter._validation_string) < 0: