|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import math |
| 4 | +from contextlib import contextmanager |
4 | 5 | from functools import lru_cache |
5 | | -from typing import TYPE_CHECKING |
6 | 6 |
|
7 | | -from PyQt5.QtCore import QPointF, QRectF, Qt, QPoint |
8 | | -from PyQt5.QtGui import QPainter, QPainterPath, QColor, QLinearGradient |
9 | | - |
10 | | -if TYPE_CHECKING: |
11 | | - from PyQt5.QtGui import QFont, QPaintDevice |
12 | | - |
13 | | - from siui.typing import T_Brush, T_PenStyle, T_RenderHint |
14 | | - |
15 | | - |
16 | | -def createPainter( |
17 | | - paintDevice: QPaintDevice, |
18 | | - renderHint: T_RenderHint = QPainter.RenderHint.Antialiasing, |
19 | | - penStyle: T_PenStyle = Qt.PenStyle.NoPen, |
20 | | - brush: T_Brush = None, |
21 | | - font: QFont | None = None, |
22 | | -) -> QPainter: |
23 | | - """构造并初始化 QPainter 对象 |
24 | | - 应该使用 with 关键字来创建和关闭 QPainter 对象 |
25 | | -
|
26 | | - 参数: |
27 | | - - parent: QPaintDevice 的子类实例,通常是 QWidget 或 QImage |
28 | | - - renderHint: 指定渲染提示,默认为 QPainter.RenderHint.Antialiasing 标准抗锯齿 |
29 | | - - penStyle: Qt.PenStyle 类型,指定画笔样式,默认为 Qt.PenStyle.NoPen |
30 | | - - brushColor: 字符串或 QColor 对象,指定画刷颜色,默认不指定 |
31 | | - - font: QFont 对象,指定字体,默认不指定 |
32 | | -
|
33 | | - 返回: |
34 | | - QPainter 对象实例 |
35 | | - """ |
36 | | - painter = QPainter(paintDevice) |
37 | | - if renderHint is not None: |
38 | | - painter.setRenderHints(renderHint) |
39 | | - |
40 | | - if penStyle is not None: |
41 | | - painter.setPen(penStyle) |
42 | | - |
43 | | - if brush is not None: |
44 | | - painter.setBrush(brush) |
45 | | - |
46 | | - if font is not None: |
47 | | - painter.setFont(font) |
48 | | - |
49 | | - return painter |
| 7 | +from PyQt5.QtCore import QPoint, QPointF, QRectF, Qt |
| 8 | +from PyQt5.QtGui import QColor, QLinearGradient, QPaintDevice, QPainter, QPainterPath |
| 9 | + |
| 10 | + |
| 11 | +@contextmanager |
| 12 | +def createPainter(device: QPaintDevice, |
| 13 | + render_hints: QPainter.RenderHints = QPainter.Antialiasing) -> QPainter: |
| 14 | + painter = QPainter(device) |
| 15 | + painter.setRenderHints(render_hints) |
| 16 | + painter.setPen(Qt.NoPen) |
| 17 | + painter.setBrush(Qt.NoBrush) |
| 18 | + try: |
| 19 | + yield painter |
| 20 | + finally: |
| 21 | + painter.end() |
50 | 22 |
|
51 | 23 |
|
52 | 24 | @lru_cache(maxsize=None) |
|
0 commit comments