Tighten up the use of item and nwdoc objects in editor class

This commit is contained in:
Veronica K. B. Olsen
2021-04-25 23:46:09 +02:00
parent e7102725bd
commit 5ee39d7ecc
3 changed files with 40 additions and 43 deletions
+8 -18
View File
@@ -40,26 +40,20 @@ class NWDoc():
# Internal Variables # Internal Variables
self._docHandle = theHandle self._docHandle = theHandle
self._theItem = self.theProject.projTree[theHandle] self._theItem = None
self._fileLoc = None self._fileLoc = None
self._docMeta = {} self._docMeta = {}
self._docError = "" self._docError = ""
if theHandle is not None:
self._theItem = self.theProject.projTree[theHandle]
return return
## ##
# Class Methods # Class Methods
## ##
def clearDocument(self):
"""Clear the document contents.
"""
self._theItem = None
self._docHandle = None
self._fileLoc = None
self._docMeta = {}
return
def readDocument(self, isOrphan=False): def readDocument(self, isOrphan=False):
"""Read a document from set handle, capturing potential file """Read a document from set handle, capturing potential file
system errors and parse meta data. If the document doesn't exist system errors and parse meta data. If the document doesn't exist
@@ -68,11 +62,11 @@ class NWDoc():
""" """
self._docError = "" self._docError = ""
if not isHandle(self._docHandle): if not isHandle(self._docHandle):
self._docError = "No document handle set." logger.error("No document handle set")
return None return None
if self._theItem is None and not isOrphan: if self._theItem is None and not isOrphan:
self._docError = "Unknown novelWriter document." logger.error("Unknown novelWriter document")
return None return None
docFile = self._docHandle+".nwd" docFile = self._docHandle+".nwd"
@@ -101,10 +95,6 @@ class NWDoc():
except Exception as e: except Exception as e:
self._docError = str(e) self._docError = str(e)
# Note: Document must be cleared in case of an io error,
# or else the auto-save or save will try to overwrite it
# with an empty file. Return None to alert the caller.
self.clearDocument()
return None return None
else: else:
@@ -121,7 +111,7 @@ class NWDoc():
""" """
self._docError = "" self._docError = ""
if not isHandle(self._docHandle): if not isHandle(self._docHandle):
self._docError = "No document handle set." logger.error("No document handle set")
return False return False
self.theProject.ensureFolderStructure() self.theProject.ensureFolderStructure()
@@ -164,7 +154,7 @@ class NWDoc():
""" """
self._docError = "" self._docError = ""
if not isHandle(self._docHandle): if not isHandle(self._docHandle):
self._docError = "No document handle set." logger.error("No document handle set")
return False return False
docFile = self._docHandle+".nwd" docFile = self._docHandle+".nwd"
+31 -24
View File
@@ -75,7 +75,9 @@ class GuiDocEditor(QTextEdit):
self.theTheme = theParent.theTheme self.theTheme = theParent.theTheme
self.theIndex = theParent.theIndex self.theIndex = theParent.theIndex
self.theProject = theParent.theProject self.theProject = theParent.theProject
self.nwDocument = NWDoc(self.theProject, None)
self.nwDocument = None
self.nwItem = None
self.docChanged = False # Flag for changed status of document self.docChanged = False # Flag for changed status of document
self.spellCheck = False # Flag for spell checking enabled self.spellCheck = False # Flag for spell checking enabled
@@ -165,7 +167,7 @@ class GuiDocEditor(QTextEdit):
"""Clear the current document and reset all document related """Clear the current document and reset all document related
flags and counters. flags and counters.
""" """
self.nwDocument = NWDoc(self.theProject, None) self.nwDocument = None
self.setReadOnly(True) self.setReadOnly(True)
self.clear() self.clear()
self.wcTimer.stop() self.wcTimer.stop()
@@ -298,6 +300,7 @@ class GuiDocEditor(QTextEdit):
the file. the file.
""" """
self.nwDocument = NWDoc(self.theProject, tHandle) self.nwDocument = NWDoc(self.theProject, tHandle)
self.nwItem = self.nwDocument.getCurrentItem()
theDoc = self.nwDocument.readDocument() theDoc = self.nwDocument.readDocument()
if theDoc is None: if theDoc is None:
@@ -352,16 +355,15 @@ class GuiDocEditor(QTextEdit):
self.updateDocMargins() self.updateDocMargins()
self.hLight.spellCheck = spTemp self.hLight.spellCheck = spTemp
theItem = self.nwDocument.getCurrentItem() if tLine is None and self.nwItem is not None:
if tLine is None and theItem is not None:
# For large documents we queue the repositioning until the # For large documents we queue the repositioning until the
# document layout has grown past the point we want to move # document layout has grown past the point we want to move
# the cursor to. This makes the loading significantly # the cursor to. This makes the loading significantly
# faster. # faster.
if docSize > 50000: if docSize > 50000:
self.queuePos = theItem.cursorPos self.queuePos = self.nwItem.cursorPos
else: else:
self.setCursorPosition(theItem.cursorPos) self.setCursorPosition(self.nwItem.cursorPos)
else: else:
self.setCursorLine(tLine) self.setCursorLine(tLine)
@@ -380,9 +382,9 @@ class GuiDocEditor(QTextEdit):
self.setCursorPosition(0) self.setCursorPosition(0)
# Update the status bar # Update the status bar
if theItem is not None: if self.nwItem is not None:
self.theParent.setStatus( self.theParent.setStatus(
self.tr("Opened Document: {0}").format(theItem.itemName) self.tr("Opened Document: {0}").format(self.nwItem.itemName)
) )
return True return True
@@ -431,19 +433,25 @@ class GuiDocEditor(QTextEdit):
"""Save the text currently in the editor to the NWDoc object, """Save the text currently in the editor to the NWDoc object,
and update the NWItem meta data. and update the NWItem meta data.
""" """
theItem = self.nwDocument.getCurrentItem() if self.nwItem is None or self.nwDocument is None:
if theItem is None: logger.error("Cannot save text as no document is open")
return False
tHandle = self.nwItem.itemHandle
if self.theHandle != tHandle:
logger.error("Editor handle %s and item handle %s do not match" % (
self.theHandle, tHandle
))
return False return False
docText = self.getText() docText = self.getText()
tHandle = theItem.itemHandle
cC, wC, pC = countWords(docText) cC, wC, pC = countWords(docText)
self._updateCounts(cC, wC, pC) self._updateCounts(cC, wC, pC)
theItem.setCharCount(self.charCount) self.nwItem.setCharCount(self.charCount)
theItem.setWordCount(self.wordCount) self.nwItem.setWordCount(self.wordCount)
theItem.setParaCount(self.paraCount) self.nwItem.setParaCount(self.paraCount)
self.saveCursorPosition() self.saveCursorPosition()
if not self.nwDocument.writeDocument(docText): if not self.nwDocument.writeDocument(docText):
@@ -472,7 +480,7 @@ class GuiDocEditor(QTextEdit):
# Update the status bar # Update the status bar
self.theParent.setStatus( self.theParent.setStatus(
self.tr("Saved Document: {0}").format(theItem.itemName) self.tr("Saved Document: {0}").format(self.nwItem.itemName)
) )
return True return True
@@ -599,10 +607,9 @@ class GuiDocEditor(QTextEdit):
def saveCursorPosition(self): def saveCursorPosition(self):
"""Save the cursor position to the current project item object. """Save the cursor position to the current project item object.
""" """
theItem = self.nwDocument.getCurrentItem() if self.nwItem is not None:
if theItem is not None:
cursPos = self.getCursorPosition() cursPos = self.getCursorPosition()
theItem.setCursorPos(cursPos) self.nwItem.setCursorPos(cursPos)
return return
def setCursorLine(self, theLine): def setCursorLine(self, theLine):
@@ -773,7 +780,7 @@ class GuiDocEditor(QTextEdit):
"""Tell the user where on the file system the file in the editor """Tell the user where on the file system the file in the editor
is saved. is saved.
""" """
if self.theHandle is None: if self.nwDocument is None:
logger.error("No document open") logger.error("No document open")
return False return False
@@ -1140,8 +1147,7 @@ class GuiDocEditor(QTextEdit):
def _updateCounts(self, cCount, wCount, pCount): def _updateCounts(self, cCount, wCount, pCount):
"""Slot for the word counter's finished signal """Slot for the word counter's finished signal
""" """
theItem = self.nwDocument.getCurrentItem() if self.theHandle is None or self.nwItem is None:
if self.theHandle is None or theItem is None:
return return
logger.verbose("Updating word count") logger.verbose("Updating word count")
@@ -1149,9 +1155,10 @@ class GuiDocEditor(QTextEdit):
self.charCount = cCount self.charCount = cCount
self.wordCount = wCount self.wordCount = wCount
self.paraCount = pCount self.paraCount = pCount
theItem.setCharCount(cCount)
theItem.setWordCount(wCount) self.nwItem.setCharCount(cCount)
theItem.setParaCount(pCount) self.nwItem.setWordCount(wCount)
self.nwItem.setParaCount(pCount)
self.theParent.treeView.propagateCount(self.theHandle, wCount) self.theParent.treeView.propagateCount(self.theHandle, wCount)
self.theParent.treeView.projectWordCount() self.theParent.treeView.projectWordCount()
+1 -1
View File
@@ -98,7 +98,7 @@ def testCoreDocument_LoadSave(monkeypatch, dummyGUI, nwMinimal):
assert theDoc.getError() == "OSError" assert theDoc.getError() == "OSError"
# Saving with no handle # Saving with no handle
theDoc.clearDocument() theDoc._docHandle = None
assert not theDoc.writeDocument(theText) assert not theDoc.writeDocument(theText)
# Delete the last document # Delete the last document