Implement nre doc merge functionality in tree
This commit is contained in:
@@ -25,60 +25,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
|
||||
from novelwriter.core.document import NWDoc
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# logger.verbose("GuiDocMerge merge button clicked")
|
||||
|
||||
# finalOrder = []
|
||||
# for i in range(self.listBox.count()):
|
||||
# finalOrder.append(self.listBox.item(i).data(Qt.UserRole))
|
||||
|
||||
# if len(finalOrder) == 0:
|
||||
# self.mainGui.makeAlert(self.tr(
|
||||
# "No source documents found. Nothing to do."
|
||||
# ), nwAlert.ERROR)
|
||||
# return False
|
||||
|
||||
# theText = ""
|
||||
# for tHandle in finalOrder:
|
||||
# inDoc = NWDoc(self.theProject, tHandle)
|
||||
# docText = inDoc.readDocument()
|
||||
# docErr = inDoc.getError()
|
||||
# if docText is None and docErr:
|
||||
# self.mainGui.makeAlert([
|
||||
# self.tr("Failed to open document file."), docErr
|
||||
# ], nwAlert.ERROR)
|
||||
# if docText:
|
||||
# theText += docText.rstrip("\n")+"\n\n"
|
||||
|
||||
# if self.sourceItem is None:
|
||||
# self.mainGui.makeAlert(self.tr(
|
||||
# "No source folder selected. Nothing to do."
|
||||
# ), nwAlert.ERROR)
|
||||
# return False
|
||||
|
||||
# srcItem = self.theProject.tree[self.sourceItem]
|
||||
# if srcItem is None:
|
||||
# self.mainGui.makeAlert(self.tr("Internal error."), nwAlert.ERROR)
|
||||
# return False
|
||||
|
||||
# nHandle = self.theProject.newFile(srcItem.itemName, srcItem.itemParent)
|
||||
# newItem = self.theProject.tree[nHandle]
|
||||
# newItem.setStatus(srcItem.itemStatus)
|
||||
# newItem.setImport(srcItem.itemImport)
|
||||
|
||||
# outDoc = NWDoc(self.theProject, nHandle)
|
||||
# if not outDoc.writeDocument(theText):
|
||||
# self.mainGui.makeAlert([
|
||||
# self.tr("Could not save document."), outDoc.getError()
|
||||
# ], nwAlert.ERROR)
|
||||
# return False
|
||||
|
||||
# self.mainGui.projView.revealNewTreeItem(nHandle)
|
||||
# self.mainGui.openDocument(nHandle, doScroll=True)
|
||||
|
||||
# self._doClose()
|
||||
|
||||
|
||||
class DocMerger:
|
||||
|
||||
@@ -86,17 +36,84 @@ class DocMerger:
|
||||
|
||||
self.theProject = theProject
|
||||
|
||||
self._error = ""
|
||||
self._targetDoc = None
|
||||
self._targetText = []
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
# Methods
|
||||
##
|
||||
|
||||
def getError(self):
|
||||
"""Return any collected errors.
|
||||
"""
|
||||
return self._error
|
||||
|
||||
def setTargetDoc(self, tHandle):
|
||||
"""Set the target document for the merging. Calling this
|
||||
function resets the class.
|
||||
"""
|
||||
self._targetDoc = tHandle
|
||||
self._targetText = []
|
||||
return
|
||||
|
||||
def createNewDoc(self, docLabel, pHandle, itemLayout):
|
||||
return
|
||||
def newTargetDoc(self, srcHandle, docLabel):
|
||||
"""Create a barnd new target document based on a source handle
|
||||
and a new doc label. Calling this function resets the class.
|
||||
"""
|
||||
srcItem = self.theProject.tree[srcHandle]
|
||||
if srcItem is None:
|
||||
return None
|
||||
|
||||
def appendDoc(self, tHandle):
|
||||
return
|
||||
newHandle = self.theProject.newFile(docLabel, srcItem.itemParent)
|
||||
newItem = self.theProject.tree[newHandle]
|
||||
newItem.setLayout(srcItem.itemLayout)
|
||||
newItem.setStatus(srcItem.itemStatus)
|
||||
newItem.setImport(srcItem.itemImport)
|
||||
|
||||
self._targetDoc = newHandle
|
||||
self._targetText = []
|
||||
|
||||
return newHandle
|
||||
|
||||
def appendText(self, srcHandle, addComment, cmtPrefix):
|
||||
"""Append text from an existing document to the text buffer.
|
||||
"""
|
||||
srcItem = self.theProject.tree[srcHandle]
|
||||
if srcItem is None:
|
||||
return False
|
||||
|
||||
inDoc = NWDoc(self.theProject, srcHandle)
|
||||
docText = (inDoc.readDocument() or "").rstrip("\n")
|
||||
|
||||
if addComment:
|
||||
docInfo = srcItem.describeMe("H0")
|
||||
docSt, _ = srcItem.getImportStatus(incIcon=False)
|
||||
cmtLine = f"% {cmtPrefix} {docInfo}: {srcItem.itemName} [{docSt}]\n\n"
|
||||
docText = cmtLine + docText
|
||||
|
||||
self._targetText.append(docText)
|
||||
|
||||
return True
|
||||
|
||||
def writeTargetDoc(self):
|
||||
"""Write the accumulated text into the designated target
|
||||
document, appending any existing text.
|
||||
"""
|
||||
if self._targetDoc is None:
|
||||
return False
|
||||
|
||||
outDoc = NWDoc(self.theProject, self._targetDoc)
|
||||
docText = (outDoc.readDocument() or "").rstrip("\n")
|
||||
if docText:
|
||||
self._targetText.insert(0, docText)
|
||||
|
||||
status = outDoc.writeDocument("\n\n".join(self._targetText))
|
||||
if not status:
|
||||
self._error = outDoc.getError()
|
||||
|
||||
return status
|
||||
|
||||
# END Class DocMerger
|
||||
|
||||
@@ -292,16 +292,16 @@ class NWItem():
|
||||
|
||||
return trConst(nwLabels.ITEM_DESCRIPTION.get(descKey, ""))
|
||||
|
||||
def getImportStatus(self):
|
||||
def getImportStatus(self, incIcon=True):
|
||||
"""Return the relevant importance or status label and icon for
|
||||
the current item based on its class.
|
||||
"""
|
||||
if self.isNovelLike():
|
||||
stName = self.theProject.statusItems.name(self._status)
|
||||
stIcon = self.theProject.statusItems.icon(self._status)
|
||||
stIcon = self.theProject.statusItems.icon(self._status) if incIcon else None
|
||||
else:
|
||||
stName = self.theProject.importItems.name(self._import)
|
||||
stIcon = self.theProject.importItems.icon(self._import)
|
||||
stIcon = self.theProject.importItems.icon(self._import) if incIcon else None
|
||||
return stName, stIcon
|
||||
|
||||
##
|
||||
|
||||
Reference in New Issue
Block a user