Update gui options file format to macth others
This commit is contained in:
+27
-25
@@ -79,7 +79,7 @@ class OptionState:
|
|||||||
|
|
||||||
def __init__(self, project: NWProject):
|
def __init__(self, project: NWProject):
|
||||||
self._project = project
|
self._project = project
|
||||||
self._theState = {}
|
self._state = {}
|
||||||
return
|
return
|
||||||
|
|
||||||
##
|
##
|
||||||
@@ -93,24 +93,25 @@ class OptionState:
|
|||||||
if not isinstance(stateFile, Path):
|
if not isinstance(stateFile, Path):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
theState = {}
|
data = {}
|
||||||
if stateFile.exists():
|
if stateFile.exists():
|
||||||
logger.debug("Loading GUI options file")
|
logger.debug("Loading GUI options file")
|
||||||
try:
|
try:
|
||||||
with open(stateFile, mode="r", encoding="utf-8") as inFile:
|
with open(stateFile, mode="r", encoding="utf-8") as inFile:
|
||||||
theState = json.load(inFile)
|
data = json.load(inFile)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.error("Failed to load GUI options file")
|
logger.error("Failed to load GUI options file")
|
||||||
logException()
|
logException()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Filter out unused variables
|
# Filter out unused variables
|
||||||
for aGroup in theState:
|
state = data.get("novelWriter.guiOptions", {})
|
||||||
|
for aGroup in state:
|
||||||
if aGroup in VALID_MAP:
|
if aGroup in VALID_MAP:
|
||||||
self._theState[aGroup] = {}
|
self._state[aGroup] = {}
|
||||||
for anOpt in theState[aGroup]:
|
for anOpt in state[aGroup]:
|
||||||
if anOpt in VALID_MAP[aGroup]:
|
if anOpt in VALID_MAP[aGroup]:
|
||||||
self._theState[aGroup][anOpt] = theState[aGroup][anOpt]
|
self._state[aGroup][anOpt] = state[aGroup][anOpt]
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -123,7 +124,8 @@ class OptionState:
|
|||||||
logger.debug("Saving GUI options file")
|
logger.debug("Saving GUI options file")
|
||||||
try:
|
try:
|
||||||
with open(stateFile, mode="w+", encoding="utf-8") as fObj:
|
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:
|
except Exception:
|
||||||
logger.error("Failed to save GUI options file")
|
logger.error("Failed to save GUI options file")
|
||||||
logException()
|
logException()
|
||||||
@@ -145,13 +147,13 @@ class OptionState:
|
|||||||
logger.error("Unknown option name '%s'", name)
|
logger.error("Unknown option name '%s'", name)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if group not in self._theState:
|
if group not in self._state:
|
||||||
self._theState[group] = {}
|
self._state[group] = {}
|
||||||
|
|
||||||
if isinstance(value, Enum):
|
if isinstance(value, Enum):
|
||||||
self._theState[group][name] = value.name
|
self._state[group][name] = value.name
|
||||||
else:
|
else:
|
||||||
self._theState[group][name] = value
|
self._state[group][name] = value
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -163,40 +165,40 @@ class OptionState:
|
|||||||
"""Return an arbitrary type value, if it exists. Otherwise,
|
"""Return an arbitrary type value, if it exists. Otherwise,
|
||||||
return the default value.
|
return the default value.
|
||||||
"""
|
"""
|
||||||
if group in self._theState:
|
if group in self._state:
|
||||||
return self._theState[group].get(name, default)
|
return self._state[group].get(name, default)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getString(self, group: str, name: str, default: str) -> str:
|
def getString(self, group: str, name: str, default: str) -> str:
|
||||||
"""Return the value as a string, if it exists. Otherwise, return
|
"""Return the value as a string, if it exists. Otherwise, return
|
||||||
the default value.
|
the default value.
|
||||||
"""
|
"""
|
||||||
if group in self._theState:
|
if group in self._state:
|
||||||
return checkString(self._theState[group].get(name, default), default)
|
return checkString(self._state[group].get(name, default), default)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getInt(self, group: str, name: str, default: int) -> int:
|
def getInt(self, group: str, name: str, default: int) -> int:
|
||||||
"""Return the value as an int, if it exists. Otherwise, return
|
"""Return the value as an int, if it exists. Otherwise, return
|
||||||
the default value.
|
the default value.
|
||||||
"""
|
"""
|
||||||
if group in self._theState:
|
if group in self._state:
|
||||||
return checkInt(self._theState[group].get(name, default), default)
|
return checkInt(self._state[group].get(name, default), default)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getFloat(self, group: str, name: str, default: float) -> float:
|
def getFloat(self, group: str, name: str, default: float) -> float:
|
||||||
"""Return the value as a float, if it exists. Otherwise, return
|
"""Return the value as a float, if it exists. Otherwise, return
|
||||||
the default value.
|
the default value.
|
||||||
"""
|
"""
|
||||||
if group in self._theState:
|
if group in self._state:
|
||||||
return checkFloat(self._theState[group].get(name, default), default)
|
return checkFloat(self._state[group].get(name, default), default)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getBool(self, group: str, name: str, default: bool) -> bool:
|
def getBool(self, group: str, name: str, default: bool) -> bool:
|
||||||
"""Return the value as a bool, if it exists. Otherwise, return
|
"""Return the value as a bool, if it exists. Otherwise, return
|
||||||
the default value.
|
the default value.
|
||||||
"""
|
"""
|
||||||
if group in self._theState:
|
if group in self._state:
|
||||||
return checkBool(self._theState[group].get(name, default), default)
|
return checkBool(self._state[group].get(name, default), default)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getEnum(self, group: str, name: str, lookup: type, default: Enum) -> Enum:
|
def getEnum(self, group: str, name: str, lookup: type, default: Enum) -> Enum:
|
||||||
@@ -204,9 +206,9 @@ class OptionState:
|
|||||||
default value.
|
default value.
|
||||||
"""
|
"""
|
||||||
if issubclass(lookup, Enum):
|
if issubclass(lookup, Enum):
|
||||||
if group in self._theState:
|
if group in self._state:
|
||||||
if name in self._theState[group]:
|
if name in self._state[group]:
|
||||||
value = self._theState[group][name]
|
value = self._state[group][name]
|
||||||
if value in lookup.__members__:
|
if value in lookup.__members__:
|
||||||
return lookup[value]
|
return lookup[value]
|
||||||
return default
|
return default
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ from novelwriter.enum import (
|
|||||||
)
|
)
|
||||||
from novelwriter.common import checkInt
|
from novelwriter.common import checkInt
|
||||||
from novelwriter.constants import nwHeaders, trConst, nwKeyWords, nwLabels
|
from novelwriter.constants import nwHeaders, trConst, nwKeyWords, nwLabels
|
||||||
|
from novelwriter.error import logException
|
||||||
from novelwriter.gui.components import NovelSelector
|
from novelwriter.gui.components import NovelSelector
|
||||||
|
|
||||||
|
|
||||||
@@ -577,20 +578,23 @@ class GuiOutlineTree(QTreeWidget):
|
|||||||
and column width.
|
and column width.
|
||||||
"""
|
"""
|
||||||
# Load whatever we saved last time, regardless of wether it
|
# Load whatever we saved last time, regardless of wether it
|
||||||
# contains the correct names or number of columns. The names
|
# contains the correct names or number of columns.
|
||||||
# must be valid though.
|
|
||||||
colState = self.theProject.options.getValue("GuiOutline", "columnState", {})
|
colState = self.theProject.options.getValue("GuiOutline", "columnState", {})
|
||||||
|
|
||||||
tmpOrder = []
|
tmpOrder = []
|
||||||
tmpHidden = {}
|
tmpHidden = {}
|
||||||
tmpWidth = {}
|
tmpWidth = {}
|
||||||
for name, (hidden, width) in colState.items():
|
try:
|
||||||
if name not in nwOutline.__members__:
|
for name, (hidden, width) in colState.items():
|
||||||
logger.warning("Ignored unknown outline column '%s'", str(name))
|
if name not in nwOutline.__members__:
|
||||||
continue
|
logger.warning("Ignored unknown outline column '%s'", str(name))
|
||||||
tmpOrder.append(nwOutline[name])
|
continue
|
||||||
tmpHidden[nwOutline[name]] = hidden
|
tmpOrder.append(nwOutline[name])
|
||||||
tmpWidth[nwOutline[name]] = CONFIG.pxInt(width)
|
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.
|
# Add columns that was not in the file to the treeOrder array.
|
||||||
for hItem in nwOutline:
|
for hItem in nwOutline:
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ def testCoreOptions_LoadSave(monkeypatch, mockGUI, fncPath):
|
|||||||
assert theOpts.loadSettings()
|
assert theOpts.loadSettings()
|
||||||
|
|
||||||
# Check that unwanted items have been removed
|
# Check that unwanted items have been removed
|
||||||
assert theOpts._theState == {
|
assert theOpts._state == {
|
||||||
"GuiProjectSettings": {
|
"GuiProjectSettings": {
|
||||||
"winWidth": 570,
|
"winWidth": 570,
|
||||||
"winHeight": 375,
|
"winHeight": 375,
|
||||||
@@ -88,7 +88,7 @@ def testCoreOptions_LoadSave(monkeypatch, mockGUI, fncPath):
|
|||||||
|
|
||||||
# Load again to check we get the values back
|
# Load again to check we get the values back
|
||||||
assert theOpts.loadSettings()
|
assert theOpts.loadSettings()
|
||||||
assert theOpts._theState == {
|
assert theOpts._state == {
|
||||||
"GuiProjectSettings": {
|
"GuiProjectSettings": {
|
||||||
"winWidth": 570,
|
"winWidth": 570,
|
||||||
"winHeight": 375,
|
"winHeight": 375,
|
||||||
|
|||||||
Reference in New Issue
Block a user