Clean up the config class names and methods

This commit is contained in:
Veronica Berglyd Olsen
2022-11-09 18:47:50 +01:00
parent 47b57172eb
commit 8721d53ab2
13 changed files with 81 additions and 86 deletions
+28 -36
View File
@@ -74,10 +74,10 @@ class Config:
self._appPath = self._appRoot
# Runtime Settings and Variables
self.hasError = False # True if the config class encountered an error
self.errData = [] # List of error messages
self.hasError = False # True if the config class encountered an error
self.errData = [] # List of error messages
self.confChanged = False # True whenever the config has chenged, false after save
self.cmdOpen = None # Path from command line for project to be opened on launch
self.cmdOpen = None # Path from command line for project to be opened on launch
# Localisation Info
self._qLocal = QLocale.system()
@@ -171,7 +171,7 @@ class Config:
self.fmtPadThin = False
# Spell Checking Settings
self.spellLanguage = None
self.spellLanguage = "en"
# Search Bar Switches
self.searchCase = False
@@ -261,21 +261,21 @@ class Config:
"""
return int(theSize/self.guiScale)
def getDataPath(self, target=None):
def dataPath(self, target=None):
"""Return a path in the data folder.
"""
if isinstance(target, str):
return self._dataPath / target
return self._dataPath
def getAssetPath(self, target=None):
def assetPath(self, target=None):
"""Return a path in the assets folder.
"""
if isinstance(target, str):
return self._appPath / "assets" / target
return self._appPath / "assets"
def getLastPath(self):
def lastPath(self):
"""Return the last path used by the user, but ensure it exists.
"""
if self._lastPath.is_dir():
@@ -294,7 +294,6 @@ class Config:
if isinstance(confPath, (str, Path)):
logger.info("Setting config from alternative path: %s", confPath)
self._confPath = Path(confPath)
if isinstance(dataPath, (str, Path)):
logger.info("Setting data path from alternative path: %s", dataPath)
self._dataPath = Path(dataPath)
@@ -306,33 +305,25 @@ class Config:
logger.debug("Last Path: %s", self._lastPath)
logger.debug("PDF Manual: %s", self.pdfDocs)
# If the config and data folders don't not exist, create them
# If the config and data folders don't exist, create them
# This assumes that the os config and data folders exist
self._confPath.mkdir(exist_ok=True)
self._dataPath.mkdir(exist_ok=True)
# We don't error on these failing since they are not essential
# Also create the syntax and themes folders if possible
if self._dataPath.is_dir():
(self._dataPath / "syntax").mkdir(exist_ok=True)
(self._dataPath / "themes").mkdir(exist_ok=True)
# Check if config file exists
# Check if config file exists, and load it. If not, we save defaults
if (self._confPath / nwFiles.CONF_FILE).is_file():
# If it exists, load it
self.loadConfig()
else:
# If it does not exist, save a copy of the default values
self.saveConfig()
# Load recent projects cache
self.loadRecentCache()
# Check the availability of optional packages
self._checkOptionalPackages()
if not self.spellLanguage:
self.spellLanguage = "en"
logger.debug("Config initialisation complete")
return True
@@ -694,16 +685,17 @@ class Config:
##
def setLastPath(self, lastPath):
"""Set the last used path (by the user).
"""Set the last used path. Only the folder is saved, so if the
path is not a folder, the parent of the path is used instead.
"""
if isinstance(lastPath, str):
if isinstance(lastPath, (str, Path)):
lastPath = Path(lastPath)
if isinstance(lastPath, Path):
if lastPath.is_file():
self._lastPath = lastPath.parent
elif lastPath.is_dir():
if not lastPath.is_dir():
lastPath = lastPath.parent
if lastPath.is_dir():
self._lastPath = lastPath
return True
logger.debug("Last path updated: %s" % self._lastPath)
return
def setWinSize(self, newWidth, newHeight):
"""Set the size of the main window, but only if the change is
@@ -719,7 +711,7 @@ class Config:
if abs(self.winGeometry[1] - newHeight) > 5:
self.winGeometry[1] = newHeight
self.confChanged = True
return True
return
def setPreferencesSize(self, newWidth, newHeight):
"""Sat the size of the Preferences dialog window.
@@ -727,63 +719,63 @@ class Config:
self.prefGeometry[0] = int(newWidth/self.guiScale)
self.prefGeometry[1] = int(newHeight/self.guiScale)
self.confChanged = True
return True
return
def setProjColWidths(self, colWidths):
"""Set the column widths of the Load Project dialog.
"""
self.projColWidth = [int(x/self.guiScale) for x in colWidths]
self.confChanged = True
return True
return
def setMainPanePos(self, panePos):
"""Set the position of the main GUI splitter.
"""
self.mainPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True
return
def setDocPanePos(self, panePos):
"""Set the position of the main editor/viewer splitter.
"""
self.docPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True
return
def setViewPanePos(self, panePos):
"""Set the position of the viewer meta data splitter.
"""
self.viewPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True
return
def setOutlinePanePos(self, panePos):
"""Set the position of the outline details splitter.
"""
self.outlnPanePos = [int(x/self.guiScale) for x in panePos]
self.confChanged = True
return True
return
def setShowRefPanel(self, checkState):
"""Set the visibility state of the reference panel.
"""
self.showRefPanel = checkState
self.confChanged = True
return self.showRefPanel
return
def setViewComments(self, viewState):
"""Set the visibility state of comments in the viewer.
"""
self.viewComments = viewState
self.confChanged = True
return self.viewComments
return
def setViewSynopsis(self, viewState):
"""Set the visibility state of synopsis comments in the viewer.
"""
self.viewSynopsis = viewState
self.confChanged = True
return self.viewSynopsis
return
##
# Default Setters
+1 -1
View File
@@ -430,7 +430,7 @@ class ProjectBuilder:
logger.error("No project path set for the example project")
return False
pkgSample = self.mainConf.getAssetPath("sample.zip")
pkgSample = self.mainConf.assetPath("sample.zip")
if pkgSample.is_file():
try:
shutil.unpack_archive(pkgSample, projPath)
+2 -2
View File
@@ -232,7 +232,7 @@ class GuiAbout(QDialog):
def _fillNotesPage(self):
"""Load the content for the Release Notes page.
"""
docPath = self.mainConf.getAssetPath("text") / "release_notes.htm"
docPath = self.mainConf.assetPath("text") / "release_notes.htm"
docText = readTextFile(docPath)
if docText:
self.pageNotes.setHtml(docText)
@@ -243,7 +243,7 @@ class GuiAbout(QDialog):
def _fillLicensePage(self):
"""Load the content for the Licence page.
"""
docPath = self.mainConf.getAssetPath("text") / "gplv3_en.htm"
docPath = self.mainConf.assetPath("text") / "gplv3_en.htm"
docText = readTextFile(docPath)
if docText:
self.pageLicense.setHtml(docText)
+6 -6
View File
@@ -118,10 +118,10 @@ class GuiTheme:
self._availThemes = {}
self._availSyntax = {}
self._listConf(self._availSyntax, self.mainConf.getAssetPath("syntax"))
self._listConf(self._availThemes, self.mainConf.getAssetPath("themes"))
self._listConf(self._availSyntax, self.mainConf.getDataPath("syntax"))
self._listConf(self._availThemes, self.mainConf.getDataPath("themes"))
self._listConf(self._availSyntax, self.mainConf.assetPath("syntax"))
self._listConf(self._availThemes, self.mainConf.assetPath("themes"))
self._listConf(self._availSyntax, self.mainConf.dataPath("syntax"))
self._listConf(self._availThemes, self.mainConf.dataPath("themes"))
self.loadTheme()
self.loadSyntax()
@@ -472,7 +472,7 @@ class GuiIcons:
self._confName = "icons.conf"
# Icon Theme Path
self._iconPath = self.mainConf.getAssetPath("icons")
self._iconPath = self.mainConf.assetPath("icons")
# Icon Theme Meta
self.themeName = ""
@@ -568,7 +568,7 @@ class GuiIcons:
if decoKey in self._themeMap:
imgPath = self._themeMap[decoKey]
elif decoKey in self.IMAGE_MAP:
imgPath = self.mainConf.getAssetPath("images") / self.IMAGE_MAP[decoKey]
imgPath = self.mainConf.assetPath("images") / self.IMAGE_MAP[decoKey]
else:
logger.error("Decoration with name '%s' does not exist", decoKey)
return QPixmap()
+2 -2
View File
@@ -97,7 +97,7 @@ class GuiMain(QMainWindow):
self.resize(*self.mainConf.getWinSize())
self._updateWindowTitle()
nwIcon = self.mainConf.getAssetPath("icons") / "novelwriter.svg"
nwIcon = self.mainConf.assetPath("icons") / "novelwriter.svg"
self.nwIcon = QIcon(str(nwIcon)) if nwIcon.is_file() else QIcon()
self.setWindowIcon(self.nwIcon)
qApp.setWindowIcon(self.nwIcon)
@@ -698,7 +698,7 @@ class GuiMain(QMainWindow):
logger.error("No project open")
return False
lastPath = self.mainConf.getLastPath()
lastPath = self.mainConf.lastPath()
extFilter = [
self.tr("Text files ({0})").format("*.txt"),
self.tr("Markdown files ({0})").format("*.md"),
+1 -1
View File
@@ -890,7 +890,7 @@ class GuiBuildNovel(QDialog):
cleanName = makeFileNameSafe(self.theProject.data.name)
fileName = "%s.%s" % (cleanName, fileExt)
savePath = self.mainConf.getLastPath() / fileName
savePath = self.mainConf.lastPath() / fileName
savePath, _ = QFileDialog.getSaveFileName(
self, self.tr("Save Document As"), str(savePath)
)
+1 -1
View File
@@ -119,7 +119,7 @@ class GuiLipsum(QDialog):
def _doInsert(self):
"""Load the text and insert it in the open document.
"""
lipsumFile = self.mainConf.getAssetPath("text") / "lipsum.txt"
lipsumFile = self.mainConf.assetPath("text") / "lipsum.txt"
lipsumText = readTextFile(lipsumFile).splitlines()
if self.randSwitch.isChecked():
+1 -1
View File
@@ -236,7 +236,7 @@ class ProjWizardFolderPage(QWizardPage):
def _doBrowse(self):
"""Select a project folder.
"""
lastPath = self.mainConf.getLastPath()
lastPath = self.mainConf.lastPath()
projDir = QFileDialog.getExistingDirectory(
self, self.tr("Select Project Folder"), str(lastPath), options=QFileDialog.ShowDirsOnly
)
+1 -1
View File
@@ -362,7 +362,7 @@ class GuiWritingStats(QDialog):
return False
# Generate the file name
savePath = self.mainConf.getLastPath() / f"sessionStats.{fileExt}"
savePath = self.mainConf.lastPath() / f"sessionStats.{fileExt}"
savePath, _ = QFileDialog.getSaveFileName(
self, self.tr("Save Data As"), str(savePath), "%s (*.%s)" % (textFmt, fileExt)
)