def show(cursor, pos=None, num_lines=6):
    """Displays a tooltip showing part of the cursor's Document.
    
    If the cursor has a selection, those blocks are displayed.
    Otherwise, num_lines lines are displayed.
    
    If pos is not given, the global mouse position is used.
    
    """
    block = cursor.document().findBlock(cursor.selectionStart())
    c2 = QTextCursor(block)
    if cursor.hasSelection():
        c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
        c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
    else:
        c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines)
    
    data = textformats.formatData('editor')
    
    doc = QTextDocument()
    font = QFont(data.font)
    font.setPointSizeF(font.pointSizeF() * .8)
    doc.setDefaultFont(font)
    doc.setPlainText(c2.selection().toPlainText())
    if metainfo.info(cursor.document()).highlighting:
        highlighter.highlight(doc, state=tokeniter.state(block))
    size = doc.size().toSize() + QSize(8, -4)
    pix = QPixmap(size)
    pix.fill(data.baseColors['background'])
    doc.drawContents(QPainter(pix))
    label = QLabel()
    label.setPixmap(pix)
    label.setStyleSheet("QLabel { border: 1px solid #777; }")
    label.resize(size)
    widgets.customtooltip.show(label, pos)
Exemple #2
0
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
Exemple #3
0
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)
Exemple #4
0
    def drawContents(self, painter):
        """
        Reimplementation of drawContents to limit the drawing
        inside `textRext`.

        """
        painter.setPen(self.__color)
        painter.setFont(self.font())

        if self.__textRect:
            rect = self.__textRect
        else:
            rect = self.rect().adjusted(5, 5, -5, -5)
        if Qt.mightBeRichText(self.__message):
            doc = QTextDocument()
            doc.setHtml(self.__message)
            doc.setTextWidth(rect.width())
            cursor = QTextCursor(doc)
            cursor.select(QTextCursor.Document)
            fmt = QTextBlockFormat()
            fmt.setAlignment(self.__alignment)
            cursor.mergeBlockFormat(fmt)
            painter.save()
            painter.translate(rect.topLeft())
            doc.drawContents(painter)
            painter.restore()
        else:
            painter.drawText(rect, self.__alignment, self.__message)
Exemple #5
0
    def drawContents(self, painter):
        """
        Reimplementation of drawContents to limit the drawing
        inside `textRext`.

        """
        painter.setPen(self.__color)
        painter.setFont(self.font())

        if self.__textRect:
            rect = self.__textRect
        else:
            rect = self.rect().adjusted(5, 5, -5, -5)
        if Qt.mightBeRichText(self.__message):
            doc = QTextDocument()
            doc.setHtml(self.__message)
            doc.setTextWidth(rect.width())
            cursor = QTextCursor(doc)
            cursor.select(QTextCursor.Document)
            fmt = QTextBlockFormat()
            fmt.setAlignment(self.__alignment)
            cursor.mergeBlockFormat(fmt)
            painter.save()
            painter.translate(rect.topLeft())
            doc.drawContents(painter)
            painter.restore()
        else:
            painter.drawText(rect, self.__alignment, self.__message)
class HtmlItemDelegate(QItemDelegate):
    def __init__(self, parent):
        QItemDelegate.__init__(self, parent)
        self.document = QTextDocument(self)
        self.document.setDocumentMargin(0)
        self.hl_color = QApplication.palette().highlight().color()
        self.hl_color = ",".join([repr(self.hl_color.red()), repr(self.hl_color.green()), repr(self.hl_color.blue())])

    def drawDisplay(self, painter, option, rect, text):
        point = rect.topLeft()
        painter.translate(point)
        if option.state & QStyle.State_Selected:
            text = "<div style='background-color: rgb(%s)'>%s</div>" % (self.hl_color, text)
        self.document.setHtml(text)
        self.document.drawContents(painter)
        painter.translate(-point)
 def paint(self, painter, option, index):
     text = index.model().data(index, Qt.DisplayRole).toString()
     palette = QApplication.palette()
     document = QTextDocument()
     document.setDefaultFont(option.font)
     if option.state & QStyle.State_Selected:
         document.setHtml(
             QString("<font color=%1>%2</font>").arg(
                 palette.highlightedText().color().name()).arg(text))
     else:
         document.setHtml(text)
     painter.save()
     color = (palette.highlight().color() if option.state
              & QStyle.State_Selected else QColor(index.model().data(
                  index, Qt.BackgroundColorRole)))
     painter.fillRect(option.rect, color)
     painter.translate(option.rect.x(), option.rect.y())
     document.drawContents(painter)
     painter.restore()
 def paint(self, painter, option, index):
     text = index.model().data(index, Qt.DisplayRole).toString()
     palette = QApplication.palette()
     document = QTextDocument()
     document.setDefaultFont(option.font)
     if option.state & QStyle.State_Selected:
         document.setHtml(QString("<font color=%1>%2</font>")
                 .arg(palette.highlightedText().color().name())
                 .arg(text))
     else:
         document.setHtml(text)
     painter.save()
     color = (palette.highlight().color()
              if option.state & QStyle.State_Selected
              else QColor(index.model().data(index,
                          Qt.BackgroundColorRole)))
     painter.fillRect(option.rect, color)
     painter.translate(option.rect.x(), option.rect.y())
     document.drawContents(painter)
     painter.restore()
