]>
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', | |
17 | 'Opened?', 'Fixed?', 'Tested?'] | |
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, | |
26 | wxGRID_VALUE_BOOL] | |
27 | ||
28 | self.data = [ | |
29 | [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1], | |
30 | [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0], | |
31 | [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0] | |
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 | return not self.data[row][col] | |
47 | ||
48 | # Get/Set values in the table. The Python version of these | |
49 | # methods can handle any data-type, (as long as the Editor and | |
50 | # Renderer understands the type too,) not just strings as in the | |
51 | # C++ version. | |
52 | def GetValue(self, row, col): | |
53 | try: | |
54 | return self.data[row][col] | |
55 | except IndexError: | |
56 | return '' | |
57 | ||
58 | def SetValue(self, row, col, value): | |
59 | try: | |
60 | self.data[row][col] = value | |
61 | except IndexError: | |
62 | # add a new row | |
63 | self.data.append([''] * self.GetNumberCols()) | |
64 | self.SetValue(row, col, value) | |
65 | ||
66 | # tell the grid we've added a row | |
67 | msg = wxGridTableMessage(self, # The table | |
68 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it | |
69 | 1) # how many | |
70 | ||
71 | self.GetView().ProcessTableMessage(msg) | |
72 | ||
73 | ||
74 | #-------------------------------------------------- | |
75 | # Some optional methods | |
76 | ||
77 | # Called when the grid needs to display labels | |
78 | def GetColLabelValue(self, col): | |
79 | return self.colLabels[col] | |
80 | ||
81 | # Called to determine the kind of editor/renderer to use by | |
82 | # default, doesn't necessarily have to be the same type used | |
83 | # nativly by the editor/renderer if they know how to convert. | |
84 | def GetTypeName(self, row, col): | |
85 | return self.dataTypes[col] | |
86 | ||
87 | # Called to determine how the data can be fetched and stored by the | |
88 | # editor and renderer. This allows you to enforce some type-safety | |
89 | # in the grid. | |
90 | def CanGetValueAs(self, row, col, typeName): | |
91 | colType = string.split(self.dataTypes[col], ':')[0] | |
92 | if typeName == colType: | |
93 | return true | |
94 | else: | |
95 | return false | |
96 | ||
97 | def CanSetValueAs(self, row, col, typeName): | |
98 | return self.CanGetValueAs(row, col, typeName) | |
99 | ||
100 | ||
101 | ||
102 | ||
103 | ||
104 | #--------------------------------------------------------------------------- | |
105 | ||
106 | ||
107 | ||
108 | class CustTableGrid(wxGrid): | |
109 | def __init__(self, parent, log): | |
110 | wxGrid.__init__(self, parent, -1) | |
111 | ||
112 | table = CustomDataTable(log) | |
113 | ||
114 | # The second parameter means that the grid is to take ownership of the | |
115 | # table and will destroy it when done. Otherwise you would need to keep | |
116 | # a reference to it and call it's Destroy method later. | |
117 | self.SetTable(table, true) | |
118 | ||
119 | self.SetRowLabelSize(0) | |
120 | self.SetMargins(0,0) | |
121 | self.AutoSizeColumns(false) | |
122 | ||
123 | EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick) | |
124 | ||
125 | ||
126 | ||
127 | # I do this because I don't like the default behaviour of not starting the | |
128 | # cell editor on double clicks, but only a second click. | |
129 | def OnLeftDClick(self, evt): | |
130 | if self.CanEnableCellControl(): | |
131 | self.EnableCellEditControl() | |
132 | ||
133 | ||
134 | #--------------------------------------------------------------------------- | |
135 | ||
136 | class TestFrame(wxFrame): | |
137 | def __init__(self, parent, log): | |
138 | wxFrame.__init__(self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480)) | |
139 | grid = CustTableGrid(self, log) | |
140 | ||
141 | ||
142 | ||
143 | #--------------------------------------------------------------------------- | |
144 | ||
145 | if __name__ == '__main__': | |
146 | import sys | |
147 | app = wxPySimpleApp() | |
148 | frame = TestFrame(None, sys.stdout) | |
149 | frame.Show(true) | |
150 | app.MainLoop() | |
151 | ||
152 | ||
153 | #--------------------------------------------------------------------------- |