summaryrefslogtreecommitdiff
path: root/client/widgets/widget.cc
diff options
context:
space:
mode:
authordeva <deva>2010-08-12 10:57:04 +0000
committerdeva <deva>2010-08-12 10:57:04 +0000
commitd9338083192084613e5530b02710b796252d342b (patch)
treee0ec2b36e0de62328e5fd5d3b597f6ee71d1b18f /client/widgets/widget.cc
parentdbab8458dcce186e7eb7a114a83f759d7db5445a (diff)
New scripting system part2.
Diffstat (limited to 'client/widgets/widget.cc')
-rw-r--r--client/widgets/widget.cc290
1 files changed, 254 insertions, 36 deletions
diff --git a/client/widgets/widget.cc b/client/widgets/widget.cc
index c17633f..af03843 100644
--- a/client/widgets/widget.cc
+++ b/client/widgets/widget.cc
@@ -26,43 +26,77 @@
*/
#include "widget.h"
+#include "macrowindow.h"
+#include "lua.h"
+
+#include "../widgets.h"
+#include <QLayout>
+#include <QObject>
+
Widget::Widget(QDomNode &node, MacroWindow *macrowindow)
{
+ widget = NULL;
+
QDomElement elem = node.toElement();
this->macrowindow = macrowindow;
+ this->lua = macrowindow->lua;
widget_type = elem.tagName();
if(elem.hasAttribute("name")) {
widget_name = elem.attribute("name");
- } else {
- if(elem.tagName() != "frame" && elem.tagName() != "label"
- && elem.tagName() != "button" && elem.tagName() != "widgets")
- printf("XML ERROR!! Unnamed widget of type: %s\n",
- elem.tagName().toStdString().c_str());
}
+ widget_local =
+ elem.hasAttribute("local") && elem.attribute("local") == "true";
+
if(elem.hasAttribute("prefilled")) {
prefilled = elem.attribute("prefilled");
}
- if(elem.hasAttribute("script")) {
- luaprogram = elem.attribute("script");
- hasluaprogram = true;
- } else {
- hasluaprogram = false;
+ has_initial_value = false;
+ initial_value = "";
+
+ if((hasOnChangeEvent = elem.hasAttribute("onChange"))) {
+ onChangeEventScript = elem.attribute("onChange");
}
-
- if(elem.hasAttribute("regexp")) {
- rx = QRegExp(elem.attribute("regexp"));
- hasregexpvalidator = true;
- } else {
- hasregexpvalidator = false;
+
+ is_valid = true;
+
+ printf("Create Widget '%s' of type '%s'\n",
+ name().toStdString().c_str(),
+ type().toStdString().c_str());
+}
+
+Widget::~Widget()
+{
+ printf("Delete Widget '%s' of type '%s'\n",
+ name().toStdString().c_str(),
+ type().toStdString().c_str());
+
+ /* // This is done by Qt
+ if(widget) {
+ delete widget;
+ widget = NULL;
+ }
+ */
+ QVector< Widget* >::iterator i = children.begin();
+ while(i != children.end()) {
+ if(*i) delete *i;
+ i++;
}
- has_initial_value = false;
- initial_value = "";
+ children.clear();
+}
+
+void Widget::addChildren(QDomNode &node)
+{
+ QDomNodeList children = node.childNodes();
+ for (int i=0; i<children.count();i++) {
+ QDomNode child = children.at(i);
+ widgetBuilder(child);
+ }
}
QString Widget::name()
@@ -75,29 +109,29 @@ QString Widget::type()
return widget_type;
}
-void Widget::setValue(QString, QString)
+bool Widget::local()
{
+ return widget_local;
}
-bool Widget::isValid()
+bool Widget::valid()
{
- return regexpValidator() && luaValidator();
-}
+ if(preValid() == false) return false;
+ if(is_valid == false) return false;
+
+ QVector< Widget* >::iterator i = children.begin();
+ while(i != children.end()) {
+ if((*i)->valid() == false) return false;
+ i++;
+ }
-bool Widget::regexpValidator()
-{
- return !hasregexpvalidator || rx.exactMatch(getValue());
+ return true;
}
-bool Widget::luaValidator()
-{
- if(!hasluaprogram) return true;
-
- QString program = "";
-
- program += luaprogram;
-
- return macrowindow->lua->runValidator(program, this, name(), getValue());
+void Widget::setValid(bool valid)
+{
+ is_valid = valid;
+ setWdgValid(valid);
}
void Widget::setInitialValue(QString value)
@@ -113,8 +147,192 @@ bool Widget::hasInitialValue()
return has_initial_value;
}
-
void Widget::reset()
{
- setValue(initial_value);
+ setValue(initial_value, "");
+}
+
+void Widget::eventOnChange()
+{
+ if(enabled() && hasOnChangeEvent)
+ lua->runScript(onChangeEventScript, this, "onChange");
+}
+
+void Widget::setEnabled(bool enabled)
+{
+ widget->setEnabled(enabled);
+}
+
+bool Widget::enabled()
+{
+ return widget->isEnabled();
+}
+
+void Widget::setVisible(bool visible)
+{
+ widget->setVisible(visible);
+}
+
+bool Widget::visible()
+{
+ return widget->isVisible();
+}
+
+bool Widget::setKeyboardFocus()
+{
+ widget->setFocus();
+ return true;
+}
+
+Widget *Widget::findWidget(QString n, bool deep)
+{
+ printf("Find Widget %p\n", this); fflush(stdout);
+
+ if(n == name()) return this;
+
+ if(hideChildren && deep == false) return NULL;
+
+ QVector< Widget* >::iterator i = children.begin();
+ while(i != children.end()) {
+ Widget *w = (*i)->findWidget(n, deep);
+ if(w) return w;
+ i++;
+ }
+
+ return NULL;
+}
+
+QVector< Widget* > Widget::widgetList(bool deep)
+{
+ printf("Widget List %p\n", this); fflush(stdout);
+
+ QVector< Widget* > lst = children;
+
+ if(hideChildren && deep == false) return lst;
+
+ QVector< Widget* >::iterator i = children.begin();
+ while(i != children.end()) {
+ lst += (*i)->widgetList(deep);
+ i++;
+ }
+
+ return lst;
+}
+
+void Widget::childWasChanged()
+{
+ emit wasChanged();
+}
+
+void Widget::addChild(Widget *widget)
+{
+ if(widget == NULL) {
+ printf("Trying to add NULL child to '%s'\n", name().toStdString().c_str());
+ return;
+ }
+ children.push_back(widget);
+ connect(widget, SIGNAL(wasChanged()), this, SLOT(childWasChanged()));
+}
+
+void Widget::widgetBuilder(QDomNode &xml_node)
+{
+ QDomElement xml_elem = xml_node.toElement();
+
+
+ // TODO: Why do we do this??
+ if(xml_elem.hasAttribute("prefilled") &&
+ xml_elem.attribute("prefilled") != "pracro") {
+ macrowindow->macroChanged();
+ }
+
+ Widget *widget = NULL;
+ if(xml_elem.tagName() == "spacer") {
+
+ if(qwidget() && qwidget()->layout()) {
+ ((QBoxLayout*)qwidget()->layout())->addStretch();
+ }
+ return; // This is not a real widget.
+ } else if(xml_elem.tagName() == "frame") {
+
+ if(xml_elem.hasAttribute("caption")) {
+ GroupBox *frame = new GroupBox(xml_elem, macrowindow);
+ widget = frame;
+ } else {
+ Frame *frame = new Frame(xml_elem, macrowindow);
+ widget = frame;
+ }
+
+ } else if(xml_elem.tagName() == "label") {
+
+ Label *label = new Label(xml_elem, macrowindow);
+ widget = label;
+
+ } else if(xml_elem.tagName() == "lineedit") {
+
+ LineEdit *lineedit = new LineEdit(xml_elem, macrowindow);
+ widget = lineedit;
+
+ } else if(xml_elem.tagName() == "datetime") {
+
+ DateTime *datetime = new DateTime(xml_elem, macrowindow);
+ widget = datetime;
+
+ } else if(xml_elem.tagName() == "button") {
+
+ Button *button = new Button(xml_elem, macrowindow);
+ widget = button;
+
+ } else if(xml_elem.tagName() == "textedit") {
+
+ TextEdit *textedit = new TextEdit(xml_elem, macrowindow);
+ widget = textedit;
+
+ } else if(xml_elem.tagName() == "checkbox") {
+
+ CheckBox *checkbox = new CheckBox(xml_elem, macrowindow);
+ widget = checkbox;
+
+ } else if(xml_elem.tagName() == "radiobuttons") {
+
+ RadioButtons *radiobuttons = new RadioButtons(xml_elem, macrowindow);
+ widget = radiobuttons;
+
+ } else if(xml_elem.tagName() == "combobox") {
+
+ ComboBox *combobox = new ComboBox(xml_elem, macrowindow);
+ widget = combobox;
+
+ } else if(xml_elem.tagName() == "dbwidget") {
+
+ DBWidget *dbwidget = new DBWidget(xml_elem, macrowindow);
+ widget = dbwidget;
+
+ } else if(xml_elem.tagName() == "listbox") {
+
+ ListBox *listbox = new ListBox(xml_elem, macrowindow);
+ widget = listbox;
+
+ } else if(xml_elem.tagName() == "multilist") {
+
+ MultiList *multilist = new MultiList(xml_elem, macrowindow);
+ widget = multilist;
+
+ } else if(xml_elem.tagName() == "altcombobox") {
+
+ AltComboBox *altcombobox = new AltComboBox(xml_elem, macrowindow);
+ widget = altcombobox;
+
+ } else if(xml_elem.tagName() == "metawidget") {
+
+ MetaWidget *metawidget = new MetaWidget(xml_elem, macrowindow);
+ widget = metawidget;
+
+ }
+
+ addChild(widget);
+
+ if(qwidget() && qwidget()->layout())
+ qwidget()->layout()->addWidget(widget->qwidget());
+
+ if(widget && widget->qwidget()) widget->qwidget()->show();
}