3 import wx
.grid
as gridlib
4 import wx
.lib
.gridmovers
as gridmovers
6 #---------------------------------------------------------------------------
8 class CustomDataTable(gridlib
.PyGridTableBase
):
9 def __init__(self
, log
):
10 gridlib
.PyGridTableBase
.__init
__(self
)
13 self
.identifiers
= ['id','ds','sv','pr','pl','op','fx','ts']
15 self
.rowLabels
= ['Row1','Row2','Row3']
17 self
.colLabels
= {'id':'ID','ds':'Description','sv':'Severity',
18 'pr':'Priority','pl':'Platform','op':'Opened?',
19 'fx':'Fixed?','ts':'Tested?'}
21 self
.data
= [{'id':1010,
22 'ds':"The foo doesn't bar",
31 'ds':"I've got a wicket in my wocket",
40 'ds':"Rectangle() returns a triangle",
50 #--------------------------------------------------
51 # required methods for the wxPyGridTableBase interface
53 def GetNumberRows(self
):
56 def GetNumberCols(self
):
57 return len(self
.identifiers
)
59 def IsEmptyCell(self
, row
, col
):
60 id = self
.identifiers
[col
]
61 return not self
.data
[row
][id]
63 def GetValue(self
, row
, col
):
64 id = self
.identifiers
[col
]
65 return self
.data
[row
][id]
67 def SetValue(self
, row
, col
, value
):
68 id = self
.identifiers
[col
]
69 self
.data
[row
][id] = value
71 #--------------------------------------------------
72 # Some optional methods
74 # Called when the grid needs to display column labels
75 def GetColLabelValue(self
, col
):
76 id = self
.identifiers
[col
]
77 return self
.colLabels
[id]
79 # Called when the grid needs to display row labels
80 def GetRowLabelValue(self
,row
):
81 return self
.rowLabels
[row
]
83 #--------------------------------------------------
84 # Methods added for demo purposes.
86 # The physical moving of the cols/rows is left to the implementer.
87 # Because of the dynamic nature of a wxGrid the physical moving of
88 # columns differs from implementation to implementation
91 def MoveColumn(self
,frm
,to
):
95 # Move the identifiers
96 old
= self
.identifiers
[frm
]
97 del self
.identifiers
[frm
]
100 self
.identifiers
.insert(to
-1,old
)
102 self
.identifiers
.insert(to
,old
)
106 msg
= gridlib
.GridTableMessage(
107 self
, gridlib
.GRIDTABLE_NOTIFY_COLS_DELETED
, frm
, 1
110 grid
.ProcessTableMessage(msg
)
112 msg
= gridlib
.GridTableMessage(
113 self
, gridlib
.GRIDTABLE_NOTIFY_COLS_INSERTED
, to
, 1
116 grid
.ProcessTableMessage(msg
)
120 def MoveRow(self
,frm
,to
):
121 grid
= self
.GetView()
124 # Move the rowLabels and data rows
125 oldLabel
= self
.rowLabels
[frm
]
126 oldData
= self
.data
[frm
]
127 del self
.rowLabels
[frm
]
131 self
.rowLabels
.insert(to
-1,oldLabel
)
132 self
.data
.insert(to
-1,oldData
)
134 self
.rowLabels
.insert(to
,oldLabel
)
135 self
.data
.insert(to
,oldData
)
140 msg
= gridlib
.GridTableMessage(
141 self
, gridlib
.GRIDTABLE_NOTIFY_ROWS_DELETED
, frm
, 1
144 grid
.ProcessTableMessage(msg
)
146 msg
= gridlib
.GridTableMessage(
147 self
, gridlib
.GRIDTABLE_NOTIFY_ROWS_INSERTED
, to
, 1
150 grid
.ProcessTableMessage(msg
)
154 #---------------------------------------------------------------------------
157 class DragableGrid(gridlib
.Grid
):
158 def __init__(self
, parent
, log
):
159 gridlib
.Grid
.__init
__(self
, parent
, -1)
161 table
= CustomDataTable(log
)
163 # The second parameter means that the grid is to take ownership of the
164 # table and will destroy it when done. Otherwise you would need to keep
165 # a reference to it and call it's Destroy method later.
166 self
.SetTable(table
, True)
168 # Enable Column moving
169 gridmovers
.GridColMover(self
)
170 self
.Bind(gridmovers
.EVT_GRID_COL_MOVE
, self
.OnColMove
, self
)
173 gridmovers
.GridRowMover(self
)
174 self
.Bind(gridmovers
.EVT_GRID_ROW_MOVE
, self
.OnRowMove
, self
)
176 # Event method called when a column move needs to take place
177 def OnColMove(self
,evt
):
178 frm
= evt
.GetMoveColumn() # Column being moved
179 to
= evt
.GetBeforeColumn() # Before which column to insert
180 self
.GetTable().MoveColumn(frm
,to
)
182 # Event method called when a row move needs to take place
183 def OnRowMove(self
,evt
):
184 frm
= evt
.GetMoveRow() # Row being moved
185 to
= evt
.GetBeforeRow() # Before which row to insert
186 self
.GetTable().MoveRow(frm
,to
)
188 #---------------------------------------------------------------------------
190 class TestFrame(wx
.Frame
):
191 def __init__(self
, parent
, log
):
192 wx
.Frame
.__init
__(self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480))
193 grid
= DragableGrid(self
, log
)
196 #---------------------------------------------------------------------------
198 if __name__
== '__main__':
200 app
= wx
.PySimpleApp()
201 frame
= TestFrame(None, sys
.stdout
)
205 #---------------------------------------------------------------------------