Merging a folder of documents into one file now works
This commit is contained in:
+68
-10
@@ -16,11 +16,11 @@ import nw
|
|||||||
from PyQt5.QtCore import Qt
|
from PyQt5.QtCore import Qt
|
||||||
from PyQt5.QtWidgets import (
|
from PyQt5.QtWidgets import (
|
||||||
QDialog, QHBoxLayout, QVBoxLayout, QGridLayout, QPushButton, QLabel,
|
QDialog, QHBoxLayout, QVBoxLayout, QGridLayout, QPushButton, QLabel,
|
||||||
QProgressBar
|
QProgressBar, QListWidget, QAbstractItemView, QListWidgetItem
|
||||||
)
|
)
|
||||||
|
from nw.constants import nwFiles, nwAlert, nwItemClass, nwItemType
|
||||||
|
from nw.project import NWDoc
|
||||||
from nw.tools import OptLastState
|
from nw.tools import OptLastState
|
||||||
from nw.constants import nwFiles
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -34,6 +34,7 @@ class GuiDocMerge(QDialog):
|
|||||||
self.mainConf = nw.CONFIG
|
self.mainConf = nw.CONFIG
|
||||||
self.theParent = theParent
|
self.theParent = theParent
|
||||||
self.theProject = theProject
|
self.theProject = theProject
|
||||||
|
self.sourceItem = None
|
||||||
self.optState = DocMergeLastState(self.theProject,nwFiles.MERGE_OPT)
|
self.optState = DocMergeLastState(self.theProject,nwFiles.MERGE_OPT)
|
||||||
self.optState.loadSettings()
|
self.optState.loadSettings()
|
||||||
|
|
||||||
@@ -50,25 +51,26 @@ class GuiDocMerge(QDialog):
|
|||||||
self.doMergeForm = QGridLayout()
|
self.doMergeForm = QGridLayout()
|
||||||
self.doMergeForm.setContentsMargins(10,5,0,10)
|
self.doMergeForm.setContentsMargins(10,5,0,10)
|
||||||
|
|
||||||
|
self.listBox = QListWidget()
|
||||||
|
self.listBox.setDragDropMode(QAbstractItemView.InternalMove)
|
||||||
|
|
||||||
self.mergeButton = QPushButton("Merge")
|
self.mergeButton = QPushButton("Merge")
|
||||||
self.mergeButton.clicked.connect(self._doMerge)
|
self.mergeButton.clicked.connect(self._doMerge)
|
||||||
|
|
||||||
self.closeButton = QPushButton("Close")
|
self.closeButton = QPushButton("Close")
|
||||||
self.closeButton.clicked.connect(self._doClose)
|
self.closeButton.clicked.connect(self._doClose)
|
||||||
|
|
||||||
self.mergeStatus = QLabel("Ready ...")
|
self.doMergeForm.addWidget(self.listBox, 0, 0, 1, 3)
|
||||||
self.mergeProgress = QProgressBar(self)
|
self.doMergeForm.addWidget(self.mergeButton, 1, 1)
|
||||||
|
self.doMergeForm.addWidget(self.closeButton, 1, 2)
|
||||||
self.doMergeForm.addWidget(self.mergeStatus, 0, 0, 1, 3)
|
|
||||||
self.doMergeForm.addWidget(self.mergeProgress, 1, 0)
|
|
||||||
self.doMergeForm.addWidget(self.mergeButton, 1, 1)
|
|
||||||
self.doMergeForm.addWidget(self.closeButton, 1, 2)
|
|
||||||
|
|
||||||
self.innerBox.addLayout(self.doMergeForm)
|
self.innerBox.addLayout(self.doMergeForm)
|
||||||
|
|
||||||
self.rejected.connect(self._doClose)
|
self.rejected.connect(self._doClose)
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
|
self._populateList()
|
||||||
|
|
||||||
logger.debug("GuiDocMerge initialisation complete")
|
logger.debug("GuiDocMerge initialisation complete")
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -81,6 +83,31 @@ class GuiDocMerge(QDialog):
|
|||||||
|
|
||||||
logger.verbose("GuiDocMerge merge button clicked")
|
logger.verbose("GuiDocMerge merge button clicked")
|
||||||
|
|
||||||
|
finalOrder = []
|
||||||
|
for i in range(self.listBox.count()):
|
||||||
|
finalOrder.append(self.listBox.item(i).data(Qt.UserRole))
|
||||||
|
|
||||||
|
theDoc = NWDoc(self.theProject, self.theParent)
|
||||||
|
theText = ""
|
||||||
|
for tHandle in finalOrder:
|
||||||
|
theText += theDoc.openDocument(tHandle, False).rstrip()
|
||||||
|
theText += "\n\n"
|
||||||
|
|
||||||
|
if self.sourceItem is None:
|
||||||
|
self.theParent.makeAlert((
|
||||||
|
"Cannot parse source item."
|
||||||
|
), nwAlert.ERROR)
|
||||||
|
return
|
||||||
|
|
||||||
|
srcItem = self.theProject.getItem(self.sourceItem)
|
||||||
|
nHandle = self.theProject.newFile(srcItem.itemName, srcItem.itemClass, srcItem.parHandle)
|
||||||
|
self.theParent.treeView.revealTreeItem(nHandle)
|
||||||
|
theDoc.openDocument(nHandle, False)
|
||||||
|
theDoc.saveDocument(theText)
|
||||||
|
self.theParent.openDocument(nHandle)
|
||||||
|
|
||||||
|
self.close()
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def _doClose(self):
|
def _doClose(self):
|
||||||
@@ -92,6 +119,37 @@ class GuiDocMerge(QDialog):
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
##
|
||||||
|
# Internal Functions
|
||||||
|
##
|
||||||
|
|
||||||
|
def _populateList(self):
|
||||||
|
|
||||||
|
tHandle = self.theParent.treeView.getSelectedHandle()
|
||||||
|
self.sourceItem = tHandle
|
||||||
|
if tHandle is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
nwItem = self.theProject.getItem(tHandle)
|
||||||
|
if nwItem is None:
|
||||||
|
return
|
||||||
|
if nwItem.itemType is not nwItemType.FOLDER:
|
||||||
|
self.theParent.makeAlert((
|
||||||
|
"Element selected in the project tree must be a folder."
|
||||||
|
), nwAlert.ERROR)
|
||||||
|
return
|
||||||
|
|
||||||
|
for sHandle in self.theParent.treeView.getTreeFromHandle(tHandle):
|
||||||
|
newItem = QListWidgetItem()
|
||||||
|
nwItem = self.theProject.getItem(sHandle)
|
||||||
|
if nwItem.itemType is not nwItemType.FILE:
|
||||||
|
continue
|
||||||
|
newItem.setText(nwItem.itemName)
|
||||||
|
newItem.setData(Qt.UserRole, sHandle)
|
||||||
|
self.listBox.addItem(newItem)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
# END Class GuiDocMerge
|
# END Class GuiDocMerge
|
||||||
|
|
||||||
class DocMergeLastState(OptLastState):
|
class DocMergeLastState(OptLastState):
|
||||||
|
|||||||
@@ -171,14 +171,21 @@ class GuiDocTree(QTreeWidget):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
# Add the new item to the tree
|
# Add the new item to the tree
|
||||||
nwItem = self.theProject.getItem(tHandle)
|
self.revealTreeItem(tHandle)
|
||||||
trItem = self._addTreeItem(nwItem)
|
self.theParent.editItem()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def revealTreeItem(self, tHandle):
|
||||||
|
"""Reveal a newly added project item in the project tree.
|
||||||
|
"""
|
||||||
|
nwItem = self.theProject.getItem(tHandle)
|
||||||
|
trItem = self._addTreeItem(nwItem)
|
||||||
|
pHandle = nwItem.parHandle
|
||||||
if pHandle is not None and pHandle in self.theMap.keys():
|
if pHandle is not None and pHandle in self.theMap.keys():
|
||||||
self.theMap[pHandle].setExpanded(True)
|
self.theMap[pHandle].setExpanded(True)
|
||||||
self.clearSelection()
|
self.clearSelection()
|
||||||
trItem.setSelected(True)
|
trItem.setSelected(True)
|
||||||
self.theParent.editItem()
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def moveTreeItem(self, nStep):
|
def moveTreeItem(self, nStep):
|
||||||
@@ -221,6 +228,16 @@ class GuiDocTree(QTreeWidget):
|
|||||||
self.theProject.setTreeOrder(theList)
|
self.theProject.setTreeOrder(theList)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def getTreeFromHandle(self, tHandle):
|
||||||
|
"""Recursively return all the children items starting from a
|
||||||
|
given item handle.
|
||||||
|
"""
|
||||||
|
theList = []
|
||||||
|
theItem = self._getTreeItem(tHandle)
|
||||||
|
if theItem is not None:
|
||||||
|
theList = self._scanChildren(theList, theItem, 0)
|
||||||
|
return theList
|
||||||
|
|
||||||
def getColumnSizes(self):
|
def getColumnSizes(self):
|
||||||
retVals = [
|
retVals = [
|
||||||
self.columnWidth(0),
|
self.columnWidth(0),
|
||||||
|
|||||||
Reference in New Issue
Block a user