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