Wt  4.0.2
Public Member Functions | List of all members
Wt::WAggregateProxyModel Class Reference

A proxy model for Wt's item models that provides column aggregation. More...

#include <Wt/WAggregateProxyModel.h>

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

Public Member Functions

 WAggregateProxyModel ()
 Constructor. More...
 
virtual ~WAggregateProxyModel ()
 Destructor.
 
void addAggregate (int parentColumn, int firstColumn, int lastColumn)
 Adds a new column aggregation definition. More...
 
virtual WModelIndex mapFromSource (const WModelIndex &sourceIndex) const override
 Maps a source model index to the proxy model. More...
 
virtual WModelIndex mapToSource (const WModelIndex &proxyIndex) const override
 Maps a proxy model index to the source model. More...
 
virtual void setSourceModel (const std::shared_ptr< WAbstractItemModel > &sourceModel) override
 Sets the source model. More...
 
virtual void expandColumn (int column) override
 Expands a column. More...
 
virtual void collapseColumn (int column) override
 Collapses a column. More...
 
virtual int columnCount (const WModelIndex &parent=WModelIndex()) const override
 Returns the number of columns. More...
 
virtual int rowCount (const WModelIndex &parent=WModelIndex()) const override
 Returns the number of rows. More...
 
virtual WFlags< HeaderFlagheaderFlags (int section, Orientation orientation=Orientation::Horizontal) const override
 Returns the flags for a header. More...
 
virtual bool setHeaderData (int section, Orientation orientation, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit) override
 Sets header data for a column or row. More...
 
virtual cpp17::any headerData (int section, Orientation orientation=Orientation::Horizontal, ItemDataRole role=ItemDataRole::Display) const override
 Returns the row or column header data. More...
 
virtual WModelIndex parent (const WModelIndex &index) const override
 Returns the parent for a model index. More...
 
virtual WModelIndex index (int row, int column, const WModelIndex &parent=WModelIndex()) const override
 Returns the child index for the given row and column. More...
 
virtual void sort (int column, SortOrder order=SortOrder::Ascending) override
 Sorts the model according to a particular column. More...
 
- Public Member Functions inherited from Wt::WAbstractProxyModel
 WAbstractProxyModel ()
 Constructor.
 
std::shared_ptr< WAbstractItemModelsourceModel () const
 Returns the source model. More...
 
virtual cpp17::any data (const WModelIndex &index, ItemDataRole role=ItemDataRole::Display) const override
 Returns the data at a specific model index. More...
 
virtual bool setData (const WModelIndex &index, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit) override
 Sets the data at the given model index. More...
 
virtual bool setItemData (const WModelIndex &index, const DataMap &values) override
 Sets the data at the given model index. More...
 
virtual WFlags< ItemFlagflags (const WModelIndex &index) const override
 Returns the flags for an item. More...
 
virtual bool insertColumns (int column, int count, const WModelIndex &parent=WModelIndex()) override
 Inserts one or more columns. More...
 
virtual bool removeColumns (int column, int count, const WModelIndex &parent=WModelIndex()) override
 Removes columns. More...
 
virtual std::string mimeType () const override
 Returns a mime-type for dragging a set of indexes. More...
 
virtual std::vector< std::string > acceptDropMimeTypes () const override
 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) override
 Handles a drop event. More...
 
virtual void * toRawIndex (const WModelIndex &index) const override
 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 override
 Converts a raw pointer to a model index. More...
 
- Public Member Functions inherited from Wt::WAbstractItemModel
 WAbstractItemModel ()
 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, ItemDataRole role, const cpp17::any &value, int hits=-1, WFlags< MatchFlag > flags=WFlags< MatchFlag >(MatchFlag::StartsWith|MatchFlag::Wrap)) const
 Returns an index list for data items that match. More...
 
