summaryrefslogtreecommitdiff
path: root/client/widgetbuilder.cc
blob: e688f6004692ed69185ff778066ae3bcb9ad45f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            widgetbuilder.cc
 *
 *  Wed Jun 18 08:03:04 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.
 */
#include "widgetbuilder.h"
#include <QLayout>
#include "widgets.h"

QVector< Widget* > widgetBuilder(QDomNode xml_node, QWidget *parent, MacroWindow *macrowindow)
{
  QVector< Widget* > widgets;

  QDomElement xml_elem = xml_node.toElement();

  QWidget *widget = NULL;
  if(xml_elem.tagName() == "spacer") {
    if(parent && parent->layout()) { 
      ((QBoxLayout*)parent->layout())->addStretch();
    }
  } else if(xml_elem.tagName() == "frame") {
    if(xml_elem.hasAttribute("caption")) {
      GroupBox *frame = new GroupBox(xml_elem, macrowindow);
      widgets.push_back(frame);
      widget = frame;
    } else {
      Frame *frame = new Frame(xml_elem, macrowindow);
      widgets.push_back(frame);
      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);
    widgets.push_back(lineedit);
    widget = lineedit;

  } else if(xml_elem.tagName() == "button") {
    Button *button = new Button(xml_elem, macrowindow);
    //macrowindow->connect(pushbutton, SIGNAL(act_continue()), main, SLOT(get_macro()));
    macrowindow->connect(button, SIGNAL(act_commit()), macrowindow, SLOT(commit()));
    macrowindow->connect(button, SIGNAL(act_reset()), macrowindow, SLOT(reset()));
    macrowindow->connect(button, SIGNAL(act_cancel()), macrowindow, SLOT(cancel()));
    macrowindow->connect(button, SIGNAL(act_continue(QString)), macrowindow, SLOT(cont(QString)));
    macrowindow->connect(button, SIGNAL(act_continue_nocommit(QString)),
                         macrowindow, SLOT(cont_nocommit(QString)));
    widget = button;

  } else if(xml_elem.tagName() == "textedit") {
    TextEdit *textedit = new TextEdit(xml_elem, macrowindow);
    widgets.push_back(textedit);
    widget = textedit;

  } else if(xml_elem.tagName() == "checkbox") {
    CheckBox *checkbox = new CheckBox(xml_elem, macrowindow);
    widgets.push_back(checkbox);
    widget = checkbox;

  } else if(xml_elem.tagName() == "radiobuttons") {
    RadioButtons *radiobuttons = new RadioButtons(xml_elem, macrowindow);
    widgets.push_back(radiobuttons);
    widget = radiobuttons;
    //return; // Don't iterate children

  } else if(xml_elem.tagName() == "combobox") {
    ComboBox *combobox = new ComboBox(xml_elem, macrowindow);
    widgets.push_back(combobox);
    widget = combobox;
    //return; // Don't iterate children

  } else if(xml_elem.tagName() == "dbwidget") {
    DBWidget *dbwidget = new DBWidget(xml_elem, macrowindow);
    widgets.push_back(dbwidget);
    widget = dbwidget;

  } else if(xml_elem.tagName() == "listbox") {
    ListBox *listbox = new ListBox(xml_elem, macrowindow);
    widgets.push_back(listbox);
    widget = listbox;
    //return; // Don't iterate children
  } else if(xml_elem.tagName() == "multilist") {
    MultiList *multilist = new MultiList(xml_elem, macrowindow);
    widgets.push_back(multilist);
    widget = multilist;

    if(parent != NULL && widget != NULL) parent->layout()->addWidget(widget);
    if(widget != NULL) widget->show();
    
    return widgets; // Don't iterate children
  }

  QDomNodeList children = xml_node.childNodes();

  for (int i=0; i<children.count();i++) {
    QDomNode child = children.at(i);
    widgets += widgetBuilder(child, widget, macrowindow);
  }

  if(parent != NULL && widget != NULL) parent->layout()->addWidget(widget);
  if(widget != NULL) widget->show();

  return widgets;
}

void setValues(QDomNode xml_node, MacroWindow *macrowindow)
{
  QDomElement xml_elem = xml_node.toElement();

  if(xml_elem.tagName() == "item" || xml_elem.tagName() == "radiobutton") return;

  if(xml_elem.hasAttribute("name") && xml_elem.hasAttribute("value")) {
    Widget *widget = macrowindow->getWidget(xml_elem.attribute("name"));
    if(widget) widget->setValue(xml_elem.attribute("value"));
  }

  QDomNodeList children = xml_node.childNodes();

  for (int i=0; i<children.count();i++) {
    QDomNode child = children.at(i);
    setValues(child, macrowindow);
  }
}