1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
4 # o Also corrected minor bug where 'true' was being used instead of 'True'.
5 # Doesn't fail for * import (I guess that is still defined in wx), but does
6 # in the manner we're importing wx now.
9 import wx
.grid
as gridlib
11 #---------------------------------------------------------------------------
13 class CustomDataTable(gridlib
.PyGridTableBase
):
14 def __init__(self
, log
):
15 gridlib
.PyGridTableBase
.__init
__(self
)
18 self
.colLabels
= ['ID', 'Description', 'Severity', 'Priority', 'Platform',
19 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
21 self
.dataTypes
= [gridlib
.GRID_VALUE_NUMBER
,
22 gridlib
.GRID_VALUE_STRING
,
23 gridlib
.GRID_VALUE_CHOICE
+ ':only in a million years!,wish list,minor,normal,major,critical',
24 gridlib
.GRID_VALUE_NUMBER
+ ':1,5',
25 gridlib
.GRID_VALUE_CHOICE
+ ':all,MSW,GTK,other',
26 gridlib
.GRID_VALUE_BOOL
,
27 gridlib
.GRID_VALUE_BOOL
,
28 gridlib
.GRID_VALUE_BOOL
,
29 gridlib
.GRID_VALUE_FLOAT
+ ':6,2',
33 [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
34 [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
35 [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
40 #--------------------------------------------------
41 # required methods for the wxPyGridTableBase interface
43 def GetNumberRows(self
):
44 return len(self
.data
) + 1
46 def GetNumberCols(self
):
47 return len(self
.data
[0])
49 def IsEmptyCell(self
, row
, col
):
51 return not self
.data
[row
][col
]
55 # Get/Set values in the table. The Python version of these
56 # methods can handle any data-type, (as long as the Editor and
57 # Renderer understands the type too,) not just strings as in the
59 def GetValue(self
, row
, col
):
61 return self
.data
[row
][col
]
65 def SetValue(self
, row
, col
, value
):
67 self
.data
[row
][col
] = value
70 self
.data
.append([''] * self
.GetNumberCols())
71 self
.SetValue(row
, col
, value
)
73 # tell the grid we've added a row
74 msg
= gridlib
.GridTableMessage(self
, # The table
75 gridlib
.GRIDTABLE_NOTIFY_ROWS_APPENDED
, # what we did to it
79 self
.GetView().ProcessTableMessage(msg
)
82 #--------------------------------------------------
83 # Some optional methods
85 # Called when the grid needs to display labels
86 def GetColLabelValue(self
, col
):
87 return self
.colLabels
[col
]
89 # Called to determine the kind of editor/renderer to use by
90 # default, doesn't necessarily have to be the same type used
91 # natively by the editor/renderer if they know how to convert.
92 def GetTypeName(self
, row
, col
):
93 return self
.dataTypes
[col
]
95 # Called to determine how the data can be fetched and stored by the
96 # editor and renderer. This allows you to enforce some type-safety
98 def CanGetValueAs(self
, row
, col
, typeName
):
99 colType
= self
.dataTypes
[col
].split(':')[0]
100 if typeName
== colType
:
105 def CanSetValueAs(self
, row
, col
, typeName
):
106 return self
.CanGetValueAs(row
, col
, typeName
)
112 #---------------------------------------------------------------------------
116 class CustTableGrid(gridlib
.Grid
):
117 def __init__(self
, parent
, log
):
118 gridlib
.Grid
.__init
__(self
, parent
, -1)
120 table
= CustomDataTable(log
)
122 # The second parameter means that the grid is to take ownership of the
123 # table and will destroy it when done. Otherwise you would need to keep
124 # a reference to it and call it's Destroy method later.
125 self
.SetTable(table
, True)
127 self
.SetRowLabelSize(0)
129 self
.AutoSizeColumns(False)
131 gridlib
.EVT_GRID_CELL_LEFT_DCLICK(self
, self
.OnLeftDClick
)
134 # I do this because I don't like the default behaviour of not starting the
135 # cell editor on double clicks, but only a second click.
136 def OnLeftDClick(self
, evt
):
137 if self
.CanEnableCellControl():
138 self
.EnableCellEditControl()
141 #---------------------------------------------------------------------------
143 class TestFrame(wx
.Frame
):
144 def __init__(self
, parent
, log
):
147 self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480)
150 p
= wx
.Panel(self
, -1, style
=0)
151 grid
= CustTableGrid(p
, log
)
152 b
= wx
.Button(p
, -1, "Another Control...")
154 self
.Bind(wx
.EVT_BUTTON
, self
.OnButton
, b
)
155 b
.Bind(wx
.EVT_SET_FOCUS
, self
.OnButtonFocus
)
156 bs
= wx
.BoxSizer(wx
.VERTICAL
)
157 bs
.Add(grid
, 1, wx
.GROW|wx
.ALL
, 5)
161 def OnButton(self
, evt
):
162 print "button selected"
164 def OnButtonFocus(self
, evt
):
168 #---------------------------------------------------------------------------
170 if __name__
== '__main__':
172 app
= wx
.PySimpleApp()
173 frame
= TestFrame(None, sys
.stdout
)
178 #---------------------------------------------------------------------------