Wt  3.3.9
Public Member Functions | Protected Member Functions | List of all members
Wt::WSortFilterProxyModel Class Reference

A proxy model for Wt's item models that provides filtering and/or sorting. More...

#include <Wt/WSortFilterProxyModel>

Inheritance diagram for Wt::WSortFilterProxyModel:
Inheritance graph
[legend]

Public Member Functions

 WSortFilterProxyModel (WObject *parent=0)
 Constructor.
 
virtual ~WSortFilterProxyModel ()
 Destructor.
 
virtual WModelIndex mapFromSource (const WModelIndex &sourceIndex) const
 Maps a source model index to the proxy model. More...
 
virtual WModelIndex mapToSource (const WModelIndex &proxyIndex) const
 Maps a proxy model index to the source model. More...
 
virtual void setSourceModel (WAbstractItemModel *sourceModel)
 Sets the source model. More...
 
void setFilterKeyColumn (int column)
 Specify the column on which the filtering is applied. More...
 
int filterKeyColumn () const
 Return the column on which the filtering is applied. More...
 
void setFilterRegExp (const WString &pattern)
 Specify a regular expression for filtering. More...
 
WString filterRegExp () const
 Return the regular expression used for filtering. More...
 
void setFilterFlags (WFlags< RegExpFlag > flags)
 Sets the filter regular expression flags.
 
WFlags< RegExpFlagfilterFlags () const
 Returns the filter regular expression flags.
 
void setFilterRole (int role)
 Specify the data role used for filtering. More...
 
int filterRole () const
 Return the data role used for filtering. More...
 
void setSortRole (int role)
 Specify the data role used used for sorting. More...
 
int sortRole () const
 Return the data role used for sorting. More...
 
int sortColumn () const
 Returns the current sort column. More...
 
SortOrder sortOrder () const
 Returns the current sort order. More...
 
void setDynamicSortFilter (bool enable)
 Configure the proxy to dynamically track changes in the source model. More...
 
bool dynamicSortFilter () const
 Returns whether this proxy dynmically filters and sorts. More...
 
void invalidate ()
 Invalidates the current filter. More...
 
virtual int columnCount (const WModelIndex &parent=WModelIndex()) const
 Returns the number of columns. More...
 
virtual int rowCount (const WModelIndex &parent=WModelIndex()) const
 Returns the number of rows. More...
 
virtual WModelIndex parent (const WModelIndex &index) const
 Returns the parent for a model index. More...
 
virtual WModelIndex index (int row, int column, const WModelIndex &parent=WModelIndex()) const
 Returns the child index for the given row and column. More...
 
virtual bool setHeaderData (int section, Orientation orientation, const boost::any &value, int role=EditRole)
 Sets header data for a column or row. More...
 
virtual boost::any headerData (int section, Orientation orientation=Horizontal, int role=DisplayRole) const
 Returns the row or column header data. More...
 
virtual WFlags< HeaderFlagheaderFlags (int section, Orientation orientation=Horizontal) const
 Returns the flags for a header. More...
 
virtual bool insertRows (int row, int count, const WModelIndex &parent=WModelIndex())
 Inserts a number rows. More...
 
virtual bool removeRows (int row, int count, const WModelIndex &parent=WModelIndex())
 Removes a number rows. More...
 
virtual void sort (int column, SortOrder order=AscendingOrder)
 Sorts the model according to a particular column. More...
 
- Public Member Functions inherited from Wt::WAbstractProxyModel
 WAbstractProxyModel (WObject *parent=0)
 Constructor.
 
WAbstractItemModelsourceModel () const
 Returns the source model. More...
 
virtual boost::any data (const WModelIndex &index, int role=DisplayRole) const
 Returns the data at a specific model index. More...
 
virtual bool setData (const WModelIndex &index, const boost::any &value, int role=EditRole)
 Sets the data at the given model index. More...
 
virtual bool setItemData (const WModelIndex &index, const DataMap &values)
 Sets the data at the given model index. More...
 
virtual WFlags< ItemFlagflags (const WModelIndex &index) const
 Returns the flags for an item. More...
 
virtual bool insertColumns (int column, int count, const WModelIndex &parent=WModelIndex())
 Inserts one or more columns. More...
 
virtual bool removeColumns (int column, int count, const WModelIndex &parent=WModelIndex())
 Removes columns. More...
 
virtual std::string mimeType () const
 Returns a mime-type for dragging a set of indexes. More...
 
virtual std::vector< std::string > acceptDropMimeTypes () const
 Returns a list of mime-types that could be accepted for a drop event. More...
 
virtual void dropEvent (const WDropEvent &e, DropAction action, int row, int column, const WModelIndex &parent)
 Handles a drop event. More...
 
virtual void * toRawIndex (const WModelIndex &index) const
 Converts a model index to a raw pointer that remains valid while the model's layout is changed. More...
 
