all code revisited.
Main additions: - now it's possible to change the DVC style dynamically - make models more robust against deleting items, and against other allowed user actions - comment portions of the code to make it more didactive git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59323 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "null.xpm"
|
||||
#include "wx_small.xpm"
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -38,26 +39,31 @@
|
||||
MyMusicTreeModel::MyMusicTreeModel()
|
||||
{
|
||||
m_root = new MyMusicTreeModelNode( NULL, wxT("My Music" ));
|
||||
|
||||
// setup pop music
|
||||
m_pop = new MyMusicTreeModelNode( m_root, wxT("Pop music") );
|
||||
m_pop->Append(
|
||||
new MyMusicTreeModelNode( m_pop, wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
|
||||
m_pop->Append(
|
||||
new MyMusicTreeModelNode( m_pop, wxT("Take a bow"), wxT("Madonna"), 1994 ) );
|
||||
m_root->Append( m_pop );
|
||||
m_pop->Append( new MyMusicTreeModelNode( m_pop,
|
||||
wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) );
|
||||
m_pop->Append( new MyMusicTreeModelNode( m_pop,
|
||||
wxT("Take a bow"), wxT("Madonna"), 1994 ) );
|
||||
|
||||
// setup classical music
|
||||
m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") );
|
||||
m_root->Append( m_classical );
|
||||
m_ninth = new MyMusicTreeModelNode( m_classical,
|
||||
wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 );
|
||||
m_ninth = new MyMusicTreeModelNode( m_classical, wxT("Ninth symphony"),
|
||||
wxT("Ludwig van Beethoven"), 1824 );
|
||||
m_classical->Append( m_ninth );
|
||||
m_classical->Append( new MyMusicTreeModelNode( m_classical,
|
||||
wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) );
|
||||
m_classical->Append( new MyMusicTreeModelNode( m_classical, wxT("German Requiem"),
|
||||
wxT("Johannes Brahms"), 1868 ) );
|
||||
m_root->Append( m_classical );
|
||||
|
||||
m_classicalMusicIsKnownToControl = false;
|
||||
}
|
||||
|
||||
wxString MyMusicTreeModel::GetTitle( const wxDataViewItem &item ) const
|
||||
{
|
||||
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
|
||||
if (!node)
|
||||
if (!node) // happens if item.IsOk()==false
|
||||
return wxEmptyString;
|
||||
|
||||
return node->m_title;
|
||||
@@ -66,18 +72,27 @@ wxString MyMusicTreeModel::GetTitle( const wxDataViewItem &item ) const
|
||||
int MyMusicTreeModel::GetYear( const wxDataViewItem &item ) const
|
||||
{
|
||||
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
|
||||
if (!node)
|
||||
if (!node) // happens if item.IsOk()==false
|
||||
return 2000;
|
||||
|
||||
return node->m_year;
|
||||
}
|
||||
|
||||
void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &artist, int year )
|
||||
void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &artist,
|
||||
unsigned int year )
|
||||
{
|
||||
// add to data
|
||||
if (!m_classical)
|
||||
{
|
||||
wxASSERT(m_root);
|
||||
|
||||
// it was removed: restore it
|
||||
m_classical = new MyMusicTreeModelNode( m_root, wxT("Classical music") );
|
||||
m_root->Append( m_classical );
|
||||
}
|
||||
|
||||
// add to the classical music node a new node:
|
||||
MyMusicTreeModelNode *child_node =
|
||||
new MyMusicTreeModelNode( m_classical, title, artist, year );
|
||||
|
||||
m_classical->Append( child_node );
|
||||
|
||||
if (m_classicalMusicIsKnownToControl)
|
||||
@@ -92,9 +107,33 @@ void MyMusicTreeModel::AddToClassical( const wxString &title, const wxString &ar
|
||||
void MyMusicTreeModel::Delete( const wxDataViewItem &item )
|
||||
{
|
||||
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
|
||||
wxDataViewItem parent( node->GetParent() );
|
||||
if (!node) // happens if item.IsOk()==false
|
||||
return;
|
||||
|
||||
wxDataViewItem parent( node->GetParent() );
|
||||
if (!parent.IsOk())
|
||||
{
|
||||
wxASSERT(node == m_root);
|
||||
|
||||
// don't make the control completely empty:
|
||||
wxLogError("Cannot remove the root item!");
|
||||
return;
|
||||
}
|
||||
|
||||
// is the node one of those we keep stored in special pointers?
|
||||
if (node == m_pop)
|
||||
m_pop = NULL;
|
||||
else if (node == m_classical)
|
||||
m_classical = NULL;
|
||||
else if (node == m_ninth)
|
||||
m_ninth = NULL;
|
||||
|
||||
// first remove the node from the parent's array of children;
|
||||
// NOTE: MyMusicTreeModelNodePtrArray is only an array of _pointers_
|
||||
// thus removing the node from it doesn't result in freeing it
|
||||
node->GetParent()->GetChildren().Remove( node );
|
||||
|
||||
// free the node
|
||||
delete node;
|
||||
|
||||
// notify control
|
||||
@@ -104,9 +143,12 @@ void MyMusicTreeModel::Delete( const wxDataViewItem &item )
|
||||
int MyMusicTreeModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
|
||||
unsigned int column, bool ascending )
|
||||
{
|
||||
wxASSERT(item1.IsOk() && item2.IsOk());
|
||||
// should never happen
|
||||
|
||||
if (IsContainer(item1) && IsContainer(item2))
|
||||
{
|
||||
wxVariant value1,value2;
|
||||
wxVariant value1, value2;
|
||||
GetValue( value1, item1, 0 );
|
||||
GetValue( value2, item2, 0 );
|
||||
|
||||
@@ -128,60 +170,73 @@ int MyMusicTreeModel::Compare( const wxDataViewItem &item1, const wxDataViewItem
|
||||
void MyMusicTreeModel::GetValue( wxVariant &variant,
|
||||
const wxDataViewItem &item, unsigned int col ) const
|
||||
{
|
||||
wxASSERT(item.IsOk());
|
||||
|
||||
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
|
||||
switch (col)
|
||||
{
|
||||
case 0: variant = node->m_title; break;
|
||||
case 1: variant = node->m_artist; break;
|
||||
case 2: variant = (long) node->m_year; break;
|
||||
case 3: variant = node->m_quality; break;
|
||||
case 4:
|
||||
// wxMac doesn't conceal the popularity progress renderer, return 0 for containers
|
||||
if (IsContainer(item))
|
||||
variant = (long) 0;
|
||||
else
|
||||
variant = (long) 80; // all music is very 80% popular
|
||||
break;
|
||||
case 5:
|
||||
// Make size of red square depend on year
|
||||
if (GetYear(item) < 1900)
|
||||
variant = (long) 35;
|
||||
else
|
||||
variant = (long) 25;
|
||||
break;
|
||||
default:
|
||||
{
|
||||
wxLogError( wxT("MyMusicTreeModel::GetValue: wrong column %d"), col );
|
||||
case 0:
|
||||
variant = node->m_title;
|
||||
break;
|
||||
case 1:
|
||||
variant = node->m_artist;
|
||||
break;
|
||||
case 2:
|
||||
variant = (long) node->m_year;
|
||||
break;
|
||||
case 3:
|
||||
variant = node->m_quality;
|
||||
break;
|
||||
case 4:
|
||||
// wxMac doesn't conceal the popularity progress renderer, return 0 for containers
|
||||
if (IsContainer(item))
|
||||
variant = (long) 0;
|
||||
else
|
||||
variant = (long) 80; // all music is very 80% popular
|
||||
break;
|
||||
case 5:
|
||||
// Make size of red square depend on year
|
||||
if (GetYear(item) < 1900)
|
||||
variant = (long) 35;
|
||||
else
|
||||
variant = (long) 25;
|
||||
break;
|
||||
|
||||
// provoke a crash when mouse button down
|
||||
wxMouseState state = wxGetMouseState();
|
||||
if (state.ShiftDown())
|
||||
{
|
||||
char *crash = 0;
|
||||
*crash = 0;
|
||||
}
|
||||
}
|
||||
default:
|
||||
wxLogError( wxT("MyMusicTreeModel::GetValue: wrong column %d"), col );
|
||||
}
|
||||
}
|
||||
|
||||
bool MyMusicTreeModel::SetValue( const wxVariant &variant,
|
||||
const wxDataViewItem &item, unsigned int col )
|
||||
{
|
||||
wxASSERT(item.IsOk());
|
||||
|
||||
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID();
|
||||
switch (col)
|
||||
{
|
||||
case 0: node->m_title = variant.GetString(); return true;
|
||||
case 1: node->m_artist = variant.GetString(); return true;
|
||||
case 2: node->m_year = variant.GetLong(); return true;
|
||||
case 3: node->m_quality = variant.GetString(); return true;
|
||||
default: wxLogError( wxT("MyMusicTreeModel::SetValue: wrong column") );
|
||||
case 0:
|
||||
node->m_title = variant.GetString();
|
||||
return true;
|
||||
case 1:
|
||||
node->m_artist = variant.GetString();
|
||||
return true;
|
||||
case 2:
|
||||
node->m_year = variant.GetLong();
|
||||
return true;
|
||||
case 3:
|
||||
node->m_quality = variant.GetString();
|
||||
return true;
|
||||
|
||||
default:
|
||||
wxLogError( wxT("MyMusicTreeModel::SetValue: wrong column") );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
wxDataViewItem MyMusicTreeModel::GetParent( const wxDataViewItem &item ) const
|
||||
{
|
||||
// the invisble root node has no parent
|
||||
// the invisible root node has no parent
|
||||
if (!item.IsOk())
|
||||
return wxDataViewItem(0);
|
||||
|
||||
@@ -196,8 +251,8 @@ wxDataViewItem MyMusicTreeModel::GetParent( const wxDataViewItem &item ) const
|
||||
|
||||
bool MyMusicTreeModel::IsContainer( const wxDataViewItem &item ) const
|
||||
{
|
||||
// the invisble root node can have children (in
|
||||
// our model always "MyMusic")
|
||||
// the invisble root node can have children
|
||||
// (in our model always "MyMusic")
|
||||
if (!item.IsOk())
|
||||
return true;
|
||||
|
||||
@@ -205,7 +260,8 @@ bool MyMusicTreeModel::IsContainer( const wxDataViewItem &item ) const
|
||||
return node->IsContainer();
|
||||
}
|
||||
|
||||
unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const
|
||||
unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent,
|
||||
wxDataViewItemArray &array ) const
|
||||
{
|
||||
MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) parent.GetID();
|
||||
if (!node)
|
||||
@@ -226,12 +282,12 @@ unsigned int MyMusicTreeModel::GetChildren( const wxDataViewItem &parent, wxData
|
||||
}
|
||||
|
||||
unsigned int count = node->GetChildren().GetCount();
|
||||
unsigned int pos;
|
||||
for (pos = 0; pos < count; pos++)
|
||||
for (unsigned int pos = 0; pos < count; pos++)
|
||||
{
|
||||
MyMusicTreeModelNode *child = node->GetChildren().Item( pos );
|
||||
array.Add( wxDataViewItem( (void*) child ) );
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@@ -251,28 +307,28 @@ static int my_sort( int *v1, int *v2 )
|
||||
return *v1-*v2;
|
||||
}
|
||||
|
||||
MyListModel::MyListModel() :
|
||||
#ifdef __WXMAC__
|
||||
wxDataViewVirtualListModel( 1000 + 100 )
|
||||
#define INITIAL_NUMBER_OF_ITEMS 100
|
||||
#else
|
||||
wxDataViewVirtualListModel( 100000 + 100 )
|
||||
#endif
|
||||
{
|
||||
#ifdef __WXMAC__
|
||||
m_virtualItems = 1000;
|
||||
#else
|
||||
m_virtualItems = 100000;
|
||||
#define INITIAL_NUMBER_OF_ITEMS 100000
|
||||
#endif
|
||||
|
||||
unsigned int i;
|
||||
for (i = 0; i < 100; i++)
|
||||
MyListModel::MyListModel() :
|
||||
wxDataViewVirtualListModel( INITIAL_NUMBER_OF_ITEMS )
|
||||
{
|
||||
m_virtualItems = INITIAL_NUMBER_OF_ITEMS;
|
||||
|
||||
// the first 100 items are really stored in this model;
|
||||
// all the others are synthetized on request
|
||||
for (unsigned int i = 0; i < 100; i++)
|
||||
{
|
||||
wxString str;
|
||||
str.Printf( wxT("row number %d"), i );
|
||||
str.Printf( wxT("real row %d"), i );
|
||||
m_array.Add( str );
|
||||
}
|
||||
|
||||
m_icon = wxIcon( null_xpm );
|
||||
m_icon[0] = wxIcon( null_xpm );
|
||||
m_icon[1] = wxIcon( wx_small_xpm );
|
||||
}
|
||||
|
||||
void MyListModel::Prepend( const wxString &text )
|
||||
@@ -294,14 +350,21 @@ void MyListModel::DeleteItem( const wxDataViewItem &item )
|
||||
void MyListModel::DeleteItems( const wxDataViewItemArray &items )
|
||||
{
|
||||
wxArrayInt rows;
|
||||
unsigned int i;
|
||||
for (i = 0; i < items.GetCount(); i++)
|
||||
for (unsigned int i = 0; i < items.GetCount(); i++)
|
||||
{
|
||||
unsigned int row = GetRow( items[i] );
|
||||
if (row < m_array.GetCount())
|
||||
rows.Add( row );
|
||||
}
|
||||
|
||||
if (rows.GetCount() == 0)
|
||||
{
|
||||
// none of the selected items were in the range of the items
|
||||
// which we store... for simplicity, don't allow removing them
|
||||
wxLogError("Cannot remove rows with an index greater than %d", m_array.GetCount());
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort in descending order so that the last
|
||||
// row will be deleted first. Otherwise the
|
||||
// remaining indeces would all be wrong.
|
||||
@@ -328,39 +391,35 @@ void MyListModel::GetValueByRow( wxVariant &variant,
|
||||
if (col==0)
|
||||
{
|
||||
if (row >= m_array.GetCount())
|
||||
{
|
||||
wxString str;
|
||||
str.Printf(wxT("row %d"), row - m_array.GetCount() );
|
||||
variant = str;
|
||||
}
|
||||
variant = wxString::Format( wxT("virtual row %d"), row );
|
||||
else
|
||||
{
|
||||
variant = m_array[ row ];
|
||||
}
|
||||
} else
|
||||
if (col==1)
|
||||
}
|
||||
else if (col==1)
|
||||
{
|
||||
wxDataViewIconText data( wxT("test"), m_icon );
|
||||
wxDataViewIconText data( wxT("test"), m_icon[ row%2 ] );
|
||||
variant << data;
|
||||
} else
|
||||
if (col==2)
|
||||
}
|
||||
else if (col==2)
|
||||
{
|
||||
if (row >= m_array.GetCount())
|
||||
variant = wxT("plain");
|
||||
else
|
||||
variant = wxT("blue");
|
||||
variant = wxT("blue/green/red");
|
||||
}
|
||||
}
|
||||
|
||||
bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr )
|
||||
bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col,
|
||||
wxDataViewItemAttr &attr )
|
||||
{
|
||||
if (col != 2)
|
||||
return false;
|
||||
|
||||
if (row < m_array.GetCount())
|
||||
{
|
||||
attr.SetColour( *wxBLUE );
|
||||
attr.SetItalic( true );
|
||||
attr.SetColour( (row%2) == 0 ? *wxBLUE :
|
||||
((row%3) == 0 ? *wxGREEN : *wxRED) );
|
||||
attr.SetItalic( (row%2) == 5 );
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -372,7 +431,12 @@ bool MyListModel::SetValueByRow( const wxVariant &variant,
|
||||
if (col == 0)
|
||||
{
|
||||
if (row >= m_array.GetCount())
|
||||
{
|
||||
// the item is not in the range of the items
|
||||
// which we store... for simplicity, don't allow editing it
|
||||
wxLogError("Cannot edit rows with an index greater than %d", m_array.GetCount());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_array[row] = variant.GetString();
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user