Skip to content

Polygon

Base classes

Name Children Inherits
SerializeMixin
prettyqt.utils.serializemixin
QPolygon
PySide6.QtGui
QPolygon(self, /) -> None

⋔ Inheritance diagram

graph TD
  2306009724["gui.Polygon"]
  69336483["utils.SerializeMixin"]
  2751680898["builtins.object"]
  1522866559["QtGui.QPolygon"]
  37984305["Shiboken.Object"]
  69336483 --> 2306009724
  2751680898 --> 69336483
  1522866559 --> 2306009724
  37984305 --> 1522866559
  2751680898 --> 37984305

🛈 DocStrings

Bases: SerializeMixin, QPolygon

List of points using integer precision.

Source code in prettyqt/gui/polygon.py
class Polygon(serializemixin.SerializeMixin, QtGui.QPolygon):
    """List of points using integer precision."""

    def __repr__(self):
        return f"{type(self).__name__}(<{len(self)} points>)"

    def __iter__(self) -> Iterator[core.Point]:
        return iter(self.get_point(i) for i in range(self.size()))

    def __len__(self) -> int:
        return self.size()

    def __bool__(self):
        return not self.isEmpty()

    def __contains__(self, point: datatypes.PointType) -> bool:
        if isinstance(point, tuple):
            point = core.Point(*point)
        return self.containsPoint(point, constants.FillRule.OddEvenFill)

    def __getitem__(self, index: int) -> core.Point:
        size = self.size()
        if index < 0:
            index += size
        if index < 0 or index >= size:
            raise IndexError(index)
        return self.get_point(index)

    def __setitem__(self, index: int, value: datatypes.PointType):
        p = core.Point(*value) if isinstance(value, tuple) else value
        # PySide6 workaround: setPoint does not exist
        self.remove(index)
        self.insert(index, p)

    def __sub__(self, other: QtGui.QPolygon) -> Polygon:
        return Polygon(self.subtracted(other))

    def __and__(self, other: QtGui.QPolygon) -> Polygon:  # &
        return Polygon(self.intersected(other))

    def __xor__(self, other: QtGui.QPolygon) -> Polygon:  # ^
        union = self | other
        intersect = self & other
        return union - intersect

    def __or__(self, other: QtGui.QPolygon) -> Polygon:  # |
        return Polygon(self.united(other))

    def get_point(self, index: int) -> core.Point:
        # PySide6 doesnt have self.point method
        return core.Point(self.value(index))

    def get_points(self) -> list[core.Point]:
        return [self.get_point(i) for i in range(self.size())]

    def add_points(self, *points: datatypes.PointType):
        for p in points:
            point = core.Point(*p) if isinstance(p, tuple) else p
            self.append(point)

    def get_data_buffer(self, size: int):
        self.resize(size)
        if API == "pyside6":
            import shiboken6

            address = shiboken6.getCppPointer(self.data())
            buffer = (ctypes.c_int * 2 * self.size()).from_address(address[0])
        else:
            buffer = self.data()
            buffer.setsize(8 * self.size())  # ty: ignore[missing-attribute]  # sip.voidptr
        return buffer

    @classmethod
    def from_xy(cls, xdata, ydata) -> Self:
        import numpy as np

        size = len(xdata)
        polyline = cls()
        buffer = polyline.get_data_buffer(size)
        # QPoint is two 32-bit ints
        memory = np.frombuffer(buffer, np.int32)
        memory[: (size - 1) * 2 + 1 : 2] = np.asarray(xdata, dtype=np.int32)
        memory[1 : (size - 1) * 2 + 2 : 2] = np.asarray(ydata, dtype=np.int32)
        return polyline

:fa-brands-github: Show source on GitHub