virtual WModelIndex fromRawIndex (void *rawIndex) const
 Converts a raw pointer to a model index. More...
 
- Public Member Functions inherited from Wt::WAbstractItemModel
 WAbstractItemModel (WObject *parent=0)
 Creates a new data model.
 
virtual bool hasChildren (const WModelIndex &index) const
 Returns if there are children at an index. More...
 
virtual DataMap itemData (const WModelIndex &index) const
 Returns all data at a specific index. More...
 
virtual WModelIndexList match (const WModelIndex &start, int role, const boost::any &value, int hits=-1, WFlags< MatchFlag > flags=WFlags< MatchFlag >(MatchStartsWith|MatchWrap)) const
 Returns an index list for data items that match. More...
 
boost::any data (int row, int column, int role=DisplayRole, const WModelIndex &parent=WModelIndex()) const
 Returns the data item at the given column and row. More...
 
virtual bool hasIndex (int row, int column, const WModelIndex &parent=WModelIndex()) const
 Returns if an index at the given position is valid (i.e. falls within the column-row bounds). More...
 
bool setHeaderData (int section, const boost::any &value)
 Sets column header data. More...
 
virtual void expandColumn (int column)
 Expands a column. More...
 
virtual void collapseColumn (int column)
 Collapses a column. More...
 
bool insertColumn (int column, const WModelIndex &parent=WModelIndex())
 Inserts one column. More...
 
bool insertRow (int row, const WModelIndex &parent=WModelIndex())
 Inserts one row. More...
 
bool removeColumn (int column, const WModelIndex &parent=WModelIndex())
 Removes one column. More...
 
bool removeRow (int row, const WModelIndex &parent=WModelIndex())
 Removes one row. More...
 
bool setData (int row, int column, const boost::any &value, int role=EditRole, const WModelIndex &parent=WModelIndex())
 Sets data at the given row and column. More...
 
virtual Signal< WModelIndex, int, int > & columnsAboutToBeInserted ()
 Signal emitted before a number of columns will be inserted. More...
 
virtual Signal< WModelIndex, int, int > & columnsAboutToBeRemoved ()
 Signal emitted before a number of columns will be removed. More...
 
virtual Signal< WModelIndex, int, int > & columnsInserted ()
 Signal emitted after a number of columns were inserted. More...
 
virtual Signal< WModelIndex, int, int > & columnsRemoved ()
 Signal emitted after a number of columns were removed. More...
 
virtual Signal< WModelIndex, int, int > & rowsAboutToBeInserted ()
 Signal emitted before a number of rows will be inserted. More...
 
virtual Signal< WModelIndex, int, int > & rowsAboutToBeRemoved ()
 Signal emitted before a number of rows will be removed. More...
 
virtual Signal< WModelIndex, int, int > & rowsInserted ()
 Signal emitted after a number of rows were inserted. More...
 
virtual Signal< WModelIndex, int, int > & rowsRemoved ()
 Signal emitted after a number of rows were removed. More...
 
virtual Signal< WModelIndex, WModelIndex > & dataChanged ()
 Signal emitted when some data was changed. More...
 
virtual Signal< Orientation, int, int > & headerDataChanged ()
 Signal emitted when some header data was changed. More...
 
virtual SignallayoutAboutToBeChanged ()
 Signal emitted when the layout is about to be changed. More...
 
virtual SignallayoutChanged ()
 Signal emitted when the layout is changed. More...
 
virtual SignalmodelReset ()
 Signal emitted when the model was reset. More...
 
- Public Member Functions inherited from Wt::WObject
 WObject (WObject *parent=0)
 Create a WObject with a given parent object. More...
 
virtual ~WObject ()
 Destructor. More...
 
virtual const std::string id () const
 Returns the (unique) identifier for this object. More...
 
virtual void setObjectName (const std::string &name)
 Sets an object name. More...
 
virtual std::string objectName () const
 Returns the object name. More...
 
void resetLearnedSlots ()
 Resets learned stateless slot implementations. More...
 
template<class T >
void resetLearnedSlot (void(T::*method)())
 Resets a learned stateless slot implementation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)())
 Declares a slot to be stateless and learn client-side behaviour on first invocation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 Declares a slot to be stateless and learn client-side behaviour in advance. More...
 
void isNotStateless ()
 Marks the current function as not stateless. More...
 
template<class T >
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 Provides a JavaScript implementation for a method. More...
 
void addChild (WObject *child)
 Adds a child object. More...
 
virtual void removeChild (WObject *child)
 Removes a child object. More...
 
const std::vector< WObject * > & children () const
 Returns the children.
 
WObjectparent () const
 Returns the parent object.
 

Protected Member Functions

virtual bool filterAcceptRow (int sourceRow, const WModelIndex &sourceParent) const
 Returns whether a source row is accepted by the filter. More...
 
