From ecb739425bd2adf40765a4ea842668dac81db8d4 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 19 Feb 2020 18:58:19 +0100 Subject: [PATCH] Added new class to save dialog optins --- nw/constants/constants.py | 1 + nw/tools/optlaststate.py | 119 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/nw/constants/constants.py b/nw/constants/constants.py index 78e0c7b3..80887188 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -25,6 +25,7 @@ class nwFiles(): PROJ_DICT = "wordlist.txt" SESS_INFO = "sessionInfo.log" INDEX_FILE = "tagsIndex.json" + OPTS_FILE = "guiOptions.json" EXPORT_OPT = "exportOptions.json" TLINE_OPT = "timelineOptions.json" SLOG_OPT = "sessionLogOptions.json" diff --git a/nw/tools/optlaststate.py b/nw/tools/optlaststate.py index 0df6ff68..44c6a402 100644 --- a/nw/tools/optlaststate.py +++ b/nw/tools/optlaststate.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- -"""novelWriter Options Last State +"""novelWriter Options State - novelWriter – Options Last State -================================== + novelWriter – Options State +============================= Class holding the last state of GUI options File History: - Created: 2019-10-21 [0.3.1] + Created: 2019-10-21 [0.3.1] - Original version meant to be sub classed + Created: 2020-02-19 [0.4.5] - Rewritten from superclass to single file tool """ @@ -17,9 +18,119 @@ import nw from os import path from nw.common import checkString, checkBool, checkInt +from nw.constants import nwFiles logger = logging.getLogger(__name__) +class OptionState(): + + def __init__(self, theProject): + + self.theProject = theProject + self.theState = {} + self.stringOpt = () + self.boolOpt = () + self.intOpt = () + + return + + def loadSettings(self): + """Load the options dictionary from the project settings file. + """ + + stateFile = path.join(self.theProject.projMeta, nwFiles.OPTS_FILE) + theState = {} + + if path.isfile(stateFile): + logger.debug("Loading GUI options file") + try: + with open(stateFile,mode="r",encoding="utf8") as inFile: + theJson = inFile.read() + theState = json.loads(theJson) + except Exception as e: + logger.error("Failed to load GUI options file") + logger.error(str(e)) + return False + for anOpt in theState: + self.theState[anOpt] = theState[anOpt] + + return True + + def saveSettings(self): + """Save the options dictionary to the project settings file. + """ + + stateFile = path.join(self.theProject.projMeta, nwFiles.OPTS_FILE) + logger.debug("Saving GUI options file") + + try: + with open(stateFile,mode="w+",encoding="utf8") as outFile: + outFile.write(json.dumps(self.theState, indent=2)) + except Exception as e: + logger.error("Failed to save GUI options file") + logger.error(str(e)) + return False + + return True + + def setValue(self, setGroup, setName, setValue): + """Saves a value, with a given group and name. + """ + if not setGroup in self.theState: + self.theState[setGroup] = {} + self.theState[setGroup][setName] = setValue + return True + + def getString(self, getGroup, getName, defaultValue): + """Return the value as a string, if it exists. Otherwise, return + the default value. + """ + if getGroup in self.theState: + if getName in self.theState[getGroup]: + try: + return str(self.theState[getGroup][getName]) + except: + return defaultValue + return defaultValue + + def getInt(self, getGroup, getName, defaultValue): + """Return the value as an int, if it exists. Otherwise, return + the default value. + """ + if getGroup in self.theState: + if getName in self.theState[getGroup]: + try: + return int(self.theState[getGroup][getName]) + except: + return defaultValue + return defaultValue + + def getFloat(self, getGroup, getName, defaultValue): + """Return the value as a float, if it exists. Otherwise, return + the default value. + """ + if getGroup in self.theState: + if getName in self.theState[getGroup]: + try: + return float(self.theState[getGroup][getName]) + except: + return defaultValue + return defaultValue + + def getBool(self, getGroup, getName, defaultValue): + """Return the value as a bool, if it exists. Otherwise, return + the default value. + """ + if getGroup in self.theState: + if getName in self.theState[getGroup]: + try: + return bool(self.theState[getGroup][getName]) + except: + return defaultValue + return defaultValue + +# END Class OptionState + class OptLastState(): def __init__(self, theProject, theFile):