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 | """ |
---|
21 | """ |
---|
22 | def __init__(self, parent, panel=None): |
---|
23 | """ |
---|
24 | """ |
---|
25 | sheet.CSheet.__init__(self, parent) |
---|
26 | self.SetLabelBackgroundColour('#DBD4D4') |
---|
27 | self.AutoSize() |
---|
28 | self.panel = panel |
---|
29 | self.col_names = [] |
---|
30 | self._cols = 50 |
---|
31 | self._rows = 51 |
---|
32 | col_with = 30 |
---|
33 | row_height = 20 |
---|
34 | self.axis_value = [] |
---|
35 | self.axis_label = "" |
---|
36 | self.selected_cells = [] |
---|
37 | self.selected_cols = [] |
---|
38 | self.SetColMinimalAcceptableWidth(col_with) |
---|
39 | self.SetRowMinimalAcceptableHeight(row_height) |
---|
40 | self.SetNumberRows(self._cols) |
---|
41 | self.SetNumberCols(self._rows) |
---|
42 | self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_left_click) |
---|
43 | self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click) |
---|
44 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.on_selected_cell) |
---|
45 | |
---|
46 | |
---|
47 | def on_selected_cell(self, event): |
---|
48 | """ |
---|
49 | Handler catching cell selection |
---|
50 | """ |
---|
51 | flag = event.CmdDown() or event.ControlDown() |
---|
52 | cell = (event.GetRow(), event.GetCol()) |
---|
53 | if not flag: |
---|
54 | self.selected_cells = [] |
---|
55 | if cell in self.selected_cells: |
---|
56 | self.selected_cells.remove(cell) |
---|
57 | else: |
---|
58 | self.selected_cells.append(cell) |
---|
59 | event.Skip() |
---|
60 | |
---|
61 | def on_left_click(self, event): |
---|
62 | """ |
---|
63 | Catch the left click on label mouse event |
---|
64 | """ |
---|
65 | flag = event.CmdDown() or event.ControlDown() |
---|
66 | col = event.GetCol() |
---|
67 | if not flag: |
---|
68 | self.selected_cols = [] |
---|
69 | if col not in self.selected_cols: |
---|
70 | self.selected_cols.append(col) |
---|
71 | event.Skip() |
---|
72 | |
---|
73 | def on_right_click(self, event): |
---|
74 | """ |
---|
75 | Catch the right click mouse |
---|
76 | """ |
---|
77 | col = event.GetCol() |
---|
78 | if col != -1 and len(self.col_names) > col: |
---|
79 | label = self.col_names[int(col)] |
---|
80 | self.axis_label = label |
---|
81 | if label in self.data.keys(): |
---|
82 | col_val = self.data[label] |
---|
83 | self.axis_value = col_val |
---|
84 | # Slicer plot popup menu |
---|
85 | slicerpop = wx.Menu() |
---|
86 | id = wx.NewId() |
---|
87 | slicerpop.Append(id, '&Set X axis', 'Set X axis') |
---|
88 | wx.EVT_MENU(self, id, self.on_set_x_axis) |
---|
89 | |
---|
90 | id = wx.NewId() |
---|
91 | slicerpop.Append(id, '&Set Y axis', 'Set Y axis') |
---|
92 | wx.EVT_MENU(self, id, self.on_set_y_axis) |
---|
93 | pos = event.GetPosition() |
---|
94 | pos = self.ScreenToClient(pos) |
---|
95 | self.PopupMenu(slicerpop, pos) |
---|
96 | |
---|
97 | |
---|
98 | def on_set_x_axis(self, event): |
---|
99 | """ |
---|
100 | """ |
---|
101 | self.panel.set_xaxis(x=self.axis_value, label=self.axis_label) |
---|
102 | |
---|
103 | def on_set_y_axis(self, event): |
---|
104 | """ |
---|
105 | """ |
---|
106 | self.panel.set_yaxis(y=self.axis_value, label=self.axis_label) |
---|
107 | |
---|
108 | def set_data(self, data): |
---|
109 | """ |
---|
110 | Add data to the grid |
---|
111 | """ |
---|
112 | if data is None: |
---|
113 | data = {} |
---|
114 | if len(data) > 0: |
---|
115 | |
---|
116 | self._cols = self.GetNumberCols() |
---|
117 | self._rows = self.GetNumberRows() |
---|
118 | self.data = data |
---|
119 | self.col_names = data.keys() |
---|
120 | self.col_names.sort() |
---|
121 | nbr_user_cols = len(self.col_names) |
---|
122 | #Add more columns to the grid if necessary |
---|
123 | if nbr_user_cols > self._cols: |
---|
124 | new_col_nbr = nbr_user_cols - self._cols |
---|
125 | self.AppendCols(new_col_nbr, True) |
---|
126 | #Add more rows to the grid if necessary |
---|
127 | nbr_user_row = len(self.data.values()) |
---|
128 | if nbr_user_row > self._rows + 1: |
---|
129 | new_row_nbr = nbr_user_row - self._rows |
---|
130 | self.AppendRows(new_row_nbr, True) |
---|
131 | # add data to the grid |
---|
132 | label_row = 0 |
---|
133 | for index in range(nbr_user_cols): |
---|
134 | # use the first row of the grid to add user defined labels |
---|
135 | self.SetCellValue(label_row, index, str(self.col_names[index])) |
---|
136 | col = 0 |
---|
137 | for value_list in self.data.values(): |
---|
138 | for row in range(1, len(value_list)): |
---|
139 | self.SetCellValue(row, col, str(value_list[row])) |
---|
140 | col += 1 |
---|
141 | self.AutoSize() |
---|
142 | |
---|
143 | |
---|
144 | class Notebook(nb, PanelBase): |
---|
145 | """ |
---|
146 | ## Internal name for the AUI manager |
---|
147 | window_name = "Fit panel" |
---|
148 | ## Title to appear on top of the window |
---|
149 | """ |
---|
150 | window_caption = "Notebook " |
---|
151 | |
---|
152 | def __init__(self, parent, manager=None, data=None, *args, **kwargs): |
---|
153 | """ |
---|
154 | """ |
---|
155 | nb.__init__(self, parent, -1, |
---|
156 | style= wx.aui.AUI_BUTTON_DOWN|wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
157 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
158 | wx.CLIP_CHILDREN) |
---|
159 | PanelBase.__init__(self, parent) |
---|
160 | self.enable_close_button() |
---|
161 | self.parent = parent |
---|
162 | self.manager = manager |
---|
163 | self.data = data |
---|
164 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page) |
---|
165 | |
---|
166 | |
---|
167 | def enable_close_button(self): |
---|
168 | """ |
---|
169 | display the close button on tab for more than 1 tabs else remove the |
---|
170 | close button |
---|
171 | """ |
---|
172 | if self.GetPageCount() <= 1: |
---|
173 | style = self.GetWindowStyleFlag() |
---|
174 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
175 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == flag: |
---|
176 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
177 | self.SetWindowStyle(style) |
---|
178 | else: |
---|
179 | style = self.GetWindowStyleFlag() |
---|
180 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
181 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != flag: |
---|
182 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
183 | self.SetWindowStyle(style) |
---|
184 | |
---|
185 | def on_edit_axis(self): |
---|
186 | """ |
---|
187 | Return the select cell of a given selected column |
---|
188 | """ |
---|
189 | pos = self.GetSelection() |
---|
190 | grid = self.GetPage(pos) |
---|
191 | if len(grid.selected_cols) > 1: |
---|
192 | msg = "Edit axis doesn't understand this selection.\n" |
---|
193 | msg += "Please select only one column" |
---|
194 | raise ValueError, msg |
---|
195 | list_of_cells = [] |
---|
196 | if len(grid.selected_cols) == 1: |
---|
197 | col = grid.selected_cols[0] |
---|
198 | for row in range(grid.GetNumberRows()): |
---|
199 | list_of_cells.append((row, col)) |
---|
200 | |
---|
201 | print "selected cell", grid.selected_cells.sort(), grid.selected_cells |
---|
202 | |
---|
203 | |
---|
204 | def on_close_page(self, event): |
---|
205 | """ |
---|
206 | close the page |
---|
207 | """ |
---|
208 | if self.GetPageCount() == 1: |
---|
209 | event.Veto() |
---|
210 | self.enable_close_button() |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | def set_data(self, data): |
---|
215 | if data is None: |
---|
216 | return |
---|
217 | |
---|
218 | grid = GridPage(self, panel=self.parent) |
---|
219 | grid.set_data(data) |
---|
220 | self.AddPage(grid, "") |
---|
221 | pos = self.GetPageIndex(grid) |
---|
222 | title = "Batch " + str(self.GetPageCount()) |
---|
223 | self.SetPageText(pos, title) |
---|
224 | |
---|
225 | def add_column(self): |
---|
226 | """ |
---|
227 | Append a new column to the grid |
---|
228 | """ |
---|
229 | pos = self.GetSelection() |
---|
230 | grid = self.GetPage(pos) |
---|
231 | grid.AppendCols(1, True) |
---|
232 | |
---|
233 | def on_remove_column(self): |
---|
234 | """ |
---|
235 | Remove column to the current grid |
---|
236 | """ |
---|
237 | pos = self.GetSelection() |
---|
238 | grid = self.GetPage(pos) |
---|
239 | cols_pos = grid.GetSelectedCols() |
---|
240 | for cpos in cols_pos: |
---|
241 | grid.DeleteCols(cpos) |
---|
242 | |
---|
243 | |
---|
244 | class SPanel(ScrolledPanel): |
---|
245 | def __init__(self, parent, *args, **kwds): |
---|
246 | ScrolledPanel.__init__(self, parent , *args, **kwds) |
---|
247 | self.SetupScrolling() |
---|
248 | |
---|
249 | |
---|
250 | class GridPanel(SPanel): |
---|
251 | def __init__(self, parent,data=None, *args, **kwds): |
---|
252 | SPanel.__init__(self, parent , *args, **kwds) |
---|
253 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
254 | |
---|
255 | self.plotting_sizer = wx.FlexGridSizer(3, 7, 10, 5) |
---|
256 | w, h = self.GetSize() |
---|
257 | #self.panel_grid = SPanel(self, -1, size=(w, -1)) |
---|
258 | self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
259 | self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0), |
---|
260 | (wx.StaticLine(self, -1), 0, wx.EXPAND, 0), |
---|
261 | (self.plotting_sizer)]) |
---|
262 | self.parent = parent |
---|
263 | self._data = data |
---|
264 | self.x = [] |
---|
265 | self.y = [] |
---|
266 | |
---|
267 | self.x_axis_label = None |
---|
268 | self.y_axis_label = None |
---|
269 | self.x_axis_value = None |
---|
270 | self.y_axis_value = None |
---|
271 | self.x_axis_unit = None |
---|
272 | self.y_axis_unit = None |
---|
273 | self.plot_button = None |
---|
274 | self.grid = None |
---|
275 | self.layout_grid() |
---|
276 | self.layout_plotting_area() |
---|
277 | self.SetSizer(self.vbox) |
---|
278 | |
---|
279 | def set_data(self, data): |
---|
280 | """ |
---|
281 | """ |
---|
282 | if self.grid is not None: |
---|
283 | self.grid.set_data(data) |
---|
284 | |
---|
285 | def set_xaxis(self, label="", x=None) : |
---|
286 | if x is None: |
---|
287 | x = [] |
---|
288 | self.x = x |
---|
289 | self.x_axis_value.SetValue("%s[:]" % str(label)) |
---|
290 | self.x_axis_label.SetValue(str(label)) |
---|
291 | |
---|
292 | def set_yaxis(self, label="", y=None) : |
---|
293 | if y is None: |
---|
294 | y = [] |
---|
295 | self.y = y |
---|
296 | self.y_axis_value.SetValue("%s[:]" % str(label)) |
---|
297 | self.y_axis_label.SetValue(str(label)) |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | def on_plot(self, event): |
---|
302 | """ |
---|
303 | plotting |
---|
304 | """ |
---|
305 | new_plot = Data1D(x=self.x, y=self.y) |
---|
306 | new_plot.id = wx.NewId() |
---|
307 | new_plot.group_id = wx.NewId() |
---|
308 | title = "%s vs %s" % (self.y_axis_label.GetValue(), self.x_axis_label.GetValue()) |
---|
309 | new_plot.xaxis(self.x_axis_label.GetValue(), self.x_axis_unit.GetValue()) |
---|
310 | new_plot.yaxis(self.y_axis_label.GetValue(), self.y_axis_unit.GetValue()) |
---|
311 | wx.PostEvent(self.parent.parent, |
---|
312 | NewPlotEvent(plot=new_plot, |
---|
313 | group_id=str(new_plot.group_id), title ="batch")) |
---|
314 | def layout_grid(self): |
---|
315 | """ |
---|
316 | Draw the area related to the grid |
---|
317 | """ |
---|
318 | self.grid = Notebook(parent=self) |
---|
319 | self.grid.set_data(self._data) |
---|
320 | self.grid_sizer.Add(self.grid, 1, wx.EXPAND, 0) |
---|
321 | |
---|
322 | |
---|
323 | def layout_grid1(self): |
---|
324 | """ |
---|
325 | Draw the area related to the grid |
---|
326 | """ |
---|
327 | self.grid = Table(parent=self.panel_grid, data=self._data) |
---|
328 | vbox = wx.BoxSizer(wx.HORIZONTAL) |
---|
329 | vbox.Add(self.grid, 1, wx.EXPAND, 0) |
---|
330 | self.panel_grid.SetSizer(vbox) |
---|
331 | |
---|
332 | def layout_plotting_area(self): |
---|
333 | """ |
---|
334 | Draw area containing options to plot |
---|
335 | """ |
---|
336 | self.x_axis_label = wx.TextCtrl(self, -1) |
---|
337 | self.y_axis_label = wx.TextCtrl(self, -1) |
---|
338 | self.x_axis_value = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
339 | self.y_axis_value = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
340 | self.x_axis_add = wx.Button(self, -1, "Add") |
---|
341 | self.x_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
342 | id=self.x_axis_add.GetId()) |
---|
343 | self.y_axis_add = wx.Button(self, -1, "Add") |
---|
344 | self.y_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
345 | id=self.y_axis_add.GetId()) |
---|
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([ |
---|
351 | (wx.StaticText(self, -1, "x-axis label"), 1, wx.LEFT, 10), |
---|
352 | (self.x_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
353 | (wx.StaticText(self, -1, "x-axis value"), 1, wx.LEFT, 10), |
---|
354 | (self.x_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
355 | (self.x_axis_add, 1, wx.LEFT|wx.RIGHT, 0), |
---|
356 | (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT, 0), |
---|
357 | (self.x_axis_unit, 0, wx.LEFT, 0), |
---|
358 | (wx.StaticText(self, -1, "y-axis label"), 1, wx.LEFT, 10), |
---|
359 | (self.y_axis_label, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
360 | (wx.StaticText(self, -1, "y-axis value"), 1, wx.LEFT, 10), |
---|
361 | (self.y_axis_value, wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
362 | (self.y_axis_add, 1, wx.LEFT|wx.RIGHT, 0), |
---|
363 | (wx.StaticText(self, -1 , "unit"), 1, wx.LEFT|wx.RIGHT, 0), |
---|
364 | (self.y_axis_unit, 0, wx.LEFT, 0), |
---|
365 | (-1, -1), |
---|
366 | (-1, -1), |
---|
367 | (-1, -1), |
---|
368 | (-1, -1), |
---|
369 | (-1, -1), |
---|
370 | (-1, -1), |
---|
371 | (self.plot_button, 1, wx.LEFT, 0)]) |
---|
372 | |
---|
373 | def on_edit_axis(self, event): |
---|
374 | """ |
---|
375 | Get the selected column on the visible grid and set values for axis |
---|
376 | """ |
---|
377 | self.grid.on_edit_axis() |
---|
378 | |
---|
379 | def edit_axis_helper(self, tcrtl_label, tcrtl_value): |
---|
380 | """ |
---|
381 | """ |
---|
382 | def add_column(self): |
---|
383 | """ |
---|
384 | """ |
---|
385 | if self.grid is not None: |
---|
386 | self.grid.add_column() |
---|
387 | |
---|
388 | def on_remove_column(self): |
---|
389 | """ |
---|
390 | """ |
---|
391 | if self.grid is not None: |
---|
392 | self.grid.on_remove_column() |
---|
393 | |
---|
394 | |
---|
395 | class GridFrame(wx.Frame): |
---|
396 | def __init__(self, parent=None, data=None, id=-1, title="Batch Results", size=(700, 400)): |
---|
397 | wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
398 | self.parent = parent |
---|
399 | self.panel = GridPanel(self, data) |
---|
400 | menubar = wx.MenuBar() |
---|
401 | self.SetMenuBar(menubar) |
---|
402 | edit = wx.Menu() |
---|
403 | menubar.Append(edit, "&Edit") |
---|
404 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
405 | |
---|
406 | add_col_menu = edit.Append(wx.NewId(), 'Add Column', 'Add column') |
---|
407 | wx.EVT_MENU(self, add_col_menu.GetId(), self.on_add_column) |
---|
408 | remove_col_menu = edit.Append(wx.NewId(), 'Remove Column', |
---|
409 | 'Remove Column') |
---|
410 | wx.EVT_MENU(self, remove_col_menu.GetId(), self.on_remove_column) |
---|
411 | |
---|
412 | def on_close(self, event): |
---|
413 | """ |
---|
414 | """ |
---|
415 | self.Hide() |
---|
416 | |
---|
417 | def on_remove_column(self, event): |
---|
418 | """ |
---|
419 | Remove the selected column to the grid |
---|
420 | """ |
---|
421 | self.panel.on_remove_column() |
---|
422 | |
---|
423 | def on_add_column(self, event): |
---|
424 | """ |
---|
425 | Append a new column to the grid |
---|
426 | """ |
---|
427 | self.panel.add_column() |
---|
428 | |
---|
429 | def set_data(self, data): |
---|
430 | """ |
---|
431 | """ |
---|
432 | self.panel.set_data(data) |
---|
433 | |
---|
434 | |
---|
435 | if __name__ == "__main__": |
---|
436 | app = wx.App() |
---|
437 | |
---|
438 | try: |
---|
439 | data = {} |
---|
440 | j = 0 |
---|
441 | for i in range(4): |
---|
442 | j += 1 |
---|
443 | data["index"+str(i)] = [i/j, i*j, i, i+j] |
---|
444 | |
---|
445 | frame = GridFrame(data=data) |
---|
446 | frame.Show(True) |
---|
447 | except: |
---|
448 | print sys.exc_value |
---|
449 | |
---|
450 | app.MainLoop() |
---|