Add remaining logic to document splitter, and fix bug in recursive permanet delete

This commit is contained in:
Veronica Berglyd Olsen
2022-10-13 17:11:43 +02:00
parent 049190739f
commit 02976c5f12
2 changed files with 70 additions and 27 deletions
+37 -7
View File
@@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging import logging
from novelwriter.common import minmax
from novelwriter.core.document import NWDoc from novelwriter.core.document import NWDoc
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -131,6 +132,7 @@ class DocSplitter:
self._srcHandle = None self._srcHandle = None
self._srcItem = None self._srcItem = None
self._inFolder = False
self._rawData = [] self._rawData = []
srcItem = self.theProject.tree[sHandle] srcItem = self.theProject.tree[sHandle]
@@ -154,6 +156,7 @@ class DocSplitter:
new documents. new documents.
""" """
self._parHandle = pHandle self._parHandle = pHandle
self._inFolder = False
return return
def newParentFolder(self, pHandle, folderLabel): def newParentFolder(self, pHandle, folderLabel):
@@ -164,11 +167,12 @@ class DocSplitter:
return None return None
newHandle = self.theProject.newFolder(folderLabel, pHandle) newHandle = self.theProject.newFolder(folderLabel, pHandle)
newItem = self.theProject.tree[self._parHandle] newItem = self.theProject.tree[newHandle]
newItem.setStatus(self._srcItem.itemStatus) newItem.setStatus(self._srcItem.itemStatus)
newItem.setImport(self._srcItem.itemImport) newItem.setImport(self._srcItem.itemImport)
self._parHandle = newHandle self._parHandle = newHandle
self._inFolder = True
return newHandle return newHandle
@@ -184,23 +188,49 @@ class DocSplitter:
return True return True
def writeDocuments(self): def writeDocuments(self, docHierarchy):
"""An iterator that will write each document in the buffer, and """An iterator that will write each document in the buffer, and
return its new handle, parent handle, and sibling handle. return its new handle, parent handle, and sibling handle.
""" """
nearHandle = self._parHandle pHandle = self._parHandle
nHandle = self._parHandle if self._inFolder else self._srcHandle
hHandle = [self._parHandle, None, None, None, None]
pLevel = 0
for docText, hLevel, docLabel in self._rawData: for docText, hLevel, docLabel in self._rawData:
newHandle = self.theProject.newFile(docLabel, self._parHandle) hLevel = minmax(hLevel, 1, 4)
if pLevel == 0:
pLevel = hLevel
outDoc = NWDoc(self.theProject, newHandle) if docHierarchy:
if hLevel == 1:
pHandle = self._parHandle
elif hLevel == 2:
pHandle = hHandle[1] or hHandle[0]
elif hLevel == 3:
pHandle = hHandle[2] or hHandle[1] or hHandle[0]
elif hLevel == 4:
pHandle = hHandle[3] or hHandle[2] or hHandle[1] or hHandle[0]
if hLevel < pLevel:
nHandle = hHandle[hLevel] or hHandle[0]
elif hLevel > pLevel:
nHandle = pHandle
dHandle = self.theProject.newFile(docLabel, pHandle)
hHandle[hLevel] = dHandle
outDoc = NWDoc(self.theProject, dHandle)
status = outDoc.writeDocument("\n".join(docText)) status = outDoc.writeDocument("\n".join(docText))
if not status: if not status:
self._error = outDoc.getError() self._error = outDoc.getError()
yield status, newHandle, nearHandle yield status, dHandle, nHandle
nearHandle = newHandle hHandle[hLevel] = dHandle
nHandle = dHandle
pLevel = hLevel
return return
+33 -20
View File
@@ -517,18 +517,14 @@ class GuiProjectTree(QTreeWidget):
# Handle new file creation # Handle new file creation
if itemType == nwItemType.FILE and hLevel > 0: if itemType == nwItemType.FILE and hLevel > 0:
if self.theProject.writeNewFile(tHandle, hLevel, not isNote): self.theProject.writeNewFile(tHandle, hLevel, not isNote)
# If successful, update word count
wC = self.theProject.index.getCounts(tHandle)[1]
self.propagateCount(tHandle, wC)
self.projView.wordCountsChanged.emit()
# Add the new item to the project tree # Add the new item to the project tree
self.revealNewTreeItem(tHandle, nHandle) self.revealNewTreeItem(tHandle, nHandle=nHandle, wordCount=True)
return True return True
def revealNewTreeItem(self, tHandle, nHandle=None): def revealNewTreeItem(self, tHandle, nHandle=None, wordCount=False):
"""Reveal a newly added project item in the project tree. """Reveal a newly added project item in the project tree.
""" """
nwItem = self.theProject.tree[tHandle] nwItem = self.theProject.tree[tHandle]
@@ -539,6 +535,11 @@ class GuiProjectTree(QTreeWidget):
if trItem is None: if trItem is None:
return False return False
if nwItem.isFileType() and wordCount:
wC = self.theProject.index.getCounts(tHandle)[1]
self.propagateCount(tHandle, wC)
self.projView.wordCountsChanged.emit()
pHandle = nwItem.itemParent pHandle = nwItem.itemParent
if pHandle is not None and pHandle in self._treeMap: if pHandle is not None and pHandle in self._treeMap:
self._treeMap[pHandle].setExpanded(True) self._treeMap[pHandle].setExpanded(True)
@@ -802,17 +803,16 @@ class GuiProjectTree(QTreeWidget):
logger.debug("Permanently deleting item '%s'", tHandle) logger.debug("Permanently deleting item '%s'", tHandle)
self.propagateCount(tHandle, 0) self.propagateCount(tHandle, 0)
itemList = self.getTreeFromHandle(tHandle)
trItemP = trItemS.parent() trItemP = trItemS.parent()
tIndex = trItemP.indexOfChild(trItemS) tIndex = trItemP.indexOfChild(trItemS)
trItemP.takeChild(tIndex) trItemP.takeChild(tIndex)
for dHandle in reversed(itemList): for dHandle in reversed(self.getTreeFromHandle(tHandle)):
if self.mainGui.docEditor.docHandle() == dHandle: if self.mainGui.docEditor.docHandle() == dHandle:
self.mainGui.closeDocument() self.mainGui.closeDocument()
self.theProject.removeItem(tHandle) self.theProject.removeItem(dHandle)
self._treeMap.pop(tHandle, None) self._treeMap.pop(dHandle, None)
self._alertTreeChange(tHandle, flush=flush) self._alertTreeChange(tHandle, flush=flush)
self.projView.wordCountsChanged.emit() self.projView.wordCountsChanged.emit()
@@ -1433,14 +1433,14 @@ class GuiProjectTree(QTreeWidget):
if not docMerger.writeTargetDoc(): if not docMerger.writeTargetDoc():
self.mainGui.makeAlert([ self.mainGui.makeAlert([
self.tr("Could not save document."), docMerger.getError() self.tr("Could not write document content."), docMerger.getError()
], nwAlert.ERROR) ], nwAlert.ERROR)
return False return False
if newFile:
self.mainGui.projView.revealNewTreeItem(mHandle, tHandle)
self.theProject.index.reIndexHandle(mHandle) self.theProject.index.reIndexHandle(mHandle)
if newFile:
self.revealNewTreeItem(mHandle, nHandle=tHandle, wordCount=True)
self.mainGui.openDocument(mHandle, doScroll=True) self.mainGui.openDocument(mHandle, doScroll=True)
if mrgData.get("moveToTrash", False): if mrgData.get("moveToTrash", False):
@@ -1477,17 +1477,30 @@ class GuiProjectTree(QTreeWidget):
if dlgSplit.result() == QDialog.Accepted: if dlgSplit.result() == QDialog.Accepted:
splitData, splitText = dlgSplit.getData() splitData, splitText = dlgSplit.getData()
print(splitData)
headerList = splitData.get("headerList", []) headerList = splitData.get("headerList", [])
intoFolder = splitData.get("intoFolder", False)
docHierarchy = splitData.get("docHierarchy", False)
docSplit = DocSplitter(self.theProject, tHandle) docSplit = DocSplitter(self.theProject, tHandle)
docSplit.setParentItem(tItem.itemParent) if intoFolder:
docSplit.splitDocument(headerList, splitText) fHandle = docSplit.newParentFolder(tItem.itemParent, tItem.itemName)
self.revealNewTreeItem(fHandle, nHandle=tHandle)
self._alertTreeChange(fHandle, flush=False)
else:
docSplit.setParentItem(tItem.itemParent)
for writeOk, dHandle, nHandle in docSplit.writeDocuments(): docSplit.splitDocument(headerList, splitText)
self.mainGui.projView.revealNewTreeItem(dHandle, nHandle) for writeOk, dHandle, nHandle in docSplit.writeDocuments(docHierarchy):
self.theProject.index.reIndexHandle(dHandle)
self.revealNewTreeItem(dHandle, nHandle=nHandle, wordCount=True)
self._alertTreeChange(dHandle, flush=False) self._alertTreeChange(dHandle, flush=False)
if not writeOk:
self.mainGui.makeAlert([
self.tr("Could not write document content."), docSplit.getError()
], nwAlert.ERROR)
self.saveTreeOrder()
else: else:
logger.info("Action cancelled by user") logger.info("Action cancelled by user")