diff --git a/src/calibre/gui2/dialogs/template_dialog.py b/src/calibre/gui2/dialogs/template_dialog.py
index 0bd30367b2..7f7c6749e3 100644
--- a/src/calibre/gui2/dialogs/template_dialog.py
+++ b/src/calibre/gui2/dialogs/template_dialog.py
@@ -9,7 +9,8 @@ import json, os, traceback
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,15 @@ class TemplateHighlighter(QSyntaxHighlighter):
self.highlighted_paren = False
def initializeFormats(self):
+ font_name = gprefs.get('gpm_template_editor_font', None)
+ size = gprefs['gpm_template_editor_font_size']
+ if font_name is None:
+ font = QFont()
+ font.setFixedPitch(True)
+ font.setPointSize(size)
+ font_name = font.family()
Config = self.Config
- Config["fontfamily"] = "monospace"
+ Config["fontfamily"] = font_name
pal = QApplication.instance().palette()
for name, color, bold, italic in (
("normal", None, False, False),
@@ -98,7 +106,7 @@ class TemplateHighlighter(QSyntaxHighlighter):
Config["%sfontitalic" % name] = italic
baseFormat = QTextCharFormat()
baseFormat.setFontFamily(Config["fontfamily"])
- Config["fontsize"] = gprefs['gpm_template_editor_font_size']
+ Config["fontsize"] = size
baseFormat.setFontPointSize(Config["fontsize"])
for name in ("normal", "keyword", "builtin", "comment",
@@ -408,8 +416,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
'%s' % (
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 +426,31 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
except Exception:
pass
+ def set_up_font_boxes(self):
+ font_name = gprefs.get('gpm_template_editor_font', None)
+ size = gprefs['gpm_template_editor_font_size']
+ if font_name is None:
+ font = QFont()
+ font.setFixedPitch(True)
+ font.setPointSize(size)
+ else:
+ font = QFont(font_name, pointSize=size)
+ self.font_box.setWritingSystem(QFontDatabase.Latin)
+ self.font_box.setCurrentFont(font)
+ self.font_box.setEditable(False)
+ gprefs['gpm_template_editor_font'] = unicode_type(font.family())
+ self.font_size_box.setValue(size)
+ self.font_box.currentFontChanged.connect(self.font_changed)
+ self.font_size_box.valueChanged.connect(self.font_size_changed)
+ self.highlighter.initializeFormats()
+ self.highlighter.rehighlight()
+
+ def font_changed(self, font):
+ fi = QFontInfo(font)
+ 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()
diff --git a/src/calibre/gui2/dialogs/template_dialog.ui b/src/calibre/gui2/dialogs/template_dialog.ui
index 06a116dd5d..709bf30039 100644
--- a/src/calibre/gui2/dialogs/template_dialog.ui
+++ b/src/calibre/gui2/dialogs/template_dialog.ui
@@ -380,43 +380,54 @@
- -
-
-
- Font size:
-
-
-
- -
-
-
-
-
+
-
+
+
-
+
+
+ Current font:
+
+
+ font_box
+
+
- -
-
-
- Qt::Horizontal
+
-
+
+
+ Select the font for the Template box
-
-
- 0
- 0
-
-
-
+
- -
-
-
- Qt::Horizontal
+
-
+
+
+ Size:
-
- QDialogButtonBox::Cancel|QDialogButtonBox::Ok
+
+ font_size_box
+
+
+
+ -
+
+
+ Select the font size for the Template box
+ -
+
+
+ Qt::Horizontal
+
+
+ QDialogButtonBox::Cancel|QDialogButtonBox::Ok
+
+
+
-
@@ -424,94 +435,95 @@
- -
-
-
- 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
-
-
-
+
-
+
+
-
+
+
+ Template Function Reference
+
+
+
+ -
+
+
+ Function &name:
+
+
+ function
+
+
+
+ -
+
+
+ -
+
+
+ &Function type:
+
+
+ Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop
+
+
+ func_type
+
+
+
+ -
+
+
+ true
+
+
+
+ -
+
+
+ &Documentation:
+
+
+ Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop
+
+
+ documentation
+
+
+
+ -
+
+
+
+ 16777215
+ 75
+
+
+
+
+ -
+
+
+ &Code:
+
+
+ Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop
+
+
+ source_code
+
+
+
+ -
+
+
+
+ 16777215
+ 75
+
+
+
+
+
-