Enhancement #1920574: allow changing fonts in the templat tester

This commit is contained in:
Charles Haley 2021-03-21 15:29:32 +00:00
parent 320c525cfe
commit 7fe23d9e58
2 changed files with 151 additions and 120 deletions

View file

@ -9,7 +9,8 @@
from qt.core import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, QFont,
QRegExp, QApplication, QTextCharFormat, QColor, QCursor,
QIcon, QSize, QPalette, QLineEdit, QByteArray)
QIcon, QSize, QPalette, QLineEdit, QByteArray,
QFontInfo, QFontDatabase)
from calibre import sanitize_file_name
from calibre.constants import config_dir
@ -81,8 +82,9 @@ def __init__(self, parent=None, builtin_functions=None):
self.highlighted_paren = False
def initializeFormats(self):
font = gprefs.get('gpm_template_editor_font', 'monospace')
Config = self.Config
Config["fontfamily"] = "monospace"
Config["fontfamily"] = font
pal = QApplication.instance().palette()
for name, color, bold, italic in (
("normal", None, False, False),
@ -408,8 +410,7 @@ def __init__(self, parent, text, mi=None, fm=None, color_field=None,
'<a href="%s">%s</a>' % (
localize_user_manual_link('https://manual.calibre-ebook.com/generated/en/template_ref.html'), tt))
self.font_size_box.setValue(gprefs['gpm_template_editor_font_size'])
self.font_size_box.valueChanged.connect(self.font_size_changed)
self.set_up_font_boxes()
self.textbox.setFocus()
# Now geometry
try:
@ -419,6 +420,24 @@ def __init__(self, parent, text, mi=None, fm=None, color_field=None,
except Exception:
pass
def set_up_font_boxes(self):
family = gprefs.get('gpm_template_editor_font', 'monospace')
size = gprefs['gpm_template_editor_font_size']
font = QFont(family, pointSize=size)
self.font_box.setWritingSystem(QFontDatabase.Latin)
self.font_box.setCurrentFont(font)
self.font_box.setEditable(False)
self.font_box.currentFontChanged.connect(self.font_changed)
self.font_size_box.setValue(gprefs['gpm_template_editor_font_size'])
self.font_size_box.valueChanged.connect(self.font_size_changed)
def font_changed(self, font):
fi = QFontInfo(font)
gprefs['gpm_template_editor_font_size'] = fi.pointSize()
gprefs['gpm_template_editor_font'] = unicode_type(fi.family())
self.highlighter.initializeFormats()
self.highlighter.rehighlight()
def font_size_changed(self, toWhat):
gprefs['gpm_template_editor_font_size'] = toWhat
self.highlighter.initializeFormats()

View file

@ -380,43 +380,54 @@
<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="20" column="1" colspan="3">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QSpinBox" name="font_size_box"/>
<item row="20" column="0" colspan="2">
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="font_name_label">
<property name="text">
<string>Current font:</string>
</property>
<property name="buddy">
<cstring>font_box</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
<item>
<widget class="QFontComboBox" name="font_box">
<property name="toolTip">
<string>Select the font for the Template box</string>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</widget>
</item>
<item row="1" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<item>
<widget class="QLabel" name="font_size_label">
<property name="text">
<string>Size:</string>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<property name="buddy">
<cstring>font_size_box</cstring>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="font_size_box">
<property name="toolTip">
<string>Select the font size for the Template box</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="20" column="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="21" column="0" colspan="4">
<widget class="QFrame">
<property name="frameShape">
@ -424,94 +435,95 @@
</property>
</widget>
</item>
<item row="22" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>Template Function Reference</string>
</property>
<property name="buddy">
<cstring>function</cstring>
</property>
</widget>
</item>
<item row="23" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Function &amp;name:</string>
</property>
<property name="buddy">
<cstring>function</cstring>
</property>
</widget>
</item>
<item row="23" column="1" colspan="3">
<widget class="QComboBox" name="function"/>
</item>
<item row="24" column="0">
<widget class="QLabel" name="label_22">
<property name="text">
<string>&amp;Function type:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>func_type</cstring>
</property>
</widget>
</item>
<item row="24" column="1" colspan="3">
<widget class="QLineEdit" name="func_type">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="25" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Documentation:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>documentation</cstring>
</property>
</widget>
</item>
<item row="26" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Code:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>source_code</cstring>
</property>
</widget>
</item>
<item row="25" column="1" colspan="3">
<widget class="QPlainTextEdit" name="documentation">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>75</height>
</size>
</property>
</widget>
</item>
<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 row="22" column="0" colspan="4">
<layout class="QGridLayout">
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>Template Function Reference</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Function &amp;name:</string>
</property>
<property name="buddy">
<cstring>function</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="function"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_22">
<property name="text">
<string>&amp;Function type:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>func_type</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="func_type">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Documentation:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>documentation</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPlainTextEdit" name="documentation">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>75</height>
</size>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Code:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>source_code</cstring>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QPlainTextEdit" name="source_code">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>75</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item row="27" column="1">
<widget class="QLabel" name="template_tutorial">