mirror of
https://github.com/slint-ui/slint.git
synced 2025-07-16 01:25:27 +00:00

This exposes a slint.Image class, which has a load_from_path class method as well as size/width/height properties. cc #4202
152 lines
5.7 KiB
Python
152 lines
5.7 KiB
Python
# Copyright © SixtyFPS GmbH <info@slint.dev>
|
|
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
|
|
|
|
import pytest
|
|
from slint import slint as native
|
|
from slint.slint import ValueType, PyImage;
|
|
import os
|
|
|
|
def test_property_access():
|
|
compiler = native.ComponentCompiler()
|
|
|
|
compdef = compiler.build_from_source("""
|
|
export global TestGlobal {
|
|
in property <string> theglobalprop: "Hey";
|
|
callback globallogic();
|
|
}
|
|
|
|
export struct MyStruct {
|
|
title: string,
|
|
finished: bool,
|
|
}
|
|
|
|
export component Test {
|
|
in property <string> strprop: "Hello";
|
|
in property <int> intprop: 42;
|
|
in property <float> floatprop: 100;
|
|
in property <bool> boolprop: true;
|
|
in property <image> imgprop;
|
|
in property <brush> brushprop;
|
|
in property <color> colprop;
|
|
in property <[string]> modelprop;
|
|
in property <MyStruct> structprop: {
|
|
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()
|
|
assert instance != None
|
|
|
|
with pytest.raises(ValueError, match="no such property"):
|
|
instance.set_property("nonexistent", 42)
|
|
|
|
assert instance.get_property("strprop") == "Hello"
|
|
instance.set_property("strprop", "World")
|
|
assert instance.get_property("strprop") == "World"
|
|
with pytest.raises(ValueError, match="wrong type"):
|
|
instance.set_property("strprop", 42)
|
|
|
|
assert instance.get_property("intprop") == 42
|
|
instance.set_property("intprop", 100)
|
|
assert instance.get_property("intprop") == 100
|
|
with pytest.raises(ValueError, match="wrong type"):
|
|
instance.set_property("intprop", False)
|
|
|
|
assert instance.get_property("floatprop") == 100
|
|
instance.set_property("floatprop", 42)
|
|
assert instance.get_property("floatprop") == 42
|
|
with pytest.raises(ValueError, match="wrong type"):
|
|
instance.set_property("floatprop", "Blah")
|
|
|
|
assert instance.get_property("boolprop") == True
|
|
instance.set_property("boolprop", False)
|
|
assert instance.get_property("boolprop") == False
|
|
with pytest.raises(ValueError, match="wrong type"):
|
|
instance.set_property("boolprop", 0)
|
|
|
|
structval = instance.get_property("structprop")
|
|
assert isinstance(structval, dict)
|
|
assert structval == {'title': 'builtin', 'finished': True}
|
|
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'})
|
|
|
|
with pytest.raises(ValueError, match="no such property"):
|
|
instance.set_global_property("nonexistent", "theglobalprop", 42)
|
|
with pytest.raises(ValueError, match="no such property"):
|
|
instance.set_global_property("TestGlobal", "nonexistent", 42)
|
|
|
|
assert instance.get_global_property("TestGlobal", "theglobalprop") == "Hey"
|
|
instance.set_global_property("TestGlobal", "theglobalprop", "Ok")
|
|
assert instance.get_global_property("TestGlobal", "theglobalprop") == "Ok"
|
|
|
|
def test_callbacks():
|
|
compiler = native.ComponentCompiler()
|
|
|
|
compdef = compiler.build_from_source("""
|
|
export global TestGlobal {
|
|
callback globallogic(string) -> string;
|
|
globallogic(value) => {
|
|
return "global " + value;
|
|
}
|
|
}
|
|
|
|
export component Test {
|
|
callback test-callback(string) -> string;
|
|
test-callback(value) => {
|
|
return "local " + value;
|
|
}
|
|
callback void-callback();
|
|
}
|
|
""", "")
|
|
assert compdef != None
|
|
|
|
instance = compdef.create()
|
|
assert instance != None
|
|
|
|
assert instance.invoke("test-callback", "foo") == "local foo"
|
|
|
|
assert instance.invoke_global("TestGlobal", "globallogic", "foo") == "global foo"
|
|
|
|
with pytest.raises(RuntimeError, match="no such callback"):
|
|
instance.set_callback("non-existent", lambda x: x)
|
|
|
|
instance.set_callback("test-callback", lambda x: "python " + x)
|
|
assert instance.invoke("test-callback", "foo") == "python foo"
|
|
|
|
with pytest.raises(RuntimeError, match="no such callback"):
|
|
instance.set_global_callback("TestGlobal", "non-existent", lambda x: x)
|
|
|
|
instance.set_global_callback("TestGlobal", "globallogic", lambda x: "python global " + x)
|
|
assert instance.invoke_global("TestGlobal", "globallogic", "foo") == "python global foo"
|
|
|
|
instance.set_callback("void-callback", lambda : None)
|
|
instance.invoke("void-callback")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import slint
|
|
instance = slint.load_file("../../examples/printerdemo/ui/printerdemo.slint")
|
|
instance.set_global_callback("PrinterQueue", "start-job", lambda title: print(f"new print job {title}"))
|
|
instance.run()
|