From 8eff6c56f09d20b1f18ee842efa16864d43ba930 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Fri, 16 Jan 2009 11:23:37 +0000 Subject: [PATCH] Change GetValue() and SetValue() to GetValueByRow() and SetValueByRow() in row based interface, Add wxDataViewListStore (WIP) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58124 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/dataview.h | 88 ++++++++++++++++++++++++-- interface/wx/dataview.h | 12 ++-- samples/dataview/dataview.cpp | 6 +- src/common/datavcmn.cpp | 116 ++++++++++++++++++++++++++++++++-- 4 files changed, 201 insertions(+), 21 deletions(-) diff --git a/include/wx/dataview.h b/include/wx/dataview.h index fee4defdf7..63a565efc4 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -24,6 +24,7 @@ #include "wx/icon.h" #include "wx/imaglist.h" #include "wx/weakref.h" +#include "wx/vector.h" #if !(defined(__WXGTK20__) || defined(__WXMAC__)) || defined(__WXUNIVERSAL__) // #if !(defined(__WXMAC__)) || defined(__WXUNIVERSAL__) @@ -242,13 +243,13 @@ public: wxDataViewIndexListModel( unsigned int initial_size = 0 ); ~wxDataViewIndexListModel(); - virtual void GetValue( wxVariant &variant, + virtual void GetValueByRow( wxVariant &variant, unsigned int row, unsigned int col ) const = 0; - virtual bool SetValue( const wxVariant &variant, + virtual bool SetValueByRow( const wxVariant &variant, unsigned int row, unsigned int col ) = 0; - virtual bool GetAttr( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) + virtual bool GetAttrByRow( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) { return false; } void RowPrepended(); @@ -307,13 +308,13 @@ public: wxDataViewVirtualListModel( unsigned int initial_size = 0 ); ~wxDataViewVirtualListModel(); - virtual void GetValue( wxVariant &variant, + virtual void GetValueByRow( wxVariant &variant, unsigned int row, unsigned int col ) const = 0; - virtual bool SetValue( const wxVariant &variant, + virtual bool SetValueByRow( const wxVariant &variant, unsigned int row, unsigned int col ) = 0; - virtual bool GetAttr( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) + virtual bool GetAttrByRow( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) { return false; } void RowPrepended(); @@ -907,6 +908,81 @@ private: #endif +//----------------------------------------------------------------------------- +// wxDataViewListStore +//----------------------------------------------------------------------------- + +class WXDLLIMPEXP_ADV wxDataViewListStoreLine +{ +public: + wxDataViewListStoreLine( wxClientData *data = NULL ) + { + m_data = data; + } + virtual ~wxDataViewListStoreLine() + { + delete m_data; + } + + void SetData( wxClientData *data ) + { if (m_data) delete m_data; m_data = data; } + wxClientData *GetData() const + { return m_data; } + + wxVector m_values; + +private: + wxClientData *m_data; +}; + + +class WXDLLIMPEXP_ADV wxDataViewListStore: public wxDataViewIndexListModel +{ +public: + wxDataViewListStore(); + ~wxDataViewListStore(); + + void PrependColumn( const wxString &varianttype ); + void InsertColumn( unsigned int pos, const wxString &varianttype ); + void AppendColumn( const wxString &varianttype ); + + void PrependStringColumn() + { PrependColumn( wxT("string") ); } + void InsertStringColumn( unsigned int pos ) + { InsertColumn( pos, wxT("string") ); } + void AppendStringColumn() + { AppendColumn( wxT("string") ); } + + void AppendItem( const wxVector &values, wxClientData *data = NULL ); + void PrependItem( const wxVector &values, wxClientData *data = NULL ); + void InsertItem( unsigned int row, const wxVector &values, wxClientData *data = NULL ); + void DeleteItem( unsigned pos ); + void DeleteAllItems(); + + void SetStringValue( const wxString &value, unsigned int row, unsigned int col ) + { SetValueByRow( value, row, col ); } + wxString GetStringValue( unsigned int row, unsigned int col ) + { wxVariant value; GetValueByRow( value, row, col ); return value.GetString(); } + + // override base virtuals + + virtual unsigned int GetColumnCount() const; + + virtual wxString GetColumnType( unsigned int col ) const; + + virtual void GetValueByRow( wxVariant &value, + unsigned int row, unsigned int col ) const; + + virtual bool SetValueByRow( const wxVariant &value, + unsigned int row, unsigned int col ); + + +public: + wxVector m_data; + wxArrayString m_cols; +}; + + //----------------------------------------------------------------------------- // wxDataViewTreeStore //----------------------------------------------------------------------------- diff --git a/interface/wx/dataview.h b/interface/wx/dataview.h index aa54468bcf..91af033806 100644 --- a/interface/wx/dataview.h +++ b/interface/wx/dataview.h @@ -299,7 +299,7 @@ public: @see wxDataViewItemAttr. */ - virtual bool GetAttr(unsigned int row, unsigned int col, + virtual bool GetAttrByRow(unsigned int row, unsigned int col, wxDataViewItemAttr& attr); /** @@ -315,7 +315,7 @@ public: /** Override this to allow getting values from the model. */ - virtual void GetValue(wxVariant& variant, unsigned int row, + virtual void GetValueByRow(wxVariant& variant, unsigned int row, unsigned int col) const = 0; /** @@ -365,7 +365,7 @@ public: /** Called in order to set a value in the model. */ - virtual bool SetValue(const wxVariant& variant, unsigned int row, + virtual bool SetValueByRow(const wxVariant& variant, unsigned int row, unsigned int col) = 0; }; @@ -1673,9 +1673,9 @@ public: wxDataViewTreeStore is a specialised wxDataViewModel for displaying simple trees very much like wxTreeCtrl does and it offers a similar API. - This class actually stores the entire tree (therefore its name) and implements - all virtual methods from the base class so it can be used directly without - having to derive any class from it. + This class actually stores the entire tree and the values (therefore its name) + and implements all virtual methods from the base class so it can be used directly + without having to derive any class from it. This comes at the price of much reduced flexibility. @library{wxadv} diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 239ca116fb..37e7541708 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -530,7 +530,7 @@ public: return m_array.GetCount(); } - virtual void GetValue( wxVariant &variant, + virtual void GetValueByRow( wxVariant &variant, unsigned int row, unsigned int col ) const { if (col==0) @@ -560,7 +560,7 @@ public: } } - virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ) + virtual bool GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ) { if (col != 2) return false; @@ -574,7 +574,7 @@ public: return true; } - virtual bool SetValue( const wxVariant &variant, + virtual bool SetValueByRow( const wxVariant &variant, unsigned int row, unsigned int col ) { if (col == 0) diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index 79f5b89c45..6e84094301 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -456,18 +456,18 @@ int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1, void wxDataViewIndexListModel::GetValue( wxVariant &variant, const wxDataViewItem &item, unsigned int col ) const { - GetValue( variant, GetRow(item), col ); + GetValueByRow( variant, GetRow(item), col ); } bool wxDataViewIndexListModel::SetValue( const wxVariant &variant, const wxDataViewItem &item, unsigned int col ) { - return SetValue( variant, GetRow(item), col ); + return SetValueByRow( variant, GetRow(item), col ); } bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr ) { - return GetAttr( GetRow(item), col, attr ); + return GetAttrByRow( GetRow(item), col, attr ); } wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const @@ -603,18 +603,18 @@ int wxDataViewVirtualListModel::Compare(const wxDataViewItem& item1, void wxDataViewVirtualListModel::GetValue( wxVariant &variant, const wxDataViewItem &item, unsigned int col ) const { - GetValue( variant, GetRow(item), col ); + GetValueByRow( variant, GetRow(item), col ); } bool wxDataViewVirtualListModel::SetValue( const wxVariant &variant, const wxDataViewItem &item, unsigned int col ) { - return SetValue( variant, GetRow(item), col ); + return SetValueByRow( variant, GetRow(item), col ); } bool wxDataViewVirtualListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr ) { - return GetAttr( GetRow(item), col, attr ); + return GetAttrByRow( GetRow(item), col, attr ); } wxDataViewItem wxDataViewVirtualListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const @@ -1354,6 +1354,110 @@ bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const #endif +//----------------------------------------------------------------------------- +// wxDataViewListStore +//----------------------------------------------------------------------------- + +wxDataViewListStore::wxDataViewListStore() +{ +} + +wxDataViewListStore::~wxDataViewListStore() +{ + wxVector::iterator it; + for (it = m_data.begin(); it != m_data.end(); ++it) + { + wxDataViewListStoreLine* line = *it; + delete line; + } +} + +void wxDataViewListStore::PrependColumn( const wxString &varianttype ) +{ + m_cols.Insert( varianttype, 0 ); +} + +void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype ) +{ + m_cols.Insert( varianttype, pos ); +} + +void wxDataViewListStore::AppendColumn( const wxString &varianttype ) +{ + m_cols.Add( varianttype ); +} + +unsigned int wxDataViewListStore::GetColumnCount() const +{ + return m_cols.GetCount(); +} + +wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const +{ + return m_cols[pos]; +} + +void wxDataViewListStore::AppendItem( const wxVector &values, wxClientData *data ) +{ + wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data ); + line->m_values = values; + m_data.push_back( line ); + + RowAppended(); +} + +void wxDataViewListStore::PrependItem( const wxVector &values, wxClientData *data ) +{ + wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data ); + line->m_values = values; + m_data.insert( m_data.begin(), line ); + + RowPrepended(); +} + +void wxDataViewListStore::InsertItem( unsigned int row, const wxVector &values, wxClientData *data ) +{ + wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data ); + line->m_values = values; + m_data.insert( m_data.begin()+row, line ); + + RowInserted( row ); +} + +void wxDataViewListStore::DeleteItem( unsigned row ) +{ + wxVector::iterator it = m_data.begin() + row; + m_data.erase( it ); + + RowDeleted( row ); +} + +void wxDataViewListStore::DeleteAllItems() +{ + wxVector::iterator it; + for (it = m_data.begin(); it != m_data.end(); ++it) + { + wxDataViewListStoreLine* line = *it; + delete line; + } + + Reset( 0 ); +} + +void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const +{ + wxDataViewListStoreLine *line = m_data[row]; + value = line->m_values[col]; +} + +bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col ) +{ + wxDataViewListStoreLine *line = m_data[row]; + line->m_values[col] = value; + + return true; +} + //----------------------------------------------------------------------------- // wxDataViewTreeStore //----------------------------------------------------------------------------- -- 2.45.2