/src/wt/src/Wt/WAbstractItemDelegate.h
Line | Count | Source |
1 | | // This may look like C code, but it's really -*- C++ -*- |
2 | | /* |
3 | | * Copyright (C) 2009 Emweb bv, Herent, Belgium. |
4 | | * |
5 | | * See the LICENSE file for terms of use. |
6 | | */ |
7 | | #ifndef WABSTRACTITEMDELEGATE_H_ |
8 | | #define WABSTRACTITEMDELEGATE_H_ |
9 | | |
10 | | #include <Wt/WAny.h> |
11 | | #include <Wt/WObject.h> |
12 | | #include <Wt/WFlags.h> |
13 | | #include <Wt/WSignal.h> |
14 | | #include <Wt/WValidator.h> |
15 | | |
16 | | namespace Wt { |
17 | | |
18 | | /*! \brief Enumeration that specifies an option for rendering a view item. |
19 | | * |
20 | | * \sa WAbstractItemDelegate::update() |
21 | | */ |
22 | | enum class ViewItemRenderFlag { |
23 | | Selected = 0x1, //!< %Render as selected |
24 | | Editing = 0x2, //!< %Render in editing mode |
25 | | Focused = 0x4, //!< %Render (the editor) focused |
26 | | Invalid = 0x8 //!< %Render as invalid |
27 | | }; |
28 | | |
29 | | W_DECLARE_OPERATORS_FOR_FLAGS(ViewItemRenderFlag) |
30 | | |
31 | | class WAbstractItemModel; |
32 | | class WWidget; |
33 | | class WModelIndex; |
34 | | |
35 | | /*! \class WAbstractItemDelegate Wt/WAbstractItemDelegate.h Wt/WAbstractItemDelegate.h |
36 | | * \brief Abstract delegate class for rendering an item in an item view. |
37 | | * |
38 | | * Rendering of an item in a WAbstractItemView is delegated to an |
39 | | * implementation of this delegate class. The default implementation |
40 | | * used by %Wt's item views is WItemDelegate. To provide specialized |
41 | | * rendering support, you can reimplement this class (or specialize |
42 | | * WItemDelegate). |
43 | | * |
44 | | * As a delegate is used for rendering multiple items, the class should |
45 | | * not keep state about one specific item. |
46 | | * |
47 | | * A delegate may provide editing support by instantiating an editor |
48 | | * when update() is called with the Wt::ViewItemRenderFlag::Editing flag. In that |
49 | | * case, you will also need to implement editState() and |
50 | | * setEditState() to support virtual scrolling and setModelData() to |
51 | | * save the edited value to the model. For an example, see the |
52 | | * WItemDelegate. |
53 | | * |
54 | | * \sa WAbstractItemView::setItemDelegateForColumn() |
55 | | * |
56 | | * \ingroup modelview |
57 | | */ |
58 | | class WT_API WAbstractItemDelegate : public WObject |
59 | | { |
60 | | public: |
61 | | /*! \brief Constructor. |
62 | | */ |
63 | | WAbstractItemDelegate(); |
64 | | |
65 | | /*! \brief Destructor. |
66 | | */ |
67 | | virtual ~WAbstractItemDelegate(); |
68 | | |
69 | | /*! \brief Creates or updates a widget that renders an item. |
70 | | * |
71 | | * The item is specified by its model \p index, which also |
72 | | * indicates the model. If an existing widget already renders the |
73 | | * item, but needs to be updated, it is passed as the \p widget |
74 | | * parameter. |
75 | | * |
76 | | * When \p widget is \c nullptr, a new widget needs to be created and returned. |
77 | | * |
78 | | * If you want to replace the \p widget with a new one, |
79 | | * return the new widget. The old \p widget will be removed. |
80 | | * Return \c nullptr if you do not want to replace the \p widget. |
81 | | * |
82 | | * You can remove the \p widget from its parent for reuse with WWidget::removeFromParent(). |
83 | | * |
84 | | * The returned widget should be a widget that responds properly to |
85 | | * be given a height, width and style class. In practice, that means |
86 | | * it cannot have a border or margin, and thus cannot be a |
87 | | * WFormWidget since those widgets typically have built-in borders |
88 | | * and margins. If you want to return a form widget (for editing the item), |
89 | | * you should wrap it in a container widget. |
90 | | * |
91 | | * The \p flags parameter indicates options for rendering the |
92 | | * item. |
93 | | */ |
94 | | virtual std::unique_ptr<WWidget> update |
95 | | (WWidget *widget, const WModelIndex& index, |
96 | | WFlags<ViewItemRenderFlag> flags) = 0; |
97 | | |
98 | | /*! \brief Updates the model index of a widget. |
99 | | * |
100 | | * This method is invoked by the view when due to row/column insertions or |
101 | | * removals, the index has shifted. |
102 | | * |
103 | | * You should reimplement this method only if you are storing the |
104 | | * model index in the \p widget, to update the stored model index. |
105 | | * |
106 | | * The default implementation does nothing. |
107 | | */ |
108 | | virtual void updateModelIndex(WWidget *widget, const WModelIndex& index); |
109 | | |
110 | | /*! \brief Returns the current edit state. |
111 | | * |
112 | | * Because a View may support virtual scrolling in combination with |
113 | | * editing, it may happen that the view decides to delete the editor |
114 | | * widget while the user is editing. To allow to reconstruct the editor |
115 | | * in its original state, the View will therefore ask for the editor |
116 | | * to serialize its state in a boost::any. |
117 | | * |
118 | | * When the view decides to close an editor and save its value back |
119 | | * to the model, he will first call editState() and then |
120 | | * setModelData(). |
121 | | * |
122 | | * The default implementation assumes a read-only delegate, and |
123 | | * returns a boost::any(). |
124 | | * |
125 | | * \sa setEditState(), setModelData() |
126 | | */ |
127 | | virtual cpp17::any editState(WWidget *widget, const WModelIndex& index) const; |
128 | | |
129 | | /*! \brief Sets the editor data from the editor state. |
130 | | * |
131 | | * When the View scrolls back into view an item that was being edited, |
132 | | * he will use setEditState() to allow the editor to restore its current |
133 | | * editor state. |
134 | | * |
135 | | * The default implementation assumes a read-only delegate and does |
136 | | * nothing. |
137 | | * |
138 | | * \sa editState() |
139 | | */ |
140 | | virtual void setEditState(WWidget *widget, const WModelIndex& index, |
141 | | const cpp17::any& value) const; |
142 | | |
143 | | /*! \brief Returns whether the edited value is valid. |
144 | | * |
145 | | * The default implementation does nothing and returns Valid. |
146 | | * |
147 | | * \sa WValidator::validate() |
148 | | */ |
149 | | virtual ValidationState validate(const WModelIndex& index, |
150 | | const cpp17::any& editState) const; |
151 | | |
152 | | /*! \brief Saves the edited data to the model. |
153 | | * |
154 | | * The View will use this method to save the edited value to the model. |
155 | | * The \p editState is first fetched from the editor using editState(). |
156 | | * |
157 | | * The default implementation assumes a read-only delegate does |
158 | | * nothing. |
159 | | */ |
160 | | virtual void setModelData(const cpp17::any& editState, |
161 | | WAbstractItemModel *model, |
162 | | const WModelIndex& index) const; |
163 | | |
164 | | /*! \brief %Signal which indicates that an editor needs to be closed. |
165 | | * |
166 | | * The delegate should emit this signal when it decides for itself |
167 | | * that it should be closed (e.g. because the user confirmed the |
168 | | * edited value or cancelled the editing). The View will then rerender |
169 | | * the item if needed. |
170 | | * |
171 | | * The second boolean argument passed to the signal is a flag which |
172 | | * indicates whether the editor feels that the value should be saved or |
173 | | * cancelled. |
174 | | * |
175 | | * \sa WAbstractItemView::closeEditor() |
176 | | */ |
177 | 0 | Signal<WWidget *, bool>& closeEditor() { return closeEditor_; } |
178 | | |
179 | | /*! \brief %Signal which indicates that an editor needs to be closed. |
180 | | * |
181 | | * \sa closeEditor() |
182 | | */ |
183 | 0 | const Signal<WWidget *, bool>& closeEditor() const { return closeEditor_; } |
184 | | |
185 | | private: |
186 | | Signal<WWidget *, bool> closeEditor_; |
187 | | }; |
188 | | |
189 | | } |
190 | | |
191 | | #endif // WABSTRACTITEMDELEGATE_H_ |