]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/GridDragable.py
reworked vertical scrolling code to correspond better to what I think is correct...
[wxWidgets.git] / wxPython / demo / GridDragable.py
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
5
6 #---------------------------------------------------------------------------
7
8 class CustomDataTable(wxPyGridTableBase):
9 """
10 """
11
12 def __init__(self, log):
13 wxPyGridTableBase.__init__(self)
14 self.log = log
15
16 self.identifiers = ['id','ds','sv','pr','pl','op','fx','ts']
17
18 self.rowLabels = ['Row1','Row2','Row3']
19
20 self.colLabels = {'id':'ID','ds':'Description','sv':'Severity',
21 'pr':'Priority','pl':'Platform','op':'Opened?',
22 'fx':'Fixed?','ts':'Tested?'}
23
24 self.data = [{'id':1010,
25 'ds':"The foo doesn't bar",
26 'sv':"major",
27 'pr':1,
28 'pl':'MSW',
29 'op':1,
30 'fx':1,
31 'ts':1
32 },
33 {'id':1011,
34 'ds':"I've got a wicket in my wocket",
35 'sv':"wish list",
36 'pr':2,
37 'pl':'other',
38 'op':0,
39 'fx':0,
40 'ts':0
41 },
42 {'id':1012,
43 'ds':"Rectangle() returns a triangle",
44 'sv':"critical",
45 'pr':5,
46 'pl':'all',
47 'op':0,
48 'fx':0,
49 'ts':0
50 }
51 ]
52
53 #--------------------------------------------------
54 # required methods for the wxPyGridTableBase interface
55
56 def GetNumberRows(self):
57 return len(self.data)
58
59 def GetNumberCols(self):
60 return len(self.identifiers)
61
62 def IsEmptyCell(self, row, col):
63 id = self.identifiers[col]
64 return not self.data[row][id]
65
66 def GetValue(self, row, col):
67 id = self.identifiers[col]
68 return self.data[row][id]
69
70 def SetValue(self, row, col, value):
71 id = self.identifiers[col]
72 self.data[row][id] = value
73
74 #--------------------------------------------------
75 # Some optional methods
76
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]
81
82 # Called when the grid needs to display row labels
83 def GetRowLabelValue(self,row):
84 return self.rowLabels[row]
85
86 #--------------------------------------------------
87 # Methods added for demo purposes.
88
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
92
93 # Move the column
94 def MoveColumn(self,frm,to):
95 grid = self.GetView()
96 if grid:
97 # Move the identifiers
98 old = self.identifiers[frm]
99 del self.identifiers[frm]
100 if to > frm:
101 self.identifiers.insert(to-1,old)
102 else:
103 self.identifiers.insert(to,old)
104
105 # Notify the grid
106 grid.BeginBatch()
107 msg = wxGridTableMessage(self,wxGRIDTABLE_NOTIFY_COLS_DELETED,
108 frm,1)
109 grid.ProcessTableMessage(msg)
110 msg = wxGridTableMessage(self,wxGRIDTABLE_NOTIFY_COLS_INSERTED,
111 to,1)
112 grid.ProcessTableMessage(msg)
113 grid.EndBatch()
114
115 # Move the row
116 def MoveRow(self,frm,to):
117 grid = self.GetView()
118 if grid:
119 # Move the rowLabels and data rows
120 oldLabel = self.rowLabels[frm]
121 oldData = self.data[frm]
122 del self.rowLabels[frm]
123 del self.data[frm]
124 if to > frm:
125 self.rowLabels.insert(to-1,oldLabel)
126 self.data.insert(to-1,oldData)
127 else:
128 self.rowLabels.insert(to,oldLabel)
129 self.data.insert(to,oldData)
130
131 # Notify the grid
132 grid.BeginBatch()
133 msg = wxGridTableMessage(self,wxGRIDTABLE_NOTIFY_ROWS_DELETED,
134 frm,1)
135 grid.ProcessTableMessage(msg)
136 msg = wxGridTableMessage(self,wxGRIDTABLE_NOTIFY_ROWS_INSERTED,
137 to,1)
138 grid.ProcessTableMessage(msg)
139 grid.EndBatch()
140
141
142 #---------------------------------------------------------------------------
143
144
145 class DragableGrid(wxGrid):
146 def __init__(self, parent, log):
147 wxGrid.__init__(self, parent, -1)
148
149 table = CustomDataTable(log)
150
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)
155
156 # Enable Column moving
157 wxGridColMover(self)
158 EVT_GRID_COL_MOVE(self,self.GetId(),self.OnColMove)
159
160 # Enable Row moving
161 wxGridRowMover(self)
162 EVT_GRID_ROW_MOVE(self,self.GetId(),self.OnRowMove)
163
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)
169
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)
175
176 #---------------------------------------------------------------------------
177
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)
182
183
184 #---------------------------------------------------------------------------
185
186 if __name__ == '__main__':
187 import sys
188 app = wxPySimpleApp()
189 frame = TestFrame(None, sys.stdout)
190 frame.Show(True)
191 app.MainLoop()
192
193 #---------------------------------------------------------------------------