Remove variable sharing hack from Position
Now, resolve it with constraints.
This commit is contained in:
parent
928e82397c
commit
422d561955
@ -16,9 +16,7 @@ def deprecated(message, since):
|
|||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
f"{func.__name__}: {message}",
|
f"{func.__name__}: {message}", category=DeprecationWarning, stacklevel=2
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
)
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
@ -56,22 +54,6 @@ class Position:
|
|||||||
|
|
||||||
pos = property(lambda s: (s.x, s.y), _set_pos)
|
pos = property(lambda s: (s.x, s.y), _set_pos)
|
||||||
|
|
||||||
def set_x(self, vx):
|
|
||||||
"""
|
|
||||||
Set the variable for x.
|
|
||||||
|
|
||||||
NOTE: This changes the variable object itself, not only the value!
|
|
||||||
"""
|
|
||||||
self._v_x = vx
|
|
||||||
|
|
||||||
def set_y(self, vy):
|
|
||||||
"""
|
|
||||||
Set the variable for y.
|
|
||||||
|
|
||||||
NOTE: This changes the variable object itself, not only the value!
|
|
||||||
"""
|
|
||||||
self._v_y = vy
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"<{self.__class__.__name__} object on ({self.x}, {self.y})>"
|
return f"<{self.__class__.__name__} object on ({self.x}, {self.y})>"
|
||||||
|
|
||||||
|
@ -319,12 +319,6 @@ class Element(Item):
|
|||||||
h_sw = handles[SW]
|
h_sw = handles[SW]
|
||||||
h_se = handles[SE]
|
h_se = handles[SE]
|
||||||
|
|
||||||
# Share variables
|
|
||||||
h_sw.pos.set_x(h_nw.pos.x)
|
|
||||||
h_se.pos.set_x(h_ne.pos.x)
|
|
||||||
h_ne.pos.set_y(h_nw.pos.y)
|
|
||||||
h_se.pos.set_y(h_sw.pos.y)
|
|
||||||
|
|
||||||
# edge of element define default element ports
|
# edge of element define default element ports
|
||||||
self._ports = [
|
self._ports = [
|
||||||
LinePort(h_nw.pos, h_ne.pos),
|
LinePort(h_nw.pos, h_ne.pos),
|
||||||
@ -336,6 +330,11 @@ class Element(Item):
|
|||||||
# initialize min_x variables
|
# initialize min_x variables
|
||||||
self.min_width, self.min_height = 10, 10
|
self.min_width, self.min_height = 10, 10
|
||||||
|
|
||||||
|
self.constraint(horizontal=(h_nw.pos, h_ne.pos))
|
||||||
|
self.constraint(horizontal=(h_sw.pos, h_se.pos))
|
||||||
|
self.constraint(vertical=(h_nw.pos, h_sw.pos))
|
||||||
|
self.constraint(vertical=(h_ne.pos, h_se.pos))
|
||||||
|
|
||||||
# create minimal size constraints
|
# create minimal size constraints
|
||||||
self.constraint(left_of=(h_nw.pos, h_se.pos), delta=self._min_width)
|
self.constraint(left_of=(h_nw.pos, h_se.pos), delta=self._min_width)
|
||||||
self.constraint(above=(h_nw.pos, h_se.pos), delta=self._min_height)
|
self.constraint(above=(h_nw.pos, h_se.pos), delta=self._min_height)
|
||||||
|
@ -94,13 +94,12 @@ def test_disconnect_item_with_constraint():
|
|||||||
c.connect_item(line, line.handles()[0], b1, b1.ports()[0], constraint=cons)
|
c.connect_item(line, line.handles()[0], b1, b1.ports()[0], constraint=cons)
|
||||||
assert count(c.get_connections(handle=line.handles()[0])) == 1
|
assert count(c.get_connections(handle=line.handles()[0])) == 1
|
||||||
|
|
||||||
ncons = len(c.solver.constraints)
|
assert len(c.solver.constraints) == 13
|
||||||
assert ncons == 5
|
|
||||||
|
|
||||||
c.disconnect_item(line, line.handles()[0])
|
c.disconnect_item(line, line.handles()[0])
|
||||||
assert count(c.get_connections(handle=line.handles()[0])) == 0
|
assert count(c.get_connections(handle=line.handles()[0])) == 0
|
||||||
|
|
||||||
assert len(c.solver.constraints) == 4
|
assert len(c.solver.constraints) == 12
|
||||||
|
|
||||||
|
|
||||||
def test_disconnect_item_by_deleting_element():
|
def test_disconnect_item_by_deleting_element():
|
||||||
@ -141,13 +140,13 @@ def test_disconnect_item_with_constraint_by_deleting_element():
|
|||||||
assert count(c.get_connections(handle=line.handles()[0])) == 1
|
assert count(c.get_connections(handle=line.handles()[0])) == 1
|
||||||
|
|
||||||
ncons = len(c.solver.constraints)
|
ncons = len(c.solver.constraints)
|
||||||
assert ncons == 5
|
assert ncons == 13
|
||||||
|
|
||||||
c.remove(b1)
|
c.remove(b1)
|
||||||
|
|
||||||
assert count(c.get_connections(handle=line.handles()[0])) == 0
|
assert count(c.get_connections(handle=line.handles()[0])) == 0
|
||||||
|
|
||||||
assert 2 == len(c.solver.constraints)
|
assert 6 == len(c.solver.constraints)
|
||||||
|
|
||||||
|
|
||||||
def test_line_projection():
|
def test_line_projection():
|
||||||
|
@ -11,19 +11,6 @@ def test_position(position):
|
|||||||
assert position[1] == pos.y
|
assert position[1] == pos.y
|
||||||
|
|
||||||
|
|
||||||
def test_set_xy():
|
|
||||||
pos = Position((1, 2))
|
|
||||||
x = Variable()
|
|
||||||
y = Variable()
|
|
||||||
assert x is not pos.x
|
|
||||||
assert y is not pos.y
|
|
||||||
|
|
||||||
pos.set_x(x)
|
|
||||||
pos.set_y(y)
|
|
||||||
assert x is pos.x
|
|
||||||
assert y is pos.y
|
|
||||||
|
|
||||||
|
|
||||||
def test_handle_x_y():
|
def test_handle_x_y():
|
||||||
h = Handle()
|
h = Handle()
|
||||||
assert 0.0 == h.pos.x
|
assert 0.0 == h.pos.x
|
||||||
|
@ -369,7 +369,7 @@ def test_orthogonal_line_merge(simple_canvas):
|
|||||||
"""Test orthogonal line merging.
|
"""Test orthogonal line merging.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
assert 4 == len(simple_canvas.canvas.solver._constraints)
|
assert 12 == len(simple_canvas.canvas.solver._constraints)
|
||||||
|
|
||||||
simple_canvas.line.handles()[-1].pos = 100, 100
|
simple_canvas.line.handles()[-1].pos = 100, 100
|
||||||
|
|
||||||
@ -379,14 +379,14 @@ def test_orthogonal_line_merge(simple_canvas):
|
|||||||
segment.split_segment(0)
|
segment.split_segment(0)
|
||||||
simple_canvas.line.orthogonal = True
|
simple_canvas.line.orthogonal = True
|
||||||
|
|
||||||
assert 4 + 3 == len(simple_canvas.canvas.solver._constraints)
|
assert 6 + 6 + 3 == len(simple_canvas.canvas.solver._constraints)
|
||||||
assert 4 == len(simple_canvas.line.handles())
|
assert 4 == len(simple_canvas.line.handles())
|
||||||
assert 3 == len(simple_canvas.line.ports())
|
assert 3 == len(simple_canvas.line.ports())
|
||||||
|
|
||||||
# Test the merging
|
# Test the merging
|
||||||
segment.merge_segment(0)
|
segment.merge_segment(0)
|
||||||
|
|
||||||
assert 4 + 2 == len(simple_canvas.canvas.solver._constraints)
|
assert 6 + 6 + 2 == len(simple_canvas.canvas.solver._constraints)
|
||||||
assert 3 == len(simple_canvas.line.handles())
|
assert 3 == len(simple_canvas.line.handles())
|
||||||
assert 2 == len(simple_canvas.line.ports())
|
assert 2 == len(simple_canvas.line.ports())
|
||||||
|
|
||||||
|
@ -11,10 +11,10 @@ def test_undo_on_delete_element(revert_undo, undo_fixture):
|
|||||||
|
|
||||||
canvas = Canvas()
|
canvas = Canvas()
|
||||||
canvas.add(b1)
|
canvas.add(b1)
|
||||||
assert 2 == len(canvas.solver.constraints)
|
assert 6 == len(canvas.solver.constraints)
|
||||||
|
|
||||||
canvas.add(b2)
|
canvas.add(b2)
|
||||||
assert 4 == len(canvas.solver.constraints)
|
assert 12 == len(canvas.solver.constraints)
|
||||||
|
|
||||||
canvas.add(line)
|
canvas.add(line)
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ def test_undo_on_delete_element(revert_undo, undo_fixture):
|
|||||||
connector = Connector(line, line.handles()[-1])
|
connector = Connector(line, line.handles()[-1])
|
||||||
connector.connect(sink)
|
connector.connect(sink)
|
||||||
|
|
||||||
assert 6 == len(canvas.solver.constraints)
|
assert 14 == len(canvas.solver.constraints)
|
||||||
assert 2 == len(list(canvas.get_connections(item=line)))
|
assert 2 == len(list(canvas.get_connections(item=line)))
|
||||||
|
|
||||||
del undo_fixture[2][:] # Clear undo_list
|
del undo_fixture[2][:] # Clear undo_list
|
||||||
@ -34,7 +34,7 @@ def test_undo_on_delete_element(revert_undo, undo_fixture):
|
|||||||
# Here disconnect is not invoked!
|
# Here disconnect is not invoked!
|
||||||
canvas.remove(b2)
|
canvas.remove(b2)
|
||||||
|
|
||||||
assert 3 == len(canvas.solver.constraints)
|
assert 7 == len(canvas.solver.constraints)
|
||||||
assert 1 == len(list(canvas.get_connections(item=line)))
|
assert 1 == len(list(canvas.get_connections(item=line)))
|
||||||
|
|
||||||
cinfo = canvas.get_connection(line.handles()[0])
|
cinfo = canvas.get_connection(line.handles()[0])
|
||||||
@ -48,7 +48,7 @@ def test_undo_on_delete_element(revert_undo, undo_fixture):
|
|||||||
|
|
||||||
undo_fixture[0]() # Call undo
|
undo_fixture[0]() # Call undo
|
||||||
|
|
||||||
assert 6 == len(canvas.solver.constraints)
|
assert 14 == len(canvas.solver.constraints)
|
||||||
assert 2 == len(list(canvas.get_connections(item=line)))
|
assert 2 == len(list(canvas.get_connections(item=line)))
|
||||||
|
|
||||||
cinfo = canvas.get_connection(line.handles()[0])
|
cinfo = canvas.get_connection(line.handles()[0])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user