From dbab8458dcce186e7eb7a114a83f759d7db5445a Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 3 Aug 2010 12:30:27 +0000 Subject: New scripting interface part1. --- client/client.pro | 2 + client/formatparser.cc | 2 +- client/lua.cc | 193 +++++-------------------------------- client/lua.h | 2 +- client/luawidget.cc | 222 +++++++++++++++++++++++++++++++++++++++++++ client/luawidget.h | 39 ++++++++ client/macrowindow.cc | 13 ++- client/macrowindow.h | 2 +- client/netcom.cc | 4 +- client/widgets/datetime.cc | 12 +-- client/widgets/metawidget.cc | 4 +- client/widgets/multilist.cc | 2 +- client/widgets/widget.cc | 20 ++-- client/widgets/widget.h | 8 +- 14 files changed, 327 insertions(+), 198 deletions(-) create mode 100644 client/luawidget.cc create mode 100644 client/luawidget.h diff --git a/client/client.pro b/client/client.pro index 90e0b66..9f2780d 100644 --- a/client/client.pro +++ b/client/client.pro @@ -35,6 +35,7 @@ HEADERS += \ collapser.h \ formatparser.h \ lua.h \ + luawidget.h \ macro.h \ macrowindow.h \ mainwindow.h \ @@ -71,6 +72,7 @@ SOURCES += \ collapser.cc \ formatparser.cc \ lua.cc \ + luawidget.cc \ macro.cc \ macrowindow.cc \ mainwindow.cc \ diff --git a/client/formatparser.cc b/client/formatparser.cc index 751c83e..62f0b21 100644 --- a/client/formatparser.cc +++ b/client/formatparser.cc @@ -67,7 +67,7 @@ static QString format_parser_pracro(QString format, QVector< Widget *> widgets) QVector< Widget* >::iterator i = widgets.begin(); while (i != widgets.end()) { Widget* w = *i; - if(w->getName() == var) resume += w->getValue(); + if(w->name() == var) resume += w->getValue(); i++; } } diff --git a/client/lua.cc b/client/lua.cc index 5e6850b..19b94c0 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -30,14 +30,16 @@ #include "widgets/widget.h" +#include "luawidget.h" + #define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten" -static int _enable(lua_State *L) +static int get_widget(lua_State *L) { int n = lua_gettop(L); // number of arguments if(n != 1) { char errstr[512]; - sprintf(errstr, "Number of args expected 0, got %d", n); + sprintf(errstr, "Number of args expected 1, got %d", n); lua_pushstring(L, errstr); lua_error(L); return 1; @@ -54,127 +56,21 @@ static int _enable(lua_State *L) return 1; } - lua->enable(name); + Widget *widget = lua->getWidget(name); - return 0; -} + printf("FIND: %s (%p)\n", name.toStdString().c_str(), widget); -static int _disable(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 1) { - char errstr[512]; - sprintf(errstr, "Number of args expected 0, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 1; + if(widget) { + wdg_make_widget(L, widget); + } else { + lua_pushnil(L); } - QString name = lua_tostring(L, lua_gettop(L)); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - lua->disable(name); - - return 0; -} - -static int _getValue(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 1) { - char errstr[512]; - sprintf(errstr, "Number of args expected 0, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 1; - } - - QString name = lua_tostring(L, lua_gettop(L)); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - QString value = lua->getValue(name); - lua_pushstring(L, value.toStdString().c_str()); + printf("DONE\n"); return 1; } -static int _setValue(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 2) { - char errstr[512]; - sprintf(errstr, "Number of args expected 2, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - QString value = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - QString name = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - lua->setValue(name, value); - - return 0; -} - -static int _setVisible(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 2) { - char errstr[512]; - sprintf(errstr, "Number of args expected 2, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - bool value = lua_toboolean(L, lua_gettop(L)); - lua_pop(L, 1); - QString name = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - lua->setVisible(name, value); - - return 0; -} - - LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets) { this->widgets = widgets; @@ -191,11 +87,9 @@ LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets) lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. - lua_register(L, "getValue", _getValue); - lua_register(L, "setValue", _setValue); - lua_register(L, "enable", _enable); - lua_register(L, "disable", _disable); - lua_register(L, "setVisible", _setVisible); + lua_register(L, "widget", get_widget); + + register_widget(L); } LUA::~LUA() @@ -203,38 +97,7 @@ LUA::~LUA() lua_close(L); } -QString LUA::getValue(QString name) -{ - Widget *widget = getWidget(name); - if(widget) return widget->getValue(); - return ""; -} - -void LUA::setValue(QString name, QString value) -{ - Widget *widget = getWidget(name); - if(widget) widget->setValue(value); -} - -void LUA::enable(QString name) -{ - Widget *widget = getWidget(name); - if(widget) widget->enable(); -} - -void LUA::disable(QString name) -{ - Widget *widget = getWidget(name); - if(widget) widget->disable(); -} - -void LUA::setVisible(QString name, bool value) -{ - Widget *widget = getWidget(name); - if(widget) widget->setVisibility(value); -} - -bool LUA::runValidator(QString program, QString name, QString value) +bool LUA::runValidator(QString program, Widget *widget, QString name, QString value) { if(L == NULL) { error("LUA state not initialized!"); @@ -252,7 +115,12 @@ bool LUA::runValidator(QString program, QString name, QString value) lua_pushstring(L, name.toStdString().c_str()); lua_setglobal(L, "name"); - int top = lua_gettop(L); + if(widget) { + wdg_make_widget(L, widget); + lua_setglobal(L, "this"); + } + + // int top = lua_gettop(L); if(luaL_loadbuffer(L, program.toStdString().c_str(), @@ -268,20 +136,7 @@ bool LUA::runValidator(QString program, QString name, QString value) return false; } - if(top != lua_gettop(L) - 1) { - error("Program did not return a single value.\n"); - return false; - } - - if(lua_isboolean(L, lua_gettop(L)) == false) { - error("Program did not return a boolean value.\n"); - return false; - } - - bool res = lua_toboolean(L, lua_gettop(L)); - lua_pop(L, 1); - - return res; + return true; } QString LUA::runParser(QString program) @@ -335,7 +190,7 @@ Widget *LUA::getWidget(QString name) QVector< Widget* >::iterator i = widgets->begin(); while (i != widgets->end()) { Widget* w = *i; - if(name == w->getName()) return w; + if(name == w->name()) return w; i++; } @@ -343,7 +198,7 @@ Widget *LUA::getWidget(QString name) QVector< Widget* >::iterator j = auxwidgets->begin(); while (j != auxwidgets->end()) { Widget* w = *j; - if(name == w->getName()) return w; + if(name == w->name()) return w; j++; } } diff --git a/client/lua.h b/client/lua.h index ba074dd..d12bc6e 100644 --- a/client/lua.h +++ b/client/lua.h @@ -41,7 +41,7 @@ public: LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets = NULL); ~LUA(); - bool runValidator(QString program, QString name, QString value); + bool runValidator(QString program, Widget *widget, QString name, QString value); QString runParser(QString program); QString getValue(QString name); diff --git a/client/luawidget.cc b/client/luawidget.cc new file mode 100644 index 0000000..c8e6017 --- /dev/null +++ b/client/luawidget.cc @@ -0,0 +1,222 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * luawidget.cc + * + * Tue Aug 3 10:17:02 CEST 2010 + * Copyright 2010 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "luawidget.h" + +#define luaL_checkbool(L, i) \ + (lua_isboolean(L,i) ? lua_toboolean(L,i) : luaL_checkint(L,i)) + +typedef struct wdg_userdata { + Widget *widget; +} wdg_userdata; + +static int wdg_name(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + lua_pushstring(L, wdgu->widget->name().toStdString().c_str()); + + return 1; +} + +static int wdg_type(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + // return error code + lua_pushstring(L, wdgu->widget->type().toStdString().c_str()); + + return 1; +} + +static int wdg_value(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + lua_pushstring(L, wdgu->widget->getValue().toStdString().c_str()); + + return 1; +} + +static int wdg_set_value(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + const char *val = luaL_checkstring(L, 2); + + wdgu->widget->setValue(val); + + return 0; +} + +static int wdg_enabled(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + lua_pushboolean(L, wdgu->widget->isDisabled() == false); + + return 1; +} + +static int wdg_set_enabled(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + bool val = luaL_checkbool(L, 2); + + if(val) wdgu->widget->enable(); + else wdgu->widget->disable(); + + return 0; +} + +static int wdg_visible(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + lua_pushboolean(L, wdgu->widget->getVisibility()); + + return 1; +} + +static int wdg_set_visible(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + bool val = luaL_checkbool(L, 2); + + wdgu->widget->setVisibility(val); + + return 0; +} + +static int wdg_valid(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + lua_pushboolean(L, wdgu->widget->isValid()); + + return 1; +} + +static int wdg_set_valid(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + bool val = luaL_checkbool(L, 2); + + wdgu->widget->setValid(val); + + return 0; +} + +static int wdg_close(lua_State *L) +{ + wdg_userdata *wdgu; + + wdgu = (wdg_userdata *)luaL_checkudata(L, 1, "Widget"); + luaL_argcheck(L, wdgu, 1, "widget expected"); + + return 0; +} + +static const struct luaL_reg wdg_meths[] = +{ + {"name", wdg_name}, + {"type", wdg_type}, + + {"value", wdg_value}, + {"setValue", wdg_set_value}, + + {"enabled", wdg_enabled}, + {"setEnabled", wdg_set_enabled}, + + {"visible", wdg_visible}, + {"setVisible", wdg_set_visible}, + + {"valid", wdg_valid}, + {"setValid", wdg_set_valid}, + + {"close", wdg_close}, + {"__gc", wdg_close}, + {NULL, NULL} +}; + +int wdg_make_widget(lua_State *L, Widget *widget) +{ + wdg_userdata *wdgu; + wdgu = (wdg_userdata *)lua_newuserdata(L, sizeof(wdg_userdata)); + + luaL_getmetatable(L, "Widget"); + lua_setmetatable(L, -2); + + wdgu->widget = widget; + + return 1; +} + +void register_widget(lua_State *L) +{ + luaL_newmetatable(L, "Widget"); + + // metatable.__index = metatable + lua_pushliteral(L, "__index"); + lua_pushvalue(L, -2); + lua_rawset(L, -3); + + luaL_openlib(L, NULL, wdg_meths, 0); +} diff --git a/client/luawidget.h b/client/luawidget.h new file mode 100644 index 0000000..db615b5 --- /dev/null +++ b/client/luawidget.h @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * luawidget.h + * + * Tue Aug 3 10:17:02 CEST 2010 + * Copyright 2010 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_LUAWIDGET_H__ +#define __PRACRO_LUAWIDGET_H__ + +#include +#include + +#include "widgets/widget.h" + +void register_widget(lua_State *L); +int wdg_make_widget(lua_State *L, Widget *widget); + +#endif/*__PRACRO_LUAWIDGET_H__*/ diff --git a/client/macrowindow.cc b/client/macrowindow.cc index b37fe5f..4c51c07 100644 --- a/client/macrowindow.cc +++ b/client/macrowindow.cc @@ -105,9 +105,8 @@ void MacroWindow::initMacro(QDomNode &node) if(xml_elem.hasAttribute("language") && xml_elem.attribute("language") == "lua") { - if(xml_elem.hasAttribute("name")) { - luaprograms[xml_elem.attribute("name")] = xml_elem.text(); - } + // luaprograms.push_back(xml_elem.text()); + this->lua->runValidator(xml_elem.text(), NULL, "preload", ""); } else { printf("Unknown script type %s\n", xml_elem.attribute("language").toStdString().c_str()); } @@ -230,7 +229,7 @@ void MacroWindow::cont(QString name) QVector< Widget* >::iterator i=widgets.begin(); while (i != widgets.end()) { Widget* w = *i; - if(w->getName() == name) { + if(w->name() == name) { macro = w->getValue(); } i++; @@ -257,7 +256,7 @@ void MacroWindow::cont_nocommit(QString name) QVector< Widget* >::iterator i=widgets.begin(); while (i != widgets.end()) { Widget* w = *i; - if(w->getName() == name) { + if(w->name() == name) { macro = w->getValue(); } i++; @@ -288,14 +287,14 @@ Widget *MacroWindow::getWidget(QString name) QVector< Widget* >::iterator i = widgets.begin(); while (i != widgets.end()) { Widget* w = *i; - if(name == w->getName()) return w; + if(name == w->name()) return w; i++; } QVector< Widget* >::iterator j = auxwidgets.begin(); while (j != auxwidgets.end()) { Widget* w = *j; - if(name == w->getName()) return w; + if(name == w->name()) return w; j++; } diff --git a/client/macrowindow.h b/client/macrowindow.h index 51a675e..279394f 100644 --- a/client/macrowindow.h +++ b/client/macrowindow.h @@ -53,7 +53,7 @@ public: bool isClosed(); - QMap< QString, QString > luaprograms; + QVector< QString > luaprograms; LUA *lua; diff --git a/client/netcom.cc b/client/netcom.cc index 97f9ee1..f569a6f 100644 --- a/client/netcom.cc +++ b/client/netcom.cc @@ -189,9 +189,9 @@ QDomDocument NetCom::send(QVector< Widget* > widgets, QString templ, while (i != widgets.end()) { Widget* w = *i; - if(!w->isDisabled() && w->getName() != "") { + if(!w->isDisabled() && w->name() != "") { QDomElement field_elem = doc.createElement("field"); - field_elem.setAttribute("name", w->getName()); + field_elem.setAttribute("name", w->name()); field_elem.setAttribute("value", w->getValue()); commit_elem.appendChild(field_elem); } diff --git a/client/widgets/datetime.cc b/client/widgets/datetime.cc index f5e74e7..0b91a40 100644 --- a/client/widgets/datetime.cc +++ b/client/widgets/datetime.cc @@ -47,28 +47,28 @@ DateTime::DateTime(QDomNode &node, MacroWindow *macrowindow) break; case 2: - setDisplayFormat("MMM yyyy"); + setDisplayFormat("MMMM yyyy"); break; case 3: - setDisplayFormat("dd MMM yyyy"); + setDisplayFormat("dd MMMM yyyy"); break; case 4: - setDisplayFormat("dd MMM yyyy hh"); + setDisplayFormat("dd MMMM yyyy hh"); break; case 5: - setDisplayFormat("dd MMM yyyy hh:mm"); + setDisplayFormat("dd MMMM yyyy hh:mm"); break; case 6: - setDisplayFormat("dd MMM yyyy hh:mm:ss"); + setDisplayFormat("dd MMMM yyyy hh:mm:ss"); break; case 7: default: - setDisplayFormat("dd MMM yyyy hh:mm:ss:zzz"); + setDisplayFormat("dd MMMM yyyy hh:mm:ss:zzz"); break; } diff --git a/client/widgets/metawidget.cc b/client/widgets/metawidget.cc index 5491790..5933212 100644 --- a/client/widgets/metawidget.cc +++ b/client/widgets/metawidget.cc @@ -68,7 +68,7 @@ MetaWidget::MetaWidget(QDomNode &node, MacroWindow *macrowindow) while (i != widgets.end()) { Widget* w = *i; if(format != "") format += ", "; - format += "${" + w->getName() + "}"; + format += "${" + w->name() + "}"; i++; } } @@ -121,7 +121,7 @@ bool MetaWidget::isValid() Widget* w = *i; if(w->isValid() == false) { MessageBox::critical(NULL, "Fejl", - "Et af inputfelterne (" + w->getName() + "Et af inputfelterne (" + w->name() + ") er ikke udfyldt korrekt, prøv igen.\n" , MessageBox::Ok); return false; diff --git a/client/widgets/multilist.cc b/client/widgets/multilist.cc index 2788383..01a2d77 100644 --- a/client/widgets/multilist.cc +++ b/client/widgets/multilist.cc @@ -101,7 +101,7 @@ MultiList::MultiList(QDomNode &node, MacroWindow *macrowindow) QString iwname = elem.attribute("innerwidget"); QVector< Widget* >::iterator ws = widgets.begin(); while(ws != widgets.end()) { - if((*ws)->getName() == iwname) { + if((*ws)->name() == iwname) { innerwidget = *ws; innerwidget->connectFrom(SIGNAL(wasChanged()), this, SLOT(changed())); } diff --git a/client/widgets/widget.cc b/client/widgets/widget.cc index 5899839..c17633f 100644 --- a/client/widgets/widget.cc +++ b/client/widgets/widget.cc @@ -31,7 +31,9 @@ Widget::Widget(QDomNode &node, MacroWindow *macrowindow) QDomElement elem = node.toElement(); this->macrowindow = macrowindow; - + + widget_type = elem.tagName(); + if(elem.hasAttribute("name")) { widget_name = elem.attribute("name"); } else { @@ -63,11 +65,16 @@ Widget::Widget(QDomNode &node, MacroWindow *macrowindow) initial_value = ""; } -QString Widget::getName() +QString Widget::name() { return widget_name; } +QString Widget::type() +{ + return widget_type; +} + void Widget::setValue(QString, QString) { } @@ -85,11 +92,12 @@ bool Widget::regexpValidator() bool Widget::luaValidator() { if(!hasluaprogram) return true; - - if(macrowindow->luaprograms.contains(luaprogram) == false) return false; - QString program = macrowindow->luaprograms.value(luaprogram); - return macrowindow->lua->runValidator(program, getName(), getValue()); + QString program = ""; + + program += luaprogram; + + return macrowindow->lua->runValidator(program, this, name(), getValue()); } void Widget::setInitialValue(QString value) diff --git a/client/widgets/widget.h b/client/widgets/widget.h index bd84705..732dce9 100644 --- a/client/widgets/widget.h +++ b/client/widgets/widget.h @@ -43,14 +43,17 @@ public: virtual void setValue(QString value, QString source = ""); virtual bool isValid(); + virtual void setValid(bool valid) { valid = valid; } virtual void disable() {} virtual void enable() {} virtual bool isDisabled() { return false; } virtual void setVisibility(bool) {} - - QString getName(); + virtual bool getVisibility() { return true; } + + QString name(); + QString type(); /** * Connect some signal from this object to some slot in some other object. @@ -70,6 +73,7 @@ public: protected: QString widget_name; + QString widget_type; bool luaValidator(); bool regexpValidator(); -- cgit v1.2.3