Move project data class to project file
This commit is contained in:
+314
-16
@@ -4,7 +4,8 @@ novelWriter – Project Wrapper
|
||||
Data class for novelWriter projects
|
||||
|
||||
File History:
|
||||
Created: 2018-09-29 [0.0.1]
|
||||
Created: 2018-09-29 [0.0.1] NWProject
|
||||
Created: 2022-10-30 [2.0rc1] NWProjectData
|
||||
|
||||
This file is a part of novelWriter
|
||||
Copyright 2018–2022, Veronica Berglyd Olsen
|
||||
@@ -32,38 +33,40 @@ import novelwriter
|
||||
from time import time
|
||||
from functools import partial
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication
|
||||
from PyQt5.QtCore import QCoreApplication, QObject, pyqtSignal
|
||||
|
||||
from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
|
||||
from novelwriter.error import logException
|
||||
from novelwriter.common import (
|
||||
checkStringNone, isHandle, formatTimeStamp, makeFileNameSafe, hexToInt,
|
||||
minmax, simplified
|
||||
)
|
||||
from novelwriter.constants import trConst, nwFiles, nwLabels
|
||||
from novelwriter.core.tree import NWTree
|
||||
from novelwriter.core.item import NWItem
|
||||
from novelwriter.core.index import NWIndex
|
||||
from novelwriter.core.status import NWStatus
|
||||
from novelwriter.core.options import OptionState
|
||||
from novelwriter.core.document import NWDoc
|
||||
from novelwriter.core.projectxml import ProjectXMLReader, ProjectXMLWriter, XMLReadState
|
||||
from novelwriter.core.projectdata import NWProjectData
|
||||
from novelwriter.common import (
|
||||
checkBool, checkInt, checkString, checkStringNone, formatTimeStamp, hexToInt, isHandle,
|
||||
makeFileNameSafe, minmax, simplified,
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class NWProject:
|
||||
class NWProject(QObject):
|
||||
|
||||
FILE_VERSION = "1.4" # The current project file format version
|
||||
projectStatusChanged = pyqtSignal(bool)
|
||||
|
||||
def __init__(self, mainGui):
|
||||
super().__init__(parent=mainGui)
|
||||
|
||||
# Internal
|
||||
self.mainConf = novelwriter.CONFIG
|
||||
self.mainGui = mainGui
|
||||
|
||||
# Project Data
|
||||
self._data = NWProjectData()
|
||||
self._data = NWProjectData(self)
|
||||
|
||||
# Core Elements
|
||||
self._optState = OptionState(self) # Project-specific GUI options
|
||||
@@ -240,7 +243,7 @@ class NWProject:
|
||||
# Project Tree
|
||||
self._projTree.clear()
|
||||
|
||||
self._data = NWProjectData()
|
||||
self._data = NWProjectData(self)
|
||||
|
||||
# Project Settings
|
||||
self.projPath = None
|
||||
@@ -446,7 +449,7 @@ class NWProject:
|
||||
# Open The Project XML File
|
||||
# =========================
|
||||
|
||||
self._data = NWProjectData()
|
||||
self._data = NWProjectData(self)
|
||||
xmlReader = ProjectXMLReader(fileName)
|
||||
xmlParsed = xmlReader.read(self._data)
|
||||
|
||||
@@ -857,13 +860,10 @@ class NWProject:
|
||||
information to the GUI statusbar.
|
||||
"""
|
||||
self._projChanged = bValue
|
||||
self.mainGui.mainStatus.doUpdateProjectStatus(bValue)
|
||||
self.projectStatusChanged.emit(self._projChanged)
|
||||
if bValue is True:
|
||||
# If we've changed the project at all, this should be True
|
||||
self._projAltered = True
|
||||
else:
|
||||
# If we're resetting the status, also reset for data class
|
||||
self._data.resetProjectChanged()
|
||||
return self._projChanged
|
||||
|
||||
##
|
||||
@@ -1310,3 +1310,301 @@ class NWProject:
|
||||
return True
|
||||
|
||||
# END Class NWProject
|
||||
|
||||
|
||||
class NWProjectData:
|
||||
|
||||
def __init__(self, theProject):
|
||||
|
||||
self.theProject = theProject
|
||||
|
||||
# Project Meta
|
||||
self._name = ""
|
||||
self._title = ""
|
||||
self._authors = []
|
||||
self._saveCount = 0
|
||||
self._autoCount = 0
|
||||
self._editTime = 0
|
||||
|
||||
# Project Settings
|
||||
self._doBackup = True
|
||||
self._language = None
|
||||
self._spellCheck = False
|
||||
self._spellLang = None
|
||||
self._lastHandle = {
|
||||
"editor": "",
|
||||
"viewer": "",
|
||||
"novelTree": "",
|
||||
"outline": "",
|
||||
}
|
||||
self._lastCount = {}
|
||||
self._currCount = {}
|
||||
|
||||
self._autoReplace = {}
|
||||
self._titleFormat = {
|
||||
"title": "%title%",
|
||||
"chapter": "%title%",
|
||||
"unnumbered": "%title%",
|
||||
"scene": "* * *",
|
||||
"section": "",
|
||||
}
|
||||
|
||||
self._status = NWStatus(NWStatus.STATUS)
|
||||
self._import = NWStatus(NWStatus.IMPORT)
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
# Properties
|
||||
##
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
return self._title
|
||||
|
||||
@property
|
||||
def authors(self):
|
||||
return self._authors
|
||||
|
||||
@property
|
||||
def saveCount(self):
|
||||
return self._saveCount
|
||||
|
||||
@property
|
||||
def autoCount(self):
|
||||
return self._autoCount
|
||||
|
||||
@property
|
||||
def editTime(self):
|
||||
return self._editTime
|
||||
|
||||
@property
|
||||
def doBackup(self):
|
||||
return self._doBackup
|
||||
|
||||
@property
|
||||
def language(self):
|
||||
return self._language
|
||||
|
||||
@property
|
||||
def spellCheck(self):
|
||||
return self._spellCheck
|
||||
|
||||
@property
|
||||
def spellLang(self):
|
||||
return self._spellLang
|
||||
|
||||
@property
|
||||
def lastHandle(self):
|
||||
return self._lastHandle
|
||||
|
||||
@property
|
||||
def autoReplace(self):
|
||||
return self._autoReplace
|
||||
|
||||
@property
|
||||
def titleFormat(self):
|
||||
return self._titleFormat
|
||||
|
||||
@property
|
||||
def itemStatus(self):
|
||||
return self._status
|
||||
|
||||
@property
|
||||
def itemImport(self):
|
||||
return self._import
|
||||
|
||||
##
|
||||
# Methods
|
||||
##
|
||||
|
||||
def addAuthor(self, value):
|
||||
"""Add an author to the authors list.
|
||||
"""
|
||||
self._authors.append(simplified(str(value)))
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def incSaveCount(self):
|
||||
"""Increment the save count by one.
|
||||
"""
|
||||
self._saveCount += 1
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def incAutoCount(self):
|
||||
"""Increment the auto save count by one.
|
||||
"""
|
||||
self._autoCount += 1
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
##
|
||||
# Getters
|
||||
##
|
||||
|
||||
def getLastHandle(self, component):
|
||||
"""Retrieve the last used handle for a given component.
|
||||
"""
|
||||
return self._lastHandle.get(component, None)
|
||||
|
||||
def getLastCount(self, type):
|
||||
"""Retrieve the last word count for a given type.
|
||||
"""
|
||||
return self._lastCount.get(type, 0)
|
||||
|
||||
def getCurrCount(self, type):
|
||||
"""Retrieve the current word count for a given type.
|
||||
"""
|
||||
return self._currCount.get(type, 0)
|
||||
|
||||
def getTitleFormat(self, kind):
|
||||
"""Retrieve the title format string for a given kind of header.
|
||||
"""
|
||||
return self._titleFormat.get(kind, "%title%")
|
||||
|
||||
##
|
||||
# Setters
|
||||
##
|
||||
|
||||
def setName(self, value):
|
||||
"""Set a new project name.
|
||||
"""
|
||||
if value != self._name:
|
||||
self._name = simplified(str(value))
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setTitle(self, value):
|
||||
"""Set a new novel title.
|
||||
"""
|
||||
if value != self._title:
|
||||
self._title = simplified(str(value))
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setAuthors(self, value):
|
||||
"""Set the list of authors from either a string with one author
|
||||
per line, or a list of authors.
|
||||
"""
|
||||
self._authors = []
|
||||
self.theProject.setProjectChanged(True)
|
||||
if isinstance(value, str):
|
||||
for author in value.splitlines():
|
||||
author = simplified(author)
|
||||
if author:
|
||||
self._authors.append(author)
|
||||
self.theProject.setProjectChanged(True)
|
||||
elif isinstance(value, list):
|
||||
self._authors = value
|
||||
return
|
||||
|
||||
def setSaveCount(self, value):
|
||||
"""Set the save count from last session.
|
||||
"""
|
||||
self._saveCount = checkInt(value, 0)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setAutoCount(self, value):
|
||||
"""Set the auto save count from last session.
|
||||
"""
|
||||
self._autoCount = checkInt(value, 0)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setEditTime(self, value):
|
||||
"""Set tyje edit time from last session.
|
||||
"""
|
||||
self._editTime = checkInt(value, 0)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setDoBackup(self, value):
|
||||
"""Set the do write backup flag.
|
||||
"""
|
||||
if value != self._doBackup:
|
||||
self._doBackup = checkBool(value, False)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setLanguage(self, value):
|
||||
"""Set the project language.
|
||||
"""
|
||||
if value != self._language:
|
||||
self._language = checkStringNone(value, None)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setSpellCheck(self, value):
|
||||
"""Set the spell check flag.
|
||||
"""
|
||||
if value != self._spellCheck:
|
||||
self._spellCheck = checkBool(value, False)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setSpellLang(self, value):
|
||||
"""Set the spell check language.
|
||||
"""
|
||||
if value != self._spellLang:
|
||||
self._spellLang = checkStringNone(value, None)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setLastHandle(self, value, component=None):
|
||||
"""Set a last used handle into the handle registry. If component
|
||||
is None, the value is assumed to be the whole dictionary of
|
||||
values.
|
||||
"""
|
||||
if isinstance(component, str):
|
||||
self._lastHandle[component] = checkString(value, "")
|
||||
self.theProject.setProjectChanged(True)
|
||||
elif isinstance(value, dict):
|
||||
for key, entry in value.items():
|
||||
if key in self._lastHandle:
|
||||
self._lastHandle[key] = checkString(entry, "")
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setLastCount(self, value, type):
|
||||
"""Set the word counts from last session.
|
||||
"""
|
||||
self._lastCount[type] = checkInt(value, 0)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setCurrCount(self, value, type):
|
||||
"""Set the current word counts.
|
||||
"""
|
||||
if value != self._currCount.get(type, 0):
|
||||
self._currCount[type] = checkInt(value, 0)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setAutoReplace(self, value):
|
||||
"""Set the auto-replace dictionary.
|
||||
"""
|
||||
if isinstance(value, dict):
|
||||
self._autoReplace = {}
|
||||
for key, entry in value.items():
|
||||
if isinstance(entry, str):
|
||||
self._autoReplace[key] = simplified(entry)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
def setTitleFormat(self, value):
|
||||
"""Set the title formats.
|
||||
"""
|
||||
if isinstance(value, dict):
|
||||
for key, entry in value.items():
|
||||
if key in self._titleFormat and isinstance(entry, str):
|
||||
self._titleFormat[key] = simplified(entry)
|
||||
self.theProject.setProjectChanged(True)
|
||||
return
|
||||
|
||||
# END Class NWProjectData
|
||||
|
||||
Reference in New Issue
Block a user