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()
+29 -22
View File
@@ -131,8 +131,7 @@ 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
@@ -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.
""" """
if isinstance(value, QFont):
self.textFont = value
elif value and isinstance(value, str):
self.textFont = QFont()
self.textFont.fromString(value)
else:
font = QFont()
fontDB = QFontDatabase() fontDB = QFontDatabase()
fontFam = fontDB.families() fontFam = fontDB.families()
self.textSize = pointSize
if family is None or family not in fontFam:
logger.warning("Unknown font '%s'", family)
if self.osWindows and "Arial" in fontFam: if self.osWindows and "Arial" in fontFam:
self.textFont = "Arial" font.setFamily("Arial")
font.setPointSize(12)
elif self.osDarwin and "Helvetica" in fontFam: elif self.osDarwin and "Helvetica" in fontFam:
self.textFont = "Helvetica" font.setFamily("Helvetica")
font.setPointSize(12)
else: else:
self.textFont = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont).family() font = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont)
else: self.textFont = font
self.textFont = family
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: