Remove normalize() code

One less method to worry about.
This commit is contained in:
Arjan Molenaar 2020-10-24 17:32:36 +02:00
parent 8ca36bdefd
commit 1a1435a4ef
3 changed files with 6 additions and 94 deletions

View File

@ -431,10 +431,6 @@ class Canvas:
if len(dirty_items) != len(self._dirty_items):
dirty_items = list(reversed(sort(self._dirty_items)))
# normalize items, which changed after constraint solving;
# recalculate matrices of normalized items
dirty_matrix_items.update(self._normalize(dirty_items))
for d in dirty_matrix_items:
d.matrix_i2c.set(*self.get_matrix_i2c(d))
@ -479,40 +475,6 @@ class Canvas:
return changed
def _normalize(self, items):
"""Update handle positions of items, so the first handle is always
located at (0, 0).
Return those items, which matrices changed due to first handle
movement.
For example having an item
>>> from gaphas.item import Element
>>> c = Canvas()
>>> e = Element()
>>> c.add(e)
>>> e.min_width = e.min_height = 0
>>> c.update_now()
>>> e.handles()
[<Handle object on (0, 0)>, <Handle object on (10, 0)>, <Handle object on (10, 10)>, <Handle object on (0, 10)>]
and moving its first handle a bit
>>> e.handles()[0].pos.x += 1
>>> list(map(float, e.handles()[0].pos))
[1.0, 0.0]
After normalization
>>> c._normalize([e]) # doctest: +ELLIPSIS
{<gaphas.item.Element object at 0x...>}
>>> e.handles()
[<Handle object on (0, 0)>, <Handle object on (9, 0)>, <Handle object on (9, 10)>, <Handle object on (0, 10)>]
"""
dirty_matrix_items = {item for item in items if item.normalize()}
return self.update_matrices(dirty_matrix_items)
def register_view(self, view):
"""Register a view on this canvas.

View File

@ -123,39 +123,6 @@ class Item:
"""
pass
def normalize(self):
"""Update handle positions of the item, so the first handle is always
located at (0, 0).
Note that, since this method basically does some housekeeping
during the update phase, there's no need to keep track of the
changes.
Alternative implementation can also be created, e.g. set (0,
0) in the center of a circle or change it depending on the
location of a rotation point.
Returns ``True`` if some updates have been done, ``False``
otherwise.
See ``canvas._normalize()`` for tests.
"""
updated = False
handles = self._handles
if handles:
x, y = list(map(float, handles[0].pos))
if x:
self.matrix.translate(x, 0)
updated = True
for h in handles:
h.pos.x -= x
if y:
self.matrix.translate(0, y)
updated = True
for h in handles:
h.pos.y -= y
return updated
def draw(self, context):
"""Render the item to a canvas view. Context contains the following
attributes:
@ -314,23 +281,6 @@ class Element(Item):
height = property(_get_height, _set_height)
def normalize(self):
"""Normalize only NW and SE handles."""
updated = False
handles = (self._handles[NW], self._handles[SE])
x, y = list(map(float, handles[0].pos))
if x:
self.matrix.translate(x, 0)
updated = True
for h in handles:
h.pos.x -= x
if y:
self.matrix.translate(0, y)
updated = True
for h in handles:
h.pos.y -= y
return updated
def point(self, pos):
"""Distance from the point (x, y) to the item.

View File

@ -67,8 +67,8 @@ def test_line_guide(win):
guides = list(Guide(win.line).vertical())
assert 2 == len(guides)
assert 00.0 == guides[0]
assert 20.0 == guides[1]
assert 10.0 == guides[0]
assert 30.0 == guides[1]
def test_line_guide_horizontal(win):
@ -81,13 +81,13 @@ def test_line_guide_horizontal(win):
guides = list(Guide(win.line).horizontal())
assert 2 == len(guides)
assert 0.0 == guides[0]
assert 20.0 == guides[1]
assert 10.0 == guides[0]
assert 30.0 == guides[1]
guides = list(Guide(win.line).horizontal())
assert 2 == len(guides)
assert 0.0 == guides[0]
assert 20.0 == guides[1]
assert 10.0 == guides[0]
assert 30.0 == guides[1]
def test_guide_item_in_motion(win):