diff options
author | deva <deva> | 2008-06-03 14:45:48 +0000 |
---|---|---|
committer | deva <deva> | 2008-06-03 14:45:48 +0000 |
commit | 0febf6ea9cbd1a6e04e41339fc46d2e6b07da5e7 (patch) | |
tree | da9cee1a15609b8aa2ed556160c5660819621db2 | |
parent | bb3fad646f94739a876869464a484c99795bce61 (diff) |
LUA rocks
31 files changed, 298 insertions, 137 deletions
diff --git a/client/NOTES b/client/NOTES new file mode 100644 index 0000000..34b730b --- /dev/null +++ b/client/NOTES @@ -0,0 +1,66 @@ +-*- auto-fill -*- +############################# +# LUA programmer i klienten # +############################# +OnChanged eventen kan som udgangspunkt daekke alle behov for +udfoerelse af LUA kode. + +OnChanged lua koden skal returnere en boolean som er true hvis feltet +er valid, eller false hvis det ikke er. + +Der skal stilles en raekke metoder til raadighed for programmerne, som +kan interagere med felterne i makroen. + - getValue(feltnavn) + - setValue(feltnavn) + +Feltets egen vaerdig skal vaere tilknyttet lua variables 'value' saa +det er let at lave simpel validering af feltets vaerdi. + +Der skal som udgangspunkt kun vaere et LUA objekt pr. macro, for at +sikre gode svartider. Naar et nyt program skal udfoeres skal stakken +saaledes kunne genbruges (tidligere vaerdier skal fjernes og nye skal +indsaettes). + +LUA programmerne i XML'en: +<lineedit name="linse4" + regexp="[24]*" + map="axis" + lua_validator="if( tonumber(value) == 42 ) then return true else return false end" + value="90K"/> + +eller maaske + +<lineedit name="linse4" + regexp="[24]*" + map="axis" + value="90K"> + if( tonumber(value) == 42 ) + then + return true + else + return false + end +</lineedit> + +eller en loesning inspireret af query/maps paa serveren: +<luaprograms> + <luaprogram name="dims"> + if( tonumber(value) == 42 ) + then + return true + else + return false + end + </luaprogram> +</luaprograms> +<lineedit name="linse4" + regexp="[24]*" + map="axis" + lua="dims" + value="90K"/> + +Det skal helt klart vaere sidstnaevnte! + +Der kan laves sanity checks paa runtime for at sikre at der ikke +opstaar uendelige ulykker. +F.eks kan man checke om man er i gang med setValue paa feltet selv.
\ No newline at end of file diff --git a/client/lua.cc b/client/lua.cc index c803e23..2299acc 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -26,40 +26,91 @@ */ #include "lua.h" -LUA::LUA(Variables &variables) +#include "macrowindow.h" + +#define GLOBAL_POINTER "_pracroGlobalLUAObjectPointer" + +LUA *glua; + +static int _getValue(lua_State *L) { - L = luaL_newstate(); - if(L == NULL) { - // throw LUADataParserException("Could not create LUA state."); + int n = lua_gettop(L); // number of arguments + if(n != 1) { + char errstr[512]; + sprintf(errstr, "Number of args expected 0, got %d", n); + fprintf(stderr, errstr); + lua_pushstring(L, errstr); + lua_error(L); + return 1; } - luaL_openlibs(L); + QString name = lua_tostring(L, lua_gettop(L)); - std::string preload; - Variables::iterator var = variables.begin(); - while(var != variables.end()) { - preload += (*var).first + " = \"" + (*var).second + "\"\n"; - var++; + lua_getglobal(L, GLOBAL_POINTER); + LUA *lua = glua;//(LUA*)lua_touserdata(L, 1); + + if(!lua) { + printf("No LUA pointer!\n"); + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; } - - // printf("preload: [%s]\n", preload.c_str()); - int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload"); - switch(s) { - case 0: //no errors; - break; - case LUA_ERRSYNTAX: //syntax error during pre-compilation; - case LUA_ERRMEM: //memory allocation error. - case LUA_ERRFILE: //cannot open/read the file. - //throw LUADataParserException(lua_tostring(L, lua_gettop(L))); - break; - default: - //throw LUADataParserException("Unknown return value of luaL_loadfile."); - break; + QString value = lua->getValue(name); + lua_pushstring(L, value.toStdString().c_str()); + + 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 0, got %d", n); + fprintf(stderr, errstr); + lua_pushstring(L, errstr); + lua_error(L); + return 0; } - // Run program (init) - lua_pcall(L, 0, LUA_MULTRET, 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 = glua;//(LUA*)lua_touserdata(L, 1); + + if(!lua) { + printf("No LUA pointer!\n"); + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } + + lua->setValue(name, value); + + return 0; +} + +LUA::LUA(MacroWindow *macrowindow) +{ + glua = this; + this->macrowindow = macrowindow; + + L = luaL_newstate(); + if(L == NULL) { + // throw LUADataParserException("Could not create LUA state."); + } + + luaL_openlibs(L); + + 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::~LUA() @@ -67,9 +118,28 @@ LUA::~LUA() lua_close(L); } -std::string LUA::run(std::string &program) +QString LUA::getValue(QString name) +{ + return macrowindow->getValue(name); +} + +void LUA::setValue(QString name, QString value) +{ + macrowindow->setValue(name, value); +} + +bool LUA::run(QString program, QString value) { - int s = luaL_loadbuffer(L, program.c_str(), program.size(), "program"); + if(macrowindow->luaprograms.contains(program) == false) return false; + + QString luacode = "value = " + value + "\n"; + QString luaprogram = macrowindow->luaprograms.value(program); + luacode += luaprogram; + + int s = luaL_loadbuffer(L, + luacode.toStdString().c_str(), + luacode.size(), + program.toStdString().c_str()); switch(s) { case 0: //no errors; break; @@ -86,7 +156,7 @@ std::string LUA::run(std::string &program) // Run the loaded code lua_pcall(L, 0, LUA_MULTRET, 0); - std::string res = lua_tostring(L, lua_gettop(L)); + bool res = lua_toboolean(L, lua_gettop(L)); lua_pop(L, 1); return res; diff --git a/client/lua.h b/client/lua.h index 0c69b04..92ae809 100644 --- a/client/lua.h +++ b/client/lua.h @@ -30,17 +30,23 @@ #include <lua.hpp> #include <lauxlib.h> -#include "variables.h" +#include <QString> + +class MacroWindow; class LUA { public: - LUA(Variables &variables); + LUA(MacroWindow *macrowindow); ~LUA(); - std::string run(std::string &program); + bool run(QString program, QString value); + + QString getValue(QString name); + void setValue(QString name, QString value); private: lua_State *L; + MacroWindow *macrowindow; }; #endif/*__PRACRO_LUA_H__*/ diff --git a/client/macrowindow.cc b/client/macrowindow.cc index 74e8d54..aab9857 100644 --- a/client/macrowindow.cc +++ b/client/macrowindow.cc @@ -35,6 +35,8 @@ #include <QDomNode> #include <QByteArray> +#include "lua.h" + extern QString cpr; extern QString user; extern QString host; @@ -45,12 +47,15 @@ MacroWindow::MacroWindow(QDomDocument *xml_doc) { isclosed = false; + this->lua = new LUA(this); + // Execute the recursive function with documentElement recurser(xml_doc->documentElement(), NULL); } MacroWindow::~MacroWindow() { + delete lua; } void MacroWindow::recurser(QDomNode xml_node, QWidget *parent) @@ -63,31 +68,39 @@ void MacroWindow::recurser(QDomNode xml_node, QWidget *parent) if(xml_elem.hasAttribute("name")) macro = xml_elem.attribute("name"); if(xml_elem.hasAttribute("version")) version = xml_elem.attribute("version"); + } else if(xml_elem.tagName() == "luaprograms") { + // Nothing to do here + } else if(xml_elem.tagName() == "luaprogram") { + + if(xml_elem.hasAttribute("name")) { + luaprograms[xml_elem.attribute("name")] = xml_elem.text(); + } + } else if(xml_elem.tagName() == "window") { - Window *window = new Window(xml_elem); + Window *window = new Window(xml_elem, this); widget = window; mainwidget = window; } else if(xml_elem.tagName() == "frame") { if(xml_elem.hasAttribute("caption")) { - GroupBox *frame = new GroupBox(xml_elem); + GroupBox *frame = new GroupBox(xml_elem, this); widget = frame; } else { - Frame *frame = new Frame(xml_elem); + Frame *frame = new Frame(xml_elem, this); widget = frame; } } else if(xml_elem.tagName() == "label") { - Label *label = new Label(xml_elem); + Label *label = new Label(xml_elem, this); widget = label; } else if(xml_elem.tagName() == "lineedit") { - LineEdit *lineedit = new LineEdit(xml_elem); + LineEdit *lineedit = new LineEdit(xml_elem, this); widgets.push_back(lineedit); widget = lineedit; } else if(xml_elem.tagName() == "button") { - PushButton *pushbutton = new PushButton(xml_elem); + PushButton *pushbutton = new PushButton(xml_elem, this); //connect(pushbutton, SIGNAL(act_continue()), main, SLOT(get_macro())); connect(pushbutton, SIGNAL(act_commit()), this, SLOT(commit())); connect(pushbutton, SIGNAL(act_reset()), this, SLOT(reset())); @@ -96,29 +109,29 @@ void MacroWindow::recurser(QDomNode xml_node, QWidget *parent) widget = pushbutton; } else if(xml_elem.tagName() == "textedit") { - TextEdit *textedit = new TextEdit(xml_elem); + TextEdit *textedit = new TextEdit(xml_elem, this); widgets.push_back(textedit); widget = textedit; } else if(xml_elem.tagName() == "checkbox") { - CheckBox *checkbox = new CheckBox(xml_elem); + CheckBox *checkbox = new CheckBox(xml_elem, this); widgets.push_back(checkbox); widget = checkbox; } else if(xml_elem.tagName() == "radiobuttons") { - RadioButtons *radiobuttons = new RadioButtons(xml_elem); + RadioButtons *radiobuttons = new RadioButtons(xml_elem, this); widgets.push_back(radiobuttons); widget = radiobuttons; //return; // Don't iterate children } else if(xml_elem.tagName() == "combobox") { - ComboBox *combobox = new ComboBox(xml_elem); + ComboBox *combobox = new ComboBox(xml_elem, this); widgets.push_back(combobox); widget = combobox; //return; // Don't iterate children } else if(xml_elem.tagName() == "listbox") { - ListBox *listbox = new ListBox(xml_elem); + ListBox *listbox = new ListBox(xml_elem, this); widgets.push_back(listbox); widget = listbox; //return; // Don't iterate children @@ -254,3 +267,26 @@ bool MacroWindow::isClosed() { return isclosed; } + +QString MacroWindow::getValue(QString name) +{ + // Iterate the different entries, and append their results to the commit string + QVector< Widget* >::iterator i=widgets.begin(); + while (i != widgets.end()) { + Widget* w = *i; + if(name == w->getName()) return w->getValue(); + i++; + } + return name + " - No such field!"; +} + +void MacroWindow::setValue(QString name, QString value) +{ + // Iterate the different entries, and append their results to the commit string + QVector< Widget* >::iterator i = widgets.begin(); + while (i != widgets.end()) { + Widget* w = *i; + if(name == w->getName()) w->setValue(value); + i++; + } +} diff --git a/client/macrowindow.h b/client/macrowindow.h index cad6124..7138464 100644 --- a/client/macrowindow.h +++ b/client/macrowindow.h @@ -27,12 +27,15 @@ #ifndef __PRACRO_MACROWINDOW_H__ #define __PRACRO_MACROWINDOW_H__ -#include "widgets/widget.h" #include <QDomDocument> #include <QWidget> #include <QDomNode> #include <QObject> #include <QVector> +#include <QMap> + +class LUA; +class Widget; class MacroWindow : public QObject { @@ -42,7 +45,14 @@ public: ~MacroWindow(); bool isClosed(); + + QMap< QString, QString > luaprograms; + LUA *lua; + + QString getValue(QString name); + void setValue(QString name, QString value); + public slots: void commit(); void reset(); diff --git a/client/variables.h b/client/variables.h deleted file mode 100644 index b58ab72..0000000 --- a/client/variables.h +++ /dev/null @@ -1,35 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * variables.h - * - * Mon Jun 2 09:58:52 CEST 2008 - * Copyright 2008 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_VARIABLES_H__ -#define __PRACRO_VARIABLES_H__ - -#include <string> -#include <map> - -typedef std::map< std::string, std::string > Variables; - -#endif/*__PRACRO_VARIABLES_H__*/ diff --git a/client/widgets/checkbox.cc b/client/widgets/checkbox.cc index 2ce3802..6b6f0e8 100644 --- a/client/widgets/checkbox.cc +++ b/client/widgets/checkbox.cc @@ -26,8 +26,8 @@ */ #include "checkbox.h" -CheckBox::CheckBox(QDomNode &node) - : QCheckBox(), Widget(node) +CheckBox::CheckBox(QDomNode &node, MacroWindow *macrowindow) + : QCheckBox(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/checkbox.h b/client/widgets/checkbox.h index 228723e..e24792c 100644 --- a/client/widgets/checkbox.h +++ b/client/widgets/checkbox.h @@ -34,7 +34,7 @@ class CheckBox : public QCheckBox, public Widget { public: - CheckBox(QDomNode &node); + CheckBox(QDomNode &node, MacroWindow *macrowindow); bool isValid(); diff --git a/client/widgets/combobox.cc b/client/widgets/combobox.cc index 480cada..aa2d78b 100644 --- a/client/widgets/combobox.cc +++ b/client/widgets/combobox.cc @@ -27,8 +27,8 @@ #include "combobox.h" #include <QDomNodeList> -ComboBox::ComboBox(QDomNode &node) - : QComboBox(), Widget(node) +ComboBox::ComboBox(QDomNode &node, MacroWindow *macrowindow) + : QComboBox(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/combobox.h b/client/widgets/combobox.h index ab92e64..a79b2aa 100644 --- a/client/widgets/combobox.h +++ b/client/widgets/combobox.h @@ -34,7 +34,7 @@ class ComboBox : public QComboBox, public Widget { public: - ComboBox(QDomNode &node); + ComboBox(QDomNode &node, MacroWindow *macrowindow); public slots: bool isValid(); diff --git a/client/widgets/frame.cc b/client/widgets/frame.cc index e2227cc..2560994 100644 --- a/client/widgets/frame.cc +++ b/client/widgets/frame.cc @@ -28,8 +28,8 @@ #include <QVBoxLayout> #include <QHBoxLayout> -Frame::Frame(QDomNode &node) - : QFrame(), Widget(node) +Frame::Frame(QDomNode &node, MacroWindow *macrowindow) + : QFrame(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/frame.h b/client/widgets/frame.h index 80aeded..1daf3d4 100644 --- a/client/widgets/frame.h +++ b/client/widgets/frame.h @@ -34,7 +34,7 @@ class Frame : public QFrame, public Widget { public: - Frame(QDomNode &node); + Frame(QDomNode &node, MacroWindow *macrowindow); public slots: QString getValue(); diff --git a/client/widgets/groupbox.cc b/client/widgets/groupbox.cc index dba97cd..8461a1b 100644 --- a/client/widgets/groupbox.cc +++ b/client/widgets/groupbox.cc @@ -28,8 +28,8 @@ #include <QVBoxLayout> #include <QHBoxLayout> -GroupBox::GroupBox(QDomNode &node) - : QGroupBox(), Widget(node) +GroupBox::GroupBox(QDomNode &node, MacroWindow *macrowindow) + : QGroupBox(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/groupbox.h b/client/widgets/groupbox.h index 0cc1890..567fa3f 100644 --- a/client/widgets/groupbox.h +++ b/client/widgets/groupbox.h @@ -34,7 +34,7 @@ class GroupBox : public QGroupBox, public Widget { public: - GroupBox(QDomNode &node); + GroupBox(QDomNode &node, MacroWindow *macrowindow); public slots: QString getValue(); diff --git a/client/widgets/label.cc b/client/widgets/label.cc index 95d53ac..c495c12 100644 --- a/client/widgets/label.cc +++ b/client/widgets/label.cc @@ -27,8 +27,8 @@ #include "label.h" #include <stdio.h> -Label::Label(QDomNode &node) - : QLabel(), Widget(node) +Label::Label(QDomNode &node, MacroWindow *macrowindow) + : QLabel(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/label.h b/client/widgets/label.h index 9c539e7..c1ede43 100644 --- a/client/widgets/label.h +++ b/client/widgets/label.h @@ -36,7 +36,7 @@ class Label : public QLabel, public Widget { Q_OBJECT public: - Label(QDomNode &node); + Label(QDomNode &node, MacroWindow *macrowindow); public slots: QString getValue(); diff --git a/client/widgets/lineedit.cc b/client/widgets/lineedit.cc index a6f2e83..7f270e9 100644 --- a/client/widgets/lineedit.cc +++ b/client/widgets/lineedit.cc @@ -27,8 +27,8 @@ #include "lineedit.h" #include <stdio.h> -LineEdit::LineEdit(QDomNode &node) - : QLineEdit(), Widget(node) +LineEdit::LineEdit(QDomNode &node, MacroWindow *macrowindow) + : QLineEdit(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); @@ -75,3 +75,8 @@ QString LineEdit::getValue() { return text(); } + +void LineEdit::setValue(QString value) +{ + setText(value); +} diff --git a/client/widgets/lineedit.h b/client/widgets/lineedit.h index faea440..41296a9 100644 --- a/client/widgets/lineedit.h +++ b/client/widgets/lineedit.h @@ -36,11 +36,12 @@ class LineEdit : public QLineEdit, public Widget { Q_OBJECT public: - LineEdit(QDomNode &node); + LineEdit(QDomNode &node, MacroWindow *macrowindow); public slots: void changed(); QString getValue(); + void setValue(QString value); private: bool valid; diff --git a/client/widgets/listbox.cc b/client/widgets/listbox.cc index c7151cc..3021bd9 100644 --- a/client/widgets/listbox.cc +++ b/client/widgets/listbox.cc @@ -27,8 +27,8 @@ #include "listbox.h" #include <QListWidgetItem> -ListBox::ListBox(QDomNode &node) - : QListWidget(), Widget(node) +ListBox::ListBox(QDomNode &node, MacroWindow *macrowindow) + : QListWidget(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/listbox.h b/client/widgets/listbox.h index e41838b..b283bcc 100644 --- a/client/widgets/listbox.h +++ b/client/widgets/listbox.h @@ -34,7 +34,7 @@ class ListBox : public QListWidget, public Widget { public: - ListBox(QDomNode &node); + ListBox(QDomNode &node, MacroWindow *macrowindow); public slots: bool isValid(); diff --git a/client/widgets/pushbutton.cc b/client/widgets/pushbutton.cc index d674162..a0540f3 100644 --- a/client/widgets/pushbutton.cc +++ b/client/widgets/pushbutton.cc @@ -27,8 +27,8 @@ #include "pushbutton.h" #include <stdio.h> -PushButton::PushButton(QDomNode &node) - : QPushButton(), Widget(node) +PushButton::PushButton(QDomNode &node, MacroWindow *macrowindow) + : QPushButton(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/pushbutton.h b/client/widgets/pushbutton.h index 42e3156..fe75fbf 100644 --- a/client/widgets/pushbutton.h +++ b/client/widgets/pushbutton.h @@ -36,7 +36,7 @@ class PushButton : public QPushButton, public Widget { Q_OBJECT public: - PushButton(QDomNode &node); + PushButton(QDomNode &node, MacroWindow *macrowindow); QString field; public slots: diff --git a/client/widgets/radiobuttons.cc b/client/widgets/radiobuttons.cc index 8c87a0c..7508974 100644 --- a/client/widgets/radiobuttons.cc +++ b/client/widgets/radiobuttons.cc @@ -30,8 +30,8 @@ #include <QHBoxLayout> #include <QVBoxLayout> -RadioButtons::RadioButtons(QDomNode &node) - : QFrame(), Widget(node) +RadioButtons::RadioButtons(QDomNode &node, MacroWindow *macrowindow) + : QFrame(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/radiobuttons.h b/client/widgets/radiobuttons.h index 454d159..67c1cd0 100644 --- a/client/widgets/radiobuttons.h +++ b/client/widgets/radiobuttons.h @@ -37,7 +37,7 @@ class RadioButtons : public QFrame, public Widget { public: - RadioButtons(QDomNode &node); + RadioButtons(QDomNode &node, MacroWindow *macrowindow); public slots: bool isValid(); diff --git a/client/widgets/textedit.cc b/client/widgets/textedit.cc index e217cc6..a284967 100644 --- a/client/widgets/textedit.cc +++ b/client/widgets/textedit.cc @@ -28,8 +28,8 @@ #include <stdio.h> #include <QPalette> -TextEdit::TextEdit(QDomNode &node) - : QTextEdit(), Widget(node) +TextEdit::TextEdit(QDomNode &node, MacroWindow *macrowindow) + : QTextEdit(), Widget(node, macrowindow) { //setAutoFillBackground(true); /* Default is false, which disables background // color manipulation.*/ diff --git a/client/widgets/textedit.h b/client/widgets/textedit.h index 466af0f..946e7b9 100644 --- a/client/widgets/textedit.h +++ b/client/widgets/textedit.h @@ -36,7 +36,7 @@ class TextEdit : public QTextEdit, public Widget { Q_OBJECT public: - TextEdit(QDomNode &node); + TextEdit(QDomNode &node, MacroWindow *macrowindow); public slots: void changed(); diff --git a/client/widgets/widget.cc b/client/widgets/widget.cc index d17754e..61e2a53 100644 --- a/client/widgets/widget.cc +++ b/client/widgets/widget.cc @@ -26,10 +26,12 @@ */ #include "widget.h" -Widget::Widget(QDomNode &node) +Widget::Widget(QDomNode &node, MacroWindow *macrowindow) { QDomElement elem = node.toElement(); + this->macrowindow = macrowindow; + if(elem.hasAttribute("name")) { widget_name = elem.attribute("name"); } else { @@ -37,11 +39,11 @@ Widget::Widget(QDomNode &node) elem.tagName().toStdString().c_str()); } - if(elem.hasAttribute("lua_validator")) { - lua_validator = elem.attribute("lua_validator"); - hasluavalidator = true; + if(elem.hasAttribute("lua")) { + luaprogram = elem.attribute("lua"); + hasluaprogram = true; } else { - hasluavalidator = false; + hasluaprogram = false; } if(elem.hasAttribute("regexp")) { @@ -63,6 +65,10 @@ QString Widget::getValue() return ""; } +void Widget::setValue(QString) +{ +} + bool Widget::isValid() { return regexpValidator() && luaValidator(); @@ -75,17 +81,7 @@ bool Widget::regexpValidator() bool Widget::luaValidator() { - 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()); + if(!hasluaprogram) return true; - return result == "true"; + return macrowindow->lua->run(luaprogram, getValue()); } diff --git a/client/widgets/widget.h b/client/widgets/widget.h index 391f2e9..cba6be9 100644 --- a/client/widgets/widget.h +++ b/client/widgets/widget.h @@ -31,13 +31,15 @@ #include <QDomNode> #include <QRegExp> +#include "macrowindow.h" #include "lua.h" class Widget { public: - Widget(QDomNode &node); + Widget(QDomNode &node, MacroWindow *macrowindow); virtual ~Widget(){} virtual QString getValue(); + virtual void setValue(QString value); virtual bool isValid(); QString getName(); @@ -51,9 +53,11 @@ private: QRegExp rx; bool hasregexpvalidator; - bool hasluavalidator; + bool hasluaprogram; - QString lua_validator; + QString luaprogram; + LUA *lua; + MacroWindow *macrowindow; }; #endif/*__PRACRO_WIDGET_H__*/ diff --git a/client/widgets/window.cc b/client/widgets/window.cc index 5d15733..b2b4298 100644 --- a/client/widgets/window.cc +++ b/client/widgets/window.cc @@ -28,8 +28,8 @@ #include <QVBoxLayout> #include <QHBoxLayout> -Window::Window(QDomNode &node) - : QWidget(NULL), Widget(node) +Window::Window(QDomNode &node, MacroWindow *macrowindow) + : QWidget(NULL), Widget(node, macrowindow) { QDomElement elem = node.toElement(); diff --git a/client/widgets/window.h b/client/widgets/window.h index 93c48ab..bd4c77b 100644 --- a/client/widgets/window.h +++ b/client/widgets/window.h @@ -34,7 +34,7 @@ class Window : public QWidget, public Widget { public: - Window(QDomNode &node); + Window(QDomNode &node, MacroWindow *macrowindow); public slots: QString getValue(); diff --git a/server/xml/example2.xml b/server/xml/example2.xml index ddcc4e6..826b694 100644 --- a/server/xml/example2.xml +++ b/server/xml/example2.xml @@ -24,8 +24,10 @@ </maps> <luaprograms> <luaprogram name="theanswer"> - if( tonumber(value) == 42 ) + fisk = getValue('sphere') + if( tonumber(fisk) == tonumber(value) ) then + setValue('cyl', value) return true else return false @@ -40,15 +42,15 @@ <frame name="linse_frame" caption="Linser:" layout="vbox"> <frame name="linse_framea" layout="hbox"> <label name="a" width="300" caption="Akse rotation:"/> - <lineedit name="linse4" regexp="[24]*" map="axis" lua="theanswer" value="90K"/> + <lineedit name="axis" regexp="[24]*" map="axis" lua="theanswer" value="244"/> </frame> <frame name="linse_frameb" layout="hbox"> <label name="b" width="300" caption="Sphere:"/> - <lineedit name="linse2" regexp="-{0,1}[0-9]{1,3}\.[0-9]{1,3}" map="sphere" value="90K"/> + <lineedit name="sphere" regexp="[24]*" map="sphere" value="42"/> </frame> <frame name="linse_framec" layout="hbox"> - <label name="c" width="300" caption="Sphere:"/> - <lineedit name="linse3" regexp="-{0,1}[0-9]{1,3}\.[0-9]{1,3}" map="cyl" value="90K"/> + <label name="c" width="300" caption="Cyl:"/> + <lineedit name="cyl" regexp="[24]*" map="cyl" value=""/> </frame> </frame> <frame name="buttons" layout="hbox"> |