From 56be5c89d8121432478f58a886be5aee77a34866 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 2 Jun 2008 13:52:54 +0000 Subject: First real shot at lua validation. --- client/lua.cc | 2 ++ client/widgets/lineedit.cc | 9 +++++++-- client/widgets/widget.cc | 16 ++++++++++++++-- client/widgets/widget.h | 4 +++- 4 files changed, 26 insertions(+), 5 deletions(-) (limited to 'client') diff --git a/client/lua.cc b/client/lua.cc index 46c8fbd..c803e23 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -42,6 +42,8 @@ LUA::LUA(Variables &variables) var++; } + // printf("preload: [%s]\n", preload.c_str()); + int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload"); switch(s) { case 0: //no errors; diff --git a/client/widgets/lineedit.cc b/client/widgets/lineedit.cc index 26c2760..a6f2e83 100644 --- a/client/widgets/lineedit.cc +++ b/client/widgets/lineedit.cc @@ -56,8 +56,13 @@ void LineEdit::changed() QPalette palette; if(regexpValidator()) { - // valid string - palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255))); + if(luaValidator()) { + // valid string + palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255))); + } else { + // invalid string + palette.setBrush(QPalette::Base, QBrush(QColor(200, 230, 200))); + } } else { // invalid string palette.setBrush(QPalette::Base, QBrush(QColor(230, 200, 200))); diff --git a/client/widgets/widget.cc b/client/widgets/widget.cc index b128a53..d17754e 100644 --- a/client/widgets/widget.cc +++ b/client/widgets/widget.cc @@ -38,7 +38,7 @@ Widget::Widget(QDomNode &node) } if(elem.hasAttribute("lua_validator")) { - // lua_validator = elem.attribute("lua_validator"); + lua_validator = elem.attribute("lua_validator"); hasluavalidator = true; } else { hasluavalidator = false; @@ -75,5 +75,17 @@ bool Widget::regexpValidator() bool Widget::luaValidator() { - return !hasluavalidator || true; + if(!hasluavalidator) return true; + + Variables v; + v["value"] = getValue().toStdString(); + LUA lua(v); + + std::string program = lua_validator.toStdString(); + + std::string result = lua.run(program); + + // printf("Running [%s] => [%s]\n", program.c_str(), result.c_str()); + + return result == "true"; } diff --git a/client/widgets/widget.h b/client/widgets/widget.h index 89610bb..391f2e9 100644 --- a/client/widgets/widget.h +++ b/client/widgets/widget.h @@ -49,9 +49,11 @@ protected: private: QRegExp rx; - LUA *lua; + bool hasregexpvalidator; bool hasluavalidator; + + QString lua_validator; }; #endif/*__PRACRO_WIDGET_H__*/ -- cgit v1.2.3