提交 4b25ad5b 编写于 作者: 之一Yo's avatar 之一Yo

添加图标抽象类 `FluentIconBase`

上级 8a25718b
......@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: MIT, see LICENSE for more details.
"""
__version__ = "0.3.0"
__version__ = "0.3.1"
from .components import *
from .common import *
......
from .config import *
from .auto_wrap import TextWrap
from .icon import Icon, getIconColor, drawSvgIcon, FluentIcon, drawIcon
from .icon import Icon, getIconColor, drawSvgIcon, FluentIcon, drawIcon, FluentIconBase
from .style_sheet import setStyleSheet, getStyleSheet, setTheme
from .smooth_scroll import SmoothScroll, SmoothMode
\ No newline at end of file
......@@ -70,12 +70,12 @@ def drawSvgIcon(iconPath, painter, rect):
renderer.render(painter, QRectF(rect))
def drawIcon(icon, painter:QPainter, rect):
def drawIcon(icon, painter, rect):
""" draw icon
Parameters
----------
icon: `str` | `QIcon` | `FluentIcon`
icon: str | QIcon | FluentIconBaseBase
the icon to be drawn
painter: QPainter
......@@ -84,7 +84,7 @@ def drawIcon(icon, painter:QPainter, rect):
rect: QRect | QRectF
the rect to render icon
"""
if isinstance(icon, FluentIcon):
if isinstance(icon, FluentIconBase):
icon.render(painter, rect)
else:
icon = QIcon(icon)
......@@ -92,7 +92,57 @@ def drawIcon(icon, painter:QPainter, rect):
painter.drawPixmap(rect, image)
class FluentIcon(Enum):
class FluentIconBase:
""" Fluent icon base class """
def path(self, theme=Theme.AUTO):
""" get the path of icon
Parameters
----------
theme: Theme
the theme of icon
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
"""
raise NotImplementedError
def icon(self, theme=Theme.AUTO):
""" create an fluent icon
Parameters
----------
theme: Theme
the theme of icon
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
"""
return QIcon(self.path(theme))
def render(self, painter, rect, theme=Theme.AUTO):
""" draw svg icon
Parameters
----------
painter: QPainter
painter
rect: QRect | QRectF
the rect to render icon
theme: Theme
the theme of icon
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
"""
drawSvgIcon(self.path(theme), painter, rect)
class FluentIcon(FluentIconBase, Enum):
""" Fluent icon """
WEB = "Web"
......@@ -134,16 +184,6 @@ class FluentIcon(Enum):
FLUORESCENT_PEN = "FluorescentPen"
def path(self, theme=Theme.AUTO):
""" get the path of icon
Parameters
----------
theme: Theme
the theme of icon
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
"""
if theme == Theme.AUTO:
c = getIconColor()
else:
......@@ -151,34 +191,3 @@ class FluentIcon(Enum):
return f':/qfluentwidgets/images/icons/{self.value}_{c}.svg'
def icon(self, theme=Theme.AUTO):
""" create an fluent icon
Parameters
----------
theme: Theme
the theme of icon
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
"""
return QIcon(self.path(theme))
def render(self, painter, rect, theme=Theme.AUTO):
""" draw svg icon
Parameters
----------
painter: QPainter
painter
rect: QRect | QRectF
the rect to render icon
theme: Theme
the theme of icon
* `Theme.Light`: black icon
* `Theme.DARK`: white icon
* `Theme.AUTO`: icon color depends on `config.theme`
"""
drawSvgIcon(self.path(theme), painter, rect)
\ No newline at end of file
......@@ -7,7 +7,7 @@ from PyQt5.QtWidgets import QWidget
from .navigation_panel import NavigationPanel, NavigationItemPostion, NavigationWidget, NavigationDisplayMode
from ...common.style_sheet import setStyleSheet
from ...common.icon import FluentIcon
from ...common.icon import FluentIconBase
class NavigationInterface(QWidget):
......@@ -33,7 +33,7 @@ class NavigationInterface(QWidget):
self.setAttribute(Qt.WA_StyledBackground)
setStyleSheet(self, 'navigation_interface')
def addItem(self, routeKey: str, icon: Union[str, QIcon, FluentIcon], text: str, onClick, selectable=True, position=NavigationItemPostion.TOP):
def addItem(self, routeKey: str, icon: Union[str, QIcon, FluentIconBase], text: str, onClick, selectable=True, position=NavigationItemPostion.TOP):
""" add navigation item
Parameters
......@@ -41,7 +41,7 @@ class NavigationInterface(QWidget):
routKey: str
the unique name of item
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon of navigation item
text: str
......
......@@ -9,7 +9,7 @@ from PyQt5.QtWidgets import QWidget, QVBoxLayout, QFrame, QApplication
from .navigation_widget import NavigationButton, MenuButton, NavigationWidget, NavigationSeparator
from ..widgets.scroll_area import ScrollArea
from ...common.style_sheet import setStyleSheet, getStyleSheet
from ...common.icon import FluentIcon
from ...common.icon import FluentIconBase
class NavigationDisplayMode(Enum):
......@@ -96,7 +96,7 @@ class NavigationPanel(QFrame):
self.topLayout.addWidget(self.menuButton, 0, Qt.AlignTop)
def addItem(self, routeKey: str, icon: Union[str, QIcon, FluentIcon], text: str, onClick, selectable=True, position=NavigationItemPostion.TOP):
def addItem(self, routeKey: str, icon: Union[str, QIcon, FluentIconBase], text: str, onClick, selectable=True, position=NavigationItemPostion.TOP):
""" add navigation item
Parameters
......@@ -104,7 +104,7 @@ class NavigationPanel(QFrame):
routeKey: str
the unique name of item
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon of navigation item
text: str
......@@ -289,7 +289,7 @@ class NavigationPanel(QFrame):
elif self.displayMode == NavigationDisplayMode.COMPACT:
self.setProperty('menu', False)
self.setStyle(QApplication.style())
for item in self.items.values():
item.setCompacted(True)
......
......@@ -77,7 +77,7 @@ class NavigationButton(NavigationWidget):
"""
Parameters
----------
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
text: str
......
......@@ -5,7 +5,7 @@ from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QButtonGroup, QLabel, QRadioButton
from ...common.config import OptionsConfigItem, qconfig
from ...common.icon import FluentIcon
from ...common.icon import FluentIconBase
from .expand_setting_card import ExpandSettingCard
......@@ -14,14 +14,14 @@ class OptionsSettingCard(ExpandSettingCard):
optionChanged = pyqtSignal(OptionsConfigItem)
def __init__(self, configItem, icon: Union[str, QIcon, FluentIcon], title, content=None, texts=None, parent=None):
def __init__(self, configItem, icon: Union[str, QIcon, FluentIconBase], title, content=None, texts=None, parent=None):
"""
Parameters
----------
configItem: OptionsConfigItem
options config item
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......
......@@ -13,17 +13,17 @@ from ..widgets.slider import Slider
from ..widgets.icon_widget import IconWidget
from ...common.style_sheet import setStyleSheet
from ...common.config import qconfig, isDarkTheme
from ...common.icon import FluentIcon
from ...common.icon import FluentIconBase
class SettingCard(QFrame):
""" Setting card """
def __init__(self, icon: Union[str, QIcon, FluentIcon], title, content=None, parent=None):
def __init__(self, icon: Union[str, QIcon, FluentIconBase], title, content=None, parent=None):
"""
Parameters
----------
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......@@ -84,11 +84,11 @@ class SwitchSettingCard(SettingCard):
checkedChanged = pyqtSignal(bool)
def __init__(self, icon: Union[str, QIcon, FluentIcon], title, content=None, configItem=None, parent=None):
def __init__(self, icon: Union[str, QIcon, FluentIconBase], title, content=None, configItem=None, parent=None):
"""
Parameters
----------
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......@@ -142,14 +142,14 @@ class RangeSettingCard(SettingCard):
valueChanged = pyqtSignal(int)
def __init__(self, configItem, icon: Union[str, QIcon, FluentIcon], title, content=None, parent=None):
def __init__(self, configItem, icon: Union[str, QIcon, FluentIconBase], title, content=None, parent=None):
"""
Parameters
----------
configItem: RangeConfigItem
configuration item operated by the card
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......@@ -193,14 +193,14 @@ class PushSettingCard(SettingCard):
clicked = pyqtSignal()
def __init__(self, text, icon: Union[str, QIcon, FluentIcon], title, content=None, parent=None):
def __init__(self, text, icon: Union[str, QIcon, FluentIconBase], title, content=None, parent=None):
"""
Parameters
----------
text: str
the text of push button
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......@@ -230,7 +230,7 @@ class PrimaryPushSettingCard(PushSettingCard):
class HyperlinkCard(SettingCard):
""" Hyperlink card """
def __init__(self, url, text, icon: Union[str, QIcon, FluentIcon], title, content=None, parent=None):
def __init__(self, url, text, icon: Union[str, QIcon, FluentIconBase], title, content=None, parent=None):
"""
Parameters
----------
......@@ -240,7 +240,7 @@ class HyperlinkCard(SettingCard):
text: str
text of url
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......@@ -315,14 +315,14 @@ class ColorSettingCard(SettingCard):
colorChanged = pyqtSignal(QColor)
def __init__(self, configItem, icon: Union[str, QIcon, FluentIcon], title, content=None, parent=None):
def __init__(self, configItem, icon: Union[str, QIcon, FluentIconBase], title, content=None, parent=None):
"""
Parameters
----------
configItem: RangeConfigItem
configuration item operated by the card
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......@@ -350,14 +350,14 @@ class ColorSettingCard(SettingCard):
class ComboBoxSettingCard(SettingCard):
""" Setting card with a combo box """
def __init__(self, configItem, icon: Union[str, QIcon, FluentIcon], title, content=None, texts=None, parent=None):
def __init__(self, configItem, icon: Union[str, QIcon, FluentIconBase], title, content=None, texts=None, parent=None):
"""
Parameters
----------
configItem: OptionsConfigItem
configuration item operated by the card
icon: str | QIcon | FluentIcon
icon: str | QIcon | FluentIconBase
the icon to be drawn
title: str
......
# coding:utf-8
from typing import Union
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QIcon, QPainter
from PyQt5.QtWidgets import QWidget
from ...common.icon import FluentIcon, drawIcon
from ...common.icon import FluentIconBase, drawIcon
class IconWidget(QWidget):
""" Icon widget """
def __init__(self, icon: Union[str, QIcon, FluentIcon], parent=None):
def __init__(self, icon: Union[str, QIcon, FluentIconBase], parent=None):
super().__init__(parent=parent)
self.icon = icon
def paintEvent(self, e):
painter = QPainter(self)
painter.setRenderHints(QPainter.Antialiasing|QPainter.SmoothPixmapTransform)
painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
drawIcon(self.icon, painter, self.rect())
......@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup(
name="PyQt-Fluent-Widgets",
version="0.3.0",
version="0.3.1",
keywords="pyqt fluent widgets",
author="zhiyiYo",
author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册