From 45349d9c38db1b99c22bf21c7d7524be3bd2970f Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Thu, 21 Jan 2021 14:36:43 +0000 Subject: [PATCH] Improvements to the template editor dialog 1) Make the dialog scrollable. 2) Add rows where a developer can put more fields while maintaining alignment. 3) Allow passing in a dictionary of formatter functions, allowing the developer to temporarily add new functions. 4) Adjust some sizes to make the dialog scroll more nicely --- src/calibre/gui2/dialogs/template_dialog.py | 28 +- src/calibre/gui2/dialogs/template_dialog.ui | 846 +++++++++++------- .../dialogs/template_dialog_box_layout.py | 13 + .../gui2/preferences/template_functions.ui | 25 +- 4 files changed, 548 insertions(+), 364 deletions(-) create mode 100644 src/calibre/gui2/dialogs/template_dialog_box_layout.py diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py index f93b2c6fc6..0141774014 100644 --- a/src/calibre/gui2/dialogs/template_dialog.py +++ b/src/calibre/gui2/dialogs/template_dialog.py @@ -46,7 +46,7 @@ class TemplateHighlighter(QSyntaxHighlighter): KEYWORDS = ["program", 'if', 'then', 'else', 'elif', 'fi'] - def __init__(self, parent=None): + def __init__(self, parent=None, builtin_functions=None): super(TemplateHighlighter, self).__init__(parent) self.initializeFormats() @@ -56,7 +56,8 @@ class TemplateHighlighter(QSyntaxHighlighter): "keyword")) TemplateHighlighter.Rules.append((QRegExp( "|".join([r"\b%s\b" % builtin for builtin in - formatter_functions().get_builtins()])), + (builtin_functions if builtin_functions else + formatter_functions().get_builtins())])), "builtin")) TemplateHighlighter.Rules.append((QRegExp( @@ -214,7 +215,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): def __init__(self, parent, text, mi=None, fm=None, color_field=None, icon_field_key=None, icon_rule_kind=None, doing_emblem=False, text_is_placeholder=False, dialog_is_st_editor=False, - global_vars={}): + global_vars={}, all_functions=None, builtin_functions=None): QDialog.__init__(self, parent) Ui_TemplateDialog.__init__(self) self.setupUi(self) @@ -314,8 +315,11 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): self.setWindowFlags(self.windowFlags()&(~Qt.WindowType.WindowContextHelpButtonHint)) self.setWindowIcon(icon) + self.all_functions = all_functions if all_functions else formatter_functions().get_functions() + self.builtins = builtin_functions if builtin_functions else formatter_functions().get_builtins() + self.last_text = '' - self.highlighter = TemplateHighlighter(self.textbox.document()) + self.highlighter = TemplateHighlighter(self.textbox.document(), builtin_functions=self.builtins) self.textbox.cursorPositionChanged.connect(self.text_cursor_changed) self.textbox.textChanged.connect(self.textbox_changed) @@ -342,10 +346,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): except: self.builtin_source_dict = {} - self.funcs = formatter_functions().get_functions() - self.builtins = formatter_functions().get_builtins() - - func_names = sorted(self.funcs) + func_names = sorted(self.all_functions) self.function.clear() self.function.addItem('') for f in func_names: @@ -430,7 +431,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): self.text_cursor_changed() self.template_value.setText( SafeFormat().safe_format(cur_text, self.mi, _('EXCEPTION: '), - self.mi, global_vars=self.global_vars)) + self.mi, global_vars=self.global_vars, + template_functions= self.all_functions)) def text_cursor_changed(self): cursor = self.textbox.textCursor() @@ -443,7 +445,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): pos_in_block) def function_type_string(self, name, longform=True): - if self.funcs[name].is_python: + if self.all_functions[name].is_python: if name in self.builtins: return (_('Built-in template function') if longform else _('Built-in function')) @@ -457,12 +459,12 @@ class TemplateDialog(QDialog, Ui_TemplateDialog): self.source_code.clear() self.documentation.clear() self.func_type.clear() - if name in self.funcs: - self.documentation.setPlainText(self.funcs[name].doc) + if name in self.all_functions: + self.documentation.setPlainText(self.all_functions[name].doc) if name in self.builtins and name in self.builtin_source_dict: self.source_code.setPlainText(self.builtin_source_dict[name]) else: - self.source_code.setPlainText(self.funcs[name].program_text) + self.source_code.setPlainText(self.all_functions[name].program_text) self.func_type.setText(self.function_type_string(name, longform=True)) def accept(self): diff --git a/src/calibre/gui2/dialogs/template_dialog.ui b/src/calibre/gui2/dialogs/template_dialog.ui index 6c5b45f666..c71ed66831 100644 --- a/src/calibre/gui2/dialogs/template_dialog.ui +++ b/src/calibre/gui2/dialogs/template_dialog.ui @@ -21,349 +21,520 @@ - - - - - - Set the color of the column: - - - colored_field - - - - - - - - - - - Copy a color name to the clipboard: - - - color_name - - - - - - - - - - - - :/images/edit-copy.png:/images/edit-copy.png - - - Copy the selected color name to the clipboard - - - - - - - - - - - - - - - Kind: - - - - - - - - - - - - - Apply the icon to column: - - - icon_field - - - - - - - - - - - Copy an icon file name to the clipboard: - - - color_name - - - - - - - - - - - - - - - :/images/edit-copy.png:/images/edit-copy.png - - - Copy the selected icon file name to the clipboard - - - - - + + + true + + + + + + + + + + Set the color of the column: + + + colored_field + + + + + + + + + + + Copy a color name to the clipboard: + + + color_name + + + + + + + + + + + + :/images/edit-copy.png:/images/edit-copy.png + + + Copy the selected color name to the clipboard + + + + + + + + + + + + + + + Kind: + + + + + + + + + + + + + Apply the icon to column: + + + icon_field + + + + + + + + + + + Copy an icon file name to the clipboard: + + + color_name + + + + + + + + + + + + + + + :/images/edit-copy.png:/images/edit-copy.png + + + Copy the selected icon file name to the clipboard + + + + + + + Add icon + + + Add an icon file to the set of choices + + + + + + + + + + + + + - Add icon + Template &name: + + + template_name + + + + + + + true - Add an icon file to the set of choices + The name of the callable template + + + + + + + T&emplate: + + + textbox + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + The template program text + + + + + + + D&ocumentation: + + + new_doc + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + Documentation for the function being defined or edited + + + + + + + Template value: + + + template_value + + + The value of the template using the current book in the library view + + + + + + + true + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + User Label + + + template_value + + + false + + + + + + + + + + + Font size: + + + + + + + + + + + + Qt::Horizontal + + + + 0 + 0 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + QFrame::HLine + + + + + + + Template Function Reference + + + function + + + + + + + Function &name: + + + function + + + + + + + + + + &Function type: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + func_type + + + + + + + true + + + + + + + &Documentation: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + documentation + + + + + + + &Code: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + source_code + + + + + + + + 16777215 + 75 + + + + + + + + + 16777215 + 75 + + + + + + + + true + + + + + + + true - - - - - - - - - - - Template &name: - - - template_name - - - - - - - true - - - The name of the callable template - - - - - - - T&emplate: - - - textbox - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - - - - - The template program text - - - - - - - D&ocumentation: - - - new_doc - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - - - - - Documentation for the function being defined or edited - - - - - - - Template value: - - - template_value - - - The value of the template using the current book in the library view - - - - - - - true - - - - - - - Font size: - - - - - - - - - - - - Qt::Horizontal - - - - 0 - 0 - - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - QFrame::HLine - - - - - - - Template Function Reference - - - function - - - - - - - Function &name: - - - function - - - - - - - - - - &Function type: - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - func_type - - - - - - - true - - - - - - - &Documentation: - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - documentation - - - - - - - &Code: - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - source_code - - - - - - - - 16777215 - 75 - - - - - - - - - - - true - - - - - - - true - - - - + + @@ -373,6 +544,11 @@ QPushButton
calibre/gui2/widgets2.h
+ + BoxLayout + QBoxLayout +
calibre/gui2/dialogs/template_dialog_box_layout.h
+
@@ -409,4 +585,4 @@ - + \ No newline at end of file diff --git a/src/calibre/gui2/dialogs/template_dialog_box_layout.py b/src/calibre/gui2/dialogs/template_dialog_box_layout.py new file mode 100644 index 0000000000..a3336e50f8 --- /dev/null +++ b/src/calibre/gui2/dialogs/template_dialog_box_layout.py @@ -0,0 +1,13 @@ +''' +Created on 20 Jan 2021 + +@author: Charles Haley +''' + +from PyQt5.Qt import (QBoxLayout) + + +class BoxLayout(QBoxLayout): + + def __init__(self): + QBoxLayout.__init__(self, QBoxLayout.Direction.TopToBottom) \ No newline at end of file diff --git a/src/calibre/gui2/preferences/template_functions.ui b/src/calibre/gui2/preferences/template_functions.ui index cf58dc766c..22b24555ca 100644 --- a/src/calibre/gui2/preferences/template_functions.ui +++ b/src/calibre/gui2/preferences/template_functions.ui @@ -25,11 +25,17 @@ - + + + + 16777215 + 75 + + + - - + @@ -76,19 +82,6 @@ - - - - Qt::Vertical - - - - 20 - 400 - - - -