]>
Commit | Line | Data |
---|---|---|
f6bcfd97 BP |
1 | from wxPython.wx import * |
2 | from wxPython.grid import * | |
3 | ||
4 | import string | |
5 | ||
6 | #--------------------------------------------------------------------------- | |
7 | ||
8 | class CustomDataTable(wxPyGridTableBase): | |
9 | """ | |
10 | """ | |
11 | ||
12 | def __init__(self, log): | |
13 | wxPyGridTableBase.__init__(self) | |
14 | self.log = log | |
15 | ||
16 | self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform', | |
f19533bf | 17 | 'Opened?', 'Fixed?', 'Tested?', 'TestFloat'] |
f6bcfd97 BP |
18 | |
19 | self.dataTypes = [wxGRID_VALUE_NUMBER, | |
20 | wxGRID_VALUE_STRING, | |
21 | wxGRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical', | |
22 | wxGRID_VALUE_NUMBER + ':1,5', | |
23 | wxGRID_VALUE_CHOICE + ':all,MSW,GTK,other', | |
24 | wxGRID_VALUE_BOOL, | |
25 | wxGRID_VALUE_BOOL, | |
f19533bf RD |
26 | wxGRID_VALUE_BOOL, |
27 | wxGRID_VALUE_FLOAT + ':6,2', | |
28 | ] | |
f6bcfd97 BP |
29 | |
30 | self.data = [ | |
f19533bf RD |
31 | [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12], |
32 | [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50], | |
33 | [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56] | |
f6bcfd97 BP |
34 | |
35 | ] | |
36 | ||
37 | ||
38 | #-------------------------------------------------- | |
39 | # required methods for the wxPyGridTableBase interface | |
40 | ||
41 | def GetNumberRows(self): | |
42 | return len(self.data) + 1 | |
43 | ||
44 | def GetNumberCols(self): | |
45 | return len(self.data[0]) | |
46 | ||
47 | def IsEmptyCell(self, row, col): | |
48 | return not self.data[row][col] | |
49 | ||
50 | # Get/Set values in the table. The Python version of these | |
51 | # methods can handle any data-type, (as long as the Editor and | |
52 | # Renderer understands the type too,) not just strings as in the | |
53 | # C++ version. | |
54 | def GetValue(self, row, col): | |
55 | try: | |
56 | return self.data[row][col] | |
57 | except IndexError: | |
58 | return '' | |
59 | ||
60 | def SetValue(self, row, col, value): | |
61 | try: | |
62 | self.data[row][col] = value | |
63 | except IndexError: | |
64 | # add a new row | |
65 | self.data.append([''] * self.GetNumberCols()) | |
66 | self.SetValue(row, col, value) | |
67 | ||
68 | # tell the grid we've added a row | |
69 | msg = wxGridTableMessage(self, # The table | |
70 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it | |
71 | 1) # how many | |
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 | # nativly 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 = string.split(self.dataTypes[col], ':')[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 | ||
110 | class CustTableGrid(wxGrid): | |
111 | def __init__(self, parent, log): | |
112 | wxGrid.__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 | EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick) | |
126 | ||
127 | ||
128 | ||
129 | # I do this because I don't like the default behaviour of not starting the | |
130 | # cell editor on double clicks, but only a second click. | |
131 | def OnLeftDClick(self, evt): | |
132 | if self.CanEnableCellControl(): | |
133 | self.EnableCellEditControl() | |
134 | ||
135 | ||
136 | #--------------------------------------------------------------------------- | |
137 | ||
138 | class TestFrame(wxFrame): | |
139 | def __init__(self, parent, log): | |
140 | wxFrame.__init__(self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480)) | |
141 | grid = CustTableGrid(self, log) | |
142 | ||
143 | ||
144 | ||
145 | #--------------------------------------------------------------------------- | |
146 | ||
147 | if __name__ == '__main__': | |
148 | import sys | |
149 | app = wxPySimpleApp() | |
150 | frame = TestFrame(None, sys.stdout) | |
151 | frame.Show(true) | |
152 | app.MainLoop() | |
153 | ||
154 | ||
155 | #--------------------------------------------------------------------------- |