Skip to content

Commit

Permalink
Add native font dialog option and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed May 23, 2024
1 parent 1fe7ca3 commit 69b362b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
12 changes: 6 additions & 6 deletions novelwriter/core/toodt.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ def initDocument(self) -> None:
fontWeight = str(intWeight)
fontBold = str(min(intWeight + 300, 900))

self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
self._fontStyle = FONT_STYLE.get(self._textFont.style(), "normal")
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)
self._fontFamily = self._textFont.family()
self._fontSize = self._textFont.pointSize()
self._fontWeight = FONT_WEIGHT_MAP.get(fontWeight, fontWeight)
self._fontStyle = FONT_STYLE.get(self._textFont.style(), "normal")
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)

self._fSizeTitle = f"{round(2.50 * self._fontSize):d}pt"
self._fSizeHead1 = f"{round(2.00 * self._fontSize):d}pt"
Expand Down
13 changes: 11 additions & 2 deletions novelwriter/dialogs/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ def buildForm(self) -> None:
self.tr("Scrolling available with mouse wheel and keys only.")
)

# Native Font Dialog
self.nativeFont = NSwitch(self)
self.nativeFont.setChecked(CONFIG.nativeFont)
self.mainForm.addRow(
self.tr("Use the system's font selection dialog"), self.nativeFont,
self.tr("Turn off to use the Qt font dialog, which may have more options.")
)

# Document Style
# ==============

Expand Down Expand Up @@ -803,7 +811,7 @@ def _gotoSearch(self) -> None:
@pyqtSlot()
def _selectGuiFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
font, status = SHARED.getFont(self._guiFont, CONFIG.nativeFont)
font, status = SHARED.getFont(self._guiFont, self.nativeFont.isChecked())
if status:
self.guiFont.setText(describeFont(font))
self.guiFont.setCursorPosition(0)
Expand All @@ -813,7 +821,7 @@ def _selectGuiFont(self) -> None:
@pyqtSlot()
def _selectTextFont(self) -> None:
"""Open the QFontDialog and set a font for the font style."""
font, status = SHARED.getFont(self._textFont, CONFIG.nativeFont)
font, status = SHARED.getFont(self._textFont, self.nativeFont.isChecked())
if status:
self.textFont.setText(describeFont(font))
self.textFont.setCursorPosition(0)
Expand Down Expand Up @@ -882,6 +890,7 @@ def _saveValues(self) -> None:
CONFIG.guiTheme = guiTheme
CONFIG.hideVScroll = self.hideVScroll.isChecked()
CONFIG.hideHScroll = self.hideHScroll.isChecked()
CONFIG.nativeFont = self.nativeFont.isChecked()
CONFIG.setGuiFont(self._guiFont)

# Document Style
Expand Down
1 change: 1 addition & 0 deletions novelwriter/dialogs/quotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, parent: QWidget, current: str = '"') -> None:

logger.debug("Create: GuiQuoteSelect")
self.setObjectName("GuiQuoteSelect")
self.setWindowTitle(self.tr("Select Quote Style"))

self.outerBox = QVBoxLayout()
self.innerBox = QHBoxLayout()
Expand Down
20 changes: 9 additions & 11 deletions novelwriter/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,11 @@ def runInThreadPool(self, runnable: QRunnable, priority: int = 0) -> None:
QThreadPool.globalInstance().start(runnable, priority=priority)
return

def getProjectPath(self, parent: QWidget, path: str | Path | None = None,
allowZip: bool = False) -> Path | None:
def getProjectPath(
self, parent: QWidget,
path: str | Path | None = None,
allowZip: bool = False
) -> Path | None:
"""Open the file dialog and select a novelWriter project file."""
label = (self.tr("novelWriter Project File or Zip File")
if allowZip else self.tr("novelWriter Project File"))
Expand All @@ -270,15 +273,10 @@ def getProjectPath(self, parent: QWidget, path: str | Path | None = None,

def getFont(self, current: QFont, native: bool) -> tuple[QFont, bool]:
"""Open the font dialog and select a font."""
if native:
font, status = QFontDialog.getFont(current, self.mainGui)
else:
options = QFontDialog.FontDialogOption.DontUseNativeDialog
font, status = QFontDialog.getFont(current, self.mainGui, options=options)
font.setOverline(False)
font.setStrikeOut(False)
font.setUnderline(False)
return font, status
kwargs = {}
if not native:
kwargs["options"] = QFontDialog.FontDialogOption.DontUseNativeDialog
return QFontDialog.getFont(current, self.mainGui, self.tr("Select Font"), **kwargs)

def findTopLevelWidget(self, kind: type[NWWidget]) -> NWWidget | None:
"""Find a top level widget."""
Expand Down
11 changes: 2 additions & 9 deletions tests/test_dialogs/test_dlg_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,12 @@ def testDlgPreferences_Settings(qtbot, monkeypatch, nwGUI, tstPaths):
prefs = GuiPreferences(nwGUI)
prefs.show()

# Mock Font
class MockFont:

def family(self):
return "TestFont"

def pointSize(self):
return 42

# Appearance
prefs.guiLocale.setCurrentIndex(prefs.guiLocale.findData("en_US"))
prefs.guiTheme.setCurrentIndex(prefs.guiTheme.findData("default_dark"))
with monkeypatch.context() as mp:
mp.setattr(QFontDialog, "getFont", lambda *a, **k: (QFont(), True))
prefs.nativeFont.setChecked(True) # Use OS font dialog
prefs.guiFontButton.click()
prefs.hideVScroll.setChecked(True)
prefs.hideHScroll.setChecked(True)
Expand All @@ -188,6 +180,7 @@ def pointSize(self):
prefs.guiSyntax.setCurrentIndex(prefs.guiSyntax.findData("default_dark"))
with monkeypatch.context() as mp:
mp.setattr(QFontDialog, "getFont", lambda *a, **k: (QFont(), True))
prefs.nativeFont.setChecked(False) # Use Qt font dialog
prefs.textFontButton.click()
prefs.emphLabels.setChecked(False)
prefs.showFullPath.setChecked(False)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_tools/test_tools_manussettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QFontDialog

from novelwriter import CONFIG, SHARED
from novelwriter import SHARED
from novelwriter.common import describeFont
from novelwriter.constants import nwHeadFmt
from novelwriter.core.buildsettings import BuildSettings, FilterMode
Expand Down Expand Up @@ -575,7 +575,6 @@ def testBuildSettings_Format(monkeypatch, qtbot, nwGUI):
assert bSettings.toolStack.currentWidget() is fmtTab

# Check initial values
assert fmtTab._textFont == CONFIG.textFont
assert fmtTab.lineHeight.value() == 1.2

assert fmtTab.justifyText.isChecked() is False
Expand Down

0 comments on commit 69b362b

Please sign in to comment.