Exemple #1
0
    def paintEvent(self, e):

        qp = QPainter()
        qp.begin(self)
        qp.setRenderHint(QPainter.RenderHints.Antialiasing)
        self.drawBezierCurve(qp)
        qp.end()
Exemple #2
0
 def paintEvent(self, ev):
     r = self.rect()
     painter = QPainter(self)
     painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, True)
     icon = get_icon('busy.svg' if self.running else DOWNLOAD_ICON_NAME)
     pmap = icon.pixmap(r.width(), r.height())
     x = (r.width() - int(pmap.width() / pmap.devicePixelRatio())) // 2
     y = (r.height() - int(pmap.height() / pmap.devicePixelRatio())) // 2 + 1
     painter.drawPixmap(x, y, pmap)
Exemple #3
0
 def paintEvent(self, ev):
     painter = QPainter(self)
     painter.setRenderHint(QPainter.RenderHint.TextAntialiasing)
     f = painter.font()
     f.setBold(True)
     f.setPixelSize(self.height() - 1)
     painter.setFont(f)
     painter.setPen(QColor('red' if self.is_enabled else 'green'))
     painter.drawText(self.rect(), Qt.AlignmentFlag.AlignCenter, 'Z')
     painter.end()
Exemple #4
0
 def paintEvent(self, ev):
     painter = QPainter(self)
     painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
     painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, True)
     try:
         self.paint_background(painter)
     except Exception:
         pass
     finally:
         painter.end()
     QWidget.paintEvent(self, ev)
