Skip to content

Commit

Permalink
feat(core): impl createPainter
Browse files Browse the repository at this point in the history
  • Loading branch information
rainzee committed Nov 17, 2024
1 parent 46dd2c1 commit de6fa95
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
30 changes: 20 additions & 10 deletions siui/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from .alignment import SiQuickAlignmentManager
from .animation import (
ABCSiAnimation,
Expand All @@ -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",
)
47 changes: 47 additions & 0 deletions siui/core/painter.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions siui/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"""

0 comments on commit de6fa95

Please sign in to comment.