Update sample to to show DataViewModel.HasValue usage

This commit is contained in:
Robin Dunn
2020-04-15 12:23:50 -07:00
parent a00c8ff447
commit c84aa19c55

View File

@@ -159,6 +159,16 @@ class MyTreeListModel(dv.PyDataViewModel):
return self.ObjectToItem(g)
def HasValue(self, item, col):
# Overriding this method allows you to let the view know if there is any
# data at all in the cell. If it returns False then GetValue will not be
# called for this item and column.
node = self.ItemToObject(item)
if isinstance(node, Genre) and col > 0:
return False
return True
def GetValue(self, item, col):
# Return the value to be displayed for this item and column. For this
# example we'll just pull the values from the data objects we
@@ -168,17 +178,11 @@ class MyTreeListModel(dv.PyDataViewModel):
node = self.ItemToObject(item)
if isinstance(node, Genre):
# We'll only use the first column for the Genre objects,
# for the other columns lets just return empty values
mapper = { 0 : node.name,
1 : "",
2 : "",
3 : "",
4 : wx.DateTime.FromTimeT(0), # TODO: There should be some way to indicate a null value...
5 : False,
}
return mapper[col]
# Due to the HasValue implementation above, GetValue should only
# be called for the first column for Genre objects. We'll verify
# that with this assert.
assert col == 0, "Unexpected column value for Genre objects"
return node.name
elif isinstance(node, Song):
mapper = { 0 : node.genre,