virtual bool lessThan (const WModelIndex &lhs, const WModelIndex &rhs) const
 Compares two indexes. More...
 
- Protected Member Functions inherited from Wt::WAbstractProxyModel
WModelIndex createSourceIndex (int row, int column, void *ptr) const
 Create a source model index. More...
 
void startShiftModelIndexes (const WModelIndex &sourceParent, int start, int count, ItemMap &items)
 Utility methods to shift items in an item map. More...
 
- Protected Member Functions inherited from Wt::WAbstractItemModel
void reset ()
 Resets the model and invalidate any data. More...
 
WModelIndex createIndex (int row, int column, void *ptr) const
 Creates a model index for the given row and column. More...
 
WModelIndex createIndex (int row, int column,::uint64_t id) const
 Creates a model index for the given row and column. More...
 
void beginInsertColumns (const WModelIndex &parent, int first, int last)
 Method to be called before inserting columns. More...
 
void beginInsertRows (const WModelIndex &parent, int first, int last)
 Method to be called before inserting rows. More...
 
void beginRemoveColumns (const WModelIndex &parent, int first, int last)
 Method to be called before removing columns. More...
 
void beginRemoveRows (const WModelIndex &parent, int first, int last)
 Method to be called before removing rows. More...
 
void endInsertColumns ()
 Method to be called after inserting columns. More...
 
void endInsertRows ()
 Method to be called after inserting rows. More...
 
void endRemoveColumns ()
 Method to be called after removing columns. More...
 
void endRemoveRows ()
 Method to be called after removing rows. More...
 
- Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * getStateless (Method method)
 On-demand stateless slot implementation. More...
 

Additional Inherited Members

- Public Types inherited from Wt::WAbstractItemModel
typedef std::map< int, boost::any > DataMap
 Data map. More...
 
- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 Typedef for a WObject method without arguments.
 
- Protected Types inherited from Wt::WAbstractProxyModel
typedef std::map< WModelIndex, BaseItem * > ItemMap
 A map for items. More...
 
- Static Protected Member Functions inherited from Wt::WObject
static WObjectsender ()
 Returns the sender of the current slot call. More...
 

Detailed Description

A proxy model for Wt's item models that provides filtering and/or sorting.

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 WAbstractProxyModel::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
// we setup a proxy to filter the source model
Wt::WSortFilterProxyModel *proxy = new Wt::WSortFilterProxyModel(this);
proxy->setSourceModel(model);
proxy->setDynamicSortFilter(true);
proxy->setFilterKeyColumn(0);
proxy->setFilterRole(Wt::UserRole);
proxy->setFilterRegExp("Wt::.*");
// configure a view to use the proxy model instead of the source model
Wt::WTreeView *view = new Wt::WTreeView(this);
view->setModel(proxy);
...

Member Function Documentation

int Wt::WSortFilterProxyModel::columnCount ( const WModelIndex parent = WModelIndex()) const
virtual

Returns the number of columns.

This returns the number of columns at index parent.

See also
rowCount()

Implements Wt::WAbstractItemModel.

bool Wt::WSortFilterProxyModel::dynamicSortFilter ( ) const

Returns whether this proxy dynmically filters and sorts.

See also
setDynamicSortFilter()
bool Wt::WSortFilterProxyModel::filterAcceptRow ( int  sourceRow,
const WModelIndex sourceParent 
) const
protectedvirtual

Returns whether a source row is accepted by the filter.

The default implementation uses filterKeyColumn(), filterRole() and filterRegExp().

You may want to reimplement this method to provide specialized filtering.

int Wt::WSortFilterProxyModel::filterKeyColumn ( ) const

Return the column on which the filtering is applied.

See also
setFilterKeyColumn()
WString Wt::WSortFilterProxyModel::filterRegExp ( ) const

Return the regular expression used for filtering.

See also
setFilterRegExp()
int Wt::WSortFilterProxyModel::filterRole ( ) const

Return the data role used for filtering.

See also
setFilterRole()
boost::any Wt::WSortFilterProxyModel::headerData ( int  section,
Orientation  orientation = Horizontal,
int  role = DisplayRole 
) const
virtual

Returns the row or column header data.

The default proxy implementation constructs a dummy WModelIndex with the row set to 0 and column set to section if the orientation is Wt::Horizontal, or with the row set to section and the column set to 0 if the orientation is Wt::Vertical.

The resulting section that is forwarded to sourceModel()->headerData() depends on how the WModelIndex is transformed with mapToSource().

Reimplemented from Wt::WAbstractProxyModel.

WFlags< HeaderFlag > Wt::WSortFilterProxyModel::headerFlags ( int  section,
Orientation  orientation = Horizontal 
) const
virtual

Returns the flags for a header.

