|
28 | 28 | from plotpy.items.shape.base import AbstractShape |
29 | 29 | from plotpy.items.shape.ellipse import EllipseShape |
30 | 30 | from plotpy.items.shape.point import PointShape |
| 31 | +from plotpy.items.shape.polygon import PolygonShape |
31 | 32 | from plotpy.items.shape.rectangle import ObliqueRectangleShape, RectangleShape |
32 | 33 | from plotpy.items.shape.segment import SegmentShape |
33 | 34 | from plotpy.mathutils.geometry import ( |
@@ -461,9 +462,8 @@ def boundingRect(self) -> QRectF: |
461 | 462 |
|
462 | 463 | class AnnotatedPoint(AnnotatedShape): |
463 | 464 | """ |
464 | | - Construct an annotated point at coordinates (x, y) |
465 | | - with properties set with *annotationparam* |
466 | | - (see :py:class:`.styles.AnnotationParam`) |
| 465 | + Construct an annotated point at coordinates (x, y) with properties set with |
| 466 | + *annotationparam* (see :py:class:`.styles.AnnotationParam`) |
467 | 467 | """ |
468 | 468 |
|
469 | 469 | SHAPE_CLASS = PointShape |
@@ -575,6 +575,92 @@ def get_infos(self) -> str: |
575 | 575 | ) |
576 | 576 |
|
577 | 577 |
|
| 578 | +class AnnotatedPolygon(AnnotatedShape): |
| 579 | + """ |
| 580 | + Construct an annotated polygon with properties set with *annotationparam* |
| 581 | + (see :py:class:`.styles.AnnotationParam`) |
| 582 | +
|
| 583 | + Args: |
| 584 | + points: List of points |
| 585 | + closed: True if polygon is closed |
| 586 | + annotationparam: Annotation parameters |
| 587 | + """ |
| 588 | + |
| 589 | + SHAPE_CLASS = PolygonShape |
| 590 | + LABEL_ANCHOR = "C" |
| 591 | + |
| 592 | + def __init__( |
| 593 | + self, |
| 594 | + points: list[tuple[float, float]] | None = None, |
| 595 | + closed: bool | None = None, |
| 596 | + annotationparam: AnnotationParam | None = None, |
| 597 | + ) -> None: |
| 598 | + super().__init__(annotationparam) |
| 599 | + self.shape: PolygonShape |
| 600 | + if points is not None: |
| 601 | + self.set_points(points) |
| 602 | + if closed is not None: |
| 603 | + self.set_closed(closed) |
| 604 | + self.setIcon(get_icon("polygon.png")) |
| 605 | + |
| 606 | + # ----Public API------------------------------------------------------------- |
| 607 | + def set_points(self, points: list[tuple[float, float]] | None) -> None: |
| 608 | + """Set the polygon points |
| 609 | +
|
| 610 | + Args: |
| 611 | + points: List of point coordinates |
| 612 | + """ |
| 613 | + self.shape.set_points(points) |
| 614 | + self.set_label_position() |
| 615 | + |
| 616 | + def get_points(self) -> list[tuple[float, float]]: |
| 617 | + """Return the polygon points""" |
| 618 | + return self.shape.get_points() |
| 619 | + |
| 620 | + def set_closed(self, state: bool) -> None: |
| 621 | + """Set the polygon closed state |
| 622 | +
|
| 623 | + Args: |
| 624 | + state: True if polygon is closed |
| 625 | + """ |
| 626 | + self.shape.set_closed(state) |
| 627 | + |
| 628 | + def is_closed(self) -> bool: |
| 629 | + """Return True if polygon is closed |
| 630 | +
|
| 631 | + Returns: |
| 632 | + True if polygon is closed |
| 633 | + """ |
| 634 | + return self.shape.is_closed() |
| 635 | + |
| 636 | + # ----AnnotatedShape API----------------------------------------------------- |
| 637 | + def create_shape(self): |
| 638 | + """Return the shape object associated to this annotated shape object""" |
| 639 | + shape = self.SHAPE_CLASS() # pylint: disable=not-callable |
| 640 | + return shape |
| 641 | + |
| 642 | + def set_label_position(self) -> None: |
| 643 | + """Set label position, for instance based on shape position""" |
| 644 | + x, y = self.shape.get_center() |
| 645 | + self.label.set_pos(x, y) |
| 646 | + |
| 647 | + def get_tr_center(self) -> tuple[float, float]: |
| 648 | + """Return shape center coordinates after applying transform matrix""" |
| 649 | + return self.shape.get_center() |
| 650 | + |
| 651 | + def get_infos(self) -> str: |
| 652 | + """Get informations on current shape |
| 653 | +
|
| 654 | + Returns: |
| 655 | + str: Formatted string with informations on current shape |
| 656 | + """ |
| 657 | + return "<br>".join( |
| 658 | + [ |
| 659 | + _("Center:") + " " + self.get_tr_center_str(), |
| 660 | + ] |
| 661 | + ) |
| 662 | + |
| 663 | + |
578 | 664 | class AnnotatedRectangle(AnnotatedShape): |
579 | 665 | """ |
580 | 666 | Construct an annotated rectangle between coordinates (x1, y1) and |
|
0 commit comments