mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2025-12-16 01:30:07 +01:00
Add the rest of the dataview classes, lots of unittests and supporting helpers and MappedTypes.
Update existing dataview and variant classes and MappedTypes to add missing features and such needed for Classic compatibility and full coverage of the API. Add some samples ported from the Classic demo so the DVC and related classes can be seen in action. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@72974 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
167
samples/dataview/CustomRenderer.py
Normal file
167
samples/dataview/CustomRenderer.py
Normal file
@@ -0,0 +1,167 @@
|
||||
import sys
|
||||
import wx
|
||||
import wx.dataview as dv
|
||||
|
||||
#import os; print 'PID:', os.getpid(); raw_input("Press enter...")
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
class MyCustomRenderer(dv.DataViewCustomRenderer):
|
||||
def __init__(self, log, *args, **kw):
|
||||
dv.DataViewCustomRenderer.__init__(self, *args, **kw)
|
||||
self.log = log
|
||||
self.value = None
|
||||
|
||||
def SetValue(self, value):
|
||||
#self.log.write('SetValue: %s' % value)
|
||||
self.value = value
|
||||
return True
|
||||
|
||||
def GetValue(self):
|
||||
#self.log.write('GetValue')
|
||||
return self.value
|
||||
|
||||
def GetSize(self):
|
||||
# Return the size needed to display the value. The renderer
|
||||
# has a helper function we can use for measuring text that is
|
||||
# aware of any custom attributes that may have been set for
|
||||
# this item.
|
||||
size = self.GetTextExtent(self.value)
|
||||
return size
|
||||
|
||||
|
||||
def Render(self, rect, dc, state):
|
||||
if state != 0:
|
||||
self.log.write('Render: %s, %d\n' % (rect, state))
|
||||
|
||||
if not state & dv.DATAVIEW_CELL_SELECTED:
|
||||
# we'll draw a shaded background to see if the rect correctly
|
||||
# fills the cell
|
||||
dc.SetBrush(wx.Brush('light grey'))
|
||||
dc.SetPen(wx.TRANSPARENT_PEN)
|
||||
rect.Deflate(1, 1)
|
||||
dc.DrawRoundedRectangle(rect, 2)
|
||||
|
||||
# And then finish up with this helper function that draws the
|
||||
# text for us, dealing with alignment, font and color
|
||||
# attributes, etc
|
||||
self.RenderText(self.value,
|
||||
4, # x-offset, to compensate for the rounded rectangles
|
||||
rect,
|
||||
dc,
|
||||
state # wxDataViewCellRenderState flags
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
# The HasEditorCtrl, CreateEditorCtrl and GetValueFromEditorCtrl
|
||||
# methods need to be implemented if this renderer is going to
|
||||
# support in-place editing of the cell value, otherwise they can
|
||||
# be omitted.
|
||||
|
||||
def HasEditorCtrl(self):
|
||||
self.log.write('HasEditorCtrl')
|
||||
return True
|
||||
|
||||
|
||||
def CreateEditorCtrl(self, parent, labelRect, value):
|
||||
self.log.write('CreateEditorCtrl: %s' % labelRect)
|
||||
ctrl = wx.TextCtrl(parent,
|
||||
value=value,
|
||||
pos=labelRect.Position,
|
||||
size=labelRect.Size)
|
||||
|
||||
# select the text and put the caret at the end
|
||||
ctrl.SetInsertionPointEnd()
|
||||
ctrl.SelectAll()
|
||||
|
||||
return ctrl
|
||||
|
||||
|
||||
def GetValueFromEditorCtrl(self, editor):
|
||||
self.log.write('GetValueFromEditorCtrl: %s' % editor)
|
||||
value = editor.GetValue()
|
||||
return value
|
||||
|
||||
|
||||
# The LeftClick and Activate methods serve as notifications
|
||||
# letting you know that the user has either clicked or
|
||||
# double-clicked on an item. Implementing them in your renderer
|
||||
# is optional.
|
||||
|
||||
def LeftClick(self, pos, cellRect, model, item, col):
|
||||
self.log.write('LeftClick')
|
||||
return False
|
||||
|
||||
|
||||
def Activate(self, cellRect, model, item, col):
|
||||
self.log.write('Activate')
|
||||
return False
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
# To help focus this sample on the custom renderer, we'll reuse the
|
||||
# model class from another sample.
|
||||
from IndexListModel import TestModel
|
||||
|
||||
|
||||
|
||||
class TestPanel(wx.Panel):
|
||||
def __init__(self, parent, log, model=None, data=None):
|
||||
self.log = log
|
||||
wx.Panel.__init__(self, parent, -1)
|
||||
|
||||
# Create a dataview control
|
||||
self.dvc = dv.DataViewCtrl(self, style=wx.BORDER_THEME
|
||||
| dv.DV_ROW_LINES
|
||||
#| dv.DV_HORIZ_RULES
|
||||
| dv.DV_VERT_RULES
|
||||
| dv.DV_MULTIPLE
|
||||
)
|
||||
|
||||
# Create an instance of the model
|
||||
if model is None:
|
||||
self.model = TestModel(data, log)
|
||||
else:
|
||||
self.model = model
|
||||
self.dvc.AssociateModel(self.model)
|
||||
|
||||
# Now we create some columns.
|
||||
c0 = self.dvc.AppendTextColumn("Id", 0, width=40)
|
||||
c0.Alignment = wx.ALIGN_RIGHT
|
||||
c0.MinWidth = 40
|
||||
|
||||
# We'll use our custom renderer for these columns
|
||||
for title, col, width in [ ('Artist', 1, 170),
|
||||
('Title', 2, 260),
|
||||
('Genre', 3, 80)]:
|
||||
renderer = MyCustomRenderer(self.log, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
column = dv.DataViewColumn(title, renderer, col, width=width)
|
||||
column.Alignment = wx.ALIGN_LEFT
|
||||
self.dvc.AppendColumn(column)
|
||||
|
||||
# Layout
|
||||
self.Sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
self.Sizer.Add(self.dvc, 1, wx.EXPAND)
|
||||
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
def main():
|
||||
from data import musicdata
|
||||
|
||||
app = wx.App()
|
||||
frm = wx.Frame(None, title="CustomRenderer sample", size=(700,500))
|
||||
pnl = TestPanel(frm, sys.stdout, data=musicdata)
|
||||
frm.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
307
samples/dataview/DataViewModel.py
Normal file
307
samples/dataview/DataViewModel.py
Normal file
@@ -0,0 +1,307 @@
|
||||
import sys
|
||||
import wx
|
||||
import wx.dataview as dv
|
||||
|
||||
import random
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
def makeBlank(self):
|
||||
# Just a little helper function to make an empty image for our
|
||||
# model to use.
|
||||
empty = wx.EmptyBitmap(16,16,32)
|
||||
dc = wx.MemoryDC(empty)
|
||||
dc.SetBackground(wx.Brush((0,0,0,0)))
|
||||
dc.Clear()
|
||||
del dc
|
||||
return empty
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
# We'll use instaces of these classes to hold our music data. Items in the
|
||||
# tree will get associated back to the coresponding Song or Genre object.
|
||||
|
||||
class Song(object):
|
||||
def __init__(self, id, artist, title, genre):
|
||||
self.id = id
|
||||
self.artist = artist
|
||||
self.title = title
|
||||
self.genre = genre
|
||||
self.like = False
|
||||
# get a random date value
|
||||
d = random.choice(range(27))+1
|
||||
m = random.choice(range(12))
|
||||
y = random.choice(range(1980, 2005))
|
||||
self.date = wx.DateTime.FromDMY(d,m,y)
|
||||
|
||||
def __repr__(self):
|
||||
return 'Song: %s-%s' % (self.artist, self.title)
|
||||
|
||||
|
||||
class Genre(object):
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.songs = []
|
||||
|
||||
def __repr__(self):
|
||||
return 'Genre: ' + self.name
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
# This model acts as a bridge between the DataViewCtrl and the music data, and
|
||||
# organizes it hierarchically as a collection of Genres, each of which is a
|
||||
# collection of songs.
|
||||
|
||||
# This model provides these data columns:
|
||||
#
|
||||
# 0. Genre: string
|
||||
# 1. Artist: string
|
||||
# 2. Title: string
|
||||
# 3. id: integer
|
||||
# 4. Aquired: date
|
||||
# 5. Liked: bool
|
||||
#
|
||||
|
||||
class MyTreeListModel(dv.PyDataViewModel):
|
||||
def __init__(self, data, log):
|
||||
dv.PyDataViewModel.__init__(self)
|
||||
self.data = data
|
||||
self.log = log
|
||||
|
||||
# The PyDataViewModel derives from both DataViewModel and from
|
||||
# DataViewItemObjectMapper, which has methods that help associate
|
||||
# data view items with Python objects. Normally a dictionary is used
|
||||
# so any Python object can be used as data nodes. If the data nodes
|
||||
# are weak-referencable then the objmapper can use a
|
||||
# WeakValueDictionary instead.
|
||||
self.UseWeakRefs(True)
|
||||
|
||||
|
||||
# Report how many columns this model provides data for.
|
||||
def GetColumnCount(self):
|
||||
return 6
|
||||
|
||||
|
||||
def GetChildren(self, parent, children):
|
||||
# The view calls this method to find the children of any node in the
|
||||
# control. There is an implicit hidden root node, and the top level
|
||||
# item(s) should be reported as children of this node. A List view
|
||||
# simply provides all items as children of this hidden root. A Tree
|
||||
# view adds additional items as children of the other items, as needed,
|
||||
# to provide the tree hierachy.
|
||||
|
||||
# If the parent item is invalid then it represents the hidden root
|
||||
# item, so we'll use the genre objects as its children and they will
|
||||
# end up being the collection of visible roots in our tree.
|
||||
if not parent:
|
||||
for genre in self.data:
|
||||
children.append(self.ObjectToItem(genre))
|
||||
return len(self.data)
|
||||
|
||||
# Otherwise we'll fetch the python object associated with the parent
|
||||
# item and make DV items for each of it's child objects.
|
||||
node = self.ItemToObject(parent)
|
||||
if isinstance(node, Genre):
|
||||
for song in node.songs:
|
||||
children.append(self.ObjectToItem(song))
|
||||
return len(node.songs)
|
||||
return 0
|
||||
|
||||
|
||||
def IsContainer(self, item):
|
||||
# Return True if the item has children, False otherwise.
|
||||
|
||||
# The hidden root is a container
|
||||
if not item:
|
||||
return True
|
||||
# and in this model the genre objects are containers
|
||||
node = self.ItemToObject(item)
|
||||
if isinstance(node, Genre):
|
||||
return True
|
||||
# but everything else (the song objects) are not
|
||||
return False
|
||||
|
||||
|
||||
def GetParent(self, item):
|
||||
# Return the item which is this item's parent.
|
||||
##self.log.write("GetParent\n")
|
||||
|
||||
if not item:
|
||||
return dv.NullDataViewItem
|
||||
|
||||
node = self.ItemToObject(item)
|
||||
if isinstance(node, Genre):
|
||||
return dv.NullDataViewItem
|
||||
elif isinstance(node, Song):
|
||||
for g in self.data:
|
||||
if g.name == node.genre:
|
||||
return self.ObjectToItem(g)
|
||||
|
||||
|
||||
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
|
||||
# associated with the items in GetChildren.
|
||||
|
||||
# Fetch the data object for this item.
|
||||
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]
|
||||
|
||||
|
||||
elif isinstance(node, Song):
|
||||
mapper = { 0 : node.genre,
|
||||
1 : node.artist,
|
||||
2 : node.title,
|
||||
3 : node.id,
|
||||
4 : node.date,
|
||||
5 : node.like,
|
||||
}
|
||||
return mapper[col]
|
||||
|
||||
else:
|
||||
raise RuntimeError("unknown node type")
|
||||
|
||||
|
||||
|
||||
def GetAttr(self, item, col, attr):
|
||||
##self.log.write('GetAttr')
|
||||
node = self.ItemToObject(item)
|
||||
if isinstance(node, Genre):
|
||||
attr.SetColour('blue')
|
||||
attr.SetBold(True)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def SetValue(self, value, item, col):
|
||||
self.log.write("SetValue: col %d, %s\n" % (col, value))
|
||||
|
||||
# We're not allowing edits in column zero (see below) so we just need
|
||||
# to deal with Song objects and cols 1 - 5
|
||||
|
||||
node = self.ItemToObject(item)
|
||||
if isinstance(node, Song):
|
||||
if col == 1:
|
||||
node.artist = value
|
||||
elif col == 2:
|
||||
node.title = value
|
||||
elif col == 3:
|
||||
node.id = value
|
||||
elif col == 4:
|
||||
node.date = value
|
||||
elif col == 5:
|
||||
node.like = value
|
||||
return True
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
class TestPanel(wx.Panel):
|
||||
def __init__(self, parent, log, data=None, model=None):
|
||||
self.log = log
|
||||
wx.Panel.__init__(self, parent, -1)
|
||||
|
||||
# Create a dataview control
|
||||
self.dvc = dv.DataViewCtrl(self,
|
||||
style=wx.BORDER_THEME
|
||||
| dv.DV_ROW_LINES # nice alternating bg colors
|
||||
#| dv.DV_HORIZ_RULES
|
||||
| dv.DV_VERT_RULES
|
||||
| dv.DV_MULTIPLE
|
||||
)
|
||||
|
||||
# Create an instance of our model...
|
||||
if model is None:
|
||||
self.model = MyTreeListModel(data, log)
|
||||
else:
|
||||
self.model = model
|
||||
|
||||
# Tel the DVC to use the model
|
||||
self.dvc.AssociateModel(self.model)
|
||||
|
||||
# Define the columns that we want in the view. Notice the
|
||||
# parameter which tells the view which col in the data model to pull
|
||||
# values from for each view column.
|
||||
if 0:
|
||||
self.tr = tr = dv.DataViewTextRenderer()
|
||||
c0 = dv.DataViewColumn("Genre", # title
|
||||
tr, # renderer
|
||||
0, # data model column
|
||||
width=80)
|
||||
self.dvc.AppendColumn(c0)
|
||||
else:
|
||||
self.dvc.AppendTextColumn("Genre", 0, width=80)
|
||||
|
||||
c1 = self.dvc.AppendTextColumn("Artist", 1, width=170, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
c2 = self.dvc.AppendTextColumn("Title", 2, width=260, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
c3 = self.dvc.AppendDateColumn('Acquired', 4, width=100, mode=dv.DATAVIEW_CELL_ACTIVATABLE)
|
||||
c4 = self.dvc.AppendToggleColumn('Like', 5, width=40, mode=dv.DATAVIEW_CELL_ACTIVATABLE)
|
||||
|
||||
# Notice how we pull the data from col 3, but this is the 6th col
|
||||
# added to the DVC. The order of the view columns is not dependent on
|
||||
# the order of the model columns at all.
|
||||
c5 = self.dvc.AppendTextColumn("id", 3, width=40, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
c5.Alignment = wx.ALIGN_RIGHT
|
||||
|
||||
# Set some additional attributes for all the columns
|
||||
for c in self.dvc.Columns:
|
||||
c.Sortable = True
|
||||
c.Reorderable = True
|
||||
|
||||
|
||||
self.Sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
self.Sizer.Add(self.dvc, 1, wx.EXPAND)
|
||||
|
||||
b1 = wx.Button(self, label="New View", name="newView")
|
||||
self.Bind(wx.EVT_BUTTON, self.OnNewView, b1)
|
||||
|
||||
self.Sizer.Add(b1, 0, wx.ALL, 5)
|
||||
|
||||
|
||||
def OnNewView(self, evt):
|
||||
f = wx.Frame(None, title="New view, shared model", size=(600,400))
|
||||
TestPanel(f, self.log, model=self.model)
|
||||
b = f.FindWindowByName("newView")
|
||||
b.Disable()
|
||||
f.Show()
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
def main():
|
||||
from data import musicdata
|
||||
|
||||
# our data structure will be a collection of Genres, each of which is a
|
||||
# collection of Songs
|
||||
data = dict()
|
||||
for key, artist, title, genre in musicdata:
|
||||
song = Song(str(key), artist, title, genre)
|
||||
genre = data.get(song.genre)
|
||||
if genre is None:
|
||||
genre = Genre(song.genre)
|
||||
data[song.genre] = genre
|
||||
genre.songs.append(song)
|
||||
data = data.values()
|
||||
|
||||
app = wx.App()
|
||||
frm = wx.Frame(None, title="DataViewModel sample", size=(700,500))
|
||||
pnl = TestPanel(frm, sys.stdout, data=data)
|
||||
frm.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
228
samples/dataview/IndexListModel.py
Normal file
228
samples/dataview/IndexListModel.py
Normal file
@@ -0,0 +1,228 @@
|
||||
import sys
|
||||
import wx
|
||||
import wx.dataview as dv
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
# This model class provides the data to the view when it is asked for.
|
||||
# Since it is a list-only model (no hierachical data) then it is able
|
||||
# to be referenced by row rather than by item object, so in this way
|
||||
# it is easier to comprehend and use than other model types. In this
|
||||
# example we also provide a Compare function to assist with sorting of
|
||||
# items in our model. Notice that the data items in the data model
|
||||
# object don't ever change position due to a sort or column
|
||||
# reordering. The view manages all of that and maps view rows and
|
||||
# columns to the model's rows and columns as needed.
|
||||
#
|
||||
# For this example our data is stored in a simple list of lists. In
|
||||
# real life you can use whatever you want or need to hold your data.
|
||||
|
||||
class TestModel(dv.DataViewIndexListModel):
|
||||
def __init__(self, data, log):
|
||||
dv.DataViewIndexListModel.__init__(self, len(data))
|
||||
self.data = data
|
||||
self.log = log
|
||||
|
||||
# This method is called to provide the data object for a
|
||||
# particular row,col
|
||||
def GetValueByRow(self, row, col):
|
||||
return str(self.data[row][col])
|
||||
|
||||
# This method is called when the user edits a data item in the view.
|
||||
def SetValueByRow(self, value, row, col):
|
||||
self.log.write("SetValue: (%d,%d) %s\n" % (row, col, value))
|
||||
self.data[row][col] = value
|
||||
return True
|
||||
|
||||
# Report how many columns this model provides data for.
|
||||
def GetColumnCount(self):
|
||||
return len(self.data[0])
|
||||
|
||||
# Report the number of rows in the model
|
||||
def GetCount(self):
|
||||
#self.log.write('GetCount')
|
||||
return len(self.data)
|
||||
|
||||
# Called to check if non-standard attributes should be used in the
|
||||
# cell at (row, col)
|
||||
def GetAttrByRow(self, row, col, attr):
|
||||
##self.log.write('GetAttrByRow: (%d, %d)' % (row, col))
|
||||
if col == 3:
|
||||
attr.SetColour('blue')
|
||||
attr.SetBold(True)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# This is called to assist with sorting the data in the view. The
|
||||
# first two args are instances of the DataViewItem class, so we
|
||||
# need to convert them to row numbers with the GetRow method.
|
||||
# Then it's just a matter of fetching the right values from our
|
||||
# data set and comparing them. The return value is -1, 0, or 1,
|
||||
# just like Python's cmp() function.
|
||||
def Compare(self, item1, item2, col, ascending):
|
||||
if not ascending: # swap sort order?
|
||||
item2, item1 = item1, item2
|
||||
row1 = self.GetRow(item1)
|
||||
row2 = self.GetRow(item2)
|
||||
if col == 0:
|
||||
return cmp(int(self.data[row1][col]), int(self.data[row2][col]))
|
||||
else:
|
||||
return cmp(self.data[row1][col], self.data[row2][col])
|
||||
|
||||
|
||||
def DeleteRows(self, rows):
|
||||
# make a copy since we'll be sorting(mutating) the list
|
||||
rows = list(rows)
|
||||
# use reverse order so the indexes don't change as we remove items
|
||||
rows.sort(reverse=True)
|
||||
|
||||
for row in rows:
|
||||
# remove it from our data structure
|
||||
del self.data[row]
|
||||
# notify the view(s) using this model that it has been removed
|
||||
self.RowDeleted(row)
|
||||
|
||||
|
||||
def AddRow(self, value):
|
||||
# update data structure
|
||||
self.data.append(value)
|
||||
# notify views
|
||||
self.RowAppended()
|
||||
|
||||
|
||||
|
||||
class TestPanel(wx.Panel):
|
||||
def __init__(self, parent, log, model=None, data=None):
|
||||
self.log = log
|
||||
wx.Panel.__init__(self, parent, -1)
|
||||
|
||||
# Create a dataview control
|
||||
self.dvc = dv.DataViewCtrl(self,
|
||||
style=wx.BORDER_THEME
|
||||
| dv.DV_ROW_LINES # nice alternating bg colors
|
||||
#| dv.DV_HORIZ_RULES
|
||||
| dv.DV_VERT_RULES
|
||||
| dv.DV_MULTIPLE
|
||||
)
|
||||
|
||||
# Create an instance of our simple model...
|
||||
if model is None:
|
||||
self.model = TestModel(data, log)
|
||||
else:
|
||||
self.model = model
|
||||
|
||||
# ...and associate it with the dataview control. Models can
|
||||
# be shared between multiple DataViewCtrls, so this does not
|
||||
# assign ownership like many things in wx do. There is some
|
||||
# internal reference counting happening so you don't really
|
||||
# need to hold a reference to it either, but we do for this
|
||||
# example so we can fiddle with the model from the widget
|
||||
# inspector or whatever.
|
||||
self.dvc.AssociateModel(self.model)
|
||||
|
||||
# Now we create some columns. The second parameter is the
|
||||
# column number within the model that the DataViewColumn will
|
||||
# fetch the data from. This means that you can have views
|
||||
# using the same model that show different columns of data, or
|
||||
# that they can be in a different order than in the model.
|
||||
self.dvc.AppendTextColumn("Artist", 1, width=170, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
self.dvc.AppendTextColumn("Title", 2, width=260, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
self.dvc.AppendTextColumn("Genre", 3, width=80, mode=dv.DATAVIEW_CELL_EDITABLE)
|
||||
|
||||
# There are Prepend methods too, and also convenience methods
|
||||
# for other data types but we are only using strings in this
|
||||
# example. You can also create a DataViewColumn object
|
||||
# yourself and then just use AppendColumn or PrependColumn.
|
||||
c0 = self.dvc.PrependTextColumn("Id", 0, width=40)
|
||||
|
||||
# The DataViewColumn object is returned from the Append and
|
||||
# Prepend methods, and we can modify some of it's properties
|
||||
# like this.
|
||||
c0.Alignment = wx.ALIGN_RIGHT
|
||||
c0.Renderer.Alignment = wx.ALIGN_RIGHT
|
||||
c0.MinWidth = 40
|
||||
|
||||
# Through the magic of Python we can also access the columns
|
||||
# as a list via the Columns property. Here we'll mark them
|
||||
# all as sortable and reorderable.
|
||||
for c in self.dvc.Columns:
|
||||
c.Sortable = True
|
||||
c.Reorderable = True
|
||||
|
||||
# Let's change our minds and not let the first col be moved.
|
||||
c0.Reorderable = False
|
||||
|
||||
# set the Sizer property (same as SetSizer)
|
||||
self.Sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
self.Sizer.Add(self.dvc, 1, wx.EXPAND)
|
||||
|
||||
# Add some buttons to help out with the tests
|
||||
b1 = wx.Button(self, label="New View", name="newView")
|
||||
self.Bind(wx.EVT_BUTTON, self.OnNewView, b1)
|
||||
b2 = wx.Button(self, label="Add Row")
|
||||
self.Bind(wx.EVT_BUTTON, self.OnAddRow, b2)
|
||||
b3 = wx.Button(self, label="Delete Row(s)")
|
||||
self.Bind(wx.EVT_BUTTON, self.OnDeleteRows, b3)
|
||||
|
||||
btnbox = wx.BoxSizer(wx.HORIZONTAL)
|
||||
btnbox.Add(b1, 0, wx.LEFT|wx.RIGHT, 5)
|
||||
btnbox.Add(b2, 0, wx.LEFT|wx.RIGHT, 5)
|
||||
btnbox.Add(b3, 0, wx.LEFT|wx.RIGHT, 5)
|
||||
self.Sizer.Add(btnbox, 0, wx.TOP|wx.BOTTOM, 5)
|
||||
|
||||
# Bind some events so we can see what the DVC sends us
|
||||
self.Bind(dv.EVT_DATAVIEW_ITEM_EDITING_DONE, self.OnEditingDone, self.dvc)
|
||||
self.Bind(dv.EVT_DATAVIEW_ITEM_VALUE_CHANGED, self.OnValueChanged, self.dvc)
|
||||
|
||||
|
||||
def OnNewView(self, evt):
|
||||
f = wx.Frame(None, title="New view, shared model", size=(600,400))
|
||||
TestPanel(f, self.log, self.model)
|
||||
b = f.FindWindowByName("newView")
|
||||
b.Disable()
|
||||
f.Show()
|
||||
|
||||
|
||||
def OnDeleteRows(self, evt):
|
||||
# Remove the selected row(s) from the model. The model will take care
|
||||
# of notifying the view (and any other observers) that the change has
|
||||
# happened.
|
||||
items = self.dvc.GetSelections()
|
||||
rows = [self.model.GetRow(item) for item in items]
|
||||
self.model.DeleteRows(rows)
|
||||
|
||||
|
||||
def OnAddRow(self, evt):
|
||||
# Add some bogus data to a new row in the model's data
|
||||
id = len(self.model.data) + 1
|
||||
value = [str(id),
|
||||
'new artist %d' % id,
|
||||
'new title %d' % id,
|
||||
'genre %d' % id]
|
||||
self.model.AddRow(value)
|
||||
|
||||
|
||||
def OnEditingDone(self, evt):
|
||||
self.log.write("OnEditingDone\n")
|
||||
|
||||
def OnValueChanged(self, evt):
|
||||
self.log.write("OnValueChanged\n")
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
def main():
|
||||
from data import musicdata
|
||||
|
||||
app = wx.App()
|
||||
frm = wx.Frame(None, title="IndexListModel sample", size=(700,500))
|
||||
pnl = TestPanel(frm, sys.stdout, data=musicdata)
|
||||
frm.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
57
samples/dataview/data.py
Normal file
57
samples/dataview/data.py
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
musicdata = [
|
||||
[1, "Bad English", "The Price Of Love", "Rock"],
|
||||
[2, "DNA featuring Suzanne Vega", "Tom's Diner", "Rock"],
|
||||
[3, "George Michael", "Praying For Time", "Rock"],
|
||||
[4, "Gloria Estefan", "Here We Are", "Rock"],
|
||||
[5, "Linda Ronstadt", "Don't Know Much", "Rock"],
|
||||
[6, "Michael Bolton", "How Am I Supposed To Live Without You", "Blues"],
|
||||
[7, "Paul Young", "Oh Girl", "Rock"],
|
||||
[8, "Paula Abdul", "Opposites Attract", "Rock"],
|
||||
[9, "Richard Marx", "Should've Known Better", "Rock"],
|
||||
[10, "Rod Stewart", "Forever Young", "Rock"],
|
||||
[11, "Roxette", "Dangerous", "Rock"],
|
||||
[12, "Sheena Easton", "The Lover In Me", "Rock"],
|
||||
[13, "Sinead O'Connor", "Nothing Compares 2 U", "Rock"],
|
||||
[14, "Stevie B.", "Because I Love You", "Rock"],
|
||||
[15, "Taylor Dayne", "Love Will Lead You Back", "Rock"],
|
||||
[16, "The Bangles", "Eternal Flame", "Rock"],
|
||||
[17, "Wilson Phillips", "Release Me", "Rock"],
|
||||
[18, "Billy Joel", "Blonde Over Blue", "Rock"],
|
||||
[19, "Billy Joel", "Famous Last Words", "Rock"],
|
||||
[20, "Janet Jackson", "State Of The World", "Rock"],
|
||||
[21, "Janet Jackson", "The Knowledge", "Rock"],
|
||||
[22, "Spyro Gyra", "End of Romanticism", "Jazz"],
|
||||
[23, "Spyro Gyra", "Heliopolis", "Jazz"],
|
||||
[24, "Spyro Gyra", "Jubilee", "Jazz"],
|
||||
[25, "Spyro Gyra", "Little Linda", "Jazz"],
|
||||
[26, "Spyro Gyra", "Morning Dance", "Jazz"],
|
||||
[27, "Spyro Gyra", "Song for Lorraine", "Jazz"],
|
||||
[28, "Yes", "Owner Of A Lonely Heart", "Rock"],
|
||||
[29, "Yes", "Rhythm Of Love", "Rock"],
|
||||
[30, "Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"],
|
||||
[31, "Billy Joel", "The River Of Dreams", "Rock"],
|
||||
[32, "Billy Joel", "Two Thousand Years", "Rock"],
|
||||
[33, "Janet Jackson", "Alright", "Rock"],
|
||||
[34, "Janet Jackson", "Black Cat", "Rock"],
|
||||
[35, "Janet Jackson", "Come Back To Me", "Rock"],
|
||||
[36, "Janet Jackson", "Escapade", "Rock"],
|
||||
[37, "Janet Jackson", "Love Will Never Do (Without You)", "Rock"],
|
||||
[38, "Janet Jackson", "Miss You Much", "Rock"],
|
||||
[39, "Janet Jackson", "Rhythm Nation", "Rock"],
|
||||
[40, "Cusco", "Dream Catcher", "New Age"],
|
||||
[41, "Cusco", "Geronimos Laughter", "New Age"],
|
||||
[42, "Cusco", "Ghost Dance", "New Age"],
|
||||
[43, "Blue Man Group", "Drumbone", "New Age"],
|
||||
[44, "Blue Man Group", "Endless Column", "New Age"],
|
||||
[45, "Blue Man Group", "Klein Mandelbrot", "New Age"],
|
||||
[46, "Kenny G", "Silhouette", "Jazz"],
|
||||
[47, "Sade", "Smooth Operator", "Jazz"],
|
||||
[48, "David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"],
|
||||
[49, "David Arkenstone", "Stepping Stars", "New Age"],
|
||||
[50, "David Arkenstone", "Carnation Lily Lily Rose", "New Age"],
|
||||
[51, "David Lanz", "Behind The Waterfall", "New Age"],
|
||||
[52, "David Lanz", "Cristofori's Dream", "New Age"],
|
||||
[53, "David Lanz", "Heartsounds", "New Age"],
|
||||
[54, "David Lanz", "Leaves on the Seine", "New Age"],
|
||||
]
|
||||
Reference in New Issue
Block a user