Save all font info for text font

This commit is contained in:
Veronica Berglyd Olsen
2024-05-20 16:32:10 +02:00
parent c0673b8128
commit 774437f7bb
6 changed files with 75 additions and 92 deletions
-1
View File
@@ -235,7 +235,6 @@ def main(sysArgs: list | None = None) -> GuiMain | None:
# Run Config steps that require the QApplication # Run Config steps that require the QApplication
CONFIG.loadConfig() CONFIG.loadConfig()
CONFIG.initLocalisation(nwApp) CONFIG.initLocalisation(nwApp)
CONFIG.setTextFont(CONFIG.textFont, CONFIG.textSize) # Makes sure these are valid
# Launch main GUI # Launch main GUI
nwGUI = GuiMain() nwGUI = GuiMain()
+62 -55
View File
@@ -131,43 +131,42 @@ class Config:
self.askBeforeBackup = True # Flag for asking before running automatic backup self.askBeforeBackup = True # Flag for asking before running automatic backup
# Text Editor Settings # Text Editor Settings
self.textFont = "" # Editor font self.textFont = QFont() # Editor font
self.textSize = 12 # Editor font size self.textWidth = 700 # Editor text width
self.textWidth = 700 # Editor text width self.textMargin = 40 # Editor/viewer text margin
self.textMargin = 40 # Editor/viewer text margin self.tabWidth = 40 # Editor tabulator width
self.tabWidth = 40 # Editor tabulator width
self.focusWidth = 800 # Focus Mode text width self.focusWidth = 800 # Focus Mode text width
self.hideFocusFooter = False # Hide document footer in Focus Mode self.hideFocusFooter = False # Hide document footer in Focus Mode
self.showFullPath = True # Show full document path in editor header self.showFullPath = True # Show full document path in editor header
self.autoSelect = True # Auto-select word when applying format with no selection self.autoSelect = True # Auto-select word when applying format with no selection
self.doJustify = False # Justify text self.doJustify = False # Justify text
self.showTabsNSpaces = False # Show tabs and spaces in editor self.showTabsNSpaces = False # Show tabs and spaces in editor
self.showLineEndings = False # Show line endings in editor self.showLineEndings = False # Show line endings in editor
self.showMultiSpaces = True # Highlight multiple spaces in the text self.showMultiSpaces = True # Highlight multiple spaces in the text
self.doReplace = True # Enable auto-replace as you type self.doReplace = True # Enable auto-replace as you type
self.doReplaceSQuote = True # Smart single quotes self.doReplaceSQuote = True # Smart single quotes
self.doReplaceDQuote = True # Smart double quotes self.doReplaceDQuote = True # Smart double quotes
self.doReplaceDash = True # Replace multiple hyphens with dashes self.doReplaceDash = True # Replace multiple hyphens with dashes
self.doReplaceDots = True # Replace three dots with ellipsis self.doReplaceDots = True # Replace three dots with ellipsis
self.autoScroll = False # Typewriter-like scrolling self.autoScroll = False # Typewriter-like scrolling
self.autoScrollPos = 30 # Start point for typewriter-like scrolling self.autoScrollPos = 30 # Start point for typewriter-like scrolling
self.scrollPastEnd = True # Scroll past end of document, and centre cursor self.scrollPastEnd = True # Scroll past end of document, and centre cursor
self.dialogStyle = 2 # Quote type to use for dialogue self.dialogStyle = 2 # Quote type to use for dialogue
self.allowOpenDial = True # Allow open-ended dialogue quotes self.allowOpenDial = True # Allow open-ended dialogue quotes
self.narratorBreak = "" # Symbol to use for narrator break self.narratorBreak = "" # Symbol to use for narrator break
self.dialogLine = "" # Symbol to use for dialogue line self.dialogLine = "" # Symbol to use for dialogue line
self.altDialogOpen = "" # Alternative dialog symbol, open self.altDialogOpen = "" # Alternative dialog symbol, open
self.altDialogClose = "" # Alternative dialog symbol, close self.altDialogClose = "" # Alternative dialog symbol, close
self.highlightEmph = True # Add colour to text emphasis self.highlightEmph = True # Add colour to text emphasis
self.stopWhenIdle = True # Stop the status bar clock when the user is idle self.stopWhenIdle = True # Stop the status bar clock when the user is idle
self.userIdleTime = 300 # Time of inactivity to consider user idle self.userIdleTime = 300 # Time of inactivity to consider user idle
self.incNotesWCount = True # The status bar word count includes notes self.incNotesWCount = True # The status bar word count includes notes
# User-Selected Symbol Settings # User-Selected Symbol Settings
self.fmtApostrophe = nwUnicode.U_RSQUO self.fmtApostrophe = nwUnicode.U_RSQUO
@@ -383,23 +382,28 @@ class Config:
return return
def setTextFont(self, family: str | None, pointSize: int = 12) -> None: def setTextFont(self, value: QFont | str | None) -> None:
"""Set the text font if it exists. If it doesn't, or is None, """Set the text font if it exists. If it doesn't, or is None,
set to default font. set to default font.
""" """
fontDB = QFontDatabase() if isinstance(value, QFont):
fontFam = fontDB.families() self.textFont = value
self.textSize = pointSize elif value and isinstance(value, str):
if family is None or family not in fontFam: self.textFont = QFont()
logger.warning("Unknown font '%s'", family) self.textFont.fromString(value)
if self.osWindows and "Arial" in fontFam:
self.textFont = "Arial"
elif self.osDarwin and "Helvetica" in fontFam:
self.textFont = "Helvetica"
else:
self.textFont = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont).family()
else: else:
self.textFont = family font = QFont()
fontDB = QFontDatabase()
fontFam = fontDB.families()
if self.osWindows and "Arial" in fontFam:
font.setFamily("Arial")
font.setPointSize(12)
elif self.osDarwin and "Helvetica" in fontFam:
font.setFamily("Helvetica")
font.setPointSize(12)
else:
font = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont)
self.textFont = font
return return
## ##
@@ -585,19 +589,17 @@ class Config:
sec = "Main" sec = "Main"
self.guiTheme = conf.rdStr(sec, "theme", self.guiTheme) self.guiTheme = conf.rdStr(sec, "theme", self.guiTheme)
self.guiSyntax = conf.rdStr(sec, "syntax", self.guiSyntax) self.guiSyntax = conf.rdStr(sec, "syntax", self.guiSyntax)
guiFont = conf.rdStr(sec, "guifont", "") guiFont = conf.rdStr(sec, "font", "")
self.guiLocale = conf.rdStr(sec, "localisation", self.guiLocale) self.guiLocale = conf.rdStr(sec, "localisation", self.guiLocale)
self.hideVScroll = conf.rdBool(sec, "hidevscroll", self.hideVScroll) self.hideVScroll = conf.rdBool(sec, "hidevscroll", self.hideVScroll)
self.hideHScroll = conf.rdBool(sec, "hidehscroll", self.hideHScroll) self.hideHScroll = conf.rdBool(sec, "hidehscroll", self.hideHScroll)
self.lastNotes = conf.rdStr(sec, "lastnotes", self.lastNotes) self.lastNotes = conf.rdStr(sec, "lastnotes", self.lastNotes)
self._lastPath = conf.rdPath(sec, "lastpath", self._lastPath) self._lastPath = conf.rdPath(sec, "lastpath", self._lastPath)
# If we have an old config file with the following settings, use those instead # If we have an old config file, we have an explicit font size,
legacyFont = conf.rdStr(sec, "font", "") # in which case we convert the old settings.
legacySize = conf.rdInt(sec, "fontsize", 11) if fontSize := conf.rdInt(sec, "fontsize", 0):
if legacyFont: guiFont = f"{guiFont},{fontSize}"
guiFont = QFont()
guiFont.fromString(f"{legacyFont},{legacySize}")
self.setGuiFont(guiFont) self.setGuiFont(guiFont)
@@ -621,8 +623,7 @@ class Config:
# Editor # Editor
sec = "Editor" sec = "Editor"
self.textFont = conf.rdStr(sec, "textfont", self.textFont) textFont = conf.rdStr(sec, "textfont", "")
self.textSize = conf.rdInt(sec, "textsize", self.textSize)
self.textWidth = conf.rdInt(sec, "width", self.textWidth) self.textWidth = conf.rdInt(sec, "width", self.textWidth)
self.textMargin = conf.rdInt(sec, "margin", self.textMargin) self.textMargin = conf.rdInt(sec, "margin", self.textMargin)
self.tabWidth = conf.rdInt(sec, "tabwidth", self.tabWidth) self.tabWidth = conf.rdInt(sec, "tabwidth", self.tabWidth)
@@ -661,6 +662,13 @@ class Config:
self.stopWhenIdle = conf.rdBool(sec, "stopwhenidle", self.stopWhenIdle) self.stopWhenIdle = conf.rdBool(sec, "stopwhenidle", self.stopWhenIdle)
self.userIdleTime = conf.rdInt(sec, "useridletime", self.userIdleTime) self.userIdleTime = conf.rdInt(sec, "useridletime", self.userIdleTime)
# If we have an old config file, we have an explicit font size,
# in which case we convert the old settings.
if textSize := conf.rdInt(sec, "textsize", 0):
textFont = f"{textFont},{textSize}"
self.setTextFont(textFont)
# State # State
sec = "State" sec = "State"
self.showViewerPanel = conf.rdBool(sec, "showviewerpanel", self.showViewerPanel) self.showViewerPanel = conf.rdBool(sec, "showviewerpanel", self.showViewerPanel)
@@ -731,8 +739,7 @@ class Config:
} }
conf["Editor"] = { conf["Editor"] = {
"textfont": str(self.textFont), "textfont": str(self.textFont.toString()),
"textsize": str(self.textSize),
"width": str(self.textWidth), "width": str(self.textWidth),
"margin": str(self.textMargin), "margin": str(self.textMargin),
"tabwidth": str(self.tabWidth), "tabwidth": str(self.tabWidth),
+10 -21
View File
@@ -27,7 +27,7 @@ from __future__ import annotations
import logging import logging
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QFont, QKeyEvent, QKeySequence from PyQt5.QtGui import QCloseEvent, QKeyEvent, QKeySequence
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QAbstractButton, QApplication, QCompleter, QDialog, QDialogButtonBox, QAbstractButton, QApplication, QCompleter, QDialog, QDialogButtonBox,
QFileDialog, QFontDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout, QFileDialog, QFontDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout,
@@ -140,6 +140,7 @@ class GuiPreferences(QDialog):
# Temporary Variables # Temporary Variables
self._guiFont = CONFIG.guiFont self._guiFont = CONFIG.guiFont
self._textFont = CONFIG.textFont
# Label # Label
self.sidebar.addLabel(self.tr("General")) self.sidebar.addLabel(self.tr("General"))
@@ -230,26 +231,16 @@ class GuiPreferences(QDialog):
self.textFont = QLineEdit(self) self.textFont = QLineEdit(self)
self.textFont.setReadOnly(True) self.textFont.setReadOnly(True)
self.textFont.setMinimumWidth(fontWidth) self.textFont.setMinimumWidth(fontWidth)
self.textFont.setText(CONFIG.textFont) self.textFont.setText(describeFont(CONFIG.textFont))
self.textFont.setCursorPosition(0)
self.textFontButton = NIconToolButton(self, iSz, "more") self.textFontButton = NIconToolButton(self, iSz, "more")
self.textFontButton.clicked.connect(self._selectTextFont) self.textFontButton.clicked.connect(self._selectTextFont)
self.mainForm.addRow( self.mainForm.addRow(
self.tr("Document font family"), self.textFont, self.tr("Document font"), self.textFont,
self.tr("Applies to both document editor and viewer."), stretch=(3, 2), self.tr("Applies to both document editor and viewer."), stretch=(3, 2),
button=self.textFontButton button=self.textFontButton
) )
# Document Font Size
self.textSize = NSpinBox(self)
self.textSize.setMinimum(8)
self.textSize.setMaximum(60)
self.textSize.setSingleStep(1)
self.textSize.setValue(CONFIG.textSize)
self.mainForm.addRow(
self.tr("Document font size"), self.textSize,
self.tr("Applies to both document editor and viewer."), unit=self.tr("pt")
)
# Emphasise Labels # Emphasise Labels
self.emphLabels = NSwitch(self) self.emphLabels = NSwitch(self)
self.emphLabels.setChecked(CONFIG.emphLabels) self.emphLabels.setChecked(CONFIG.emphLabels)
@@ -823,13 +814,11 @@ class GuiPreferences(QDialog):
@pyqtSlot() @pyqtSlot()
def _selectTextFont(self) -> None: def _selectTextFont(self) -> None:
"""Open the QFontDialog and set a font for the font style.""" """Open the QFontDialog and set a font for the font style."""
current = QFont() font, status = QFontDialog.getFont(CONFIG.textFont, self)
current.setFamily(CONFIG.textFont)
current.setPointSize(CONFIG.textSize)
font, status = QFontDialog.getFont(current, self)
if status: if status:
self.textFont.setText(font.family()) self.textFont.setText(describeFont(font))
self.textSize.setValue(font.pointSize()) self.textFont.setCursorPosition(0)
self._textFont = font
return return
@pyqtSlot() @pyqtSlot()
@@ -907,7 +896,7 @@ class GuiPreferences(QDialog):
CONFIG.emphLabels = emphLabels CONFIG.emphLabels = emphLabels
CONFIG.showFullPath = self.showFullPath.isChecked() CONFIG.showFullPath = self.showFullPath.isChecked()
CONFIG.incNotesWCount = self.incNotesWCount.isChecked() CONFIG.incNotesWCount = self.incNotesWCount.isChecked()
CONFIG.setTextFont(self.textFont.text(), self.textSize.value()) CONFIG.textFont = self._textFont
# Auto Save # Auto Save
CONFIG.autoSaveDoc = self.autoSaveDoc.value() CONFIG.autoSaveDoc = self.autoSaveDoc.value()
+1 -7
View File
@@ -377,16 +377,10 @@ class GuiDocEditor(QPlainTextEdit):
special attention since there appears to be a bug in Qt 5.15.3. special attention since there appears to be a bug in Qt 5.15.3.
See issues #1862 and #1875. See issues #1862 and #1875.
""" """
font = self.font() self.setFont(CONFIG.textFont)
font.setFamily(CONFIG.textFont)
font.setPointSize(CONFIG.textSize)
self.setFont(font)
# Reset sub-widget font to GUI font
self.docHeader.updateFont() self.docHeader.updateFont()
self.docFooter.updateFont() self.docFooter.updateFont()
self.docSearch.updateFont() self.docSearch.updateFont()
return return
def loadText(self, tHandle: str, tLine: int | None = None) -> bool: def loadText(self, tHandle: str, tLine: int | None = None) -> bool:
+1 -1
View File
@@ -474,7 +474,7 @@ class GuiDocHighlighter(QSyntaxHighlighter):
charFormat.setBackground(QBrush(color, Qt.BrushStyle.SolidPattern)) charFormat.setBackground(QBrush(color, Qt.BrushStyle.SolidPattern))
if size: if size:
charFormat.setFontPointSize(round(size*CONFIG.textSize)) charFormat.setFontPointSize(round(size*CONFIG.textFont.pointSize()))
self._hStyles[name] = charFormat self._hStyles[name] = charFormat
+1 -7
View File
@@ -186,15 +186,9 @@ class GuiDocViewer(QTextBrowser):
special attention since there appears to be a bug in Qt 5.15.3. special attention since there appears to be a bug in Qt 5.15.3.
See issues #1862 and #1875. See issues #1862 and #1875.
""" """
font = self.font() self.setFont(CONFIG.textFont)
font.setFamily(CONFIG.textFont)
font.setPointSize(CONFIG.textSize)
self.setFont(font)
# Reset sub-widget font to GUI font
self.docHeader.updateFont() self.docHeader.updateFont()
self.docFooter.updateFont() self.docFooter.updateFont()
return return
def loadText(self, tHandle: str, updateHistory: bool = True) -> bool: def loadText(self, tHandle: str, updateHistory: bool = True) -> bool: