def show_tree(t, layout=None, tree_style=None): """ Interactively shows a tree.""" scene, img = init_scene(t, layout, tree_style) tree_item, n2i, n2f = render(t, img) scene.init_data(t, img, n2i, n2f) tree_item.setParentItem(scene.master_item) scene.addItem(scene.master_item) size = tree_item.rect() w, h = size.width(), size.height() svg = QtSvg.QSvgGenerator() svg.setFileName("test.svg") svg.setSize(QtCore.QSize(w, h)) svg.setViewBox(size) pp = QtGui.QPainter() pp.begin(svg) #pp.setRenderHint(QtGui.QPainter.Antialiasing) #pp.setRenderHint(QtGui.QPainter.TextAntialiasing) #pp.setRenderHint(QtGui.QPainter.SmoothPixmapTransform) scene.render(pp, tree_item.rect(), tree_item.rect(), QtCore.Qt.KeepAspectRatio) pp.end() img = QtSvg.QGraphicsSvgItem("test.svg") #img.setParentItem(scene.master_item) #scene.removeItem(tree_item) #tree_item.setVisible(False) mainapp = _GUI(scene) mainapp.show() _QApp.exec_()
def plotTree(self, tree, out_fn=None, rotation=270, show_leaf_name=False, show_branch_length=False, circularTree=False, show_division_nodes=True, distance_between_branches=4, show_border=False, width=None, height=None): from ete2 import TreeStyle from PyQt4 import QtSvg, QtCore, QtGui from ete2.treeview import qt4_render, drawer, main ts = TreeStyle() ts.show_scale = False ts.show_border = show_border ts.orientation = 1 # 0, tree is drawn from left-to-right. 1, tree is drawn from right-to-left ts.rotation = rotation ts.show_leaf_name = show_leaf_name ts.show_branch_length = show_branch_length if circularTree: ts.mode = 'c' else: ts.mode = 'r' ts.branch_vertical_margin = distance_between_branches def hideInternalNodesLayout(node): if not node.is_leaf(): node.img_style["size"] = 0 if show_division_nodes is False: ts.layout_fn = hideInternalNodesLayout if out_fn is not None: scene = qt4_render._TreeScene() img = ts tree_item, n2i, n2f = qt4_render.render(tree, img) scene.init_data(tree, img, n2i, n2f) tree_item.setParentItem(scene.master_item) scene.master_item.setPos(0,0) scene.addItem(scene.master_item) main.save(scene, out_fn, w=width, h=height, dpi=600) else: scene, img = drawer.init_scene(tree, None, ts) tree_item, n2i, n2f = qt4_render.render(tree, img) scene.init_data(tree, img, n2i, n2f) tree_item.setParentItem(scene.master_item) scene.addItem(scene.master_item) size = tree_item.rect() w, h = size.width(), size.height() svg = QtSvg.QSvgGenerator() svg.setFileName("test.svg") svg.setSize(QtCore.QSize(w, h)) svg.setViewBox(size) pp = QtGui.QPainter() pp.begin(svg) scene.render(pp, tree_item.rect(), tree_item.rect(), QtCore.Qt.KeepAspectRatio)
def writeSvg(self, fileName=None): if fileName is None: fileName = str(QtGui.QFileDialog.getSaveFileName()) self.svg = QtSvg.QSvgGenerator() self.svg.setFileName(fileName) self.svg.setSize(self.size()) self.svg.setResolution(600) painter = QtGui.QPainter(self.svg) self.render(painter)
def save_svg(self, fname): '''Write the QCA circuit to an svg file''' generator = QtSvg.QSvgGenerator() generator.setFileName(fname) generator.setSize(self.canvas.size()) generator.setViewBox(self.canvas.rect()) painter = QtGui.QPainter() painter.begin(generator) self.canvas.paint(painter) painter.end()
def saveToSVG(self): """ save into a file in svg format """ self.svgGen = QtSvg.QSvgGenerator() self.svgGen.setFileName("/local/new_home/indi/coba.svg") szF = self.scene.itemsBoundingRect() sz = Qt.QSize(int(szF.width()), int(szF.height())) self.svgGen.setSize(sz) bx = Qt.QRect(0,0,sz.width(),sz.height()) self.svgGen.setViewBox(bx) self.svgGen.setTitle("Coba untuk tgsdp") self.svgGen.setDescription("By Indar Sugiarto") painter = Qt.QPainter(self.svgGen) self.scene.render(painter)
def save_canvas(self): ''' ''' fname = QtGui.QFileDialog.getSaveFileName(self, 'Save SVG file...') if len(fname) == 0: return generator = QtSvg.QSvgGenerator() generator.setFileName(fname) generator.setSize(self.canvas.size()) generator.setViewBox(self.canvas.rect()) painter = QtGui.QPainter() painter.begin(generator) self.canvas.paint(painter) painter.end()
def DrawSomething(self): print "MySvgWriter.DrawSomething" gen = QtSvg.QSvgGenerator() gen.setFileName("output.svg") gen.setSize(QSize(200, 200)) gen.setViewBox(QRect(0, 0, 200, 200)) #gen.setDescription(self.tr("YOLO" "YOYO")) painter = QPainter() painter.begin(gen) painter.setClipRect(QRect(0, 0, 200, 200)) painter.setPen(Qt.NoPen) painter.fillRect(QRect(0, 0, 200, 200), Qt.darkBlue) painter.end()
def save(scene, imgName, w=None, h=None, dpi=90,\ take_region=False, units="px"): ipython_inline = False if imgName == "%%inline": ipython_inline = True ext = "PNG" elif imgName == "%%inlineSVG": ipython_inline = True ext = "SVG" elif imgName.startswith("%%return"): try: ext = imgName.split(".")[1].upper() except IndexError: ext = 'SVG' imgName = '%%return' else: ext = imgName.split(".")[-1].upper() main_rect = scene.sceneRect() aspect_ratio = main_rect.height() / main_rect.width() # auto adjust size if not w and not h: units = "px" w = main_rect.width() h = main_rect.height() ratio_mode = QtCore.Qt.KeepAspectRatio elif w and h: ratio_mode = QtCore.Qt.IgnoreAspectRatio elif h is None : h = w * aspect_ratio ratio_mode = QtCore.Qt.KeepAspectRatio elif w is None: w = h / aspect_ratio ratio_mode = QtCore.Qt.KeepAspectRatio # Adjust to resolution if units == "mm": if w: w = w * 0.0393700787 * dpi if h: h = h * 0.0393700787 * dpi elif units == "in": if w: w = w * dpi if h: h = h * dpi elif units == "px": pass else: raise Exception("wrong unit format") x_scale, y_scale = w/main_rect.width(), h/main_rect.height() if ext == "SVG": from PyQt4 import QtSvg svg = QtSvg.QSvgGenerator() targetRect = QtCore.QRectF(0, 0, w, h) svg.setSize(QtCore.QSize(w, h)) svg.setViewBox(targetRect) svg.setTitle("Generated with ETE http://etetoolkit.org") svg.setDescription("Generated with ETE http://etetoolkit.org") if imgName == '%%return': ba = QtCore.QByteArray() buf = QtCore.QBuffer(ba) buf.open(QtCore.QIODevice.WriteOnly) svg.setOutputDevice(buf) else: svg.setFileName(imgName) pp = QPainter() pp.begin(svg) scene.render(pp, targetRect, scene.sceneRect(), ratio_mode) pp.end() if imgName == '%%return': compatible_code = str(ba) print('from memory') else: compatible_code = open(imgName).read() # Fix a very annoying problem with Radial gradients in # inkscape and browsers... compatible_code = compatible_code.replace("xml:id=", "id=") compatible_code = re.sub('font-size="(\d+)"', 'font-size="\\1pt"', compatible_code) compatible_code = compatible_code.replace('\n', ' ') compatible_code = re.sub('<g [^>]+>\s*</g>', '', compatible_code) # End of fix if ipython_inline: from IPython.core.display import SVG return SVG(compatible_code) elif imgName == '%%return': return x_scale, y_scale, compatible_code else: open(imgName, "w").write(compatible_code) elif ext == "PDF" or ext == "PS": if ext == "PS": format = QPrinter.PostScriptFormat else: format = QPrinter.PdfFormat printer = QPrinter(QPrinter.HighResolution) printer.setResolution(dpi) printer.setOutputFormat(format) printer.setPageSize(QPrinter.A4) printer.setPaperSize(QtCore.QSizeF(w, h), QPrinter.DevicePixel) printer.setPageMargins(0, 0, 0, 0, QPrinter.DevicePixel) #pageTopLeft = printer.pageRect().topLeft() #paperTopLeft = printer.paperRect().topLeft() # For PS -> problems with margins #print paperTopLeft.x(), paperTopLeft.y() #print pageTopLeft.x(), pageTopLeft.y() # print printer.paperRect().height(), printer.pageRect().height() #topleft = pageTopLeft - paperTopLeft printer.setFullPage(True); printer.setOutputFileName(imgName); pp = QPainter(printer) targetRect = QtCore.QRectF(0, 0 , w, h) scene.render(pp, targetRect, scene.sceneRect(), ratio_mode) else: targetRect = QtCore.QRectF(0, 0, w, h) ii= QImage(w, h, QImage.Format_ARGB32) ii.fill(QColor(QtCore.Qt.white).rgb()) ii.setDotsPerMeterX(dpi / 0.0254) # Convert inches to meters ii.setDotsPerMeterY(dpi / 0.0254) pp = QPainter(ii) pp.setRenderHint(QPainter.Antialiasing) pp.setRenderHint(QPainter.TextAntialiasing) pp.setRenderHint(QPainter.SmoothPixmapTransform) scene.render(pp, targetRect, scene.sceneRect(), ratio_mode) pp.end() if ipython_inline: ba = QtCore.QByteArray() buf = QtCore.QBuffer(ba) buf.open(QtCore.QIODevice.WriteOnly) ii.save(buf, "PNG") from IPython.core.display import Image return Image(ba.data()) elif imgName == '%%return': ba = QtCore.QByteArray() buf = QtCore.QBuffer(ba) buf.open(QtCore.QIODevice.WriteOnly) ii.save(buf, "PNG") return x_scale, y_scale, ba.toBase64() else: ii.save(imgName) return w/main_rect.width(), h/main_rect.height()
def _get_buffer(size, filename): buffer = QtSvg.QSvgGenerator() buffer.setFileName(filename) buffer.setSize(QtCore.QSize(int(size.width()), int(size.height()))) return buffer
def save(self, imgName, w=None, h=None, header=None, \ dpi=150, take_region=False): ext = imgName.split(".")[-1].upper() root = self.startNode #aspect_ratio = root.fullRegion.height() / root.fullRegion.width() aspect_ratio = self.i_height / self.i_width # auto adjust size if w is None and h is None and (ext == "PDF" or ext == "PS"): w = dpi * 6.4 h = w * aspect_ratio if h > dpi * 11: h = dpi * 11 w = h / aspect_ratio elif w is None and h is None: w = self.i_width h = self.i_height elif h is None: h = w * aspect_ratio elif w is None: w = h / aspect_ratio if ext == "SVG": svg = QtSvg.QSvgGenerator() svg.setFileName(imgName) svg.setSize(QtCore.QSize(w, h)) svg.setViewBox(QtCore.QRect(0, 0, w, h)) #svg.setTitle("SVG Generator Example Drawing") #svg.setDescription("An SVG drawing created by the SVG Generator") pp = QtGui.QPainter() pp.begin(svg) targetRect = QtCore.QRectF(0, 0, w, h) self.render(pp, targetRect, self.sceneRect()) pp.end() elif ext == "PDF" or ext == "PS": format = QPrinter.PostScriptFormat if ext == "PS" else QPrinter.PdfFormat printer = QPrinter(QPrinter.HighResolution) printer.setResolution(dpi) printer.setOutputFormat(format) printer.setPageSize(QPrinter.A4) pageTopLeft = printer.pageRect().topLeft() paperTopLeft = printer.paperRect().topLeft() # For PS -> problems with margins # print paperTopLeft.x(), paperTopLeft.y() # print pageTopLeft.x(), pageTopLeft.y() # print printer.paperRect().height(), printer.pageRect().height() topleft = pageTopLeft - paperTopLeft printer.setFullPage(True) printer.setOutputFileName(imgName) pp = QtGui.QPainter(printer) if header: pp.setFont(QtGui.QFont("Verdana", 12)) pp.drawText(topleft.x(), 20, header) targetRect = QtCore.QRectF(topleft.x(), 20 + (topleft.y() * 2), w, h) else: targetRect = QtCore.QRectF(topleft.x(), topleft.y() * 2, w, h) if take_region: self.selector.setVisible(False) self.render(pp, targetRect, self.selector.rect()) self.selector.setVisible(True) else: self.render(pp, targetRect, self.sceneRect()) pp.end() return else: targetRect = QtCore.QRectF(0, 0, w, h) ii= QtGui.QImage(w, \ h, \ QtGui.QImage.Format_ARGB32) pp = QtGui.QPainter(ii) pp.setRenderHint(QtGui.QPainter.Antialiasing) pp.setRenderHint(QtGui.QPainter.TextAntialiasing) pp.setRenderHint(QtGui.QPainter.SmoothPixmapTransform) if take_region: self.selector.setVisible(False) self.render(pp, targetRect, self.selector.rect()) self.selector.setVisible(True) else: self.render(pp, targetRect, self.sceneRect()) pp.end() ii.save(imgName)
def gen_tooth_svg(tooth, size=TOOTH_SIZE): r = QtCore.QRect(0, 0, size, size) out = QtCore.QByteArray() buf = QtCore.QBuffer(out) gen = QtSvg.QSvgGenerator() gen.setSize(QtCore.QSize(size, size)) gen.setViewBox(QtCore.QRect(-1, -1, size + 2, size + 2)) gen.setOutputDevice(buf) painter = QtGui.QPainter() painter.begin(gen) painter.setBrush(QtGui.QBrush(QtCore.Qt.white)) sections = tooth.sections painter.setRenderHint(QtGui.QPainter.Antialiasing) painter.setPen(QtGui.QPen(QtCore.Qt.black)) painter.setFont(ToothSchemaDelegate.get_font(float(size) / TOOTH_SIZE)) x, y, w, h = r.x(), r.y(), r.width(), r.height() if sections.FULL: painter.setFont( ToothSchemaDelegate.get_large_font(float(size) / TOOTH_SIZE)) painter.drawEllipse(r) painter.drawText(r, QtCore.Qt.AlignCenter, '\n'.join( (state.code for state in sections.FULL))) painter.end() return unicode(out) painter.drawPie(r, 45 * 16, 90 * 16) # U painter.drawPie(r, 135 * 16, 90 * 16) # L painter.drawPie(r, 225 * 16, 90 * 16) # D painter.drawPie(r, 315 * 16, 90 * 16) # R if not tooth.is_simplified(): r = QtCore.QRect(x + w / 4, y + h / 4, w / 2, h / 2) painter.drawPie(r, 90 * 16, 180 * 16) # CL painter.drawPie(r, 270 * 16, 180 * 16) # CR painter.drawText(QtCore.QRect(x, y, w / 4, h), QtCore.Qt.AlignCenter, '\n'.join((state.code for state in sections.L))) painter.drawText(QtCore.QRect(x, y, w, h / 4), QtCore.Qt.AlignCenter, ' '.join((state.code for state in sections.U))) painter.drawText(QtCore.QRect(x + w * 3 / 4, y, w / 4, h), QtCore.Qt.AlignCenter, '\n'.join( (state.code for state in sections.R))) painter.drawText(QtCore.QRect(x, y + h * 3 / 4, w, h / 4), QtCore.Qt.AlignCenter, ' '.join( (state.code for state in sections.D))) painter.drawText(QtCore.QRect(x + w / 4, y, w / 4, h), QtCore.Qt.AlignCenter, '\n'.join( (state.code for state in sections.CL))) painter.drawText(QtCore.QRect(x + w * 2 / 4, y, w / 4, h), QtCore.Qt.AlignCenter, '\n'.join( (state.code for state in sections.CR))) else: painter.drawText(QtCore.QRect(x, y, w / 2, h), QtCore.Qt.AlignCenter, '\n'.join((state.code for state in sections.L))) painter.drawText(QtCore.QRect(x, y, w, h / 2), QtCore.Qt.AlignCenter, ' '.join((state.code for state in sections.U))) painter.drawText(QtCore.QRect(x + w / 2, y, w / 2, h), QtCore.Qt.AlignCenter, '\n'.join( (state.code for state in sections.R))) painter.drawText(QtCore.QRect(x, y + h / 2, w, h / 2), QtCore.Qt.AlignCenter, ' '.join( (state.code for state in sections.D))) painter.end() return unicode(out)