2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import wx
|
|
|
|
import wx.grid as gridlib
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
#---------------------------------------------------------------------------
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class CustomDataTable(gridlib.PyGridTableBase):
|
2000-07-15 15:51:35 -04:00
|
|
|
def __init__(self, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
gridlib.PyGridTableBase.__init__(self)
|
2000-07-15 15:51:35 -04:00
|
|
|
self.log = log
|
|
|
|
|
|
|
|
self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
|
2002-08-24 17:44:02 -04:00
|
|
|
'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
self.dataTypes = [gridlib.GRID_VALUE_NUMBER,
|
|
|
|
gridlib.GRID_VALUE_STRING,
|
|
|
|
gridlib.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
|
|
|
|
gridlib.GRID_VALUE_NUMBER + ':1,5',
|
|
|
|
gridlib.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
|
|
|
|
gridlib.GRID_VALUE_BOOL,
|
|
|
|
gridlib.GRID_VALUE_BOOL,
|
|
|
|
gridlib.GRID_VALUE_BOOL,
|
|
|
|
gridlib.GRID_VALUE_FLOAT + ':6,2',
|
2002-08-24 17:44:02 -04:00
|
|
|
]
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
self.data = [
|
2002-08-24 17:44:02 -04:00
|
|
|
[1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
|
|
|
|
[1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
|
|
|
|
[1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
#--------------------------------------------------
|
|
|
|
# required methods for the wxPyGridTableBase interface
|
|
|
|
|
|
|
|
def GetNumberRows(self):
|
|
|
|
return len(self.data) + 1
|
|
|
|
|
|
|
|
def GetNumberCols(self):
|
|
|
|
return len(self.data[0])
|
|
|
|
|
|
|
|
def IsEmptyCell(self, row, col):
|
2003-03-25 01:35:27 -05:00
|
|
|
try:
|
|
|
|
return not self.data[row][col]
|
|
|
|
except IndexError:
|
2003-11-12 16:34:20 -05:00
|
|
|
return True
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
# Get/Set values in the table. The Python version of these
|
|
|
|
# methods can handle any data-type, (as long as the Editor and
|
|
|
|
# Renderer understands the type too,) not just strings as in the
|
|
|
|
# C++ version.
|
|
|
|
def GetValue(self, row, col):
|
|
|
|
try:
|
|
|
|
return self.data[row][col]
|
|
|
|
except IndexError:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def SetValue(self, row, col, value):
|
|
|
|
try:
|
|
|
|
self.data[row][col] = value
|
|
|
|
except IndexError:
|
|
|
|
# add a new row
|
|
|
|
self.data.append([''] * self.GetNumberCols())
|
|
|
|
self.SetValue(row, col, value)
|
|
|
|
|
|
|
|
# tell the grid we've added a row
|
2003-12-08 20:23:28 -05:00
|
|
|
msg = gridlib.GridTableMessage(self, # The table
|
|
|
|
gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
|
|
|
|
1 # how many
|
|
|
|
)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
self.GetView().ProcessTableMessage(msg)
|
|
|
|
|
|
|
|
|
|
|
|
#--------------------------------------------------
|
|
|
|
# Some optional methods
|
|
|
|
|
|
|
|
# Called when the grid needs to display labels
|
|
|
|
def GetColLabelValue(self, col):
|
|
|
|
return self.colLabels[col]
|
|
|
|
|
|
|
|
# Called to determine the kind of editor/renderer to use by
|
|
|
|
# default, doesn't necessarily have to be the same type used
|
2003-10-01 20:58:06 -04:00
|
|
|
# natively by the editor/renderer if they know how to convert.
|
2000-07-15 15:51:35 -04:00
|
|
|
def GetTypeName(self, row, col):
|
|
|
|
return self.dataTypes[col]
|
|
|
|
|
|
|
|
# Called to determine how the data can be fetched and stored by the
|
|
|
|
# editor and renderer. This allows you to enforce some type-safety
|
|
|
|
# in the grid.
|
|
|
|
def CanGetValueAs(self, row, col, typeName):
|
2003-03-25 01:35:27 -05:00
|
|
|
colType = self.dataTypes[col].split(':')[0]
|
2000-07-15 15:51:35 -04:00
|
|
|
if typeName == colType:
|
2003-11-12 16:34:20 -05:00
|
|
|
return True
|
2000-07-15 15:51:35 -04:00
|
|
|
else:
|
2003-03-25 01:35:27 -05:00
|
|
|
return False
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
def CanSetValueAs(self, row, col, typeName):
|
|
|
|
return self.CanGetValueAs(row, col, typeName)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class CustTableGrid(gridlib.Grid):
|
2000-07-15 15:51:35 -04:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
gridlib.Grid.__init__(self, parent, -1)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
table = CustomDataTable(log)
|
|
|
|
|
|
|
|
# The second parameter means that the grid is to take ownership of the
|
|
|
|
# table and will destroy it when done. Otherwise you would need to keep
|
|
|
|
# a reference to it and call it's Destroy method later.
|
2003-11-12 16:34:20 -05:00
|
|
|
self.SetTable(table, True)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
self.SetRowLabelSize(0)
|
|
|
|
self.SetMargins(0,0)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.AutoSizeColumns(False)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
gridlib.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
# I do this because I don't like the default behaviour of not starting the
|
|
|
|
# cell editor on double clicks, but only a second click.
|
|
|
|
def OnLeftDClick(self, evt):
|
|
|
|
if self.CanEnableCellControl():
|
|
|
|
self.EnableCellEditControl()
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestFrame(wx.Frame):
|
2000-07-15 15:51:35 -04:00
|
|
|
def __init__(self, parent, log):
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
wx.Frame.__init__(
|
|
|
|
self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480)
|
|
|
|
)
|
|
|
|
|
|
|
|
p = wx.Panel(self, -1, style=0)
|
2003-03-25 01:35:27 -05:00
|
|
|
grid = CustTableGrid(p, log)
|
2003-12-08 20:23:28 -05:00
|
|
|
b = wx.Button(p, -1, "Another Control...")
|
2003-03-25 01:35:27 -05:00
|
|
|
b.SetDefault()
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
|
|
|
|
b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus)
|
|
|
|
bs = wx.BoxSizer(wx.VERTICAL)
|
|
|
|
bs.Add(grid, 1, wx.GROW|wx.ALL, 5)
|
2003-03-25 01:35:27 -05:00
|
|
|
bs.Add(b)
|
|
|
|
p.SetSizer(bs)
|
|
|
|
|
|
|
|
def OnButton(self, evt):
|
|
|
|
print "button selected"
|
|
|
|
|
|
|
|
def OnButtonFocus(self, evt):
|
|
|
|
print "button focus"
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2003-12-08 20:23:28 -05:00
|
|
|
app = wx.PySimpleApp()
|
2000-07-15 15:51:35 -04:00
|
|
|
frame = TestFrame(None, sys.stdout)
|
2003-11-12 16:34:20 -05:00
|
|
|
frame.Show(True)
|
2000-07-15 15:51:35 -04:00
|
|
|
app.MainLoop()
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|