Skip to content

Commit

Permalink
Move DraggableVertex to its own file (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
BryceBeagle authored Aug 29, 2021
1 parent 8bdae81 commit 1e68e32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from PyQt5.QtGui import QColor

from brainframe_qt.ui.resources.video_items.base import CircleItem, VideoItem


class DraggableVertex(CircleItem):
# TODO: Tie to size of scene
DEFAULT_RADIUS = 10
DEFAULT_BORDER_THICKNESS = 5
DEFAULT_COLOR = QColor(200, 50, 50)

def __init__(self, position: VideoItem.PointType, *, parent: VideoItem):
super().__init__(position, color=self.DEFAULT_COLOR,
radius=self.DEFAULT_RADIUS,
border_thickness=self.DEFAULT_BORDER_THICKNESS,
parent=parent)
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from abc import ABC, abstractmethod
from typing import List, Optional
from typing import List, Optional, Dict

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor

from brainframe.api import bf_codecs

from brainframe_qt.ui.resources.config import RenderSettings
from brainframe_qt.ui.resources.video_items.base import CircleItem, VideoItem
from brainframe_qt.ui.resources.video_items.base import VideoItem
from brainframe_qt.ui.resources.video_items.zones import ZoneLineItem, ZoneRegionItem

from ..core.zone import Line, Region, Zone
from .draggable_vertex import DraggableVertex


class InProgressZoneItem(VideoItem, ABC):
Expand All @@ -29,6 +29,7 @@ def __init__(self, zone: Zone, *,
self._line_style = line_style

self._zone_item = self._init_zone_item()
self._vertex_items: Dict[VideoItem.PointType, DraggableVertex]
self._vertex_items = self._init_vertex_items()

@classmethod
Expand All @@ -55,20 +56,16 @@ def current_vertices(self) -> List[VideoItem.PointType]:

def add_vertex(self, vertex: VideoItem.PointType) -> None:
if not self.zone.takes_additional_points():
raise RuntimeError("This zone item does not support adding any "
"more points")
raise RuntimeError("This zone item does not support adding any more points")

self.zone.coords.append(vertex)

self.refresh_shape()

vertex_item = _DraggableVertex(vertex, parent=self)
self._vertex_items.append(vertex_item)

def refresh_shape(self) -> None:
if self._zone_item is not None and self.scene() is not None:
self.scene().removeItem(self._zone_item)
for item in self._vertex_items:
for item in self._vertex_items.values():
self.scene().removeItem(item)

self._zone_item = self._init_zone_item()
Expand All @@ -82,12 +79,13 @@ def _init_zone_item(self) -> Optional["InProgressZoneItem"]:
zone is not ready to be drawn yet
"""

def _init_vertex_items(self) -> List['_DraggableVertex']:
def _init_vertex_items(self) -> Dict[VideoItem.PointType, DraggableVertex]:
assert self.zone.coords is not None

vertex_items: List[_DraggableVertex] = []
vertex_items: Dict[VideoItem.PointType, DraggableVertex] = {}
for coord in self.zone.coords:
vertex_items.append(_DraggableVertex(coord, parent=self))
vertex_item = DraggableVertex(coord, parent=self)
vertex_items[coord] = vertex_item

return vertex_items

Expand Down Expand Up @@ -122,27 +120,3 @@ def _init_zone_item(self) -> Optional[ZoneLineItem]:
line_style=self._line_style,
parent=self
)


class _DraggableVertex(CircleItem):
# TODO: Tie to size of scene
DEFAULT_RADIUS = 10
DEFAULT_BORDER_THICKNESS = 5
DEFAULT_COLOR = QColor(200, 50, 50)

def __init__(self, position: VideoItem.PointType, *, parent: VideoItem):
super().__init__(position, color=self.DEFAULT_COLOR,
radius=self.DEFAULT_RADIUS,
border_thickness=self.DEFAULT_BORDER_THICKNESS,
parent=parent)

# self.setFlag(self.ItemIsSelectable, True)
# self.setFlag(self.ItemIsMovable, True)

# def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent) -> None:
# event.ignore()
# super().mouseMoveEvent(event)
#
# def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent) -> None:
# event.ignore()
# super().mouseMoveEvent(event)
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def mouseMoveEvent(self, event: QMouseEvent) -> None:

self._update_zone_items()

super().mouseMoveEvent(event)

def _handle_click(self, click_pos: VideoItem.PointType) -> None:
# Don't do anything if we're not currently making a zone
if self.in_progress_zone is None:
Expand Down

0 comments on commit 1e68e32

Please sign in to comment.