From 0c944ff5bed80420c9916af355aee0593561c3f5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 1 Dec 2021 02:24:45 +0100 Subject: [PATCH] Test returning invalid value from GetValue() in dataview sample Add an item with unspecified year and update the custom model GetValue() to not return anything in this case to check, and confirm, that all implementations handle this properly by simply not showing anything in the cell in this case. --- samples/dataview/mymodels.cpp | 20 +++++++++++++++++--- samples/dataview/mymodels.h | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index 609c749fa0..d6a4149459 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -40,6 +40,8 @@ MyMusicTreeModel::MyMusicTreeModel() m_pop = new MyMusicTreeModelNode( m_root, "Pop music" ); m_pop->Append( new MyMusicTreeModelNode( m_pop, "You are not alone", "Michael Jackson", 1995 ) ); + m_pop->Append( + new MyMusicTreeModelNode( m_pop, "Yesterday", "The Beatles", -1 /* not specified */ ) ); m_pop->Append( new MyMusicTreeModelNode( m_pop, "Take a bow", "Madonna", 1994 ) ); m_root->Append( m_pop ); @@ -193,7 +195,8 @@ void MyMusicTreeModel::GetValue( wxVariant &variant, variant = node->m_artist; break; case 2: - variant = (long) node->m_year; + if (node->m_year != -1) + variant = (long) node->m_year; break; case 3: variant = node->m_quality; @@ -202,7 +205,9 @@ void MyMusicTreeModel::GetValue( wxVariant &variant, variant = 80L; // all music is very 80% popular break; case 5: - if (GetYear(item) < 1900) + if (node->m_year == -1) + variant = "n/a"; + else if (node->m_year < 1900) variant = "old"; else variant = "new"; @@ -248,7 +253,16 @@ bool MyMusicTreeModel::IsEnabled( const wxDataViewItem &item, MyMusicTreeModelNode *node = (MyMusicTreeModelNode*) item.GetID(); // disable Beethoven's ratings, his pieces can only be good - return !(col == 3 && node->m_artist.EndsWith("Beethoven")); + if (col == 3 && node->m_artist.EndsWith("Beethoven")) + return false; + + // also disable editing the year when it's not specified, this doesn't work + // because the editor needs some initial value + if (col == 2 && node->m_year == -1) + return false; + + // otherwise allow editing + return true; } wxDataViewItem MyMusicTreeModel::GetParent( const wxDataViewItem &item ) const diff --git a/samples/dataview/mymodels.h b/samples/dataview/mymodels.h index a81c82e28c..59733ac61c 100644 --- a/samples/dataview/mymodels.h +++ b/samples/dataview/mymodels.h @@ -26,7 +26,7 @@ class MyMusicTreeModelNode public: MyMusicTreeModelNode( MyMusicTreeModelNode* parent, const wxString &title, const wxString &artist, - unsigned int year ) + int year ) { m_parent = parent;