set svn properties correctly for the new files (please use the auto-props!)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59334 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-03-05 14:00:46 +00:00
parent a4991d4333
commit 03dfc8f515
2 changed files with 663 additions and 663 deletions

View File

@@ -1,447 +1,447 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: mymodels.cpp // Name: mymodels.cpp
// Purpose: wxDataViewCtrl wxWidgets sample // Purpose: wxDataViewCtrl wxWidgets sample
// Author: Robert Roebling // Author: Robert Roebling
// Modified by: Francesco Montorsi, Bo Yang // Modified by: Francesco Montorsi, Bo Yang
// Created: 06/01/06 // Created: 06/01/06
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Robert Roebling // Copyright: (c) Robert Roebling
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// For compilers that support precompilation, includes "wx/wx.h". // For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h" #include "wx/wxprec.h"
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#pragma hdrstop #pragma hdrstop
#endif #endif
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/wx.h" #include "wx/wx.h"
#endif #endif
#include "wx/dataview.h" #include "wx/dataview.h"
#include "mymodels.h" #include "mymodels.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// resources // resources
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#include "null.xpm" #include "null.xpm"
#include "wx_small.xpm" #include "wx_small.xpm"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyMusicTreeModel // MyMusicTreeModel
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
MyMusicTreeModel::MyMusicTreeModel() MyMusicTreeModel::MyMusicTreeModel()
{ {
m_root = new MyMusicTreeModelNode( NULL, wxT("My Music" )); m_root = new MyMusicTreeModelNode( NULL, wxT("My Music" ));
// setup pop music // setup pop music
m_pop = new MyMusicTreeModelNode( m_root, wxT("Pop music") ); m_pop = new MyMusicTreeModelNode( m_root, wxT("Pop music") );
m_pop->Append( m_pop->Append(
new MyMusicTreeModelNode( m_pop, wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) ); new MyMusicTreeModelNode( m_pop, wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
m_pop->Append( m_pop->Append(
new MyMusicTreeModelNode( m_pop, wxT("Take a bow"), wxT("Madonna"), 1994 ) ); new MyMusicTreeModelNode( m_pop, wxT("Take a bow"), wxT("Madonna"), 1994 ) );
m_root->Append( m_pop ); m_root->Append( m_pop );
// setup classical music // setup classical music
m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") ); m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") );
m_ninth = new MyMusicTreeModelNode( m_classical, wxT("Ninth symphony"), m_ninth = new MyMusicTreeModelNode( m_classical, wxT("Ninth symphony"),
wxT("Ludwig van Beethoven"), 1824 ); wxT("Ludwig van Beethoven"), 1824 );
m_classical->Append( m_ninth ); m_classical->Append( m_ninth );
m_classical->Append( new MyMusicTreeModelNode( m_classical, wxT("German Requiem"), m_classical->Append( new MyMusicTreeModelNode( m_classical, wxT("German Requiem"),
wxT("Johannes Brahms"), 1868 ) ); wxT("Johannes Brahms"), 1868 ) );
m_root->Append( m_classical ); m_root->Append( m_classical );
m_classicalMusicIsKnownToControl = false; m_classicalMusicIsKnownToControl = false;
} }
wxString MyMusicTreeModel::GetTitle( const wxDataViewItem &item ) const wxString MyMusicTreeModel::GetTitle( const wxDataViewItem &item ) const
{ {
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
if (!node) // happens if item.IsOk()==false if (!node) // happens if item.IsOk()==false
return wxEmptyString; return wxEmptyString;
return node->m_title; return node->m_title;
} }
int MyMusicTreeModel::GetYear( const wxDataViewItem &item ) const int MyMusicTreeModel::GetYear( const wxDataViewItem &item ) const
{ {
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
if (!node) // happens if item.IsOk()==false if (!node) // happens if item.IsOk()==false
return 2000; return 2000;
return node->m_year; return node->m_year;
} }
void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &artist, void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &artist,
unsigned int year ) unsigned int year )
{ {
if (!m_classical) if (!m_classical)
{ {
wxASSERT(m_root); wxASSERT(m_root);
// it was removed: restore it // it was removed: restore it
m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") ); m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") );
m_root->Append( m_classical ); m_root->Append( m_classical );
} }
// add to the classical music node a new node: // add to the classical music node a new node:
MyMusicTreeModelNode *child_node = MyMusicTreeModelNode *child_node =
new MyMusicTreeModelNode( m_classical, title, artist, year ); new MyMusicTreeModelNode( m_classical, title, artist, year );
m_classical->Append( child_node ); m_classical->Append( child_node );
if (m_classicalMusicIsKnownToControl) if (m_classicalMusicIsKnownToControl)
{ {
// notify control // notify control
wxDataViewItem child( (void*) child_node ); wxDataViewItem child( (void*) child_node );
wxDataViewItem parent( (void*) m_classical ); wxDataViewItem parent( (void*) m_classical );
ItemAdded( parent, child ); ItemAdded( parent, child );
} }
} }
void MyMusicTreeModel::Delete( const wxDataViewItem &item ) void MyMusicTreeModel::Delete( const wxDataViewItem &item )
{ {
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
if (!node) // happens if item.IsOk()==false if (!node) // happens if item.IsOk()==false
return; return;
wxDataViewItem parent( node->GetParent() ); wxDataViewItem parent( node->GetParent() );
if (!parent.IsOk()) if (!parent.IsOk())
{ {
wxASSERT(node == m_root); wxASSERT(node == m_root);
// don't make the control completely empty: // don't make the control completely empty:
wxLogError("Cannot remove the root item!"); wxLogError("Cannot remove the root item!");
return; return;
} }
// is the node one of those we keep stored in special pointers? // is the node one of those we keep stored in special pointers?
if (node == m_pop) if (node == m_pop)
m_pop = NULL; m_pop = NULL;
else if (node == m_classical) else if (node == m_classical)
m_classical = NULL; m_classical = NULL;
else if (node == m_ninth) else if (node == m_ninth)
m_ninth = NULL; m_ninth = NULL;
// first remove the node from the parent's array of children; // first remove the node from the parent's array of children;
// NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_ // NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_
// thus removing the node from it doesn't result in freeing it // thus removing the node from it doesn't result in freeing it
node->GetParent()->GetChildren().Remove( node ); node->GetParent()->GetChildren().Remove( node );
// free the node // free the node
delete node; delete node;
// notify control // notify control
ItemDeleted( parent, item ); ItemDeleted( parent, item );
} }
int MyMusicTreeModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, int MyMusicTreeModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
unsigned int column, bool ascending ) unsigned int column, bool ascending )
{ {
wxASSERT(item1.IsOk() && item2.IsOk()); wxASSERT(item1.IsOk() && item2.IsOk());
// should never happen // should never happen
if (IsContainer(item1) && IsContainer(item2)) if (IsContainer(item1) && IsContainer(item2))
{ {
wxVariant value1, value2; wxVariant value1, value2;
GetValue( value1, item1, 0 ); GetValue( value1, item1, 0 );
GetValue( value2, item2, 0 ); GetValue( value2, item2, 0 );
wxString str1 = value1.GetString(); wxString str1 = value1.GetString();
wxString str2 = value2.GetString(); wxString str2 = value2.GetString();
int res = str1.Cmp( str2 ); int res = str1.Cmp( str2 );
if (res) return res; if (res) return res;
// items must be different // items must be different
wxUIntPtr litem1 = (wxUIntPtr) item1.GetID(); wxUIntPtr litem1 = (wxUIntPtr) item1.GetID();
wxUIntPtr litem2 = (wxUIntPtr) item2.GetID(); wxUIntPtr litem2 = (wxUIntPtr) item2.GetID();
return litem1-litem2; return litem1-litem2;
} }
return wxDataViewModel::Compare( item1, item2, column, ascending ); return wxDataViewModel::Compare( item1, item2, column, ascending );
} }
void MyMusicTreeModel::GetValue( wxVariant &variant, void MyMusicTreeModel::GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const const wxDataViewItem &item, unsigned int col ) const
{ {
wxASSERT(item.IsOk()); wxASSERT(item.IsOk());
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
switch (col) switch (col)
{ {
case 0: case 0:
variant = node->m_title; variant = node->m_title;
break; break;
case 1: case 1:
variant = node->m_artist; variant = node->m_artist;
break; break;
case 2: case 2:
variant = (long) node->m_year; variant = (long) node->m_year;
break; break;
case 3: case 3:
variant = node->m_quality; variant = node->m_quality;
break; break;
case 4: case 4:
// wxMac doesn't conceal the popularity progress renderer, return 0 for containers // wxMac doesn't conceal the popularity progress renderer, return 0 for containers
if (IsContainer(item)) if (IsContainer(item))
variant = (long) 0; variant = (long) 0;
else else
variant = (long) 80; // all music is very 80% popular variant = (long) 80; // all music is very 80% popular
break; break;
case 5: case 5:
// Make size of red square depend on year // Make size of red square depend on year
if (GetYear(item) < 1900) if (GetYear(item) < 1900)
variant = (long) 35; variant = (long) 35;
else else
variant = (long) 25; variant = (long) 25;
break; break;
default: default:
wxLogError( wxT("MyMusicTreeModel::GetValue: wrong column %d"), col ); wxLogError( wxT("MyMusicTreeModel::GetValue: wrong column %d"), col );
} }
} }
bool MyMusicTreeModel::SetValue( const wxVariant &variant, bool MyMusicTreeModel::SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const wxDataViewItem &item, unsigned int col )
{ {
wxASSERT(item.IsOk()); wxASSERT(item.IsOk());
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
switch (col) switch (col)
{ {
case 0: case 0:
node->m_title = variant.GetString(); node->m_title = variant.GetString();
return true; return true;
case 1: case 1:
node->m_artist = variant.GetString(); node->m_artist = variant.GetString();
return true; return true;
case 2: case 2:
node->m_year = variant.GetLong(); node->m_year = variant.GetLong();
return true; return true;
case 3: case 3:
node->m_quality = variant.GetString(); node->m_quality = variant.GetString();
return true; return true;
default: default:
wxLogError( wxT("MyMusicTreeModel::SetValue: wrong column") ); wxLogError( wxT("MyMusicTreeModel::SetValue: wrong column") );
} }
return false; return false;
} }
wxDataViewItem MyMusicTreeModel::GetParent( const wxDataViewItem &item ) const wxDataViewItem MyMusicTreeModel::GetParent( const wxDataViewItem &item ) const
{ {
// the invisible root node has no parent // the invisible root node has no parent
if (!item.IsOk()) if (!item.IsOk())
return wxDataViewItem(0); return wxDataViewItem(0);
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
// "MyMusic" also has no parent // "MyMusic" also has no parent
if (node == m_root) if (node == m_root)
return wxDataViewItem(0); return wxDataViewItem(0);
return wxDataViewItem( (void*) node->GetParent() ); return wxDataViewItem( (void*) node->GetParent() );
} }
bool MyMusicTreeModel::IsContainer( const wxDataViewItem &item ) const bool MyMusicTreeModel::IsContainer( const wxDataViewItem &item ) const
{ {
// the invisble root node can have children // the invisble root node can have children
// (in our model always "MyMusic") // (in our model always "MyMusic")
if (!item.IsOk()) if (!item.IsOk())
return true; return true;
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
return node->IsContainer(); return node->IsContainer();
} }
unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent, unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent,
wxDataViewItemArray &array ) const wxDataViewItemArray &array ) const
{ {
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) parent.GetID(); MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) parent.GetID();
if (!node) if (!node)
{ {
array.Add( wxDataViewItem( (void*) m_root ) ); array.Add( wxDataViewItem( (void*) m_root ) );
return 1; return 1;
} }
if (node == m_classical) if (node == m_classical)
{ {
MyMusicTreeModel *model = (MyMusicTreeModel*)(const MyMusicTreeModel*) this; MyMusicTreeModel *model = (MyMusicTreeModel*)(const MyMusicTreeModel*) this;
model->m_classicalMusicIsKnownToControl = true; model->m_classicalMusicIsKnownToControl = true;
} }
if (node->GetChildCount() == 0) if (node->GetChildCount() == 0)
{ {
return 0; return 0;
} }
unsigned int count = node->GetChildren().GetCount(); unsigned int count = node->GetChildren().GetCount();
for (unsigned int pos = 0; pos < count; pos++) for (unsigned int pos = 0; pos < count; pos++)
{ {
MyMusicTreeModelNode *child = node->GetChildren().Item( pos ); MyMusicTreeModelNode *child = node->GetChildren().Item( pos );
array.Add( wxDataViewItem( (void*) child ) ); array.Add( wxDataViewItem( (void*) child ) );
} }
return count; return count;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyListModel // MyListModel
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static int my_sort_reverse( int *v1, int *v2 ) static int my_sort_reverse( int *v1, int *v2 )
{ {
return *v2-*v1; return *v2-*v1;
} }
static int my_sort( int *v1, int *v2 ) static int my_sort( int *v1, int *v2 )
{ {
return *v1-*v2; return *v1-*v2;
} }
#ifdef __WXMAC__ #ifdef __WXMAC__
#define INITIAL_NUMBER_OF_ITEMS 100 #define INITIAL_NUMBER_OF_ITEMS 100
#else #else
#define INITIAL_NUMBER_OF_ITEMS 100000 #define INITIAL_NUMBER_OF_ITEMS 100000
#endif #endif
MyListModel::MyListModel() : MyListModel::MyListModel() :
wxDataViewVirtualListModel( INITIAL_NUMBER_OF_ITEMS ) wxDataViewVirtualListModel( INITIAL_NUMBER_OF_ITEMS )
{ {
m_virtualItems = INITIAL_NUMBER_OF_ITEMS; m_virtualItems = INITIAL_NUMBER_OF_ITEMS;
// the first 100 items are really stored in this model; // the first 100 items are really stored in this model;
// all the others are synthetized on request // all the others are synthetized on request
for (unsigned int i = 0; i < 100; i++) for (unsigned int i = 0; i < 100; i++)
{ {
wxString str; wxString str;
str.Printf( wxT("real row %d"), i ); str.Printf( wxT("real row %d"), i );
m_array.Add( str ); m_array.Add( str );
} }
m_icon[0] = wxIcon( null_xpm ); m_icon[0] = wxIcon( null_xpm );
m_icon[1] = wxIcon( wx_small_xpm ); m_icon[1] = wxIcon( wx_small_xpm );
} }
void MyListModel::Prepend( const wxString &text ) void MyListModel::Prepend( const wxString &text )
{ {
m_array.Insert( text, 0 ); m_array.Insert( text, 0 );
RowPrepended(); RowPrepended();
} }
void MyListModel::DeleteItem( const wxDataViewItem &item ) void MyListModel::DeleteItem( const wxDataViewItem &item )
{ {
unsigned int row = GetRow( item ); unsigned int row = GetRow( item );
if (row >= m_array.GetCount()) if (row >= m_array.GetCount())
return; return;
m_array.RemoveAt( row ); m_array.RemoveAt( row );
RowDeleted( row ); RowDeleted( row );
} }
void MyListModel::DeleteItems( const wxDataViewItemArray &items ) void MyListModel::DeleteItems( const wxDataViewItemArray &items )
{ {
unsigned i; unsigned i;
wxArrayInt rows; wxArrayInt rows;
for (int i = 0; i < items.GetCount(); i++) for (int i = 0; i < items.GetCount(); i++)
{ {
unsigned int row = GetRow( items[i] ); unsigned int row = GetRow( items[i] );
if (row < m_array.GetCount()) if (row < m_array.GetCount())
rows.Add( row ); rows.Add( row );
} }
if (rows.GetCount() == 0) if (rows.GetCount() == 0)
{ {
// none of the selected items were in the range of the items // none of the selected items were in the range of the items
// which we store... for simplicity, don't allow removing them // which we store... for simplicity, don't allow removing them
wxLogError("Cannot remove rows with an index greater than %d", m_array.GetCount()); wxLogError("Cannot remove rows with an index greater than %d", m_array.GetCount());
return; return;
} }
// Sort in descending order so that the last // Sort in descending order so that the last
// row will be deleted first. Otherwise the // row will be deleted first. Otherwise the
// remaining indeces would all be wrong. // remaining indeces would all be wrong.
rows.Sort( my_sort_reverse ); rows.Sort( my_sort_reverse );
for (i = 0; i < rows.GetCount(); i++) for (i = 0; i < rows.GetCount(); i++)
m_array.RemoveAt( rows[i] ); m_array.RemoveAt( rows[i] );
// This is just to test if wxDataViewCtrl can // This is just to test if wxDataViewCtrl can
// cope with removing rows not sorted in // cope with removing rows not sorted in
// descending order // descending order
rows.Sort( my_sort ); rows.Sort( my_sort );
RowsDeleted( rows ); RowsDeleted( rows );
} }
void MyListModel::AddMany() void MyListModel::AddMany()
{ {
m_virtualItems += 1000; m_virtualItems += 1000;
Reset( m_array.GetCount() + m_virtualItems ); Reset( m_array.GetCount() + m_virtualItems );
} }
void MyListModel::GetValueByRow( wxVariant &variant, void MyListModel::GetValueByRow( wxVariant &variant,
unsigned int row, unsigned int col ) const unsigned int row, unsigned int col ) const
{ {
if (col==0) if (col==0)
{ {
if (row >= m_array.GetCount()) if (row >= m_array.GetCount())
variant = wxString::Format( wxT("virtual row %d"), row ); variant = wxString::Format( wxT("virtual row %d"), row );
else else
variant = m_array[ row ]; variant = m_array[ row ];
} }
else if (col==1) else if (col==1)
{ {
wxDataViewIconText data( wxT("test"), m_icon[ row%2 ] ); wxDataViewIconText data( wxT("test"), m_icon[ row%2 ] );
variant << data; variant << data;
} }
else if (col==2) else if (col==2)
{ {
if (row >= m_array.GetCount()) if (row >= m_array.GetCount())
variant = wxT("plain"); variant = wxT("plain");
else else
variant = wxT("blue/green/red"); variant = wxT("blue/green/red");
} }
} }
bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col, bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col,
wxDataViewItemAttr &attr ) wxDataViewItemAttr &attr )
{ {
if (col != 2) if (col != 2)
return false; return false;
if (row < m_array.GetCount()) if (row < m_array.GetCount())
{ {
attr.SetColour( (row%2) == 0 ? *wxBLUE : attr.SetColour( (row%2) == 0 ? *wxBLUE :
((row%3) == 0 ? *wxGREEN : *wxRED) ); ((row%3) == 0 ? *wxGREEN : *wxRED) );
attr.SetItalic( (row%2) == 5 ); attr.SetItalic( (row%2) == 5 );
} }
return true; return true;
} }
bool MyListModel::SetValueByRow( const wxVariant &variant, bool MyListModel::SetValueByRow( const wxVariant &variant,
unsigned int row, unsigned int col ) unsigned int row, unsigned int col )
{ {
if (col == 0) if (col == 0)
{ {
if (row >= m_array.GetCount()) if (row >= m_array.GetCount())
{ {
// the item is not in the range of the items // the item is not in the range of the items
// which we store... for simplicity, don't allow editing it // which we store... for simplicity, don't allow editing it
wxLogError("Cannot edit rows with an index greater than %d", m_array.GetCount()); wxLogError("Cannot edit rows with an index greater than %d", m_array.GetCount());
return false; return false;
} }
m_array[row] = variant.GetString(); m_array[row] = variant.GetString();
return true; return true;
} }
return false; return false;
} }

