Inherit Box and Ellipse from ElementPresentation

Now diagram is set correctly, and it saves some duplicate code.
This commit is contained in:
Arjan Molenaar 2021-01-06 10:00:04 +01:00
parent 574ba56f30
commit e49b3d5fc6
No known key found for this signature in database
GPG Key ID: BF977B918996CB13
3 changed files with 16 additions and 56 deletions

View File

@ -1,12 +1,9 @@
"""Trivial drawing aids (box, line, ellipse).""" """Trivial drawing aids (box, line, ellipse)."""
import ast from gaphas.item import NW
from gaphas.item import NW, Element
from gaphas.util import path_ellipse from gaphas.util import path_ellipse
from gaphor.core.modeling import Presentation from gaphor.diagram.presentation import ElementPresentation, LinePresentation
from gaphor.diagram.presentation import LinePresentation
from gaphor.diagram.shapes import stroke from gaphor.diagram.shapes import stroke
@ -17,36 +14,14 @@ class Line(LinePresentation):
self._handles[-1].connectable = False self._handles[-1].connectable = False
class Box(Element, Presentation): class Box(ElementPresentation):
"""A Box has 4 handles (for a start):: """A Box has 4 handles (for a start)::
NW +---+ NE SW +---+ SE NW +---+ NE SW +---+ SE
""" """
def __init__(self, diagram, id=None): def __init__(self, diagram, id=None):
super().__init__(connections=diagram.connections, diagram=diagram, id=id) # type: ignore[call-arg] super().__init__(diagram=diagram, id=id)
def save(self, save_func):
save_func("matrix", tuple(self.matrix))
save_func("width", self.width)
save_func("height", self.height)
def load(self, name, value):
if name == "matrix":
self.matrix.set(*ast.literal_eval(value))
elif name == "width":
self.width = ast.literal_eval(value)
elif name == "height":
self.height = ast.literal_eval(value)
def postload(self):
pass
def pre_update(self, context):
pass
def post_update(self, context):
pass
def draw(self, context): def draw(self, context):
cr = context.cairo cr = context.cairo
@ -55,33 +30,11 @@ class Box(Element, Presentation):
stroke(context) stroke(context)
class Ellipse(Element, Presentation): class Ellipse(ElementPresentation):
"""""" """"""
def __init__(self, diagram, id=None): def __init__(self, diagram, id=None):
super().__init__(connections=diagram.connections, diagram=diagram, id=id) # type: ignore[call-arg] super().__init__(diagram=diagram, id=id)
def save(self, save_func):
save_func("matrix", tuple(self.matrix))
save_func("width", self.width)
save_func("height", self.height)
def load(self, name, value):
if name == "matrix":
self.matrix.set(*ast.literal_eval(value))
elif name == "width":
self.width = ast.literal_eval(value)
elif name == "height":
self.height = ast.literal_eval(value)
def postload(self):
pass
def pre_update(self, context):
pass
def post_update(self, context):
pass
def draw(self, context): def draw(self, context):
cr = context.cairo cr = context.cairo

View File

@ -123,7 +123,8 @@ class ElementPresentation(gaphas.Element, HandlePositionUpdate, Presentation[S])
def pre_update(self, context): def pre_update(self, context):
if not self.shape: if not self.shape:
self.update_shapes() self.update_shapes()
self.min_width, self.min_height = self.shape.size(context) if self.shape:
self.min_width, self.min_height = self.shape.size(context)
def post_update(self, context): def post_update(self, context):
pass pass

View File

@ -47,11 +47,14 @@
<val>(1.0, 0.0, 0.0, 1.0, 215.0, 121.0)</val> <val>(1.0, 0.0, 0.0, 1.0, 215.0, 121.0)</val>
</matrix> </matrix>
<width> <width>
<val>97.0</val> <val>96.0</val>
</width> </width>
<height> <height>
<val>74.0</val> <val>71.0</val>
</height> </height>
<diagram>
<ref refid="DCE:658E851A-3A1D-11DC-8B61-000D93868322"/>
</diagram>
</item> </item>
<item id="DCE:68ECCFB2-3A1D-11DC-8B61-000D93868322" type="Ellipse"> <item id="DCE:68ECCFB2-3A1D-11DC-8B61-000D93868322" type="Ellipse">
<matrix> <matrix>
@ -63,6 +66,9 @@
<height> <height>
<val>60.0</val> <val>60.0</val>
</height> </height>
<diagram>
<ref refid="DCE:658E851A-3A1D-11DC-8B61-000D93868322"/>
</diagram>
</item> </item>
</canvas> </canvas>
</Diagram> </Diagram>