]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridCustTable.py
Typo
[wxWidgets.git] / wxPython / demo / GridCustTable.py
CommitLineData
f6bcfd97 1
8fa876ca
RD
2import wx
3import wx.grid as gridlib
f6bcfd97 4
8fa876ca 5#---------------------------------------------------------------------------
f6bcfd97 6
8fa876ca 7class CustomDataTable(gridlib.PyGridTableBase):
f6bcfd97 8 def __init__(self, log):
8fa876ca 9 gridlib.PyGridTableBase.__init__(self)
f6bcfd97
BP
10 self.log = log
11
12 self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
f19533bf 13 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
f6bcfd97 14
8fa876ca
RD
15 self.dataTypes = [gridlib.GRID_VALUE_NUMBER,
16 gridlib.GRID_VALUE_STRING,
17 gridlib.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
18 gridlib.GRID_VALUE_NUMBER + ':1,5',
19 gridlib.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
20 gridlib.GRID_VALUE_BOOL,
21 gridlib.GRID_VALUE_BOOL,
22 gridlib.GRID_VALUE_BOOL,
23 gridlib.GRID_VALUE_FLOAT + ':6,2',
f19533bf 24 ]
f6bcfd97
BP
25
26 self.data = [
f19533bf
RD
27 [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
28 [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
29 [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
f6bcfd97
BP
30
31 ]
32
33
34 #--------------------------------------------------
35 # required methods for the wxPyGridTableBase interface
36
37 def GetNumberRows(self):
38 return len(self.data) + 1
39
40 def GetNumberCols(self):
41 return len(self.data[0])
42
43 def IsEmptyCell(self, row, col):
1e4a197e
RD
44 try:
45 return not self.data[row][col]
46 except IndexError:
d14a1e28 47 return True
f6bcfd97
BP
48
49 # Get/Set values in the table. The Python version of these
50 # methods can handle any data-type, (as long as the Editor and
51 # Renderer understands the type too,) not just strings as in the
52 # C++ version.
53 def GetValue(self, row, col):
54 try:
55 return self.data[row][col]
56 except IndexError:
57 return ''
58
59 def SetValue(self, row, col, value):
60 try:
61 self.data[row][col] = value
62 except IndexError:
63 # add a new row
64 self.data.append([''] * self.GetNumberCols())
65 self.SetValue(row, col, value)
66
67 # tell the grid we've added a row
8fa876ca
RD
68 msg = gridlib.GridTableMessage(self, # The table
69 gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
70 1 # how many
71 )
f6bcfd97
BP
72
73 self.GetView().ProcessTableMessage(msg)
74
75
76 #--------------------------------------------------
77 # Some optional methods
78
79 # Called when the grid needs to display labels
80 def GetColLabelValue(self, col):
81 return self.colLabels[col]
82
83 # Called to determine the kind of editor/renderer to use by
84 # default, doesn't necessarily have to be the same type used
8b9a4190 85 # natively by the editor/renderer if they know how to convert.
f6bcfd97
BP
86 def GetTypeName(self, row, col):
87 return self.dataTypes[col]
88
89 # Called to determine how the data can be fetched and stored by the
90 # editor and renderer. This allows you to enforce some type-safety
91 # in the grid.
92 def CanGetValueAs(self, row, col, typeName):
1e4a197e 93 colType = self.dataTypes[col].split(':')[0]
f6bcfd97 94 if typeName == colType:
d14a1e28 95 return True
f6bcfd97 96 else:
1e4a197e 97 return False
f6bcfd97
BP
98
99 def CanSetValueAs(self, row, col, typeName):
100 return self.CanGetValueAs(row, col, typeName)
101
102
103
104
105
106#---------------------------------------------------------------------------
107
108
109
8fa876ca 110class CustTableGrid(gridlib.Grid):
f6bcfd97 111 def __init__(self, parent, log):
8fa876ca 112 gridlib.Grid.__init__(self, parent, -1)
f6bcfd97
BP
113
114 table = CustomDataTable(log)
115
116 # The second parameter means that the grid is to take ownership of the
117 # table and will destroy it when done. Otherwise you would need to keep
118 # a reference to it and call it's Destroy method later.
d14a1e28 119 self.SetTable(table, True)
f6bcfd97
BP
120
121 self.SetRowLabelSize(0)
122 self.SetMargins(0,0)
1e4a197e 123 self.AutoSizeColumns(False)
f6bcfd97 124
8fa876ca 125 gridlib.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
f6bcfd97
BP
126
127
128 # I do this because I don't like the default behaviour of not starting the
129 # cell editor on double clicks, but only a second click.
130 def OnLeftDClick(self, evt):
131 if self.CanEnableCellControl():
132 self.EnableCellEditControl()
133
134
135#---------------------------------------------------------------------------
136
8fa876ca 137class TestFrame(wx.Frame):
f6bcfd97 138 def __init__(self, parent, log):
8fa876ca
RD
139
140 wx.Frame.__init__(
141 self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480)
142 )
143
144 p = wx.Panel(self, -1, style=0)
1e4a197e 145 grid = CustTableGrid(p, log)
8fa876ca 146 b = wx.Button(p, -1, "Another Control...")
1e4a197e 147 b.SetDefault()
8fa876ca
RD
148 self.Bind(wx.EVT_BUTTON, self.OnButton, b)
149 b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus)
150 bs = wx.BoxSizer(wx.VERTICAL)
151 bs.Add(grid, 1, wx.GROW|wx.ALL, 5)
1e4a197e
RD
152 bs.Add(b)
153 p.SetSizer(bs)
154
155 def OnButton(self, evt):
156 print "button selected"
157
158 def OnButtonFocus(self, evt):
159 print "button focus"
f6bcfd97
BP
160
161
162#---------------------------------------------------------------------------
163
164if __name__ == '__main__':
165 import sys
8fa876ca 166 app = wx.PySimpleApp()
f6bcfd97 167 frame = TestFrame(None, sys.stdout)
d14a1e28 168 frame.Show(True)
f6bcfd97
BP
169 app.MainLoop()
170
171
172#---------------------------------------------------------------------------