def stamp_image(image, expression_str, position, feature): painter = QPainter(image) data = QgsExpression.replaceExpressionText(expression_str, feature, None) if not data: return image data = data.replace(r"\n", "<br>") style = """ body { color: yellow; } """ doc = QTextDocument() doc.setDefaultStyleSheet(style) data = "<body>{}</body>".format(data) doc.setHtml(data) point = QPointF(20, 20) # Wrap the text so we don't go crazy if doc.size().width() > 300: doc.setTextWidth(300) if position == "top-left": point = QPointF(20, 20) elif position == "top-right": x = image.width() - 20 - doc.size().width() point = QPointF(x, 20) elif position == "bottom-left": point = QPointF(20, image.height() - 20 - doc.size().height()) elif position == "bottom-right": x = image.width() - 20 - doc.size().width() y = image.height() - 20 - doc.size().height() point = QPointF(x, y) painter.translate(point) doc.drawContents(painter) return image
class FeatureShape(LabeledPolygonShape): def draw(self, painter, xMap, yMap, canvasRect): self._setup_painter(painter) self._set_outer_pen_and_brush(painter, xMap, yMap) rtmin = self.item.rtmin rtmax = self.item.rtmax mzmin = self.item.mzmin mzmax = self.item.mzmax self._draw_polygon(painter, xMap, yMap, (rtmin, rtmax, mzmin, mzmax)) self._set_inner_pen_and_brush(painter, xMap, yMap) for mass_trace in self.item.mass_traces: self._draw_polygon(painter, xMap, yMap, mass_trace) if self.label is not None: self._draw_label(painter, xMap, yMap) def _draw_label(self, painter, xMap, yMap): self.text = QTextDocument() self.text.setDefaultStyleSheet("""div { color: rgb(%d, %d, %d); }""" % self.color) self.text.setHtml("<div>%s</div>" % (self.label, )) x0 = xMap.transform(self.item.rtmax) # y0: height between m0 and m1 masstrace if m1 exists, else at height of m0 yi = sorted(m.mzmin for m in self.item.mass_traces) if len(yi) >= 2: y0 = yMap.transform(0.5 * yi[0] + 0.5 * yi[1]) else: y0 = yMap.transform(yi[0]) h = self.text.size().height() painter.translate(x0, y0 - 0.5 * h) self.text.drawContents(painter)
def makeTableDocument(self): printer = QPrinter() document = QTextDocument() document.setDefaultStyleSheet("table {border:1px; border-color:teal}") document.setDefaultStyleSheet("h1, h2, h3 {color:teal}") document.setDocumentMargin(0.0) document.setPageSize(QSizeF(printer.pageRect().size())) header = ''' <html> <body> <div style="line-height:2.5"> <h1>Desmond International College</h1> <h2>Km4, Happiness Street, Kafanchan</h2> <h2>Kaduna, Nigeria</h2> </div> <div> <h2 style='display:block; text-align:center; word-spacing:10vw; text-transform: uppercase; margin-top:25px; margin-bottom:15px'><u>STUDENT DATA TABLE</u></h2> </div> </body> </html> ''' #print(dir(document)) cursor = QTextCursor(document) rows = self.table.rowCount() columns = self.table.columnCount() cursor.insertHtml(header) table = cursor.insertTable(rows + 1, columns) formats = table.format() formats.setHeaderRowCount(1) table.setFormat(formats) formats = cursor.blockCharFormat() formats.setFontWeight(QFont.Bold) for column in range(columns): cursor.setCharFormat(formats) cursor.insertText(self.table.horizontalHeaderItem(column).text()) cursor.movePosition(QTextCursor.NextCell) for row in range(rows): for column in range(columns): cursor.insertText(self.table.item(row, column).text()) cursor.movePosition(QTextCursor.NextCell) return document
class PeakRangeShape(LabeledPolygonShape): def draw(self, painter, xMap, yMap, canvasRect): self._setup_painter(painter) self._set_inner_pen_and_brush(painter, xMap, yMap) self._draw_polygon(painter, xMap, yMap, self.item) if self.label is not None: self._draw_label(painter, xMap, yMap) def _draw_label(self, painter, xMap, yMap): self.text = QTextDocument() self.text.setDefaultStyleSheet("""div { color: rgb(%d, %d, %d); }""" % self.color) self.text.setHtml("<div>%s</div>" % (self.label, )) x0 = xMap.transform(self.item.rtmax) # y0: height between m0 and m1 masstrace if m1 exists, else at height of m0 y0 = yMap.transform(0.5 * self.item.mzmin + 0.5 * self.item.mzmax) h = self.text.size().height() painter.translate(x0, y0 - 0.5 * h) self.text.drawContents(painter)