mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
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
This commit is contained in:
parent
1f85e01ad5
commit
45349d9c38
@ -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):
|
||||
|
@ -19,6 +19,13 @@
|
||||
<property name="windowTitle">
|
||||
<string>Edit template</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QScrollArea" name="scroll_area">
|
||||
<property name="widgetResizable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<widget class="QWidget" name="wid1">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QWidget" name="color_layout">
|
||||
@ -223,14 +230,167 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0">
|
||||
<item row="11" column="0">
|
||||
<widget class="QLabel" name="user_label_1">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="11" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_1" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="12" column="0">
|
||||
<widget class="QLabel" name="user_label_2">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="12" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_2" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="13" column="0">
|
||||
<widget class="QLabel" name="user_label_3">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="13" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_3" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="14" column="0">
|
||||
<widget class="QLabel" name="user_label_4">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="14" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_4" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="15" column="0">
|
||||
<widget class="QLabel" name="user_label_5">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="15" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_5" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="16" column="0">
|
||||
<widget class="QLabel" name="user_label_6">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="16" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_6" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="17" column="0">
|
||||
<widget class="QLabel" name="user_label_7">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="17" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_7" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="18" column="0">
|
||||
<widget class="QLabel" name="user_label_8">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="18" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_8" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="19" column="0">
|
||||
<widget class="QLabel" name="user_label_9">
|
||||
<property name="text">
|
||||
<string>User Label</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>template_value</cstring>
|
||||
</property>
|
||||
<property name="visible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="19" column="1">
|
||||
<layout class="BoxLayout" name="user_layout_9" dir="TopToBottom">
|
||||
</layout>
|
||||
</item>
|
||||
<item row="20" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Font size:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="1" colspan="3">
|
||||
<item row="20" column="1" colspan="3">
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="1" column="0">
|
||||
<widget class="QSpinBox" name="font_size_box"/>
|
||||
@ -260,14 +420,14 @@
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="7" column="0" colspan="5">
|
||||
<item row="21" column="0" colspan="5">
|
||||
<widget class="QFrame">
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::HLine</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="8" column="0" colspan="2">
|
||||
<item row="22" column="0" colspan="2">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Template Function Reference</string>
|
||||
@ -277,7 +437,7 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="9" column="0">
|
||||
<item row="23" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Function &name:</string>
|
||||
@ -287,10 +447,10 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="9" column="1" colspan="3">
|
||||
<item row="23" column="1" colspan="3">
|
||||
<widget class="QComboBox" name="function"/>
|
||||
</item>
|
||||
<item row="10" column="0">
|
||||
<item row="24" column="0">
|
||||
<widget class="QLabel" name="label_22">
|
||||
<property name="text">
|
||||
<string>&Function type:</string>
|
||||
@ -303,14 +463,14 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="10" column="1" colspan="3">
|
||||
<item row="24" column="1" colspan="3">
|
||||
<widget class="QLineEdit" name="func_type">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="11" column="0">
|
||||
<item row="25" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>&Documentation:</string>
|
||||
@ -323,7 +483,7 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="12" column="0">
|
||||
<item row="26" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>&Code:</string>
|
||||
@ -336,7 +496,7 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="11" column="1" colspan="3">
|
||||
<item row="25" column="1" colspan="3">
|
||||
<widget class="QPlainTextEdit" name="documentation">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
@ -346,17 +506,24 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="12" column="1" colspan="3">
|
||||
<widget class="QPlainTextEdit" name="source_code"/>
|
||||
<item row="26" column="1" colspan="3">
|
||||
<widget class="QPlainTextEdit" name="source_code">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>16777215</width>
|
||||
<height>75</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="13" column="1">
|
||||
<item row="27" column="1">
|
||||
<widget class="QLabel" name="template_tutorial">
|
||||
<property name="openExternalLinks">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="14" column="1">
|
||||
<item row="28" column="1">
|
||||
<widget class="QLabel" name="template_func_reference">
|
||||
<property name="openExternalLinks">
|
||||
<bool>true</bool>
|
||||
@ -367,12 +534,21 @@
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>ColorButton</class>
|
||||
<extends>QPushButton</extends>
|
||||
<header>calibre/gui2/widgets2.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>BoxLayout</class>
|
||||
<extends>QBoxLayout</extends>
|
||||
<header>calibre/gui2/dialogs/template_dialog_box_layout.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections>
|
||||
|
13
src/calibre/gui2/dialogs/template_dialog_box_layout.py
Normal file
13
src/calibre/gui2/dialogs/template_dialog_box_layout.py
Normal file
@ -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)
|
@ -25,11 +25,17 @@
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0" colspan="2" stretch="0">
|
||||
<widget class="QTextBrowser" name="st_textBrowser"/>
|
||||
<widget class="QTextBrowser" name="st_textBrowser">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>16777215</width>
|
||||
<height>75</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1" stretch="1">
|
||||
<widget class="EmbeddedTemplateDialog" name="template_editor">
|
||||
</widget>
|
||||
<widget class="EmbeddedTemplateDialog" name="template_editor"/>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<layout class="QVBoxLayout" name="st_button_layout">
|
||||
@ -76,19 +82,6 @@
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="5" column="0">
|
||||
<spacer name="verticalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>400</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab">
|
||||
|
Loading…
x
Reference in New Issue
Block a user