improvements in loading from DML - issue #58

This commit is contained in:
Vadim Lopatin 2015-04-01 17:39:58 +03:00
parent b62334e5c7
commit 22118b5654
7 changed files with 115 additions and 80 deletions

View File

@ -458,14 +458,20 @@ extern (C) int UIAppMain(string[] args) {
*/ */
id: myLabel1 id: myLabel1
text: "Some text"; padding: 5 text: "Some text"; padding: 5
enabled: false
} }
TextWidget { TextWidget {
id: myLabel2 id: myLabel2
text: SOME_TEXT_RESOURCE_ID; margins: 5 text: SOME_TEXT_RESOURCE_ID; margins: 5
enabled: true
} }
} }
}); });
Log.d("id=", w.id, " text=", w.text, " padding=", w.padding, " margins=", w.margins, " lbl1=", w.childById!TextWidget("myLabel1").text, " lbl2=", w.childById!TextWidget("myLabel2").text); Log.d("id=", w.id, " text=", w.text, " padding=", w.padding, " margins=", w.margins,
" lbl1.text=", w.childById!TextWidget("myLabel1").text,
" lbl1.enabled=", w.childById!TextWidget("myLabel1").enabled,
" lbl2.text=", w.childById!TextWidget("myLabel2").text
);
destroy(w); destroy(w);
layout.childById("BTN1").onClickListener = (delegate (Widget w) { Log.d("onClick ", w.id); return true; }); layout.childById("BTN1").onClickListener = (delegate (Widget w) { Log.d("onClick ", w.id); return true; });

View File

