CtrlLib: CtrlMapper now provides operator()(Ctrl, T, const T& factor) for simple unit conversions

This commit is contained in:
Mirek Fidler 2024-03-05 21:32:18 +01:00
parent 01f40a9efb
commit 597ed21aed
4 changed files with 27 additions and 7 deletions

View file

@ -1,7 +1,10 @@
LAYOUT(MyDlgLayout, 200, 88)
ITEM(Label, dv___0, SetLabel(t_("Text")).LeftPosZ(8, 32).TopPosZ(8, 19))
ITEM(EditString, text, LeftPosZ(44, 152).TopPosZ(8, 19))
ITEM(Option, option, SetLabel(t_("Option")).LeftPosZ(44, 108).TopPosZ(36, 15))
ITEM(Button, ok, SetLabel(t_("OK")).LeftPosZ(132, 64).TopPosZ(60, 24))
LAYOUT(MyDlgLayout, 200, 120)
ITEM(Upp::Label, dv___0, SetLabel(t_("Text")).LeftPosZ(8, 32).TopPosZ(8, 19))
ITEM(Upp::EditString, text, LeftPosZ(64, 132).TopPosZ(8, 19))
ITEM(Upp::Option, option, SetLabel(t_("Option")).LeftPosZ(64, 108).TopPosZ(32, 20))
ITEM(Upp::Button, ok, SetLabel(t_("OK")).LeftPosZ(132, 64).TopPosZ(92, 24))
ITEM(Upp::Label, dv___4, SetLabel(t_("Distance")).LeftPosZ(8, 56).TopPosZ(60, 19))
ITEM(Upp::EditDouble, distance, LeftPosZ(64, 72).TopPosZ(60, 19))
ITEM(Upp::Label, dv___6, SetLabel(t_("km")).LeftPosZ(140, 40).TopPosZ(60, 19))
END_LAYOUT

View file

@ -8,6 +8,7 @@ using namespace Upp;
struct MyStruct {
String text;
bool option;
double distance;
};
struct MyDlg : WithMyDlgLayout<TopWindow> {
@ -28,6 +29,7 @@ void MyDlg::Map(CtrlMapper& m, MyStruct& s)
m
(text, s.text)
(option, s.option)
(distance, s.distance, 0.001)
;
}
@ -36,12 +38,14 @@ GUI_APP_MAIN
MyStruct data;
data.text = "Some text";
data.option = true;
data.distance = 1700;
MyDlg dlg;
dlg.Map(CtrlMapper().ToCtrls(), data);
if(dlg.Run() == IDOK)
dlg.Map(CtrlMapper().ToValues(), data);
PromptOK(String() << "text: [* \1" << data.text << "\1]&"
<< "option: [* " << data.option);
<< "option: [* " << data.option << "]&"
<< "distance: [* " << data.distance << " m]");
}

View file

@ -385,6 +385,9 @@ class CtrlMapper {
public:
template <class T>
CtrlMapper& operator()(Ctrl& ctrl, T& val) { if(toctrls) ctrl <<= val; else val = ~ctrl; return *this; }
template <class T>
CtrlMapper& operator()(Ctrl& ctrl, T& val, const T& f) { if(toctrls) ctrl <<= f * val; else val = (T)~ctrl / f; return *this; }
CtrlMapper& ToCtrls() { toctrls = true; return *this; }
CtrlMapper& ToValues() { toctrls = false; return *this; }

View file

@ -27,6 +27,16 @@ trl][@(0.0.255) `&]_[*@3 ctrl], [*@4 T][@(0.0.255) `&]_[*@3 val])&]
based on current direction setting.&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:CtrlMapper`:`:operator`(`)`(Upp`:`:Ctrl`&`,T`&`,const T`&`): [@(0.0.255) te
mplate]_<[@(0.0.255) class]_[*@4 T]>_[_^Upp`:`:CtrlMapper^ CtrlMapper][@(0.0.255) `&]_[* op
erator()]([_^Upp`:`:Ctrl^ Ctrl][@(0.0.255) `&]_[*@3 ctrl], [*@4 T][@(0.0.255) `&]_[*@3 val],
[@(0.0.255) const] [*@4 T][@(0.0.255) `&]_[*@3 f])&]
[s2;%% Similar to two parameter operator(), but before setting [%-*@3 val]
to [%-*@3 ctrl], it multiplies it by [%-*@3 f] and before retrieving,
divides by [%-*@3 f]. Useful as simple conversion tool when e.g.
the value is to be stored in meters but edited as kilometers.&]
[s3; &]
[s4; &]
[s5;:Upp`:`:CtrlMapper`:`:ToCtrls`(`): [_^Upp`:`:CtrlMapper^ CtrlMapper][@(0.0.255) `&]_[* T
oCtrls]()&]
[s2;%% Sets direction flag to move values to widgets.&]