Save top-left handle position for elements

Otherwise the top-left handle position is always (0, 0),

Which results in unexpected visual changes in the model when loaded.

Fixes #1467.
This commit is contained in:
Arjan Molenaar 2022-04-21 22:32:58 +02:00
parent d32cc87929
commit 79f044eb1b
4 changed files with 37 additions and 4 deletions

View File

@ -9,3 +9,22 @@ def test_create_action(diagram, element_factory):
item = diagram.create(ActionItem, subject=action)
assert item.subject is action
def test_load_action(element_factory, diagram, saver, loader):
action = diagram.create(ActionItem)
action.handles()[0].pos = (1, 2)
action.matrix.translate(10, 20)
action.width = 201
action.height = 202
dump = saver()
loader(dump)
new_action = next(element_factory.select(ActionItem))
assert tuple(new_action.handles()[0].pos) == (1, 2)
assert tuple(new_action.matrix) == (1.0, 0.0, 0.0, 1.0, 10, 20)
assert new_action.width == 201
assert new_action.height == 202

View File

@ -151,12 +151,19 @@ class ElementPresentation(gaphas.Element, HandlePositionUpdate, Presentation[S])
def save(self, save_func):
save_func("matrix", tuple(self.matrix))
save_func("top-left", tuple(map(float, self._handles[0].pos)))
save_func("width", self.width)
save_func("height", self.height)
super().save(save_func)
def load(self, name, value):
if name == "width":
if name == "top-left":
pos = ast.literal_eval(value)
self._handles[0].pos = pos
# Also adjust bottom-right handle to keep width and height intact
self._handles[2].pos.x += pos[0]
self._handles[2].pos.y += pos[1]
elif name == "width":
self.width = ast.literal_eval(value)
elif name == "height":
self.height = ast.literal_eval(value)

View File

@ -53,10 +53,11 @@ def test_element_saving(element_factory, diagram):
p.save(save_func)
assert len(properties) == 5
assert len(properties) == 6
assert properties["matrix"] == (1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
assert properties["width"] == 100.0
assert properties["height"] == 50.0
assert properties["top-left"] == (0, 0)
assert properties["subject"] is subject
assert properties["diagram"] is diagram

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<gaphor xmlns="http://gaphor.sourceforge.net/model" version="3.0" gaphor-version="2.5.0">
<gaphor xmlns="http://gaphor.sourceforge.net/model" version="3.0" gaphor-version="2.9.2">
<Package id="DCE:658E43CC-3A1D-11DC-8B61-000D93868322">
<name>
<val>New model</val>
@ -44,8 +44,11 @@
</Line>
<Box id="DCE:680E2142-3A1D-11DC-8B61-000D93868322">
<matrix>
<val>(1.0, 0.0, 0.0, 1.0, 215.0, 121.0)</val>
<val>(1.0, 0.0, 0.0, 1.0, 214.0, 119.0)</val>
</matrix>
<top-left>
<val>(0.0, 0.0)</val>
</top-left>
<width>
<val>100.0</val>
</width>
@ -60,6 +63,9 @@
<matrix>
<val>(1.0, 0.0, 0.0, 1.0, 332.0, 137.0)</val>
</matrix>
<top-left>
<val>(0.0, 0.0)</val>
</top-left>
<width>
<val>100.0</val>
</width>