~launchpad-p-s/sofastatistics/main

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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import wx
import wx.grid
import pprint

import text_browser

COL_STR = "col_string"
COL_INT = "col_integer"
COL_FLOAT = "col_float"
COL_TEXT_BROWSE = "col_button"


class TableEntryDlg(wx.Dialog):
    def __init__(self, title, grid_size, col_dets, data, new_grid_data):
        """
        col_dets - [(col_type, col_label, col_width), ( , ) ...]
        data - list of tuples (must have at least one item, even if only a 
            "rename me".
        new_grid_data - is effectively "returned".  Add details to it in form 
            of a list of tuples.
        """
        wx.Dialog.__init__(self, None, title=title,
                          size=(400,400), 
                          style=wx.RESIZE_BORDER|wx.CAPTION|wx.CLOSE_BOX|
                              wx.SYSTEM_MENU)
        self.panel = wx.Panel(self)
        self.szrMain = wx.BoxSizer(wx.VERTICAL)
        self.tabentry = TableEntry(self, self.panel, self.szrMain, grid_size, 
                                   col_dets, data, new_grid_data)
        # Close
        self.SetupButtons()
        # sizers
        self.szrMain.Add(self.szrButtons, 0, wx.ALL, 10)
        self.panel.SetSizer(self.szrMain)
        self.szrMain.SetSizeHints(self)
        self.Layout()
        self.tabentry.grid.SetFocus()
        
    def SetupButtons(self):
        "Separated for text_browser reuse"
        btnCancel = wx.Button(self.panel, wx.ID_CANCEL)
        btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)            
        btnOK = wx.Button(self.panel, wx.ID_OK) # must have ID of wx.ID_OK 
        # to trigger validators (no event binding needed) and 
        # for std dialog button layout
        btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
        btnOK.SetDefault()
        # using the approach which will follow the platform convention 
        # for standard buttons
        self.szrButtons = wx.StdDialogButtonSizer()
        self.szrButtons.AddButton(btnCancel)
        self.szrButtons.AddButton(btnOK)
        self.szrButtons.Realize()

    def OnCancel(self, event):
        self.Destroy()
        self.SetReturnCode(wx.ID_CANCEL)

    def OnOK(self, event):
        self.tabentry.UpdateNewGridData()
        self.Destroy()
        self.SetReturnCode(wx.ID_OK)
    
                