Exemple #5
0
 def rotated_by(self, pixmap, angle):
     ans = pixmap.copy()
     ans.fill(Qt.GlobalColor.transparent)
     p = QPainter(ans)
     p.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
     p.setRenderHint(QPainter.RenderHint.Antialiasing)
     sz = ans.size().width() / ans.devicePixelRatio()
     p.translate(sz // 2, sz // 2)
     p.rotate(angle)
     p.translate(-sz // 2, -sz // 2)
     p.drawPixmap(0, 0, pixmap)
     p.end()
     return ans
Exemple #6
0
    def paintEvent(self, event):
        if (not self.m_displayedWhenStopped) and (not self.isAnimated()):
            return

        width = min(self.width(), self.height())

        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)

        outerRadius = (width - 1) * 0.5
        innerRadius = (width - 1) * 0.5 * 0.4375

        capsuleHeight = outerRadius - innerRadius
        capsuleWidth = width * 3 / 32
        capsuleRadius = capsuleWidth / 2

        for i in range(0, 12):
            color = QtGui.QColor(self.m_color)

            if self.isAnimated():
                color.setAlphaF(1.0 - (i / 12.0))
            else:
                color.setAlphaF(0.2)

            painter.setPen(Qt.PenStyle.NoPen)
            painter.setBrush(color)
            painter.save()
            painter.translate(self.rect().center())
            painter.rotate(self.m_angle - (i * 30.0))

            width = -1 * capsuleWidth / 2
            height = -1 * (innerRadius + capsuleHeight)

            painter.drawRoundedRect(
                round(width),
                round(height),
                round(capsuleWidth),
                round(capsuleHeight),
                capsuleRadius,
                capsuleRadius,
            )
            painter.restore()
Exemple #7
0
 def paintEvent(self, ev):
     if not self.static_text or not self.static_text.text():
         return
     p = QPainter(self)
     p.setRenderHint(QPainter.RenderHint.TextAntialiasing)
     # If text is too long too fit, fade it out at the end
     self.static_text.setTextWidth(self.rect().width())
     sz = self.static_text.size()
     r = self.rect()
     p.drawStaticText(0,
                      int(r.height() - sz.height()) // 2, self.static_text)
     if sz.width() > r.width():
         g = QLinearGradient(QPointF(self.rect().topLeft()),
                             QPointF(self.rect().topRight()))
         c = QColor(self.sb_background)
         c.setAlpha(0)
         g.setColorAt(0, c)
         g.setColorAt(0.8, c)
         g.setColorAt(1.0, self.sb_background)
         p.fillRect(self.rect(), QBrush(g))
     p.end()
Exemple #8
0
    def paintEvent(self, a0: QtGui.QPaintEvent) -> None:

        super().paintEvent(a0)

        qp = QPainter()
        qp.begin(self)
        qp.setRenderHint(QPainter.RenderHint.Antialiasing)

        # Hintergrund malen
        pen = QPen(Qt.GlobalColor.gray, 1, Qt.PenStyle.SolidLine)
        qp.setPen(pen)
        qp.setBrush(Qt.GlobalColor.gray)
        qp.drawRect(0, 0, self.width(), self.height())

        # Sample malen
        pen.setColor(Qt.GlobalColor.white)
        qp.setPen(pen)
        qp.setBrush(Qt.GlobalColor.white)
        qp.drawRect(*self.norm_to_pixel_coord_int(-self.margin, -self.margin),
                    self.norm_to_pixel_rel_int(self.x_range + 2 * self.margin),
                    self.norm_to_pixel_rel_int(self.x_range + 2 * self.margin))

        qp.end()
Exemple #9
0
def get_masked_image(path, size=64, overlay_text=""):
    """
    Returns a pixmap from an image file masked with a smooth circle.
    The returned pixmap will have a size of *size* × *size* pixels.

    :param str path: Path to image file.
    :param int size: Target size. Will be the diameter of the masked image.
    :param str overlay_text: Overlay text. This will be shown in white sans-serif on top
        of the image.
    :return: Masked image with overlay text.
    :rtype: QPixmap
    """

    with open(path, "rb") as f:
        imgdata = f.read()

    imgtype = path.split(".")[-1]

    # Load image and convert to 32-bit ARGB (adds an alpha channel):
    image = QImage.fromData(imgdata, imgtype)
    image.convertToFormat(QImage.Format.Format_ARGB32)

    # Crop image to a square:
    imgsize = min(image.width(), image.height())
    width = (image.width() - imgsize) / 2
    height = (image.height() - imgsize) / 2

    rect = QRect(
        round(width),
        round(height),
        imgsize,
        imgsize,
    )
    image = image.copy(rect)

    # Create the output image with the same dimensions and an alpha channel
    # and make it completely transparent:
    out_img = QImage(imgsize, imgsize, QImage.Format.Format_ARGB32)
    out_img.fill(Qt.GlobalColor.transparent)

    # Create a texture brush and paint a circle with the original image onto
    # the output image:
    brush = QBrush(image)  # Create texture brush
    painter = QPainter(out_img)  # Paint the output image
    painter.setBrush(brush)  # Use the image texture brush
    painter.setPen(Qt.PenStyle.NoPen)  # Don't draw an outline
    painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)  # Use AA
    painter.drawEllipse(0, 0, imgsize, imgsize)  # Actually draw the circle

    if overlay_text:
        # draw text
        font = QtGui.QFont("Arial Rounded MT Bold")
        font.setPointSize(imgsize * 0.4)
        painter.setFont(font)
        painter.setPen(Qt.GlobalColor.white)
        painter.drawText(QRect(0, 0, imgsize, imgsize),
                         Qt.AlignmentFlag.AlignCenter, overlay_text)

    painter.end()  # We are done (segfault if you forget this)

    # Convert the image to a pixmap and rescale it.  Take pixel ratio into
    # account to get a sharp image on retina displays:
    pr = QtWidgets.QApplication.instance().devicePixelRatio()
    pm = QPixmap.fromImage(out_img)
    pm.setDevicePixelRatio(pr)
    size = int(pr * size)
    pm = pm.scaled(
        size,
        size,
        Qt.AspectRatioMode.KeepAspectRatio,
        Qt.TransformationMode.SmoothTransformation,
    )

    return pm