mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-03 18:29:09 +00:00

This provides a Model base class in Python and sub-classes of that can be set as data models in slint. The ListModel is provided as basic sub-class operating on a list() and allowing mutation and notifying the view on the Slint side. Similarly, an array declared in Slint is exposed as an object to Python that looks like a Model. Both support the sequence protocol. Fixes #4135
103 lines
2.5 KiB
Python
103 lines
2.5 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
|
|
|
|
from slint import slint as native
|
|
from slint import models as models
|
|
|
|
|
|
def test_model_notify():
|
|
compiler = native.ComponentCompiler()
|
|
|
|
compdef = compiler.build_from_source("""
|
|
export component App {
|
|
width: 300px;
|
|
height: 300px;
|
|
|
|
out property<length> layout-height: layout.height;
|
|
in-out property<[length]> fixed-height-model;
|
|
|
|
VerticalLayout {
|
|
alignment: start;
|
|
|
|
layout := VerticalLayout {
|
|
for fixed-height in fixed-height-model: Rectangle {
|
|
background: blue;
|
|
height: fixed-height;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
""", "")
|
|
assert compdef != None
|
|
|
|
instance = compdef.create()
|
|
assert instance != None
|
|
|
|
model = models.ListModel([100, 0])
|
|
|
|
instance.set_property(
|
|
"fixed-height-model", model)
|
|
|
|
assert instance.get_property("layout-height") == 100
|
|
model.set_row_data(1, 50)
|
|
assert instance.get_property("layout-height") == 150
|
|
model.append(75)
|
|
assert instance.get_property("layout-height") == 225
|
|
del model[1:]
|
|
assert instance.get_property("layout-height") == 100
|
|
|
|
assert isinstance(instance.get_property(
|
|
"fixed-height-model"), models.ListModel)
|
|
|
|
|
|
def test_model_from_list():
|
|
compiler = native.ComponentCompiler()
|
|
|
|
compdef = compiler.build_from_source("""
|
|
export component App {
|
|
in-out property<[int]> data: [1, 2, 3, 4];
|
|
}
|
|
""", "")
|
|
assert compdef != None
|
|
|
|
instance = compdef.create()
|
|
assert instance != None
|
|
|
|
model = instance.get_property("data")
|
|
assert model.row_count() == 4
|
|
assert model.row_data(2) == 3
|
|
|
|
instance.set_property("data", models.ListModel([0]))
|
|
instance.set_property("data", model)
|
|
assert list(instance.get_property("data")) == [1, 2, 3, 4]
|
|
|
|
|
|
def test_python_model_sequence():
|
|
model = models.ListModel([1, 2, 3, 4, 5])
|
|
|
|
assert len(model) == 5
|
|
assert list(model) == [1, 2, 3, 4, 5]
|
|
model[0] = 100
|
|
assert list(model) == [100, 2, 3, 4, 5]
|
|
assert model[2] == 3
|
|
|
|
|
|
def test_rust_model_sequence():
|
|
compiler = native.ComponentCompiler()
|
|
|
|
compdef = compiler.build_from_source("""
|
|
export component App {
|
|
in-out property<[int]> data: [1, 2, 3, 4, 5];
|
|
}
|
|
""", "")
|
|
assert compdef != None
|
|
|
|
instance = compdef.create()
|
|
assert instance != None
|
|
|
|
model = instance.get_property("data")
|
|
|
|
assert len(model) == 5
|
|
assert list(model) == [1, 2, 3, 4, 5]
|
|
assert model[2] == 3
|