The default proxy implementation constructs a dummy WModelIndex with the row set to 0 and column set to section if the orientation is Wt::Horizontal, or with the row set to section and the column set to 0 if the orientation is Wt::Vertical.

The resulting section that is forwarded to sourceModel()->headerFlags() depends on how the WModelIndex is transformed with mapToSource().

Reimplemented from Wt::WAbstractProxyModel.

WModelIndex Wt::WSortFilterProxyModel::index ( int  row,
int  column,
const WModelIndex parent = WModelIndex() 
) const
virtual

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 at row and column within parent.

If the location is invalid (out of bounds at the parent), then an invalid index must be returned.

See also
parent()

Implements Wt::WAbstractItemModel.

bool Wt::WSortFilterProxyModel::insertRows ( int  row,
int  count,
const WModelIndex parent = WModelIndex() 
)
virtual

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.

Reimplemented from Wt::WAbstractItemModel.

void Wt::WSortFilterProxyModel::invalidate ( )

Invalidates the current filter.

This refilters and resorts the model, and is useful only if you have reimplemented filterAcceptRow() and/or lessThan()

bool Wt::WSortFilterProxyModel::lessThan ( const WModelIndex lhs,
const WModelIndex rhs 
) const
protectedvirtual

Compares two indexes.

The default implementation uses sortRole() 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.

WModelIndex Wt::WSortFilterProxyModel::mapFromSource ( const WModelIndex sourceIndex) const
virtual

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 of mapToSource().

See also
mapToSource()

Implements Wt::WAbstractProxyModel.

WModelIndex Wt::WSortFilterProxyModel::mapToSource ( const WModelIndex proxyIndex) const
virtual

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.

See also
mapFromSource()

Implements Wt::WAbstractProxyModel.

WModelIndex Wt::WSortFilterProxyModel::parent ( const WModelIndex index) const
virtual

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::internalPointer()) 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
index()

Implements Wt::WAbstractItemModel.

bool Wt::WSortFilterProxyModel::removeRows ( int  row,
int  count,
const WModelIndex parent = WModelIndex() 
)
virtual

Removes a number rows.

The rows are removed from the source model.

Reimplemented from Wt::WAbstractItemModel.

int Wt::WSortFilterProxyModel::rowCount ( const WModelIndex parent = WModelIndex()) const
virtual

Returns the number of rows.

This returns the number of rows at index parent.

See also
columnCount()

Implements Wt::WAbstractItemModel.

void Wt::WSortFilterProxyModel::setDynamicSortFilter ( bool  enable)

Configure the proxy to dynamically track changes in the source model.

When enable is true, 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.
See also
lessThan()
void Wt::WSortFilterProxyModel::setFilterKeyColumn ( int  column)

Specify the column on which the filtering is applied.

This configures the column on which the filterRegExp() is applied.

The default value is 0.

See also
setFilterRegExp(), setFilterRole()
void Wt::WSortFilterProxyModel::setFilterRegExp ( const WString pattern)

Specify a regular expression for filtering.

This configures the regular expression used for filtering on filterKeyColumn().

The default value is an empty expression, which disables filtering.

See also
setFilterKeyColumn(), setFilterRole()
void Wt::WSortFilterProxyModel::setFilterRole ( int  role)

Specify the data role used for filtering.

This configures the data role used for filtering on filterKeyColumn().

The default value is DisplayRole.

See also
setFilterKeyColumn(), setFilterRegExp()
bool Wt::WSortFilterProxyModel::setHeaderData ( int  section,
Orientation  orientation,
const boost::any &  value,
int  role = EditRole 
)
virtual

Sets header data for a column or row.

Returns true if the operation was successful.

See also
headerData()

Reimplemented from Wt::WAbstractItemModel.

void Wt::WSortFilterProxyModel::setSortRole ( int  role)

Specify the data role used used for sorting.

This configures the data role used for sorting.

The default value is DisplayRole.

See also
lessThan()
void Wt::WSortFilterProxyModel::setSourceModel ( WAbstractItemModel sourceModel)
virtual

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.

Reimplemented from Wt::WAbstractProxyModel.

void Wt::WSortFilterProxyModel::sort ( int  column,
SortOrder  order = AscendingOrder 
)
virtual

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 the layoutChanged() signal.

See also
layoutAboutToBeChanged(), layoutChanged()

Reimplemented from Wt::WAbstractItemModel.

int Wt::WSortFilterProxyModel::sortColumn ( ) const

Returns the current sort column.

When sort() has not been called, the model is unsorted, and this method returns -1.

See also
sort()
SortOrder Wt::WSortFilterProxyModel::sortOrder ( ) const

Returns the current sort order.

See also
sort()
int Wt::WSortFilterProxyModel::sortRole ( ) const

Return the data role used for sorting.

See also
setSortRole()

Generated on Tue Nov 21 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11