From 8d38f27eee59f78b703998cb30ecdac35bbc8231 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Tue, 13 Jun 2023 17:31:40 +0200 Subject: [PATCH] Update gui options file format to macth others --- novelwriter/core/options.py | 52 +++++++++++++++------------- novelwriter/gui/outline.py | 22 +++++++----- tests/test_core/test_core_options.py | 4 +-- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/novelwriter/core/options.py b/novelwriter/core/options.py index ad1a0aee..9409f743 100644 --- a/novelwriter/core/options.py +++ b/novelwriter/core/options.py @@ -79,7 +79,7 @@ class OptionState: def __init__(self, project: NWProject): self._project = project - self._theState = {} + self._state = {} return ## @@ -93,24 +93,25 @@ class OptionState: if not isinstance(stateFile, Path): return False - theState = {} + data = {} if stateFile.exists(): logger.debug("Loading GUI options file") try: with open(stateFile, mode="r", encoding="utf-8") as inFile: - theState = json.load(inFile) + data = json.load(inFile) except Exception: logger.error("Failed to load GUI options file") logException() return False # Filter out unused variables - for aGroup in theState: + state = data.get("novelWriter.guiOptions", {}) + for aGroup in state: if aGroup in VALID_MAP: - self._theState[aGroup] = {} - for anOpt in theState[aGroup]: + self._state[aGroup] = {} + for anOpt in state[aGroup]: if anOpt in VALID_MAP[aGroup]: - self._theState[aGroup][anOpt] = theState[aGroup][anOpt] + self._state[aGroup][anOpt] = state[aGroup][anOpt] return True @@ -123,7 +124,8 @@ class OptionState: logger.debug("Saving GUI options file") try: with open(stateFile, mode="w+", encoding="utf-8") as fObj: - fObj.write(jsonEncode(self._theState, nmax=3)) + data = {"novelWriter.guiOptions": self._state} + fObj.write(jsonEncode(data, nmax=4)) except Exception: logger.error("Failed to save GUI options file") logException() @@ -145,13 +147,13 @@ class OptionState: logger.error("Unknown option name '%s'", name) return False - if group not in self._theState: - self._theState[group] = {} + if group not in self._state: + self._state[group] = {} if isinstance(value, Enum): - self._theState[group][name] = value.name + self._state[group][name] = value.name else: - self._theState[group][name] = value + self._state[group][name] = value return True @@ -163,40 +165,40 @@ class OptionState: """Return an arbitrary type value, if it exists. Otherwise, return the default value. """ - if group in self._theState: - return self._theState[group].get(name, default) + if group in self._state: + return self._state[group].get(name, default) return default def getString(self, group: str, name: str, default: str) -> str: """Return the value as a string, if it exists. Otherwise, return the default value. """ - if group in self._theState: - return checkString(self._theState[group].get(name, default), default) + if group in self._state: + return checkString(self._state[group].get(name, default), default) return default def getInt(self, group: str, name: str, default: int) -> int: """Return the value as an int, if it exists. Otherwise, return the default value. """ - if group in self._theState: - return checkInt(self._theState[group].get(name, default), default) + if group in self._state: + return checkInt(self._state[group].get(name, default), default) return default def getFloat(self, group: str, name: str, default: float) -> float: """Return the value as a float, if it exists. Otherwise, return the default value. """ - if group in self._theState: - return checkFloat(self._theState[group].get(name, default), default) + if group in self._state: + return checkFloat(self._state[group].get(name, default), default) return default def getBool(self, group: str, name: str, default: bool) -> bool: """Return the value as a bool, if it exists. Otherwise, return the default value. """ - if group in self._theState: - return checkBool(self._theState[group].get(name, default), default) + if group in self._state: + return checkBool(self._state[group].get(name, default), default) return default def getEnum(self, group: str, name: str, lookup: type, default: Enum) -> Enum: @@ -204,9 +206,9 @@ class OptionState: default value. """ if issubclass(lookup, Enum): - if group in self._theState: - if name in self._theState[group]: - value = self._theState[group][name] + if group in self._state: + if name in self._state[group]: + value = self._state[group][name] if value in lookup.__members__: return lookup[value] return default diff --git a/novelwriter/gui/outline.py b/novelwriter/gui/outline.py index aa0828c1..0e4982fc 100644 --- a/novelwriter/gui/outline.py +++ b/novelwriter/gui/outline.py @@ -47,6 +47,7 @@ from novelwriter.enum import ( ) from novelwriter.common import checkInt from novelwriter.constants import nwHeaders, trConst, nwKeyWords, nwLabels +from novelwriter.error import logException from novelwriter.gui.components import NovelSelector @@ -577,20 +578,23 @@ class GuiOutlineTree(QTreeWidget): and column width. """ # Load whatever we saved last time, regardless of wether it - # contains the correct names or number of columns. The names - # must be valid though. + # contains the correct names or number of columns. colState = self.theProject.options.getValue("GuiOutline", "columnState", {}) tmpOrder = [] tmpHidden = {} tmpWidth = {} - for name, (hidden, width) in colState.items(): - if name not in nwOutline.__members__: - logger.warning("Ignored unknown outline column '%s'", str(name)) - continue - tmpOrder.append(nwOutline[name]) - tmpHidden[nwOutline[name]] = hidden - tmpWidth[nwOutline[name]] = CONFIG.pxInt(width) + try: + for name, (hidden, width) in colState.items(): + if name not in nwOutline.__members__: + logger.warning("Ignored unknown outline column '%s'", str(name)) + continue + tmpOrder.append(nwOutline[name]) + tmpHidden[nwOutline[name]] = hidden + tmpWidth[nwOutline[name]] = CONFIG.pxInt(width) + except Exception: + logger.error("Invalid column state") + logException() # Add columns that was not in the file to the treeOrder array. for hItem in nwOutline: diff --git a/tests/test_core/test_core_options.py b/tests/test_core/test_core_options.py index c241504e..6c4a3472 100644 --- a/tests/test_core/test_core_options.py +++ b/tests/test_core/test_core_options.py @@ -73,7 +73,7 @@ def testCoreOptions_LoadSave(monkeypatch, mockGUI, fncPath): assert theOpts.loadSettings() # Check that unwanted items have been removed - assert theOpts._theState == { + assert theOpts._state == { "GuiProjectSettings": { "winWidth": 570, "winHeight": 375, @@ -88,7 +88,7 @@ def testCoreOptions_LoadSave(monkeypatch, mockGUI, fncPath): # Load again to check we get the values back assert theOpts.loadSettings() - assert theOpts._theState == { + assert theOpts._state == { "GuiProjectSettings": { "winWidth": 570, "winHeight": 375,