"""Basic connectors such as Ports and Handles."""from__future__importannotationsfromgaphas.positionimportPositionfromgaphas.solverimportNORMALfromgaphas.typesimportPos,SupportsFloatPos,TypedProperty
[docs]classHandle:"""Handles are used to support modifications of Items. If the handle is connected to an item, the ``connected_to`` property should refer to the item. A ``disconnect`` handler should be provided that handles all disconnect behaviour (e.g. clean up constraints and ``connected_to``). Note for those of you that use the Pickle module to persist a canvas: The property ``disconnect`` should contain a callable object (with __call__() method), so the pickle handler can also pickle that. Pickle is not capable of pickling ``instancemethod`` or ``function`` objects. """def__init__(self,pos:Pos=(0,0),strength:int=NORMAL,connectable:bool=False,movable:bool=True,)->None:"""Create a new handle. Position is in item coordinates. """self._pos=Position(pos[0],pos[1],strength)self._connectable=connectableself._movable=movableself._visible=Trueself._glued=Falsedef_set_pos(self,pos:Position|SupportsFloatPos)->None:""" Shortcut for ``handle.pos.pos = pos`` >>> h = Handle((10, 10)) >>> h.pos = (20, 15) >>> h.pos <Position object on (20, 15)> """self._pos.pos=pospos:TypedProperty[Position,Position|SupportsFloatPos]pos=property(lambdas:s._pos,_set_pos,doc="The Handle's position")@propertydefconnectable(self)->bool:"""Can this handle actually connectect to a port?"""returnself._connectable@connectable.setterdefconnectable(self,connectable:bool)->None:self._connectable=connectable@propertydefmovable(self)->bool:"""Can this handle be moved by a mouse pointer?"""returnself._movable@movable.setterdefmovable(self,movable:bool)->None:self._movable=movable@propertydefvisible(self)->bool:"""Is this handle visible to the user?"""returnself._visible@visible.setterdefvisible(self,visible:bool)->None:self._visible=visible@propertydefglued(self)->bool:"""Is the handle being moved and about to be connected?"""returnself._glued@glued.setterdefglued(self,glued:bool)->None:self._glued=glueddef__str__(self)->str:returnf"<{self.__class__.__name__} object on ({self._pos.x}, {self._pos.y})>"__repr__=__str__