Add wxDataViewListModel base class for list models.

Introduce a base class for wxDataViewIndexListModel and
wxDataViewVirtualListModel instead of duplicating the same code in both of
them making the code difficult to maintain and change.

For now this class is not documented as it is used just to avoid duplication
in the implementation but maybe we should make it public to allow defining
other flat list data models (if this can be made to work in Carbon version).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62499 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2009-10-24 21:41:54 +00:00
parent 43c64cc6c9
commit 2feacb6eb3
2 changed files with 68 additions and 111 deletions

View File

@ -252,24 +252,78 @@ protected:
wxDataViewModelNotifiers m_notifiers;
};
// ----------------------------------------------------------------------------
// wxDataViewListModel: a model of a list, i.e. flat data structure without any
// branches/containers, used as base class by wxDataViewIndexListModel and
// wxDataViewVirtualListModel
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_ADV wxDataViewListModel : public wxDataViewModel
{
public:
// derived classes should override these methods instead of
// {Get,Set}Value() and GetAttr() inherited from the base class
virtual void GetValueByRow(wxVariant &variant,
unsigned row, unsigned col) const = 0;
virtual bool SetValueByRow(const wxVariant &variant,
unsigned row, unsigned col) = 0;
virtual bool
GetAttrByRow(unsigned WXUNUSED(row), unsigned WXUNUSED(col),
wxDataViewItemAttr &WXUNUSED(attr))
{
return false;
}
// helper methods provided by list models only
virtual unsigned GetRow( const wxDataViewItem &item ) const = 0;
// implement some base class pure virtual directly
virtual wxDataViewItem
GetParent( const wxDataViewItem & WXUNUSED(item) ) const
{
// items never have valid parent in this model
return wxDataViewItem();
}
virtual bool IsContainer( const wxDataViewItem &item ) const
{
// only the invisible (and invalid) root item has children
return !item.IsOk();
}
// and implement some others by forwarding them to our own ones
virtual void GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const
{
GetValueByRow(variant, GetRow(item), col);
}
virtual bool SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col )
{
return SetValueByRow( variant, GetRow(item), col );
}
virtual bool GetAttr(const wxDataViewItem &item, unsigned int col,
wxDataViewItemAttr &attr)
{
return GetAttrByRow( GetRow(item), col, attr );
}
};
// ---------------------------------------------------------
// wxDataViewIndexListModel
// ---------------------------------------------------------
class WXDLLIMPEXP_ADV wxDataViewIndexListModel: public wxDataViewModel
class WXDLLIMPEXP_ADV wxDataViewIndexListModel: public wxDataViewListModel
{
public:
wxDataViewIndexListModel( unsigned int initial_size = 0 );
~wxDataViewIndexListModel();
virtual void GetValueByRow( wxVariant &variant,
unsigned int row, unsigned int col ) const = 0;
virtual bool SetValueByRow( const wxVariant &variant,
unsigned int row, unsigned int col ) = 0;
virtual bool GetAttrByRow( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) )
{ return false; }
void RowPrepended();
void RowInserted( unsigned int before );
@ -282,7 +336,7 @@ public:
// convert to/from row/wxDataViewItem
unsigned int GetRow( const wxDataViewItem &item ) const;
virtual unsigned GetRow( const wxDataViewItem &item ) const;
wxDataViewItem GetItem( unsigned int row ) const;
// compare based on index
@ -292,18 +346,9 @@ public:
virtual bool HasDefaultCompare() const;
// implement base methods
virtual void GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const;
virtual bool SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col );
virtual bool GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr );
virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const;
virtual bool IsContainer( const wxDataViewItem &item ) const;
virtual unsigned int GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const;
// internal
virtual bool IsVirtualListModel() const { return false; }
unsigned int GetCount() const { return m_hash.GetCount(); }
private:
@ -321,20 +366,10 @@ private:
typedef wxDataViewIndexListModel wxDataViewVirtualListModel;
#else
class WXDLLIMPEXP_ADV wxDataViewVirtualListModel: public wxDataViewModel
class WXDLLIMPEXP_ADV wxDataViewVirtualListModel: public wxDataViewListModel
{
public:
wxDataViewVirtualListModel( unsigned int initial_size = 0 );
~wxDataViewVirtualListModel();
virtual void GetValueByRow( wxVariant &variant,
unsigned int row, unsigned int col ) const = 0;
virtual bool SetValueByRow( const wxVariant &variant,
unsigned int row, unsigned int col ) = 0;
virtual bool GetAttrByRow( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) )
{ return false; }
void RowPrepended();
void RowInserted( unsigned int before );
@ -347,7 +382,7 @@ public:
// convert to/from row/wxDataViewItem
unsigned int GetRow( const wxDataViewItem &item ) const;
virtual unsigned GetRow( const wxDataViewItem &item ) const;
wxDataViewItem GetItem( unsigned int row ) const;
// compare based on index
@ -357,14 +392,6 @@ public:
virtual bool HasDefaultCompare() const;
// implement base methods
virtual void GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const;
virtual bool SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col );
virtual bool GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr );
virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const;
virtual bool IsContainer( const wxDataViewItem &item ) const;
virtual unsigned int GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const;
// internal

View File

@ -306,10 +306,6 @@ wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
m_nextFreeID = initial_size + 1;
}
wxDataViewIndexListModel::~wxDataViewIndexListModel()
{
}
void wxDataViewIndexListModel::Reset( unsigned int new_size )
{
m_hash.Clear();
@ -443,37 +439,6 @@ int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
return GetRow(item2) - GetRow(item1);
}
void wxDataViewIndexListModel::GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const
{
GetValueByRow( variant, GetRow(item), col );
}
bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col )
{
return SetValueByRow( variant, GetRow(item), col );
}
bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
{
return GetAttrByRow( GetRow(item), col, attr );
}
wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
{
return wxDataViewItem(0);
}
bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const
{
// only the invisible root item has children
if (!item.IsOk())
return true;
return false;
}
unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
{
if (item.IsOk())
@ -495,10 +460,6 @@ wxDataViewVirtualListModel::wxDataViewVirtualListModel( unsigned int initial_siz
m_size = initial_size;
}
wxDataViewVirtualListModel::~wxDataViewVirtualListModel()
{
}
void wxDataViewVirtualListModel::Reset( unsigned int new_size )
{
m_size = new_size;
@ -590,37 +551,6 @@ int wxDataViewVirtualListModel::Compare(const wxDataViewItem& item1,
return pos2 - pos1;
}
void wxDataViewVirtualListModel::GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const
{
GetValueByRow( variant, GetRow(item), col );
}
bool wxDataViewVirtualListModel::SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col )
{
return SetValueByRow( variant, GetRow(item), col );
}
bool wxDataViewVirtualListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
{
return GetAttrByRow( GetRow(item), col, attr );
}
wxDataViewItem wxDataViewVirtualListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
{
return wxDataViewItem(0);
}
bool wxDataViewVirtualListModel::IsContainer( const wxDataViewItem &item ) const
{
// only the invisible root item has children
if (!item.IsOk())
return true;
return false;
}
unsigned int wxDataViewVirtualListModel::GetChildren( const wxDataViewItem &WXUNUSED(item), wxDataViewItemArray &WXUNUSED(children) ) const
{
return 0; // should we report an error ?