Add support for mapping image properties

This exposes a slint.Image class, which has a load_from_path class
method as well as size/width/height properties.

cc #4202
This commit is contained in:
Simon Hausmann 2024-02-06 11:52:28 +01:00 committed by Simon Hausmann
parent 9aa931f1f8
commit 93efd74e24
6 changed files with 97 additions and 3 deletions

View file

@ -3,7 +3,8 @@
import pytest
from slint import slint as native
from slint.slint import ValueType;
from slint.slint import ValueType, PyImage;
import os
def test_property_access():
compiler = native.ComponentCompiler()
@ -32,10 +33,11 @@ def test_property_access():
title: "builtin",
finished: true,
};
in property <image> imageprop: @image-url("../../../examples/printerdemo/ui/images/cat.jpg");
callback test-callback();
}
""", "")
""", os.path.join(os.path.dirname(__file__), "main.slint"))
assert compdef != None
instance = compdef.create()
@ -74,6 +76,19 @@ def test_property_access():
instance.set_property("structprop", {'title': 'new', 'finished': False})
assert instance.get_property("structprop") == {'title': 'new', 'finished': False}
imageval = instance.get_property("imageprop")
assert imageval.width == 320
assert imageval.height == 480
assert "cat.jpg" in imageval.path
with pytest.raises(RuntimeError, match="The image cannot be loaded"):
PyImage.load_from_path("non-existent.png")
instance.set_property("imageprop", PyImage.load_from_path(os.path.join(os.path.dirname(__file__), "../../../examples/iot-dashboard/images/humidity.png")))
imageval = instance.get_property("imageprop")
assert imageval.size == (36, 36)
assert "humidity.png" in imageval.path
with pytest.raises(TypeError, match="'int' object cannot be converted to 'PyString'"):
instance.set_property("structprop", {42: 'wrong'})