class TableEntry(object):
    def __init__(self, frame, panel, szr, read_only, grid_size, col_dets, 
                 data, new_grid_data):
        """
        data - list of tuples (must have at least one item, even if only a 
            "rename me".
        col_dets - [(col_type, col_label), ( , ) ...]
        new_grid_data - is effectively "returned" - add details to it in form 
            of a list of tuples.
        """
        self.frame = frame
        self.panel = panel
        self.szr = szr
        self.read_only = read_only
        self.SetupGrid(grid_size, col_dets, data, new_grid_data)
        self.szr.Add(self.grid, 1, wx.GROW|wx.ALL, 5)

    def SetupGrid(self, size, col_dets, data, new_grid_data):
        """
        Set up grid.  Convenient to separate so can reuse when subclassing,
            perhaps to add extra controls.
        col dets - the col det tuple should be label, type, min, max
        """
        self.col_dets = col_dets
        # store any fixed min col_widths
        self.col_widths = [None for x in range(len(col_dets))] # initialise
        for col_idx, col_det in enumerate(self.col_dets):
            if len(col_det) >= 3:
                self.col_widths[col_idx] = col_det[2]
        data.sort(key=lambda s: s[0])
        self.data = data
        self.new_grid_data = new_grid_data
        self.prev_vals = []
        self.new_editor_shown = False
        # grid control
        self.grid = wx.grid.Grid(self.panel, size=size)
        self.rows_n = len(self.data) # data rows only - not inc new entry row
        self.cols_n = len(self.col_dets)
        if self.rows_n:
            data_cols_n = len(data[0])
            #pprint.pprint(data) # debug
            if data_cols_n != self.cols_n:
                raise Exception, "There must be one set of column details " + \
                    "per column of data (currently %s details for %s columns)" % \
                    (self.cols_n, data_cols_n)
        self.grid.CreateGrid(numRows=self.rows_n + 1, # plus data entry row
                             numCols=self.cols_n)
        self.grid.EnableEditing(not self.read_only)
        # Set any col min widths specifically specified
        for col_idx in range(len(self.col_dets)):
            col_width = self.col_widths[col_idx]
            if col_width:
                self.grid.SetColMinimalWidth(col_idx, col_width)
                self.grid.SetColSize(col_idx, col_width) # otherwise will only see effect after resizing
            else:
                self.grid.AutoSizeColumn(col_idx, setAsMin=False)
        self.grid.ForceRefresh()
        # set col rendering and editing (string is default)
        for col_idx, col_det in enumerate(self.col_dets):
            col_type = col_det[1]
            if col_type == COL_INT:
                self.grid.SetColFormatNumber(col_idx)
            elif col_type == COL_FLOAT:
                width, precision = self.GetWidthPrecision(col_idx)
                self.grid.SetColFormatFloat(col_idx, width, precision)
            # must set editor cell by cell amazingly
            for j in range(self.rows_n + 1):
                renderer, editor = self.GetNewRendererEditor(col_idx)
                self.grid.SetCellRenderer(j, col_idx, renderer)
                self.grid.SetCellEditor(j, col_idx, editor)
        # grid event handling
        self.grid.Bind(text_browser.EVT_TEXT_BROWSE_KEY_DOWN, 
                       self.OnTextBrowseKeyDown)        
        self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnCellChange)
        self.grid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
        self.frame.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN, self.EditorShown)
        self.frame.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN, self.EditorHidden)
        self.grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.grid.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.OnLabelClick)
        for col_idx, col_det in enumerate(self.col_dets):
            self.grid.SetColLabelValue(col_idx, col_det[0])
        for i in range(self.rows_n):
            for j in range(self.cols_n):
                self.grid.SetCellValue(row=i, col=j, s=str(data[i][j]))
        self.grid.SetRowLabelValue(self.rows_n, "*")
        self.grid.SetGridCursor(self.rows_n, 0)
        self.grid.MakeCellVisible(self.rows_n, 0)
    
    def OnTextBrowseKeyDown(self, event):
        if event.GetKeyCode() in [wx.WXK_RETURN]:
            self.grid.DisableCellEditControl()
            self.grid.MoveCursorDown(expandSelection=False)
    
    def GetNewRendererEditor(self, col_idx):
        """
        For a given column index, return a fresh renderer and editor object.
        Objects must be unique to cell.
        Returns renderer, editor.
        Nearly (but not quite) making classes ;-)
        """
        col_type = self.col_dets[col_idx][1]
        if col_type == COL_INT:
            try:
                min = self.col_dets[col_idx][2]
            except Exception:
                min = -1
            try:
                max = self.col_dets[col_idx][3]
            except Exception:
                max = min
            renderer = wx.grid.GridCellNumberRenderer()
            editor = wx.grid.GridCellNumberEditor(min, max)
        elif col_type == COL_FLOAT:
            width, precision = self.GetWidthPrecision(col_idx)
            renderer = wx.grid.GridCellFloatRenderer(width, precision)
            editor = wx.grid.GridCellFloatEditor(width, precision)
        elif col_type == COL_TEXT_BROWSE:
            renderer = wx.grid.GridCellStringRenderer()
            file_phrase = self.col_dets[col_idx][3]
            if len(self.col_dets[col_idx]) == 5:
                wildcard = self.col_dets[col_idx][4]
            else:
                wildcard = "Any file (*.*)|*.*"
            editor = text_browser.GridCellTextBrowseEditor(file_phrase, 
                                                           wildcard)
        else:
            renderer = wx.grid.GridCellStringRenderer()
            editor = wx.grid.GridCellTextEditor()
        return renderer, editor

    def GetWidthPrecision(self, col_idx):
        """
        Returns width, precision.
        """
        # the col tuple should be label, type, width, precision
        try:
            width = self.col_dets[col_idx][2]
        except Exception:
            width = 5
        try:
            precision = self.col_dets[col_idx][3]
        except Exception:
            precision = 1
        return width, precision

    def OnLabelClick(self, event):
        "Need to give grid the focus so can process keystrokes e.g. delete"
        self.grid.SetFocus()
        event.Skip()

    def OnKeyDown(self, event):
        """
        http://wiki.wxpython.org/AnotherTutorial#head-999ff1e3fbf5694a51a91cf4ed2140f692da013c
        """
        if event.GetKeyCode() in [wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE]:
            self.TryToDeleteRow()
        else:
            event.Skip()

    def TryToDeleteRow(self):
        """
        Delete row if a row selected and not the data entry row
            and put focus on new line.
        """
        selected_rows = self.grid.GetSelectedRows()
        if len(selected_rows) == 1:
            row = selected_rows[0]
            if row != self.rows_n:
                self.grid.DeleteRows(pos=row, numRows=1)
                self.rows_n -= 1
                self.grid.SetRowLabelValue(self.rows_n, "*")
                self.grid.SetGridCursor(self.rows_n, 0)
                self.grid.HideCellEditControl()
                self.grid.ForceRefresh()
                self.SafeLayoutAdjustment()
    
    def EditorShown(self, event):
        # disable resizing until finished
        self.grid.DisableDragColSize()
        self.grid.DisableDragRowSize()
        if event.GetRow() == self.rows_n:
            self.new_editor_shown = True
        event.Skip()
        
    def EditorHidden(self, event):
        # re-enable resizing
        self.grid.EnableDragColSize()
        self.grid.EnableDragRowSize()
        self.new_editor_shown = False
        event.Skip()
        
    def OnSelectCell(self, event):
        "Store value in case we need to undo"
        row = event.GetRow()
        col = event.GetCol()
        try:
            prev_val = self.grid.GetCellValue(row, col)
            prev_tup = ((row, col), prev_val)
            self.prev_vals.append(prev_tup)
        except Exception, e:
            pass
        if len(self.prev_vals) > 2: # only need two to manage undos
            self.prev_vals.pop(0)
        new_entry = (row == self.rows_n)
        if new_entry:
            if (self.new_editor_shown or self.RowHasData(row)):
                self.grid.SetRowLabelValue(self.rows_n, "...")
            else:
                self.grid.SetRowLabelValue(self.rows_n, "*")
        event.Skip() # continue - I'm only being a man-in-the-middle ;-)

    def OnCellChange(self, event):
        row = event.GetRow()
        col = event.GetCol()
        new_entry = (row == self.rows_n) # if 3 rows (0,1,2) 
            # row 3 will be the new entry one
        if new_entry:
            if self.ValidRow(row=row):
                self.AddRow(row)
                event.Skip()
            else:
                self.grid.SetRowLabelValue(self.rows_n, "...")
                #print "Can't save new record - not valid"
                self.SafeLayoutAdjustment()
                event.Skip()
        else:
            if not self.ValidRow(row=row):
                #print "Invalid row"
                self.UndoCell(row, col)
            else:
                self.SafeLayoutAdjustment()
    
    def AddRow(self, row):
        ""
        # change label from * and add a new entry row on end of grid
        self.grid.AppendRows()
        # set up cell rendering and editing
        for col_idx in range(self.cols_n):
            renderer, editor = self.GetNewRendererEditor(col_idx)
            self.grid.SetCellRenderer(row + 1, col_idx, renderer)
            self.grid.SetCellEditor(row + 1, col_idx, editor)
        self.grid.SetRowLabelValue(self.rows_n, str(self.rows_n + 1))
        self.rows_n += 1
        self.grid.SetRowLabelValue(self.rows_n, "*")
        self.SafeLayoutAdjustment()
        # jump to first cell
        self.grid.SetGridCursor(self.rows_n, 0)
        # ensure we scroll there completely
        self.grid.MakeCellVisible(self.rows_n, 0)
        
    def SafeLayoutAdjustment(self):
        """
        Uses CallAfter to avoid infinite recursion.
        http://lists.wxwidgets.org/pipermail/wxpython-users/2007-April/063536.html
        """
        wx.CallAfter(self.RunSafeLayoutAdjustment)
    
    def RunSafeLayoutAdjustment(self):
        for col_idx in range(len(self.col_dets)):
            current_width = self.grid.GetColSize(col_idx)
            # identify optimal width according to content
            self.grid.AutoSizeColumn(col_idx, setAsMin=False)
            new_width = self.grid.GetColSize(col_idx)
            if new_width < current_width: # only the user can shrink a column
                # restore to current size
                self.grid.SetColSize(col_idx, current_width)
        # otherwise will only see effect after resizing
        self.grid.ForceRefresh()
    
    def UndoCell(self, row, col):
        """
        Restore original value.
        If more than one, this will be because we have selected another 
            cell while editing another.  The select event fires first adding 
            another item to prev_vals.
        NB not activated on number and float formatted cells.  These will 
            default to zero if you try to save '' to them.
        """
        for (prev_row, prev_col), prev_val in self.prev_vals:
            if prev_row == row and prev_col == col:
                self.grid.SetCellValue(row, col, prev_val)
                break

    def ValidRow(self, row):
        "Is row valid?"
        row_complete = True
        for i in range(self.cols_n):
            cell_val = self.grid.GetCellValue(row=row, col=i)
            if not cell_val:
                row_complete = False
                break
        return row_complete

    def RowHasData(self, row):
        """
        Has the row got any data stored yet?
        NB data won't be picked up if you are in the middle of entering 
            it.
        """
        has_data = False
        for i in range(self.cols_n):
            cell_val = self.grid.GetCellValue(row=row, col=i)
            if cell_val:
                has_data = True
                break
        return has_data

    def UpdateNewGridData(self):
        """
        Update new_grid_data.  Separated for reuse.
        """
        # get data from grid (except for final row (either empty or not saved)
        for i in range(self.rows_n):
            row_data = []
            for j, col_type in enumerate(self.col_dets):
                val = self.grid.GetCellValue(row=i, col=j)
                row_data.append(val)
            self.new_grid_data.append(tuple(row_data))


if __name__ == "__main__":
    app = wx.PySimpleApp()
    data = [(1, "Auckland"), (2, "Wellington"), (3, "Hamilton"), 
            (4, "Waiuku"), (5, "Tuakau"), (6, "Pukekohe"), (7, "Orewa"),]
    #        (8, "The People's Republic of China"), 
    #        (9, "The Democratic Republic of Congo")]
    new_grid_data = []
    # For text_browse - col label, type, width, file phrase, wildcard
    # example wildcard: "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif"
    col_dets = [("Value", COL_INT, 1, 20), 
                ("Path", COL_TEXT_BROWSE, 400, "Choose a SOFA label file", 
                 "SOFA label files (*.lbl)|*.lbl")]
    grid_size = (550, 350)
    dlg = TableEntryDlg("Settings", grid_size, col_dets, data, new_grid_data)
    dlg.Show()
    app.MainLoop()
    pprint.pprint(new_grid_data)