DataModels

A DataModel is a class that contains data of a specific type. DataModels are designed for use with UI widgets since widgets don’t hold their data and therefore don’t provide their own accessors. A DataModel may be assigned to multiple UI widgets if desired. In addition to Getter and Setter methods they also provide callbacks for user interactions since they are subclasses of AbstractValueModel

Example code

my_int_model = ui.SimpleIntModel(16, min=9, max=MAX_FONT_SIZE)
my_int_field = ui.IntField(my_int_model)

# we can also assign it to another widget to display or modify its value
# both IntFields will now operate on, and display, the same Int data
my_other_int_field = ui.IntField(my_int_model)

# change the value, and therefor the value displayed in both IntFields
my_other_int_field._as_int = 24

A list of the Value DataModels

DataModel Class

Data Type

SimpleBoolModel

Boolean

SimpleFloatModel

Float

SimpleIntModel

Int

SimpleStringModel

String

All of the above Model types can cast to any of the other types, making model sharing between widgets of different data types possible.

print("My Int Model as a String: " + my_int_model._as_string)

They all also provide get_min() and get_max() for getting their minimum and maximum values of their respective type.

All Inherit from AbstractValueModel

All of the above DataModels are subclasses of AbstractValueModel providing them with the below callbacks and properties.

class AbstractValueModel():

Properies / Methods:

Show...

def _value_changed(self) -> None:

Called when any data of the model is changed. It will notify the subscribed widgets.

def add_begin_edit_fn(self, arg0: typing.Callable[[AbstractValueModel], None]) -> int:

Adds the function that will be called every time the user starts the editing. The id of the callback that is used to remove the callback.

def add_end_edit_fn(self, arg0: typing.Callable[[AbstractValueModel], None]) -> int:

Adds the function that will be called every time the user finishes the editing. The id of the callback that is used to remove the callback.

def add_value_changed_fn(self, arg0: typing.Callable[[AbstractValueModel], None]) -> int:

Adds the function that will be called every time the value changes. The id of the callback that is used to remove the callback.

def begin_edit(self) -> None:

Called when the user starts the editing. If it’s a field, this method is called when the user activates the field and places the cursor inside. This method should be reimplemented.

def end_edit(self) -> None:

Called when the user finishes the editing. If it’s a field, this method is called when the user presses Enter or selects another field for editing. It’s useful for undo/redo. This method should be reimplemented.

def get_value_as_bool(self) -> bool:

Return the bool representation of the value.

def get_value_as_float(self) -> float:

Return the float representation of the value.

def get_value_as_int(self) -> int:

Return the int representation of the value.

def get_value_as_string(self) -> str:

Return the string representation of the value.

def remove_begin_edit_fn(self, arg0: int) -> None:

Remove the callback by its id. ### Arguments: id : The id that addBeginEditFn returns.

def remove_end_edit_fn(self, arg0: int) -> None:

Remove the callback by its id. ### Arguments: id : The id that addEndEditFn returns.

def remove_value_changed_fn(self, arg0: int) -> None:

Remove the callback by its id. ### Arguments: id : The id that addValueChangedFn returns.

def set_value(self, value: bool) -> None:

Set the value. Set the value. Set the value. Set the value.

def subscribe_begin_edit_fn(self, arg0: typing.Callable[[AbstractValueModel], None]) -> carb._carb.Subscription:

Adds the function that will be called every time the user starts the editing. The id of the callback that is used to remove the callback.

def subscribe_end_edit_fn(self, arg0: typing.Callable[[AbstractValueModel], None]) -> carb._carb.Subscription:

Adds the function that will be called every time the user finishes the editing. The id of the callback that is used to remove the callback.

def subscribe_value_changed_fn(self, arg0: typing.Callable[[AbstractValueModel], None]) -> carb._carb.Subscription:

Adds the function that will be called every time the value changes. The id of the callback that is used to remove the callback.

def as_bool(self) -> bool:

Return the bool representation of the value. type : bool

def as_bool(self, arg1: bool) -> None:

Return the bool representation of the value.

def as_float(self) -> float:

Return the float representation of the value. type : float

def as_float(self, arg1: float) -> None:

Return the float representation of the value.

def as_int(self) -> int:

Return the int representation of the value. type : int

def as_int(self, arg1: int) -> None:

Return the int representation of the value.

def as_string(self) -> str:

Return the string representation of the value. type : str

def as_string(self, arg1: str) -> None:

Return the string representation of the value.