def pasteChild(self, treeStruct, treeView): """Paste child nodes from the clipbaord. Return True on success. Arguments: treeStruct -- a ref to the existing tree structure treeView -- a ref to the tree view for expanding nodes """ mimeData = QApplication.clipboard().mimeData() parentNodes = self.relatedNodes() if not parentNodes: parentNodes = [treeStruct] undoObj = undo.ChildListUndo(treeStruct.undoList, parentNodes, treeFormats=treeStruct.treeFormats) for parent in parentNodes: newStruct = treestructure.structFromMimeData(mimeData) if not newStruct: treeStruct.undoList.removeLastUndo(undoObj) return False newStruct.replaceDuplicateIds(treeStruct.nodeDict) treeStruct.addNodesFromStruct(newStruct, parent) for spot in self: treeView.expandSpot(spot) return True
def pasteSibling(self, treeStruct, insertBefore=True): """Paste a sibling at the these spots. Return True on success. Arguments: treeStruct -- a ref to the existing tree structure insertBefore -- if True, insert before these nodes, o/w after """ mimeData = QApplication.clipboard().mimeData() parentNodes = [spot.parentSpot.nodeRef for spot in self] undoObj = undo.ChildListUndo(treeStruct.undoList, parentNodes, treeFormats=treeStruct.treeFormats) for spot in self: newStruct = treestructure.structFromMimeData(mimeData) if not newStruct: treeStruct.undoList.removeLastUndo(undoObj) return False newStruct.replaceDuplicateIds(treeStruct.nodeDict) parent = spot.parentSpot.nodeRef pos = parent.childList.index(spot.nodeRef) if not insertBefore: pos += 1 treeStruct.addNodesFromStruct(newStruct, parent, pos) return True
def insertSibling(self, treeStruct, insertBefore=True): """Insert a new sibling node at these nodes. Return the new spots. Arguments: treeStruct -- a ref to the existing tree structure insertBefore -- if True, insert before these nodes, o/w after """ undo.ChildListUndo(treeStruct.undoList, [spot.parentSpot.nodeRef for spot in self]) newSpots = [] for spot in self: newNode = spot.parentSpot.nodeRef.addNewChild( treeStruct, spot.nodeRef, insertBefore) newSpots.append(newNode.matchedSpot(spot.parentSpot)) return newSpots
def move(self, treeStruct, up=True): """Move these spots up or down by one item. Arguments: treeStruct -- a ref to the existing tree structure up -- if True move up, o/w down """ undo.ChildListUndo(treeStruct.undoList, [spot.parentSpot.nodeRef for spot in self]) if not up: self.reverse() for spot in self: parent = spot.parentSpot.nodeRef pos = parent.childList.index(spot.nodeRef) del parent.childList[pos] pos = pos - 1 if up else pos + 1 parent.childList.insert(pos, spot.nodeRef)
def moveToEnd(self, treeStruct, first=True): """Move these spots to the first or last position. Arguments: treeStruct -- a ref to the existing tree structure first -- if True move to first position, o/w last """ undo.ChildListUndo(treeStruct.undoList, [spot.parentSpot.nodeRef for spot in self]) if first: self.reverse() for spot in self: parent = spot.parentSpot.nodeRef parent.childList.remove(spot.nodeRef) if first: parent.childList.insert(0, spot.nodeRef) else: parent.childList.append(spot.nodeRef)
def dropMimeData(self, mimeData, dropAction, row, column, index): """Decode mime data and add as a child node to the given index. Return True if successful. Arguments: mimeData -- data for the node branch to be added dropAction -- a drop type enum value row -- a row number for the drop location column -- the column number for the drop location (normally 0) index -- the index of the parent node for the drop """ parent = (index.internalPointer().nodeRef if index.internalPointer() else self.treeStructure) isMove = (dropAction == Qt.MoveAction and TreeModel.storedDragModel == self) undoParents = [parent] if isMove: moveParents = { spot.parentSpot.nodeRef for spot in TreeModel.storedDragSpots } undoParents.extend(list(moveParents)) newStruct = treestructure.structFromMimeData(mimeData) # check for valid structure and no circular clone ref and not siblings: if newStruct and ( not isMove or (not parent.uId in newStruct.nodeDict and (row >= 0 or {node.uId for node in parent.childList}.isdisjoint( {node.uId for node in newStruct.childList})))): undo.ChildListUndo(self.treeStructure.undoList, undoParents, treeFormats=self.treeStructure.treeFormats) if isMove: for spot in TreeModel.storedDragSpots: self.treeStructure.deleteNodeSpot(spot) newStruct.replaceClonedBranches(self.treeStructure) else: newStruct.replaceDuplicateIds(self.treeStructure.nodeDict) self.treeStructure.addNodesFromStruct(newStruct, parent, row) return True return False
def indent(self, treeStruct): """Indent these spots. Makes them children of their previous siblings. Return the new spots. Arguments: treeStruct -- a ref to the existing tree structure """ undoSpots = ([spot.parentSpot for spot in self] + [spot.prevSiblingSpot() for spot in self]) undo.ChildListUndo(treeStruct.undoList, [spot.nodeRef for spot in undoSpots]) newSpots = [] for spot in self: node = spot.nodeRef newParentSpot = spot.prevSiblingSpot() node.changeParent(spot.parentSpot, newParentSpot) newSpots.append(node.matchedSpot(newParentSpot)) return newSpots
def addChild(self, treeStruct, treeView): """Add new child to these spots. Return the new spots. Arguments: treeStruct -- a ref to the existing tree structure treeView -- a ref to the tree view for expanding nodes """ selSpots = self if not selSpots: selSpots = list(treeStruct.spotRefs) undo.ChildListUndo(treeStruct.undoList, [spot.nodeRef for spot in selSpots]) newSpots = [] for spot in selSpots: newNode = spot.nodeRef.addNewChild(treeStruct) newSpots.append(newNode.matchedSpot(spot)) if spot.parentSpot: # can't expand root struct spot treeView.expandSpot(spot) return newSpots
def delete(self, treeStruct): """Delete these spots, return a new spot to select. Arguments: treeStruct -- a ref to the existing tree structure """ # gather next selected node in decreasing order of desirability nextSel = [spot.nextSiblingSpot() for spot in self] nextSel.extend([spot.prevSiblingSpot() for spot in self]) nextSel.extend([spot.parentSpot for spot in self]) while (not nextSel[0] or not nextSel[0].parentSpot or nextSel[0] in self): del nextSel[0] spotSet = set(self) branchSpots = [ spot for spot in self if spot.parentSpotSet().isdisjoint(spotSet) ] undoParents = {spot.parentSpot.nodeRef for spot in branchSpots} undo.ChildListUndo(treeStruct.undoList, list(undoParents)) for spot in branchSpots: treeStruct.deleteNodeSpot(spot) return nextSel[0]
def unindent(self, treeStruct): """Unindent these spots. Makes them their parent's next sibling. Return the new spots. Arguments: treeStruct -- a ref to the existing tree structure """ undoSpots = [spot.parentSpot for spot in self] undoSpots.extend([spot.parentSpot for spot in undoSpots]) undo.ChildListUndo(treeStruct.undoList, [spot.nodeRef for spot in undoSpots]) newSpots = [] for spot in reversed(self): node = spot.nodeRef oldParentSpot = spot.parentSpot newParentSpot = oldParentSpot.parentSpot pos = ( newParentSpot.nodeRef.childList.index(oldParentSpot.nodeRef) + 1) node.changeParent(oldParentSpot, newParentSpot, pos) newSpots.append(node.matchedSpot(newParentSpot)) return newSpots
def pasteCloneSibling(self, treeStruct, insertBefore=True): """Paste sibling clones at the these spots. Return True on success. Arguments: treeStruct -- a ref to the existing tree structure insertBefore -- if True, insert before these nodes, o/w after """ mimeData = QApplication.clipboard().mimeData() newStruct = treestructure.structFromMimeData(mimeData) if not newStruct: return False try: existNodes = [ treeStruct.nodeDict[node.uId] for node in newStruct.childList ] except KeyError: return False # nodes copied from other file parentNodes = [spot.parentSpot.nodeRef for spot in self] for parent in parentNodes: if not parent.ancestors().isdisjoint(set(existNodes)): return False # circular ref for node in existNodes: if parent in node.parents(): return False # identical siblings undoObj = undo.ChildListUndo(treeStruct.undoList, parentNodes, treeFormats=treeStruct.treeFormats) for spot in self: parent = spot.parentSpot.nodeRef pos = parent.childList.index(spot.nodeRef) if not insertBefore: pos += 1 for node in existNodes: parent.childList.insert(pos, node) node.addSpotRef(parent) return True
def pasteCloneChild(self, treeStruct, treeView): """Paste child clones from the clipbaord. Return True on success. Arguments: treeStruct -- a ref to the existing tree structure treeView -- a ref to the tree view for expanding nodes """ mimeData = QApplication.clipboard().mimeData() newStruct = treestructure.structFromMimeData(mimeData) if not newStruct: return False try: existNodes = [ treeStruct.nodeDict[node.uId] for node in newStruct.childList ] except KeyError: return False # nodes copied from other file parentNodes = self.relatedNodes() if not parentNodes: parentNodes = [treeStruct] for parent in parentNodes: if not parent.ancestors().isdisjoint(set(existNodes)): return False # circular ref for node in existNodes: if parent in node.parents(): return False # identical siblings undoObj = undo.ChildListUndo(treeStruct.undoList, parentNodes, treeFormats=treeStruct.treeFormats) for parent in parentNodes: for node in existNodes: parent.childList.append(node) node.addSpotRef(parent) for spot in self: treeView.expandSpot(spot) return True