You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.4 KiB
42 lines
1.4 KiB
# coding: utf-8
|
|
from PySide6.QtCore import Qt, QRect
|
|
from PySide6.QtGui import QPainter, QImage, QBrush, QColor, QFont
|
|
from qfluentwidgets import NavigationWidget, isDarkTheme
|
|
|
|
|
|
class AvatarWidget(NavigationWidget):
|
|
""" Avatar widget """
|
|
|
|
def __init__(self, image_path, parent=None):
|
|
super().__init__(isSelectable=False, parent=parent)
|
|
self.avatar = QImage(image_path).scaled(
|
|
24, 24, Qt.KeepAspectRatio, Qt.SmoothTransformation)
|
|
|
|
def paintEvent(self, e):
|
|
painter = QPainter(self)
|
|
painter.setRenderHints(
|
|
QPainter.SmoothPixmapTransform | QPainter.Antialiasing)
|
|
|
|
painter.setPen(Qt.NoPen)
|
|
|
|
if self.isPressed:
|
|
painter.setOpacity(0.7)
|
|
|
|
# draw background
|
|
if self.isEnter:
|
|
c = 255 if isDarkTheme() else 0
|
|
painter.setBrush(QColor(c, c, c, 10))
|
|
painter.drawRoundedRect(self.rect(), 5, 5)
|
|
|
|
# draw avatar
|
|
painter.setBrush(QBrush(self.avatar))
|
|
painter.translate(8, 6)
|
|
painter.drawEllipse(0, 0, 24, 24)
|
|
painter.translate(-8, -6)
|
|
|
|
if not self.isCompacted:
|
|
painter.setPen(Qt.white if isDarkTheme() else Qt.black)
|
|
font = QFont('Segoe UI')
|
|
font.setPixelSize(14)
|
|
painter.setFont(font)
|
|
painter.drawText(QRect(44, 0, 255, 36), Qt.AlignVCenter, 'zhiyiYo') |