forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReorderableGrid.py
260 lines (223 loc) · 8.48 KB
/
ReorderableGrid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import wx
import os
import wx.grid as gridlib
import wx.lib.mixins.gridlabelrenderer as glr
import wx.lib.mixins.grid as gae
import Utils
class GridCellMultiLineStringRenderer(gridlib.PyGridCellRenderer):
def __init__(self):
gridlib.PyGridCellRenderer.__init__(self)
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
text = grid.GetCellValue(row, col)
dc.SetFont( attr.GetFont() )
hAlign, vAlign = attr.GetAlignment()
if isSelected:
bg = grid.GetSelectionBackground()
fg = grid.GetSelectionForeground()
else:
bg = attr.GetBackgroundColour()
fg = attr.GetTextColour()
dc.SetTextBackground(bg)
dc.SetTextForeground(fg)
dc.SetBrush(wx.Brush(bg, wx.SOLID))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangleRect(rect)
grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
def GetBestSize(self, grid, attr, dc, row, col):
text = grid.GetCellValue(row, col)
dc.SetFont(attr.GetFont())
w, h, lineHeight = dc.GetMultiLineTextExtent(text)
return wx.Size(w, h)
def Clone(self):
return GridCellMultiLineStringRenderer()
class ReorderableGridRowMixin( object ):
"""This mixin allows the use of grid rows as Rearrange sources, you can Rearrange
rows off of a grid to a text target. Only row labels are Rearrangegable, internal
cell contents are not.
You must Initialize this class *after* the wxGrid initialization.
example
class MyGrid(Grid, ReorderableGridRowMixin):
def __init__(self, parent, id):
Grid.__init__(self, parent, id)
ReorderableGridRowMixin.__init__(self)
"""
def __init__( self ):
rowWindow = self.GetGridRowLabelWindow()
# various flags to indicate whether we should have a select
# event or a Rearrange event
self._potentialRearrange = False
self._lastRow = None
self._enableReorderRows = True
rowWindow.Bind( wx.EVT_LEFT_DOWN, self.OnReorderableGridLeftDown )
rowWindow.Bind( wx.EVT_LEFT_UP, self.OnRearrangeEnd )
rowWindow.Bind( wx.EVT_MOTION, self.OnReorderableGridMotion )
self.Bind( wx.EVT_LEFT_UP, self.OnRearrangeEnd )
self.Bind( wx.EVT_MOTION, self.OnRearrangeEnd )
gridWindow = self.GetGridWindow()
gridWindow.Bind( wx.EVT_LEFT_UP, self.OnRearrangeEnd )
gridWindow.Bind( wx.EVT_MOTION, self.OnRearrangeEnd )
def EnableReorderRows( self, enableReorderRows ):
self._enableReorderRows = enableReorderRows
def OnReorderableGridLeftDown(self, evt):
if not self._enableReorderRows:
self._potentialRearrange = False
evt.Skip()
return
x,y = evt.GetX(), evt.GetY()
row, col = self.ReorderableGridRowXYToCell(x,y, colheight=0)
self.HideCellEditControl()
self._lastRow = row
self.ClearSelection()
self.SelectRow(row, True)
self._potentialRearrange = True
evt.Skip()
def OnRearrangeEnd(self, evt):
"""We are not Rearranging anymore, so unset the potentialRearrange flag"""
self._potentialRearrange = False
evt.Skip()
def copyRow( self, fromRow, toRow ):
for c in xrange(self.GetNumberCols()):
self.SetCellValue( toRow, c, self.GetCellValue(fromRow, c) )
self.SetCellBackgroundColour( toRow, c, self.GetCellBackgroundColour(fromRow, c) )
def OnReorderableGridMotion(self, evt):
"""We are moving so see whether this should be a Rearrange event or not"""
if not self._potentialRearrange:
evt.Skip()
return
if not self._enableReorderRows:
self._potentialRearrange = False
evt.Skip()
return
x, y = evt.GetX(), evt.GetY()
row, col = self.ReorderableGridRowXYToCell(x,y, colheight=0)
if row == self._lastRow:
return
self.DeselectRow( self._lastRow )
lastRowSave = [self.GetCellValue(self._lastRow, c) for c in xrange(self.GetNumberCols())]
lastRowBackgroundColourSave = [self.GetCellBackgroundColour(self._lastRow, c) for c in xrange(self.GetNumberCols())]
direction = cmp(row, self._lastRow)
for r in xrange(self._lastRow, row, direction ):
self.copyRow( r + direction, r )
for c in xrange(self.GetNumberCols()):
self.SetCellValue( row, c, lastRowSave[c] )
self.SetCellBackgroundColour( row, c, lastRowBackgroundColourSave[c] )
self.SelectRow( row, False )
self._lastRow = row
def ReorderableGridRowXYToCell(self, x, y, colheight=None, rowwidth=None):
# For virtual grids, XYToCell doesn't work properly
# For some reason, the width and heights of the labels
# are not computed properly and thw row and column
# returned are computed as if the window wasn't
# scrolled
# This function replaces XYToCell for Virtual Grids
if rowwidth is None:
rowwidth = self.GetGridRowLabelWindow().GetRect().width
if colheight is None:
colheight = self.GetGridColLabelWindow().GetRect().height
yunit, xunit = self.GetScrollPixelsPerUnit()
xoff = self.GetScrollPos(wx.HORIZONTAL) * xunit
yoff = self.GetScrollPos(wx.VERTICAL) * yunit
# the solution is to offset the x and y values
# by the width and height of the label windows
# and then adjust by the scroll position
# Then just go through the columns and rows
# incrementing by the current column and row sizes
# until the offset points lie within the computed
# bounding boxes.
x += xoff - rowwidth
xpos = 0
for col in range(self.GetNumberCols()):
nextx = xpos + self.GetColSize(col)
if xpos <= x <= nextx:
break
xpos = nextx
y += yoff - colheight
ypos = 0
for row in range(self.GetNumberRows()):
nexty = ypos + self.GetRowSize(row)
if ypos <= y <= nexty:
break
ypos = nexty
return row, col
class KeyboardNavigationGridMixin( object ):
def __init__( self ):
self.Bind( gridlib.EVT_GRID_EDITOR_CREATED, self.onCellEdit )
def onCellEdit( self, event ):
editor = event.GetControl()
editor.Bind( wx.EVT_KEY_DOWN, self.onEditorKey )
event.Skip()
def onEditorKey( self, event ):
keycode = event.GetKeyCode()
if keycode == wx.WXK_UP:
self.MoveCursorUp(False)
elif keycode == wx.WXK_DOWN:
self.MoveCursorDown(False)
elif keycode == wx.WXK_TAB:
if event.ShiftDown():
if self.GetGridCursorCol() == 0 and self.GetGridCursorRow() != 0:
self.MoveCursorUp(False)
for c in xrange(self.GetNumberCols()):
self.MoveCursorRight(False)
else:
self.MoveCursorLeft(False)
else:
if self.GetGridCursorCol() == self.GetNumberCols() - 1 and self.GetGridCursorRow() != self.GetNumberRows() - 1:
self.MoveCursorDown(False)
for c in xrange(self.GetNumberCols()):
self.MoveCursorLeft(False)
else:
self.MoveCursorRight(False)
else:
pass
event.Skip()
class SaveEditWhenFocusChangesGridMixin( object ):
def __init__(self):
self.Bind(gridlib.EVT_GRID_EDITOR_CREATED, self.OnGridEditorCreated)
def OnGridEditorCreated(self, event):
""" Bind the kill focus event to the newly instantiated cell editor """
editor = event.GetControl()
editor.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
event.Skip()
def OnKillFocus(self, event):
# Cell editor's grandparent, the grid GridWindow's parent, is the grid.
grid = event.GetEventObject().GetGrandParent()
grid.SaveEditControlValue()
grid.HideCellEditControl()
event.Skip()
########################################################################
class CornerReorderableGridLabelRenderer(glr.GridLabelRenderer):
def __init__(self):
self._bmp = wx.Bitmap( os.path.join(Utils.getImageFolder(), 'UpDown.png'), wx.BITMAP_TYPE_PNG )
def Draw(self, grid, dc, rect, rc):
if grid._enableReorderRows:
x = rect.left + (rect.width - self._bmp.GetWidth()) / 2
y = rect.top + (rect.height - self._bmp.GetHeight()) / 2
dc.DrawBitmap(self._bmp, x, y, True)
class ReorderableGrid( gridlib.Grid,
ReorderableGridRowMixin,
KeyboardNavigationGridMixin,
SaveEditWhenFocusChangesGridMixin,
glr.GridWithLabelRenderersMixin,
gae.GridAutoEditMixin ):
def __init__( self, parent, style = 0 ):
gridlib.Grid.__init__( self, parent, style=style )
ReorderableGridRowMixin.__init__( self )
KeyboardNavigationGridMixin.__init__( self )
SaveEditWhenFocusChangesGridMixin.__init__( self )
glr.GridWithLabelRenderersMixin.__init__(self)
gae.GridAutoEditMixin.__init__(self)
self.SetCornerLabelRenderer(CornerReorderableGridLabelRenderer())
def GetSelectedRows( self ):
rows=[]
gcr=self.GetGridCursorRow()
set1=self.GetSelectionBlockTopLeft()
set2=self.GetSelectionBlockBottomRight()
if len(set1):
assert len(set1)==len(set2)
for i in range(len(set1)):
for row in range(set1[i][0], set2[i][0]+1): # range in wx is inclusive of last element
if row not in rows:
rows.append(row)
else:
rows.append(gcr)
return rows