Skip to content

Commit

Permalink
Simplify logic around orthogonal lines
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed Mar 28, 2023
1 parent 01671d7 commit 8b30756
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
25 changes: 10 additions & 15 deletions gaphas/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ def __init__(self, connections: Connections, **kwargs: object) -> None:

self._line_width = 2.0
self._fuzziness = 0.0
self._orthogonal_constraints: list[Constraint] = []
self._horizontal = False
self._orthogonal = False
self._orthogonal_constraints: list[Constraint] = []

@property
def head(self) -> Handle:
Expand All @@ -285,22 +286,17 @@ def fuzziness(self) -> float:
def fuzziness(self, fuzziness: float) -> None:
self._fuzziness = fuzziness

def update_orthogonal_constraints(self, orthogonal: bool) -> None:
"""Update the constraints required to maintain the orthogonal line.
The actual constraints attribute (``_orthogonal_constraints``)
is observed, so the undo system will update the contents
properly
"""
def update_orthogonal_constraints(self) -> None:
"""Update the constraints required to maintain the orthogonal line."""
# Use public `horizontal` and `orthogonal` field, so properties can be overwritten
for c in self._orthogonal_constraints:
self._connections.remove_constraint(self, c)
del self._orthogonal_constraints[:]

if not orthogonal:
if not self._orthogonal or len(self._handles) < 3:
return

add = self._connections.add_constraint
# Use public `horizontal` field, so that property can be overwritten
cons = [
add(self, c)
for c in create_orthogonal_constraints(self._handles, self.horizontal)
Expand All @@ -318,7 +314,7 @@ def _set_orthogonal_constraints(

@property
def orthogonal(self) -> bool:
return bool(self._orthogonal_constraints)
return self._orthogonal

@orthogonal.setter
def orthogonal(self, orthogonal: bool) -> None:
Expand All @@ -327,9 +323,8 @@ def orthogonal(self, orthogonal: bool) -> None:
>>> a.orthogonal
False
"""
if orthogonal and len(self.handles()) < 3:
raise ValueError("Can't set orthogonal line with less than 3 handles")
self.update_orthogonal_constraints(orthogonal)
self._orthogonal = True
self.update_orthogonal_constraints()

@property
def horizontal(self) -> bool:
Expand All @@ -346,7 +341,7 @@ def horizontal(self, horizontal: bool) -> None:
False
"""
self._horizontal = horizontal
self.update_orthogonal_constraints(self.orthogonal)
self.update_orthogonal_constraints()

def insert_handle(self, index: int, handle: Handle) -> None:
self._handles.insert(index, handle)
Expand Down
4 changes: 2 additions & 2 deletions gaphas/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def do_split(segment, count):
do_split(segment, count)

# force orthogonal constraints to be recreated
item.update_orthogonal_constraints(item.orthogonal)
item.update_orthogonal_constraints()

self.recreate_constraints()

Expand Down Expand Up @@ -135,7 +135,7 @@ def merge_segment(self, segment, count=2):
item.insert_port(segment, port)

# force orthogonal constraints to be recreated
item.update_orthogonal_constraints(item.orthogonal)
item.update_orthogonal_constraints()

self.recreate_constraints()
self.model.request_update(item)
Expand Down

0 comments on commit 8b30756

Please sign in to comment.