Make options class handle enums, and add plot column to novel tree
This commit is contained in:
@@ -28,6 +28,8 @@ import os
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
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
|
||||||
from novelwriter.constants import nwFiles
|
from novelwriter.constants import nwFiles
|
||||||
@@ -138,7 +140,10 @@ class OptionState():
|
|||||||
if group not in self._theState:
|
if group not in self._theState:
|
||||||
self._theState[group] = {}
|
self._theState[group] = {}
|
||||||
|
|
||||||
self._theState[group][name] = value
|
if isinstance(value, Enum):
|
||||||
|
self._theState[group][name] = value.name
|
||||||
|
else:
|
||||||
|
self._theState[group][name] = value
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -187,4 +192,16 @@ class OptionState():
|
|||||||
return checkBool(self._theState[group].get(name, default), default)
|
return checkBool(self._theState[group].get(name, default), default)
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
def getEnum(self, group, name, lookup, default):
|
||||||
|
"""Return the value mapped to an enum. Otherwise return the
|
||||||
|
default value
|
||||||
|
"""
|
||||||
|
if issubclass(lookup, Enum):
|
||||||
|
if group in self._theState:
|
||||||
|
if name in self._theState[group]:
|
||||||
|
value = self._theState[group][name]
|
||||||
|
if value in lookup.__members__:
|
||||||
|
return lookup[value]
|
||||||
|
return default
|
||||||
|
|
||||||
# END Class OptionState
|
# END Class OptionState
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ class NovelColumnType(Enum):
|
|||||||
HIDDEN = 0
|
HIDDEN = 0
|
||||||
POV = 1
|
POV = 1
|
||||||
FOCUS = 2
|
FOCUS = 2
|
||||||
|
PLOT = 3
|
||||||
|
|
||||||
# END Enum NovelColumnType
|
# END Enum NovelColumnType
|
||||||
|
|
||||||
@@ -216,6 +217,9 @@ class GuiNovelToolBar(QWidget):
|
|||||||
self.mCol3.addAction(self.tr("Focus Character")).triggered.connect(
|
self.mCol3.addAction(self.tr("Focus Character")).triggered.connect(
|
||||||
lambda: self.novelView.novelTree.setLastColType(NovelColumnType.FOCUS)
|
lambda: self.novelView.novelTree.setLastColType(NovelColumnType.FOCUS)
|
||||||
)
|
)
|
||||||
|
self.mCol3.addAction(self.tr("Novel Plot")).triggered.connect(
|
||||||
|
lambda: self.novelView.novelTree.setLastColType(NovelColumnType.PLOT)
|
||||||
|
)
|
||||||
|
|
||||||
self.tbMore = QToolButton(self)
|
self.tbMore = QToolButton(self)
|
||||||
self.tbMore.setToolTip(self.tr("More Options"))
|
self.tbMore.setToolTip(self.tr("More Options"))
|
||||||
@@ -306,6 +310,7 @@ class GuiNovelTree(QTreeWidget):
|
|||||||
# Cached i18n Strings
|
# Cached i18n Strings
|
||||||
self._povLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.POV_KEY])
|
self._povLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.POV_KEY])
|
||||||
self._focLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.FOCUS_KEY])
|
self._focLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.FOCUS_KEY])
|
||||||
|
self._pltLabel = trConst(nwLabels.KEY_NAME[nwKeyWords.PLOT_KEY])
|
||||||
|
|
||||||
# Build GUI
|
# Build GUI
|
||||||
# =========
|
# =========
|
||||||
@@ -373,28 +378,17 @@ class GuiNovelTree(QTreeWidget):
|
|||||||
def loadOptions(self):
|
def loadOptions(self):
|
||||||
"""Load user options.
|
"""Load user options.
|
||||||
"""
|
"""
|
||||||
try:
|
self._lastCol = self.theProject.options.getEnum(
|
||||||
lastCol = NovelColumnType[self.theProject.options.getString(
|
"GuiNovelView", "lastCol", NovelColumnType, NovelColumnType.POV
|
||||||
"GuiNovelView", "lastCol", NovelColumnType.POV.name
|
)
|
||||||
)]
|
self.setColumnHidden(self.C_LAST, self._lastCol == NovelColumnType.HIDDEN)
|
||||||
except Exception:
|
|
||||||
logger.error("Failed to load last column type from options")
|
|
||||||
return False
|
|
||||||
|
|
||||||
self._lastCol = lastCol
|
|
||||||
self.setColumnHidden(self.C_LAST, lastCol == NovelColumnType.HIDDEN)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def saveOptions(self):
|
def saveOptions(self):
|
||||||
"""Save user options.
|
"""Save user options.
|
||||||
"""
|
"""
|
||||||
try:
|
self.theProject.options.setValue("GuiNovelView", "lastCol", self._lastCol)
|
||||||
self.theProject.options.setValue("GuiNovelView", "lastCol", self._lastCol.name)
|
return
|
||||||
except Exception:
|
|
||||||
logger.error("Failed to save last column type to options")
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def refreshTree(self, rootHandle=None, overRide=False):
|
def refreshTree(self, rootHandle=None, overRide=False):
|
||||||
"""Called whenever the Novel tab is activated.
|
"""Called whenever the Novel tab is activated.
|
||||||
@@ -603,6 +597,11 @@ class GuiNovelTree(QTreeWidget):
|
|||||||
newItem.setText(self.C_LAST, newText)
|
newItem.setText(self.C_LAST, newText)
|
||||||
if newText:
|
if newText:
|
||||||
newItem.setToolTip(self.C_LAST, f"{self._focLabel}: {newText}")
|
newItem.setToolTip(self.C_LAST, f"{self._focLabel}: {newText}")
|
||||||
|
elif self._lastCol == NovelColumnType.PLOT:
|
||||||
|
newText = ", ".join(theRefs[nwKeyWords.PLOT_KEY])
|
||||||
|
newItem.setText(self.C_LAST, newText)
|
||||||
|
if newText:
|
||||||
|
newItem.setToolTip(self.C_LAST, f"{self._pltLabel}: {newText}")
|
||||||
|
|
||||||
return newItem
|
return newItem
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user