def updateContents(self): """Reload the view's content if the view is shown. Avoids update if view is not visible or has zero height or width. """ selNodes = self.selectModel.selectedNodes() if self.isChildView and (len(selNodes) != 1 or self.hideChildView or not selNodes[0].childList): self.hide() else: self.show() if not self.isVisible() or self.height() == 0 or self.width() == 0: return if not selNodes: self.setHtml('') return if self.isChildView: if self.showDescendants: outputGroup = treeoutput.OutputGroup(selNodes, False, True) if outputGroup.hasPrefixes(): outputGroup.combineAllSiblings() outputGroup.addBlanksBetween() outputGroup.addAbsoluteIndents() else: outputGroup = treeoutput.OutputGroup(selNodes[0].childList) outputGroup.addBlanksBetween() outputGroup.addSiblingPrefixes() else: outputGroup = treeoutput.OutputGroup(selNodes) outputGroup.addBlanksBetween() outputGroup.addSiblingPrefixes() self.setHtml('\n'.join(outputGroup.getLines())) self.setSearchPaths([globalref.mainControl.defaultFilePath(True)])
def getChildNodeOutput(self, node): """Return the formatted HTML text for the node children's output. Arguments: node -- the given parent node """ outputGroup = treeoutput.OutputGroup(node.childList) outputGroup.addBlanksBetween() outputGroup.addAbsoluteIndents() return '\n'.join(outputGroup.getLines())
def exportHtmlSingle(self, filePath=''): """Export to a single web page, use ExportDialog options. Prompt user for path if not given in argument. Return True on successful export. Arguments: filePath -- use if given, otherwise prompt user """ if not filePath: filePath = self.getFileName(_('TreeLine - Export HTML'), 'html') if not filePath: return False QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) if ExportDialog.exportWhat == ExportDialog.entireTree: self.selectedNodes = [self.rootNode] outputGroup = treeoutput.OutputGroup( self.selectedNodes, ExportDialog.includeRoot, ExportDialog.exportWhat != ExportDialog.selectNode, ExportDialog.openOnly, True) outputGroup.addBlanksBetween() outputGroup.addIndents() outputGroup.addSiblingPrefixes() outGroups = outputGroup.splitColumns(ExportDialog.numColumns) htmlTitle = os.path.splitext(os.path.basename(filePath))[0] indent = globalref.genOptions.getValue('IndentOffset') lines = [ '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 ' 'Transitional//EN">', '<html>', '<head>', '<meta http-equiv="Content-Type" content="text/html; ' 'charset=utf-8">', '<title>{0}</title>'.format(htmlTitle), '<style type="text/css"><!--', 'div {{margin-left: {0}em}}'.format(indent), 'td {padding: 10px}', 'tr {vertical-align: top}', '--></style>', '</head>', '<body>' ] if ExportDialog.addHeader: headerText = (globalref.mainControl.activeControl.printData. formatHeaderFooter(True)) if headerText: lines.append(headerText) lines.extend(['<table>', '<tr><td>']) lines.extend(outGroups[0].getLines()) for group in outGroups[1:]: lines.append('</td><td>') lines.extend(group.getLines()) lines.extend(['</td></tr>', '</table>']) if ExportDialog.addHeader: footerText = (globalref.mainControl.activeControl.printData. formatHeaderFooter(False)) if footerText: lines.append(footerText) lines.extend(['</body>', '</html>']) with open(filePath, 'w', encoding='utf-8') as f: f.writelines([(line + '\n') for line in lines]) return True
def setupData(self): """Load data to be printed and set page info. """ if self.printWhat == entireTree: selNodes = [self.localControl.model.root] else: selNodes = ( self.localControl.currentSelectionModel().selectedNodes()) self.outputGroup = treeoutput.OutputGroup(selNodes, self.includeRoot, self.printWhat != selectNode, self.openOnly) self.paginate()
def updateContents(self): """Reload the view's content if the view is shown. Avoids update if view is not visible or has zero height or width. """ selSpots = self.treeView.selectionModel().selectedSpots() if self.isChildView: if (len(selSpots) > 1 or self.hideChildView or (selSpots and not selSpots[0].nodeRef.childList)): self.hide() return if not selSpots: # use top node childList from tree structure selSpots = [ globalref.mainControl.activeControl.structure.structSpot() ] elif not selSpots: self.hide() return self.show() if not self.isVisible() or self.height() == 0 or self.width() == 0: return if self.isChildView: if self.showDescendants: outputGroup = treeoutput.OutputGroup(selSpots, False, True) if outputGroup.hasPrefixes(): outputGroup.combineAllSiblings() outputGroup.addBlanksBetween() outputGroup.addAbsoluteIndents() else: outputGroup = treeoutput.OutputGroup(selSpots[0].childSpots()) outputGroup.addBlanksBetween() outputGroup.addSiblingPrefixes() else: outputGroup = treeoutput.OutputGroup(selSpots) outputGroup.addBlanksBetween() outputGroup.addSiblingPrefixes() self.setHtml('\n'.join(outputGroup.getLines())) self.setSearchPaths([str(globalref.mainControl.defaultPathObj(True))])
def setupData(self): """Load data to be printed and set page info. """ if self.printWhat == PrintScope.entireTree: selSpots = self.localControl.structure.rootSpots() else: selSpots = ( self.localControl.currentSelectionModel().selectedSpots()) if not selSpots: selSpots = self.localControl.structure.rootSpots() self.outputGroup = treeoutput.OutputGroup( selSpots, self.includeRoot, self.printWhat != PrintScope.selectNode, self.openOnly) self.paginate()
def exportHtmlNavSingle(self, filePath=''): """Export single web page with a navigation pane, ExportDialog options. Prompt user for path if not given in argument. Return True on successful export. Arguments: filePath -- use if given, otherwise prompt user """ if not filePath: filePath = self.getFileName(_('TreeLine - Export HTML'), 'html') if not filePath: return False QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) if ExportDialog.exportWhat == ExportDialog.entireTree: self.selectedNodes = [self.rootNode] outputGroup = treeoutput.OutputGroup(self.selectedNodes, ExportDialog.includeRoot, True, ExportDialog.openOnly, True, ExportDialog.navPaneLevels) outputGroup.addBlanksBetween() outputGroup.addIndents() outputGroup.addSiblingPrefixes() htmlTitle = os.path.splitext(os.path.basename(filePath))[0] indent = globalref.genOptions.getValue('IndentOffset') lines = [ '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 ' 'Transitional//EN">', '<html>', '<head>', '<meta http-equiv="Content-Type" content="text/html; ' 'charset=utf-8">', '<title>{0}</title>'.format(htmlTitle), '<style type="text/css"><!--', ' #sidebar {', ' width: 16em;', ' float: left;', ' border-right: 1px solid black;', ' }', ' #sidebar div {{margin-left: {0}em;}}'.format(indent), ' #content {', ' margin-left: 16em;', ' border-left: 1px solid black;', ' padding-left: 6px;', ' }', ' #content div {{margin-left: {0}em;}}'.format(indent), '--></style>', '</head>', '<body>', '<div id="sidebar">' ] prevLevel = 0 for parent in self.selectedNodes: for node, level in parent.levelDescendantGen( ExportDialog.includeRoot, ExportDialog.navPaneLevels, ExportDialog.openOnly): if level > prevLevel: lines.append('<div>') while level < prevLevel: lines.append('</div>') prevLevel -= 1 lines.append('• <a href="#{0}">{1}</a><br />'.format( node.uniqueId, node.title())) prevLevel = level while level > 0: lines.append('</div>') level -= 1 lines.extend(['</div>', '<div id="content">']) if ExportDialog.addHeader: headerText = (globalref.mainControl.activeControl.printData. formatHeaderFooter(True)) if headerText: lines.append(headerText) lines.extend(outputGroup.getLines()) if ExportDialog.addHeader: footerText = (globalref.mainControl.activeControl.printData. formatHeaderFooter(False)) if footerText: lines.append(footerText) lines.extend(['</div>', '</body>', '</html>']) with open(filePath, 'w', encoding='utf-8') as f: f.writelines([(line + '\n') for line in lines]) return True
def paginate(self): """Define the pages and locations of output items and set page range. """ pageNum = 1 columnNum = 0 pagePos = 0 itemSplit = False self.checkPageLayout() heightAvail = (self.pageLayout.paintRect().height() * self.printer.logicalDpiY()) columnSpacing = int(self.columnSpacing * self.printer.logicalDpiX()) widthAvail = ( (self.pageLayout.paintRect().width() * self.printer.logicalDpiX() - columnSpacing * (self.numColumns - 1)) // self.numColumns) newGroup = treeoutput.OutputGroup([]) while self.outputGroup: item = self.outputGroup.pop(0) widthRemain = widthAvail - item.level * self.indentSize if pagePos != 0 and (newGroup[-1].addSpace or item.addSpace): pagePos += self.lineSpacing if item.siblingPrefix: siblings = treeoutput.OutputGroup([]) siblings.append(item) while True: item = siblings.combineLines() item.setDocHeight(self.printer, widthRemain, self.mainFont, True) if pagePos + item.height > heightAvail: self.outputGroup.insert(0, siblings.pop()) item = (siblings.combineLines() if siblings else None) break if (self.outputGroup and item.level == self.outputGroup[0].level and item.equalPrefix(self.outputGroup[0])): siblings.append(self.outputGroup.pop(0)) else: break if item: item.setDocHeight(self.printer, widthRemain, self.mainFont, True) if item.height > heightAvail and not itemSplit: item, newItem = item.splitDocHeight( heightAvail - pagePos, heightAvail, self.printer, widthRemain, self.mainFont) if newItem: self.outputGroup.insert(0, newItem) itemSplit = True if item and (pagePos + item.height <= heightAvail or pagePos == 0): item.pageNum = pageNum item.columnNum = columnNum item.pagePos = pagePos newGroup.append(item) pagePos += item.height else: if columnNum + 1 < self.numColumns: columnNum += 1 else: pageNum += 1 columnNum = 0 pagePos = 0 itemSplit = False if item: self.outputGroup.insert(0, item) if self.widowControl and not item.siblingPrefix: moveItems = [] moveHeight = 0 level = item.level while (newGroup and not newGroup[-1].siblingPrefix and newGroup[-1].level == level - 1 and ((newGroup[-1].pageNum == pageNum - 1 and newGroup[-1].columnNum == columnNum) or (newGroup[-1].pageNum == pageNum and newGroup[-1].columnNum == columnNum - 1))): moveItems.insert(0, newGroup.pop()) moveHeight += moveItems[0].height level -= 1 if (moveItems and newGroup and moveHeight < (heightAvail // 5)): self.outputGroup[0:0] = moveItems else: newGroup.extend(moveItems) self.outputGroup = newGroup self.outputGroup.loadFamilyRefs() self.printer.setFromTo(1, pageNum)