From de6fa95fd163f157440f9ac1178b9f6d2d00e948 Mon Sep 17 00:00:00 2001 From: rainzee Date: Sun, 17 Nov 2024 18:27:12 +0800 Subject: [PATCH] feat(core): impl `createPainter` --- siui/core/__init__.py | 30 ++++++++++++++++++--------- siui/core/painter.py | 47 +++++++++++++++++++++++++++++++++++++++++++ siui/typing.py | 11 ++++++++-- 3 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 siui/core/painter.py diff --git a/siui/core/__init__.py b/siui/core/__init__.py index 47f349b..8fbd86d 100644 --- a/siui/core/__init__.py +++ b/siui/core/__init__.py @@ -1,4 +1,3 @@ - from .alignment import SiQuickAlignmentManager from .animation import ( ABCSiAnimation, @@ -13,17 +12,28 @@ from .effect import SiQuickEffect from .enumrates import Si from .globals import NewGlobal, SiGlobal +from .painter import createPainter as createPainter from .token import FontStyle as FontStyle from .token import GlobalFont as GlobalFont from .token import GlobalFontSize as GlobalFontSize from .token import GlobalFontWeight as GlobalFontWeight -__all__ = ("SiQuickAlignmentManager", - "ABCSiAnimation", "Curve", "SiAnimationGroup", "SiCounterAnimation", - "SiExpAccelerateAnimation", "SiExpAnimation", "SiSqrExpAnimation", - "SiColor", - "SiQuickEffect", - "Si", - "NewGlobal", "SiGlobal", - "FontStyle", "GlobalFont", "GlobalFontSize", "GlobalFontWeight") - +__all__ = ( + "SiQuickAlignmentManager", + "ABCSiAnimation", + "Curve", + "SiAnimationGroup", + "SiCounterAnimation", + "SiExpAccelerateAnimation", + "SiExpAnimation", + "SiSqrExpAnimation", + "SiColor", + "SiQuickEffect", + "Si", + "NewGlobal", + "SiGlobal", + "FontStyle", + "GlobalFont", + "GlobalFontSize", + "GlobalFontWeight", +) diff --git a/siui/core/painter.py b/siui/core/painter.py new file mode 100644 index 0000000..ee876fa --- /dev/null +++ b/siui/core/painter.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Optional + +from PyQt5.QtCore import Qt +from PyQt5.QtGui import QPainter + +if TYPE_CHECKING: + from PyQt5.QtGui import QFont, QPaintDevice + + from siui.typing import T_Brush, T_PenStyle + + +def createPainter( + paintDevice: QPaintDevice, + renderHint: Optional[QPainter.RenderHint] = QPainter.RenderHint.Antialiasing, + penStyle: T_PenStyle = Qt.PenStyle.NoPen, + brush: T_Brush = None, + font: Optional[QFont] = None, +) -> QPainter: + """构造并初始化 QPainter 对象 + 应该使用 with 关键字来创建和关闭 QPainter 对象 + + 参数: + - parent: QPaintDevice 的子类实例,通常是 QWidget 或 QImage + - renderHint: 指定渲染提示,默认为 QPainter.RenderHint.Antialiasing 标准抗锯齿 + - penStyle: Qt.PenStyle 类型,指定画笔样式,默认为 Qt.PenStyle.NoPen + - brushColor: 字符串或 QColor 对象,指定画刷颜色,默认不指定 + - font: QFont 对象,指定字体,默认不指定 + + 返回: + QPainter 对象实例 + """ + painter = QPainter(paintDevice) + if renderHint is not None: + painter.setRenderHint(renderHint) + + if penStyle is not None: + painter.setPen(penStyle) + + if brush is not None: + painter.setBrush(brush) + + if font is not None: + painter.setFont(font) + + return painter diff --git a/siui/typing.py b/siui/typing.py index e033445..db76463 100644 --- a/siui/typing.py +++ b/siui/typing.py @@ -7,9 +7,10 @@ - [`PEP 526`](https://www.python.org/dev/peps/pep-0526/) """ -from typing import Optional +from typing import Optional, Union -from PyQt5.QtCore import QObject +from PyQt5.QtCore import QObject, Qt +from PyQt5.QtGui import QColor, QGradient, QPen from PyQt5.QtWidgets import QWidget from typing_extensions import TypeAlias @@ -18,3 +19,9 @@ T_ObjectParent: TypeAlias = Optional[QObject] """Type of object parent""" + +T_PenStyle: TypeAlias = Union[QPen, Qt.PenStyle, QColor, Qt.GlobalColor] +"""Type of QPen style""" + +T_Brush: TypeAlias = Optional[Union[QGradient, QColor, Qt.GlobalColor]] +"""Type of QBrush"""