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
CONFIG.loadConfig()
CONFIG.initLocalisation(nwApp)
CONFIG.setTextFont(CONFIG.textFont, CONFIG.textSize) # Makes sure these are valid
# Launch main GUI
nwGUI = GuiMain()
+29 -22
View File
@@ -131,8 +131,7 @@ class Config:
self.askBeforeBackup = True # Flag for asking before running automatic backup
# Text Editor Settings
self.textFont = "" # Editor font
self.textSize = 12 # Editor font size
self.textFont = QFont() # Editor font
self.textWidth = 700 # Editor text width
self.textMargin = 40 # Editor/viewer text margin
self.tabWidth = 40 # Editor tabulator width
@@ -383,23 +382,28 @@ class Config:
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 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()
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:
self.textFont = "Arial"
font.setFamily("Arial")
font.setPointSize(12)
elif self.osDarwin and "Helvetica" in fontFam:
self.textFont = "Helvetica"
font.setFamily("Helvetica")
font.setPointSize(12)
else:
self.textFont = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont).family()
else:
self.textFont = family
font = fontDB.systemFont(QFontDatabase.SystemFont.GeneralFont)
self.textFont = font
return
##
@@ -585,19 +589,17 @@ class Config:
sec = "Main"
self.guiTheme = conf.rdStr(sec, "theme", self.guiTheme)
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.hideVScroll = conf.rdBool(sec, "hidevscroll", self.hideVScroll)
self.hideHScroll = conf.rdBool(sec, "hidehscroll", self.hideHScroll)
self.lastNotes = conf.rdStr(sec, "lastnotes", self.lastNotes)
self._lastPath = conf.rdPath(sec, "lastpath", self._lastPath)
# If we have an old config file with the following settings, use those instead
legacyFont = conf.rdStr(sec, "font", "")
legacySize = conf.rdInt(sec, "fontsize", 11)
if legacyFont:
guiFont = QFont()
guiFont.fromString(f"{legacyFont},{legacySize}")
# If we have an old config file, we have an explicit font size,
# in which case we convert the old settings.
if fontSize := conf.rdInt(sec, "fontsize", 0):
guiFont = f"{guiFont},{fontSize}"
self.setGuiFont(guiFont)
@@ -621,8 +623,7 @@ class Config:
# Editor
sec = "Editor"
self.textFont = conf.rdStr(sec, "textfont", self.textFont)
self.textSize = conf.rdInt(sec, "textsize", self.textSize)
textFont = conf.rdStr(sec, "textfont", "")
self.textWidth = conf.rdInt(sec, "width", self.textWidth)
self.textMargin = conf.rdInt(sec, "margin", self.textMargin)
self.tabWidth = conf.rdInt(sec, "tabwidth", self.tabWidth)
@@ -661,6 +662,13 @@ class Config:
self.stopWhenIdle = conf.rdBool(sec, "stopwhenidle", self.stopWhenIdle)
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
sec = "State"
self.showViewerPanel = conf.rdBool(sec, "showviewerpanel", self.showViewerPanel)
@@ -731,8 +739,7 @@ class Config:
}
conf["Editor"] = {
"textfont": str(self.textFont),
"textsize": str(self.textSize),
"textfont": str(self.textFont.toString()),
"width": str(self.textWidth),
"margin": str(self.textMargin),
"tabwidth": str(self.tabWidth),
+10 -21
View File
@@ -27,7 +27,7 @@ from __future__ import annotations
import logging
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 (
QAbstractButton, QApplication, QCompleter, QDialog, QDialogButtonBox,
QFileDialog, QFontDialog, QHBoxLayout, QLineEdit, QPushButton, QVBoxLayout,
@@ -140,6 +140,7 @@ class GuiPreferences(QDialog):
# Temporary Variables
self._guiFont = CONFIG.guiFont
self._textFont = CONFIG.textFont
# Label
self.sidebar.addLabel(self.tr("General"))
@@ -230,26 +231,16 @@ class GuiPreferences(QDialog):
self.textFont = QLineEdit(self)
self.textFont.setReadOnly(True)
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.clicked.connect(self._selectTextFont)
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),
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
self.emphLabels = NSwitch(self)
self.emphLabels.setChecked(CONFIG.emphLabels)
@@ -823,13 +814,11 @@ class GuiPreferences(QDialog):
@pyqtSlot()
def _selectTextFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
current = QFont()
current.setFamily(CONFIG.textFont)
current.setPointSize(CONFIG.textSize)
font, status = QFontDialog.getFont(current, self)
font, status = QFontDialog.getFont(CONFIG.textFont, self)
if status:
self.textFont.setText(font.family())
self.textSize.setValue(font.pointSize())
self.textFont.setText(describeFont(font))
self.textFont.setCursorPosition(0)
self._textFont = font
return
@pyqtSlot()
@@ -907,7 +896,7 @@ class GuiPreferences(QDialog):
CONFIG.emphLabels = emphLabels
CONFIG.showFullPath = self.showFullPath.isChecked()
CONFIG.incNotesWCount = self.incNotesWCount.isChecked()
CONFIG.setTextFont(self.textFont.text(), self.textSize.value())
CONFIG.textFont = self._textFont
# Auto Save
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.
See issues #1862 and #1875.
"""
font = self.font()
font.setFamily(CONFIG.textFont)
font.setPointSize(CONFIG.textSize)
self.setFont(font)
# Reset sub-widget font to GUI font
self.setFont(CONFIG.textFont)
self.docHeader.updateFont()
self.docFooter.updateFont()
self.docSearch.updateFont()
return
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))
if size:
charFormat.setFontPointSize(round(size*CONFIG.textSize))
charFormat.setFontPointSize(round(size*CONFIG.textFont.pointSize()))
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.
See issues #1862 and #1875.
"""
font = self.font()
font.setFamily(CONFIG.textFont)
font.setPointSize(CONFIG.textSize)
self.setFont(font)
# Reset sub-widget font to GUI font
self.setFont(CONFIG.textFont)
self.docHeader.updateFont()
self.docFooter.updateFont()
return
def loadText(self, tHandle: str, updateHistory: bool = True) -> bool: