Simplify how outline column headers are saved

This commit is contained in:
Veronica Berglyd Olsen
2023-06-10 00:45:25 +02:00
parent 00350140d3
commit 6f02f3f74d
2 changed files with 31 additions and 53 deletions
+4 -4
View File
@@ -32,7 +32,7 @@ from typing import TYPE_CHECKING, Any
from pathlib import Path from pathlib import Path
from novelwriter.error import logException from novelwriter.error import logException
from novelwriter.common import checkBool, checkFloat, checkInt, checkString from novelwriter.common import checkBool, checkFloat, checkInt, checkString, jsonEncode
from novelwriter.constants import nwFiles from novelwriter.constants import nwFiles
if TYPE_CHECKING: # pragma: no cover if TYPE_CHECKING: # pragma: no cover
@@ -47,7 +47,7 @@ VALID_MAP = {
"hideZeros", "hideNegative", "groupByDay", "showIdleTime", "histMax", "hideZeros", "hideNegative", "groupByDay", "showIdleTime", "histMax",
}, },
"GuiDocSplit": {"spLevel", "intoFolder", "docHierarchy"}, "GuiDocSplit": {"spLevel", "intoFolder", "docHierarchy"},
"GuiOutline": {"headerOrder", "columnWidth", "columnHidden"}, "GuiOutline": {"columnState"},
"GuiProjectSettings": { "GuiProjectSettings": {
"winWidth", "winHeight", "replaceColW", "statusColW", "importColW", "winWidth", "winHeight", "replaceColW", "statusColW", "importColW",
}, },
@@ -122,8 +122,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 outFile: with open(stateFile, mode="w+", encoding="utf-8") as fObj:
json.dump(self._theState, outFile, indent=2) fObj.write(jsonEncode(self._theState, nmax=3))
except Exception: except Exception:
logger.error("Failed to save GUI options file") logger.error("Failed to save GUI options file")
logException() logException()
+27 -49
View File
@@ -576,47 +576,36 @@ class GuiOutlineTree(QTreeWidget):
"""Load the state of the main tree header, that is, column order """Load the state of the main tree header, that is, column order
and column width. and column width.
""" """
pOptions = self.theProject.options
# 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. The names
# must be valid though. # must be valid though.
tempOrder = pOptions.getValue("GuiOutline", "headerOrder", []) colState = self.theProject.options.getValue("GuiOutline", "columnState", {})
treeOrder = []
for hName in tempOrder: tmpOrder = []
try: tmpHidden = {}
treeOrder.append(nwOutline[hName]) tmpWidth = {}
except Exception: for name, (hidden, width) in colState.items():
logger.warning("Ignored unknown outline column '%s'", str(hName)) 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)
# 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:
if hItem not in treeOrder: if hItem not in tmpOrder:
treeOrder.append(hItem) tmpOrder.append(hItem)
# Check that we now have a complete list, and only if so, save # Check that we now have a complete list, and only if so, save
# the order loaded from file. Otherwise, we keep the default. # the order loaded from file. Otherwise, we keep the default.
if len(treeOrder) == self._treeNCols: if len(tmpOrder) == self._treeNCols:
self._treeOrder = treeOrder self._treeOrder = tmpOrder
self._colHidden.update(tmpHidden)
self._colWidth.update(tmpWidth)
else: else:
logger.error("Failed to extract outline column order from previous session") logger.error("Failed to extract outline column order from previous session")
logger.error("Column count doesn't match %d != %d", len(treeOrder), self._treeNCols) logger.error("Column count doesn't match %d != %d", len(tmpOrder), self._treeNCols)
# We load whatever column widths and hidden states we find in
# the file, and leave the rest in their default state.
tmpWidth = pOptions.getValue("GuiOutline", "columnWidth", {})
for hName in tmpWidth:
try:
self._colWidth[nwOutline[hName]] = CONFIG.pxInt(tmpWidth[hName])
except Exception:
logger.warning("Ignored unknown outline column '%s'", str(hName))
tmpHidden = pOptions.getValue("GuiOutline", "columnHidden", {})
for hName in tmpHidden:
try:
self._colHidden[nwOutline[hName]] = tmpHidden[hName]
except Exception:
logger.warning("Ignored unknown outline column '%s'", str(hName))
self.hiddenStateChanged.emit() self.hiddenStateChanged.emit()
@@ -632,30 +621,19 @@ class GuiOutlineTree(QTreeWidget):
if self._lastBuild == 0: if self._lastBuild == 0:
return return
treeOrder = [] colState = {}
colWidth = {}
colHidden = {}
for hItem in nwOutline:
colWidth[hItem.name] = CONFIG.rpxInt(self._colWidth[hItem])
colHidden[hItem.name] = self._colHidden[hItem]
for iCol in range(self.columnCount()): for iCol in range(self.columnCount()):
hName = self._treeOrder[iCol].name hItem = self._treeOrder[iCol]
treeOrder.append(hName)
iLog = self.treeHead.logicalIndex(iCol) iLog = self.treeHead.logicalIndex(iCol)
logWidth = CONFIG.rpxInt(self.columnWidth(iLog))
logHidden = self.isColumnHidden(iLog) logHidden = self.isColumnHidden(iLog)
orgWidth = CONFIG.rpxInt(self._colWidth[hItem])
colHidden[hName] = logHidden logWidth = CONFIG.rpxInt(self.columnWidth(iLog))
if not logHidden and logWidth > 0: colState[hItem.name] = [
colWidth[hName] = logWidth logHidden, orgWidth if logHidden and logWidth == 0 else logWidth
]
pOptions = self.theProject.options pOptions = self.theProject.options
pOptions.setValue("GuiOutline", "headerOrder", treeOrder) pOptions.setValue("GuiOutline", "columnState", colState)
pOptions.setValue("GuiOutline", "columnWidth", colWidth)
pOptions.setValue("GuiOutline", "columnHidden", colHidden)
pOptions.saveSettings() pOptions.saveSettings()
return return