Exemple #9
0
	def paint(self, painter, option, index):
		if index.column() in (SRC_ADDR, DST_ADDR):
			text = index.model().data(index).toString()
			palette = QApplication.palette()
			document = QTextDocument()
			document.setDefaultFont(option.font)
			if option.state & QStyle.State_Selected:
				document.setHtml("<font color=%s>%s</font>" % 
								(palette.highlightedText().color().name(), text))
				color = palette.highlight().color()
			else:
				document.setHtml(text)
				color = QColor(index.model().data(index, Qt.BackgroundColorRole))
			#document.setPageSize(option.rect.size())
			painter.save()
			painter.fillRect(option.rect, color)
			painter.translate(option.rect.x(), option.rect.y())
			document.drawContents(painter)
			painter.restore()
		else:
			QItemDelegate.paint(self, painter, option, index)
Exemple #10
0
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)
def show(cursor, pos=None, num_lines=6):
    """Displays a tooltip showing part of the cursor's Document.
    
    If the cursor has a selection, those blocks are displayed.
    Otherwise, num_lines lines are displayed.
    
    If pos is not given, the global mouse position is used.
    
    """
    block = cursor.document().findBlock(cursor.selectionStart())
    c2 = QTextCursor(block)
    if cursor.hasSelection():
        c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
        c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
    else:
        c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor,
                        num_lines)

    data = textformats.formatData('editor')

    doc = QTextDocument()
    font = QFont(data.font)
    font.setPointSizeF(font.pointSizeF() * .8)
    doc.setDefaultFont(font)
    doc.setPlainText(c2.selection().toPlainText())
    if metainfo.info(cursor.document()).highlighting:
        highlighter.highlight(doc, state=tokeniter.state(block))
    size = doc.size().toSize() + QSize(8, -4)
    pix = QPixmap(size)
    pix.fill(data.baseColors['background'])
    doc.drawContents(QPainter(pix))
    label = QLabel()
    label.setPixmap(pix)
    label.setStyleSheet("QLabel { border: 1px solid #777; }")
    label.resize(size)
    widgets.customtooltip.show(label, pos)
def main():
	app = QApplication(sys.argv)
	
	# Preparing the pdf
	doc = QTextDocument()
	printer = QPrinter()
	printer.setOutputFileName("KabitGurdas.pdf")
	printer.setOutputFormat(QPrinter.PdfFormat)
	
	# Setting page size as A5
	printer.setPaperSize(QPrinter.A5);
	# page margin
	
	printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter)
	doc.setPageSize(QSizeF(printer.pageRect().size()))
	
	painter = QPainter()
	painter.begin( printer )	
	
	# Set poem book and get each poem in pdf
	book = kb()
	
	for KabitNumber in range(1,676):		
		# Add text in html to page
		doc.setHtml(book.getKabitHTML(KabitNumber))
		doc.defaultFont().setPointSize(20)
	
		# A new page
		printer.newPage()
		# Create a QPainter to draw our content    
		doc.drawContents(painter)#,printer.pageRect().translated( -printer.pageRect().x(), -printer.pageRect().y() ))
		#QTextDocument.drawContents(QPainter, QRectF rect=QRectF())
		doc.print_(printer)
	
	# Done.
	painter.end()
