|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# For licensing and distribution details, please read carefully xgrid/__init__.py |
| 4 | + |
| 5 | +# pylint: disable=invalid-name # Allows short reference names like x, y, ... |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from qtpy import QtCore as QC |
| 10 | +from qtpy import QtGui as QG |
| 11 | +from qtpy import QtSvg as QS |
| 12 | +from qwt.scale_map import QwtScaleMap |
| 13 | +from qwt.symbol import QwtSymbol |
| 14 | + |
| 15 | +from plotpy.items import shapes |
| 16 | +from plotpy.styles import ShapeParam |
| 17 | + |
| 18 | + |
| 19 | +class RectangleSVGShape(shapes.RectangleShape): |
| 20 | + """Rectangle SVG shape |
| 21 | +
|
| 22 | + Args: |
| 23 | + x1: X coordinate of the top-left corner |
| 24 | + y1: Y coordinate of the top-left corner |
| 25 | + x2: X coordinate of the bottom-right corner |
| 26 | + y2: Y coordinate of the bottom-right corner |
| 27 | + svg_data: SVG bytes |
| 28 | + shapeparam: Shape parameters |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + x1: float = 0.0, |
| 34 | + y1: float = 0.0, |
| 35 | + x2: float = 0.0, |
| 36 | + y2: float = 0.0, |
| 37 | + svg_data: bytes | None = None, |
| 38 | + shapeparam: ShapeParam = None, |
| 39 | + ) -> None: |
| 40 | + self.svg_data = svg_data |
| 41 | + shapes.RectangleShape.__init__(self, x1, y1, x2, y2, shapeparam) |
| 42 | + |
| 43 | + def set_data(self, svg_data: bytes) -> None: |
| 44 | + """Set SVG data""" |
| 45 | + self.svg_data = svg_data |
| 46 | + |
| 47 | + def draw( |
| 48 | + self, |
| 49 | + painter: QG.QPainter, |
| 50 | + xMap: QwtScaleMap, |
| 51 | + yMap: QwtScaleMap, |
| 52 | + canvasRect: QC.QRectF, |
| 53 | + ) -> None: |
| 54 | + """Draw shape (reimplement shapes.Shape.draw))""" |
| 55 | + points = self.transform_points(xMap, yMap) |
| 56 | + renderer = QS.QSvgRenderer(self.svg_data) |
| 57 | + renderer.render(painter, points.boundingRect()) |
| 58 | + |
| 59 | + |
| 60 | +class SquareSVGShape(RectangleSVGShape): |
| 61 | + """Square SVG shape |
| 62 | +
|
| 63 | + Args: |
| 64 | + svg_data: SVG bytes |
| 65 | + x1: X coordinate of the top-left corner |
| 66 | + y1: Y coordinate of the top-left corner |
| 67 | + x2: X coordinate of the bottom-right corner |
| 68 | + y2: Y coordinate of the bottom-right corner |
| 69 | + shapeparam: Shape parameters |
| 70 | + """ |
| 71 | + |
| 72 | + def move_point_to(self, handle: int, pos: tuple[float], ctrl: bool = None): |
| 73 | + """Reimplement RectangleShape.move_point_to""" |
| 74 | + nx, ny = pos |
| 75 | + x1, y1, x2, y2 = self.get_rect() |
| 76 | + if handle == 0: |
| 77 | + self.set_rect(nx, y2 - (x2 - nx), x2, y2) |
| 78 | + elif handle == 1: |
| 79 | + self.set_rect(x1, y2 - (nx - x1), nx, y2) |
| 80 | + elif handle == 2: |
| 81 | + self.set_rect(x1, y1, nx, y1 + (nx - x1)) |
| 82 | + elif handle == 3: |
| 83 | + self.set_rect(nx, y1, x2, y1 + (x2 - nx)) |
| 84 | + elif handle == -1: |
| 85 | + delta = (nx, ny) - self.points.mean(axis=0) |
| 86 | + self.points += delta |
| 87 | + |
| 88 | + |
| 89 | +class CircleSVGShape(shapes.EllipseShape): |
| 90 | + """Circle SVG shape |
| 91 | +
|
| 92 | + Args: |
| 93 | + x1: X coordinate of the top-left corner |
| 94 | + y1: Y coordinate of the top-left corner |
| 95 | + x2: X coordinate of the bottom-right corner |
| 96 | + y2: Y coordinate of the bottom-right corner |
| 97 | + svg_data: SVG bytes |
| 98 | + shapeparam: Shape parameters |
| 99 | + """ |
| 100 | + |
| 101 | + def __init__( |
| 102 | + self, |
| 103 | + x1: float = 0.0, |
| 104 | + y1: float = 0.0, |
| 105 | + x2: float = 0.0, |
| 106 | + y2: float = 0.0, |
| 107 | + svg_data: bytes | None = None, |
| 108 | + shapeparam: ShapeParam = None, |
| 109 | + ) -> None: |
| 110 | + self.svg_data = svg_data |
| 111 | + shapes.EllipseShape.__init__(self, x1, y1, x2, y2, shapeparam) |
| 112 | + |
| 113 | + def set_data(self, svg_data: bytes) -> None: |
| 114 | + """Set SVG data""" |
| 115 | + self.svg_data = svg_data |
| 116 | + |
| 117 | + def draw( |
| 118 | + self, |
| 119 | + painter: QG.QPainter, |
| 120 | + xMap: QwtScaleMap, |
| 121 | + yMap: QwtScaleMap, |
| 122 | + canvasRect: QC.QRect, |
| 123 | + ) -> None: |
| 124 | + """Draw shape (reimplement shapes.Shape.draw))""" |
| 125 | + points, line0, line1, rect = self.compute_elements(xMap, yMap) |
| 126 | + if canvasRect.intersects(rect.toRect()) and self.svg_data is not None: |
| 127 | + pen, brush, symbol = self.get_pen_brush(xMap, yMap) |
| 128 | + painter.setRenderHint(QG.QPainter.Antialiasing) |
| 129 | + painter.setPen(pen) |
| 130 | + painter.setBrush(brush) |
| 131 | + painter.drawLine(line0) |
| 132 | + painter.drawLine(line1) |
| 133 | + painter.save() |
| 134 | + painter.translate(rect.center()) |
| 135 | + painter.rotate(-line0.angle()) |
| 136 | + painter.translate(-rect.center()) |
| 137 | + renderer = QS.QSvgRenderer(self.svg_data) |
| 138 | + renderer.render(painter, rect) |
| 139 | + painter.restore() |
| 140 | + if symbol != QwtSymbol.NoSymbol: |
| 141 | + for i in range(points.size()): |
| 142 | + symbol.drawSymbol(painter, points[i].toPoint()) |
0 commit comments