|
19 | 19 | */ |
20 | 20 | class ComboDelegate : public Wt::WItemDelegate { |
21 | 21 | public: |
22 | | - ComboDelegate(Wt::WStringListModel* items) |
23 | | - : items_(items) |
24 | | - {} |
25 | | - |
26 | | - void setModelData(const boost::any &editState, Wt::WAbstractItemModel* model, |
27 | | - const Wt::WModelIndex &index) const |
28 | | - { |
29 | | - int stringIdx = Wt::asNumber(editState); |
30 | | - model->setData(index, stringIdx, Wt::UserRole); |
31 | | - model->setData(index, items_->stringList()[stringIdx], Wt::DisplayRole); |
32 | | - } |
33 | | - |
34 | | - boost::any editState(Wt::WWidget* editor) const |
35 | | - { |
36 | | - Wt::WComboBox* combo = dynamic_cast<Wt::WComboBox*> |
37 | | - (dynamic_cast<Wt::WContainerWidget*>(editor)->widget(0)); |
38 | | - return combo->currentIndex(); |
39 | | - } |
40 | | - |
41 | | - void setEditState(Wt::WWidget* editor, const boost::any &value) const |
42 | | - { |
43 | | - Wt::WComboBox* combo = dynamic_cast<Wt::WComboBox*> |
44 | | - (dynamic_cast<Wt::WContainerWidget*>(editor)->widget(0)); |
45 | | - combo->setCurrentIndex(Wt::asNumber(value)); |
46 | | - } |
| 22 | + ComboDelegate(Wt::WAbstractItemModel* items) |
| 23 | + : items_(items) |
| 24 | + { } |
| 25 | + |
| 26 | + void setModelData(const boost::any &editState, Wt::WAbstractItemModel* model, |
| 27 | + const Wt::WModelIndex &index) const |
| 28 | + { |
| 29 | + int stringIdx = Wt::asNumber(editState); |
| 30 | + model->setData(index, stringIdx, Wt::UserRole); |
| 31 | + model->setData(index, items_->data(stringIdx, 0), Wt::DisplayRole); |
| 32 | + } |
| 33 | + |
| 34 | + boost::any editState(Wt::WWidget* editor) const |
| 35 | + { |
| 36 | + Wt::WComboBox* combo = dynamic_cast<Wt::WComboBox*> |
| 37 | + (dynamic_cast<Wt::WContainerWidget*>(editor)->widget(0)); |
| 38 | + return combo->currentIndex(); |
| 39 | + } |
| 40 | + |
| 41 | + void setEditState(Wt::WWidget* editor, const boost::any &value) const |
| 42 | + { |
| 43 | + Wt::WComboBox* combo = dynamic_cast<Wt::WComboBox*> |
| 44 | + (dynamic_cast<Wt::WContainerWidget*>(editor)->widget(0)); |
| 45 | + combo->setCurrentIndex(Wt::asNumber(value)); |
| 46 | + } |
47 | 47 |
|
48 | 48 | protected: |
49 | | - virtual Wt::WWidget* createEditor(const Wt::WModelIndex &index, |
50 | | - Wt::WFlags<Wt::ViewItemRenderFlag> flags) const |
51 | | - { |
52 | | - Wt::WContainerWidget* container = new Wt::WContainerWidget(); |
53 | | - Wt::WComboBox* combo = new Wt::WComboBox(container); |
54 | | - combo->setModel(items_); |
55 | | - combo->setCurrentIndex(Wt::asNumber(index.data(Wt::UserRole))); |
56 | | - |
57 | | - combo->changed().connect(boost::bind(&ComboDelegate::doCloseEditor, this, |
58 | | - container, true)); |
59 | | - combo->enterPressed().connect(boost::bind(&ComboDelegate::doCloseEditor, |
| 49 | + virtual Wt::WWidget* createEditor(const Wt::WModelIndex &index, |
| 50 | + Wt::WFlags<Wt::ViewItemRenderFlag> flags) const |
| 51 | + { |
| 52 | + Wt::WContainerWidget* container = new Wt::WContainerWidget(); |
| 53 | + Wt::WComboBox* combo = new Wt::WComboBox(container); |
| 54 | + combo->setModel(items_); |
| 55 | + combo->setCurrentIndex(Wt::asNumber(index.data(Wt::UserRole))); |
| 56 | + |
| 57 | + combo->changed().connect(boost::bind(&ComboDelegate::doCloseEditor, this, |
| 58 | + container, true)); |
| 59 | + combo->enterPressed().connect(boost::bind(&ComboDelegate::doCloseEditor, |
60 | 60 | this, container, true)); |
61 | | - combo->escapePressed().connect(boost::bind(&ComboDelegate::doCloseEditor, |
62 | | - this, container, false)); |
| 61 | + combo->escapePressed().connect(boost::bind(&ComboDelegate::doCloseEditor, |
| 62 | + this, container, false)); |
63 | 63 |
|
64 | | - return container; |
65 | | - } |
| 64 | + return container; |
| 65 | + } |
66 | 66 |
|
67 | 67 | private: |
68 | | - Wt::WStringListModel* items_; |
| 68 | + Wt::WAbstractItemModel* items_; |
69 | 69 |
|
70 | | - void doCloseEditor(Wt::WWidget *editor, bool save) const |
71 | | - { |
72 | | - closeEditor().emit(editor, save); |
73 | | - } |
| 70 | + void doCloseEditor(Wt::WWidget *editor, bool save) const |
| 71 | + { |
| 72 | + closeEditor().emit(editor, save); |
| 73 | + } |
74 | 74 | }; |
75 | 75 |
|
76 | 76 | SAMPLE_BEGIN(ComboDelegateTable) |
| 77 | + |
77 | 78 | Wt::WTableView *table = new Wt::WTableView(); |
78 | 79 |
|
79 | 80 | // create model |
|
0 commit comments