diff options
Diffstat (limited to 'Swift/QtUI/QtFormWidget.cpp')
-rw-r--r-- | Swift/QtUI/QtFormWidget.cpp | 156 |
1 files changed, 43 insertions, 113 deletions
diff --git a/Swift/QtUI/QtFormWidget.cpp b/Swift/QtUI/QtFormWidget.cpp index 4216863..9b213cf 100644 --- a/Swift/QtUI/QtFormWidget.cpp +++ b/Swift/QtUI/QtFormWidget.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2011 Kevin Smith + * Copyright (c) 2010-2013 Kevin Smith * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -17,6 +17,8 @@ #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 { @@ -54,18 +56,16 @@ QtFormWidget::~QtFormWidget() { QListWidget* QtFormWidget::createList(FormField::ref field) { QListWidget* listWidget = new QListWidget(this); listWidget->setSortingEnabled(false); - listWidget->setSelectionMode(boost::dynamic_pointer_cast<ListMultiFormField>(field) ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection); - boost::shared_ptr<ListMultiFormField> listMultiField = boost::dynamic_pointer_cast<ListMultiFormField>(field); - boost::shared_ptr<ListSingleFormField> listSingleField = boost::dynamic_pointer_cast<ListSingleFormField>(field); + listWidget->setSelectionMode(field->getType() == FormField::ListMultiType ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection); std::vector<bool> selected; foreach (FormField::Option option, field->getOptions()) { listWidget->addItem(option.label.c_str()); - if (listSingleField) { - selected.push_back(option.value == listSingleField->getValue()); + if (field->getType() == FormField::ListMultiType) { + selected.push_back(!field->getValues().empty() && option.value == field->getValues()[0]); } - else if (listMultiField) { + else if (field->getType() == FormField::ListMultiType) { std::string text = option.value; - selected.push_back(std::find(listMultiField->getValue().begin(), listMultiField->getValue().end(), text) != listMultiField->getValue().end()); + selected.push_back(std::find(field->getValues().begin(), field->getValues().end(), text) != field->getValues().end()); } } @@ -78,67 +78,47 @@ QListWidget* QtFormWidget::createList(FormField::ref field) { QWidget* QtFormWidget::createWidget(FormField::ref field) { QWidget* widget = NULL; - boost::shared_ptr<BooleanFormField> booleanField = boost::dynamic_pointer_cast<BooleanFormField>(field); - if (booleanField) { + if (field->getType() == FormField::BooleanType) { QCheckBox* checkWidget = new QCheckBox(this); - checkWidget->setCheckState(booleanField->getValue() ? Qt::Checked : Qt::Unchecked); + checkWidget->setCheckState(field->getBoolValue() ? Qt::Checked : Qt::Unchecked); widget = checkWidget; } - boost::shared_ptr<FixedFormField> fixedField = boost::dynamic_pointer_cast<FixedFormField>(field); - if (fixedField) { - QString value = fixedField->getValue().c_str(); + if (field->getType() == FormField::FixedType) { + QString value = field->getFixedValue().c_str(); widget = new QLabel(value, this); } - boost::shared_ptr<ListSingleFormField> listSingleField = boost::dynamic_pointer_cast<ListSingleFormField>(field); - if (listSingleField) { + if (field->getType() == FormField::ListSingleType) { widget = createList(field); } - boost::shared_ptr<TextMultiFormField> textMultiField = boost::dynamic_pointer_cast<TextMultiFormField>(field); - if (textMultiField) { - QString value = textMultiField->getValue().c_str(); + if (field->getType() == FormField::TextMultiType) { + QString value = field->getTextMultiValue().c_str(); QTextEdit* textWidget = new QTextEdit(this); textWidget->setPlainText(value); widget = textWidget; } - boost::shared_ptr<TextPrivateFormField> textPrivateField = boost::dynamic_pointer_cast<TextPrivateFormField>(field); - if (textPrivateField) { - QString value = textPrivateField->getValue().c_str(); + if (field->getType() == FormField::TextPrivateType) { + QString value = field->getTextPrivateValue().c_str(); QLineEdit* lineWidget = new QLineEdit(value, this); lineWidget->setEchoMode(QLineEdit::Password); widget = lineWidget; } - boost::shared_ptr<TextSingleFormField> textSingleField = boost::dynamic_pointer_cast<TextSingleFormField>(field); - if (textSingleField) { - QString value = textSingleField->getValue().c_str(); + if (field->getType() == FormField::TextSingleType) { + QString value = field->getTextSingleValue().c_str(); widget = new QLineEdit(value, this); } - boost::shared_ptr<JIDSingleFormField> jidSingleField = boost::dynamic_pointer_cast<JIDSingleFormField>(field); - if (jidSingleField) { - QString value = jidSingleField->getValue().toString().c_str(); + if (field->getType() == FormField::JIDSingleType) { + QString value = field->getJIDSingleValue().toString().c_str(); widget = new QLineEdit(value, this); } - boost::shared_ptr<JIDMultiFormField> jidMultiField = boost::dynamic_pointer_cast<JIDMultiFormField>(field); - if (jidMultiField) { - QString text; - bool prev = false; - foreach (JID line, jidMultiField->getValue()) { - if (prev) { - text += "\n"; - } - prev = true; - text += line.toString().c_str(); - } + if (field->getType() == FormField::JIDMultiType) { + QString text = boost::join(field->getValues(), "\n").c_str(); QTextEdit* textWidget = new QTextEdit(this); textWidget->setPlainText(text); widget = textWidget; } - boost::shared_ptr<ListMultiFormField> listMultiField = boost::dynamic_pointer_cast<ListMultiFormField>(field); - if (listMultiField) { + if (field->getType() == FormField::ListMultiType) { widget = createList(field); } - boost::shared_ptr<HiddenFormField> hiddenField = boost::dynamic_pointer_cast<HiddenFormField>(field); - if (hiddenField) { - } fields_[field->getName()] = widget; return widget; } @@ -146,101 +126,51 @@ QWidget* QtFormWidget::createWidget(FormField::ref field) { 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::shared_ptr<BooleanFormField> booleanField = boost::dynamic_pointer_cast<BooleanFormField>(field); - if (booleanField) { - resultField = FormField::ref(BooleanFormField::create(qobject_cast<QCheckBox*>(fields_[field->getName()])->checkState() == Qt::Checked)); + 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); } - boost::shared_ptr<FixedFormField> fixedField = boost::dynamic_pointer_cast<FixedFormField>(field); - if (fixedField) { - resultField = FormField::ref(FixedFormField::create(fixedField->getValue())); + if (field->getType() == FormField::FixedType || field->getType() == FormField::HiddenType) { + resultField->addValue(field->getValues().empty() ? "" : field->getValues()[0]); } - boost::shared_ptr<ListSingleFormField> listSingleField = boost::dynamic_pointer_cast<ListSingleFormField>(field); - if (listSingleField) { + 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 = FormField::ref(ListSingleFormField::create(field->getOptions()[i].value)); - } - else { - resultField = FormField::ref(ListSingleFormField::create()); + resultField->addValue(field->getOptions()[i].value); } } - boost::shared_ptr<TextMultiFormField> textMultiField = boost::dynamic_pointer_cast<TextMultiFormField>(field); - if (textMultiField) { + if (field->getType() == FormField::TextMultiType) { QTextEdit* widget = qobject_cast<QTextEdit*>(fields_[field->getName()]); QString string = widget->toPlainText(); - if (string.isEmpty()) { - resultField = FormField::ref(TextMultiFormField::create()); - } - else { - resultField = FormField::ref(TextMultiFormField::create(Q2PSTRING(string))); - } - } - boost::shared_ptr<TextPrivateFormField> textPrivateField = boost::dynamic_pointer_cast<TextPrivateFormField>(field); - if (textPrivateField) { - QLineEdit* widget = qobject_cast<QLineEdit*>(fields_[field->getName()]); - QString string = widget->text(); - if (string.isEmpty()) { - resultField = FormField::ref(TextPrivateFormField::create()); - } - else { - resultField = FormField::ref(TextPrivateFormField::create(Q2PSTRING(string))); + if (!string.isEmpty()) { + resultField->setTextMultiValue(Q2PSTRING(string)); } } - boost::shared_ptr<TextSingleFormField> textSingleField = boost::dynamic_pointer_cast<TextSingleFormField>(field); - if (textSingleField) { + 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 = FormField::ref(TextSingleFormField::create()); - } - else { - resultField = FormField::ref(TextSingleFormField::create(Q2PSTRING(string))); + if (!string.isEmpty()) { + resultField->addValue(Q2PSTRING(string)); } } - boost::shared_ptr<JIDSingleFormField> jidSingleField = boost::dynamic_pointer_cast<JIDSingleFormField>(field); - if (jidSingleField) { - QLineEdit* widget = qobject_cast<QLineEdit*>(fields_[field->getName()]); - QString string = widget->text(); - JID jid(Q2PSTRING(string)); - if (string.isEmpty()) { - resultField = FormField::ref(JIDSingleFormField::create()); - } - else { - resultField = FormField::ref(JIDSingleFormField::create(jid)); - } - } - boost::shared_ptr<JIDMultiFormField> jidMultiField = boost::dynamic_pointer_cast<JIDMultiFormField>(field); - if (jidMultiField) { + if (field->getType() == FormField::JIDMultiType) { QTextEdit* widget = qobject_cast<QTextEdit*>(fields_[field->getName()]); QString string = widget->toPlainText(); - if (string.isEmpty()) { - resultField = FormField::ref(JIDMultiFormField::create()); - } - else { + if (!string.isEmpty()) { QStringList lines = string.split("\n"); - std::vector<JID> value; foreach (QString line, lines) { - value.push_back(JID(Q2PSTRING(line))); + resultField->addValue(Q2PSTRING(line)); } - resultField = FormField::ref(JIDMultiFormField::create(value)); } } - boost::shared_ptr<ListMultiFormField> listMultiField = boost::dynamic_pointer_cast<ListMultiFormField>(field); - if (listMultiField) { + if (field->getType() == FormField::ListMultiType) { QListWidget* listWidget = qobject_cast<QListWidget*>(fields_[field->getName()]); - std::vector<std::string> values; foreach (QListWidgetItem* item, listWidget->selectedItems()) { - values.push_back(field->getOptions()[listWidget->row(item)].value); + resultField->addValue(field->getOptions()[listWidget->row(item)].value); } - resultField = FormField::ref(ListMultiFormField::create(values)); - } - boost::shared_ptr<HiddenFormField> hiddenField = boost::dynamic_pointer_cast<HiddenFormField>(field); - if (hiddenField) { - resultField = FormField::ref(HiddenFormField::create(hiddenField->getValue())); } - resultField->setName(field->getName()); + field->setName(field->getName()); result->addField(resultField); } return result; |