Add tweak to set the tab stop width in the template editor.

This commit is contained in:
Charles Haley 2021-04-18 15:53:39 +01:00
parent 6e3595a680
commit 2da2b1400e
2 changed files with 21 additions and 12 deletions

View File

@ -570,3 +570,8 @@ exclude_fields_on_paste = []
# Useful if for some reason your operating systems network checking # Useful if for some reason your operating systems network checking
# facilities are not reliable (for example NetworkManager on Linux). # facilities are not reliable (for example NetworkManager on Linux).
skip_network_check = False skip_network_check = False
#: Tab stop width in the template editor
# Sets the width of the tab stop in the template editor in "average characters".
# For example, a value of 1 results in a space the width of one average character.
template_editor_tab_stop_width = 4

View File

@ -12,7 +12,7 @@ from qt.core import (Qt, QDialog, QDialogButtonBox, QSyntaxHighlighter, QFont,
QRegExp, QApplication, QTextCharFormat, QColor, QCursor, QRegExp, QApplication, QTextCharFormat, QColor, QCursor,
QIcon, QSize, QPalette, QLineEdit, QByteArray, QFontInfo, QIcon, QSize, QPalette, QLineEdit, QByteArray, QFontInfo,
QFontDatabase, QVBoxLayout, QTableWidget, QTableWidgetItem, QFontDatabase, QVBoxLayout, QTableWidget, QTableWidgetItem,
QComboBox, QAbstractItemView, QTextOption) QComboBox, QAbstractItemView, QTextOption, QFontMetrics)
from calibre import sanitize_file_name from calibre import sanitize_file_name
from calibre.constants import config_dir from calibre.constants import config_dir
@ -21,6 +21,7 @@ from calibre.ebooks.metadata.book.formatter import SafeFormat
from calibre.gui2 import gprefs, error_dialog, choose_files, choose_save_file, pixmap_to_data from calibre.gui2 import gprefs, error_dialog, choose_files, choose_save_file, pixmap_to_data
from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog from calibre.gui2.dialogs.template_dialog_ui import Ui_TemplateDialog
from calibre.library.coloring import (displayable_columns, color_row_key) from calibre.library.coloring import (displayable_columns, color_row_key)
from calibre.utils.config_base import tweaks
from calibre.utils.formatter_functions import formatter_functions from calibre.utils.formatter_functions import formatter_functions
from calibre.utils.formatter import StopException from calibre.utils.formatter import StopException
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
@ -322,10 +323,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.highlighter = TemplateHighlighter(self.textbox.document(), builtin_functions=self.builtins) self.highlighter = TemplateHighlighter(self.textbox.document(), builtin_functions=self.builtins)
self.textbox.cursorPositionChanged.connect(self.text_cursor_changed) self.textbox.cursorPositionChanged.connect(self.text_cursor_changed)
self.textbox.textChanged.connect(self.textbox_changed) self.textbox.textChanged.connect(self.textbox_changed)
self.textbox.setFont(self.get_current_font()) self.set_editor_font()
self.textbox.setTabStopWidth(10)
self.source_code.setTabStopWidth(10)
self.documentation.setReadOnly(True) self.documentation.setReadOnly(True)
self.source_code.setReadOnly(True) self.source_code.setReadOnly(True)
@ -524,6 +523,17 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
font = QFont(font_name, pointSize=size) font = QFont(font_name, pointSize=size)
return font return font
def set_editor_font(self):
font = self.get_current_font()
fm = QFontMetrics(font)
chars = tweaks['template_editor_tab_stop_width']
w = fm.averageCharWidth() * chars
self.textbox.setTabStopDistance(w)
self.source_code.setTabStopDistance(w)
self.textbox.setFont(font)
self.highlighter.initializeFormats()
self.highlighter.rehighlight()
def set_up_font_boxes(self): def set_up_font_boxes(self):
font = self.get_current_font() font = self.get_current_font()
self.font_box.setWritingSystem(QFontDatabase.Latin) self.font_box.setWritingSystem(QFontDatabase.Latin)
@ -533,21 +543,15 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
self.font_size_box.setValue(font.pointSize()) self.font_size_box.setValue(font.pointSize())
self.font_box.currentFontChanged.connect(self.font_changed) self.font_box.currentFontChanged.connect(self.font_changed)
self.font_size_box.valueChanged.connect(self.font_size_changed) self.font_size_box.valueChanged.connect(self.font_size_changed)
self.highlighter.initializeFormats()
self.highlighter.rehighlight()
def font_changed(self, font): def font_changed(self, font):
fi = QFontInfo(font) fi = QFontInfo(font)
gprefs['gpm_template_editor_font'] = unicode_type(fi.family()) gprefs['gpm_template_editor_font'] = unicode_type(fi.family())
self.textbox.setFont(self.get_current_font()) self.set_editor_font()
self.highlighter.initializeFormats()
self.highlighter.rehighlight()
def font_size_changed(self, toWhat): def font_size_changed(self, toWhat):
gprefs['gpm_template_editor_font_size'] = toWhat gprefs['gpm_template_editor_font_size'] = toWhat
self.textbox.setFont(self.get_current_font()) self.set_editor_font()
self.highlighter.initializeFormats()
self.highlighter.rehighlight()
def break_box_changed(self, new_state): def break_box_changed(self, new_state):
gprefs['template_editor_break_on_print'] = new_state != 0 gprefs['template_editor_break_on_print'] = new_state != 0