Skip to content

Commit

Permalink
persist path
Browse files Browse the repository at this point in the history
  • Loading branch information
zkovari committed Oct 7, 2024
1 parent ca7ab79 commit dc73c16
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/main/python/plotlyst/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,12 @@ def __hash__(self):
return hash(self.key)


@dataclass
class Point:
x: float
y: float


@dataclass
class WorldBuildingMarker:
x: float
Expand All @@ -2191,6 +2197,7 @@ class WorldBuildingMarker:
height: int = field(default=0, metadata=config(exclude=exclude_if_empty))
width: int = field(default=0, metadata=config(exclude=exclude_if_empty))
ref: Optional[uuid.UUID] = field(default=None, metadata=config(exclude=exclude_if_empty))
points: List[Point] = field(default_factory=list, metadata=config(exclude=exclude_if_empty))


@dataclass
Expand Down
36 changes: 26 additions & 10 deletions src/main/python/plotlyst/view/widget/world/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from qtpy import sip

from plotlyst.common import PLOTLYST_SECONDARY_COLOR, RELAXED_WHITE_COLOR, PLOTLYST_TERTIARY_COLOR, PLOTLYST_MAIN_COLOR
from plotlyst.core.domain import Novel, WorldBuildingMap, WorldBuildingMarker, GraphicsItemType, Location
from plotlyst.core.domain import Novel, WorldBuildingMap, WorldBuildingMarker, GraphicsItemType, Location, Point
from plotlyst.resources import resource_registry
from plotlyst.service.cache import entities_registry
from plotlyst.service.image import load_image, upload_image, LoadedImage
Expand Down Expand Up @@ -531,12 +531,11 @@ def addPoint(self, point: QPointF):
path.moveTo(point)
self._last_point = point
else:
distance = self.distance(self._last_point, point)
if distance >= self._threshold:
path.lineTo(point)
self._last_point = point
else:
print(f'ignore {point}')
if self._distance(self._last_point, point) < self._threshold:
return
path.lineTo(point)
self._last_point = point
self._marker.points.append(Point(int(point.x()), int(point.y())))

self.setPath(path)

Expand All @@ -547,9 +546,21 @@ def finish(self, point: QPointF):

self._last_point = None

def distance(self, p1: QPointF, p2: QPointF) -> float:
def _distance(self, p1: QPointF, p2: QPointF) -> float:
return math.hypot(p2.x() - p1.x(), p2.y() - p1.y())

@overrides
def _posChangedOnTimeout(self):
self._posChangedTimer.stop()
self._marker.x += self.scenePos().x()
self._marker.y += self.scenePos().y()
for point in self._marker.points:
point.x += self.scenePos().x()
point.y += self.scenePos().y()
scene = self.mapScene()
if scene:
scene.markerChangedEvent(self)


class AreaSelectorWidget(SecondarySelectorWidget):
def __init__(self, parent=None):
Expand Down Expand Up @@ -658,6 +669,12 @@ def loadMap(self, map: WorldBuildingMap) -> Optional[QGraphicsPixmapItem]:
elif marker.type == GraphicsItemType.MAP_AREA_CIRCLE:
rect = QRectF(marker.x, marker.y, marker.width, marker.height)
markerItem = AreaCircleItem(marker, rect)
elif marker.type == GraphicsItemType.MAP_AREA_CUSTOM:
path = QPainterPath(QPointF(marker.x, marker.y))
for point in marker.points:
path.lineTo(point.x, point.y)
path.lineTo(marker.x, marker.y)
markerItem = AreaCustomPathItem(marker, path)
else:
continue
self.addItem(markerItem)
Expand Down Expand Up @@ -694,7 +711,6 @@ def mouseMoveEvent(self, event: 'QGraphicsSceneMouseEvent') -> None:
def mouseReleaseEvent(self, event: 'QGraphicsSceneMouseEvent') -> None:
if self._current_area_item and self._additionDescriptor == GraphicsItemType.MAP_AREA_CUSTOM:
self._current_area_item.finish(self._area_start_point)
print(self._current_area_item.path().elementCount())
self.repo.update_world(self._novel)

self._area_start_point = None
Expand Down Expand Up @@ -751,7 +767,7 @@ def _addArea(self, pos: QPointF):
self._area_start_point = pos
marker = WorldBuildingMarker(self._area_start_point.x(), self._area_start_point.y(),
type=self._additionDescriptor)
# self._map.markers.append(marker)
self._map.markers.append(marker)
if self._additionDescriptor == GraphicsItemType.MAP_AREA_SQUARE:
self._current_area_item = AreaSquareItem(marker, QRectF(self._area_start_point, self._area_start_point))
elif self._additionDescriptor == GraphicsItemType.MAP_AREA_CIRCLE:
Expand Down

0 comments on commit dc73c16

Please sign in to comment.