]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/GridCustTable.py
merge from 2.8 branch
[wxWidgets.git] / wxPython / demo / GridCustTable.py
... / ...
CommitLineData
1
2import wx
3import wx.grid as gridlib
4
5#---------------------------------------------------------------------------
6
7class CustomDataTable(gridlib.PyGridTableBase):
8 def __init__(self, log):
9 gridlib.PyGridTableBase.__init__(self)
10 self.log = log
11
12 self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
13 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
14
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',
24 ]
25
26 self.data = [
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]
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):
44 try:
45 return not self.data[row][col]
46 except IndexError:
47 return True
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
68 msg = gridlib.GridTableMessage(self, # The table
69 gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
70 1 # how many
71 )
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
85 # natively by the editor/renderer if they know how to convert.
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):
93 colType = self.dataTypes[col].split(':')[0]
94 if typeName == colType:
95 return True
96 else:
97 return False
98
99 def CanSetValueAs(self, row, col, typeName):
100 return self.CanGetValueAs(row, col, typeName)
101
102
103
104
105
106#---------------------------------------------------------------------------
107
108
109
110class CustTableGrid(gridlib.Grid):
111 def __init__(self, parent, log):
112 gridlib.Grid.__init__(self, parent, -1)
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.
119 self.SetTable(table, True)
120
121 self.SetRowLabelSize(0)
122 self.SetMargins(0,0)
123 self.AutoSizeColumns(False)
124
125 gridlib.EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
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
137class TestFrame(wx.Frame):
138 def __init__(self, parent, log):
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)
145 grid = CustTableGrid(p, log)
146 b = wx.Button(p, -1, "Another Control...")
147 b.SetDefault()
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)
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"
160
161
162#---------------------------------------------------------------------------
163
164if __name__ == '__main__':
165 import sys
166 app = wx.PySimpleApp()
167 frame = TestFrame(None, sys.stdout)
168 frame.Show(True)
169 app.MainLoop()
170
171
172#---------------------------------------------------------------------------