Exemple #13
0
    def paint(self, painter, option, index):
        painter.save()

        cell_width = self.size.width()

        #if option.state & QStyle.State_Selected:
        #    painter.fillRect(option.rect, option.palette.highlight())
        #painter.drawRect(option.rect)


        # Draw marks before translating painter
        # =====================================

        # Draw avatar
        if not self.avatar:
            avatar_filepath = index.data(self.AvatarRole).toPyObject()
            self.avatar = QPixmap(avatar_filepath)
        x = option.rect.left() + (self.BOX_MARGIN * 2)
        y = option.rect.top() + (self.BOX_MARGIN * 2)
        rect = QRect(x, y, self.AVATAR_SIZE, self.AVATAR_SIZE)
        painter.drawPixmap(rect, self.avatar)

        # Draw verified account icon
        if index.data(self.VerifiedRole).toPyObject():
            rect2 = QRect(rect.right() - 11, rect.bottom() - 10, 16, 16)
            painter.drawPixmap(rect2, self.verified_icon)

        marks_margin = 0
        # Favorite mark
        if index.data(self.FavoritedRole).toPyObject():
            x = cell_width - 16 - self.BOX_MARGIN
            y = option.rect.top() + self.BOX_MARGIN
            rect = QRect(x, y, 16, 16)
            painter.drawPixmap(rect, self.favorite_icon)
            marks_margin = 16

        # Draw reposted icon
        if index.data(self.RepeatedRole).toPyObject():
            x = cell_width - 16 - self.BOX_MARGIN - marks_margin
            y = option.rect.top() + self.BOX_MARGIN
            rect = QRect(x, y, 16, 16)
            painter.drawPixmap(rect, self.repeated_icon)

        # Draw protected account icon
        protected_icon_margin = 0
        if index.data(self.ProtectedRole).toPyObject():
            x = option.rect.left() + self.BOX_MARGIN + self.AVATAR_SIZE + self.LEFT_MESSAGE_MARGIN
            y = option.rect.top() + self.BOX_MARGIN
            rect = QRect(x, y, 16, 16)
            painter.drawPixmap(rect, self.protected_icon)
            protected_icon_margin = 16

        # ==== End of pixmap drawing ====

        accumulated_height = 0

        # Draw fullname
        fullname = self.__render_fullname(cell_width, index)
        x = option.rect.left() + self.BOX_MARGIN + self.AVATAR_SIZE
        x += self.LEFT_MESSAGE_MARGIN + protected_icon_margin
        y = option.rect.top()
        painter.translate(x, y)
        fullname.drawContents(painter)

        # Draw username
        username = self.__render_username(cell_width, index)
        painter.translate(fullname.idealWidth(), 0)
        username.drawContents(painter)

        # Draw status message
        x = -fullname.idealWidth() - protected_icon_margin
        y = fullname.size().height() + self.TOP_MESSAGE_MARGIN
        painter.translate(x, y)
        message = self.__render_status_message(cell_width, index)
        message.drawContents(painter)
        accumulated_height += y + message.size().height()

        # Draw reposted by
        x = self.BOX_MARGIN + 16 - (self.LEFT_MESSAGE_MARGIN + self.AVATAR_SIZE)
        y = message.size().height() + self.BOTTOM_MESSAGE_MARGIN
        if accumulated_height < self.AVATAR_SIZE:
            y += (self.AVATAR_SIZE - accumulated_height) + self.COMPLEMENT_HEIGHT
        painter.translate(x, y)

        reposted_by = index.data(self.RepostedRole).toPyObject()
        if reposted_by:
            reposted = QTextDocument()
            reposted.setHtml("<span style='color: #999;'>%s</span>" % reposted_by)
            reposted.setDefaultFont(FOOTER_FONT)
            reposted.setTextWidth(self.__calculate_text_width(cell_width))
            reposted.drawContents(painter)

            # Draw reposted icon
            rect2 = QRect(-16, 3, 16, 16)
            painter.drawPixmap(rect2, self.reposted_icon)

        # Draw datetime
        datetime = index.data(self.DateRole).toPyObject()
        timestamp = QTextDocument()
        timestamp.setHtml("<span style='color: #999;'>%s</span>" % datetime)
        timestamp.setDefaultFont(FOOTER_FONT)
        timestamp.setTextWidth(self.__calculate_text_width(cell_width))
        x = self.size.width() - timestamp.idealWidth() - 20 - self.BOX_MARGIN
        painter.translate(x, 0)
        timestamp.drawContents(painter)

        painter.resetTransform()
        painter.translate(0, option.rect.bottom())
        line = QLine(0, 0, option.rect.width(), 0)
        painter.setPen(QColor(230, 230, 230))
        painter.drawLine(line)

        painter.restore()
    # max numbers in one line then add <br> in "english" translation
    no_of_words = 20
    for num, name in enumerate(Plist):
        k = Elist[num].split()
        klen = k.__len__()
        t = klen / no_of_words
        for i in range(t):
            k.insert(no_of_words * (i + 1), "<br>")
        strline = " ".join(k)
        htmllist.append(AddHTMLTag(Center(Italics(TextSize(strline, 3))), "p"))

    body = " ".join(htmllist)
    # Necessary for unicode rendering
    headuni = '<meta http-equiv="Content-Type" content="text\/html; charset=utf-8">'

    html[count] = AddHead(headuni) + AddBody(body)

    # A new page
    printer.newPage()

    # Draw the first page removing the pageRect offset due to margins.
    doc.setHtml(html[count])
    # Create a QPainter to draw our content

    doc.drawContents(painter)  # ,printer.pageRect().translated( -printer.pageRect().x(), -printer.pageRect().y() ))
    # QTextDocument.drawContents(QPainter, QRectF rect=QRectF())
    doc.print_(printer)

