1 | """ |
---|
2 | Implement grid used to store data |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import numpy |
---|
6 | import sys |
---|
7 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
8 | import wx.grid as Grid |
---|
9 | import wx.aui |
---|
10 | from wx.aui import AuiNotebook as nb |
---|
11 | from sans.guiframe.panel_base import PanelBase |
---|
12 | import wx.lib.sheet as sheet |
---|
13 | |
---|
14 | from sans.guiframe.events import NewPlotEvent |
---|
15 | from sans.guiframe.events import StatusEvent |
---|
16 | from sans.guiframe.dataFitting import Data1D |
---|
17 | |
---|
18 | |
---|
19 | class GridPage(sheet.CSheet): |
---|
20 | def __init__(self, parent, panel=None): |
---|
21 | """ |
---|
22 | """ |
---|
23 | sheet.CSheet.__init__(self, parent) |
---|
24 | self.SetLabelBackgroundColour('#DBD4D4') |
---|
25 | self.panel = panel |
---|
26 | self.col_names = [] |
---|
27 | self._cols = 0 |
---|
28 | self._rows = 0 |
---|
29 | self.SetNumberRows(self._cols) |
---|
30 | self.SetNumberCols(self._rows) |
---|
31 | self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click) |
---|
32 | self.axis_value = [] |
---|
33 | self.axis_label = "" |
---|
34 | |
---|
35 | |
---|
36 | def on_right_click(self, event): |
---|
37 | print "on double click", event.GetRow(), event.GetCol() |
---|
38 | col = event.GetCol() |
---|
39 | if col != -1 and len(self.col_names) > col: |
---|
40 | label = self.col_names[int(col)] |
---|
41 | self.axis_label = label |
---|
42 | if label in self.data.keys(): |
---|
43 | col_val = self.data[label] |
---|
44 | self.axis_value = col_val |
---|
45 | # Slicer plot popup menu |
---|
46 | slicerpop = wx.Menu() |
---|
47 | id = wx.NewId() |
---|
48 | slicerpop.Append(id, '&Set X axis', 'Set X axis') |
---|
49 | wx.EVT_MENU(self, id, self.on_set_x_axis) |
---|
50 | |
---|
51 | |
---|
52 | id = wx.NewId() |
---|
53 | slicerpop.Append(id, '&Set Y axis', 'Set Y axis') |
---|
54 | wx.EVT_MENU(self, id, self.on_set_y_axis) |
---|
55 | pos = event.GetPosition() |
---|
56 | pos = self.ScreenToClient(pos) |
---|
57 | self.PopupMenu(slicerpop, pos) |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | def on_set_x_axis(self, event): |
---|
62 | self.panel.set_xaxis(x=self.axis_value, label=self.axis_label) |
---|
63 | |
---|
64 | def on_set_y_axis(self, event): |
---|
65 | self.panel.set_yaxis(y=self.axis_value, label=self.axis_label) |
---|
66 | |
---|
67 | def set_data(self, data): |
---|
68 | """ |
---|
69 | """ |
---|
70 | if data is None: |
---|
71 | data = {} |
---|
72 | if len(data) > 0: |
---|
73 | self.data = data |
---|
74 | self.col_names = data.keys() |
---|
75 | self.col_names.sort() |
---|
76 | self._cols = len(self.data.keys()) |
---|
77 | self._rows = max([len(v) for v in self.data.values()]) |
---|
78 | self.SetNumberRows(int(self._rows)) |
---|
79 | self.SetNumberCols(int(self._cols)) |
---|
80 | for index in range(len(self.col_names)): |
---|
81 | self.SetColLabelValue(index, str(self.col_names[index])) |
---|
82 | |
---|
83 | col = 0 |
---|
84 | for value_list in self.data.values(): |
---|
85 | for row in range(len(value_list)): |
---|
86 | self.SetCellValue(row, col, str(value_list[row])) |
---|
87 | col += 1 |
---|
88 | self.AutoSize() |
---|
89 | |
---|
90 | class Notebook(nb, PanelBase): |
---|
91 | """ |
---|
92 | ## Internal name for the AUI manager |
---|
93 | window_name = "Fit panel" |
---|
94 | ## Title to appear on top of the window |
---|
95 | """ |
---|
96 | window_caption = "Notebook " |
---|
97 | |
---|
98 | def __init__(self, parent, manager=None, data=None, *args, **kwargs): |
---|
99 | """ |
---|
100 | """ |
---|
101 | nb.__init__(self, parent, -1, |
---|
102 | style= wx.NB_BOTTOM) |
---|
103 | PanelBase.__init__(self, parent) |
---|
104 | |
---|
105 | self.parent = parent |
---|
106 | self.manager = manager |
---|
107 | self.data = data |
---|
108 | self.grid = GridPage(self, panel=self.parent) |
---|
109 | self.AddPage(self.grid, "Batch") |
---|
110 | self.grid.SetFocus() |
---|
111 | |
---|
112 | def set_data(self, data): |
---|
113 | if data is None: |
---|
114 | data = {} |
---|
115 | pos = self.GetSelection() |
---|
116 | if pos != -1: |
---|
117 | selected_page = self.GetPage(pos) |
---|
118 | selected_page.set_data(data) |
---|
119 | return |
---|
120 | if len(data) > 0: |
---|
121 | self.data = data |
---|
122 | self.col_names = data.keys() |
---|
123 | self.col_names.sort() |
---|
124 | self._cols = len(self.data.keys()) |
---|
125 | self._rows = max([len(v) for v in self.data.values()]) |
---|
126 | selected_page.SetNumberRows(int(self._rows)) |
---|
127 | selected_page.SetNumberCols(int(self._cols)) |
---|
128 | for index in range(len(self.col_names)): |
---|
129 | self.SetColLabelValue(index, str(self.col_names[index])) |
---|
130 | return |
---|
131 | |
---|
132 | if len(data) > 0: |
---|
133 | self.data = data |
---|
134 | self.col_names = data.keys() |
---|
135 | self.col_names.sort() |
---|
136 | self._cols = len(self.data.keys()) |
---|
137 | self._rows = max([len(v) for v in self.data.values()]) |
---|
138 | for index in xrange(self._rows): |
---|
139 | self.row_names.append( str(index)) |
---|
140 | self.AppendRows(index) |
---|
141 | self.SetColLabelValue(index) |
---|
142 | |
---|
143 | #AppendCols(self, numCols, updateLabels) |
---|
144 | #SetColLabelValue(int col, const wxString& value) |
---|
145 | #AppendRows(self, numRows, updateLabels) |
---|
146 | |
---|
147 | |
---|
148 | class TableBase(Grid.PyGridTableBase): |
---|
149 | """ |
---|
150 | grid receiving dictionary data structure as input |
---|
151 | """ |
---|
152 | def __init__(self, data=None): |
---|
153 | """data is a dictionary of key=column_name , value list of row value. |
---|
154 | |
---|
155 | :Note: the value stored in the list will be append to the grid in the same order . |
---|
156 | :example:data = {col_1:[value1, value2], col_2:[value11, value22]} |
---|
157 | +--------++--------++--------+ |
---|
158 | | Index | col_1 | col2 | |
---|
159 | +--------++--------++--------+ |
---|
160 | | Index1 | value1 | value2 | |
---|
161 | +--------++--------++--------+ |
---|
162 | | Index2 | value11 | value22| |
---|
163 | +--------++--------++--------+ |
---|
164 | """ |
---|
165 | # The base class must be initialized *first* |
---|
166 | Grid.PyGridTableBase.__init__(self) |
---|
167 | self.data = {} |
---|
168 | self._rows = 0 |
---|
169 | self._cols = 0 |
---|
170 | self.col_names = [] |
---|
171 | self.row_names = [] |
---|
172 | |
---|
173 | if data is None: |
---|
174 | data = {} |
---|
175 | self.set_data(data) |
---|
176 | |
---|
177 | def set_data(self, data): |
---|
178 | if len(data) > 0: |
---|
179 | self.data = data |
---|
180 | self.col_names = data.keys() |
---|
181 | self.col_names.sort() |
---|
182 | self._cols = len(self.data.keys()) |
---|
183 | self._rows = max([len(v) for v in self.data.values()]) |
---|
184 | for index in xrange(self._rows): |
---|
185 | self.row_names.append( str(index)) |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | def GetNumberCols(self): |
---|
190 | return self._cols |
---|
191 | |
---|
192 | def GetNumberRows(self): |
---|
193 | return self._rows |
---|
194 | |
---|
195 | def GetColLabelValue(self, col): |
---|
196 | if len(self.col_names) > col: |
---|
197 | return self.col_names[col] |
---|
198 | |
---|
199 | def GetRowLabelValue(self, row): |
---|
200 | if len(self.row_names) > row: |
---|
201 | return self.row_names[row] |
---|
202 | |
---|
203 | def GetValue(self, row, col): |
---|
204 | pos = int(row) |
---|
205 | if len(self.data[self.col_names[col]]) > pos: |
---|
206 | return str(self.data[self.col_names[col]][pos]) |
---|
207 | |
---|
208 | def GetRawValue(self, row, col): |
---|
209 | pos = int(row) |
---|
210 | if len(self.data[self.col_names[col]]) > pos: |
---|
211 | return self.data[self.col_names[col]][row] |
---|
212 | |
---|
213 | def SetValue(self, row, col, value): |
---|
214 | if len(self.data) == 0: |
---|
215 | return |
---|
216 | pos = int(row) |
---|
217 | if len(self.data[self.col_names[col]]) > pos: |
---|
218 | self.data[self.col_names[col]][row] = value |
---|
219 | |
---|
220 | def getRaw(self, row): |
---|
221 | pos = int(row) |
---|
222 | row_value = [] |
---|
223 | for col in xrange(self.data.values()): |
---|
224 | if len(col) < pos: |
---|
225 | row_value.append(None) |
---|
226 | else: |
---|
227 | row_value.append(col[pos]) |
---|
228 | return row_value |
---|
229 | |
---|
230 | |
---|
231 | class Table(Grid.Grid): |
---|
232 | def __init__(self, parent, data=None): |
---|
233 | """ |
---|
234 | """ |
---|
235 | Grid.Grid.__init__(self, parent, -1) |
---|
236 | self._table = TableBase(data=data) |
---|
237 | self.SetTable(self._table) |
---|
238 | self.Bind(Grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_context_menu) |
---|
239 | |
---|
240 | def on_context_menu(self, event): |
---|
241 | row, col = event.GetRow(), event.GetCol() |
---|
242 | print "right click", row, col |
---|
243 | |
---|
244 | def set_data(self, data): |
---|
245 | self._table.set_data(data) |
---|
246 | |
---|
247 | class SPanel(ScrolledPanel): |
---|
248 | def __init__(self, parent, *args, **kwds): |
---|
249 | ScrolledPanel.__init__(self, parent , *args, **kwds) |
---|
250 | self.SetupScrolling() |
---|
251 | |
---|
252 | class GridPanel(SPanel): |
---|
253 | def __init__(self, parent,data=None, *args, **kwds): |
---|
254 | SPanel.__init__(self, parent , *args, **kwds) |
---|
255 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
256 | |
---|
257 | self.plotting_sizer = wx.FlexGridSizer(3, 5, 10, 5) |
---|
258 | w, h = self.GetSize() |
---|
259 | #self.panel_grid = SPanel(self, -1, size=(w, -1)) |
---|
260 | self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
261 | self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0), |
---|
262 | (wx.StaticLine(self, -1), 0, wx.EXPAND, 0), |
---|
263 | (self.plotting_sizer)]) |
---|
264 | self.parent = parent |
---|
265 | self._data = data |
---|
266 | self.x = [] |
---|
267 | self.y = [] |
---|
268 | |
---|
269 | self.x_axis_label = None |
---|
270 | self.y_axis_label = None |
---|
271 | self.x_axis_value = None |
---|
272 | self.y_axis_value = None |
---|
273 | self.x_axis_unit = None |
---|
274 | self.y_axis_unit = None |
---|
275 | self.plot_button = None |
---|
276 | self.grid = None |
---|
277 | self.layout_grid() |
---|
278 | self.layout_plotting_area() |
---|
279 | self.SetSizer(self.vbox) |
---|
280 | |
---|
281 | def set_data(self, data): |
---|
282 | """ |
---|
283 | """ |
---|
284 | if self.grid is not None: |
---|
285 | self.grid.set_data(data) |
---|
286 | |
---|
287 | def set_xaxis(self, label="", x=None) : |
---|
288 | if x is None: |
---|
289 | x = [] |
---|
290 | self.x = x |
---|
291 | self.x_axis_value.SetValue("%s[:]" % str(label)) |
---|
292 | self.x_axis_label.SetValue(str(label)) |
---|
293 | |
---|
294 | def set_yaxis(self, label="", y=None) : |
---|
295 | if y is None: |
---|
296 | y = [] |
---|
297 | self.y = y |
---|
298 | self.y_axis_value.SetValue("%s[:]" % str(label)) |
---|
299 | self.y_axis_label.SetValue(str(label)) |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | def on_plot(self, event): |
---|
304 | """ |
---|
305 | plotting |
---|
306 | """ |
---|
307 | new_plot = Data1D(x=self.x, y=self.y) |
---|
308 | new_plot.id = wx.NewId() |
---|
309 | new_plot.group_id = wx.NewId() |
---|
310 | title = "%s vs %s" % (self.y_axis_label.GetValue(), self.x_axis_label.GetValue()) |
---|
311 | new_plot.xaxis(self.x_axis_label.GetValue(), self.x_axis_unit.GetValue()) |
---|
312 | new_plot.yaxis(self.y_axis_label.GetValue(), self.y_axis_unit.GetValue()) |
---|
313 | wx.PostEvent(self.parent.parent, |
---|
314 | NewPlotEvent(plot=new_plot, group_id=str(new_plot.group_id), title ="batch")) |
---|
315 | def layout_grid(self): |
---|
316 | """ |
---|
317 | Draw the area related to the grid |
---|
318 | """ |
---|
319 | self.grid = Notebook(parent=self) |
---|
320 | self.grid.set_data(self._data) |
---|
321 | #self.grid = Table(parent=self, data=self._data) |
---|
322 | #vbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
323 | self.grid_sizer.Add(self.grid, 1, wx.EXPAND, 0) |
---|
324 | #self.panel_grid.SetSizer(vbox) |
---|
325 | |
---|
326 | def layout_grid1(self): |
---|
327 | """ |
---|
328 | Draw the area related to the grid |
---|
329 | """ |
---|
330 | self.grid = Table(parent=self.panel_grid, data=self._data) |
---|
331 | vbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
332 | vbox.Add(self.grid, 1, wx.EXPAND, 0) |
---|
333 | self.panel_grid.SetSizer(vbox) |
---|
334 | |
---|
335 | def layout_plotting_area(self): |
---|
336 | """ |
---|
337 | Draw area containing options to plot |
---|
338 | """ |
---|
339 | self.x_axis_label = wx.TextCtrl(self, -1) |
---|
340 | |
---|
341 | self.y_axis_label = wx.TextCtrl(self, -1) |
---|
342 | |
---|
343 | self.x_axis_value = wx.TextCtrl(self, -1) |
---|
344 | self.y_axis_value = wx.TextCtrl(self, -1) |
---|
345 | |
---|
346 | self.x_axis_unit = wx.TextCtrl(self, -1) |
---|
347 | self.y_axis_unit = wx.TextCtrl(self, -1) |
---|
348 | self.plot_button = wx.Button(self, -1, "Plot") |
---|
349 | wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot) |
---|
350 | self.plotting_sizer.AddMany([(wx.StaticText(self, -1, "x"), 1, wx.LEFT, 10), |
---|
351 | (self.x_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
352 | (self.x_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
353 | (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE|wx.EXPAND, 0), |
---|
354 | (self.x_axis_unit), |
---|
355 | (wx.StaticText(self, -1, "y"), 1, wx.LEFT, 10), |
---|
356 | (self.y_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
357 | (self.y_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
358 | (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE|wx.EXPAND, 0), |
---|
359 | (self.y_axis_unit), |
---|
360 | (-1, -1), |
---|
361 | (-1, -1), |
---|
362 | (-1, -1), |
---|
363 | (self.plot_button, 1, wx.LEFT, 30)]) |
---|
364 | |
---|
365 | |
---|
366 | def add_column(self): |
---|
367 | if self.grid is not None: |
---|
368 | self.grid.add_column() |
---|
369 | |
---|
370 | |
---|
371 | class GridFrame(wx.Frame): |
---|
372 | def __init__(self, parent=None, data=None, id=-1, title="Batch Results", size=(500, 400)): |
---|
373 | wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
374 | self.parent = parent |
---|
375 | self.panel = GridPanel(self, data) |
---|
376 | menubar = wx.MenuBar() |
---|
377 | edit = wx.Menu() |
---|
378 | id_col = wx.NewId() |
---|
379 | edit.Append(id_col, 'Edit', '' ) |
---|
380 | menubar.Append(edit, "&New column") |
---|
381 | self.SetMenuBar(menubar) |
---|
382 | wx.EVT_MENU(self, id_col, self.on_add_column) |
---|
383 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
384 | |
---|
385 | |
---|
386 | def on_close(self, event): |
---|
387 | """ |
---|
388 | """ |
---|
389 | self.Hide() |
---|
390 | |
---|
391 | def on_add_column(self, event): |
---|
392 | """ |
---|
393 | """ |
---|
394 | self.panel.add_column() |
---|
395 | |
---|
396 | def set_data(self, data): |
---|
397 | """ |
---|
398 | """ |
---|
399 | self.panel.set_data(data) |
---|
400 | |
---|
401 | |
---|
402 | if __name__ == "__main__": |
---|
403 | app = wx.App() |
---|
404 | |
---|
405 | try: |
---|
406 | data = {} |
---|
407 | j = 0 |
---|
408 | for i in range(4): |
---|
409 | j += 1 |
---|
410 | data["index"+str(i)] = [i/j, i*j, i, i+j] |
---|
411 | |
---|
412 | frame = TestFrame(data=data) |
---|
413 | frame.Show(True) |
---|
414 | except: |
---|
415 | print sys.exc_value |
---|
416 | |
---|
417 | app.MainLoop() |
---|