Skip to content

Polygon

Qt Base Class: QPolygon

Signature: QPolygon(self) -> None QPolygon(self, QPolygon: Union[PySide6.QtGui.QPolygon, Sequence[PySide6.QtCore.QPoint], PySide6.QtCore.QRect]) -> None QPolygon(self, r: PySide6.QtCore.QRect, closed: bool = False) -> None QPolygon(self, v: Sequence[PySide6.QtCore.QPoint]) -> None

Base classes

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

⋔ Inheritance diagram

graph TD
  1473245644128["gui.Polygon"]
  1473299806240["utils.SerializeMixin"]
  140713234304496["builtins.object"]
  1473300041376["QtGui.QPolygon"]
  1473291690208["Shiboken.Object"]
  1473299806240 --> 1473245644128
  140713234304496 --> 1473299806240
  1473300041376 --> 1473245644128
  1473291690208 --> 1473300041376
  140713234304496 --> 1473291690208

🛈 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:
        if index >= self.size():
            raise KeyError(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_long * 2 * self.size()).from_address(address[0])
        else:
            buffer = self.data()
            buffer.setsize(8 * self.size())
        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)
        memory = np.frombuffer(buffer, np.float64)
        memory[: (size - 1) * 2 + 1 : 2] = np.array(xdata, dtype=np.float64, copy=False)
        memory[1 : (size - 1) * 2 + 2 : 2] = np.array(ydata, dtype=np.float64, copy=False)
        return polyline