-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathColGrid.py
312 lines (252 loc) · 8.66 KB
/
ColGrid.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import wx
import wx.grid as Grid
import copy
#---------------------------------------------------------------------------
class ColTable( Grid.GridTableBase ):
"""
A custom wx.Grid Table using user supplied data
"""
def __init__( self ):
super().__init__()
"""
data is a list, indexed by col, containing a list of row values
"""
self.attrs = {} # Set of unique cell attributes.
self.rightAlign = False
self.leftAlignCols = set()
self.colRenderer = {}
# Column-oriented data.
# textColour and backgroundColour are store as a dict indexed by (row, col).
# Colour is a wx.Colour.
self.data = []
self.colnames = []
self.textColour = {}
self.backgroundColour = {}
def SetRightAlign( self, ra = True ):
self.rightAlign = ra
self.attrs = {}
def SetLeftAlignCols( self, col, la = True ):
if la:
self.leftAlignCols.add( col )
else:
try:
self.leftAlignCols.remove( col )
except KeyError:
pass
self.attrs = {}
def SetColRenderer( self, col, renderer ):
self.colRenderer[col] = renderer
self.attrs = {}
def _adjustDimension( self, grid, current, new, isCol ):
if isCol:
delmsg, addmsg = Grid.GRIDTABLE_NOTIFY_COLS_DELETED, Grid.GRIDTABLE_NOTIFY_COLS_APPENDED
else:
delmsg, addmsg = Grid.GRIDTABLE_NOTIFY_ROWS_DELETED, Grid.GRIDTABLE_NOTIFY_ROWS_APPENDED
if new < current:
msg = Grid.GridTableMessage( self, delmsg, new, current-new )
grid.ProcessTableMessage( msg )
elif new > current:
msg = Grid.GridTableMessage( self, addmsg, new-current )
grid.ProcessTableMessage( msg )
def Set( self, grid, data = None, colnames = None, textColour = None, backgroundColour = None ):
if colnames is not None:
self._adjustDimension( grid, len(self.colnames), len(colnames), True )
self.colnames = list(colnames)
if data is not None:
current = max( len(c) for c in self.data ) if self.data else 0
new = max( len(c) for c in data ) if data else 0
self._adjustDimension( grid, current, new, False )
self.data = copy.copy(data)
if textColour is not None:
self.textColour = dict(textColour)
if backgroundColour is not None:
self.backgroundColour = dict(backgroundColour)
self.attrs = {}
def SetColumn( self, grid, iCol, colData ):
self.data[iCol] = copy.copy(colData)
rect = grid.BlockToDeviceRect((0, iCol), (len(self.data[iCol])-1, iCol))
grid.GetGridWindow().RefreshRect( rect )
def SortByColumn( self, iCol, descending = False ):
if not self.data:
return
colLen = len(self.data[0])
if not all(len(colData) == colLen for colData in self.data):
raise ValueError( 'Cannot sort with different column lengths' )
allNumeric = True
for e in self.data[iCol]:
try:
float(e)
except Exception:
allNumeric = False
break
if allNumeric:
elementIndex = [(float(e), i) for i, e in enumerate(self.data[iCol])]
else:
elementIndex = [(e, i) for i, e in enumerate(self.data[iCol])]
elementIndex.sort()
for c in range(len(self.data)):
self.data[c] = [self.data[c][i] for e, i in elementIndex]
if descending:
self.data[c].reverse()
def GetData( self ):
return self.data
def GetColNames( self ):
return self.colnames
def isEmpty( self ):
return True if not self.data else False
def GetNumberCols(self):
try:
return len(self.colnames)
except TypeError:
return 0
def GetNumberRows(self):
try:
return max( len(c) for c in self.data )
except (TypeError, ValueError):
return 0
def GetColLabelValue(self, col):
try:
return self.colnames[col]
except (TypeError, IndexError):
return ''
def GetRowLabelValue(self, row):
return str(row+1)
def IsEmptyCell( self, row, col ):
try:
v = self.data[col][row]
return v is None or v == ''
except (TypeError, IndexError):
return True
def GetRawValue(self, row, col):
return '' if self.IsEmptyCell(row, col) else self.data[col][row]
def GetValue(self, row, col):
return str(self.GetRawValue(row, col))
def SetValue(self, row, col, value):
# Nothing to do - everthing is read-only.
pass
def DeleteCols( self, pos = 0, numCols = 1, updateLabels = True, grid = None ):
oldCols = len(self.colnames) if self.colnames else 0
if self.data:
del self.data[pos:pos+numCols]
if self.colnames:
del self.colnames[pos:pos+numCols]
posMax = pos + numCols
for a in ['textColour', 'backgroundColour']:
if not hasattr(self, a):
continue
colD = {}
for (r, c), colour in getattr(self, a).items():
if c < pos:
colD[(r, c)] = colour
elif posMax <= c:
colD[(r, c-numCols)] = colour
setattr( self, a, colD )
newCols = len(self.colnames) if self.colnames else 0
self._adjustDimension( grid, oldCols, newCols, True )
self.attrs = {}
def GetAttr(self, row, col, someExtraParameter ):
hCellAlign = None
if col in self.leftAlignCols:
hCellAlign = wx.ALIGN_LEFT
elif self.rightAlign:
hCellAlign = wx.ALIGN_RIGHT
rc = (row, col)
textColour = self.textColour.get(rc, None)
if textColour:
textColour = textColour.GetAsString(wx.C2S_HTML_SYNTAX)
backgroundColour = self.backgroundColour.get(rc, None)
if backgroundColour:
backgroundColour = backgroundColour.GetAsString(wx.C2S_HTML_SYNTAX)
key = (textColour, backgroundColour, col, hCellAlign)
try:
attr = self.attrs[key]
except KeyError:
# Create an attribute for the cache.
attr = Grid.GridCellAttr()
attr.SetReadOnly( True ) # All cells read-only.
if rc in self.textColour:
attr.SetTextColour( self.textColour[rc] )
if rc in self.backgroundColour:
attr.SetBackgroundColour( self.backgroundColour[rc] )
if hCellAlign is not None:
attr.SetAlignment( hAlign=hCellAlign, vAlign=wx.ALIGN_CENTRE )
renderer = self.colRenderer.get(col, None)
if renderer:
attr.SetRenderer( renderer.Clone() )
self.attrs[key] = attr
attr.IncRef()
return attr
def SetAttr( self, row, col, attr ): pass
def SetRowAttr( self, row, attr ): pass
def SetColAttr( self, col, attr ) : pass
def UpdateAttrRows( self, pos, numRows ) : pass
def UpdateAttrCols( self, pos, numCols ) : pass
def ResetView(self, grid):
"""
(Grid) -> Reset the grid view. Call this to redraw the grid.
"""
self.attrs = {}
grid.AdjustScrollbars()
grid.ForceRefresh()
def UpdateValues( self, grid ):
"""Update all displayed values"""
if self.data:
rect = grid.BlockToDeviceRect((0, 0), (len(self.data[0])-1, len(self.data)-1))
grid.GetGridWindow().RefreshRect( rect )
# --------------------------------------------------------------------
# Sample Grid
class ColGrid(Grid.Grid):
def __init__(self, parent, data = None, colnames = None, textColour = None, backgroundColour = None, style = 0 ):
"""parent, data, colnames, plugins=None
Initialize a grid using the data defined in data and colnames
"""
# The base class must be initialized *first*
Grid.Grid.__init__(self, parent, style = style)
self._table = ColTable()
self.SetTable( self._table )
self.Set( data, colnames, textColour, backgroundColour )
self.zoomLevel = 1.0
def Reset( self ):
"""reset the view based on the data in the table. Call this when rows are added or destroyed"""
self._table.ResetView(self)
def Set( self, data = None, colnames = None, textColour = None, backgroundColour = None ):
self._table.Set( self, data, colnames, textColour, backgroundColour )
def SetColumn( self, iCol, colData ):
self._table.SetColumn( self, iCol, colData )
def SetColRenderer( self, col, renderer ):
self._table.SetColRenderer( col, renderer )
def GetData( self ):
return self._table.GetData()
def GetColNames( self ):
return self._table.GetColNames()
def DeleteCols( self, pos = 0, numCols = 1, updateLabels = True ):
self._table.DeleteCols(pos, numCols, updateLabels, self)
self.Reset()
def Zoom( self, zoomIn = True ):
factor = 2 if zoomIn else 0.5
if not 1.0/4.0 <= self.zoomLevel * factor <= 4.0:
return
self.zoomLevel *= factor
font = self.GetDefaultCellFont()
font.SetPointSize( int(font.GetPointSize() * factor) )
self.SetDefaultCellFont( font )
font = self.GetLabelFont()
font.SetPointSize( int(font.GetPointSize() * factor) )
self.SetLabelFont( font )
self.SetColLabelSize( int(self.GetColLabelSize() * factor) )
self.AutoSize()
self.Reset()
def SetRightAlign( self, ra = True ):
self._table.SetRightAlign( ra )
self.Reset()
def SetLeftAlignCols( self, cols ):
self._table.leftAlignCols = set()
for c in cols:
self._table.leftAlignCols.add( c )
self.Reset()
def SortByColumn( self, iCol, descending = False ):
self._table.SortByColumn( iCol, descending )
self.Refresh()
def clearGrid( self ):
self.Set( data = [], colnames = [], textColour = {}, backgroundColour = {} )
self.Reset()