diff --git a/nw/gui/dialogs/docmerge.py b/nw/gui/dialogs/docmerge.py index 264fc703..503eba44 100644 --- a/nw/gui/dialogs/docmerge.py +++ b/nw/gui/dialogs/docmerge.py @@ -16,11 +16,11 @@ import nw from PyQt5.QtCore import Qt from PyQt5.QtWidgets import ( 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.constants import nwFiles logger = logging.getLogger(__name__) @@ -34,6 +34,7 @@ class GuiDocMerge(QDialog): self.mainConf = nw.CONFIG self.theParent = theParent self.theProject = theProject + self.sourceItem = None self.optState = DocMergeLastState(self.theProject,nwFiles.MERGE_OPT) self.optState.loadSettings() @@ -50,25 +51,26 @@ class GuiDocMerge(QDialog): self.doMergeForm = QGridLayout() self.doMergeForm.setContentsMargins(10,5,0,10) + self.listBox = QListWidget() + self.listBox.setDragDropMode(QAbstractItemView.InternalMove) + self.mergeButton = QPushButton("Merge") self.mergeButton.clicked.connect(self._doMerge) self.closeButton = QPushButton("Close") self.closeButton.clicked.connect(self._doClose) - self.mergeStatus = QLabel("Ready ...") - self.mergeProgress = QProgressBar(self) - - 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.doMergeForm.addWidget(self.listBox, 0, 0, 1, 3) + self.doMergeForm.addWidget(self.mergeButton, 1, 1) + self.doMergeForm.addWidget(self.closeButton, 1, 2) self.innerBox.addLayout(self.doMergeForm) self.rejected.connect(self._doClose) self.show() + self._populateList() + logger.debug("GuiDocMerge initialisation complete") return @@ -81,6 +83,31 @@ class GuiDocMerge(QDialog): 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 def _doClose(self): @@ -92,6 +119,37 @@ class GuiDocMerge(QDialog): 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 class DocMergeLastState(OptLastState): diff --git a/nw/gui/elements/doctree.py b/nw/gui/elements/doctree.py index d5b9fb4f..63a998dd 100644 --- a/nw/gui/elements/doctree.py +++ b/nw/gui/elements/doctree.py @@ -171,14 +171,21 @@ class GuiDocTree(QTreeWidget): return False # Add the new item to the tree - nwItem = self.theProject.getItem(tHandle) - trItem = self._addTreeItem(nwItem) + self.revealTreeItem(tHandle) + 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(): self.theMap[pHandle].setExpanded(True) self.clearSelection() trItem.setSelected(True) - self.theParent.editItem() - return True def moveTreeItem(self, nStep): @@ -221,6 +228,16 @@ class GuiDocTree(QTreeWidget): self.theProject.setTreeOrder(theList) 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): retVals = [ self.columnWidth(0),