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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
* Copyright (c) 2010-2013 Kevin Smith
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swift/QtUI/QtFormWidget.h>
#include <QGridLayout>
#include <QLabel>
#include <QListWidget>
#include <QLineEdit>
#include <QTextEdit>
#include <QCheckBox>
#include <QScrollArea>
#include <qdebug.h>
#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swiften/Base/foreach.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/smart_ptr/make_shared.hpp>
namespace Swift {
QtFormWidget::QtFormWidget(Form::ref form, QWidget* parent) : QWidget(parent), form_(form) {
QGridLayout* thisLayout = new QGridLayout(this);
int row = 0;
if (!form->getTitle().empty()) {
QLabel* instructions = new QLabel(("<b>" + form->getTitle() + "</b>").c_str(), this);
thisLayout->addWidget(instructions, row++, 0, 1, 2);
}
if (!form->getInstructions().empty()) {
QLabel* instructions = new QLabel(form->getInstructions().c_str(), this);
thisLayout->addWidget(instructions, row++, 0, 1, 2);
}
QScrollArea* scrollArea = new QScrollArea(this);
thisLayout->addWidget(scrollArea);
QWidget* scroll = new QWidget(this);
QGridLayout* layout = new QGridLayout(scroll);
foreach (boost::shared_ptr<FormField> field, form->getFields()) {
QWidget* widget = createWidget(field);
if (widget) {
layout->addWidget(new QLabel(field->getLabel().c_str(), this), row, 0);
layout->addWidget(widget, row++, 1);
}
}
scrollArea->setWidget(scroll);
scrollArea->setWidgetResizable(true);
setEditable(form->getType() != Form::CancelType && form->getType() != Form::ResultType);
}
QtFormWidget::~QtFormWidget() {
}
QListWidget* QtFormWidget::createList(FormField::ref field) {
QListWidget* listWidget = new QListWidget(this);
listWidget->setSortingEnabled(false);
listWidget->setSelectionMode(field->getType() == FormField::ListMultiType ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection);
std::vector<bool> selected;
/* if this is an editable form, use the 'options' list, otherwise use the 'values' list */
if (form_->getType() != Form::FormType) {
foreach (const std::string& value, field->getValues()) {
listWidget->addItem(P2QSTRING(value));
selected.push_back(false);
}
} else {
foreach (FormField::Option option, field->getOptions()) {
listWidget->addItem(option.label.c_str());
if (field->getType() == FormField::ListSingleType) {
selected.push_back(!field->getValues().empty() && option.value == field->getValues()[0]);
}
else if (field->getType() == FormField::ListMultiType) {
std::string text = option.value;
selected.push_back(std::find(field->getValues().begin(), field->getValues().end(), text) != field->getValues().end());
}
}
}
for (int i = 0; i < listWidget->count(); i++) {
QListWidgetItem* item = listWidget->item(i);
item->setSelected(selected[i]);
}
return listWidget;
}
QWidget* QtFormWidget::createWidget(FormField::ref field) {
QWidget* widget = NULL;
if (field->getType() == FormField::BooleanType) {
QCheckBox* checkWidget = new QCheckBox(this);
checkWidget->setCheckState(field->getBoolValue() ? Qt::Checked : Qt::Unchecked);
widget = checkWidget;
}
if (field->getType() == FormField::FixedType) {
QString value = field->getFixedValue().c_str();
widget = new QLabel(value, this);
}
if (field->getType() == FormField::ListSingleType) {
widget = createList(field);
}
if (field->getType() == FormField::TextMultiType) {
QString value = field->getTextMultiValue().c_str();
QTextEdit* textWidget = new QTextEdit(this);
textWidget->setPlainText(value);
widget = textWidget;
}
if (field->getType() == FormField::TextPrivateType) {
QString value = field->getTextPrivateValue().c_str();
QLineEdit* lineWidget = new QLineEdit(value, this);
lineWidget->setEchoMode(QLineEdit::Password);
widget = lineWidget;
}
if (field->getType() == FormField::TextSingleType) {
QString value = field->getTextSingleValue().c_str();
widget = new QLineEdit(value, this);
}
if (field->getType() == FormField::JIDSingleType) {
QString value = field->getJIDSingleValue().toString().c_str();
widget = new QLineEdit(value, this);
}
if (field->getType() == FormField::JIDMultiType) {
QString text = boost::join(field->getValues(), "\n").c_str();
QTextEdit* textWidget = new QTextEdit(this);
textWidget->setPlainText(text);
widget = textWidget;
}
if (field->getType() == FormField::ListMultiType) {
widget = createList(field);
}
fields_[field->getName()] = widget;
return widget;
}
Form::ref QtFormWidget::getCompletedForm() {
Form::ref result(new Form(Form::SubmitType));
foreach (boost::shared_ptr<FormField> field, form_->getFields()) {
boost::shared_ptr<FormField> resultField = boost::make_shared<FormField>(field->getType());
if (field->getType() == FormField::BooleanType) {
resultField->setBoolValue(qobject_cast<QCheckBox*>(fields_[field->getName()])->checkState() == Qt::Checked);
}
if (field->getType() == FormField::FixedType || field->getType() == FormField::HiddenType) {
resultField->addValue(field->getValues().empty() ? "" : field->getValues()[0]);
}
if (field->getType() == FormField::ListSingleType) {
QListWidget* listWidget = qobject_cast<QListWidget*>(fields_[field->getName()]);
if (listWidget->selectedItems().size() > 0) {
int i = listWidget->row(listWidget->selectedItems()[0]);
resultField->addValue(field->getOptions()[i].value);
}
}
if (field->getType() == FormField::TextMultiType) {
QTextEdit* widget = qobject_cast<QTextEdit*>(fields_[field->getName()]);
QString string = widget->toPlainText();
if (!string.isEmpty()) {
resultField->setTextMultiValue(Q2PSTRING(string));
}
}
if (field->getType() == FormField::TextPrivateType || field->getType() == FormField::TextSingleType || field->getType() == FormField::JIDSingleType) {
QLineEdit* widget = qobject_cast<QLineEdit*>(fields_[field->getName()]);
QString string = widget->text();
if (!string.isEmpty()) {
resultField->addValue(Q2PSTRING(string));
}
}
if (field->getType() == FormField::JIDMultiType) {
QTextEdit* widget = qobject_cast<QTextEdit*>(fields_[field->getName()]);
QString string = widget->toPlainText();
if (!string.isEmpty()) {
QStringList lines = string.split("\n");
foreach (QString line, lines) {
resultField->addValue(Q2PSTRING(line));
}
}
}
if (field->getType() == FormField::ListMultiType) {
QListWidget* listWidget = qobject_cast<QListWidget*>(fields_[field->getName()]);
foreach (QListWidgetItem* item, listWidget->selectedItems()) {
resultField->addValue(field->getOptions()[listWidget->row(item)].value);
}
}
resultField->setName(field->getName());
result->addField(resultField);
}
return result;
}
template<class T> void QtFormWidget::setEnabled(QWidget* rawWidget, bool editable) {
T* widget = qobject_cast<T*>(rawWidget);
if (widget) {
widget->setEnabled(editable);
}
}
template<class T> void QtFormWidget::setEditable(QWidget* rawWidget, bool editable) {
T* widget = qobject_cast<T*>(rawWidget);
if (widget) {
widget->setReadOnly(!editable);
}
}
void QtFormWidget::setEditable(bool editable) {
if (!form_) {
return;
}
foreach (boost::shared_ptr<FormField> field, form_->getFields()) {
QWidget* widget = NULL;
if (field) {
widget = fields_[field->getName()];
}
setEnabled<QCheckBox>(widget, editable);
setEnabled<QListWidget>(widget, editable);
setEditable<QTextEdit>(widget, editable);
setEditable<QLineEdit>(widget, editable);
}
}
}
|