icon.py 3.7 KB
Newer Older
之一Yo's avatar
之一Yo 已提交
1
# coding:utf-8
之一Yo's avatar
之一Yo 已提交
2
from PyQt5.QtCore import QPoint, QRect, QRectF, Qt
之一Yo's avatar
之一Yo 已提交
3
from PyQt5.QtGui import QIcon, QIconEngine, QImage, QPainter, QPixmap
之一Yo's avatar
之一Yo 已提交
4
from PyQt5.QtSvg import QSvgRenderer
之一Yo's avatar
之一Yo 已提交
5

6
from .config import isDarkTheme
之一Yo's avatar
之一Yo 已提交
7 8


9 10
class IconEngine(QIconEngine):
    """ Icon engine """
之一Yo's avatar
之一Yo 已提交
11

之一Yo's avatar
之一Yo 已提交
12
    def __init__(self, iconPath):
之一Yo's avatar
之一Yo 已提交
13 14 15
        self.iconPath = iconPath
        super().__init__()

之一Yo's avatar
之一Yo 已提交
16
    def paint(self, painter, rect, mode, state):
之一Yo's avatar
之一Yo 已提交
17 18
        painter.setRenderHints(QPainter.Antialiasing |
                               QPainter.SmoothPixmapTransform)
之一Yo's avatar
之一Yo 已提交
19 20 21
        if not self.iconPath.lower().endswith('svg'):
            painter.drawImage(rect, QImage(self.iconPath))
        else:
之一Yo's avatar
之一Yo 已提交
22
            drawSvgIcon(self.iconPath, painter, rect)
之一Yo's avatar
之一Yo 已提交
23

之一Yo's avatar
之一Yo 已提交
24
    def pixmap(self, size, mode, state):
之一Yo's avatar
之一Yo 已提交
25 26 27 28 29 30 31 32
        pixmap = QPixmap(size)
        pixmap.fill(Qt.transparent)
        self.paint(QPainter(pixmap), QRect(QPoint(0, 0), size), mode, state)
        return pixmap


class Icon(QIcon):

之一Yo's avatar
之一Yo 已提交
33
    def __init__(self, iconPath):
之一Yo's avatar
之一Yo 已提交
34
        self.iconPath = iconPath
35 36 37 38 39 40 41 42 43 44 45
        super().__init__(IconEngine(iconPath))


class MenuIconEngine(QIconEngine):

    def __init__(self, icon):
        super().__init__()
        self.icon = icon

    def paint(self, painter, rect, mode, state):
        self.icon.paint(painter, rect, Qt.AlignHCenter, QIcon.Normal, state)
之一Yo's avatar
之一Yo 已提交
46 47 48 49


def getIconColor():
    """ get the color of icon based on theme """
50
    return "white" if isDarkTheme() else 'black'
之一Yo's avatar
之一Yo 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70


def drawSvgIcon(iconPath, painter, rect):
    """ draw svg icon

    Parameters
    ----------
    iconPath: str
        the path of svg icon

    painter: QPainter
        painter

    rect: QRect | QRectF
        the rect to render icon
    """
    renderer = QSvgRenderer(iconPath)
    renderer.render(painter, QRectF(rect))


之一Yo's avatar
之一Yo 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
def drawIcon(iconPath, painter, rect):
    """ draw icon

    Parameters
    ----------
    iconPath: str
        the path of svg icon

    painter: QPainter
        painter

    rect: QRect | QRectF
        the rect to render icon
    """
    if not iconPath.lower().endswith('svg'):
        image = QImage(iconPath).scaled(
            rect.width(), rect.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        painter.drawImage(rect, image)
    else:
        drawSvgIcon(iconPath, painter, rect)


之一Yo's avatar
之一Yo 已提交
93 94 95 96 97 98 99 100 101 102 103 104
class FluentIconFactory:
    """ Fluent icon factory """

    WEB = "Web"
    CUT = "Cut"
    ADD = "Add"
    COPY = "Copy"
    LINK = "Link"
    HELP = "Help"
    FONT = "Font"
    INFO = "Info"
    ZOOM = "Zoom"
之一Yo's avatar
之一Yo 已提交
105
    MENU = "Menu"
之一Yo's avatar
之一Yo 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    CLOSE = "Close"
    MOVIE = "Movie"
    BRUSH = "Brush"
    MUSIC = "Music"
    VIDEO = "Video"
    EMBED = "Embed"
    PASTE = "Paste"
    CANCEL = "Cancel"
    FOLDER = "Folder"
    SEARCH = "Search"
    UPDATE = "Update"
    SETTING = "Setting"
    PALETTE = "Palette"
    FEEDBACK = "Feedback"
    MINIMIZE = "Minimize"
    LANGUAGE = "Language"
    DOWNLOAD = "Download"
    QUESTION = "Question"
    ALIGNMENT = "Alignment"
    PENCIL_INK = "PencilInk"
    FOLDER_ADD = "FolderAdd"
    ARROW_DOWN = "ChevronDown"
    TRANSPARENT = "Transparent"
    MUSIC_FOLDER = "MusicFolder"
    CHEVRON_RIGHT = "ChevronRight"
    BACKGROUND_FILL = "BackgroundColor"
    FLUORESCENT_PEN = "FluorescentPen"

    @staticmethod
    def path(iconType):
        """ get the path of icon """
        return f':/qfluentwidgets/images/icons/{iconType}_{getIconColor()}.svg'

    @classmethod
    def icon(cls, iconType):
        """ create an fluent icon """
        return QIcon(cls.path(iconType))

    @classmethod
    def render(cls, iconType, painter, rect):
        """ draw svg icon

        Parameters
        ----------
        iconType: str
            fluent icon type

        painter: QPainter
            painter

        rect: QRect | QRectF
            the rect to render icon
        """
        drawSvgIcon(cls.path(iconType), painter, rect)