1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
5 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o wx renamer didn't appear to 'catch' all the classes in
9 # o Event binder not working properly with wx.lib.gridmovers
13 import wx
.grid
as gridlib
14 import wx
.lib
.gridmovers
as gridmovers
16 #---------------------------------------------------------------------------
18 class CustomDataTable(gridlib
.PyGridTableBase
):
19 def __init__(self
, log
):
20 gridlib
.PyGridTableBase
.__init
__(self
)
23 self
.identifiers
= ['id','ds','sv','pr','pl','op','fx','ts']
25 self
.rowLabels
= ['Row1','Row2','Row3']
27 self
.colLabels
= {'id':'ID','ds':'Description','sv':'Severity',
28 'pr':'Priority','pl':'Platform','op':'Opened?',
29 'fx':'Fixed?','ts':'Tested?'}
31 self
.data
= [{'id':1010,
32 'ds':"The foo doesn't bar",
41 'ds':"I've got a wicket in my wocket",
50 'ds':"Rectangle() returns a triangle",
60 #--------------------------------------------------
61 # required methods for the wxPyGridTableBase interface
63 def GetNumberRows(self
):
66 def GetNumberCols(self
):
67 return len(self
.identifiers
)
69 def IsEmptyCell(self
, row
, col
):
70 id = self
.identifiers
[col
]
71 return not self
.data
[row
][id]
73 def GetValue(self
, row
, col
):
74 id = self
.identifiers
[col
]
75 return self
.data
[row
][id]
77 def SetValue(self
, row
, col
, value
):
78 id = self
.identifiers
[col
]
79 self
.data
[row
][id] = value
81 #--------------------------------------------------
82 # Some optional methods
84 # Called when the grid needs to display column labels
85 def GetColLabelValue(self
, col
):
86 id = self
.identifiers
[col
]
87 return self
.colLabels
[id]
89 # Called when the grid needs to display row labels
90 def GetRowLabelValue(self
,row
):
91 return self
.rowLabels
[row
]
93 #--------------------------------------------------
94 # Methods added for demo purposes.
96 # The physical moving of the cols/rows is left to the implementer.
97 # Because of the dynamic nature of a wxGrid the physical moving of
98 # columns differs from implementation to implementation
101 def MoveColumn(self
,frm
,to
):
102 grid
= self
.GetView()
105 # Move the identifiers
106 old
= self
.identifiers
[frm
]
107 del self
.identifiers
[frm
]
110 self
.identifiers
.insert(to
-1,old
)
112 self
.identifiers
.insert(to
,old
)
116 msg
= gridlib
.GridTableMessage(
117 self
, gridlib
.GRIDTABLE_NOTIFY_COLS_DELETED
, frm
, 1
120 grid
.ProcessTableMessage(msg
)
122 msg
= gridlib
.GridTableMessage(
123 self
, gridlib
.GRIDTABLE_NOTIFY_COLS_INSERTED
, to
, 1
126 grid
.ProcessTableMessage(msg
)
130 def MoveRow(self
,frm
,to
):
131 grid
= self
.GetView()
134 # Move the rowLabels and data rows
135 oldLabel
= self
.rowLabels
[frm
]
136 oldData
= self
.data
[frm
]
137 del self
.rowLabels
[frm
]
141 self
.rowLabels
.insert(to
-1,oldLabel
)
142 self
.data
.insert(to
-1,oldData
)
144 self
.rowLabels
.insert(to
,oldLabel
)
145 self
.data
.insert(to
,oldData
)
150 msg
= gridlib
.GridTableMessage(
151 self
, gridlib
.GRIDTABLE_NOTIFY_ROWS_DELETED
, frm
, 1
154 grid
.ProcessTableMessage(msg
)
156 msg
= gridlib
.GridTableMessage(
157 self
, gridlib
.GRIDTABLE_NOTIFY_ROWS_INSERTED
, to
, 1
160 grid
.ProcessTableMessage(msg
)
164 #---------------------------------------------------------------------------
167 class DragableGrid(gridlib
.Grid
):
168 def __init__(self
, parent
, log
):
169 gridlib
.Grid
.__init
__(self
, parent
, -1)
171 table
= CustomDataTable(log
)
173 # The second parameter means that the grid is to take ownership of the
174 # table and will destroy it when done. Otherwise you would need to keep
175 # a reference to it and call it's Destroy method later.
176 self
.SetTable(table
, True)
178 # Enable Column moving
179 gridmovers
.GridColMover(self
)
180 self
.Bind(gridmovers
.EVT_GRID_COL_MOVE
, self
.OnColMove
, self
)
183 gridmovers
.GridRowMover(self
)
184 self
.Bind(gridmovers
.EVT_GRID_ROW_MOVE
, self
.OnRowMove
, self
)
186 # Event method called when a column move needs to take place
187 def OnColMove(self
,evt
):
188 frm
= evt
.GetMoveColumn() # Column being moved
189 to
= evt
.GetBeforeColumn() # Before which column to insert
190 self
.GetTable().MoveColumn(frm
,to
)
192 # Event method called when a row move needs to take place
193 def OnRowMove(self
,evt
):
194 frm
= evt
.GetMoveRow() # Row being moved
195 to
= evt
.GetBeforeRow() # Before which row to insert
196 self
.GetTable().MoveRow(frm
,to
)
198 #---------------------------------------------------------------------------
200 class TestFrame(wx
.Frame
):
201 def __init__(self
, parent
, log
):
202 wx
.Frame
.__init
__(self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480))
203 grid
= DragableGrid(self
, log
)
206 #---------------------------------------------------------------------------
208 if __name__
== '__main__':
210 app
= wx
.PySimpleApp()
211 frame
= TestFrame(None, sys
.stdout
)
215 #---------------------------------------------------------------------------