summaryrefslogtreecommitdiff
path: root/client/builder.cc
blob: d868f12e0b1352e33f743f664c16e824e84fcf5f (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
#include "builder.h"
#include "widgets/widget.h"
#include "widgets/label.h"
#include "widgets/lineedit.h"
#include "widgets/textedit.h"
#include "widgets/pushbutton.h"
#include "widgets/combobox.h"
#include "widgets/frame.h"
#include "widgets/radiobutton.h"
#include "widgets/checkbox.h"
#include "widgets/window.h"
//#include <QApplication>
#include <QVBoxLayout>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <vector>

std::vector< Widget* > widgets;

Builder::Builder(QDomDocument *xml_doc)
  : QObject()
{
  // Assign root element from xml_doc
  this->xml_doc = xml_doc;
  QDomElement xml_elem = xml_doc->documentElement();
    
  // Execute the recursive function with documentElement
  recurser(xml_elem, NULL);
  
  /*
  Window widget(window_elem);

  Frame *frame = new Frame(frame_elem);

  QVBoxLayout *layout = new QVBoxLayout();
  QVBoxLayout *window_layout = new QVBoxLayout();

  Label *label = new Label(label_elem);
  widgets.push_back(label);
  LineEdit *lineedit = new LineEdit(lineedit_elem);
  widgets.push_back(lineedit);
  LineEdit *lineedit2 = new LineEdit(lineedit_elem);
  widgets.push_back(lineedit2);
  TextEdit *textedit = new TextEdit(textedit_elem);
  widgets.push_back(textedit);

  ComboBox *combobox = new ComboBox(combobox_elem);
  widgets.push_back(combobox);

  RadioButton *radiobutton1 = new RadioButton(radiobutton1_elem);
  widgets.push_back(radiobutton1);
  RadioButton *radiobutton2 = new RadioButton(radiobutton2_elem);
  widgets.push_back(radiobutton2);
  CheckBox *checkbox = new CheckBox(checkbox_elem);
  widgets.push_back(checkbox); 
  PushButton *pushbutton1 = new PushButton(pushbutton1_elem);
  widgets.push_back(pushbutton1);
  PushButton *pushbutton2 = new PushButton(pushbutton2_elem);
  widgets.push_back(pushbutton2);
  PushButton *pushbutton3 = new PushButton(pushbutton3_elem);
  widgets.push_back(pushbutton3);

  layout->addWidget(label);
  layout->addWidget(lineedit);
  layout->addWidget(lineedit2);
  layout->addWidget(textedit);
  //layout->addWidget(combobox);
  layout->addWidget(radiobutton1);
  layout->addWidget(radiobutton2);
  layout->addWidget(checkbox);
  layout->addWidget(pushbutton1);
  layout->addWidget(pushbutton2);
  layout->addWidget(pushbutton3);

  frame->setLayout(layout);
  
  window_layout->addWidget(frame);

  widget.setLayout(window_layout);
  widget.show();

  std::vector< Widget* >::iterator i=widgets.begin();
  while (i != widgets.end()) {
    Widget* w = *i;
    printf("%s = %s\n", w->getName().toStdString().c_str(), w->getValue().toStdString().c_str());
    i++;
  }
  */
}

Builder::~Builder()
{
}

void Builder::recurser(QDomNode xml_node, QWidget *parent)
{
  QWidget *widget = NULL;
  QDomElement xml_elem = xml_node.toElement();
  if(xml_elem.tagName() == "window") {
    Window *window = new Window(xml_elem);
    widgets.push_back(window);
    widget = window;
  } else if(xml_elem.tagName() == "frame") {
    Frame *frame = new Frame(xml_elem);
    widgets.push_back(frame);
    widget = frame;
  } else if(xml_elem.tagName() == "label") {
    Label *label = new Label(xml_elem);
    widgets.push_back(label);
    widget = label;
  } else if(xml_elem.tagName() == "lineedit") {
    LineEdit *lineedit = new LineEdit(xml_elem);
    widgets.push_back(lineedit);
    widget = lineedit;
  } else if(xml_elem.tagName() == "button") {
    PushButton *pushbutton = new PushButton(xml_elem);
    widgets.push_back(pushbutton);
    widget = pushbutton;
  } else if(xml_elem.tagName() == "textedit") {
    TextEdit *textedit = new TextEdit(xml_elem);
    widgets.push_back(textedit);
    widget = textedit;
  } else if(xml_elem.tagName() == "checkbox") {
    CheckBox *checkbox = new CheckBox(xml_elem);
    widgets.push_back(checkbox);
    widget = checkbox;
  } else if(xml_elem.tagName() == "radiobutton") {
    RadioButton *radiobutton = new RadioButton(xml_elem);
    widgets.push_back(radiobutton);
    widget = radiobutton;
  }
  QDomNodeList children = xml_node.childNodes();

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

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