# Done.
painter.end()
Exemple #15
0
    def paint(self, painter, option, index):
        painter.save()

        cell_width = self.size.width()

        #if option.state & QStyle.State_Selected:
        #    painter.fillRect(option.rect, option.palette.highlight())
        #painter.drawRect(option.rect)

        # Draw marks before translating painter
        # =====================================

        # Draw avatar
        if not self.avatar:
            avatar_filepath = index.data(self.AvatarRole).toPyObject()
            self.avatar = QPixmap(avatar_filepath)
        x = option.rect.left() + (self.BOX_MARGIN * 2)
        y = option.rect.top() + (self.BOX_MARGIN * 2)
        rect = QRect(x, y, self.AVATAR_SIZE, self.AVATAR_SIZE)
        painter.drawPixmap(rect, self.avatar)

        # Draw verified account icon
        if index.data(self.VerifiedRole).toPyObject():
            rect2 = QRect(rect.right() - 11, rect.bottom() - 10, 16, 16)
            painter.drawPixmap(rect2, self.verified_icon)

        marks_margin = 0
        # Favorite mark
        if index.data(self.FavoritedRole).toPyObject():
            x = cell_width - 16 - self.BOX_MARGIN
            y = option.rect.top() + self.BOX_MARGIN
            rect = QRect(x, y, 16, 16)
            painter.drawPixmap(rect, self.favorite_icon)
            marks_margin = 16

        # Draw reposted icon
        if index.data(self.RepeatedRole).toPyObject():
            x = cell_width - 16 - self.BOX_MARGIN - marks_margin
            y = option.rect.top() + self.BOX_MARGIN
            rect = QRect(x, y, 16, 16)
            painter.drawPixmap(rect, self.repeated_icon)

        # Draw protected account icon
        protected_icon_margin = 0
        if index.data(self.ProtectedRole).toPyObject():
            x = option.rect.left(
            ) + self.BOX_MARGIN + self.AVATAR_SIZE + self.LEFT_MESSAGE_MARGIN
            y = option.rect.top() + self.BOX_MARGIN
            rect = QRect(x, y, 16, 16)
            painter.drawPixmap(rect, self.protected_icon)
            protected_icon_margin = 16

        # ==== End of pixmap drawing ====

        accumulated_height = 0

        # Draw fullname
        fullname = self.__render_fullname(cell_width, index)
        x = option.rect.left() + self.BOX_MARGIN + self.AVATAR_SIZE
        x += self.LEFT_MESSAGE_MARGIN + protected_icon_margin
        y = option.rect.top()
        painter.translate(x, y)
        fullname.drawContents(painter)

        # Draw username
        username = self.__render_username(cell_width, index)
        painter.translate(fullname.idealWidth(), 0)
        username.drawContents(painter)

        # Draw status message
        x = -fullname.idealWidth() - protected_icon_margin
        y = fullname.size().height() + self.TOP_MESSAGE_MARGIN
        painter.translate(x, y)
        message = self.__render_status_message(cell_width, index)
        message.drawContents(painter)
        accumulated_height += y + message.size().height()

        # Draw reposted by
        x = self.BOX_MARGIN + 16 - (self.LEFT_MESSAGE_MARGIN +
                                    self.AVATAR_SIZE)
        y = message.size().height() + self.BOTTOM_MESSAGE_MARGIN
        if accumulated_height < self.AVATAR_SIZE:
            y += (self.AVATAR_SIZE -
                  accumulated_height) + self.COMPLEMENT_HEIGHT
        painter.translate(x, y)

        reposted_by = index.data(self.RepostedRole).toPyObject()
        if reposted_by:
            reposted = QTextDocument()
            reposted.setHtml("<span style='color: #999;'>%s</span>" %
                             reposted_by)
            reposted.setDefaultFont(FOOTER_FONT)
            reposted.setTextWidth(self.__calculate_text_width(cell_width))
            reposted.drawContents(painter)

            # Draw reposted icon
            rect2 = QRect(-16, 3, 16, 16)
            painter.drawPixmap(rect2, self.reposted_icon)

        # Draw datetime
        datetime = index.data(self.DateRole).toPyObject()
        timestamp = QTextDocument()
        timestamp.setHtml("<span style='color: #999;'>%s</span>" % datetime)
        timestamp.setDefaultFont(FOOTER_FONT)
        timestamp.setTextWidth(self.__calculate_text_width(cell_width))
        x = self.size.width() - timestamp.idealWidth() - 20 - self.BOX_MARGIN
        painter.translate(x, 0)
        timestamp.drawContents(painter)

        painter.resetTransform()
        painter.translate(0, option.rect.bottom())
        line = QLine(0, 0, option.rect.width(), 0)
        painter.setPen(QColor(230, 230, 230))
        painter.drawLine(line)

        painter.restore()