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
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
beginInsertColumns
(WModelIndex parent, int first, int last) Method to be called before inserting columns.protected void
beginInsertRows
(WModelIndex parent, int first, int last) Method to be called before inserting rows.protected void
beginRemoveColumns
(WModelIndex parent, int first, int last) Method to be called before removing columns.protected void
beginRemoveRows
(WModelIndex parent, int first, int last) Method to be called before removing rows.void
collapseColumn
(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 void
copyData
(WModelIndex sIndex, WModelIndex dIndex) Copy data to an index in this model.protected WModelIndex
createIndex
(int row, int column, long id) Creates a model index for the given row and column.protected WModelIndex
createIndex
(int row, int column, Object ptr) Creates a model index for the given row and column.Signal emitted when some data was changed.void
dropEvent
(WDropEvent e, DropAction action, int row, int column, WModelIndex parent) Handles a drop event.void
dropEvent
(WDropEvent e, DropAction action, WModelIndex pindex, Side side) Handles a drop event.protected void
Method to be called after inserting columns.protected void
Method to be called after inserting rows.protected void
Method to be called after removing columns.protected void
Method to be called after removing rows.void
expandColumn
(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 int
Returns the number of columns.abstract int
getColumnCount
(WModelIndex parent) Returns the number of columns.final Object
getData
(int row, int column) Returns the data item at the given column and row.final Object
getData
(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 Object
getData
(WModelIndex index) Returns data at a specified model index for the given role.abstract Object
getData
(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 Object
getHeaderData
(int section) Returns the row or column header data.final Object
getHeaderData
(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 WModelIndex
getIndex
(int row, int column) Returns the child index for the given row and column.abstract WModelIndex
getIndex
(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 WModelIndex
getParent
(WModelIndex index) Returns the parent for a model index.final int
Returns the number of rows.abstract int
getRowCount
(WModelIndex parent) Returns the number of rows.boolean
hasChildren
(WModelIndex index) Returns if there are children at an index.final boolean
hasIndex
(int row, int column) Returns if an index at the given position is valid (i.e.boolean
hasIndex
(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 boolean
insertColumn
(int column) Inserts one column.boolean
insertColumn
(int column, WModelIndex parent) Inserts one column.final boolean
insertColumns
(int column, int count) Inserts one or more columns.boolean
insertColumns
(int column, int count, WModelIndex parent) Inserts one or more columns.final boolean
insertRow
(int row) Inserts one row.boolean
insertRow
(int row, WModelIndex parent) Inserts one row.final boolean
insertRows
(int row, int count) Inserts one or more rows.boolean
insertRows
(int row, 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 boolean
removeColumn
(int column) Removes one column.boolean
removeColumn
(int column, WModelIndex parent) Removes one column.final boolean
removeColumns
(int column, int count) Removes columns.boolean
removeColumns
(int column, int count, WModelIndex parent) Removes columns.final boolean
removeRow
(int row) Removes one row.boolean
removeRow
(int row, WModelIndex parent) Removes one row.final boolean
removeRows
(int row, int count) Removes rows.boolean
removeRows
(int row, int count, WModelIndex parent) Removes rows.protected void
reset()
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 boolean
Sets data at the given row and column.final boolean
setData
(int row, int column, Object value, ItemDataRole role) Sets data at the given row and column.boolean
setData
(int row, int column, Object value, ItemDataRole role, WModelIndex parent) Sets data at the given row and column.final boolean
setData
(WModelIndex index, Object value) Sets data at the given model index.boolean
setData
(WModelIndex index, Object value, ItemDataRole role) Sets data at the given model index.final boolean
setHeaderData
(int section, Orientation orientation, Object value) Sets header data for a column or row.boolean
setHeaderData
(int section, Orientation orientation, Object value, ItemDataRole role) Sets header data for a column or row.boolean
setHeaderData
(int section, Object value) Sets column header data.boolean
setItemData
(WModelIndex index, SortedMap<ItemDataRole, Object> values) Sets data at the given model index.final void
sort
(int column) Sorts the model according to a particular column.void
Sorts 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, 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
true
when rowCount(index) > 0 and columnCount(index) > 0. -
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. -
getData
Returns data at a specified model index for the given role.You should check the
role
to 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(). -
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.
-
getHeaderData
Returns the row or column header data.When
orientation
isOrientation.Horizontal
,section
is a column number, whenorientation
isOrientation.Vertical
,section
is a row number. -
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 atrow
andcolumn
withinparent
.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#Wrap
then 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)
-
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);
-
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
count
columns, starting atcolumn
, and returnstrue
if 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. -
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
count
rows, starting atrow
, and returnstrue
if the operation was successful. The new rows are inserted underparent
.If parent had no children, then a single column is added with
count
rows.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. -
insertRows
public final boolean insertRows(int row, int count) Inserts one or more rows.Returns
insertRows(row, count, null)
-
removeColumns
Removes columns.Returns
true
if 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. -
removeColumns
public final boolean removeColumns(int column, int count) Removes columns. -
removeRows
Removes rows.Returns
true
if 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. -
removeRows
public final boolean removeRows(int row, int count) Removes rows.Returns
removeRows(row, count, null)
-
setData
Sets data at the given model index.Returns
true
if the operation was successful.The default implementation returns
false
.The model implementation must emit the
dataChanged()
signal after data was changed. -
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.
-
setHeaderData
Sets header data for a column or row.Returns
true
if the operation was successful. -
setHeaderData
Sets header data for a column or row.Returns
setHeaderData(section, orientation, value, ItemDataRole.Edit)
-
setHeaderData
Sets column header data.Returns
true
if 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.ColumnIsCollapsed
flag 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.ColumnIsExpandedLeft
orHeaderFlag.ColumnIsExpandedRight
flag 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()
. -
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 model
for 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
row
andcolumn
within theparent
index. Ifrow
is -1, then the item is appended to theparent
. Otherwise, the item is inserted at or copied over the indicated item (and subsequent rows). Whenaction
is 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
index
item and theside
parameter 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
true
if the operation was successful. -
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
true
if the operation was successful. -
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
true
if the operation was successful. -
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
true
if the operation was successful. -
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
true
if the operation was successful. -
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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. -
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.
ptr
is an internal pointer that may be used to identify the parent of the corresponding item. For a flat table model,ptr
can 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.
id
is an internal id that may be used to identify the parent of the corresponding item. For a flat table model,ptr
can 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. -
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. -
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. -
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. -
endInsertColumns
protected void endInsertColumns()Method to be called after inserting columns. -
endInsertRows
protected void endInsertRows()Method to be called after inserting rows. -
endRemoveColumns
protected void endRemoveColumns()Method to be called after removing columns. -
endRemoveRows
protected void endRemoveRows()Method to be called after removing rows. -
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.
-