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