Class WSortFilterProxyModel
This proxy model does not store data itself, but presents data from a source model, after
filtering rows. It also allows sorting of the source model data, without actually altering the
source model. This may be convenient when the source model does not support sorting (i.e. does
not reimplement WAbstractItemModel#sort()
), or you do not want to reorder the underlying model since that
affects all views on the model.
To use the proxy model to filter data, you use the methods setFilterKeyColumn()
, setFilterRegExp()
and setFilterRole()
to specify a filtering
operation based on the values of a single column. If this filtering mechanism is too limiting,
you can provide specialized filtering by reimplementing the filterAcceptRow()
method.
Sorting is provided by reimplementing the standard WAbstractItemModel#sort()
method. In this way, a view class such as WTreeView
may resort the model as indicated by the user. Use setSortRole()
to indicate on what data role
sorting should be done, or reimplement the lessThan() method to provide a specialized sorting
method.
By default, the proxy does not automatically refilter and resort when the original model
changes. Data changes or row additions to the source model are not automatically reflected in the
proxy model, but to maintain integrity, row removals in the source model are always reflected in
the proxy model. You can enable the model to always refilter and resort when the underlying model
changes using setDynamicSortFilter()
.
Usage example:
// model is the source model
WAbstractItemModel model = ...
// we setup a proxy to filter the source model
WSortFilterProxyModel proxy = new WSortFilterProxyModel(this);
proxy.setSourceModel(model);
proxy.setDynamicSortFilter(true);
proxy.setFilterKeyColumn(0);
proxy.setFilterRole(ItemDataRole.User);
proxy.setFilterRegExp(".*");
// configure a view to use the proxy model instead of the source model
WTreeView view = new WTreeView(this);
view.setModel(proxy);
...
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WAbstractProxyModel
WAbstractProxyModel.BaseItem
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected int
compare
(WModelIndex lhs, WModelIndex rhs) Compares two indexes.protected boolean
filterAcceptRow
(int sourceRow, WModelIndex sourceParent) Returns whether a source row is accepted by the filter.int
getColumnCount
(WModelIndex parent) Returns the number of columns.int
Return the column on which the filtering is applied.Return the regular expression used for filtering.Return the data role used for filtering.getHeaderData
(int section, Orientation orientation, ItemDataRole role) Returns the row or column header data.getHeaderFlags
(int section, Orientation orientation) Returns the flags for a header.getIndex
(int row, int column, WModelIndex parent) Returns the child index for the given row and column.getParent
(WModelIndex index) Returns the parent for a model index.int
getRowCount
(WModelIndex parent) Returns the number of rows.int
Returns the current sort column.Returns the current sort order.Return the data role used for sorting.boolean
insertRows
(int row, int count, WModelIndex parent) Inserts a number rows.void
Invalidates the current filter.boolean
Returns whether this proxy dynmically filters and sorts.mapFromSource
(WModelIndex sourceIndex) Maps a source model index to the proxy model.mapToSource
(WModelIndex proxyIndex) Maps a proxy model index to the source model.boolean
removeRows
(int row, int count, WModelIndex parent) Removes a number rows.void
setDynamicSortFilter
(boolean enable) Configure the proxy to dynamically track changes in the source model.void
setFilterKeyColumn
(int column) Specify the column on which the filtering is applied.void
setFilterRegExp
(Pattern pattern) Specify a regular expression for filtering.void
setFilterRole
(ItemDataRole role) Specify the data role used for filtering.boolean
setHeaderData
(int section, Orientation orientation, Object value, ItemDataRole role) Sets header data for a column or row.void
setSortRole
(ItemDataRole role) Specify the data role used used for sorting.void
setSourceModel
(WAbstractItemModel model) Sets the source model.void
Sorts the model according to a particular column.Methods inherited from class eu.webtoolkit.jwt.WAbstractProxyModel
createSourceIndex, dropEvent, endShiftModelIndexes, fromRawIndex, getAcceptDropMimeTypes, getData, getFlags, getMimeType, getSourceModel, insertColumns, removeColumns, setData, setItemData, startShiftModelIndexes, toRawIndex
Methods inherited from class eu.webtoolkit.jwt.WAbstractItemModel
beginInsertColumns, beginInsertRows, beginRemoveColumns, beginRemoveRows, collapseColumn, columnsAboutToBeInserted, columnsAboutToBeRemoved, columnsInserted, columnsRemoved, copyData, createIndex, createIndex, dataChanged, dropEvent, endInsertColumns, endInsertRows, endRemoveColumns, endRemoveRows, expandColumn, getColumnCount, getData, getData, getData, getData, getHeaderData, getHeaderData, getHeaderFlags, getIndex, getItemData, getRowCount, hasChildren, hasIndex, hasIndex, headerDataChanged, insertColumn, insertColumn, insertColumns, insertRow, insertRow, insertRows, layoutAboutToBeChanged, layoutChanged, match, modelReset, removeColumn, removeColumn, removeColumns, removeRow, removeRow, removeRows, reset, rowsAboutToBeInserted, rowsAboutToBeRemoved, rowsInserted, rowsRemoved, setData, setData, setData, setData, setHeaderData, setHeaderData, sort
Methods inherited from class eu.webtoolkit.jwt.WObject
getId, getObjectName, remove, setFormData, setObjectName, tr
-
Constructor Details
-
WSortFilterProxyModel
public WSortFilterProxyModel()Constructor.
-
-
Method Details
-
mapFromSource
Description copied from class:WAbstractProxyModel
Maps a source model index to the proxy model.This method returns a model index in the proxy model that corresponds to the model index
sourceIndex
in the source model. This method must only be implemented for source model indexes that are mapped and thus are the result ofmapToSource()
.- Specified by:
mapFromSource
in classWAbstractProxyModel
- See Also:
-
mapToSource
Description copied from class:WAbstractProxyModel
Maps a proxy model index to the source model.This method returns a model index in the source model that corresponds to the proxy model index
proxyIndex
.- Specified by:
mapToSource
in classWAbstractProxyModel
- See Also:
-
setSourceModel
Sets the source model.The source model provides the actual data for the proxy model.
Ownership of the source model is not transferred.
All signals of the source model are forwarded to the proxy model.
- Overrides:
setSourceModel
in classWAbstractProxyModel
-
setFilterKeyColumn
public void setFilterKeyColumn(int column) Specify the column on which the filtering is applied.This configures the column on which the
getFilterRegExp()
is applied.The default value is 0.
-
getFilterKeyColumn
public int getFilterKeyColumn()Return the column on which the filtering is applied.- See Also:
-
setFilterRegExp
Specify a regular expression for filtering.This configures the regular expression used for filtering on
getFilterKeyColumn()
.The default value is an empty expression, which disables filtering.
-
getFilterRegExp
Return the regular expression used for filtering.- See Also:
-
setFilterRole
Specify the data role used for filtering.This configures the data role used for filtering on
getFilterKeyColumn()
.The default value is
ItemDataRole.Display
. -
getFilterRole
Return the data role used for filtering.- See Also:
-
setSortRole
Specify the data role used used for sorting.This configures the data role used for sorting.
The default value is
ItemDataRole.Display
. -
getSortRole
Return the data role used for sorting.- See Also:
-
getSortColumn
public int getSortColumn()Returns the current sort column.When
sort()
has not been called, the model is unsorted, and this method returns -1.- See Also:
-
getSortOrder
Returns the current sort order.- See Also:
-
setDynamicSortFilter
public void setDynamicSortFilter(boolean enable) Configure the proxy to dynamically track changes in the source model.When
enable
istrue
, the proxy will re-filter and re-sort the model when changes happen to the source model.Note: This may be ackward when editing through the proxy model, since changing some data may rearrange the model and thus invalidate model indexes. Therefore it is usually less complicated to manipulate directly the source model instead.
-
isDynamicSortFilter
public boolean isDynamicSortFilter()Returns whether this proxy dynmically filters and sorts.- See Also:
-
invalidate
public void invalidate()Invalidates the current filter.This refilters and resorts the model, and is useful only if you have reimplemented
filterAcceptRow()
and/or lessThan() -
getColumnCount
Description copied from class:WAbstractItemModel
Returns the number of columns.This returns the number of columns at index
parent
.- Specified by:
getColumnCount
in classWAbstractItemModel
- See Also:
-
getRowCount
Description copied from class:WAbstractItemModel
Returns the number of rows.This returns the number of rows at index
parent
.- Specified by:
getRowCount
in classWAbstractItemModel
- See Also:
-
getParent
Description copied from class:WAbstractItemModel
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.- Specified by:
getParent
in classWAbstractItemModel
- See Also:
-
getIndex
Description copied from class:WAbstractItemModel
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.
- Specified by:
getIndex
in classWAbstractItemModel
- See Also:
-
setHeaderData
Description copied from class:WAbstractItemModel
Sets header data for a column or row.Returns
true
if the operation was successful.- Overrides:
setHeaderData
in classWAbstractItemModel
- See Also:
-
getHeaderData
Description copied from class:WAbstractProxyModel
Returns the row or column header data.The default proxy implementation constructs a dummy
WModelIndex
with the row set to 0 and column set tosection
if the orientation isOrientation.Horizontal
, or with the row set tosection
and the column set to 0 if the orientation isOrientation.Vertical
.The resulting section that is forwarded to
getSourceModel()
.getHeaderData()
depends on how theWModelIndex
is transformed withmapToSource()
. -
getHeaderFlags
Description copied from class:WAbstractProxyModel
Returns the flags for a header.The default proxy implementation constructs a dummy
WModelIndex
with the row set to 0 and column set tosection
if the orientation isOrientation.Horizontal
, or with the row set tosection
and the column set to 0 if the orientation isOrientation.Vertical
.The resulting section that is forwarded to
getSourceModel()
.getHeaderFlags()
depends on how theWModelIndex
is transformed withmapToSource()
.- Overrides:
getHeaderFlags
in classWAbstractProxyModel
- See Also:
-
insertRows
Inserts a number rows.The rows are inserted in the source model, and if successful, also in the proxy model regardless of whether they are matched by the current filter. They are inserted at the indicated row, regardless of whether this is the correct place according to the defined sorting.
As soon as you set data for the column on which the filtering is active, or which affects the sorting, the row may be filtered out or change position when dynamic sorting/filtering is enabled. Therefore, it is usually a good idea to temporarily disable the dynamic sort/filtering behaviour while inserting new row(s) of data.
- Overrides:
insertRows
in classWAbstractItemModel
- See Also:
-
removeRows
Removes a number rows.The rows are removed from the source model.
- Overrides:
removeRows
in classWAbstractItemModel
- See Also:
-
sort
Description copied from class:WAbstractItemModel
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.- Overrides:
sort
in classWAbstractItemModel
- See Also:
-
filterAcceptRow
Returns whether a source row is accepted by the filter.The default implementation uses
getFilterKeyColumn()
,getFilterRole()
andgetFilterRegExp()
.You may want to reimplement this method to provide specialized filtering.
-
compare
Compares two indexes.The default implementation uses
getSortRole()
and an ordering using the operator< when the data is of the same type or compares lexicographically otherwise.You may want to reimplement this method to provide specialized sorting.
-