#include <wx/dataview.h>
wxDataViewModel is the base class for all data model to be displayed by a wxDataViewCtrl.
All other models derive from it and must implement its pure virtual functions in order to define a complete data model. In detail, you need to override wxDataViewModel::IsContainer, wxDataViewModel::GetParent, wxDataViewModel::GetChildren, wxDataViewModel::GetColumnCount, wxDataViewModel::GetColumnType and wxDataViewModel::GetValue in order to define the data model which acts as an interface between your actual data and the wxDataViewCtrl.
Since you will usually also allow the wxDataViewCtrl to change your data through its graphical interface, you will also have to override wxDataViewModel::SetValue which the wxDataViewCtrl will call when a change to some data has been committed.
wxDataViewModel (as indeed the entire wxDataViewCtrl code) is using wxVariant to store data and its type in a generic way. wxVariant can be extended to contain almost any data without changes to the original class.
The data that is presented through this data model is expected to change at run-time. You need to inform the data model when a change happened. Depending on what happened you need to call one of the following methods:
There are plural forms for notification of addition, change or removal of several item at once. See:
Note that wxDataViewModel does not define the position or index of any item in the control because different controls might display the same data differently. wxDataViewModel does provide a wxDataViewModel::Compare method which the wxDataViewCtrl may use to sort the data either in conjunction with a column header or without (see wxDataViewModel::HasDefaultCompare).
This class maintains a list of wxDataViewModelNotifier which link this class to the specific implementations on the supported platforms so that e.g. calling wxDataViewModel::ValueChanged on this model will just call wxDataViewModelNotifier::ValueChanged for each notifier that has been added. You can also add your own notifier in order to get informed about any changes to the data in the list model.
Currently wxWidgets provides the following models apart from the base model: wxDataViewIndexListModel, wxDataViewVirtualListModel, wxDataViewTreeStore, wxDataViewListStore.
Note that wxDataViewModel is reference counted, derives from wxRefCounter and cannot be deleted directly as it can be shared by several wxDataViewCtrls. This implies that you need to decrease the reference count after associating the model with a control like this:
wxDataViewCtrl *musicCtrl = new wxDataViewCtrl( this, wxID_ANY ); wxDataViewModel *musicModel = new MyMusicModel; m_musicCtrl->AssociateModel( musicModel ); musicModel->DecRef(); // avoid memory leak !! // add columns now
Public Member Functions | |
wxDataViewModel () | |
Constructor. | |
void | AddNotifier (wxDataViewModelNotifier *notifier) |
Adds a wxDataViewModelNotifier to the model. | |
bool | ChangeValue (const wxVariant &variant, const wxDataViewItem &item, unsigned int col) |
Change the value of the given item and update the control to reflect it. | |
virtual bool | Cleared () |
Called to inform the model that all data has been cleared. | |
virtual int | Compare (const wxDataViewItem &item1, const wxDataViewItem &item2, unsigned int column, bool ascending) const |
The compare function to be used by control. | |
virtual bool | GetAttr (const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr) const |
Override this to indicate that the item has special font attributes. | |
virtual unsigned int | GetChildren (const wxDataViewItem &item, wxDataViewItemArray &children) const =0 |
Override this so the control can query the child items of an item. | |
virtual unsigned int | GetColumnCount () const =0 |
Override this to indicate the number of columns in the model. | |
virtual wxString | GetColumnType (unsigned int col) const =0 |
Override this to indicate what type of data is stored in the column specified by col. | |
virtual wxDataViewItem | GetParent (const wxDataViewItem &item) const =0 |
Override this to indicate which wxDataViewItem representing the parent of item or an invalid wxDataViewItem if the the root item is the parent item. | |
virtual void | GetValue (wxVariant &variant, const wxDataViewItem &item, unsigned int col) const =0 |
Override this to indicate the value of item. | |
virtual bool | HasContainerColumns (const wxDataViewItem &item) const |
Override this method to indicate if a container item merely acts as a headline (or for categorisation) or if it also acts a normal item with entries for futher columns. | |
virtual bool | HasDefaultCompare () const |
Override this to indicate that the model provides a default compare function that the control should use if no wxDataViewColumn has been chosen for sorting. | |
bool | HasValue (const wxDataViewItem &item, unsigned col) const |
Return true if there is a value in the given column of this item. | |
virtual bool | IsContainer (const wxDataViewItem &item) const =0 |
Override this to indicate of item is a container, i.e. | |
virtual bool | ItemAdded (const wxDataViewItem &parent, const wxDataViewItem &item) |
Call this to inform the model that an item has been added to the data. | |
virtual bool | ItemChanged (const wxDataViewItem &item) |
Call this to inform the model that an item has changed. | |
virtual bool | ItemDeleted (const wxDataViewItem &parent, const wxDataViewItem &item) |
Call this to inform the model that an item has been deleted from the data. | |
virtual bool | ItemsAdded (const wxDataViewItem &parent, const wxDataViewItemArray &items) |
Call this to inform the model that several items have been added to the data. | |
virtual bool | ItemsChanged (const wxDataViewItemArray &items) |
Call this to inform the model that several items have changed. | |
virtual bool | ItemsDeleted (const wxDataViewItem &parent, const wxDataViewItemArray &items) |
Call this to inform the model that several items have been deleted. | |
void | RemoveNotifier (wxDataViewModelNotifier *notifier) |
Remove the notifier from the list of notifiers. | |
virtual void | Resort () |
Call this to initiate a resort after the sort function has been changed. | |
virtual bool | SetValue (const wxVariant &variant, const wxDataViewItem &item, unsigned int col)=0 |
This gets called in order to set a value in the data model. | |
virtual bool | ValueChanged (const wxDataViewItem &item, unsigned int col) |
Call this to inform this model that a value in the model has been changed. | |
Protected Member Functions | |
virtual | ~wxDataViewModel () |
Destructor. |
wxDataViewModel::wxDataViewModel | ( | ) |
Constructor.
virtual wxDataViewModel::~wxDataViewModel | ( | ) | [protected, virtual] |
Destructor.
This should not be called directly. Use DecRef() instead.
void wxDataViewModel::AddNotifier | ( | wxDataViewModelNotifier * | notifier | ) |
Adds a wxDataViewModelNotifier to the model.
bool wxDataViewModel::ChangeValue | ( | const wxVariant & | variant, | |
const wxDataViewItem & | item, | |||
unsigned int | col | |||
) |
Change the value of the given item and update the control to reflect it.
This function simply calls SetValue() and, if it succeeded, ValueChanged().
variable | The new value. | |
item | The item (row) to update. | |
col | The column to update. |
virtual bool wxDataViewModel::Cleared | ( | ) | [virtual] |
Called to inform the model that all data has been cleared.
The control will reread the data from the model again.
virtual int wxDataViewModel::Compare | ( | const wxDataViewItem & | item1, | |
const wxDataViewItem & | item2, | |||
unsigned int | column, | |||
bool | ascending | |||
) | const [virtual] |
The compare function to be used by control.
The default compare function sorts by container and other items separately and in ascending order. Override this for a different sorting behaviour.
virtual bool wxDataViewModel::GetAttr | ( | const wxDataViewItem & | item, | |
unsigned int | col, | |||
wxDataViewItemAttr & | attr | |||
) | const [virtual] |
Override this to indicate that the item has special font attributes.
This only affects the wxDataViewTextRendererText renderer.
The base class version always simply returns false.
item | The item for which the attribute is requested. | |
col | The column of the item for which the attribute is requested. | |
attr | The attribute to be filled in if the function returns true. |
virtual unsigned int wxDataViewModel::GetChildren | ( | const wxDataViewItem & | item, | |
wxDataViewItemArray & | children | |||
) | const [pure virtual] |
Override this so the control can query the child items of an item.
Returns the number of items.
virtual unsigned int wxDataViewModel::GetColumnCount | ( | ) | const [pure virtual] |
Override this to indicate the number of columns in the model.
Implemented in wxDataViewListStore.
virtual wxString wxDataViewModel::GetColumnType | ( | unsigned int | col | ) | const [pure virtual] |
Override this to indicate what type of data is stored in the column specified by col.
This should return a string indicating the type of data as reported by wxVariant.
Implemented in wxDataViewListStore.
virtual wxDataViewItem wxDataViewModel::GetParent | ( | const wxDataViewItem & | item | ) | const [pure virtual] |
Override this to indicate which wxDataViewItem representing the parent of item or an invalid wxDataViewItem if the the root item is the parent item.
virtual void wxDataViewModel::GetValue | ( | wxVariant & | variant, | |
const wxDataViewItem & | item, | |||
unsigned int | col | |||
) | const [pure virtual] |
Override this to indicate the value of item.
A wxVariant is used to store the data.
virtual bool wxDataViewModel::HasContainerColumns | ( | const wxDataViewItem & | item | ) | const [virtual] |
Override this method to indicate if a container item merely acts as a headline (or for categorisation) or if it also acts a normal item with entries for futher columns.
By default returns false.
virtual bool wxDataViewModel::HasDefaultCompare | ( | ) | const [virtual] |
Override this to indicate that the model provides a default compare function that the control should use if no wxDataViewColumn has been chosen for sorting.
Usually, the user clicks on a column header for sorting, the data will be sorted alphanumerically.
If any other order (e.g. by index or order of appearance) is required, then this should be used. See wxDataViewIndexListModel for a model which makes use of this.
bool wxDataViewModel::HasValue | ( | const wxDataViewItem & | item, | |
unsigned | col | |||
) | const |
Return true if there is a value in the given column of this item.
All normal items have values in all columns but the container items only show their label in the first column (col == 0) by default (but see HasContainerColumns()). So this function always returns true for the first column while for the other ones it returns true only if the item is not a container or HasContainerColumns() was overridden to return true for it.
virtual bool wxDataViewModel::IsContainer | ( | const wxDataViewItem & | item | ) | const [pure virtual] |
Override this to indicate of item is a container, i.e.
if it can have child items.
virtual bool wxDataViewModel::ItemAdded | ( | const wxDataViewItem & | parent, | |
const wxDataViewItem & | item | |||
) | [virtual] |
Call this to inform the model that an item has been added to the data.
virtual bool wxDataViewModel::ItemChanged | ( | const wxDataViewItem & | item | ) | [virtual] |
Call this to inform the model that an item has changed.
This will eventually emit a wxEVT_DATAVIEW_ITEM_VALUE_CHANGED event (in which the column fields will not be set) to the user.
virtual bool wxDataViewModel::ItemDeleted | ( | const wxDataViewItem & | parent, | |
const wxDataViewItem & | item | |||
) | [virtual] |
Call this to inform the model that an item has been deleted from the data.
virtual bool wxDataViewModel::ItemsAdded | ( | const wxDataViewItem & | parent, | |
const wxDataViewItemArray & | items | |||
) | [virtual] |
Call this to inform the model that several items have been added to the data.
virtual bool wxDataViewModel::ItemsChanged | ( | const wxDataViewItemArray & | items | ) | [virtual] |
Call this to inform the model that several items have changed.
This will eventually emit wxEVT_DATAVIEW_ITEM_VALUE_CHANGED events (in which the column fields will not be set) to the user.
virtual bool wxDataViewModel::ItemsDeleted | ( | const wxDataViewItem & | parent, | |
const wxDataViewItemArray & | items | |||
) | [virtual] |
Call this to inform the model that several items have been deleted.
void wxDataViewModel::RemoveNotifier | ( | wxDataViewModelNotifier * | notifier | ) |
Remove the notifier from the list of notifiers.
virtual void wxDataViewModel::Resort | ( | ) | [virtual] |
Call this to initiate a resort after the sort function has been changed.
virtual bool wxDataViewModel::SetValue | ( | const wxVariant & | variant, | |
const wxDataViewItem & | item, | |||
unsigned int | col | |||
) | [pure virtual] |
This gets called in order to set a value in the data model.
The most common scenario is that the wxDataViewCtrl calls this method after the user changed some data in the view.
This is the function you need to override in your derived class but if you want to call it, ChangeValue() is usually more convenient as otherwise you need to manually call ValueChanged() to update the control itself.
virtual bool wxDataViewModel::ValueChanged | ( | const wxDataViewItem & | item, | |
unsigned int | col | |||
) | [virtual] |
Call this to inform this model that a value in the model has been changed.
This is also called from wxDataViewCtrl's internal editing code, e.g. when editing a text field in the control.
This will eventually emit a wxEVT_DATAVIEW_ITEM_VALUE_CHANGED event to the user.
![]() |
[ top ] |