1 | """ |
---|
2 | Implement grid used to store data |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import numpy |
---|
6 | import math |
---|
7 | import re |
---|
8 | import os |
---|
9 | import sys |
---|
10 | import copy |
---|
11 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
12 | import wx.grid as Grid |
---|
13 | import wx.aui |
---|
14 | from wx.aui import AuiNotebook as nb |
---|
15 | from sans.guiframe.panel_base import PanelBase |
---|
16 | import wx.lib.sheet as sheet |
---|
17 | |
---|
18 | from sans.guiframe.events import NewPlotEvent |
---|
19 | from sans.guiframe.events import StatusEvent |
---|
20 | from sans.guiframe.dataFitting import Data1D |
---|
21 | |
---|
22 | FUNC_DICT = {"sqrt": "math.sqrt", |
---|
23 | "pow": "math.sqrt"} |
---|
24 | |
---|
25 | |
---|
26 | def parse_string(sentence, list): |
---|
27 | """ |
---|
28 | Return a dictionary of column label and index or row selected |
---|
29 | :param sentence: String to parse |
---|
30 | :param list: list of columns label |
---|
31 | """ |
---|
32 | toks = [] |
---|
33 | p2 = re.compile(r'\d+') |
---|
34 | p = re.compile(r'[\+\-\*\%\/]') |
---|
35 | labels = p.split(sentence) |
---|
36 | col_dict = {} |
---|
37 | for elt in labels: |
---|
38 | rang = None |
---|
39 | temp_arr = [] |
---|
40 | for label in list: |
---|
41 | label_pos = elt.find(label) |
---|
42 | if label_pos != -1: |
---|
43 | if elt.count(',') > 0: |
---|
44 | new_temp = [] |
---|
45 | temp = elt.split(label) |
---|
46 | for item in temp: |
---|
47 | range_pos = item.find(":") |
---|
48 | if range_pos != -1: |
---|
49 | rang = p2.findall(item) |
---|
50 | for i in xrange(int(rang[0]), int(rang[1])+1 ): |
---|
51 | new_temp.append(i) |
---|
52 | temp_arr += new_temp |
---|
53 | else: |
---|
54 | temp = elt.split(label) |
---|
55 | for item in temp: |
---|
56 | range_pos = item.find(":") |
---|
57 | if range_pos != -1: |
---|
58 | rang = p2.findall(item) |
---|
59 | for i in xrange(int(rang[0]), int(rang[1])+1 ): |
---|
60 | temp_arr.append(i) |
---|
61 | col_dict[elt] = (label, temp_arr) |
---|
62 | return col_dict |
---|
63 | |
---|
64 | class GridPage(sheet.CSheet): |
---|
65 | """ |
---|
66 | """ |
---|
67 | def __init__(self, parent, panel=None): |
---|
68 | """ |
---|
69 | """ |
---|
70 | sheet.CSheet.__init__(self, parent) |
---|
71 | self.SetLabelBackgroundColour('#DBD4D4') |
---|
72 | self.AutoSize() |
---|
73 | self.panel = panel |
---|
74 | self.col_names = [] |
---|
75 | self.data_inputs = {} |
---|
76 | self.data_outputs = {} |
---|
77 | self._cols = 50 |
---|
78 | self._rows = 51 |
---|
79 | col_with = 30 |
---|
80 | row_height = 20 |
---|
81 | self.axis_value = [] |
---|
82 | self.axis_label = "" |
---|
83 | self.selected_cells = [] |
---|
84 | self.selected_cols = [] |
---|
85 | self.SetColMinimalAcceptableWidth(col_with) |
---|
86 | self.SetRowMinimalAcceptableHeight(row_height) |
---|
87 | self.SetNumberRows(self._cols) |
---|
88 | self.SetNumberCols(self._rows) |
---|
89 | self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_left_click) |
---|
90 | self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click) |
---|
91 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.on_selected_cell) |
---|
92 | |
---|
93 | def on_selected_cell(self, event): |
---|
94 | """ |
---|
95 | Handler catching cell selection |
---|
96 | """ |
---|
97 | flag = event.CmdDown() or event.ControlDown() |
---|
98 | row, col = event.GetRow(), event.GetCol() |
---|
99 | cell = (row, col) |
---|
100 | if not flag: |
---|
101 | self.selected_cells = [] |
---|
102 | self.axis_value = [] |
---|
103 | self.axis_label = "" |
---|
104 | if cell in self.selected_cells: |
---|
105 | self.selected_cells.remove(cell) |
---|
106 | else: |
---|
107 | self.selected_cells.append(cell) |
---|
108 | if row > 1: |
---|
109 | if (cell) in self.selected_cells: |
---|
110 | self.axis_value.append(self.GetCellValue(row, col)) |
---|
111 | self.axis_label = self.GetCellValue(row, col) |
---|
112 | event.Skip() |
---|
113 | |
---|
114 | def on_left_click(self, event): |
---|
115 | """ |
---|
116 | Catch the left click on label mouse event |
---|
117 | """ |
---|
118 | flag = event.CmdDown() or event.ControlDown() |
---|
119 | col = event.GetCol() |
---|
120 | if not flag: |
---|
121 | self.selected_cols = [] |
---|
122 | self.axis_value = [] |
---|
123 | self.axis_label = "" |
---|
124 | if col not in self.selected_cols: |
---|
125 | self.selected_cols.append(col) |
---|
126 | if not flag : |
---|
127 | for row in range(2, self.GetNumberRows()+ 1): |
---|
128 | self.axis_value.append(self.GetCellValue(row, col)) |
---|
129 | row = 1 |
---|
130 | self.axis_label = self.GetCellValue(row, col) |
---|
131 | event.Skip() |
---|
132 | |
---|
133 | def on_right_click(self, event): |
---|
134 | """ |
---|
135 | Catch the right click mouse |
---|
136 | """ |
---|
137 | col = event.GetCol() |
---|
138 | if col != -1 and len(self.col_names) > col: |
---|
139 | label = self.col_names[int(col)] |
---|
140 | self.axis_label = label |
---|
141 | if label in self.data.keys(): |
---|
142 | col_val = self.data[label] |
---|
143 | self.axis_value = col_val |
---|
144 | # Slicer plot popup menu |
---|
145 | slicerpop = wx.Menu() |
---|
146 | id = wx.NewId() |
---|
147 | slicerpop.Append(id, '&Set X axis', 'Set X axis') |
---|
148 | wx.EVT_MENU(self, id, self.on_set_x_axis) |
---|
149 | |
---|
150 | id = wx.NewId() |
---|
151 | slicerpop.Append(id, '&Set Y axis', 'Set Y axis') |
---|
152 | wx.EVT_MENU(self, id, self.on_set_y_axis) |
---|
153 | pos = event.GetPosition() |
---|
154 | pos = self.ScreenToClient(pos) |
---|
155 | self.PopupMenu(slicerpop, pos) |
---|
156 | |
---|
157 | def on_set_x_axis(self, event): |
---|
158 | """ |
---|
159 | """ |
---|
160 | self.panel.set_xaxis(x=self.axis_value, label=self.axis_label) |
---|
161 | |
---|
162 | def on_set_y_axis(self, event): |
---|
163 | """ |
---|
164 | """ |
---|
165 | self.panel.set_yaxis(y=self.axis_value, label=self.axis_label) |
---|
166 | |
---|
167 | def set_data(self, data_inputs, data_outputs): |
---|
168 | """ |
---|
169 | Add data to the grid |
---|
170 | """ |
---|
171 | if data_outputs is None: |
---|
172 | data_outputs = {} |
---|
173 | self.data_outputs = data_outputs |
---|
174 | if self.data_inputs is None: |
---|
175 | data_inputs = {} |
---|
176 | self.data_inputs = data_inputs |
---|
177 | |
---|
178 | if len(self.data_outputs) > 0: |
---|
179 | self._cols = self.GetNumberCols() |
---|
180 | self._rows = self.GetNumberRows() |
---|
181 | self.col_names = self.data_outputs.keys() |
---|
182 | self.col_names.sort() |
---|
183 | nbr_user_cols = len(self.col_names) |
---|
184 | #Add more columns to the grid if necessary |
---|
185 | if nbr_user_cols > self._cols: |
---|
186 | new_col_nbr = nbr_user_cols - self._cols |
---|
187 | self.AppendCols(new_col_nbr, True) |
---|
188 | #Add more rows to the grid if necessary |
---|
189 | nbr_user_row = len(self.data_outputs.values()) |
---|
190 | if nbr_user_row > self._rows + 1: |
---|
191 | new_row_nbr = nbr_user_row - self._rows |
---|
192 | self.AppendRows(new_row_nbr, True) |
---|
193 | # add data to the grid |
---|
194 | row = 0 |
---|
195 | col = 0 |
---|
196 | cell_col = 0 |
---|
197 | for col_name in self.col_names: |
---|
198 | # use the first row of the grid to add user defined labels |
---|
199 | self.SetCellValue(row, col, str(col_name)) |
---|
200 | col += 1 |
---|
201 | cell_row = 1 |
---|
202 | value_list = self.data_outputs[col_name] |
---|
203 | |
---|
204 | for value in value_list: |
---|
205 | self.SetCellValue(cell_row, cell_col, str(value)) |
---|
206 | cell_row += 1 |
---|
207 | cell_col += 1 |
---|
208 | self.AutoSize() |
---|
209 | |
---|
210 | |
---|
211 | class Notebook(nb, PanelBase): |
---|
212 | """ |
---|
213 | ## Internal name for the AUI manager |
---|
214 | window_name = "Fit panel" |
---|
215 | ## Title to appear on top of the window |
---|
216 | """ |
---|
217 | window_caption = "Notebook " |
---|
218 | |
---|
219 | def __init__(self, parent, manager=None, data=None, *args, **kwargs): |
---|
220 | """ |
---|
221 | """ |
---|
222 | nb.__init__(self, parent, -1, |
---|
223 | style= wx.aui.AUI_BUTTON_DOWN| |
---|
224 | wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
225 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
226 | wx.CLIP_CHILDREN) |
---|
227 | PanelBase.__init__(self, parent) |
---|
228 | self.enable_close_button() |
---|
229 | self.parent = parent |
---|
230 | self.manager = manager |
---|
231 | self.data = data |
---|
232 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page) |
---|
233 | |
---|
234 | def enable_close_button(self): |
---|
235 | """ |
---|
236 | display the close button on tab for more than 1 tabs else remove the |
---|
237 | close button |
---|
238 | """ |
---|
239 | if self.GetPageCount() <= 1: |
---|
240 | style = self.GetWindowStyleFlag() |
---|
241 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
242 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == flag: |
---|
243 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
244 | self.SetWindowStyle(style) |
---|
245 | else: |
---|
246 | style = self.GetWindowStyleFlag() |
---|
247 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
248 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != flag: |
---|
249 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
250 | self.SetWindowStyle(style) |
---|
251 | |
---|
252 | def on_edit_axis(self): |
---|
253 | """ |
---|
254 | Return the select cell of a given selected column |
---|
255 | """ |
---|
256 | pos = self.GetSelection() |
---|
257 | grid = self.GetPage(pos) |
---|
258 | if len(grid.selected_cols) > 1: |
---|
259 | msg = "Edit axis doesn't understand this selection.\n" |
---|
260 | msg += "Please select only one column" |
---|
261 | raise ValueError, msg |
---|
262 | list_of_cells = [] |
---|
263 | if len(grid.selected_cols) == 1: |
---|
264 | col = grid.selected_cols[0] |
---|
265 | if len(grid.selected_cells) > 0: |
---|
266 | cell_row, cell_col = grid.selected_cells[0] |
---|
267 | if cell_col != col: |
---|
268 | msg = "Edit axis doesn't understand this selection.\n" |
---|
269 | msg += "Please select element of the same col" |
---|
270 | raise ValueError, msg |
---|
271 | for row in range(grid.GetNumberRows()): |
---|
272 | list_of_cells.append((row + 1 , col)) |
---|
273 | for item in grid.selected_cells: |
---|
274 | if item in list_of_cells: |
---|
275 | list_of_cells.remove(item) |
---|
276 | elif len(grid.selected_cols) == 0: |
---|
277 | list_of_cells = [(row + 1, col) for row, col in grid.selected_cells] |
---|
278 | return list_of_cells |
---|
279 | |
---|
280 | def get_column_labels(self): |
---|
281 | """ |
---|
282 | return dictionary of columns labels of the current page |
---|
283 | """ |
---|
284 | pos = self.GetSelection() |
---|
285 | grid = self.GetPage(pos) |
---|
286 | labels = {} |
---|
287 | row = 0 |
---|
288 | for col in range(grid.GetNumberCols()): |
---|
289 | label = grid.GetCellValue(row, col) |
---|
290 | if label.strip() != "" : |
---|
291 | labels[label.strip()] = col |
---|
292 | return labels |
---|
293 | |
---|
294 | def create_axis_label(self, cell_list): |
---|
295 | """ |
---|
296 | Receive a list of cells and create a string presenting the selected |
---|
297 | cells. |
---|
298 | :param cell_list: list of tuple |
---|
299 | |
---|
300 | """ |
---|
301 | pos = self.GetSelection() |
---|
302 | grid = self.GetPage(pos) |
---|
303 | label = "" |
---|
304 | col_name = "" |
---|
305 | if len(cell_list) > 0: |
---|
306 | temp_list = copy.deepcopy(cell_list) |
---|
307 | temp_list.sort() |
---|
308 | temp = [] |
---|
309 | for item in temp_list: |
---|
310 | if item[0] <= 1: |
---|
311 | temp.append(item) |
---|
312 | for element in temp: |
---|
313 | temp_list.remove(element) |
---|
314 | row_min, col = temp_list[0] |
---|
315 | row_max = row_min |
---|
316 | col_name = grid.GetCellValue(0, col) |
---|
317 | label += str(col_name) + "[" + str(row_min) + ":" |
---|
318 | for index in xrange(len(temp_list)): |
---|
319 | prev = index - 1 |
---|
320 | row, _ = temp_list[index] |
---|
321 | if row > row_max + 1 : |
---|
322 | if prev > 0: |
---|
323 | row_max, _ = temp_list[prev] |
---|
324 | label += str(row_max) + "]" + "," |
---|
325 | row_min = row |
---|
326 | label += "[" + str(row_min) + ":" |
---|
327 | row_max = row |
---|
328 | if (index == len(temp_list)- 1): |
---|
329 | label += str(row_max) + "]" |
---|
330 | return label, col_name |
---|
331 | |
---|
332 | def on_close_page(self, event): |
---|
333 | """ |
---|
334 | close the page |
---|
335 | """ |
---|
336 | if self.GetPageCount() == 1: |
---|
337 | event.Veto() |
---|
338 | self.enable_close_button() |
---|
339 | |
---|
340 | def set_data(self, data_inputs, data_outputs): |
---|
341 | if data_outputs is None or data_outputs == {}: |
---|
342 | return |
---|
343 | grid = GridPage(self, panel=self.parent) |
---|
344 | grid.set_data(data_inputs, data_outputs) |
---|
345 | self.AddPage(grid, "") |
---|
346 | pos = self.GetPageIndex(grid) |
---|
347 | title = "Batch " + str(self.GetPageCount()) |
---|
348 | self.SetPageText(pos, title) |
---|
349 | |
---|
350 | def add_column(self): |
---|
351 | """ |
---|
352 | Append a new column to the grid |
---|
353 | """ |
---|
354 | pos = self.GetSelection() |
---|
355 | grid = self.GetPage(pos) |
---|
356 | grid.AppendCols(1, True) |
---|
357 | |
---|
358 | def on_remove_column(self): |
---|
359 | """ |
---|
360 | Remove column to the current grid |
---|
361 | """ |
---|
362 | pos = self.GetSelection() |
---|
363 | grid = self.GetPage(pos) |
---|
364 | cols_pos = grid.GetSelectedCols() |
---|
365 | for cpos in cols_pos: |
---|
366 | grid.DeleteCols(cpos) |
---|
367 | |
---|
368 | |
---|
369 | class SPanel(ScrolledPanel): |
---|
370 | def __init__(self, parent, *args, **kwds): |
---|
371 | ScrolledPanel.__init__(self, parent , *args, **kwds) |
---|
372 | self.SetupScrolling() |
---|
373 | |
---|
374 | |
---|
375 | class GridPanel(SPanel): |
---|
376 | def __init__(self, parent, data_inputs=None, |
---|
377 | data_outputs=None, *args, **kwds): |
---|
378 | SPanel.__init__(self, parent , *args, **kwds) |
---|
379 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
380 | |
---|
381 | self.plotting_sizer = wx.FlexGridSizer(3, 7, 10, 5) |
---|
382 | self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
383 | self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0), |
---|
384 | (wx.StaticLine(self, -1), 0, wx.EXPAND, 0), |
---|
385 | (self.plotting_sizer)]) |
---|
386 | self.parent = parent |
---|
387 | self._data_inputs = data_inputs |
---|
388 | self._data_outputs = data_outputs |
---|
389 | self.x = [] |
---|
390 | self.y = [] |
---|
391 | self.x_axis_label = None |
---|
392 | self.y_axis_label = None |
---|
393 | self.x_axis_title = None |
---|
394 | self.y_axis_title = None |
---|
395 | self.x_axis_unit = None |
---|
396 | self.y_axis_unit = None |
---|
397 | self.plot_button = None |
---|
398 | self.notebook = None |
---|
399 | self.layout_grid() |
---|
400 | self.layout_plotting_area() |
---|
401 | self.SetSizer(self.vbox) |
---|
402 | |
---|
403 | def set_data(self, data_inputs, data_outputs): |
---|
404 | """ |
---|
405 | """ |
---|
406 | if self.notebook is not None: |
---|
407 | self.notebook.set_data(data_inputs, data_outputs) |
---|
408 | |
---|
409 | def set_xaxis(self, label="", x=None): |
---|
410 | """ |
---|
411 | """ |
---|
412 | if x is None: |
---|
413 | x = [] |
---|
414 | self.x = x |
---|
415 | self.x_axis_label.SetValue("%s[:]" % str(label)) |
---|
416 | self.x_axis_title.SetValue(str(label)) |
---|
417 | |
---|
418 | def set_yaxis(self, label="", y=None): |
---|
419 | """ |
---|
420 | """ |
---|
421 | if y is None: |
---|
422 | y = [] |
---|
423 | self.y = y |
---|
424 | self.y_axis_label.SetValue("%s[:]" % str(label)) |
---|
425 | self.y_axis_title.SetValue(str(label)) |
---|
426 | |
---|
427 | def get_plot_axis(self, col, list): |
---|
428 | """ |
---|
429 | |
---|
430 | """ |
---|
431 | axis = [] |
---|
432 | pos = self.notebook.GetSelection() |
---|
433 | grid = self.notebook.GetPage(pos) |
---|
434 | for row in list: |
---|
435 | label = grid.GetCellValue(row - 1, col) |
---|
436 | if label.strip() != "": |
---|
437 | axis.append(float(label.strip())) |
---|
438 | return axis |
---|
439 | |
---|
440 | def on_plot(self, event): |
---|
441 | """ |
---|
442 | Evaluate the contains of textcrtl and plot result |
---|
443 | """ |
---|
444 | pos = self.notebook.GetSelection() |
---|
445 | grid = self.notebook.GetPage(pos) |
---|
446 | column_names = {} |
---|
447 | if grid is not None: |
---|
448 | column_names = self.notebook.get_column_labels() |
---|
449 | #evalue x |
---|
450 | sentence = self.x_axis_label.GetValue() |
---|
451 | if sentence.strip() == "": |
---|
452 | msg = "select value for x axis" |
---|
453 | raise ValueError, msg |
---|
454 | dict = parse_string(sentence, column_names.keys()) |
---|
455 | for tok, (col_name, list) in dict.iteritems(): |
---|
456 | col = column_names[col_name] |
---|
457 | xaxis = self.get_plot_axis(col, list) |
---|
458 | sentence = sentence.replace(tok, |
---|
459 | "numpy.array(%s)" % str(xaxis)) |
---|
460 | for key, value in FUNC_DICT.iteritems(): |
---|
461 | sentence = sentence.replace(key.lower(), value) |
---|
462 | x = eval(sentence) |
---|
463 | #evaluate y |
---|
464 | sentence = self.y_axis_label.GetValue() |
---|
465 | if sentence.strip() == "": |
---|
466 | msg = "select value for y axis" |
---|
467 | raise ValueError, msg |
---|
468 | dict = parse_string(sentence, column_names.keys()) |
---|
469 | for tok, (col_name, list) in dict.iteritems(): |
---|
470 | col = column_names[col_name] |
---|
471 | yaxis = self.get_plot_axis(col, list) |
---|
472 | sentence = sentence.replace(tok, |
---|
473 | "numpy.array(%s)" % str(yaxis)) |
---|
474 | for key, value in FUNC_DICT.iteritems(): |
---|
475 | sentence = sentence.replace(key, value) |
---|
476 | y = eval(sentence) |
---|
477 | #plotting |
---|
478 | new_plot = Data1D(x=x, y=y) |
---|
479 | new_plot.id = wx.NewId() |
---|
480 | new_plot.group_id = wx.NewId() |
---|
481 | title = "%s vs %s" % (self.y_axis_title.GetValue(), |
---|
482 | self.x_axis_title.GetValue()) |
---|
483 | new_plot.xaxis(self.x_axis_title.GetValue(), self.x_axis_unit.GetValue()) |
---|
484 | new_plot.yaxis(self.y_axis_title.GetValue(), self.y_axis_unit.GetValue()) |
---|
485 | try: |
---|
486 | title = self.notebook.GetPageText(pos) |
---|
487 | wx.PostEvent(self.parent.parent, |
---|
488 | NewPlotEvent(plot=new_plot, |
---|
489 | group_id=str(new_plot.group_id), title =title)) |
---|
490 | except: |
---|
491 | pass |
---|
492 | |
---|
493 | def layout_grid(self): |
---|
494 | """ |
---|
495 | Draw the area related to the grid |
---|
496 | """ |
---|
497 | self.notebook = Notebook(parent=self) |
---|
498 | self.notebook.set_data(self._data_inputs, self._data_outputs) |
---|
499 | self.grid_sizer.Add(self.notebook, 1, wx.EXPAND, 0) |
---|
500 | |
---|
501 | def layout_plotting_area(self): |
---|
502 | """ |
---|
503 | Draw area containing options to plot |
---|
504 | """ |
---|
505 | self.x_axis_title = wx.TextCtrl(self, -1) |
---|
506 | self.y_axis_title = wx.TextCtrl(self, -1) |
---|
507 | self.x_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
508 | self.y_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
509 | self.x_axis_add = wx.Button(self, -1, "Add") |
---|
510 | self.x_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
511 | id=self.x_axis_add.GetId()) |
---|
512 | self.y_axis_add = wx.Button(self, -1, "Add") |
---|
513 | self.y_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
514 | id=self.y_axis_add.GetId()) |
---|
515 | self.x_axis_unit = wx.TextCtrl(self, -1) |
---|
516 | self.y_axis_unit = wx.TextCtrl(self, -1) |
---|
517 | self.plot_button = wx.Button(self, -1, "Plot") |
---|
518 | wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot) |
---|
519 | self.plotting_sizer.AddMany([ |
---|
520 | (wx.StaticText(self, -1, "x-axis label"), 1, |
---|
521 | wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
522 | (self.x_axis_label, 1, wx.TOP|wx.BOTTOM, 10), |
---|
523 | (self.x_axis_add, 1, wx.TOP|wx.BOTTOM|wx.RIGHT, 10), |
---|
524 | (wx.StaticText(self, -1, "x-axis title"), 1, |
---|
525 | wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
526 | (self.x_axis_title, 1, wx.TOP|wx.BOTTOM, 10), |
---|
527 | (wx.StaticText(self, -1 , "unit"), 1, |
---|
528 | wx.TOP|wx.BOTTOM, 10), |
---|
529 | (self.x_axis_unit, 1, wx.TOP|wx.BOTTOM, 10), |
---|
530 | (wx.StaticText(self, -1, "y-axis label"), 1, |
---|
531 | wx.BOTTOM|wx.LEFT, 10), |
---|
532 | (self.y_axis_label, wx.BOTTOM, 10), |
---|
533 | (self.y_axis_add, 1, wx.BOTTOM|wx.RIGHT, 10), |
---|
534 | (wx.StaticText(self, -1, "y-axis title"), 1, |
---|
535 | wx.BOTTOM|wx.LEFT, 10), |
---|
536 | (self.y_axis_title, wx.BOTTOM, 10), |
---|
537 | (wx.StaticText(self, -1 , "unit"), 1, wx.BOTTOM, 10), |
---|
538 | (self.y_axis_unit, 1, wx.BOTTOM, 10), |
---|
539 | (-1, -1), |
---|
540 | (-1, -1), |
---|
541 | (-1, -1), |
---|
542 | (-1, -1), |
---|
543 | (-1, -1), |
---|
544 | (-1, -1), |
---|
545 | (self.plot_button, 1, wx.LEFT|wx.BOTTOM, 10)]) |
---|
546 | |
---|
547 | def on_edit_axis(self, event): |
---|
548 | """ |
---|
549 | Get the selected column on the visible grid and set values for axis |
---|
550 | """ |
---|
551 | cell_list = self.notebook.on_edit_axis() |
---|
552 | label, title = self.create_axis_label(cell_list) |
---|
553 | tcrtl = event.GetEventObject() |
---|
554 | if tcrtl == self.x_axis_add: |
---|
555 | self.edit_axis_helper(self.x_axis_label, self.x_axis_title, |
---|
556 | label, title) |
---|
557 | elif tcrtl == self.y_axis_add: |
---|
558 | self.edit_axis_helper(self.y_axis_label, self.y_axis_title, |
---|
559 | label, title) |
---|
560 | |
---|
561 | def create_axis_label(self, cell_list): |
---|
562 | """ |
---|
563 | Receive a list of cells and create a string presenting the selected |
---|
564 | cells. |
---|
565 | :param cell_list: list of tuple |
---|
566 | |
---|
567 | """ |
---|
568 | if self.notebook is not None: |
---|
569 | return self.notebook.create_axis_label(cell_list) |
---|
570 | |
---|
571 | def edit_axis_helper(self, tcrtl_label, tcrtl_title, label, title): |
---|
572 | """ |
---|
573 | get controls to modify |
---|
574 | """ |
---|
575 | tcrtl_label.SetValue(str(label)) |
---|
576 | tcrtl_title.SetValue(str(title)) |
---|
577 | |
---|
578 | def add_column(self): |
---|
579 | """ |
---|
580 | """ |
---|
581 | if self.notebook is not None: |
---|
582 | self.notebook.add_column() |
---|
583 | |
---|
584 | def on_remove_column(self): |
---|
585 | """ |
---|
586 | """ |
---|
587 | if self.notebook is not None: |
---|
588 | self.notebook.on_remove_column() |
---|
589 | |
---|
590 | |
---|
591 | class GridFrame(wx.Frame): |
---|
592 | def __init__(self, parent=None, data_inputs=None, data_outputs=None, id=-1, |
---|
593 | title="Batch Results", size=(700, 400)): |
---|
594 | wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
595 | self.parent = parent |
---|
596 | self.panel = GridPanel(self, data_inputs, data_outputs) |
---|
597 | menubar = wx.MenuBar() |
---|
598 | self.SetMenuBar(menubar) |
---|
599 | edit = wx.Menu() |
---|
600 | menubar.Append(edit, "&Edit") |
---|
601 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
602 | |
---|
603 | add_col_menu = edit.Append(wx.NewId(), 'Add Column', 'Add column') |
---|
604 | wx.EVT_MENU(self, add_col_menu.GetId(), self.on_add_column) |
---|
605 | remove_col_menu = edit.Append(wx.NewId(), 'Remove Column', |
---|
606 | 'Remove Column') |
---|
607 | wx.EVT_MENU(self, remove_col_menu.GetId(), self.on_remove_column) |
---|
608 | |
---|
609 | def on_close(self, event): |
---|
610 | """ |
---|
611 | """ |
---|
612 | self.Hide() |
---|
613 | |
---|
614 | def on_remove_column(self, event): |
---|
615 | """ |
---|
616 | Remove the selected column to the grid |
---|
617 | """ |
---|
618 | self.panel.on_remove_column() |
---|
619 | |
---|
620 | def on_add_column(self, event): |
---|
621 | """ |
---|
622 | Append a new column to the grid |
---|
623 | """ |
---|
624 | self.panel.add_column() |
---|
625 | |
---|
626 | def set_data(self, data_inputs, data_outputs): |
---|
627 | """ |
---|
628 | """ |
---|
629 | self.panel.set_data(data_inputs, data_outputs) |
---|
630 | |
---|
631 | |
---|
632 | class BatchOutputFrame(wx.Frame): |
---|
633 | """ |
---|
634 | Allow to select where the result of batch will be displayed or stored |
---|
635 | """ |
---|
636 | def __init__(self, parent, data_inputs, data_outputs, file_name="", |
---|
637 | details="", *args, **kwds): |
---|
638 | """ |
---|
639 | :param parent: Window instantiating this dialog |
---|
640 | :param result: result to display in a grid or export to an external |
---|
641 | application. |
---|
642 | """ |
---|
643 | #kwds['style'] = wx.CAPTION|wx.SYSTEM_MENU |
---|
644 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
645 | self.parent = parent |
---|
646 | self.panel = wx.Panel(self) |
---|
647 | self.file_name = file_name |
---|
648 | self.details = details |
---|
649 | self.data_inputs = data_inputs |
---|
650 | self.data_outputs = data_outputs |
---|
651 | self.data = {} |
---|
652 | for item in (self.data_outputs, self.data_inputs): |
---|
653 | self.data.update(item) |
---|
654 | self.flag = 1 |
---|
655 | self.SetSize((300, 200)) |
---|
656 | self.local_app_selected = None |
---|
657 | self.external_app_selected = None |
---|
658 | self.save_to_file = None |
---|
659 | self._do_layout() |
---|
660 | |
---|
661 | def _do_layout(self): |
---|
662 | """ |
---|
663 | Draw the content of the current dialog window |
---|
664 | """ |
---|
665 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
666 | box_description = wx.StaticBox(self.panel, -1, str("Batch Outputs")) |
---|
667 | hint_sizer = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
668 | selection_sizer = wx.GridBagSizer(5, 5) |
---|
669 | button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
670 | text = "Open with %s" % self.parent.application_name |
---|
671 | self.local_app_selected = wx.RadioButton(self.panel, -1, text, |
---|
672 | style=wx.RB_GROUP) |
---|
673 | self.Bind(wx.EVT_RADIOBUTTON, self.onselect, |
---|
674 | id=self.local_app_selected.GetId()) |
---|
675 | text = "Open with Excel" |
---|
676 | self.external_app_selected = wx.RadioButton(self.panel, -1, text) |
---|
677 | self.Bind(wx.EVT_RADIOBUTTON, self.onselect, |
---|
678 | id=self.external_app_selected.GetId()) |
---|
679 | text = "Save to File" |
---|
680 | self.save_to_file = wx.CheckBox(self.panel, -1, text) |
---|
681 | self.Bind(wx.EVT_CHECKBOX, self.onselect, |
---|
682 | id=self.save_to_file.GetId()) |
---|
683 | self.local_app_selected.SetValue(True) |
---|
684 | self.external_app_selected.SetValue(False) |
---|
685 | self.save_to_file.SetValue(False) |
---|
686 | button_close = wx.Button(self.panel, -1, "Close") |
---|
687 | button_close.Bind(wx.EVT_BUTTON, id=button_close.GetId(), |
---|
688 | handler=self.on_close) |
---|
689 | button_apply = wx.Button(self.panel, -1, "Apply") |
---|
690 | button_apply.Bind(wx.EVT_BUTTON, id=button_apply.GetId(), |
---|
691 | handler=self.on_apply) |
---|
692 | button_apply.SetFocus() |
---|
693 | hint = "" |
---|
694 | hint_sizer.Add(wx.StaticText(self.panel, -1, hint)) |
---|
695 | hint_sizer.Add(selection_sizer) |
---|
696 | #draw area containing radio buttons |
---|
697 | ix = 0 |
---|
698 | iy = 0 |
---|
699 | selection_sizer.Add(self.local_app_selected, (iy, ix), |
---|
700 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
701 | iy += 1 |
---|
702 | selection_sizer.Add(self.external_app_selected, (iy, ix), |
---|
703 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
704 | iy += 1 |
---|
705 | selection_sizer.Add(self.save_to_file, (iy, ix), |
---|
706 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
707 | #contruction the sizer contaning button |
---|
708 | button_sizer.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
709 | |
---|
710 | button_sizer.Add(button_close, 0, |
---|
711 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
712 | button_sizer.Add(button_apply, 0, |
---|
713 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
714 | vbox.Add(hint_sizer, 0, wx.EXPAND|wx.ALL, 10) |
---|
715 | vbox.Add(wx.StaticLine(self.panel, -1), 0, wx.EXPAND, 0) |
---|
716 | vbox.Add(button_sizer, 0 , wx.TOP|wx.BOTTOM, 10) |
---|
717 | self.SetSizer(vbox) |
---|
718 | |
---|
719 | def on_apply(self, event): |
---|
720 | """ |
---|
721 | Get the user selection and display output to the selected application |
---|
722 | """ |
---|
723 | if self.flag == 1: |
---|
724 | self.parent.open_with_localapp(data_inputs=self.data_inputs, |
---|
725 | data_outputs=self.data_outputs) |
---|
726 | elif self.flag == 2: |
---|
727 | self.parent.open_with_externalapp(data=self.data, |
---|
728 | file_name=self.file_name, |
---|
729 | details=self.details) |
---|
730 | def on_close(self, event): |
---|
731 | """ |
---|
732 | close the Window |
---|
733 | """ |
---|
734 | self.Close() |
---|
735 | |
---|
736 | def onselect(self, event=None): |
---|
737 | """ |
---|
738 | Receive event and display data into third party application |
---|
739 | or save data to file. |
---|
740 | |
---|
741 | """ |
---|
742 | if self.save_to_file.GetValue(): |
---|
743 | reader, ext = os.path.splitext(self.file_name) |
---|
744 | path = None |
---|
745 | location = os.getcwd() |
---|
746 | if self.parent is not None: |
---|
747 | location = os.path.dirname(self.file_name) |
---|
748 | dlg = wx.FileDialog(self, "Save Project file", |
---|
749 | location, self.file_name, ext, wx.SAVE) |
---|
750 | path = None |
---|
751 | if dlg.ShowModal() == wx.ID_OK: |
---|
752 | path = dlg.GetPath() |
---|
753 | dlg.Destroy() |
---|
754 | if path != None: |
---|
755 | if self.parent is not None and self.data is not None: |
---|
756 | self.parent.write_batch_tofile(data=self.data, |
---|
757 | file_name=path, |
---|
758 | details=self.details) |
---|
759 | if self.local_app_selected.GetValue(): |
---|
760 | self.flag = 1 |
---|
761 | else: |
---|
762 | self.flag = 2 |
---|
763 | return self.flag |
---|
764 | |
---|
765 | |
---|
766 | |
---|
767 | if __name__ == "__main__": |
---|
768 | app = wx.App() |
---|
769 | |
---|
770 | try: |
---|
771 | data = {} |
---|
772 | j = 0 |
---|
773 | for i in range(4): |
---|
774 | j += 1 |
---|
775 | data["index"+str(i)] = [i/j, i*j, i, i+j] |
---|
776 | |
---|
777 | frame = GridFrame(data_outputs=data, data_inputs=data) |
---|
778 | frame.Show(True) |
---|
779 | except: |
---|
780 | print sys.exc_value |
---|
781 | |
---|
782 | app.MainLoop() |
---|