cpp17::any data (int row, int column, ItemDataRole role=ItemDataRole::Display, 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...
 
virtual bool insertRows (int row, int count, const WModelIndex &parent=WModelIndex())
 Inserts one or more rows. More...
 
virtual bool removeRows (int row, int count, const WModelIndex &parent=WModelIndex())
 Removes rows. More...
 
bool setHeaderData (int section, const cpp17::any &value)
 Sets column header data. 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 cpp17::any &value, ItemDataRole role=ItemDataRole::Edit, 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
void addChild (std::unique_ptr< WObject > child)
 Add a child WObject whose lifetime is determined by this WObject.
 
template<typename Child >
Child * addChild (std::unique_ptr< Child > child)
 Add a child WObject, returning a raw pointer. More...
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 Remove a child WObject, so its lifetime is no longer determined by this WObject.
 
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...
 
- Public Member Functions inherited from Wt::Core::observable
 observable () noexcept
 Default constructor.
 
virtual ~observable ()
 Destructor. More...
 

Additional Inherited Members

- Public Types inherited from Wt::WAbstractItemModel
typedef std::map< ItemDataRole, cpp17::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...
 
- 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...
 

Detailed Description

A proxy model for Wt's item models that provides column aggregation.

This proxy model does not store data itself, but presents data from a source model, and presents methods to organize columns using aggregation, through which a user may navigate (usually to obtain more detailed results related to a single aggregate quantity).

To use this proxy model, you should provide a source model using setSourceModel(), and define column ranges that can be aggregated using addAggregate().

Example:

int COLS = 18;
int ROWS = 20;
// set up the source model
model_ = std::make_shared<WStandardItemModel>(0, COLS);
std::string columns[] = {
"Supplier",
"2004",
"Q1",
"January",
"February",
"March",
"Q2",
"April",
"May",
"June",
"Q3",
"July",
"August",
"September",
"Q4",
"October",
"November",
"December"
};
for (unsigned i = 0; i < COLS; ++i) {
model_->setHeaderData(i, Orientation::Horizontal, columns[i]);
}
for (unsigned i = 0; i < ROWS; ++i) {
model_->insertRow(i);
for (unsigned j = 0; j < COLS; ++j)
model_->setData(i, j, "col " + std::to_string(j));
}
// set up the proxy model
auto proxy = std::make_shared<WAggregateProxyModel>();
proxy->setSourceModel(model_);
proxy->addAggregate(1, 2, 17);
proxy->addAggregate(2, 3, 5);
proxy->addAggregate(6, 7, 9);
proxy->addAggregate(10, 11, 13);
proxy->addAggregate(14, 15, 17);
proxy->expandColumn(1); // expand 2004 -> Q1 Q2 Q3 Q4
// define a view
treeView_ = root()->addWidget(std::make_unique<WTreeView>());
treeView_->setColumnBorder(Wt::StandardColor::Black);
treeView_->setModel(proxy);
treeView_->setColumnWidth(0, 160);
treeView_->setColumnResizeEnabled(true);
treeView_->setSortingEnabled(true);

This example would render like this:

WAggregateProxyModel-1.png
A WTreeView using a WAggregateProxyModel
Note
This model does not support dynamic changes to the column definition of the source model (i.e. insertions or deletions of source model columns).

Constructor & Destructor Documentation

Wt::WAggregateProxyModel::WAggregateProxyModel ( )

Constructor.

Sets up the proxy without aggregation functionality.

Member Function Documentation

void Wt::WAggregateProxyModel::addAggregate ( int  parentColumn,
int  firstColumn,
int  lastColumn 
)

Adds a new column aggregation definition.

The parentColumn is the column index in the source model that acts as an aggregate for columns firstColumn to lastColumn. parentColumn must border the range defined by firstColumn to lastColumn:

parentColumn == firstColumn - 1 || parentColumn == lastColumn + 1

Note that column parameters reference column indexes in the source model.

Aggregation definitions can be nested, but should be strictly hierarchical.

The aggregate column will initially be collapsed.

Only one aggregate can be defined per parentColumn.

void Wt::WAggregateProxyModel::collapseColumn ( int  column)
overridevirtual

Collapses a column.

Collapses a column. This may only be called by a view when the Wt::MatchFlag::ColumnIsExpandedLeft or Wt::MatchFlag::ColumnIsExpandedRight flag is set.

The default implementation does nothing.

See also
WAggregateProxyModel

Reimplemented from Wt::WAbstractItemModel.

int Wt::WAggregateProxyModel::columnCount ( const WModelIndex parent = WModelIndex()) const
overridevirtual

Returns the number of columns.

This returns the number of columns at index parent.

See also
rowCount()

Implements Wt::WAbstractItemModel.

void Wt::WAggregateProxyModel::expandColumn ( int  column)
overridevirtual

Expands a column.

Expands a column. This may only be called by a view when the Wt::MatchFlag::ColumnIsCollapsed flag is set.

The default implementation does nothing.

See also
WAggregateProxyModel

Reimplemented from Wt::WAbstractItemModel.

cpp17::any Wt::WAggregateProxyModel::headerData ( int  section,
Orientation  orientation = Orientation::Horizontal,
ItemDataRole  role = ItemDataRole::Display 
) const
overridevirtual

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::Orientation::Horizontal, or with the row set to section and the column set to 0 if the orientation is Wt::Orientation::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::WAggregateProxyModel::headerFlags ( int  section,
Orientation  orientation = Orientation::Horizontal 
) const
overridevirtual

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::Orientation::Horizontal, or with the row set to section and the column set to 0 if the orientation is Wt::Orientation::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::WAggregateProxyModel::index ( int  row,
int  column,
const WModelIndex parent = WModelIndex() 
) const
overridevirtual

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.

WModelIndex Wt::WAggregateProxyModel::mapFromSource ( const WModelIndex sourceIndex) const
overridevirtual

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::WAggregateProxyModel::mapToSource ( const WModelIndex proxyIndex) const
overridevirtual

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::WAggregateProxyModel::parent ( const WModelIndex index) const
overridevirtual

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.

int Wt::WAggregateProxyModel::rowCount ( const WModelIndex parent = WModelIndex()) const
overridevirtual

Returns the number of rows.

This returns the number of rows at index parent.

See also
columnCount()

Implements Wt::WAbstractItemModel.

bool Wt::WAggregateProxyModel::setHeaderData ( int  section,
Orientation  orientation,
const cpp17::any &  value,
ItemDataRole  role = ItemDataRole::Edit 
)
overridevirtual

Sets header data for a column or row.

Returns true if the operation was successful.

See also
headerData()

Reimplemented from Wt::WAbstractItemModel.

void Wt::WAggregateProxyModel::setSourceModel ( const std::shared_ptr< WAbstractItemModel > &  sourceModel)
overridevirtual

Sets the source model.

The source model provides the actual data for the proxy model.

Ownership of the source model is not transferred.

Note that the source model's signals are not forwarded to the proxy model by default, but some specializations, like WBatchEditProxyModel and WSortFilterProxyModel do. If you want to reimplement data() with no changes to row or column indices, consider the use of WIdentityProxyModel.

Reimplemented from Wt::WAbstractProxyModel.

void Wt::WAggregateProxyModel::sort ( int  column,
Wt::SortOrder  order = SortOrder::Ascending 
)
overridevirtual

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.


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