]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/GridDragable.py
Don't decref if the interpreter is not initialized, (eg. it's in its
[wxWidgets.git] / wxPython / demo / GridDragable.py
CommitLineData
8fa876ca
RD
1
2import wx
3import wx.grid as gridlib
4import wx.lib.gridmovers as gridmovers
1e4a197e
RD
5
6#---------------------------------------------------------------------------
7
8fa876ca 8class CustomDataTable(gridlib.PyGridTableBase):
1e4a197e 9 def __init__(self, log):
8fa876ca 10 gridlib.PyGridTableBase.__init__(self)
1e4a197e
RD
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()
8fa876ca 93
1e4a197e
RD
94 if grid:
95 # Move the identifiers
96 old = self.identifiers[frm]
97 del self.identifiers[frm]
8fa876ca 98
1e4a197e
RD
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()
8fa876ca
RD
106 msg = gridlib.GridTableMessage(
107 self, gridlib.GRIDTABLE_NOTIFY_COLS_DELETED, frm, 1
108 )
109
1e4a197e 110 grid.ProcessTableMessage(msg)
8fa876ca
RD
111
112 msg = gridlib.GridTableMessage(
113 self, gridlib.GRIDTABLE_NOTIFY_COLS_INSERTED, to, 1
114 )
115
1e4a197e
RD
116 grid.ProcessTableMessage(msg)
117 grid.EndBatch()
118
119 # Move the row
120 def MoveRow(self,frm,to):
121 grid = self.GetView()
8fa876ca 122
1e4a197e
RD
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]
8fa876ca 129
1e4a197e
RD
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()
8fa876ca
RD
139
140 msg = gridlib.GridTableMessage(
141 self, gridlib.GRIDTABLE_NOTIFY_ROWS_DELETED, frm, 1
142 )
143
1e4a197e 144 grid.ProcessTableMessage(msg)
8fa876ca
RD
145
146 msg = gridlib.GridTableMessage(
147 self, gridlib.GRIDTABLE_NOTIFY_ROWS_INSERTED, to, 1
148 )
149
1e4a197e
RD
150 grid.ProcessTableMessage(msg)
151 grid.EndBatch()
152
153
154#---------------------------------------------------------------------------
155
156
8fa876ca 157class DragableGrid(gridlib.Grid):
1e4a197e 158 def __init__(self, parent, log):
8fa876ca 159 gridlib.Grid.__init__(self, parent, -1)
1e4a197e
RD
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
33785d9f
RD
169 gridmovers.GridColMover(self)
170 self.Bind(gridmovers.EVT_GRID_COL_MOVE, self.OnColMove, self)
1e4a197e
RD
171
172 # Enable Row moving
33785d9f
RD
173 gridmovers.GridRowMover(self)
174 self.Bind(gridmovers.EVT_GRID_ROW_MOVE, self.OnRowMove, self)
1e4a197e
RD
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
8fa876ca 190class TestFrame(wx.Frame):
1e4a197e 191 def __init__(self, parent, log):
8fa876ca 192 wx.Frame.__init__(self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480))
1e4a197e
RD
193 grid = DragableGrid(self, log)
194
195
196#---------------------------------------------------------------------------
197
198if __name__ == '__main__':
199 import sys
8fa876ca 200 app = wx.PySimpleApp()
1e4a197e
RD
201 frame = TestFrame(None, sys.stdout)
202 frame.Show(True)
203 app.MainLoop()
204
205#---------------------------------------------------------------------------