Bug #2717 ยป 0001-Minor-fixes-to-category-chart-example.patch
| examples/widgetgallery/examples/CategoryChart.cpp | ||
|---|---|---|
|
return container;
|
||
|
/*
|
||
|
* Configure all model items as selectable and editable.
|
||
|
* Configure all model items as editable
|
||
|
*/
|
||
|
for (int row = 0; row < model->rowCount(); ++row)
|
||
|
for (int col = 0; col < model->columnCount(); ++col)
|
||
|
for (int col = 1; col < model->columnCount(); ++col)
|
||
|
model->item(row, col)->setFlags(Wt::ItemIsEditable);
|
||
|
/*
|
||
| ... | ... | |
|
if (Wt::WApplication::instance()->environment().ajax()) {
|
||
|
table->setEditTriggers(Wt::WAbstractItemView::SingleClicked);
|
||
|
table->setEditOptions(table->editOptions() |
|
||
|
Wt::WAbstractItemView::SaveWhenClosed);
|
||
|
} else {
|
||
|
table->setEditTriggers(Wt::WAbstractItemView::NoEditTrigger);
|
||
|
}
|
||
|
/*
|
||
|
* Use a delegate for the numeric data which rounds values sensibly.
|
||
|
* Specialized delegate that closes editor on blur events and provides field validation
|
||
|
* NOTE: to reduce duplicate code, some knowledge of WItemDelegate implementation is used
|
||
|
*/
|
||
|
Wt::WItemDelegate *delegate = new Wt::WItemDelegate(table);
|
||
|
class ItemDelegateWithBlurSupport : public Wt::WItemDelegate
|
||
|
{
|
||
|
public:
|
||
|
ItemDelegateWithBlurSupport(Wt::WObject *parent=0) : Wt::WItemDelegate(parent) {}
|
||
|
protected:
|
||
|
Wt::WWidget *createEditor(const Wt::WModelIndex &index,
|
||
|
Wt::WFlags<Wt::ViewItemRenderFlag> flags) const override
|
||
|
{
|
||
|
auto w = dynamic_cast<Wt::WContainerWidget *>(Wt::WItemDelegate::createEditor(index, flags));
|
||
|
auto lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));
|
||
|
lineEdit->blurred().connect(std::bind([=](){closeEditor().emit(w, false);}));
|
||
|
return w;
|
||
|
}
|
||
|
Wt::WValidator::State validate(const Wt::WModelIndex &index,
|
||
|
const boost::any &editState) const override
|
||
|
{
|
||
|
if (editState.type() == typeid(double))
|
||
|
return Wt::WValidator::Valid;
|
||
|
else
|
||
|
return Wt::WValidator::Invalid;
|
||
|
}
|
||
|
/*
|
||
|
* In conjunction with validate(), only accept values that convert to double without error
|
||
|
*/
|
||
|
boost::any editState(Wt::WWidget *editor) const override
|
||
|
{
|
||
|
auto w = dynamic_cast<Wt::WContainerWidget *>(editor);
|
||
|
auto lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));
|
||
|
try {
|
||
|
return boost::lexical_cast<double>(lineEdit->text());
|
||
|
}
|
||
|
catch (boost::bad_lexical_cast& e) {
|
||
|
return boost::any();
|
||
|
}
|
||
|
}
|
||
|
void setModelData(const boost::any &editState, Wt::WAbstractItemModel *model,
|
||
|
const Wt::WModelIndex &index) const override
|
||
|
{
|
||
|
if (validate(index, editState) == Wt::WValidator::Valid)
|
||
|
model->setData(index, editState, Wt::EditRole);
|
||
|
}
|
||
|
};
|
||
|
/*
|
||
|
* Use a delegate for the numeric data which rounds values sensibly
|
||
|
*/
|
||
|
auto delegate = new ItemDelegateWithBlurSupport(table);
|
||
|
delegate->setTextFormat("%.f");
|
||
|
table->setItemDelegate(delegate);
|
||