提交 407839f7 编写于 作者: 之一Yo's avatar 之一Yo

添加更换 svg 图标颜色的功能

上级 88fb2c71
......@@ -15,6 +15,12 @@ ToolBar>CaptionLabel {
ExampleCard {
background-color: transparent;
color: white;
}
TitleLabel,
StrongBodyLabel {
color: white;
}
ExampleCard>#card {
......@@ -28,12 +34,6 @@ ExampleCard>#card QLabel {
color: white;
}
ExampleCard>#titleLabel {
font: 14px 'Segoe UI Light', 'Microsoft YaHei';
background-color: transparent;
font-weight: 700;
color: white;
}
#sourceWidget {
background-color: rgb(51, 51, 51);
......@@ -41,8 +41,3 @@ ExampleCard>#titleLabel {
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
#sourcePathLabel {
color: white;
font: 14px 'Segoe UI', 'Microsoft YaHei';
}
......@@ -10,8 +10,7 @@ QScrollArea {
BannerWidget > #galleryLabel {
font: 42px 'Segoe UI', 'Microsoft YaHei';
font-weight: bold;
font: 42px 'Segoe UI SemiBold', 'Microsoft YaHei SemiBold';
background-color: transparent;
color: white;
padding-left: 28px;
......
......@@ -15,16 +15,12 @@ CustomTitleBar {
background-color: transparent;
}
CustomTitleBar > QLabel,
Widget > QLabel {
color: white;
}
CustomTitleBar>QLabel#titleLabel {
background: transparent;
font: 13px 'Segoe UI';
padding: 0 4px
padding: 0 4px;
color: white;
}
MinimizeButton {
......
......@@ -6,6 +6,10 @@ QScrollArea {
border: none;
}
ToolBar > StrongBodyLabel {
color: black;
}
ToolBar > CaptionLabel {
color: rgb(95, 95, 95);
}
......@@ -14,6 +18,11 @@ ExampleCard {
background-color: transparent;
}
TitleLabel,
StrongBodyLabel {
color: black;
}
ExampleCard > #card {
border: 1px solid rgb(234, 234, 234);
border-radius: 10px;
......@@ -25,21 +34,9 @@ ExampleCard > #card QLabel {
color: black;
}
ExampleCard > #titleLabel {
font: 14px 'Segoe UI Light', 'Microsoft YaHei';
font-weight: 700;
background-color: transparent;
color: black;
}
#sourceWidget {
background-color: rgb(253, 253, 253);
border-top: 1px solid rgb(234, 234, 234);
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
#sourcePathLabel {
color: black;
font: 14px 'Segoe UI', 'Microsoft YaHei';
}
......@@ -346,6 +346,7 @@ class QConfig(QObject):
"""
if isinstance(config, QConfig):
self._cfg = config
self._cfg.themeChanged.connect(self.themeChanged)
if isinstance(file, (str, Path)):
self._cfg.file = Path(file)
......
......@@ -3,8 +3,8 @@ from enum import Enum
from typing import Union
from PyQt5.QtXml import QDomDocument
from PyQt5.QtCore import QRectF, Qt, QFile, QObject
from PyQt5.QtGui import QIcon, QIconEngine
from PyQt5.QtCore import QRectF, Qt, QFile, QObject, QRect
from PyQt5.QtGui import QIcon, QIconEngine, QColor, QPixmap, QImage, QPainter
from PyQt5.QtWidgets import QAction
from PyQt5.QtSvg import QSvgRenderer
......@@ -12,7 +12,6 @@ from .config import isDarkTheme, Theme
from .overload import singledispatchmethod
class MenuIconEngine(QIconEngine):
def __init__(self, icon):
......@@ -39,6 +38,30 @@ class MenuIconEngine(QIconEngine):
painter.restore()
class SvgIconEngine(QIconEngine):
""" Svg icon engine """
def __init__(self, svg: str):
super().__init__()
self.svg = svg
def paint(self, painter, rect, mode, state):
drawSvgIcon(self.svg.encode(), painter, rect)
def clone(self) -> QIconEngine:
return SvgIconEngine(self.svg)
def pixmap(self, size, mode, state):
image = QImage(size, QImage.Format_ARGB32)
image.fill(Qt.transparent)
pixmap = QPixmap.fromImage(image, Qt.NoFormatConversion)
painter = QPainter(pixmap)
rect = QRect(0, 0, size.width(), size.height())
self.paint(painter, rect, mode, state)
return pixmap
def getIconColor(theme=Theme.AUTO, reverse=False):
""" get the color of icon based on theme """
if not reverse:
......@@ -143,7 +166,7 @@ def drawIcon(icon, painter, rect, **attributes):
class FluentIconBase:
""" Fluent icon base class """
def path(self, theme=Theme.AUTO):
def path(self, theme=Theme.AUTO) -> str:
""" get the path of icon
Parameters
......@@ -156,7 +179,7 @@ class FluentIconBase:
"""
raise NotImplementedError
def icon(self, theme=Theme.AUTO):
def icon(self, theme=Theme.AUTO, color: QColor = None):
""" create an fluent icon
Parameters
......@@ -166,8 +189,17 @@ class FluentIconBase:
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
color: QColor | Qt.GlobalColor | str
icon color, only applicable to svg icon
"""
return QIcon(self.path(theme))
path = self.path(theme)
if not (path.endswith('.svg') and color):
return QIcon(self.path(theme))
color = QColor(color).name()
return QIcon(SvgIconEngine(writeSvg(path, fill=color)))
def render(self, painter, rect, theme=Theme.AUTO, indexes=None, **attributes):
""" draw svg icon
......@@ -192,12 +224,17 @@ class FluentIconBase:
**attributes:
the attributes of modified path
"""
if attributes:
svg = writeSvg(self.path(theme), indexes, **attributes).encode()
else:
svg = self.path(theme)
icon = self.path(theme)
drawSvgIcon(svg, painter, rect)
if icon.endswith('.svg'):
if attributes:
icon = writeSvg(icon, indexes, **attributes).encode()
drawSvgIcon(icon, painter, rect)
else:
icon = QIcon(icon)
rect = QRectF(rect).toRect()
painter.drawPixmap(rect, icon.pixmap(QRectF(rect).toRect().size()))
class FluentIcon(FluentIconBase, Enum):
......@@ -347,4 +384,4 @@ class Action(QAction):
self.fluentIcon = icon
icon = icon.icon()
super().setIcon(icon)
\ No newline at end of file
super().setIcon(icon)
......@@ -52,7 +52,8 @@ class FluentLabelBase(QLabel):
def _init(self):
self.setFont(self.getFont())
self.setTextColor()
qconfig.themeChanged.connect(lambda: self.setTextColor())
qconfig.themeChanged.connect(
lambda: self.setTextColor(self.lightColor, self.darkColor))
return self
def getFont(self):
......@@ -73,6 +74,7 @@ class FluentLabelBase(QLabel):
color = self.darkColor if isDarkTheme() else self.lightColor
palette.setColor(QPalette.WindowText, color)
self.setPalette(palette)
# self.setStyleSheet(f"QLabel{{color: {color.name()}}}")
class CaptionLabel(FluentLabelBase):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册