/usr/include/Wt/WItemDelegate is in libwt-dev 3.3.3+dfsg-4.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | // This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2009 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WITEMDELEGATE_H_
#define WITEMDELEGATE_H_
#include <Wt/WAbstractItemDelegate>
#include <Wt/WCheckBox>
#include <Wt/WLineEdit>
#include <Wt/WString>
namespace Wt {
class WAnchor;
class WCheckBox;
class WContainerWidget;
class WImage;
class WLineEdit;
class WText;
#ifndef WT_CNOR
template <class Widget> class IndexEdit;
typedef IndexEdit<WCheckBox> IndexCheckBox;
typedef IndexEdit<WContainerWidget> IndexContainerWidget;
typedef IndexEdit<WAnchor> IndexAnchor;
typedef IndexEdit<WText> IndexText;
#else
class IndexCheckBox;
class IndexContainerWidget;
class IndexAnchor;
class IndexText;
#endif // WT_CNOR
/*! \class WItemDelegate Wt/WItemDelegate Wt/WItemDelegate
* \brief Standard delegate class for rendering a view item.
*
* This class provides the standard implementation for rendering an
* item (as in a WAbstractItemView), and renders data provided by the
* standard data roles (see ItemDataRole). It also provides default
* editing support using a line edit.
*
* You may provide special editing support for an item by specializing
* the widget and reimplement createEditor(), setModelData(),
* editState(), and setEditState().
*
* \ingroup modelview
*/
class WT_API WItemDelegate : public WAbstractItemDelegate
{
public:
/*! \brief Create an item delegate.
*/
WItemDelegate(WObject *parent = 0);
/*! \brief Creates or updates a widget that renders an item.
*
* The following properties of an item are rendered:
*
* - text using the Wt::DisplayRole data, with the format specified
* by setTextFormat()
* - a check box depending on the Wt::ItemIsUserCheckable flag and
* Wt::CheckStateRole data
* - an anchor depending on Wt::InternalPathRole or Wt::UrlRole values
* - an icon depending on the value of Wt::DecorationRole
* - a tooltip depending on the value of Wt::ToolTipRole
* - a custom style class depending on the value of Wt::StyleClassRole
*
* When the flags indicates Wt::RenderEditing, then createEditor() is
* called to create a suitable editor for editing the item.
*/
virtual WWidget *update(WWidget *widget, const WModelIndex& index,
WFlags<ViewItemRenderFlag> flags);
virtual void updateModelIndex(WWidget *widget, const WModelIndex& index);
/*! \brief Sets the text format string.
*
* \if cpp
*
* The DisplayRole data is converted to a string using asString() by passing
* the given format.
*
* \elseif java
*
* The DisplayRole data is converted to a string using {javadoclink
* StringUtils#asString(Object)}, passing the given format. If the format is
* an empty string, this corresponds to {javadoclink Object#toString()}.
*
* \endif
*
* The default value is "".
*/
void setTextFormat(const WT_USTRING& format);
/*! \brief Returns the text format string.
*
* \sa setTextFormat()
*/
const WT_USTRING& textFormat() const { return textFormat_; }
/*! \brief Saves the edited data to the model.
*
* The default implementation saves the current edit value to the model.
* You will need to reimplement this method for a custom editor.
*
* As an example of how to deal with a specialized editor, consider the
* default implementation:
* \if cpp
* \code
* void WItemDelegate::setModelData(const boost::any& editState,
* Wt::WAbstractItemModel *model,
* const Wt::WModelIndex& index) const
* {
* model->setData(index, editState, EditRole);
* }
* \endcode
* \elseif java
* \code
* public void setModelData(Object editState, WAbstractItemModel model, WModelIndex index) {
* model.setData(index, editState, ItemDataRole.EditRole);
* }
* \endcode
* \endif
*
* \sa createEditor(), editState()
*/
virtual void setModelData(const boost::any& editState,
WAbstractItemModel *model,
const WModelIndex& index) const;
/*! \brief Returns the current edit state.
*
* The default implementation returns the current text in the line edit.
* You will need to reimplement this method for a custom editor.
*
* As an example of how to deal with a specialized editor, consider the
* default implementation:
* \if cpp
* \code
* boost::any WItemDelegate::editState(Wt::WWidget *editor) const
* {
* Wt::WContainerWidget *w = dynamic_cast<Wt::WContainerWidget *>(editor);
* Wt::WLineEdit *lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));
*
* return boost::any(lineEdit->text());
* }
* \endcode
* \elseif java
* \code
* public Object getEditState(WWidget editor) {
* WContainerWidget w = (WContainerWidget) editor;
* WLineEdit lineEdit = (WLineEdit) w.getWidget(0);
* return lineEdit.getText();
* }
* \endcode
* \endif
*
* \sa createEditor(), setEditState(), setModelData()
*/
virtual boost::any editState(WWidget *editor) const;
/*! \brief Sets the editor data from the editor state.
*
* The default implementation resets the text in the line edit.
* You will need to reimplement this method if for a custom editor.
*
* As an example of how to deal with a specialized editor, consider the
* default implementation:
* \if cpp
* \code
* void WItemDelegate::setEditState(Wt::WWidget *editor, const boost::any& value) const
* {
* Wt::WContainerWidget *w = dynamic_cast<Wt::WContainerWidget *>(editor);
* Wt::WLineEdit *lineEdit = dynamic_cast<Wt::WLineEdit *>(w->widget(0));
*
* lineEdit->setText(boost::any_cast<Wt::WString>(value));
* }
* \endcode
* \elseif java
* \code
* public void setEditState(WWidget editor, Object value) {
* WContainerWidget w = (WContainerWidget) editor;
* WLineEdit lineEdit = (WLineEdit) w.getWidget(0);
* lineEdit.setText((String) value);
* }
* \endcode
* \endif
*
* \sa createEditor()
*/
virtual void setEditState(WWidget *editor, const boost::any& value) const;
protected:
/*! \brief Creates an editor for a data item.
*
* The default implementation returns a WLineEdit which edits the
* item's Wt::EditRole value.
*
* You may reimplement this method to provide a suitable editor, or
* to attach a custom validator. In that case, you will probably
* also want to reimplement editState(), setEditState(), and
* setModelData().
*
* The editor should not keep a reference to the model index (it
* does not need to since setModelData() will provide the proper
* model index to save the data to the model). Otherwise, because
* model indexes may shift because of row or column insertions, you
* should reimplement updateModelIndex().
*
* As an example of how to provide a specialized editor, consider the
* default implementation, which returns a WLineEdit:
* \if cpp
* \code
* Wt::WWidget *WItemDelegate::createEditor(const Wt::WModelIndex& index, WFlags<ViewItemRenderFlag> flags) const
* {
* Wt::WContainerWidget *result = new Wt::WContainerWidget();
* result->setSelectable(true);
*
* Wt::WLineEdit *lineEdit = new Wt::WLineEdit();
* lineEdit->setText(asString(index.data(EditRole), textFormat_));
* lineEdit->enterPressed().connect(boost::bind(&WItemDelegate::doCloseEditor, this, result, true));
* lineEdit->escapePressed().connect(boost::bind(&WItemDelegate::doCloseEditor, this, result, false));
*
* if (flags & RenderFocused)
* lineEdit->setFocus();
*
* // We use a layout so that the line edit fills the entire cell.
* result->setLayout(new WHBoxLayout());
* result->layout()->setContentsMargins(1, 1, 1, 1);
* result->layout()->addWidget(lineEdit);
*
* return result;
* }
*
* void WItemDelegate::doCloseEditor(Wt::WWidget *editor, bool save) const
* {
* closeEditor().emit(editor, save);
* }
* \endcode
* \elseif java
* \code
* protected WWidget createEditor(WModelIndex index, EnumSet<ViewItemRenderFlag> flags) {
* final WContainerWidget result = new WContainerWidget();
* result.setSelectable(true);
* WLineEdit lineEdit = new WLineEdit();
* lineEdit.setText(StringUtils.asString(index.getData(ItemDataRole.EditRole), this.textFormat_).toString());
* lineEdit.enterPressed().addListener(this, new Signal.Listener() {
* public void trigger() {
* WItemDelegate.this.closeEditor().trigger(result, true);
* }
* });
* lineEdit.escapePressed().addListener(this, new Signal.Listener() {
* public void trigger() {
* WItemDelegate.this.closeEditor().trigger(result, false);
* }
* });
*
* if (flags.contains(ViewItemRenderFlag.RenderFocused))
* lineEdit.setFocus();
*
* result.setLayout(new WHBoxLayout());
* result.getLayout().setContentsMargins(1, 1, 1, 1);
* result.getLayout().addWidget(lineEdit);
* return result;
* }
* \endcode
* \endif
*/
virtual WWidget *createEditor(const WModelIndex& index,
WFlags<ViewItemRenderFlag> flags) const;
private:
WT_USTRING textFormat_;
struct WidgetRef {
WWidget *w;
WidgetRef(WWidget *widget) : w(widget) { }
};
IndexCheckBox *checkBox(WidgetRef& w, const WModelIndex& index,
bool autoCreate, bool triState = false);
IndexText *textWidget(WidgetRef& w, const WModelIndex& index);
WImage *iconWidget(WidgetRef& w, const WModelIndex& index, bool autoCreate = false);
IndexAnchor *anchorWidget(WidgetRef& w, const WModelIndex& index);
void onCheckedChange(IndexCheckBox *checkBox) const;
void doCloseEditor(WWidget *editor, bool save) const;
};
}
#endif // WITEMDELEGATE_H_
|