Skip to content

Commit

Permalink
qtpy can be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
haiiliin committed Dec 7, 2024
1 parent 854c582 commit c85d945
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
16 changes: 10 additions & 6 deletions svgdatagrabber/geometry/geometrybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import sys
from abc import ABC
from enum import IntEnum
from typing import Tuple

from qtpy.QtCore import Qt
from qtpy.QtGui import QPen
from qtpy.QtWidgets import QApplication, QGraphicsScene
from typing import TYPE_CHECKING, Tuple

from ..graphics.annotations import QBrushType, QGraphicsItemType, QPenType

if TYPE_CHECKING:
from qtpy.QtWidgets import QGraphicsScene


class GeometryDrawAs(IntEnum):
"""Geometry draw as enum."""
Expand Down Expand Up @@ -58,7 +57,7 @@ def drawArgs(self) -> Tuple[str, tuple]:

def draw(
self,
scene: QGraphicsScene,
scene: "QGraphicsScene",
pen: QPenType = None,
brush: QBrushType = None,
item: QGraphicsItemType = None,
Expand All @@ -71,6 +70,8 @@ def draw(
brush: The brush to draw with.
item: The old item to draw on, if any.
"""
from qtpy.QtGui import QPen

args = self.drawArgs
if self.drawAs == DrawAsLine:
item and item.setLine(*args) or item or (item := scene.addLine(*args))
Expand All @@ -94,6 +95,9 @@ def plot(self, pen: QPenType = None, brush: QBrushType = None, fit: bool = True)
brush: The brush to draw with.
fit: Fit the view to the geometry.
"""
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QApplication

from ..graphics.graphicsview import GraphicsView

app = QApplication(sys.argv)
Expand Down
10 changes: 7 additions & 3 deletions svgdatagrabber/geometry/point.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

from typing import Iterable, Tuple, Union
from typing import TYPE_CHECKING, Iterable, Tuple, Union

import numpy as np
from qtpy.QtCore import QLineF

from .geometrybase import DrawAsEllipse, GeometryBase

if TYPE_CHECKING:
from qtpy.QtCore import QLineF

PointType = Union["Point", Iterable[float], complex]


Expand Down Expand Up @@ -312,6 +314,8 @@ def asvector(cls, v: "Vector" | PointType) -> "Vector":
return cls.aspoint(v)

@property
def drawArgs(self) -> QLineF:
def drawArgs(self) -> "QLineF":
"""Convert the vector to a Qt line."""
from qtpy.QtCore import QLineF

return QLineF(0.0, 0.0, self.x, self.y)
12 changes: 8 additions & 4 deletions svgdatagrabber/geometry/polygon.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

from typing import Iterable, List, Tuple
from typing import TYPE_CHECKING, Iterable, List, Tuple

from qtpy.QtCore import QPointF
from qtpy.QtGui import QPolygonF
from shapely.geometry import Polygon as ShapelyPolygon

from .closedshape import ClosedShape
Expand All @@ -13,6 +11,9 @@
from .sequence import PointSequence
from .straightline import LineRay, LineSegment

if TYPE_CHECKING:
from qtpy.QtGui import QPolygonF


class Polygon(ClosedShape, PointSequence):
"""A class representing a polygon."""
Expand Down Expand Up @@ -244,6 +245,9 @@ def asShapely(self) -> ShapelyPolygon:
return ShapelyPolygon(((vertex.x, vertex.y) for vertex in self.vertices))

@property
def drawArgs(self) -> Tuple[QPolygonF]:
def drawArgs(self) -> Tuple["QPolygonF"]:
"""Return the polygon as a Qt polygon."""
from qtpy.QtCore import QPointF
from qtpy.QtGui import QPolygonF

return (QPolygonF([QPointF(vertex.x, vertex.y) for vertex in self.vertices]),)
10 changes: 7 additions & 3 deletions svgdatagrabber/geometry/straightline.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from __future__ import annotations

from typing import Iterator
from typing import TYPE_CHECKING, Iterator

import numpy as np
from qtpy.QtCore import QLineF
from svgpathtools import Line as SvgPathToolsLine

from .exceptions import NotDrawableGeometryError
from .linebase import StraightLineBase
from .point import Point, PointType, Vector

if TYPE_CHECKING:
from qtpy.QtCore import QLineF


class Line(StraightLineBase):
"""A class representing a line."""
Expand Down Expand Up @@ -739,8 +741,10 @@ def reverse(self) -> "LineSegment":
return self

@property
def drawArgs(self) -> QLineF:
def drawArgs(self) -> "QLineF":
"""Get the Qt representation of this line segment."""
from qtpy.QtCore import QLineF

return QLineF(self.start.x, self.start.y, self.end.x, self.end.y)


Expand Down

0 comments on commit c85d945

Please sign in to comment.