Class WAbstractItemModel
- Direct Known Subclasses:
WAbstractListModel,WAbstractProxyModel,WAbstractTableModel,WItemModel,WStandardItemModel
This abstract model is used by several JWt view widgets as data models.
It may model data for both tree-like and table-like view widgets. Data is therefore organized
in a hierarchical structure of tables, where every item stores data and items in column 0 can be
the parent of a nested table of data. Every data item is uniquely identified by their row, column
and parent index, and items may be referenced using the helper class WModelIndex.
Each item may provide data for one or more roles, and indicate options
using flags. The different roles can be used to model different aspects of an
item (its text value, an icon, style class), or to hold auxiliary custom information. The flags
provide information to the View on possible interactivity.
Side.Top level data have a null parent WModelIndex.
The data itself is of type Object, which can either be null, or be any type of
data. Depending on the role however, view classes may expect certain types of data (e.g.
numerical types for charts) or will convert the data to a string (e.g. for ItemDataRole.Display).
To implement a custom model, you need to reimplement the following methods:
getIndex()andgetParent()methods that allow one to navigate the modelgetColumnCount()andgetRowCount()to specify the top level geometry and the nested geometry at every itemgetData()to return the data for an item- optionally,
getHeaderData()to return row and column header data - optionally,
getFlags()to indicate data options
A crucial point in implementing a hierarchical model is to decide how to reference an index in
terms of an internal pointer (WModelIndex.getInternalPointer()). Other than the top-level
index, which is special since it is referenced using an invalid index, every index with children
must be identifiable using this object. For example, in the WStandardItemModel, the
internal pointer points to the parent WStandardItem. For table models, the internal
pointer plays no role, since only the toplevel index has children.
If you want to support editing of the model, then you need to indicate this support using a
ItemFlag.Editable flag, and reimplement setData(). View classes will use the ItemDataRole.Edit to read and update the data for the editor.
When the model's data has been changed, the model must emit the dataChanged() signal.
Finally, there is a generic interface for insertion of new data or removal of data (changing the geometry), although this interface is not yet used by any View class:
Alternatively, you can provide your own API for changing the model. In either case it is important that you call the corresponding protected member functions which will emit the relevant signals so that views can adapt themselves to the new geometry.
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbeginInsertColumns(WModelIndex parent, int first, int last) Method to be called before inserting columns.protected voidbeginInsertRows(WModelIndex parent, int first, int last) Method to be called before inserting rows.protected voidbeginRemoveColumns(WModelIndex parent, int first, int last) Method to be called before removing columns.protected voidbeginRemoveRows(WModelIndex parent, int first, int last) Method to be called before removing rows.voidcollapseColumn(int column) Collapses a column.Signal emitted before a number of columns will be inserted.Signal emitted before a number of columns will be removed.Signal emitted after a number of columns were inserted.Signal emitted after a number of columns were removed.protected voidcopyData(WModelIndex sIndex, WModelIndex dIndex) Copy data to an index in this model.protected WModelIndexcreateIndex(int row, int column, long id) Creates a model index for the given row and column.protected WModelIndexcreateIndex(int row, int column, Object ptr) Creates a model index for the given row and column.Signal emitted when some data was changed.voiddropEvent(WDropEvent e, DropAction action, int row, int column, WModelIndex parent) Handles a drop event.voiddropEvent(WDropEvent e, DropAction action, WModelIndex pindex, Side side) Handles a drop event.protected voidMethod to be called after inserting columns.protected voidMethod to be called after inserting rows.protected voidMethod to be called after removing columns.protected voidMethod to be called after removing rows.voidexpandColumn(int column) Expands a column.fromRawIndex(Object rawIndex) Converts a raw pointer to a model index.Returns a list of mime-types that could be accepted for a drop event.final intReturns the number of columns.abstract intgetColumnCount(WModelIndex parent) Returns the number of columns.final ObjectgetData(int row, int column) Returns the data item at the given column and row.final ObjectgetData(int row, int column, ItemDataRole role) Returns the data item at the given column and row.getData(int row, int column, ItemDataRole role, WModelIndex parent) Returns the data item at the given column and row.final ObjectgetData(WModelIndex index) Returns data at a specified model index for the given role.abstract ObjectgetData(WModelIndex index, ItemDataRole role) Returns data at a specified model index for the given role.getFlags(WModelIndex index) Returns the flags for an item.final ObjectgetHeaderData(int section) Returns the row or column header data.final ObjectgetHeaderData(int section, Orientation orientation) Returns the row or column header data.getHeaderData(int section, Orientation orientation, ItemDataRole role) Returns the row or column header data.final EnumSet<HeaderFlag> getHeaderFlags(int section) Returns the flags for a header.getHeaderFlags(int section, Orientation orientation) Returns the flags for a header.final WModelIndexgetIndex(int row, int column) Returns the child index for the given row and column.abstract WModelIndexgetIndex(int row, int column, WModelIndex parent) Returns the child index for the given row and column.getItemData(WModelIndex index) Returns all data at a specific index.Returns a mime-type for dragging a set of indexes.abstract WModelIndexgetParent(WModelIndex index) Returns the parent for a model index.final intReturns the number of rows.abstract intgetRowCount(WModelIndex parent) Returns the number of rows.booleanhasChildren(WModelIndex index) Returns if there are children at an index.final booleanhasIndex(int row, int column) Returns if an index at the given position is valid (i.e.booleanhasIndex(int row, int column, WModelIndex parent) Returns if an index at the given position is valid (i.e.Signal emitted when some header data was changed.final booleaninsertColumn(int column) Inserts one column.booleaninsertColumn(int column, WModelIndex parent) Inserts one column.final booleaninsertColumns(int column, int count) Inserts one or more columns.booleaninsertColumns(int column, int count, WModelIndex parent) Inserts one or more columns.final booleaninsertRow(int row) Inserts one row.booleaninsertRow(int row, WModelIndex parent) Inserts one row.final booleaninsertRows(int column, int count) Inserts one or more rows.booleaninsertRows(int column, int count, WModelIndex parent) Inserts one or more rows.Signal emitted when the layout is about to be changed.Signal emitted when the layout is changed.match(WModelIndex start, ItemDataRole role, Object value, int hits, MatchOptions flags) Returns an index list for data items that match.Signal emitted when the model was reset.final booleanremoveColumn(int column) Removes one column.booleanremoveColumn(int column, WModelIndex parent) Removes one column.final booleanremoveColumns(int column, int count) Removes columns.booleanremoveColumns(int column, int count, WModelIndex parent) Removes columns.final booleanremoveRow(int row) Removes one row.booleanremoveRow(int row, WModelIndex parent) Removes one row.final booleanremoveRows(int column, int count) Removes rows.booleanremoveRows(int column, int count, WModelIndex parent) Removes rows.protected voidreset()Resets the model and invalidate any data.Signal emitted before a number of rows will be inserted.Signal emitted before a number of rows will be removed.Signal emitted after a number of rows were inserted.Signal emitted after a number of rows were removed.final booleanSets data at the given row and column.final booleansetData(int row, int column, Object value, ItemDataRole role) Sets data at the given row and column.booleansetData(int row, int column, Object value, ItemDataRole role, WModelIndex parent) Sets data at the given row and column.final booleansetData(WModelIndex index, Object value) Sets data at the given model index.booleansetData(WModelIndex index, Object value, ItemDataRole role) Sets data at the given model index.final booleansetHeaderData(int section, Orientation orientation, Object value) Sets header data for a column or row.booleansetHeaderData(int section, Orientation orientation, Object value, ItemDataRole role) Sets header data for a column or row.booleansetHeaderData(int section, Object value) Sets column header data.booleansetItemData(WModelIndex index, SortedMap<ItemDataRole, Object> values) Sets data at the given model index.final voidsort(int column) Sorts the model according to a particular column.voidSorts the model according to a particular column.toRawIndex(WModelIndex index) Converts a model index to a raw pointer that remains valid while the model's layout is changed.Methods inherited from class eu.webtoolkit.jwt.WObject
getId, getObjectName, remove, resendFormData, setFormData, setObjectName, tr
-
Constructor Details
-
WAbstractItemModel
public WAbstractItemModel()Creates a new data model.
-
-
Method Details
-
getColumnCount
Returns the number of columns.This returns the number of columns at index
parent.- See Also:
-
getColumnCount
public final int getColumnCount()Returns the number of columns.Returns
getColumnCount(null) -
getRowCount
Returns the number of rows.This returns the number of rows at index
parent.- See Also:
-
getRowCount
public final int getRowCount()Returns the number of rows.Returns
getRowCount(null) -
getFlags
Returns the flags for an item.The default implementation returns
ItemFlag.Selectable.- See Also:
-
getHeaderFlags
Returns the flags for a header.The default implementation returns no flags set.
- See Also:
-
getHeaderFlags
Returns the flags for a header. -
hasChildren
Returns if there are children at an index.Returns
truewhen rowCount(index) > 0 and columnCount(index) > 0.- See Also:
-
getParent
Returns the parent for a model index.An implementation should use
createIndex()to create a model index that corresponds to the parent of a given index.Note that the index itself may be stale (referencing a row/column within the parent that is outside the model geometry), but its parent (identified by the
WModelIndex.getInternalPointer()) is referencing an existing parent. A stale index can only be used while the model geometry is being updated, i.e. during the emission of the corresponding [rows/columns](Being)[Removed/Inserted]() signals.- See Also:
-
getData
Returns data at a specified model index for the given role.You should check the
roleto decide what data to return. Usually a View class will ask for data for several roles which affect not only the contents (ItemDataRole.Display) but also icons (ItemDataRole.Decoration), URLs (ItemDataRole.Link), and other visual aspects. If your item does not specify data for a particular role, it should simply return a Wt::cpp17::any().- See Also:
-
getData
Returns data at a specified model index for the given role. -
getItemData
Returns all data at a specific index.This is a convenience function that returns a map with data corresponding to all standard roles.
- See Also:
-
getHeaderData
Returns the row or column header data.When
orientationisOrientation.Horizontal,sectionis a column number, whenorientationisOrientation.Vertical,sectionis a row number.- See Also:
-
getHeaderData
Returns the row or column header data.Returns
getHeaderData(section, Orientation.Horizontal, ItemDataRole.Display) -
getHeaderData
Returns the row or column header data.Returns
getHeaderData(section, orientation, ItemDataRole.Display) -
getIndex
Returns the child index for the given row and column.When implementing this method, you can use
createIndex()to create an index that corresponds to the item atrowandcolumnwithinparent.If the location is invalid (out of bounds at the parent), then an invalid index must be returned.
- See Also:
-
getIndex
Returns the child index for the given row and column.Returns
getIndex(row, column, null) -
match
public List<WModelIndex> match(WModelIndex start, ItemDataRole role, Object value, int hits, MatchOptions flags) Returns an index list for data items that match.Returns an index list of data items that match, starting at start, and searching further in that column. If flags specifies
MatchFlag#Wrapthen the search wraps around from the start. If hits is not -1, then at most that number of hits are returned. -
getData
Returns the data item at the given column and row.This is a convenience method, and is equivalent to:
index(row, column, parent).data(role)- See Also:
-
getData
Returns the data item at the given column and row. -
getData
Returns the data item at the given column and row.Returns
getData(row, column, role, null) -
hasIndex
Returns if an index at the given position is valid (i.e. falls within the column-row bounds).Equivalent to:
return row >= 0 && column >= 0 && row < rowCount(parent) && column < columnCount(parent);- See Also:
-
hasIndex
public final boolean hasIndex(int row, int column) Returns if an index at the given position is valid (i.e. falls within the column-row bounds).Returns
hasIndex(row, column, null) -
insertColumns
Inserts one or more columns.In models that support column insertion, this inserts
countcolumns, starting atcolumn, and returnstrueif the operation was successful. The new columns are inserted underparent.The default implementation returns
false.The model implementation must call
beginInsertColumns()andendInsertColumns()before and after the operation whenever its geometry is changed by inserting columns. This emits signals for views to properly react to these changes.- See Also:
-
insertColumns
public final boolean insertColumns(int column, int count) Inserts one or more columns. -
insertRows
Inserts one or more rows.In models that support row insertion, this inserts
countrows, starting atrow, and returnstrueif the operation was successful. The new rows are inserted underparent.If parent had no children, then a single column is added with
countrows.The default implementation returns
false.The model implementation must call
beginInsertRows()andendInsertRows()before and after the operation whenever its geometry is changed by inserting rows. This emits signals for views to properly react to these changes.- See Also:
-
insertRows
public final boolean insertRows(int column, int count) Inserts one or more rows.Returns
insertRows(column, count, null) -
removeColumns
Removes columns.Returns
trueif the operation was successful.The default implementation returns
false.The model implementation must call
beginRemoveColumns()andendRemoveColumns()before and after the operation whenever its geometry is changed by removing columns. This emits signals for views to properly react to these changes.- See Also:
-
removeColumns
public final boolean removeColumns(int column, int count) Removes columns. -
removeRows
Removes rows.Returns
trueif the operation was successful.The default implementation returns
false.The model implementation must call
beginRemoveRows()andendRemoveRows()before and after the operation whenever its geometry is changed by removing rows. This emits signals for views to properly react to these changes.- See Also:
-
removeRows
public final boolean removeRows(int column, int count) Removes rows.Returns
removeRows(column, count, null) -
setData
Sets data at the given model index.Returns
trueif the operation was successful.The default implementation returns
false.The model implementation must emit the
dataChanged()signal after data was changed.- See Also:
-
setData
Sets data at the given model index. -
setItemData
Sets data at the given model index.This is a convenience function that sets data for all roles at once.
- See Also:
-
setHeaderData
Sets header data for a column or row.Returns
trueif the operation was successful.- See Also:
-
setHeaderData
Sets header data for a column or row.Returns
setHeaderData(section, orientation, value, ItemDataRole.Edit) -
setHeaderData
Sets column header data.Returns
trueif the operation was successful. -
sort
Sorts the model according to a particular column.If the model supports sorting, then it should emit the
layoutAboutToBeChanged()signal, rearrange its items, and afterwards emit thelayoutChanged()signal.- See Also:
-
sort
public final void sort(int column) Sorts the model according to a particular column. -
expandColumn
public void expandColumn(int column) Expands a column.Expands a column. This may only be called by a view when the
HeaderFlag.ColumnIsCollapsedflag is set.The default implementation does nothing.
- See Also:
-
collapseColumn
public void collapseColumn(int column) Collapses a column.Collapses a column. This may only be called by a view when the
HeaderFlag.ColumnIsExpandedLeftorHeaderFlag.ColumnIsExpandedRightflag is set.The default implementation does nothing.
- See Also:
-
toRawIndex
Converts a model index to a raw pointer that remains valid while the model's layout is changed.Use this method to temporarily save model indexes while the model's layout is changed by for example a sorting operation.
The default implementation returns
null, which indicates that the index cannot be converted to a raw pointer. If you reimplement this method, you also need to reimplemntfromRawIndex().- See Also:
-
fromRawIndex
Converts a raw pointer to a model index.Use this method to create model index from temporary raw pointers. It is the reciproce method of
toRawIndex().You can return an invalid modelindex if the rawIndex no longer points to a valid item because of the layout change.
- See Also:
-
getMimeType
Returns a mime-type for dragging a set of indexes.This method returns a mime-type that describes dragging of a selection of items.
The drop event will indicate a
selection modelfor this abstract item model asWDropEvent.getSource().The default implementation returns a mime-type for generic drag&drop support between abstract item models.
- See Also:
-
getAcceptDropMimeTypes
Returns a list of mime-types that could be accepted for a drop event.The default implementation only accepts drag&drop support between abstract item models.
- See Also:
-
dropEvent
Handles a drop event.The default implementation only handles generic drag&drop between abstract item models. Source item data is copied (but not the source item's flags).
This method is overloaded for handling drop events on top of items or drop events between items (see
DropLocation). This overload handles drops on top of items, but note that due to historical reasons it will also insert the items in between when called withDropAction.Move.The location in the model is indicated by the
rowandcolumnwithin theparentindex. Ifrowis -1, then the item is appended to theparent. Otherwise, the item is inserted at or copied over the indicated item (and subsequent rows). Whenactionis aDropAction.Move, the original items are deleted from the source model.You may want to reimplement this method if you want to handle other mime-type data, or if you want to refine how the drop event of an item selection must be interpreted.
Note: Currently, only row selections are handled by the default implementation.
- See Also:
-
dropEvent
Handles a drop event.The default implementation only handles generic drag&drop between abstract item models. Source item data is copied (but not the source item's flags).
This method is overloaded for handling drop events on top of items or drop events between items. This overload handles drops between items. The drop was received relative to the
indexitem and thesideparameter will only be Wt::Top or Wt::Bottom.You may want to reimplement this method if you want to handle other mime-type data, or if you want to refine how the drop event of an item selection must be interpreted.
Note: Currently, only row selections are handled by the default implementation.
- See Also:
-
insertColumn
Inserts one column.This is a convenience method that adds a single column, and is equivalent to:
insertColumns(column, 1, parent);Returns
trueif the operation was successful.- See Also:
-
insertColumn
public final boolean insertColumn(int column) Inserts one column.Returns
insertColumn(column, null) -
insertRow
Inserts one row.This is a convenience method that adds a single row, and is equivalent to:
insertRows(row, 1, parent);Returns
trueif the operation was successful.- See Also:
-
insertRow
public final boolean insertRow(int row) Inserts one row.Returns
insertRow(row, null) -
removeColumn
Removes one column.This is a convenience method that removes a single column, and is equivalent to:
removeColumns(column, 1, parent);Returns
trueif the operation was successful.- See Also:
-
removeColumn
public final boolean removeColumn(int column) Removes one column.Returns
removeColumn(column, null) -
removeRow
Removes one row.This is a convenience method that removes a single row, and is equivalent to:
removeRows(row, 1, parent);Returns
trueif the operation was successful.- See Also:
-
removeRow
public final boolean removeRow(int row) Removes one row.Returns
removeRow(row, null) -
setData
Sets data at the given row and column.This is a convience method, and is equivalent to:
setData(index(row, column, parent), value, role);Returns
trueif the operation was successful.- See Also:
-
setData
Sets data at the given row and column.Returns
setData(row, column, value, ItemDataRole.Edit, null) -
setData
Sets data at the given row and column. -
columnsAboutToBeInserted
Signal emitted before a number of columns will be inserted.The first argument is the parent index. The two integer arguments are the column numbers that the first and last column will have when inserted.
- See Also:
-
columnsAboutToBeRemoved
Signal emitted before a number of columns will be removed.The first argument is the parent index. The two integer arguments are the column numbers of the first and last column that will be removed.
- See Also:
-
columnsInserted
Signal emitted after a number of columns were inserted.The first argument is the parent index. The two integer arguments are the column numbers of the first and last column that were inserted.
- See Also:
-
columnsRemoved
Signal emitted after a number of columns were removed.The first argument is the parent index. The two integer arguments are the column numbers of the first and last column that were removed.
- See Also:
-
rowsAboutToBeInserted
Signal emitted before a number of rows will be inserted.The first argument is the parent index. The two integer arguments are the row numbers that the first and last row will have when inserted.
- See Also:
-
rowsAboutToBeRemoved
Signal emitted before a number of rows will be removed.The first argument is the parent index. The two integer arguments are the row numbers of the first and last row that will be removed.
- See Also:
-
rowsInserted
Signal emitted after a number of rows were inserted.The first argument is the parent index. The two integer arguments are the row numbers of the first and last row that were inserted.
- See Also:
-
rowsRemoved
Signal emitted after a number of rows were removed.The first argument is the parent index. The two integer arguments are the row numbers of the first and last row that were removed.
- See Also:
-
dataChanged
Signal emitted when some data was changed.The two arguments are the model indexes of the top-left and bottom-right data items that span the rectangle of changed data items.
- See Also:
-
headerDataChanged
Signal emitted when some header data was changed.The first argument indicates the orientation of the header, and the two integer arguments are the row or column numbers of the first and last header item of which the value was changed.
- See Also:
-
layoutAboutToBeChanged
Signal emitted when the layout is about to be changed.A layout change may reorder or add/remove rows in the model, but columns are preserved. Model indexes are invalidated by a layout change, but indexes may be ported across a layout change by using the
toRawIndex()andfromRawIndex()methods.- See Also:
-
layoutChanged
Signal emitted when the layout is changed.- See Also:
-
modelReset
Signal emitted when the model was reset.A model reset invalidates all existing data, and the model may change its entire geometry (column count, row count).
- See Also:
-
reset
protected void reset()Resets the model and invalidate any data.Informs any attached view that all data in the model was invalidated, and the model's data should be reread.
This causes the
modelReset()signal to be emitted. -
createIndex
Creates a model index for the given row and column.Use this method to create a model index.
ptris an internal pointer that may be used to identify the parent of the corresponding item. For a flat table model,ptrcan thus always be 0.- See Also:
-
createIndex
Creates a model index for the given row and column.Use this method to create a model index.
idis an internal id that may be used to identify the parent of the corresponding item. For a flat table model,ptrcan thus always be 0.- See Also:
-
beginInsertColumns
Method to be called before inserting columns.If your model supports insertion of columns, then you should call this method before inserting one or more columns, and
endInsertColumns()afterwards. These methods emit the necessary signals to allow view classes to update themselves.- See Also:
-
beginInsertRows
Method to be called before inserting rows.If your model supports insertion of rows, then you should call this method before inserting one or more rows, and
endInsertRows()afterwards. These methods emit the necessary signals to allow view classes to update themselves.- See Also:
-
beginRemoveColumns
Method to be called before removing columns.If your model supports removal of columns, then you should call this method before removing one or more columns, and
endRemoveColumns()afterwards. These methods emit the necessary signals to allow view classes to update themselves.- See Also:
-
beginRemoveRows
Method to be called before removing rows.If your model supports removal of rows, then you should call this method before removing one or more rows, and
endRemoveRows()afterwards. These methods emit the necessary signals to allow view classes to update themselves.- See Also:
-
endInsertColumns
protected void endInsertColumns()Method to be called after inserting columns.- See Also:
-
endInsertRows
protected void endInsertRows()Method to be called after inserting rows.- See Also:
-
endRemoveColumns
protected void endRemoveColumns()Method to be called after removing columns.- See Also:
-
endRemoveRows
protected void endRemoveRows()Method to be called after removing rows.- See Also:
-
copyData
Copy data to an index in this model.The source index can be any valid index. The destination index must be part of this model.
-