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