mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2025-08-24 03:20:21 +02:00
Initial port of wxPython demo from Classic to Phoenix.
Pulled and squashed from https://github.com/RobinD42/Phoenix/pull/7 Thanks Metallicow! git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@74199 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -10,7 +10,7 @@ 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)
|
||||
empty = wx.Bitmap(16,16,32)
|
||||
dc = wx.MemoryDC(empty)
|
||||
dc.SetBackground(wx.Brush((0,0,0,0)))
|
||||
dc.Clear()
|
||||
@@ -32,17 +32,17 @@ class Song(object):
|
||||
d = random.choice(range(27))+1
|
||||
m = random.choice(range(12))
|
||||
y = random.choice(range(1980, 2005))
|
||||
self.date = wx.DateTimeFromDMY(d,m,y)
|
||||
|
||||
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
|
||||
|
||||
@@ -50,13 +50,13 @@ class Genre(object):
|
||||
|
||||
# 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. We derive the class from PyDataViewModel, which knows
|
||||
# collection of songs. We derive the class from PyDataViewCtrl, which knows
|
||||
# how to reflect the C++ virtual methods to the Python methods in the derived
|
||||
# class.
|
||||
|
||||
# This model provides these data columns:
|
||||
#
|
||||
# 0. Genre: string
|
||||
# 0. Genre : string
|
||||
# 1. Artist: string
|
||||
# 2. Title: string
|
||||
# 3. id: integer
|
||||
@@ -69,7 +69,7 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
dv.PyDataViewModel.__init__(self)
|
||||
self.data = data
|
||||
self.log = log
|
||||
|
||||
|
||||
# The objmapper is an instance of DataViewItemObjectMapper and is used
|
||||
# to help associate Python objects with DataViewItem objects. Normally
|
||||
# a dictionary is used so any Python object can be used as data nodes.
|
||||
@@ -80,7 +80,7 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
# self.ItemToObject methods used below.
|
||||
self.objmapper.UseWeakRefs(True)
|
||||
|
||||
|
||||
|
||||
# Report how many columns this model provides data for.
|
||||
def GetColumnCount(self):
|
||||
return 6
|
||||
@@ -95,9 +95,9 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
5 : 'bool',
|
||||
}
|
||||
return mapper[col]
|
||||
|
||||
|
||||
def GetChildren(self, parent, children):
|
||||
|
||||
|
||||
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
|
||||
@@ -105,7 +105,7 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
# view adds additional items as children of the other items, as needed,
|
||||
# to provide the tree hierachy.
|
||||
##self.log.write("GetChildren\n")
|
||||
|
||||
|
||||
# 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.
|
||||
@@ -113,7 +113,7 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
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)
|
||||
@@ -122,12 +122,12 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
children.append(self.ObjectToItem(song))
|
||||
return len(node.songs)
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
def IsContainer(self, item):
|
||||
# Return True if the item has children, False otherwise.
|
||||
##self.log.write("IsContainer\n")
|
||||
|
||||
|
||||
# The hidden root is a container
|
||||
if not item:
|
||||
return True
|
||||
@@ -136,38 +136,38 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
if isinstance(node, Genre):
|
||||
return True
|
||||
# but everything else (the song objects) are not
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
#def HasContainerColumns(self, item):
|
||||
# self.log.write('HasContainerColumns\n')
|
||||
# return True
|
||||
|
||||
|
||||
|
||||
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)
|
||||
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
|
||||
@@ -179,8 +179,8 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
5 : False,
|
||||
}
|
||||
return mapper[col]
|
||||
|
||||
|
||||
|
||||
|
||||
elif isinstance(node, Song):
|
||||
mapper = { 0 : node.genre,
|
||||
1 : node.artist,
|
||||
@@ -190,10 +190,10 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
5 : node.like,
|
||||
}
|
||||
return mapper[col]
|
||||
|
||||
|
||||
else:
|
||||
raise RuntimeError("unknown node type")
|
||||
|
||||
|
||||
|
||||
|
||||
def GetAttr(self, item, col, attr):
|
||||
@@ -204,14 +204,14 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
attr.SetBold(True)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
def SetValue(self, value, item, col):
|
||||
self.log.write("SetValue: %s\n" % 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:
|
||||
@@ -224,7 +224,7 @@ class MyTreeListModel(dv.PyDataViewModel):
|
||||
node.date = value
|
||||
elif col == 5:
|
||||
node.like = value
|
||||
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
||||
@@ -241,17 +241,17 @@ class TestPanel(wx.Panel):
|
||||
| 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
|
||||
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
|
||||
# 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 1:
|
||||
@@ -263,41 +263,41 @@ class TestPanel(wx.Panel):
|
||||
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 runTest(frame, nb, log):
|
||||
|
Reference in New Issue
Block a user