Improve build settings

This commit is contained in:
Veronica Berglyd Olsen
2024-10-01 14:01:36 +02:00
parent d8bb9950d3
commit 22473823c9
4 changed files with 30 additions and 33 deletions
+9 -15
View File
@@ -40,15 +40,15 @@ from novelwriter.constants import nwFiles, nwHeadFmt
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.enum import nwBuildFmt from novelwriter.enum import nwBuildFmt
from novelwriter.error import logException from novelwriter.error import logException
from novelwriter.types import T_Basic
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# The Settings Template # The Settings Template
# ===================== # =====================
# Each entry contains a tuple on the form: # Each entry contains a tuple on the form: (type, default)
# (type, default, [min value, max value])
SETTINGS_TEMPLATE = { SETTINGS_TEMPLATE: dict[str, tuple[type, T_Basic]] = {
"filter.includeNovel": (bool, True), "filter.includeNovel": (bool, True),
"filter.includeNotes": (bool, False), "filter.includeNotes": (bool, False),
"filter.includeInactive": (bool, False), "filter.includeInactive": (bool, False),
@@ -83,7 +83,7 @@ SETTINGS_TEMPLATE = {
"text.ignoredKeywords": (str, ""), "text.ignoredKeywords": (str, ""),
"text.addNoteHeadings": (bool, True), "text.addNoteHeadings": (bool, True),
"format.textFont": (str, CONFIG.textFont.toString()), "format.textFont": (str, CONFIG.textFont.toString()),
"format.lineHeight": (float, 1.15, 0.75, 3.0), "format.lineHeight": (float, 1.15),
"format.justifyText": (bool, False), "format.justifyText": (bool, False),
"format.stripUnicode": (bool, False), "format.stripUnicode": (bool, False),
"format.replaceTabs": (bool, False), "format.replaceTabs": (bool, False),
@@ -356,18 +356,12 @@ class BuildSettings:
self._changed = True self._changed = True
return return
def setValue(self, key: str, value: str | int | bool | float) -> bool: def setValue(self, key: str, value: T_Basic) -> None:
"""Set a specific value for a build setting.""" """Set a specific value for a build setting."""
if key not in SETTINGS_TEMPLATE: if (d := SETTINGS_TEMPLATE.get(key)) and len(d) == 2 and isinstance(value, d[0]):
return False self._changed = value != self._settings[key]
definition = SETTINGS_TEMPLATE[key] self._settings[key] = value
if not isinstance(value, definition[0]): return
return False
if len(definition) == 4 and isinstance(value, (int, float)):
value = min(max(value, definition[2]), definition[3])
self._changed = value != self._settings[key]
self._settings[key] = value
return True
## ##
# Methods # Methods
+4
View File
@@ -27,6 +27,10 @@ from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QFont, QPainter, QTextCharFormat, QTextCursor, QTextFormat from PyQt5.QtGui import QColor, QFont, QPainter, QTextCharFormat, QTextCursor, QTextFormat
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QSizePolicy, QStyle from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QSizePolicy, QStyle
# Type Aliases
T_Basic = str | int | float | bool
# Qt Alignment Flags # Qt Alignment Flags
QtAlignAbsolute = Qt.AlignmentFlag.AlignAbsolute QtAlignAbsolute = Qt.AlignmentFlag.AlignAbsolute
+16 -17
View File
@@ -29,7 +29,7 @@ from pathlib import Path
import pytest import pytest
from novelwriter import CONFIG from novelwriter import CONFIG
from novelwriter.constants import nwFiles from novelwriter.constants import nwFiles, nwHeadFmt
from novelwriter.core.buildsettings import BuildCollection, BuildSettings, FilterMode from novelwriter.core.buildsettings import BuildCollection, BuildSettings, FilterMode
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.enum import nwBuildFmt, nwItemClass from novelwriter.enum import nwBuildFmt, nwItemClass
@@ -153,43 +153,42 @@ def testCoreBuildSettings_BuildValues():
floatSetting = "format.lineHeight" floatSetting = "format.lineHeight"
# Invalid setting # Invalid setting
assert build.setValue("foo", "bar") is False build.setValue("foo", "bar")
assert build.getStr("foo") == "None"
# Value must be correct type # Value must be correct type
assert build.setValue(strSetting, 15) is False build.setValue(strSetting, 15)
assert build.setValue(intSetting, 15.0) is False assert build.getStr(strSetting) == nwHeadFmt.TITLE
assert build.setValue(boolSetting, "string") is False build.setValue(intSetting, 15.0)
assert build.setValue(floatSetting, 15) is False assert build.getInt(intSetting) == 0
build.setValue(floatSetting, 15)
# Check min/max range assert build.getFloat(floatSetting) == 1.15
assert build.setValue(floatSetting, 200.0) is True build.setValue(boolSetting, "string")
assert build.getFloat(floatSetting) == 3.0 assert build.getBool(boolSetting) is True
assert build.setValue(floatSetting, 0.0) is True
assert build.getFloat(floatSetting) == 0.75
# Check string values # Check string values
assert build.setValue(strSetting, "foobar") is True build.setValue(strSetting, "foobar")
assert build.getStr(strSetting) == "foobar" assert build.getStr(strSetting) == "foobar"
assert build.getInt(strSetting) == 0 assert build.getInt(strSetting) == 0
assert build.getBool(strSetting) is True assert build.getBool(strSetting) is True
assert build.getFloat(strSetting) == 0.0 assert build.getFloat(strSetting) == 0.0
# Check int values # Check int values
assert build.setValue(intSetting, 42) is True build.setValue(intSetting, 42)
assert build.getStr(intSetting) == "42" assert build.getStr(intSetting) == "42"
assert build.getInt(intSetting) == 42 assert build.getInt(intSetting) == 42
assert build.getBool(intSetting) is True assert build.getBool(intSetting) is True
assert build.getFloat(intSetting) == 42.0 assert build.getFloat(intSetting) == 42.0
# Check bool values # Check bool values
assert build.setValue(boolSetting, True) is True build.setValue(boolSetting, True)
assert build.getStr(boolSetting) == "True" assert build.getStr(boolSetting) == "True"
assert build.getInt(boolSetting) == 1 assert build.getInt(boolSetting) == 1
assert build.getBool(boolSetting) is True assert build.getBool(boolSetting) is True
assert build.getFloat(boolSetting) == 1.0 assert build.getFloat(boolSetting) == 1.0
# Check float values # Check float values
assert build.setValue(floatSetting, 2.5) is True build.setValue(floatSetting, 2.5)
assert build.getStr(floatSetting) == "2.5" assert build.getStr(floatSetting) == "2.5"
assert build.getInt(floatSetting) == 2 assert build.getInt(floatSetting) == 2
assert build.getBool(floatSetting) is True assert build.getBool(floatSetting) is True
@@ -334,7 +333,7 @@ def testCoreBuildSettings_Filters(mockGUI, fncPath: Path, mockRnd):
hCharDoc: (True, FilterMode.INCLUDED), hCharDoc: (True, FilterMode.INCLUDED),
} }
# Set everything back to filered # Set everything back to filtered
build.setValue("filter.includeNotes", False) build.setValue("filter.includeNotes", False)
build.setAllowRoot(C.hPlotRoot, False) build.setAllowRoot(C.hPlotRoot, False)
build.setAllowRoot(C.hCharRoot, True) build.setAllowRoot(C.hCharRoot, True)
+1 -1
View File
@@ -311,7 +311,7 @@ def testToolBuildSettings_Headings(qtbot, nwGUI):
"""Test the Headings Tab of the GuiBuildSettings dialog.""" """Test the Headings Tab of the GuiBuildSettings dialog."""
build = BuildSettings() build = BuildSettings()
ttTitle = f"Title: {nwHeadFmt.TITLE}" ttTitle = f"Part: {nwHeadFmt.TITLE}"
chTitle = f"Chapter: {nwHeadFmt.TITLE}" chTitle = f"Chapter: {nwHeadFmt.TITLE}"
unTitle = f"Interlude: {nwHeadFmt.TITLE}" unTitle = f"Interlude: {nwHeadFmt.TITLE}"
scTitle = f"Scene: {nwHeadFmt.TITLE}" scTitle = f"Scene: {nwHeadFmt.TITLE}"