Skip to content

Commit 6b7bf74

Browse files
YoikaGRockinRoel
authored andcommitted
Specialized WFormDelegate for double
This delegate will create a WLineEdit for representing double values in WTemplateFormView, and a WDoubleValidator for validation.
1 parent ede2471 commit 6b7bf74

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/Wt/Form/WFormDelegate.C

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <Wt/WDateEdit.h>
1010
#include <Wt/WDateTime.h>
1111
#include <Wt/WDateValidator.h>
12+
#include <Wt/WDoubleValidator.h>
1213
#include <Wt/WIntValidator.h>
1314
#include <Wt/WLineEdit.h>
1415
#include <Wt/WLocale.h>
@@ -179,6 +180,51 @@ void WFormDelegate<int, void>::updateModelValue(Wt::WFormModel *model, Wt::WForm
179180
}
180181

181182
void WFormDelegate<int, void>::updateViewValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit)
183+
{
184+
Wt::cpp17::any v = model->value(field);
185+
if (v.type() != typeid(Wt::WValidator::Result)) {
186+
edit->setValueText(Wt::asString(v));
187+
}
188+
}
189+
190+
WFormDelegate<double, void>::WFormDelegate()
191+
: WAbstractFormDelegate()
192+
{
193+
}
194+
195+
std::unique_ptr<Wt::WWidget> WFormDelegate<double, void>::createFormWidget()
196+
{
197+
return std::make_unique<Wt::WLineEdit>();
198+
}
199+
200+
std::shared_ptr<Wt::WValidator> WFormDelegate<double, void>::createValidator()
201+
{
202+
return std::make_shared<Wt::WDoubleValidator>();
203+
}
204+
205+
void WFormDelegate<double, void>::updateModelValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit)
206+
{
207+
if (!edit->valueText().empty()) {
208+
double value = 0.0;
209+
try {
210+
value = Wt::WLocale::currentLocale().toDouble(edit->valueText());
211+
} catch (std::exception& e) {
212+
LOG_ERROR("Could not convert '" << edit->valueText() << "' to double: " << e.what());
213+
214+
std::shared_ptr<Wt::WValidator> validator = edit->validator();
215+
if (validator) {
216+
model->setValue(field, validator->validate(edit->valueText()));
217+
}
218+
219+
return;
220+
}
221+
model->setValue(field, value);
222+
} else {
223+
model->setValue(field, 0.0);
224+
}
225+
}
226+
227+
void WFormDelegate<double, void>::updateViewValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit)
182228
{
183229
Wt::cpp17::any v = model->value(field);
184230
if (v.type() != typeid(Wt::WValidator::Result)) {

src/Wt/Form/WFormDelegate.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,37 @@ class WT_API WFormDelegate<int, void> : public WAbstractFormDelegate
166166
*/
167167
void updateViewValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit) override;
168168
};
169+
170+
/*! \brief Form delegate for double
171+
*
172+
* This will create a WLineEdit to display the double value
173+
* in the View. Additionally the delegate will also initialize
174+
* the WDoubleValidator for validation.
175+
*/
176+
template<>
177+
class WT_API WFormDelegate<double, void> : public WAbstractFormDelegate
178+
{
179+
public:
180+
/*! \brief Create a form delegate
181+
*/
182+
WFormDelegate();
183+
184+
/*! \brief Create WLineEdit to be used in the View
185+
*/
186+
std::unique_ptr<Wt::WWidget> createFormWidget() override;
187+
188+
/*! \brief Create WDoubleValidator for validation
189+
*/
190+
std::shared_ptr<Wt::WValidator> createValidator() override;
191+
192+
/*! \brief Update the value in the model
193+
*/
194+
void updateModelValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit) override;
195+
196+
/*! \brief Update the value in the View
197+
*/
198+
void updateViewValue(Wt::WFormModel *model, Wt::WFormModel::Field field, Wt::WFormWidget *edit) override;
199+
};
169200
}
170201
}
171202

0 commit comments

Comments
 (0)