1 from wxPython
.wx
import *
2 from wxPython
.grid
import *
3 from wxPython
.lib
.gridmovers
import wxGridColMover
, EVT_GRID_COL_MOVE
4 from wxPython
.lib
.gridmovers
import wxGridRowMover
, EVT_GRID_ROW_MOVE
6 #---------------------------------------------------------------------------
8 class CustomDataTable(wxPyGridTableBase
):
12 def __init__(self
, log
):
13 wxPyGridTableBase
.__init
__(self
)
16 self
.identifiers
= ['id','ds','sv','pr','pl','op','fx','ts']
18 self
.rowLabels
= ['Row1','Row2','Row3']
20 self
.colLabels
= {'id':'ID','ds':'Description','sv':'Severity',
21 'pr':'Priority','pl':'Platform','op':'Opened?',
22 'fx':'Fixed?','ts':'Tested?'}
24 self
.data
= [{'id':1010,
25 'ds':"The foo doesn't bar",
34 'ds':"I've got a wicket in my wocket",
43 'ds':"Rectangle() returns a triangle",
53 #--------------------------------------------------
54 # required methods for the wxPyGridTableBase interface
56 def GetNumberRows(self
):
59 def GetNumberCols(self
):
60 return len(self
.identifiers
)
62 def IsEmptyCell(self
, row
, col
):
63 id = self
.identifiers
[col
]
64 return not self
.data
[row
][id]
66 def GetValue(self
, row
, col
):
67 id = self
.identifiers
[col
]
68 return self
.data
[row
][id]
70 def SetValue(self
, row
, col
, value
):
71 id = self
.identifiers
[col
]
72 self
.data
[row
][id] = value
74 #--------------------------------------------------
75 # Some optional methods
77 # Called when the grid needs to display column labels
78 def GetColLabelValue(self
, col
):
79 id = self
.identifiers
[col
]
80 return self
.colLabels
[id]
82 # Called when the grid needs to display row labels
83 def GetRowLabelValue(self
,row
):
84 return self
.rowLabels
[row
]
86 #--------------------------------------------------
87 # Methods added for demo purposes.
89 # The physical moving of the cols/rows is left to the implementer.
90 # Because of the dynamic nature of a wxGrid the physical moving of
91 # columns differs from implementation to implementation
94 def MoveColumn(self
,frm
,to
):
97 # Move the identifiers
98 old
= self
.identifiers
[frm
]
99 del self
.identifiers
[frm
]
101 self
.identifiers
.insert(to
-1,old
)
103 self
.identifiers
.insert(to
,old
)
107 msg
= wxGridTableMessage(self
,wxGRIDTABLE_NOTIFY_COLS_DELETED
,
109 grid
.ProcessTableMessage(msg
)
110 msg
= wxGridTableMessage(self
,wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
112 grid
.ProcessTableMessage(msg
)
116 def MoveRow(self
,frm
,to
):
117 grid
= self
.GetView()
119 # Move the rowLabels and data rows
120 oldLabel
= self
.rowLabels
[frm
]
121 oldData
= self
.data
[frm
]
122 del self
.rowLabels
[frm
]
125 self
.rowLabels
.insert(to
-1,oldLabel
)
126 self
.data
.insert(to
-1,oldData
)
128 self
.rowLabels
.insert(to
,oldLabel
)
129 self
.data
.insert(to
,oldData
)
133 msg
= wxGridTableMessage(self
,wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
135 grid
.ProcessTableMessage(msg
)
136 msg
= wxGridTableMessage(self
,wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
138 grid
.ProcessTableMessage(msg
)
142 #---------------------------------------------------------------------------
145 class DragableGrid(wxGrid
):
146 def __init__(self
, parent
, log
):
147 wxGrid
.__init
__(self
, parent
, -1)
149 table
= CustomDataTable(log
)
151 # The second parameter means that the grid is to take ownership of the
152 # table and will destroy it when done. Otherwise you would need to keep
153 # a reference to it and call it's Destroy method later.
154 self
.SetTable(table
, True)
156 # Enable Column moving
158 EVT_GRID_COL_MOVE(self
,self
.GetId(),self
.OnColMove
)
162 EVT_GRID_ROW_MOVE(self
,self
.GetId(),self
.OnRowMove
)
164 # Event method called when a column move needs to take place
165 def OnColMove(self
,evt
):
166 frm
= evt
.GetMoveColumn() # Column being moved
167 to
= evt
.GetBeforeColumn() # Before which column to insert
168 self
.GetTable().MoveColumn(frm
,to
)
170 # Event method called when a row move needs to take place
171 def OnRowMove(self
,evt
):
172 frm
= evt
.GetMoveRow() # Row being moved
173 to
= evt
.GetBeforeRow() # Before which row to insert
174 self
.GetTable().MoveRow(frm
,to
)
176 #---------------------------------------------------------------------------
178 class TestFrame(wxFrame
):
179 def __init__(self
, parent
, log
):
180 wxFrame
.__init
__(self
, parent
, -1, "Custom Table, data driven Grid Demo", size
=(640,480))
181 grid
= DragableGrid(self
, log
)
184 #---------------------------------------------------------------------------
186 if __name__
== '__main__':
188 app
= wxPySimpleApp()
189 frame
= TestFrame(None, sys
.stdout
)
193 #---------------------------------------------------------------------------