Switch Tokenizer to use QFont instead of font family and size

This commit is contained in:
Veronica Berglyd Olsen
2024-05-22 16:57:50 +02:00
parent c63a0c2f58
commit 7e1f1a725c
8 changed files with 81 additions and 73 deletions
+10 -14
View File
@@ -31,7 +31,7 @@ from time import time
from typing import TYPE_CHECKING
from PyQt5.QtCore import Qt, QTimer, QUrl, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QColor, QCursor, QPalette, QResizeEvent
from PyQt5.QtGui import QCloseEvent, QColor, QCursor, QFont, QPalette, QResizeEvent
from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog
from PyQt5.QtWidgets import (
QAbstractItemView, QApplication, QFormLayout, QGridLayout, QHBoxLayout,
@@ -404,10 +404,10 @@ class GuiManuscript(NToolDialog):
"""Update the preview widget and set relevant values."""
self.docPreview.setContent(data)
self.docPreview.setBuildName(build.name)
self.docPreview.setTextFont(
build.getStr("format.textFont"),
build.getInt("format.textSize")
)
textFont = QFont()
textFont.fromString(build.getStr("format.textFont"))
self.docPreview.setTextFont(textFont)
self.docPreview.setJustify(
build.getBool("format.justifyText")
)
@@ -787,7 +787,7 @@ class _PreviewWidget(QTextBrowser):
self._updateDocMargins()
self._updateBuildAge()
self.setTextFont(CONFIG.textFont.family(), CONFIG.textFont.pointSize())
self.setTextFont(CONFIG.textFont)
# Age Timer
self.ageTimer = QTimer(self)
@@ -817,18 +817,14 @@ class _PreviewWidget(QTextBrowser):
self.document().setDefaultTextOption(pOptions)
return
def setTextFont(self, family: str, size: int) -> None:
def setTextFont(self, font: QFont) -> None:
"""Set the text font properties and then reset for sub-widgets.
This needs special attention since there appears to be a bug in
Qt 5.15.3. See issues #1862 and #1875.
"""
if family and size > 4:
font = self.font()
font.setFamily(family)
font.setPointSize(size)
self.setFont(font)
self.buildProgress.setFont(SHARED.theme.guiFont)
self.ageLabel.setFont(SHARED.theme.guiFontSmall)
self.setFont(font)
self.buildProgress.setFont(SHARED.theme.guiFont)
self.ageLabel.setFont(SHARED.theme.guiFontSmall)
return
##
+15 -25
View File
@@ -37,6 +37,7 @@ from PyQt5.QtWidgets import (
)
from novelwriter import CONFIG, SHARED
from novelwriter.common import describeFont
from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels, trConst
from novelwriter.core.buildsettings import BuildSettings, FilterMode
from novelwriter.extensions.configlayout import (
@@ -1052,6 +1053,7 @@ class _FormatTab(NScrollableForm):
self._build = build
self._unitScale = 1.0
self._textFont = QFont(CONFIG.textFont)
iPx = SHARED.theme.baseIconHeight
iSz = SHARED.theme.baseIconSize
@@ -1063,24 +1065,16 @@ class _FormatTab(NScrollableForm):
self.addGroupLabel(self._build.getLabel("format.grpFormat"))
# Font Family
# Text Font
self.textFont = QLineEdit(self)
self.textFont.setReadOnly(True)
self.btnTextFont = NIconToolButton(self, iSz, "more")
self.btnTextFont = NIconToolButton(self, iSz, "font")
self.btnTextFont.clicked.connect(self._selectFont)
self.addRow(
self._build.getLabel("format.textFont"), self.textFont,
button=self.btnTextFont, stretch=(3, 2)
button=self.btnTextFont, stretch=(1, 1)
)
# Font Size
self.textSize = NSpinBox(self)
self.textSize.setMinimum(8)
self.textSize.setMaximum(60)
self.textSize.setSingleStep(1)
self.textSize.setMinimumWidth(spW)
self.addRow(self._build.getLabel("format.textSize"), self.textSize, unit="pt")
# Line Height
self.lineHeight = NDoubleSpinBox(self)
self.lineHeight.setFixedWidth(spW)
@@ -1175,14 +1169,13 @@ class _FormatTab(NScrollableForm):
def loadContent(self) -> None:
"""Populate the widgets."""
textFont = self._build.getStr("format.textFont")
if not textFont:
textFont = str(CONFIG.textFont.family())
self._textFont = QFont()
self._textFont.fromString(self._build.getStr("format.textFont"))
self.textFont.setText(describeFont(self._textFont))
self.textFont.setCursorPosition(0)
self.textFont.setText(textFont)
self.textSize.setValue(self._build.getInt("format.textSize"))
self.lineHeight.setValue(self._build.getFloat("format.lineHeight"))
self.justifyText.setChecked(self._build.getBool("format.justifyText"))
self.stripUnicode.setChecked(self._build.getBool("format.stripUnicode"))
self.replaceTabs.setChecked(self._build.getBool("format.replaceTabs"))
@@ -1219,8 +1212,7 @@ class _FormatTab(NScrollableForm):
def saveContent(self) -> None:
"""Save choices back into build object."""
self._build.setValue("format.textFont", self.textFont.text())
self._build.setValue("format.textSize", self.textSize.value())
self._build.setValue("format.textFont", self._textFont.toString())
self._build.setValue("format.lineHeight", self.lineHeight.value())
self._build.setValue("format.justifyText", self.justifyText.isChecked())
@@ -1249,13 +1241,11 @@ class _FormatTab(NScrollableForm):
@pyqtSlot()
def _selectFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
currFont = QFont()
currFont.setFamily(self.textFont.text())
currFont.setPointSize(self.textSize.value())
newFont, status = QFontDialog.getFont(currFont, self)
font, status = QFontDialog.getFont(self._textFont, self)
if status:
self.textFont.setText(newFont.family())
self.textSize.setValue(newFont.pointSize())
self.textFont.setText(describeFont(font))
self.textFont.setCursorPosition(0)
self._textFont = font
return
@pyqtSlot(int)