@ -511,44 +511,53 @@ class MLParser {
protected void setIntProperty(string propName, int value, string suffix = null) { protected void setIntProperty(string propName, int value, string suffix = null) {
value = applySuffix(value, suffix); value = applySuffix(value, suffix);
if (!_currentWidget.setProperty(propName, value)) if (!_currentWidget.setIntProperty(propName, value))
error("unknown int property " ~ propName);
}
protected void setBoolProperty(string propName, bool value) {
if (!_currentWidget.setBoolProperty(propName, value))
error("unknown int property " ~ propName); error("unknown int property " ~ propName);
} }
protected void setFloatProperty(string propName, double value) { protected void setFloatProperty(string propName, double value) {
if (!_currentWidget.setProperty(propName, value)) if (!_currentWidget.setDoubleProperty(propName, value))
error("unknown double property " ~ propName); error("unknown double property " ~ propName);
} }
protected void setRectProperty(string propName, Rect value) { protected void setRectProperty(string propName, Rect value) {
if (!_currentWidget.setProperty(propName, value)) if (!_currentWidget.setRectProperty(propName, value))
error("unknown Rect property " ~ propName); error("unknown Rect property " ~ propName);
} }
protected void setStringProperty(string propName, string value) { protected void setStringProperty(string propName, string value) {
if (propName.equal("id")) { if (propName.equal("id")) {
if (!_currentWidget.setProperty(propName, value)) if (!_currentWidget.setStringProperty(propName, value))
error("cannot set id property for widget"); error("cannot set id property for widget");
return; return;
} }
dstring v = toUTF32(value); dstring v = toUTF32(value);
if (!_currentWidget.setProperty(propName, v)) if (!_currentWidget.setDstringProperty(propName, v))
error("unknown string property " ~ propName); error("unknown string property " ~ propName);
} }
protected void setIdentProperty(string propName, string value) { protected void setIdentProperty(string propName, string value) {
if (propName.equal("id")) { if (propName.equal("id")) {
if (!_currentWidget.setProperty(propName, value)) if (!_currentWidget.setStringProperty(propName, value))
error("cannot set id property for widget"); error("cannot set id property for widget");
return; return;
} }
if (value.equal("FILL") || value.equal("FILL_PARENT")) if (value.equal("true"))
setBoolProperty(propName, true);
else if (value.equal("false"))
setBoolProperty(propName, false);
else if (value.equal("FILL") || value.equal("FILL_PARENT"))
setIntProperty(propName, FILL_PARENT); setIntProperty(propName, FILL_PARENT);
else if (value.equal("WRAP") || value.equal("WRAP_CONTENT")) else if (value.equal("WRAP") || value.equal("WRAP_CONTENT"))
setIntProperty(propName, WRAP_CONTENT); setIntProperty(propName, WRAP_CONTENT);
else if (!_currentWidget.setProperty(propName, value)) else if (!_currentWidget.setStringProperty(propName, value))
error("unknown ident property " ~ propName); error("unknown ident property " ~ propName);
} }

View File

@ -207,6 +207,10 @@ class ImageWidget : Widget {
return this; return this;
} }
/// set string property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setStringProperty", "string",
"drawableId"));
/// handle theme change: e.g. reload some themed resources /// handle theme change: e.g. reload some themed resources
override void onThemeChanged() { override void onThemeChanged() {
if (_drawableId !is null) if (_drawableId !is null)
@ -244,7 +248,6 @@ class ImageWidget : Widget {
} }
} }
/// button with image only /// button with image only
class ImageButton : ImageWidget { class ImageButton : ImageWidget {
/// constructor by id and icon resource id /// constructor by id and icon resource id
@ -534,6 +537,11 @@ class AbstractSlider : WidgetGroup {
} }
return this; return this;
} }
/// set int property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setIntProperty", "int",
"minValue", "maxValue", "pageSize", "position"));
/// set new range (min and max values for slider) /// set new range (min and max values for slider)
AbstractSlider setRange(int min, int max) { AbstractSlider setRange(int min, int max) {
if (_minValue != min || _maxValue != max) { if (_minValue != min || _maxValue != max) {
@ -748,6 +756,18 @@ class ScrollBar : AbstractSlider, OnClickHandler {
return this; return this;
} }
/// set string property value, for ML loaders
override bool setStringProperty(string name, string value) {
if (name.equal("orientation")) {
if (value.equal("Vertical") || value.equal("vertical"))
orientation = Orientation.Vertical;
else
orientation = Orientation.Horizontal;
return true;
}
return super.setStringProperty(name, value);
}
/// empty parameter list constructor - for usage by factory /// empty parameter list constructor - for usage by factory
this() { this() {

View File

@ -474,6 +474,13 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
return x < _pos.left + _leftPaneWidth ? CursorType.Arrow : CursorType.IBeam; return x < _pos.left + _leftPaneWidth ? CursorType.Arrow : CursorType.IBeam;
} }
/// set bool property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setBoolProperty", "bool",
"wantTabs", "showIcons", "showFolding", "showModificationMarks", "showLineNumbers", "readOnly", "replaceMode", "useSpacesForTabs"));
/// set int property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setIntProperty", "int",
"tabSize"));
/// when true, Tab / Shift+Tab presses are processed internally in widget (e.g. insert tab character) instead of focus change navigation. /// when true, Tab / Shift+Tab presses are processed internally in widget (e.g. insert tab character) instead of focus change navigation.
@property bool wantTabs() { @property bool wantTabs() {
@ -2366,6 +2373,10 @@ class LogWidget : EditBox {
/// when true, automatically scrolls down when new lines are appended (usually being reset by scrollbar interaction) /// when true, automatically scrolls down when new lines are appended (usually being reset by scrollbar interaction)
@property void scrollLock(bool flg) { _scrollLock = flg; } @property void scrollLock(bool flg) { _scrollLock = flg; }
this() {
this(null);
}
this(string ID) { this(string ID) {
super(ID); super(ID);
_scrollLock = true; _scrollLock = true;
@ -2425,3 +2436,6 @@ class LogWidget : EditBox {
} }
} }
import dlangui.widgets.metadata;
mixin(registerWidgets!(EditLine, EditBox, LogWidget)());

View File

@ -56,6 +56,7 @@ import dlangui.widgets.widget;
import dlangui.widgets.controls; import dlangui.widgets.controls;
import dlangui.widgets.scroll; import dlangui.widgets.scroll;
import std.conv; import std.conv;
import std.algorithm : equal;
/** /**
* Data provider for GridWidget. * Data provider for GridWidget.
@ -346,10 +347,19 @@ class GridWidgetBase : ScrollWidgetBase {
return this; return this;
} }
/// set bool property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setBoolProperty", "bool",
"showColHeaders", "showColHeaders", "rowSelect"));
/// set int property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setIntProperty", "int",
"headerCols", "headerRows", "fixedCols", "fixedRows", "cols", "rows", "defColumnWidth", "defRowHeight"));
/// flag to enable column headers /// flag to enable column headers
@property bool showColHeaders() { @property bool showColHeaders() {
return _showColHeaders; return _showColHeaders;
} }
@property GridWidgetBase showColHeaders(bool show) { @property GridWidgetBase showColHeaders(bool show) {
if (_showColHeaders != show) { if (_showColHeaders != show) {
_showColHeaders = show; _showColHeaders = show;
@ -1353,3 +1363,5 @@ class StringGridWidget : StringGridWidgetBase {
} }
import dlangui.widgets.metadata;
mixin(registerWidgets!(StringGridWidget)());

View File

@ -810,6 +810,10 @@ class TableLayout : WidgetGroupDefaultDrawing {
return (childCount + (_colCount - 1)) / _colCount * _colCount; return (childCount + (_colCount - 1)) / _colCount * _colCount;
} }
/// set int property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setIntProperty", "int",
"colCount"));
/// Measure widget according to desired width and height constraints. (Step 1 of two phase layout). /// Measure widget according to desired width and height constraints. (Step 1 of two phase layout).
override void measure(int parentWidth, int parentHeight) { override void measure(int parentWidth, int parentHeight) {
Rect m = margins; Rect m = margins;

View File

@ -1474,11 +1474,8 @@ class Widget {
} }
/// set string property value, for ML loaders /// set string property value, for ML loaders
bool setProperty(string name, string value) { bool setStringProperty(string name, string value) {
if (name.equal("id")) { mixin(generatePropertySetters("id"));
id = value;
return true;
}
if (name.equal("text")) { if (name.equal("text")) {
text = UIString(value); text = UIString(value);
return true; return true;
@ -1487,7 +1484,7 @@ class Widget {
} }
/// set string property value, for ML loaders /// set string property value, for ML loaders
bool setProperty(string name, dstring value) { bool setDstringProperty(string name, dstring value) {
if (name.equal("text")) { if (name.equal("text")) {
text = UIString(value); text = UIString(value);
return true; return true;
@ -1496,7 +1493,7 @@ class Widget {
} }
/// set string property value, for ML loaders /// set string property value, for ML loaders
bool setProperty(string name, UIString value) { bool setUistringProperty(string name, UIString value) {
if (name.equal("text")) { if (name.equal("text")) {
text = value; text = value;
return true; return true;
@ -1505,37 +1502,22 @@ class Widget {
} }
/// set string property value, for ML loaders /// set string property value, for ML loaders
bool setProperty(string name, bool value) { bool setBoolProperty(string name, bool value) {
if (name.equal("enabled")) { mixin(generatePropertySetters("enabled", "clickable", "checkable", "focusable", "checked"));
enabled = value;
return true;
}
if (name.equal("clickable")) {
clickable = value;
return true;
}
if (name.equal("checkable")) {
checkable = value;
return true;
}
if (name.equal("checked")) {
checked = value;
return true;
}
return false; return false;
} }
/// set double property value, for ML loaders /// set double property value, for ML loaders
bool setProperty(string name, double value) { bool setDoubleProperty(string name, double value) {
if (name.equal("alpha")) { if (name.equal("alpha")) {
int n = cast(int)(value * 255); int n = cast(int)(value * 255);
setProperty(name, n); setIntProperty(name, n);
} }
return false; return false;
} }
/// set int property value, for ML loaders /// set int property value, for ML loaders
bool setProperty(string name, int value) { bool setIntProperty(string name, int value) {
if (name.equal("alpha")) { if (name.equal("alpha")) {
if (value < 0) if (value < 0)
value = 0; value = 0;
@ -1544,38 +1526,7 @@ class Widget {
alpha = cast(ushort)value; alpha = cast(ushort)value;
return true; return true;
} }
if (name.equal("minWidth")) { mixin(generatePropertySetters("minWidth", "maxWidth", "minHeight", "maxHeight", "layoutWidth", "layoutHeight", "textColor", "backgroundColor"));
minWidth = value;
return true;
}
if (name.equal("maxWidth")) {
maxWidth = value;
return true;
}
if (name.equal("minHeight")) {
minHeight = value;
return true;
}
if (name.equal("maxHeight")) {
maxHeight = value;
return true;
}
if (name.equal("layoutWidth")) {
layoutWidth = value;
return true;
}
if (name.equal("layoutHeight")) {
layoutHeight = value;
return true;
}
if (name.equal("textColor")) {
textColor = cast(uint)value;
return true;
}
if (name.equal("backgroundColor")) {
backgroundColor = cast(uint)value;
return true;
}
if (name.equal("margins")) { // use same value for all sides if (name.equal("margins")) { // use same value for all sides
margins = Rect(value, value, value, value); margins = Rect(value, value, value, value);
return true; return true;
@ -1587,16 +1538,9 @@ class Widget {
return false; return false;
} }
/// set int property value, for ML loaders /// set Rect property value, for ML loaders
bool setProperty(string name, Rect value) { bool setRectProperty(string name, Rect value) {
if (name.equal("margins")) { mixin(generatePropertySetters("margins", "padding"));
margins = value;
return true;
}
if (name.equal("padding")) {
padding = value;
return true;
}
return false; return false;
} }
} }
@ -1756,4 +1700,30 @@ mixin template ActionTooltipSupport() {
} }
} }
/// use in mixin to set this object property with name propName with value of variable value if variable name matches propName
string generatePropertySetter(string propName) {
return " if (name.equal(\"" ~ propName ~ "\")) { \n" ~
" " ~ propName ~ " = value;\n" ~
" return true;\n" ~
" }\n";
}
/// use in mixin to set this object properties with names from parameter list with value of variable value if variable name matches propName
string generatePropertySetters(string[] propNames...) {
string res;
foreach(propName; propNames)
res ~= generatePropertySetter(propName);
return res;
}
/// use in mixin for method override to set this object properties with names from parameter list with value of variable value if variable name matches propName
string generatePropertySettersMethodOverride(string methodName, string typeName, string[] propNames...) {
return " override bool " ~ methodName ~ "(string name, " ~ typeName ~ " value) {\n" ~
" return super." ~ methodName ~ "(name, value);\n" ~
" }\n";
string res;
foreach(propName; propNames)
res ~= generatePropertySetter(propName);
return res;
}