]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridDragable.py
backwards compatibility aliases can be used in the wxPython namespace
[wxWidgets.git] / wxPython / demo / GridDragable.py
1 # 11/6/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Modified for V2.5
4 #
5 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6 #
7 # o wx renamer didn't appear to 'catch' all the classes in
8 # wx.lib.gridmovers
9 # o Event binder not working properly with wx.lib.gridmovers
10 #
11
12 import wx
13 import wx.grid as gridlib
14 import wx.lib.gridmovers as gridmovers
15
16 #---------------------------------------------------------------------------
17
18 class CustomDataTable(gridlib.PyGridTableBase):
19 def __init__(self, log):
20 gridlib.PyGridTableBase.__init__(self)
21 self.log = log
22
23 self.identifiers = ['id','ds','sv','pr','pl','op','fx','ts']
24
25 self.rowLabels = ['Row1','Row2','Row3']
26
27 self.colLabels = {'id':'ID','ds':'Description','sv':'Severity',
28 'pr':'Priority','pl':'Platform','op':'Opened?',
29 'fx':'Fixed?','ts':'Tested?'}
30
31 self.data = [{'id':1010,
32 'ds':"The foo doesn't bar",
33 'sv':"major",
34 'pr':1,
35 'pl':'MSW',
36 'op':1,
37 'fx':1,
38 'ts':1
39 },
40 {'id':1011,
41 'ds':"I've got a wicket in my wocket",
42 'sv':"wish list",
43 'pr':2,
44 'pl':'other',
45 'op':0,
46 'fx':0,
47 'ts':0
48 },
49 {'id':1012,
50 'ds':"Rectangle() returns a triangle",
51 'sv':"critical",
52 'pr':5,
53 'pl':'all',
54 'op':0,
55 'fx':0,
56 'ts':0
57 }
58 ]
59
60 #--------------------------------------------------
61 # required methods for the wxPyGridTableBase interface
62
63 def GetNumberRows(self):
64 return len(self.data)
65
66 def GetNumberCols(self):
67 return len(self.identifiers)
68
69 def IsEmptyCell(self, row, col):
70 id = self.identifiers[col]
71 return not self.data[row][id]
72
73 def GetValue(self, row, col):
74 id = self.identifiers[col]
75 return self.data[row][id]
76
77 def SetValue(self, row, col, value):
78 id = self.identifiers[col]
79 self.data[row][id] = value
80
81 #--------------------------------------------------
82 # Some optional methods
83
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]
88
89 # Called when the grid needs to display row labels
90 def GetRowLabelValue(self,row):
91 return self.rowLabels[row]
92
93 #--------------------------------------------------
94 # Methods added for demo purposes.
95
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
99
100 # Move the column
101 def MoveColumn(self,frm,to):
102 grid = self.GetView()
103
104 if grid:
105 # Move the identifiers
106 old = self.identifiers[frm]
107 del self.identifiers[frm]
108
109 if to > frm:
110 self.identifiers.insert(to-1,old)
111 else:
112 self.identifiers.insert(to,old)
113
114 # Notify the grid
115 grid.BeginBatch()
116 msg = gridlib.GridTableMessage(
117 self, gridlib.GRIDTABLE_NOTIFY_COLS_DELETED, frm, 1
118 )
119
120 grid.ProcessTableMessage(msg)
121
122 msg = gridlib.GridTableMessage(
123 self, gridlib.GRIDTABLE_NOTIFY_COLS_INSERTED, to, 1
124 )
125
126 grid.ProcessTableMessage(msg)
127 grid.EndBatch()
128
129 # Move the row
130 def MoveRow(self,frm,to):
131 grid = self.GetView()
132
133 if grid:
134 # Move the rowLabels and data rows
135 oldLabel = self.rowLabels[frm]
136 oldData = self.data[frm]
137 del self.rowLabels[frm]
138 del self.data[frm]
139
140 if to > frm:
141 self.rowLabels.insert(to-1,oldLabel)
142 self.data.insert(to-1,oldData)
143 else:
144 self.rowLabels.insert(to,oldLabel)
145 self.data.insert(to,oldData)
146
147 # Notify the grid
148 grid.BeginBatch()
149
150 msg = gridlib.GridTableMessage(
151 self, gridlib.GRIDTABLE_NOTIFY_ROWS_DELETED, frm, 1
152 )
153
154 grid.ProcessTableMessage(msg)
155
156 msg = gridlib.GridTableMessage(
157 self, gridlib.GRIDTABLE_NOTIFY_ROWS_INSERTED, to, 1
158 )
159
160 grid.ProcessTableMessage(msg)
161 grid.EndBatch()
162
163
164 #---------------------------------------------------------------------------
165
166
167 class DragableGrid(gridlib.Grid):
168 def __init__(self, parent, log):
169 gridlib.Grid.__init__(self, parent, -1)
170
171 table = CustomDataTable(log)
172
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)
177
178 # Enable Column moving
179 gridmovers.GridColMover(self)
180 self.Bind(gridmovers.EVT_GRID_COL_MOVE, self.OnColMove, self)
181
182 # Enable Row moving
183 gridmovers.GridRowMover(self)
184 self.Bind(gridmovers.EVT_GRID_ROW_MOVE, self.OnRowMove, self)
185
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)
191
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)
197
198 #---------------------------------------------------------------------------
199
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)
204
205
206 #---------------------------------------------------------------------------
207
208 if __name__ == '__main__':
209 import sys
210 app = wx.PySimpleApp()
211 frame = TestFrame(None, sys.stdout)
212 frame.Show(True)
213 app.MainLoop()
214
215 #---------------------------------------------------------------------------