Wt examples: /home/koen/project/wt/public-git/wt/examples/form/Form.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/form/Form.C
Go to the documentation of this file.

    00001 #include "Form.h"
    00002
    00003 #include <Wt/WApplication>
    00004 #include <Wt/WBreak>
    00005 #include <Wt/WDateEdit>
    00006 #include <Wt/WSelectionBox>
    00007 #include <Wt/WContainerWidget>
    00008 #include <Wt/WImage>
    00009 #include <Wt/WIntValidator>
    00010 #include <Wt/WLabel>
    00011 #include <Wt/WLineEdit>
    00012 #include <Wt/WPushButton>
    00013 #include <Wt/WTableCell>
    00014 #include <Wt/WTextArea>
    00015 #include <Wt/WText>
    00016
    00017 Form::Form(WContainerWidget *parent)
    00018   : WTable(parent)
    00019 {
    00020   createUI();
    00021 }
    00022
    00023 void Form::createUI()
    00024 {
    00025   WLabel *label;
    00026   int row = 0;
    00027
    00028   // Title
    00029   elementAt(row, 0)->setColumnSpan(3);
    00030   elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
    00031   elementAt(row, 0)->setPadding(10);
    00032   WText *title = new WText(tr("example.form"),
    00033                            elementAt(row, 0));
    00034   title->decorationStyle().font().setSize(WFont::XLarge);
    00035
    00036   // error messages
    00037   ++row;
    00038   elementAt(row, 0)->setColumnSpan(3);
    00039   feedbackMessages_ = elementAt(row, 0);
    00040   feedbackMessages_->setPadding(5);
    00041
    00042   WCssDecorationStyle& errorStyle = feedbackMessages_->decorationStyle();
    00043   errorStyle.setForegroundColor(Wt::red);
    00044   errorStyle.font().setSize(WFont::Smaller);
    00045   errorStyle.font().setWeight(WFont::Bold);
    00046   errorStyle.font().setStyle(WFont::Italic);
    00047
    00048   // Name
    00049   ++row;
    00050   nameEdit_ = new WLineEdit(elementAt(row, 2));
    00051   label = new WLabel(tr("example.name"), elementAt(row, 0));
    00052   label->setBuddy(nameEdit_);
    00053   nameEdit_->setValidator(new WValidator(true));
    00054   nameEdit_->enterPressed().connect(this, &Form::submit);
    00055
    00056   // First name
    00057   ++row;
    00058   firstNameEdit_ = new WLineEdit(elementAt(row, 2));
    00059   label = new WLabel(tr("example.firstname"), elementAt(row,0));
    00060   label->setBuddy(firstNameEdit_);
    00061
    00062   // Country
    00063   ++row;
    00064   countryEdit_ = new WComboBox(elementAt(row, 2));
    00065   countryEdit_->addItem("");
    00066   countryEdit_->addItem("Belgium");
    00067   countryEdit_->addItem("Netherlands");
    00068   countryEdit_->addItem("United Kingdom");
    00069   countryEdit_->addItem("United States");
    00070   label = new WLabel(tr("example.country"), elementAt(row, 0));
    00071   label->setBuddy(countryEdit_);
    00072   countryEdit_->setValidator(new WValidator(true));
    00073   countryEdit_->changed().connect(this, &Form::countryChanged);
    00074
    00075   // City
    00076   ++row;
    00077   cityEdit_ = new WComboBox(elementAt(row, 2));
    00078   cityEdit_->addItem(tr("example.choosecountry"));
    00079   label = new WLabel(tr("example.city"), elementAt(row, 0));
    00080   label->setBuddy(cityEdit_);
    00081
    00082   // Birth date
    00083   ++row;
    00084
    00085   birthDateEdit_ = new WDateEdit(elementAt(row, 2));
    00086   birthDateEdit_->setBottom(WDate(1900, 1, 1));
    00087   birthDateEdit_->setTop(WDate::currentDate());
    00088   label = new WLabel(tr("example.birthdate"), elementAt(row, 0));
    00089   label->setBuddy(birthDateEdit_);
    00090   birthDateEdit_->setFormat("dd/MM/yyyy");
    00091   birthDateEdit_->validator()->setMandatory(true);
    00092
    00093   // Child count
    00094   ++row;
    00095   childCountEdit_ = new WLineEdit("0", elementAt(row, 2));
    00096   label = new WLabel(tr("example.childcount"),
    00097                      elementAt(row, 0));
    00098   label->setBuddy(childCountEdit_);
    00099   childCountEdit_->setValidator(new WIntValidator(0,30));
    00100   childCountEdit_->validator()->setMandatory(true);
    00101
    00102   ++row;
    00103   remarksEdit_ = new WTextArea(elementAt(row, 2));
    00104   remarksEdit_->setColumns(40);
    00105   remarksEdit_->setRows(5);
    00106   label = new WLabel(tr("example.remarks"),
    00107                      elementAt(row, 0));
    00108   label->setBuddy(remarksEdit_);
    00109
    00110   // Submit
    00111   ++row;
    00112   WPushButton *submit = new WPushButton(tr("submit"),
    00113                                         elementAt(row, 0));
    00114   submit->clicked().connect(this, &Form::submit);
    00115   submit->setMargin(15, Top);
    00116   elementAt(row, 0)->setColumnSpan(3);
    00117   elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
    00118
    00119   // Set column widths for label and validation icon
    00120   elementAt(2, 0)->resize(WLength(30, WLength::FontEx), WLength::Auto);
    00121   elementAt(2, 1)->resize(20, WLength::Auto);
    00122 }
    00123
    00124 void Form::countryChanged()
    00125 {
    00126   cityEdit_->clear();
    00127   cityEdit_->addItem("");
    00128   cityEdit_->setCurrentIndex(-1);
    00129
    00130   switch (countryEdit_->currentIndex()) {
    00131   case 0:
    00132     break;
    00133   case 1:
    00134     cityEdit_->addItem("Antwerp");
    00135     cityEdit_->addItem("Brussels");
    00136     cityEdit_->addItem("Oekene");
    00137     break;
    00138   case 2:
    00139     cityEdit_->addItem("Amsterdam");
    00140     cityEdit_->addItem("Den Haag");
    00141     cityEdit_->addItem("Rotterdam");
    00142     break;
    00143   case 3:
    00144     cityEdit_->addItem("London");
    00145     cityEdit_->addItem("Bristol");
    00146     cityEdit_->addItem("Oxford");
    00147     cityEdit_->addItem("Stonehenge");
    00148     break;
    00149   case 4:
    00150     cityEdit_->addItem("Boston");
    00151     cityEdit_->addItem("Chicago");
    00152     cityEdit_->addItem("Los Angelos");
    00153     cityEdit_->addItem("New York");
    00154     break;
    00155   }
    00156 }
    00157
    00158 bool Form::checkValid(WFormWidget *edit, const WString& text)
    00159 {
    00160   if (edit->validate() != WValidator::Valid) {
    00161     feedbackMessages_->addWidget(new WText(text));
    00162     feedbackMessages_->addWidget(new WBreak());
    00163     edit->label()->decorationStyle().setForegroundColor(Wt::red);
    00164     edit->setStyleClass("Wt-invalid");
    00165
    00166     return false;
    00167   } else {
    00168     edit->label()->decorationStyle().setForegroundColor(WColor());
    00169     edit->setStyleClass("");
    00170
    00171     return true;
    00172   }
    00173 }
    00174
    00175 bool Form::validate()
    00176 {
    00177   feedbackMessages_->clear();
    00178   bool valid = true;
    00179
    00180   if (!checkValid(nameEdit_, tr("error.name")))
    00181     valid = false;
    00182   if (!checkValid(countryEdit_, tr("error.country")))
    00183     valid = false;
    00184   if (!checkValid(birthDateEdit_, tr("error.birthdate")))
    00185     valid = false;
    00186   if (!checkValid(childCountEdit_, tr("error.childcount")))
    00187     valid = false;
    00188
    00189   return valid;
    00190 }
    00191
    00192 void Form::submit()
    00193 {
    00194   if (validate()) {
    00195     // do something useful with the data...
    00196     std::wstring name
    00197       = firstNameEdit_->text() + L" " + nameEdit_->text();
    00198
    00199     std::wstring remarks
    00200       = remarksEdit_->text();
    00201
    00202     clear();
    00203
    00204     new WText(WString::fromUTF8("<p>Thank you, {1}, "
    00205                                 "for all this precious data.</p>").arg(name),
    00206               elementAt(0, 0));
    00207
    00208     if (!remarks.empty())
    00209       new WText("<p>You had some remarks. Splendid !</p>", elementAt(0, 0));
    00210
    00211     wApp->quit();
    00212   }
    00213 }