Better sorting example and better default

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@47562 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling 2007-07-19 14:03:43 +00:00
parent 4f167b46b4
commit cf283a470a
2 changed files with 50 additions and 31 deletions

View File

@ -167,6 +167,31 @@ public:
ItemDeleted( item );
}
// override sorting to always sort branches ascendingly
int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
{
if (HasChildren(item1) && HasChildren(item2))
{
wxVariant value1,value2;
GetValue( value1, item1, 0 );
GetValue( value2, item2, 0 );
wxString str1 = value1.GetString();
wxString str2 = value2.GetString();
int res = str1.Cmp( str2 );
if (res) return res;
// items must be different
unsigned long litem1 = (unsigned long) item1.GetID();
unsigned long litem2 = (unsigned long) item2.GetID();
return litem1-litem2;
}
return wxDataViewModel::Compare( item1, item2 );
}
// implementation of base class virtuals to define model
virtual unsigned int GetColumnCount() const

View File

@ -147,10 +147,19 @@ void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
{
// sort branches before leaves
bool item1_has_children = HasChildren(item1);
bool item2_has_children = HasChildren(item2);
if (item1_has_children && !item2_has_children)
return 1;
if (item2_has_children && !item1_has_children)
return -1;
wxVariant value1,value2;
GetValue( value1, item1, m_sortingColumn );
GetValue( value2, item2, m_sortingColumn );
if (!m_ascending)
{
wxVariant temp = value1;
@ -163,52 +172,37 @@ int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem
wxString str1 = value1.GetString();
wxString str2 = value2.GetString();
int res = str1.Cmp( str2 );
if (res == 0)
{
// no difference, try 0th column
if (m_sortingColumn != 0)
{
unsigned int temp = m_sortingColumn;
m_sortingColumn = 0;
res = Compare( item1, item2 );
m_sortingColumn = temp;
}
if (res == 0)
{
// still no difference, resort to desparate non-sense
long l1 = (long) item1.GetID();
long l2 = (long) item2.GetID();
return l1-l2;
}
}
return res;
}
if (res) return res;
} else
if (value1.GetType() == wxT("long"))
{
long l1 = value1.GetLong();
long l2 = value2.GetLong();
return l1-l2;
}
long res = l1-l2;
if (res) return res;
} else
if (value1.GetType() == wxT("double"))
{
double d1 = value1.GetDouble();
double d2 = value2.GetDouble();
if (d1 == d2) return 0;
if (d1 < d2) return 1;
return -1;
}
if (d1 > d2) return -1;
} else
if (value1.GetType() == wxT("datetime"))
{
wxDateTime dt1 = value1.GetDateTime();
wxDateTime dt2 = value2.GetDateTime();
if (dt1.IsEqualTo(dt2)) return 0;
if (dt1.IsEarlierThan(dt2)) return 1;
return -1;
if (dt2.IsEarlierThan(dt1)) return -11;
}
return 0;
// items must be different
unsigned long litem1 = (unsigned long) item1.GetID();
unsigned long litem2 = (unsigned long) item2.GetID();
if (!m_ascending)
return litem2-litem1;
return litem1-litem2;
}
// ---------------------------------------------------------