ADataBinding#
Data binding implementation.
Header: | #include <AUI/Util/ADataBinding.h> |
CMake: | aui_link(my_target PUBLIC aui::views) |
Detailed Description#
Experimental Feature
This API is experimental. Experimental APIs are likely to contain bugs, might be changed or removed in the future.
If const reference of your model passed, ADataBinding will create and manage its own copy of your model.
If pointer of your model passed, ADataBinding will reference to your model and write directly to your model. When ADataBinding is destructed the pointer will not be deleted.
Example:
_new<ATextField>() && dataBinding(&User::username)
This code will bind ATextField with username field in the User model.
Another example:
class MyWindow: public AWindow {
public:
MyWindow(): AWindow("Test") {
struct Model {
int value = 0;
};
auto data = _new<ADataBinding<Model>>(Model{});
data->addObserver(&Model::value, [](int v) {
ALogger::info("Debug") << "New value: " << v;
});
setContents(Centered {
Vertical {
Label { } AUI_LET {
data->addObserver(&Model::value, [it](int v) {
it->setText("{}"_format(v));
});
},
Horizontal{
Button{"+"}.clicked(this, [data] {
data->getEditableModel().value += 1;
data->notifyUpdate();
}),
Button{"-"}.clicked(this, [data] {
data->getEditableModel().value -= 1;
data->notifyUpdate();
}),
},
}
});
}
};
Here, we use getEditableModel() in order to change data in our model and notifyUpdate() to notify.
Also, we use [AUI_LET](aui_let.md) construction to define custom format for label.
Public fields and Signals#
modelChanged#
emits<> modelChanged
Data in the model has changed.
Public Methods#
operator#
Create a connection to setter only.
- Arguments
field
pointer-to-member-field of model.setterFunc
pointer-to-member-function setter.
Examples#
examples/7guis/cells/src/AST.cpp
7GUIs Cells - Spreadsheet processor (Excel).
Create a connection to specified lambda setter only.
- Arguments
field
pointer-to-member-field of model.setterLambda
lambda which accepts reference to your view type and const reference to data (see examples).
View type is deduces from the first argument of your lambda.
struct Model {
AString text;
};
auto model = _new<ADataBinding<Model>>(Model{});
_new<ALabel>() && model(&Model::text, [](ALabel& view, const AString& data) {
view.setText(data);
});
Examples#
examples/7guis/cells/src/AST.cpp
7GUIs Cells - Spreadsheet processor (Excel).
template<typename View, typename ModelField, typename GetterRV, aui::invocable<View *, const ModelField &> Setter >
auto ADataBinding::operator()
Create a connection to specified pointer-to-member-field signal and pointer-to-member-function setter.
- Arguments
field
pointer-to-member-field of model.getter
pointer-to-member-field of view's signalsetter
pointer-to-member-field of view's setter
Examples#
examples/7guis/cells/src/AST.cpp
7GUIs Cells - Spreadsheet processor (Excel).
Create a connection via ADataBindingDefault.
- Arguments
field
pointer-to-member-field of model.
ADataBindingDefault must be defined for model type and your view.
Examples#
examples/7guis/cells/src/AST.cpp
7GUIs Cells - Spreadsheet processor (Excel).
template<typename Data, aui::invocable<Data> Projection >
ADataBindingLinker2<Model,Data,Projection> ADataBinding::operator()
Create a connection via ADataBindingDefault and projection (setter only).
ADataBindingDefault must be defined for model type and your view.
Examples#
examples/7guis/cells/src/AST.cpp
7GUIs Cells - Spreadsheet processor (Excel).