Browse Source

Move attributes to the point itself

tb-init-ui-render
Taylor Bockman 5 years ago
parent
commit
c029d9e8b6
  1. 70
      clusterview/points.py
  2. 1
      main_window.py
  3. 7
      tests/test_point.py
  4. 24
      tests/test_point_set.py

70
clusterview/points.py

@ -29,17 +29,14 @@ class Point:
self.__viewport_width = viewport_width
self.__viewport_height = viewport_height
half_point = floor(point_size / 2.0)
self.__calculate_hitbox()
self.__check_window_bounds(x, y)
self.__selected = False
self.__top_left_corner = (self.__x - half_point,
self.__y + half_point)
self.__attributes = []
self.__bottom_right_corner = (self.__x + half_point,
self.__y - half_point)
@property
def x(self):
return self.__x
@ -56,6 +53,26 @@ class Point:
def selected(self):
return self.__selected
@property
def attributes(self):
return self.__attributes
def add_attribute(self, attr):
self.__attributes.append(attr)
def __calculate_hitbox(self):
"""
Calculates the hit box for the point given the current
position (center) and the point size.
"""
half_point = floor(self.point_size / 2.0)
self.__top_left_corner = (self.__x - half_point,
self.__y + half_point)
self.__bottom_right_corner = (self.__x + half_point,
self.__y - half_point)
def __check_window_bounds(self, x, y):
"""
Simple window bound check that raises an exception when
@ -90,7 +107,11 @@ class Point:
self.__x += dx
self.__y += dy
# It's important to note as we move the point we need to
# make sure we are constantly updating it's hitbox.
self.__calculate_hitbox()
def __eq__(self, other):
"""
Override for class equality.
@ -115,7 +136,7 @@ class Point:
s = "<POINT "
s += "X: {} | Y: {} | ".format(self.__x, self.__y)
s += "SIZE: {} | ".format(self.__point_size)
s += "VIEWPORT_WIDTH: {} |".format(self.__viewport_width)
s += "VIEWPORT_WIDTH: {} | ".format(self.__viewport_width)
s += "VIEWPORT_HEIGHT: {}".format(self.__viewport_height)
s += ">"
@ -186,7 +207,6 @@ class PointSet:
calculations.
"""
self.__points = set()
self.__attributes = {}
self.__point_size = point_size
self.__viewport_width = viewport_width
self.__viewport_height = viewport_height
@ -203,9 +223,22 @@ class PointSet:
attributes_equal = (attributes_equal and
(len(self.__points) == len(other_points)))
# This is O(N^2) - can it be improved using some sort of
# find function?
for p in self.__points:
attributes_equal = (attributes_equal and
(self.__attributes[p] == other.attributes(p)))
for i, op in enumerate(other_points):
if p == op:
attributes_equal = (attributes_equal and
(p.attributes == op.attributes))
continue
if i == len(other_points) - 1:
# In this case we have enumerated the entire second
# set and not found anything. We can safely say
# the two sets are not equal and return.
attribute_equals = False
break
return (self.__points == other_points and
attributes_equal and
@ -279,8 +312,11 @@ class PointSet:
point = Point(x, y, self.__point_size,
self.__viewport_width, self.__viewport_height)
for attr in attrs:
point.add_attribute(attr)
self.__points.add(point)
self.__attributes[point] = attrs
def remove_point(self, x, y):
"""
@ -304,15 +340,3 @@ class PointSet:
# In place set difference
self.__points = self.__points - matched
# Remove associated attributes
for point in matched:
self.__attributes.pop(point)
def attributes(self, point):
"""
Returns the attribute array for a given point.
@param point The point to get attributes for..
"""
return self.__attributes[point]

1
main_window.py

@ -172,7 +172,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
"""
print("LAUNCHING SOLVE DIALOG...")
@handle_exceptions
def __ogl_click_dispatcher(self, event):
"""

7
tests/test_point.py

@ -1,7 +1,7 @@
import pytest
from clusterview.exceptions import ExceededWindowBoundsError
from clusterview.points import Point
from clusterview.points import Point, PointSet
def test_move_point():
# The minimum starting position is 1/2 point away
@ -12,6 +12,11 @@ def test_move_point():
assert p.x == 5 and p.y == 5
def test_attributes_must_be_array_of_attributes():
with pytest.raises(ValueError):
l = PointSet(8, 100, 100)
l.add_point(4, 4, attrs=[1,2,3,4,5])
def test_move_point_outside_screen_x_positive():
p = Point(4, 4, 8, 100, 100)

24
tests/test_point_set.py

@ -18,7 +18,6 @@ def test_add_to_point_set():
p = Point(1, 2, 3, 100, 100)
assert len(points) == 1
assert points[0] == p
assert len(l.attributes(p)) == 0
def test_add_to_point_set_with_attributes():
attribute = Attribute("thing", 1)
@ -28,11 +27,9 @@ def test_add_to_point_set_with_attributes():
points = list(l.points)
point = Point(2, 3, 3, 100, 100)
attrs = l.attributes(point)
assert len(points) == 1
assert points[0] == point
assert len(l.attributes(point)) == 1
assert len(points[0].attributes) == 1
def test_remove_point_exact_click():
attribute = Attribute("thing", 1)
@ -47,13 +44,6 @@ def test_remove_point_exact_click():
assert len(points) == 0
with pytest.raises(KeyError):
# We expect a call to attributes on a removed
# point to raise a KeyError because it no
# longer exists in the point -> attribute_list
# dictionary.
l.attributes(p)
def test_remove_point_bounding_box():
"""
This test checks the bounding box hit heuristic.
@ -73,18 +63,6 @@ def test_remove_point_bounding_box():
assert len(points) == 0
with pytest.raises(KeyError):
# We expect a call to attributes on a removed
# point to raise a KeyError because it no
# longer exists in the point -> attribute_list
# dictionary.
l.attributes(p)
def test_attributes_must_be_array_of_attributes():
with pytest.raises(ValueError):
l = PointSet(8, 100, 100)
l.add_point(4, 4, attrs=[1,2,3,4,5])
def test_clear_all_selected_points():
l = PointSet(8, 100, 100)
l.add_point(4, 4)

Loading…
Cancel
Save