View File

@@ -1,216 +1,216 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: mymodels.h // Name: mymodels.h
// Purpose: wxDataViewCtrl wxWidgets sample // Purpose: wxDataViewCtrl wxWidgets sample
// Author: Robert Roebling // Author: Robert Roebling
// Modified by: Francesco Montorsi, Bo Yang // Modified by: Francesco Montorsi, Bo Yang
// Created: 06/01/06 // Created: 06/01/06
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Robert Roebling // Copyright: (c) Robert Roebling
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyMusicTreeModelNode: a node inside MyMusicTreeModel // MyMusicTreeModelNode: a node inside MyMusicTreeModel
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class MyMusicTreeModelNode; class MyMusicTreeModelNode;
WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray ); WX_DEFINE_ARRAY_PTR( MyMusicTreeModelNode*, MyMusicTreeModelNodePtrArray );
class MyMusicTreeModelNode class MyMusicTreeModelNode
{ {
public: public:
MyMusicTreeModelNode( MyMusicTreeModelNode* parent, MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
const wxString &title, const wxString &artist, const wxString &title, const wxString &artist,
unsigned int year ) unsigned int year )
{ {
m_parent = parent; m_parent = parent;
m_title = title; m_title = title;
m_artist = artist; m_artist = artist;
m_year = year; m_year = year;
m_quality = "good"; m_quality = "good";
} }
MyMusicTreeModelNode( MyMusicTreeModelNode* parent, MyMusicTreeModelNode( MyMusicTreeModelNode* parent,
const wxString &branch ) const wxString &branch )
{ {
m_parent = parent; m_parent = parent;
m_title = branch; m_title = branch;
m_year = -1; m_year = -1;
} }
~MyMusicTreeModelNode() ~MyMusicTreeModelNode()
{ {
// free all our children nodes // free all our children nodes
size_t count = m_children.GetCount(); size_t count = m_children.GetCount();
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
{ {
MyMusicTreeModelNode *child = m_children[i]; MyMusicTreeModelNode *child = m_children[i];
delete child; delete child;
} }
} }
bool IsContainer() const bool IsContainer() const
{ return m_children.GetCount()>0; } { return m_children.GetCount()>0; }
MyMusicTreeModelNode* GetParent() MyMusicTreeModelNode* GetParent()
{ return m_parent; } { return m_parent; }
MyMusicTreeModelNodePtrArray& GetChildren() MyMusicTreeModelNodePtrArray& GetChildren()
{ return m_children; } { return m_children; }
MyMusicTreeModelNode* GetNthChild( unsigned int n ) MyMusicTreeModelNode* GetNthChild( unsigned int n )
{ return m_children.Item( n ); } { return m_children.Item( n ); }
void Insert( MyMusicTreeModelNode* child, unsigned int n) void Insert( MyMusicTreeModelNode* child, unsigned int n)
{ m_children.Insert( child, n); } { m_children.Insert( child, n); }
void Append( MyMusicTreeModelNode* child ) void Append( MyMusicTreeModelNode* child )
{ m_children.Add( child ); } { m_children.Add( child ); }
unsigned int GetChildCount() const unsigned int GetChildCount() const
{ return m_children.GetCount(); } { return m_children.GetCount(); }
public: // public to avoid getters/setters public: // public to avoid getters/setters
wxString m_title; wxString m_title;
wxString m_artist; wxString m_artist;
int m_year; int m_year;
wxString m_quality; wxString m_quality;
private: private:
MyMusicTreeModelNode *m_parent; MyMusicTreeModelNode *m_parent;
MyMusicTreeModelNodePtrArray m_children; MyMusicTreeModelNodePtrArray m_children;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyMusicTreeModel // MyMusicTreeModel
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/* /*
Implement this data model Implement this data model
Title Artist Year Judgement Title Artist Year Judgement
-------------------------------------------------------------------------- --------------------------------------------------------------------------
1: My Music: 1: My Music:
2: Pop music 2: Pop music
3: You are not alone Michael Jackson 1995 good 3: You are not alone Michael Jackson 1995 good
4: Take a bow Madonna 1994 good 4: Take a bow Madonna 1994 good
5: Classical music 5: Classical music
6: Ninth Symphony Ludwig v. Beethoven 1824 good 6: Ninth Symphony Ludwig v. Beethoven 1824 good
7: German Requiem Johannes Brahms 1868 good 7: German Requiem Johannes Brahms 1868 good
*/ */
class MyMusicTreeModel: public wxDataViewModel class MyMusicTreeModel: public wxDataViewModel
{ {
public: public:
MyMusicTreeModel(); MyMusicTreeModel();
~MyMusicTreeModel() ~MyMusicTreeModel()
{ {
delete m_root; delete m_root;
} }
// helper method for wxLog // helper method for wxLog
wxString GetTitle( const wxDataViewItem &item ) const; wxString GetTitle( const wxDataViewItem &item ) const;
int GetYear( const wxDataViewItem &item ) const; int GetYear( const wxDataViewItem &item ) const;
// helper methods to change the model // helper methods to change the model
void AddToClassical( const wxString &title, const wxString &artist, void AddToClassical( const wxString &title, const wxString &artist,
unsigned int year ); unsigned int year );
void Delete( const wxDataViewItem &item ); void Delete( const wxDataViewItem &item );
wxDataViewItem GetNinthItem() const wxDataViewItem GetNinthItem() const
{ {
return wxDataViewItem( m_ninth ); return wxDataViewItem( m_ninth );
} }
// override sorting to always sort branches ascendingly // override sorting to always sort branches ascendingly
int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
unsigned int column, bool ascending ); unsigned int column, bool ascending );
// implementation of base class virtuals to define model // implementation of base class virtuals to define model
virtual unsigned int GetColumnCount() const virtual unsigned int GetColumnCount() const
{ {
return 6; return 6;
} }
virtual wxString GetColumnType( unsigned int col ) const virtual wxString GetColumnType( unsigned int col ) const
{ {
if (col == 2) if (col == 2)
return wxT("long"); return wxT("long");
return wxT("string"); return wxT("string");
} }
virtual void GetValue( wxVariant &variant, virtual void GetValue( wxVariant &variant,
const wxDataViewItem &item, unsigned int col ) const; const wxDataViewItem &item, unsigned int col ) const;
virtual bool SetValue( const wxVariant &variant, virtual bool SetValue( const wxVariant &variant,
const wxDataViewItem &item, unsigned int col ); const wxDataViewItem &item, unsigned int col );
virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const; virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const;
virtual bool IsContainer( const wxDataViewItem &item ) const; virtual bool IsContainer( const wxDataViewItem &item ) const;
virtual unsigned int GetChildren( const wxDataViewItem &parent, virtual unsigned int GetChildren( const wxDataViewItem &parent,
wxDataViewItemArray &array ) const; wxDataViewItemArray &array ) const;
private: private:
MyMusicTreeModelNode* m_root; MyMusicTreeModelNode* m_root;
// pointers to some "special" nodes of the tree: // pointers to some "special" nodes of the tree:
MyMusicTreeModelNode* m_pop; MyMusicTreeModelNode* m_pop;
MyMusicTreeModelNode* m_classical; MyMusicTreeModelNode* m_classical;
MyMusicTreeModelNode* m_ninth; MyMusicTreeModelNode* m_ninth;
// ?? // ??
bool m_classicalMusicIsKnownToControl; bool m_classicalMusicIsKnownToControl;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyListModel // MyListModel
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class MyListModel: public wxDataViewVirtualListModel class MyListModel: public wxDataViewVirtualListModel
{ {
public: public:
MyListModel(); MyListModel();
// helper methods to change the model // helper methods to change the model
void Prepend( const wxString &text ); void Prepend( const wxString &text );
void DeleteItem( const wxDataViewItem &item ); void DeleteItem( const wxDataViewItem &item );
void DeleteItems( const wxDataViewItemArray &items ); void DeleteItems( const wxDataViewItemArray &items );
void AddMany(); void AddMany();
// implementation of base class virtuals to define model // implementation of base class virtuals to define model
virtual unsigned int GetColumnCount() const virtual unsigned int GetColumnCount() const
{ {
return 3; return 3;
} }
virtual wxString GetColumnType( unsigned int col ) const virtual wxString GetColumnType( unsigned int col ) const
{ {
if (col == 1) if (col == 1)
return wxT("wxDataViewIconText"); return wxT("wxDataViewIconText");
return wxT("string"); return wxT("string");
} }
virtual unsigned int GetRowCount() virtual unsigned int GetRowCount()
{ {
return m_array.GetCount(); return m_array.GetCount();
} }
virtual void GetValueByRow( wxVariant &variant, virtual void GetValueByRow( wxVariant &variant,
unsigned int row, unsigned int col ) const; unsigned int row, unsigned int col ) const;
virtual bool GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ); virtual bool GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr );
virtual bool SetValueByRow( const wxVariant &variant, virtual bool SetValueByRow( const wxVariant &variant,
unsigned int row, unsigned int col ); unsigned int row, unsigned int col );
private: private:
wxArrayString m_array; wxArrayString m_array;
wxIcon m_icon[2]; wxIcon m_icon[2];
int m_virtualItems; int m_virtualItems;
}; };