1 | """ |
---|
2 | Implement grid used to store data |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import numpy |
---|
6 | import math |
---|
7 | import time |
---|
8 | import re |
---|
9 | import os |
---|
10 | import sys |
---|
11 | import copy |
---|
12 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
13 | import wx.grid as Grid |
---|
14 | import wx.aui |
---|
15 | from wx.aui import AuiNotebook as nb |
---|
16 | import wx.lib.sheet as sheet |
---|
17 | from sans.guiframe.panel_base import PanelBase |
---|
18 | from sans.guiframe.utils import format_number |
---|
19 | from sans.guiframe.events import NewPlotEvent |
---|
20 | from sans.guiframe.events import StatusEvent |
---|
21 | from danse.common.plottools import plottables |
---|
22 | from sans.guiframe.dataFitting import Data1D |
---|
23 | |
---|
24 | FUNC_DICT = {"sqrt": "math.sqrt", |
---|
25 | "pow": "math.sqrt"} |
---|
26 | |
---|
27 | class BatchCell: |
---|
28 | """ |
---|
29 | Object describing a cell in the grid. |
---|
30 | |
---|
31 | """ |
---|
32 | def __init__(self): |
---|
33 | self.label = "" |
---|
34 | self.value = None |
---|
35 | self.col = -1 |
---|
36 | self.row = -1 |
---|
37 | self.object = [] |
---|
38 | |
---|
39 | |
---|
40 | def parse_string(sentence, list): |
---|
41 | """ |
---|
42 | Return a dictionary of column label and index or row selected |
---|
43 | :param sentence: String to parse |
---|
44 | :param list: list of columns label |
---|
45 | """ |
---|
46 | toks = [] |
---|
47 | p2 = re.compile(r'\d+') |
---|
48 | p = re.compile(r'[\+\-\*\%\/]') |
---|
49 | labels = p.split(sentence) |
---|
50 | col_dict = {} |
---|
51 | for elt in labels: |
---|
52 | rang = None |
---|
53 | temp_arr = [] |
---|
54 | for label in list: |
---|
55 | label_pos = elt.find(label) |
---|
56 | separator_pos = label_pos + len(label) |
---|
57 | if label_pos != -1 and len(elt) >= separator_pos and\ |
---|
58 | elt[separator_pos]== "[": |
---|
59 | # the label contain , meaning the range selected is not |
---|
60 | # continuous |
---|
61 | if elt.count(',') > 0: |
---|
62 | new_temp = [] |
---|
63 | temp = elt.split(label) |
---|
64 | for item in temp: |
---|
65 | range_pos = item.find(":") |
---|
66 | if range_pos != -1: |
---|
67 | rang = p2.findall(item) |
---|
68 | for i in xrange(int(rang[0]), int(rang[1])+1 ): |
---|
69 | new_temp.append(i) |
---|
70 | temp_arr += new_temp |
---|
71 | else: |
---|
72 | # continuous range |
---|
73 | temp = elt.split(label) |
---|
74 | for item in temp: |
---|
75 | if item.strip() != "": |
---|
76 | range_pos = item.find(":") |
---|
77 | if range_pos != -1: |
---|
78 | rang = p2.findall(item) |
---|
79 | for i in xrange(int(rang[0]), int(rang[1])+1 ): |
---|
80 | temp_arr.append(i) |
---|
81 | col_dict[elt] = (label, temp_arr) |
---|
82 | return col_dict |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | class SPanel(ScrolledPanel): |
---|
87 | def __init__(self, parent, *args, **kwds): |
---|
88 | ScrolledPanel.__init__(self, parent , *args, **kwds) |
---|
89 | self.SetupScrolling() |
---|
90 | |
---|
91 | class GridPage(sheet.CSheet): |
---|
92 | """ |
---|
93 | """ |
---|
94 | def __init__(self, parent, panel=None): |
---|
95 | """ |
---|
96 | """ |
---|
97 | sheet.CSheet.__init__(self, parent) |
---|
98 | |
---|
99 | self.AdjustScrollbars() |
---|
100 | #self.SetLabelBackgroundColour('#DBD4D4') |
---|
101 | self.uid = wx.NewId() |
---|
102 | self.parent = parent |
---|
103 | self.panel = panel |
---|
104 | self.col_names = [] |
---|
105 | self.data_inputs = {} |
---|
106 | self.data_outputs = {} |
---|
107 | self.data = None |
---|
108 | self.details = "" |
---|
109 | self.file_name = None |
---|
110 | self._cols = 50 |
---|
111 | self._rows = 51 |
---|
112 | self.last_selected_row = -1 |
---|
113 | self.last_selected_col = -1 |
---|
114 | self.col_width = 30 |
---|
115 | self.row_height = 20 |
---|
116 | self.max_row_touse = 0 |
---|
117 | self.axis_value = [] |
---|
118 | self.axis_label = "" |
---|
119 | self.selected_cells = [] |
---|
120 | self.selected_cols = [] |
---|
121 | self.selected_rows = [] |
---|
122 | self.plottable_cells = [] |
---|
123 | self.plottable_flag = False |
---|
124 | self.SetColMinimalAcceptableWidth(self.col_width) |
---|
125 | self.SetRowMinimalAcceptableHeight(self.row_height) |
---|
126 | self.SetNumberRows(self._cols) |
---|
127 | self.SetNumberCols(self._rows) |
---|
128 | self.AutoSize() |
---|
129 | self.list_plot_panels = {} |
---|
130 | self.default_col_width = 75 |
---|
131 | if self.GetNumberCols() > 0: |
---|
132 | self.default_col_width = self.GetColSize(0) |
---|
133 | self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_left_click) |
---|
134 | self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click) |
---|
135 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.on_selected_cell) |
---|
136 | self.Bind(wx.grid.EVT_GRID_CMD_CELL_CHANGE, self.on_edit_cell) |
---|
137 | |
---|
138 | |
---|
139 | def on_edit_cell(self, event): |
---|
140 | """ |
---|
141 | """ |
---|
142 | row, col = event.GetRow(), event.GetCol() |
---|
143 | if row > self.max_row_touse: |
---|
144 | self.max_row_touse = row |
---|
145 | event.Skip() |
---|
146 | |
---|
147 | def on_selected_cell(self, event): |
---|
148 | """ |
---|
149 | Handler catching cell selection |
---|
150 | """ |
---|
151 | flag = event.CmdDown() or event.ControlDown() |
---|
152 | row, col = event.GetRow(), event.GetCol() |
---|
153 | cell = (row, col) |
---|
154 | event.Skip() |
---|
155 | if not flag: |
---|
156 | self.selected_cols = [] |
---|
157 | self.selected_rows = [] |
---|
158 | self.selected_cells = [] |
---|
159 | self.axis_label = "" |
---|
160 | self.axis_value = [] |
---|
161 | self.plottable_list = [] |
---|
162 | self.plottable_cells = [] |
---|
163 | self.plottable_flag = False |
---|
164 | self.last_selected_col = col |
---|
165 | self.last_selected_row = row |
---|
166 | if col >= 0: |
---|
167 | label_row = 0 |
---|
168 | self.axis_label = self.GetCellValue(label_row, col) |
---|
169 | self.selected_cols.append(col) |
---|
170 | if cell not in self.selected_cells: |
---|
171 | if row > 0: |
---|
172 | self.selected_cells.append(cell) |
---|
173 | self.selected_rows.append(row) |
---|
174 | else: |
---|
175 | if flag: |
---|
176 | self.selected_cells.remove(cell) |
---|
177 | self.axis_value = [] |
---|
178 | for cell_row, cell_col in self.selected_cells: |
---|
179 | if cell_row > 0 and cell_row < self.max_row_touse: |
---|
180 | self.axis_value.append(self.GetCellValue(cell_row, cell_col)) |
---|
181 | |
---|
182 | |
---|
183 | def on_left_click(self, event): |
---|
184 | """ |
---|
185 | Catch the left click on label mouse event |
---|
186 | """ |
---|
187 | event.Skip() |
---|
188 | flag = event.CmdDown() or event.ControlDown() |
---|
189 | col = event.GetCol() |
---|
190 | row = event.GetRow() |
---|
191 | if not flag: |
---|
192 | self.selected_cols = [] |
---|
193 | self.selected_rows = [] |
---|
194 | self.selected_cells = [] |
---|
195 | self.axis_label = "" |
---|
196 | self.axis_value = [] |
---|
197 | self.plottable_list = [] |
---|
198 | self.plottable_cells = [] |
---|
199 | self.plottable_flag = False |
---|
200 | |
---|
201 | self.last_selected_col = col |
---|
202 | self.last_selected_row = row |
---|
203 | if row != -1 and row not in self.selected_rows: |
---|
204 | self.selected_rows.append(row) |
---|
205 | |
---|
206 | if col != -1: |
---|
207 | for row in range(1, self.GetNumberRows()+ 1): |
---|
208 | cell = (row, col) |
---|
209 | if row > 0 and row < self.max_row_touse: |
---|
210 | if cell not in self.selected_cells: |
---|
211 | self.selected_cells.append(cell) |
---|
212 | else: |
---|
213 | if flag: |
---|
214 | self.selected_cells.remove(cell) |
---|
215 | self.selected_cols.append(col) |
---|
216 | self.axis_value = [] |
---|
217 | for cell_row, cell_col in self.selected_cells: |
---|
218 | self.axis_value.append(self.GetCellValue(cell_row, cell_col)) |
---|
219 | self.axis_label = self.GetCellValue(0, col) |
---|
220 | |
---|
221 | def on_right_click(self, event): |
---|
222 | """ |
---|
223 | Catch the right click mouse |
---|
224 | """ |
---|
225 | |
---|
226 | col = event.GetCol() |
---|
227 | self.selected_cols = [] |
---|
228 | self.selected_cols.append(col) |
---|
229 | # Slicer plot popup menu |
---|
230 | slicerpop = wx.Menu() |
---|
231 | col_label_menu = wx.Menu() |
---|
232 | c_name = self.GetCellValue(0, col) |
---|
233 | label = "Insert column before %s " % str(c_name) |
---|
234 | slicerpop.AppendSubMenu(col_label_menu , |
---|
235 | '&%s' % str(label), str(label)) |
---|
236 | col_name = [self.GetCellValue(0, c) |
---|
237 | for c in range(self.GetNumberCols())] |
---|
238 | row = 0 |
---|
239 | label = self.GetCellValue(row, col) |
---|
240 | self.insert_col_menu(col_label_menu, label, self) |
---|
241 | |
---|
242 | |
---|
243 | col_after_menu = wx.Menu() |
---|
244 | label = "Insert column after %s " % str(c_name) |
---|
245 | slicerpop.AppendSubMenu(col_after_menu , |
---|
246 | '&%s' % str(label), str(label)) |
---|
247 | col_name = [self.GetCellValue(0, c) |
---|
248 | for c in range(self.GetNumberCols())] |
---|
249 | self.insert_after_col_menu(col_after_menu, label, self) |
---|
250 | |
---|
251 | |
---|
252 | id = wx.NewId() |
---|
253 | hint = 'Remove selected column %s' |
---|
254 | slicerpop.Append(id, '&Remove Column', hint) |
---|
255 | wx.EVT_MENU(self, id, self.on_remove_column) |
---|
256 | |
---|
257 | pos = wx.GetMousePosition() |
---|
258 | pos = self.ScreenToClient(pos) |
---|
259 | self.PopupMenu(slicerpop, pos) |
---|
260 | event.Skip() |
---|
261 | |
---|
262 | def insert_col_menu(self, menu, label, window): |
---|
263 | """ |
---|
264 | """ |
---|
265 | id = wx.NewId() |
---|
266 | title = "Empty" |
---|
267 | hint = 'Insert empty column before %s' % str(label) |
---|
268 | menu.Append(id, title, hint) |
---|
269 | wx.EVT_MENU(window, id, self.on_insert_column) |
---|
270 | row = 0 |
---|
271 | col_name = [self.GetCellValue(row, col) |
---|
272 | for col in range(self.GetNumberCols())] |
---|
273 | for c_name in self.data.keys(): |
---|
274 | if c_name not in col_name: |
---|
275 | id = wx.NewId() |
---|
276 | hint = "Insert %s column before the " % str(c_name) |
---|
277 | hint += " %s column" % str(label) |
---|
278 | menu.Append(id, '&%s' % str(c_name), hint) |
---|
279 | wx.EVT_MENU(window, id, self.on_insert_column) |
---|
280 | |
---|
281 | def insert_after_col_menu(self, menu, label, window): |
---|
282 | """ |
---|
283 | """ |
---|
284 | id = wx.NewId() |
---|
285 | title = "Empty" |
---|
286 | hint = 'Insert empty column after %s' % str(label) |
---|
287 | menu.Append(id, title, hint) |
---|
288 | wx.EVT_MENU(window, id, self.on_insert_after_column) |
---|
289 | row = 0 |
---|
290 | col_name = [self.GetCellValue(row, col) |
---|
291 | for col in range(self.GetNumberCols())] |
---|
292 | for c_name in self.data.keys(): |
---|
293 | if c_name not in col_name: |
---|
294 | id = wx.NewId() |
---|
295 | hint = "Insert %s column after the " % str(c_name) |
---|
296 | hint += " %s column" % str(label) |
---|
297 | menu.Append(id, '&%s' % str(c_name), hint) |
---|
298 | wx.EVT_MENU(window, id, self.on_insert_after_column) |
---|
299 | |
---|
300 | def on_remove_column(self, event=None): |
---|
301 | """ |
---|
302 | """ |
---|
303 | if self.selected_cols is not None or len(self.selected_cols) > 0: |
---|
304 | col = self.selected_cols[0] |
---|
305 | self.remove_column(col=col, numCols=1) |
---|
306 | |
---|
307 | def remove_column(self, col, numCols=1): |
---|
308 | """ |
---|
309 | Remove column to the current grid |
---|
310 | """ |
---|
311 | # add data to the grid |
---|
312 | row = 0 |
---|
313 | col_name = self.GetCellValue(row, col) |
---|
314 | self.data[col_name] = [] |
---|
315 | for row in range(1, self.GetNumberRows() + 1): |
---|
316 | if row < self.max_row_touse: |
---|
317 | value = self.GetCellValue(row, col) |
---|
318 | self.data[col_name].append(value) |
---|
319 | for k , value_list in self.data.iteritems(): |
---|
320 | if k != col_name: |
---|
321 | length = len(value_list) |
---|
322 | if length < self.max_row_touse: |
---|
323 | diff = self.max_row_touse - length |
---|
324 | for i in range(diff): |
---|
325 | self.data[k].append("") |
---|
326 | self.DeleteCols(pos=col, numCols=numCols, updateLabels=True) |
---|
327 | |
---|
328 | def on_insert_column(self, event): |
---|
329 | """ |
---|
330 | """ |
---|
331 | if self.selected_cols is not None or len(self.selected_cols) > 0: |
---|
332 | col = self.selected_cols[0] |
---|
333 | # add data to the grid |
---|
334 | row = 0 |
---|
335 | id = event.GetId() |
---|
336 | col_name = event.GetEventObject().GetLabelText(id) |
---|
337 | self.insert_column(col=col, col_name=col_name) |
---|
338 | if not issubclass(event.GetEventObject().__class__ , wx.Menu): |
---|
339 | col += 1 |
---|
340 | self.selected_cols[0] += 1 |
---|
341 | |
---|
342 | def on_insert_after_column(self, event): |
---|
343 | """ |
---|
344 | Insert the given column after the highlighted column |
---|
345 | """ |
---|
346 | if self.selected_cols is not None or len(self.selected_cols) > 0: |
---|
347 | col = self.selected_cols[0] + 1 |
---|
348 | # add data to the grid |
---|
349 | row = 0 |
---|
350 | id = event.GetId() |
---|
351 | col_name = event.GetEventObject().GetLabelText(id) |
---|
352 | self.insert_column(col=col, col_name=col_name) |
---|
353 | if not issubclass(event.GetEventObject().__class__ , wx.Menu): |
---|
354 | self.selected_cols[0] += 1 |
---|
355 | |
---|
356 | def insert_column(self, col, col_name): |
---|
357 | """ |
---|
358 | """ |
---|
359 | |
---|
360 | row = 0 |
---|
361 | self.InsertCols(pos=col, numCols=1, updateLabels=True) |
---|
362 | if col_name.strip() != "Empty": |
---|
363 | self.SetCellValue(row, col, str(col_name.strip())) |
---|
364 | if col_name in self.data.keys(): |
---|
365 | value_list = self.data[col_name] |
---|
366 | cell_row = 1 |
---|
367 | for value in value_list: |
---|
368 | label = format_number(value, high=True) |
---|
369 | self.SetCellValue(cell_row, col, str(label)) |
---|
370 | cell_row += 1 |
---|
371 | self.AutoSizeColumn(col, True) |
---|
372 | width = self.GetColSize(col) |
---|
373 | if width < self.default_col_width: |
---|
374 | self.SetColSize(col, self.default_col_width) |
---|
375 | self.ForceRefresh() |
---|
376 | |
---|
377 | def on_set_x_axis(self, event): |
---|
378 | """ |
---|
379 | """ |
---|
380 | self.panel.set_xaxis(x=self.axis_value, label=self.axis_label) |
---|
381 | |
---|
382 | def on_set_y_axis(self, event): |
---|
383 | """ |
---|
384 | """ |
---|
385 | self.panel.set_yaxis(y=self.axis_value, label=self.axis_label) |
---|
386 | |
---|
387 | def set_data(self, data_inputs, data_outputs, details, file_name): |
---|
388 | """ |
---|
389 | Add data to the grid |
---|
390 | :param data_inputs: data to use from the context menu of the grid |
---|
391 | :param data_ouputs: default columns deplayed |
---|
392 | """ |
---|
393 | self.file_name = file_name |
---|
394 | self.details = details |
---|
395 | |
---|
396 | if data_outputs is None: |
---|
397 | data_outputs = {} |
---|
398 | self.data_outputs = data_outputs |
---|
399 | if data_inputs is None: |
---|
400 | data_inputs = {} |
---|
401 | self.data_inputs = data_inputs |
---|
402 | self.data = {} |
---|
403 | for item in (self.data_outputs, self.data_inputs): |
---|
404 | self.data.update(item) |
---|
405 | if len(self.data_outputs) > 0: |
---|
406 | self._cols = self.GetNumberCols() |
---|
407 | self._rows = self.GetNumberRows() |
---|
408 | self.col_names = self.data_outputs.keys() |
---|
409 | self.col_names.sort() |
---|
410 | nbr_user_cols = len(self.col_names) |
---|
411 | #Add more columns to the grid if necessary |
---|
412 | if nbr_user_cols > self._cols: |
---|
413 | new_col_nbr = nbr_user_cols - self._cols |
---|
414 | self.AppendCols(new_col_nbr, True) |
---|
415 | #Add more rows to the grid if necessary |
---|
416 | nbr_user_row = len(self.data_outputs.values()) |
---|
417 | if nbr_user_row > self._rows + 1: |
---|
418 | new_row_nbr = nbr_user_row - self._rows |
---|
419 | self.AppendRows(new_row_nbr, True) |
---|
420 | # add data to the grid |
---|
421 | row = 0 |
---|
422 | col = 0 |
---|
423 | cell_col = 0 |
---|
424 | for col_name in self.col_names: |
---|
425 | # use the first row of the grid to add user defined labels |
---|
426 | self.SetCellValue(row, col, str(col_name)) |
---|
427 | col += 1 |
---|
428 | cell_row = 1 |
---|
429 | value_list = self.data_outputs[col_name] |
---|
430 | |
---|
431 | for value in value_list: |
---|
432 | label = value |
---|
433 | if issubclass(value.__class__, BatchCell): |
---|
434 | label = value.label |
---|
435 | try: |
---|
436 | float(label) |
---|
437 | label = format_number(label, high=True) |
---|
438 | except: |
---|
439 | label = str(label) |
---|
440 | self.SetCellValue(cell_row, cell_col, label) |
---|
441 | self.AutoSizeColumn(cell_col, True) |
---|
442 | width = self.GetColSize(cell_col) |
---|
443 | if width < self.default_col_width: |
---|
444 | self.SetColSize(cell_col, self.default_col_width) |
---|
445 | |
---|
446 | cell_row += 1 |
---|
447 | cell_col += 1 |
---|
448 | if cell_row > self.max_row_touse: |
---|
449 | self.max_row_touse = cell_row |
---|
450 | self.ForceRefresh() |
---|
451 | |
---|
452 | def get_grid_view(self): |
---|
453 | """ |
---|
454 | Return value contained in the grid |
---|
455 | """ |
---|
456 | grid_view = {} |
---|
457 | for col in xrange(self.GetNumberCols()): |
---|
458 | label = self.GetCellValue(row=0, col=col) |
---|
459 | label = label.strip() |
---|
460 | if label != "": |
---|
461 | grid_view[label] = [] |
---|
462 | for row in range(1, self.max_row_touse): |
---|
463 | value = self.GetCellValue(row=row, col=col) |
---|
464 | if value != "": |
---|
465 | grid_view[label].append(value) |
---|
466 | else: |
---|
467 | grid_view[label].append(None) |
---|
468 | return grid_view |
---|
469 | |
---|
470 | class Notebook(nb, PanelBase): |
---|
471 | """ |
---|
472 | ## Internal name for the AUI manager |
---|
473 | window_name = "Fit panel" |
---|
474 | ## Title to appear on top of the window |
---|
475 | """ |
---|
476 | window_caption = "Notebook " |
---|
477 | |
---|
478 | def __init__(self, parent, manager=None, data=None, *args, **kwargs): |
---|
479 | """ |
---|
480 | """ |
---|
481 | nb.__init__(self, parent, -1, |
---|
482 | style=wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
483 | wx.aui.AUI_BUTTON_DOWN| |
---|
484 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
485 | wx.CLIP_CHILDREN) |
---|
486 | PanelBase.__init__(self, parent) |
---|
487 | self.enable_close_button() |
---|
488 | self.parent = parent |
---|
489 | self.manager = manager |
---|
490 | self.data = data |
---|
491 | #add empty page |
---|
492 | self.add_empty_page() |
---|
493 | |
---|
494 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page) |
---|
495 | |
---|
496 | def add_empty_page(self): |
---|
497 | """ |
---|
498 | """ |
---|
499 | grid = GridPage(self, panel=self.parent) |
---|
500 | self.AddPage(grid, "", True) |
---|
501 | pos = self.GetPageIndex(grid) |
---|
502 | title = "Batch " + str(self.GetPageCount()) |
---|
503 | self.SetPageText(pos, title) |
---|
504 | self.SetSelection(pos) |
---|
505 | return grid , pos |
---|
506 | |
---|
507 | def enable_close_button(self): |
---|
508 | """ |
---|
509 | display the close button on tab for more than 1 tabs else remove the |
---|
510 | close button |
---|
511 | """ |
---|
512 | if self.GetPageCount() <= 1: |
---|
513 | style = self.GetWindowStyleFlag() |
---|
514 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
515 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == flag: |
---|
516 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
517 | self.SetWindowStyle(style) |
---|
518 | else: |
---|
519 | style = self.GetWindowStyleFlag() |
---|
520 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
521 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != flag: |
---|
522 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
523 | self.SetWindowStyle(style) |
---|
524 | |
---|
525 | def on_edit_axis(self): |
---|
526 | """ |
---|
527 | Return the select cell of a given selected column. Check that all cells |
---|
528 | are from the same column |
---|
529 | """ |
---|
530 | pos = self.GetSelection() |
---|
531 | grid = self.GetPage(pos) |
---|
532 | if len(grid.selected_cols) >= 1: |
---|
533 | col = grid.selected_cols[0] |
---|
534 | for c in grid.selected_cols: |
---|
535 | if c != col: |
---|
536 | msg = "Edit axis doesn't understand this selection.\n" |
---|
537 | msg += "Please select only one column" |
---|
538 | raise ValueError, msg |
---|
539 | for (cell_row, cell_col) in grid.selected_cells: |
---|
540 | if cell_col != col: |
---|
541 | msg = "Cannot use cells from different columns for " |
---|
542 | msg += "this operation.\n" |
---|
543 | msg += "Please select elements of the same col.\n" |
---|
544 | raise ValueError, msg |
---|
545 | else: |
---|
546 | msg = "No item selected.\n" |
---|
547 | msg += "Please select only one column or one cell" |
---|
548 | raise ValueError, msg |
---|
549 | |
---|
550 | return grid.selected_cells |
---|
551 | |
---|
552 | |
---|
553 | def get_column_labels(self): |
---|
554 | """ |
---|
555 | return dictionary of columns labels of the current page |
---|
556 | """ |
---|
557 | pos = self.GetSelection() |
---|
558 | grid = self.GetPage(pos) |
---|
559 | labels = {} |
---|
560 | row = 0 |
---|
561 | for col in range(grid.GetNumberCols()): |
---|
562 | label = grid.GetCellValue(row, col) |
---|
563 | if label.strip() != "" : |
---|
564 | labels[label.strip()] = col |
---|
565 | return labels |
---|
566 | |
---|
567 | def create_axis_label(self, cell_list): |
---|
568 | """ |
---|
569 | Receive a list of cells and create a string presenting the selected |
---|
570 | cells. |
---|
571 | :param cell_list: list of tuple |
---|
572 | |
---|
573 | """ |
---|
574 | pos = self.GetSelection() |
---|
575 | grid = self.GetPage(pos) |
---|
576 | label = "" |
---|
577 | col_name = "" |
---|
578 | def create_label(col_name, row_min=None, row_max=None): |
---|
579 | """ |
---|
580 | """ |
---|
581 | result = "" |
---|
582 | if row_min is not None or row_max is not None: |
---|
583 | if row_min is None: |
---|
584 | result = str(row_max) + "]" |
---|
585 | elif row_max is None: |
---|
586 | result = str(col_name) + "[" + str(row_min) + ":" |
---|
587 | else: |
---|
588 | result = str(col_name) + "[" + str(row_min) + ":" |
---|
589 | result += str(row_max) + "]" |
---|
590 | return str(result) |
---|
591 | |
---|
592 | if len(cell_list) > 0: |
---|
593 | if len(cell_list) == 1: |
---|
594 | row_min, col = cell_list[0] |
---|
595 | col_name = grid.GetCellValue(0, col) |
---|
596 | label = create_label(col_name, row_min+1 , row_min+1) |
---|
597 | return label, col_name |
---|
598 | else: |
---|
599 | temp_list = copy.deepcopy(cell_list) |
---|
600 | temp_list.sort() |
---|
601 | length = len(temp_list) |
---|
602 | row_min, col = temp_list[0] |
---|
603 | row_max, _ = temp_list[length-1] |
---|
604 | col_name = grid.GetCellValue(0, col) |
---|
605 | index = 0 |
---|
606 | for row in xrange(row_min, row_max +1): |
---|
607 | if index > 0 and index < len(temp_list): |
---|
608 | new_row, _ = temp_list[index] |
---|
609 | if row != new_row: |
---|
610 | temp_list.insert(index, (None, None)) |
---|
611 | if index -1 >= 0: |
---|
612 | new_row, _ = temp_list[index-1] |
---|
613 | label += create_label(col_name, None, new_row +1) |
---|
614 | label += "," |
---|
615 | if index + 1 < len(temp_list): |
---|
616 | new_row, _ = temp_list[index + 1] |
---|
617 | label += create_label(col_name, new_row+1, None) |
---|
618 | if index == 0: |
---|
619 | label += create_label(col_name, row_min+1, None) |
---|
620 | elif index == len(temp_list)-1: |
---|
621 | label += create_label(col_name, None, row_max+1) |
---|
622 | index += 1 |
---|
623 | return label, col_name |
---|
624 | |
---|
625 | def on_close_page(self, event): |
---|
626 | """ |
---|
627 | close the page |
---|
628 | """ |
---|
629 | if self.GetPageCount() == 1: |
---|
630 | event.Veto() |
---|
631 | self.enable_close_button() |
---|
632 | |
---|
633 | def set_data(self, data_inputs, data_outputs, details="", file_name=None): |
---|
634 | if data_outputs is None or data_outputs == {}: |
---|
635 | return |
---|
636 | |
---|
637 | for pos in range(self.GetPageCount()): |
---|
638 | grid = self.GetPage(pos) |
---|
639 | if grid.data is None: |
---|
640 | #Found empty page |
---|
641 | grid.set_data(data_inputs=data_inputs, |
---|
642 | data_outputs=data_outputs, |
---|
643 | details=details, |
---|
644 | file_name=file_name) |
---|
645 | self.SetSelection(pos) |
---|
646 | return |
---|
647 | |
---|
648 | grid, pos = self.add_empty_page() |
---|
649 | grid.set_data(data_inputs=data_inputs, |
---|
650 | data_outputs=data_outputs, |
---|
651 | file_name=file_name, |
---|
652 | details=details) |
---|
653 | |
---|
654 | def add_column(self): |
---|
655 | """ |
---|
656 | Append a new column to the grid |
---|
657 | """ |
---|
658 | pos = self.GetSelection() |
---|
659 | grid = self.GetPage(pos) |
---|
660 | grid.AppendCols(1, True) |
---|
661 | |
---|
662 | |
---|
663 | |
---|
664 | |
---|
665 | |
---|
666 | class GridPanel(SPanel): |
---|
667 | def __init__(self, parent, data_inputs=None, |
---|
668 | data_outputs=None, *args, **kwds): |
---|
669 | SPanel.__init__(self, parent , *args, **kwds) |
---|
670 | |
---|
671 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
672 | |
---|
673 | self.plotting_sizer = wx.FlexGridSizer(3, 7, 10, 5) |
---|
674 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
675 | self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
676 | self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0), |
---|
677 | (wx.StaticLine(self, -1), 0, wx.EXPAND, 0), |
---|
678 | (self.plotting_sizer), |
---|
679 | (self.button_sizer)]) |
---|
680 | self.parent = parent |
---|
681 | self._data_inputs = data_inputs |
---|
682 | self._data_outputs = data_outputs |
---|
683 | self.x = [] |
---|
684 | self.y = [] |
---|
685 | self.x_axis_label = None |
---|
686 | self.y_axis_label = None |
---|
687 | self.x_axis_title = None |
---|
688 | self.y_axis_title = None |
---|
689 | self.x_axis_unit = None |
---|
690 | self.y_axis_unit = None |
---|
691 | self.view_button = None |
---|
692 | self.plot_button = None |
---|
693 | self.notebook = None |
---|
694 | |
---|
695 | self.layout_grid() |
---|
696 | self.layout_plotting_area() |
---|
697 | self.SetSizer(self.vbox) |
---|
698 | |
---|
699 | def set_xaxis(self, label="", x=None): |
---|
700 | """ |
---|
701 | """ |
---|
702 | if x is None: |
---|
703 | x = [] |
---|
704 | self.x = x |
---|
705 | self.x_axis_label.SetValue("%s[:]" % str(label)) |
---|
706 | self.x_axis_title.SetValue(str(label)) |
---|
707 | |
---|
708 | def set_yaxis(self, label="", y=None): |
---|
709 | """ |
---|
710 | """ |
---|
711 | if y is None: |
---|
712 | y = [] |
---|
713 | self.y = y |
---|
714 | self.y_axis_label.SetValue("%s[:]" % str(label)) |
---|
715 | self.y_axis_title.SetValue(str(label)) |
---|
716 | |
---|
717 | def get_plot_axis(self, col, list): |
---|
718 | """ |
---|
719 | |
---|
720 | """ |
---|
721 | axis = [] |
---|
722 | pos = self.notebook.GetSelection() |
---|
723 | grid = self.notebook.GetPage(pos) |
---|
724 | for row in list: |
---|
725 | label = grid.GetCellValue(0, col) |
---|
726 | value = grid.GetCellValue(row - 1, col).strip() |
---|
727 | if value != "": |
---|
728 | if label.lower().strip() == "data": |
---|
729 | axis.append(float(row - 1)) |
---|
730 | else: |
---|
731 | try: |
---|
732 | axis.append(float(value)) |
---|
733 | except: |
---|
734 | msg = "Invalid data in row %s column %s" % (str(row), |
---|
735 | str(col)) |
---|
736 | wx.PostEvent(self.parent.parent, |
---|
737 | StatusEvent(status=msg, info="error")) |
---|
738 | else: |
---|
739 | axis.append(None) |
---|
740 | return axis |
---|
741 | |
---|
742 | def on_save_column(self, parent): |
---|
743 | """ |
---|
744 | """ |
---|
745 | pos = self.notebook.GetSelection() |
---|
746 | grid = self.notebook.GetPage(pos) |
---|
747 | if parent is not None and self.data is not None: |
---|
748 | parent.write_batch_tofile(data=grid.data, |
---|
749 | file_name=path, |
---|
750 | details=self.details) |
---|
751 | |
---|
752 | def on_view(self, event): |
---|
753 | """ |
---|
754 | Get object represented buy the given cell and plot them. |
---|
755 | """ |
---|
756 | pos = self.notebook.GetSelection() |
---|
757 | grid = self.notebook.GetPage(pos) |
---|
758 | title = self.notebook.GetPageText(pos) |
---|
759 | if len(grid.selected_cells) == 0: |
---|
760 | msg = "Highlight a Data or Chi2 column first..." |
---|
761 | wx.PostEvent(self.parent.parent, |
---|
762 | StatusEvent(status=msg, info="error")) |
---|
763 | return |
---|
764 | for cell in grid.selected_cells: |
---|
765 | row, col = cell |
---|
766 | label_row = 0 |
---|
767 | label = grid.GetCellValue(label_row, col) |
---|
768 | if label in grid.data: |
---|
769 | values = grid.data[label] |
---|
770 | if row > len(values) or row < 1: |
---|
771 | msg = "Invalid cell was chosen." |
---|
772 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, |
---|
773 | info="error")) |
---|
774 | time.sleep(0.5) |
---|
775 | continue |
---|
776 | else: |
---|
777 | value = values[row -1] |
---|
778 | if issubclass(value.__class__, BatchCell): |
---|
779 | if value.object is None or len(value.object) == 0: |
---|
780 | msg = "Row %s , " % str(row) |
---|
781 | msg += "Column %s is NOT " % str(label) |
---|
782 | msg += "the results of fits to view..." |
---|
783 | #raise ValueError, msg |
---|
784 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, |
---|
785 | info="error")) |
---|
786 | return |
---|
787 | for new_plot in value.object: |
---|
788 | if new_plot is None or \ |
---|
789 | not issubclass(new_plot.__class__, |
---|
790 | plottables.Plottable): |
---|
791 | msg = "Row %s , " % str(row) |
---|
792 | msg += "Column %s is NOT " % str(label) |
---|
793 | msg += "the results of fits to view..." |
---|
794 | #raise ValueError, msg |
---|
795 | wx.PostEvent(self.parent.parent, |
---|
796 | StatusEvent(status=msg, info="error")) |
---|
797 | time.sleep(0.5) |
---|
798 | continue |
---|
799 | |
---|
800 | if issubclass(new_plot.__class__, Data1D): |
---|
801 | if label in grid.list_plot_panels.keys(): |
---|
802 | group_id = grid.list_plot_panels[label] |
---|
803 | else: |
---|
804 | group_id = str(new_plot.group_id) + str(grid.uid) |
---|
805 | grid.list_plot_panels[label] = group_id |
---|
806 | if group_id not in new_plot.list_group_id: |
---|
807 | new_plot.group_id = group_id |
---|
808 | new_plot.list_group_id.append(group_id) |
---|
809 | else: |
---|
810 | if label.lower() in ["data", "chi2"]: |
---|
811 | if len(grid.selected_cells) != 1: |
---|
812 | msg = "2D View: Please select one data set" |
---|
813 | msg += " at a time for View Results." |
---|
814 | wx.PostEvent(self.parent.parent, |
---|
815 | StatusEvent(status=msg, |
---|
816 | info="error")) |
---|
817 | time.sleep(0.5) |
---|
818 | continue |
---|
819 | """ |
---|
820 | wx.PostEvent(self.parent.parent, |
---|
821 | NewPlotEvent(action="clear", |
---|
822 | group_id=str(group_id), |
---|
823 | title=title)) |
---|
824 | """ |
---|
825 | wx.PostEvent(self.parent.parent, |
---|
826 | NewPlotEvent(plot=new_plot, |
---|
827 | group_id=str(new_plot.group_id), |
---|
828 | title=title)) |
---|
829 | msg = "Plotting the View Results completed!" |
---|
830 | wx.PostEvent( self.parent.parent, |
---|
831 | StatusEvent(status=msg)) |
---|
832 | else: |
---|
833 | |
---|
834 | msg = "Row %s , " % str(row) |
---|
835 | msg += "Column %s is NOT " % str(label) |
---|
836 | msg += "the results of fits to view..." |
---|
837 | #raise ValueError, msg |
---|
838 | wx.PostEvent(self.parent.parent, |
---|
839 | StatusEvent(status=msg, info="error")) |
---|
840 | time.sleep(0.5) |
---|
841 | continue |
---|
842 | |
---|
843 | |
---|
844 | |
---|
845 | |
---|
846 | def on_plot(self, event): |
---|
847 | """ |
---|
848 | Evaluate the contains of textcrtl and plot result |
---|
849 | """ |
---|
850 | pos = self.notebook.GetSelection() |
---|
851 | grid = self.notebook.GetPage(pos) |
---|
852 | column_names = {} |
---|
853 | if grid is not None: |
---|
854 | column_names = self.notebook.get_column_labels() |
---|
855 | #evaluate x |
---|
856 | sentence = self.x_axis_label.GetValue() |
---|
857 | try: |
---|
858 | if sentence.strip() == "": |
---|
859 | msg = "Select column values for x axis" |
---|
860 | raise ValueError, msg |
---|
861 | except: |
---|
862 | wx.PostEvent(self.parent.parent, |
---|
863 | StatusEvent(status=msg, info="error")) |
---|
864 | return |
---|
865 | |
---|
866 | dict = parse_string(sentence, column_names.keys()) |
---|
867 | for tok, (col_name, list) in dict.iteritems(): |
---|
868 | col = column_names[col_name] |
---|
869 | xaxis = self.get_plot_axis(col, list) |
---|
870 | sentence = sentence.replace(tok, |
---|
871 | "numpy.array(%s)" % str(xaxis)) |
---|
872 | for key, value in FUNC_DICT.iteritems(): |
---|
873 | sentence = sentence.replace(key.lower(), value) |
---|
874 | x = eval(sentence) |
---|
875 | #evaluate y |
---|
876 | sentence = self.y_axis_label.GetValue() |
---|
877 | if sentence.strip() == "": |
---|
878 | msg = "select value for y axis" |
---|
879 | raise ValueError, msg |
---|
880 | dict = parse_string(sentence, column_names.keys()) |
---|
881 | for tok, (col_name, list) in dict.iteritems(): |
---|
882 | col = column_names[col_name] |
---|
883 | yaxis = self.get_plot_axis(col, list) |
---|
884 | sentence = sentence.replace(tok, |
---|
885 | "numpy.array(%s)" % str(yaxis)) |
---|
886 | for key, value in FUNC_DICT.iteritems(): |
---|
887 | sentence = sentence.replace(key, value) |
---|
888 | y = eval(sentence) |
---|
889 | if len(x) != len(y) and (len(x) == 0 or len(y) == 0): |
---|
890 | msg = "Need same length for X and Y axis and both greater than 0" |
---|
891 | msg += " to plot.\n" |
---|
892 | msg += "Got X length = %s, Y length = %s" % (str(len(x)), |
---|
893 | str(len(y))) |
---|
894 | wx.PostEvent(self.parent.parent, |
---|
895 | StatusEvent(status=msg, info="error")) |
---|
896 | return |
---|
897 | |
---|
898 | #plotting |
---|
899 | new_plot = Data1D(x=x, y=y) |
---|
900 | new_plot.id = wx.NewId() |
---|
901 | new_plot.group_id = wx.NewId() |
---|
902 | title = "%s vs %s" % (self.y_axis_title.GetValue(), |
---|
903 | self.x_axis_title.GetValue()) |
---|
904 | new_plot.xaxis(self.x_axis_title.GetValue(), |
---|
905 | self.x_axis_unit.GetValue()) |
---|
906 | new_plot.yaxis(self.y_axis_title.GetValue(), |
---|
907 | self.y_axis_unit.GetValue()) |
---|
908 | try: |
---|
909 | title = self.notebook.GetPageText(pos) |
---|
910 | new_plot.name = title |
---|
911 | new_plot.xtransform = "x" |
---|
912 | new_plot.ytransform = "y" |
---|
913 | wx.PostEvent(self.parent.parent, |
---|
914 | NewPlotEvent(plot=new_plot, |
---|
915 | group_id=str(new_plot.group_id), title =title)) |
---|
916 | msg = "Plotting completed!" |
---|
917 | wx.PostEvent( self.parent.parent, |
---|
918 | StatusEvent(status=msg)) |
---|
919 | except: |
---|
920 | wx.PostEvent(self.parent.parent, |
---|
921 | StatusEvent(status=msg, info="error")) |
---|
922 | |
---|
923 | def layout_grid(self): |
---|
924 | """ |
---|
925 | Draw the area related to the grid |
---|
926 | """ |
---|
927 | self.notebook = Notebook(parent=self) |
---|
928 | self.notebook.set_data(self._data_inputs, self._data_outputs) |
---|
929 | self.grid_sizer.Add(self.notebook, 1, wx.EXPAND, 0) |
---|
930 | |
---|
931 | def layout_plotting_area(self): |
---|
932 | """ |
---|
933 | Draw area containing options to plot |
---|
934 | """ |
---|
935 | |
---|
936 | self.x_axis_title = wx.TextCtrl(self, -1) |
---|
937 | self.y_axis_title = wx.TextCtrl(self, -1) |
---|
938 | self.x_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
939 | self.y_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
940 | self.x_axis_add = wx.Button(self, -1, "Add") |
---|
941 | self.x_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
942 | id=self.x_axis_add.GetId()) |
---|
943 | self.y_axis_add = wx.Button(self, -1, "Add") |
---|
944 | self.y_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
945 | id=self.y_axis_add.GetId()) |
---|
946 | self.x_axis_unit = wx.TextCtrl(self, -1) |
---|
947 | self.y_axis_unit = wx.TextCtrl(self, -1) |
---|
948 | self.view_button = wx.Button(self, -1, "View Results") |
---|
949 | view_tip = "Highlight the data set or the Chi2 column first." |
---|
950 | self.view_button.SetToolTipString(view_tip) |
---|
951 | wx.EVT_BUTTON(self, self.view_button.GetId(), self.on_view) |
---|
952 | self.plot_button = wx.Button(self, -1, "Plot") |
---|
953 | plot_tip = "Highlight a column for each axis and \n" |
---|
954 | plot_tip += "click the Add buttons first." |
---|
955 | self.plot_button.SetToolTipString(plot_tip) |
---|
956 | self.button_sizer.AddMany( [ (500, 30), |
---|
957 | (self.view_button, 0, wx.RIGHT|wx.BOTTOM, 10), |
---|
958 | (self.plot_button, 0, wx.RIGHT|wx.BOTTOM, 10)]) |
---|
959 | |
---|
960 | wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot) |
---|
961 | self.plotting_sizer.AddMany([ |
---|
962 | (wx.StaticText(self, -1, |
---|
963 | "X-axis Label\nSelection Range"), 1, |
---|
964 | wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
965 | (self.x_axis_label, 1, wx.TOP|wx.BOTTOM, 10), |
---|
966 | (self.x_axis_add, 1, wx.TOP|wx.BOTTOM|wx.RIGHT, 10), |
---|
967 | (wx.StaticText(self, -1, "X-axis Label"), 1, |
---|
968 | wx.TOP|wx.BOTTOM|wx.LEFT, 10), |
---|
969 | (self.x_axis_title, 1, wx.TOP|wx.BOTTOM, 10), |
---|
970 | (wx.StaticText(self, -1 , "X-axis Unit"), 1, |
---|
971 | wx.TOP|wx.BOTTOM, 10), |
---|
972 | (self.x_axis_unit, 1, wx.TOP|wx.BOTTOM, 10), |
---|
973 | (wx.StaticText(self, -1, |
---|
974 | "Y-axis Label\nSelection Range"), 1, |
---|
975 | wx.BOTTOM|wx.LEFT, 10), |
---|
976 | (self.y_axis_label, wx.BOTTOM, 10), |
---|
977 | (self.y_axis_add, 1, wx.BOTTOM|wx.RIGHT, 10), |
---|
978 | (wx.StaticText(self, -1, "Y-axis Label"), 1, |
---|
979 | wx.BOTTOM|wx.LEFT, 10), |
---|
980 | (self.y_axis_title, wx.BOTTOM, 10), |
---|
981 | (wx.StaticText(self, -1 , "Y-axis Unit"), 1, wx.BOTTOM, 10), |
---|
982 | (self.y_axis_unit, 1, wx.BOTTOM, 10), |
---|
983 | (-1, -1), |
---|
984 | (-1, -1), |
---|
985 | (-1, -1), |
---|
986 | (-1, -1), |
---|
987 | (-1, -1), |
---|
988 | (-1, -1), |
---|
989 | (-1, 1)]) |
---|
990 | |
---|
991 | def on_edit_axis(self, event): |
---|
992 | """ |
---|
993 | Get the selected column on the visible grid and set values for axis |
---|
994 | """ |
---|
995 | try: |
---|
996 | cell_list = self.notebook.on_edit_axis() |
---|
997 | except: |
---|
998 | msg = str(sys.exc_value) |
---|
999 | wx.PostEvent(self.parent.parent, |
---|
1000 | StatusEvent(status=msg, info="error")) |
---|
1001 | return |
---|
1002 | label, title = self.create_axis_label(cell_list) |
---|
1003 | tcrtl = event.GetEventObject() |
---|
1004 | if tcrtl == self.x_axis_add: |
---|
1005 | self.edit_axis_helper(self.x_axis_label, self.x_axis_title, |
---|
1006 | label, title) |
---|
1007 | elif tcrtl == self.y_axis_add: |
---|
1008 | self.edit_axis_helper(self.y_axis_label, self.y_axis_title, |
---|
1009 | label, title) |
---|
1010 | |
---|
1011 | def create_axis_label(self, cell_list): |
---|
1012 | """ |
---|
1013 | Receive a list of cells and create a string presenting the selected |
---|
1014 | cells. |
---|
1015 | :param cell_list: list of tuple |
---|
1016 | |
---|
1017 | """ |
---|
1018 | if self.notebook is not None: |
---|
1019 | return self.notebook.create_axis_label(cell_list) |
---|
1020 | |
---|
1021 | def edit_axis_helper(self, tcrtl_label, tcrtl_title, label, title): |
---|
1022 | """ |
---|
1023 | get controls to modify |
---|
1024 | """ |
---|
1025 | tcrtl_label.SetValue(str(label)) |
---|
1026 | tcrtl_title.SetValue(str(title)) |
---|
1027 | |
---|
1028 | def add_column(self): |
---|
1029 | """ |
---|
1030 | """ |
---|
1031 | if self.notebook is not None: |
---|
1032 | self.notebook.add_column() |
---|
1033 | |
---|
1034 | def on_remove_column(self): |
---|
1035 | """ |
---|
1036 | """ |
---|
1037 | if self.notebook is not None: |
---|
1038 | self.notebook.on_remove_column() |
---|
1039 | |
---|
1040 | |
---|
1041 | class GridFrame(wx.Frame): |
---|
1042 | def __init__(self, parent=None, data_inputs=None, data_outputs=None, id=-1, |
---|
1043 | title="Batch Window", size=(800, 500)): |
---|
1044 | wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
1045 | self.parent = parent |
---|
1046 | self.panel = GridPanel(self, data_inputs, data_outputs) |
---|
1047 | menubar = wx.MenuBar() |
---|
1048 | self.SetMenuBar(menubar) |
---|
1049 | |
---|
1050 | self.curr_col = None |
---|
1051 | self.curr_grid = None |
---|
1052 | self.curr_col_name = "" |
---|
1053 | file = wx.Menu() |
---|
1054 | menubar.Append(file, "&File") |
---|
1055 | |
---|
1056 | hint = "Open file containing batch results" |
---|
1057 | open_menu = file.Append(wx.NewId(), 'Open ', hint) |
---|
1058 | wx.EVT_MENU(self, open_menu.GetId(), self.on_open) |
---|
1059 | |
---|
1060 | hint = "Open the the current grid into excel" |
---|
1061 | open_excel_menu = file.Append(wx.NewId(), 'Open with Excel', hint) |
---|
1062 | wx.EVT_MENU(self, open_excel_menu.GetId(), self.open_with_excel) |
---|
1063 | file.AppendSeparator() |
---|
1064 | save_menu = file.Append(wx.NewId(), 'Save As', 'Save into File') |
---|
1065 | wx.EVT_MENU(self, save_menu.GetId(), self.on_save_page) |
---|
1066 | |
---|
1067 | self.edit = wx.Menu() |
---|
1068 | hint = "Insert column before the selected column" |
---|
1069 | self.insert_before_menu = wx.Menu() |
---|
1070 | self.insert_sub_menu = self.edit.AppendSubMenu(self.insert_before_menu, |
---|
1071 | 'Insert Before', hint) |
---|
1072 | |
---|
1073 | hint = "Remove the selected column" |
---|
1074 | self.remove_menu = self.edit.Append(-1, 'Remove Column', hint) |
---|
1075 | wx.EVT_MENU(self, self.remove_menu.GetId(), self.on_remove_column) |
---|
1076 | |
---|
1077 | self.Bind(wx.EVT_MENU_OPEN, self.on_menu_open) |
---|
1078 | menubar.Append(self.edit, "&Edit") |
---|
1079 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
1080 | |
---|
1081 | def GetLabelText(self, id): |
---|
1082 | """ |
---|
1083 | """ |
---|
1084 | for item in self.insert_before_menu.GetMenuItems(): |
---|
1085 | m_id = item.GetId() |
---|
1086 | if m_id == id: |
---|
1087 | return item.GetLabel() |
---|
1088 | |
---|
1089 | def on_remove_column(self, event): |
---|
1090 | """ |
---|
1091 | """ |
---|
1092 | pos = self.panel.notebook.GetSelection() |
---|
1093 | grid = self.panel.notebook.GetPage(pos) |
---|
1094 | grid.on_remove_column(event=None) |
---|
1095 | |
---|
1096 | def on_menu_open(self, event): |
---|
1097 | """ |
---|
1098 | |
---|
1099 | """ |
---|
1100 | if self.edit == event.GetMenu(): |
---|
1101 | #get the selected column |
---|
1102 | pos = self.panel.notebook.GetSelection() |
---|
1103 | grid = self.panel.notebook.GetPage(pos) |
---|
1104 | col_list = grid.GetSelectedCols() |
---|
1105 | if len(col_list) > 0: |
---|
1106 | self.remove_menu.Enable(True) |
---|
1107 | else: |
---|
1108 | self.remove_menu.Enable(False) |
---|
1109 | if len(col_list)== 0 or len(col_list) > 1: |
---|
1110 | self.insert_sub_menu.Enable(False) |
---|
1111 | |
---|
1112 | label = "Insert Column Before" |
---|
1113 | self.insert_sub_menu.SetText(label) |
---|
1114 | else: |
---|
1115 | self.insert_sub_menu.Enable(True) |
---|
1116 | |
---|
1117 | col = col_list[0] |
---|
1118 | #GetColLabelValue(self, col) |
---|
1119 | col_name = grid.GetCellValue(row=0, col=col) |
---|
1120 | label = "Insert Column Before " + str(col_name) |
---|
1121 | self.insert_sub_menu.SetText(label) |
---|
1122 | for item in self.insert_before_menu.GetMenuItems(): |
---|
1123 | self.insert_before_menu.DeleteItem(item) |
---|
1124 | grid.insert_col_menu(menu=self.insert_before_menu, |
---|
1125 | label=col_name, window=self) |
---|
1126 | event.Skip() |
---|
1127 | |
---|
1128 | |
---|
1129 | |
---|
1130 | def on_save_page(self, event): |
---|
1131 | """ |
---|
1132 | """ |
---|
1133 | if self.parent is not None: |
---|
1134 | pos = self.panel.notebook.GetSelection() |
---|
1135 | grid = self.panel.notebook.GetPage(pos) |
---|
1136 | if grid.file_name is None or grid.file_name.strip() == "" or \ |
---|
1137 | grid.data is None or len(grid.data) == 0: |
---|
1138 | name = self.panel.notebook.GetPageText(pos) |
---|
1139 | msg = " %s has not data to save" % str(name) |
---|
1140 | wx.PostEvent(self.parent, |
---|
1141 | StatusEvent(status=msg, info="error")) |
---|
1142 | |
---|
1143 | return |
---|
1144 | reader, ext = os.path.splitext(grid.file_name) |
---|
1145 | path = None |
---|
1146 | if self.parent is not None: |
---|
1147 | location = os.path.dirname(grid.file_name) |
---|
1148 | dlg = wx.FileDialog(self, "Save Project file", |
---|
1149 | location, grid.file_name, ext, wx.SAVE) |
---|
1150 | path = None |
---|
1151 | if dlg.ShowModal() == wx.ID_OK: |
---|
1152 | path = dlg.GetPath() |
---|
1153 | dlg.Destroy() |
---|
1154 | if path != None: |
---|
1155 | if self.parent is not None: |
---|
1156 | data = grid.get_grid_view() |
---|
1157 | self.parent.write_batch_tofile(data=data, |
---|
1158 | file_name=path, |
---|
1159 | details=grid.details) |
---|
1160 | |
---|
1161 | def on_open(self, event): |
---|
1162 | """ |
---|
1163 | Open file containg batch result |
---|
1164 | """ |
---|
1165 | if self.parent is not None: |
---|
1166 | self.parent.on_read_batch_tofile(event) |
---|
1167 | |
---|
1168 | def open_with_excel(self, event): |
---|
1169 | """ |
---|
1170 | open excel and display batch result in Excel |
---|
1171 | """ |
---|
1172 | if self.parent is not None: |
---|
1173 | pos = self.panel.notebook.GetSelection() |
---|
1174 | grid = self.panel.notebook.GetPage(pos) |
---|
1175 | data = grid.get_grid_view() |
---|
1176 | if grid.file_name is None or grid.file_name.strip() == "" or \ |
---|
1177 | grid.data is None or len(grid.data) == 0: |
---|
1178 | name = self.panel.notebook.GetPageText(pos) |
---|
1179 | msg = " %s has not data to open on excel" % str(name) |
---|
1180 | wx.PostEvent(self.parent, |
---|
1181 | StatusEvent(status=msg, info="error")) |
---|
1182 | |
---|
1183 | return |
---|
1184 | self.parent.open_with_externalapp(data=data, |
---|
1185 | file_name=grid.file_name, |
---|
1186 | details=grid.details) |
---|
1187 | |
---|
1188 | def on_close(self, event): |
---|
1189 | """ |
---|
1190 | """ |
---|
1191 | self.Hide() |
---|
1192 | |
---|
1193 | |
---|
1194 | def on_append_column(self, event): |
---|
1195 | """ |
---|
1196 | Append a new column to the grid |
---|
1197 | """ |
---|
1198 | self.panel.add_column() |
---|
1199 | |
---|
1200 | def set_data(self, data_inputs, data_outputs, details="", file_name=None): |
---|
1201 | """ |
---|
1202 | """ |
---|
1203 | |
---|
1204 | self.panel.notebook.set_data(data_inputs=data_inputs, |
---|
1205 | file_name=file_name, |
---|
1206 | details=details, |
---|
1207 | data_outputs=data_outputs) |
---|
1208 | |
---|
1209 | |
---|
1210 | class BatchOutputFrame(wx.Frame): |
---|
1211 | """ |
---|
1212 | Allow to select where the result of batch will be displayed or stored |
---|
1213 | """ |
---|
1214 | def __init__(self, parent, data_inputs, data_outputs, file_name="", |
---|
1215 | details="", *args, **kwds): |
---|
1216 | """ |
---|
1217 | :param parent: Window instantiating this dialog |
---|
1218 | :param result: result to display in a grid or export to an external |
---|
1219 | application. |
---|
1220 | """ |
---|
1221 | #kwds['style'] = wx.CAPTION|wx.SYSTEM_MENU |
---|
1222 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
1223 | self.parent = parent |
---|
1224 | self.panel = wx.Panel(self) |
---|
1225 | self.file_name = file_name |
---|
1226 | self.details = details |
---|
1227 | self.data_inputs = data_inputs |
---|
1228 | self.data_outputs = data_outputs |
---|
1229 | self.data = {} |
---|
1230 | for item in (self.data_outputs, self.data_inputs): |
---|
1231 | self.data.update(item) |
---|
1232 | self.flag = 1 |
---|
1233 | self.SetSize((300, 200)) |
---|
1234 | self.local_app_selected = None |
---|
1235 | self.external_app_selected = None |
---|
1236 | self.save_to_file = None |
---|
1237 | self._do_layout() |
---|
1238 | |
---|
1239 | def _do_layout(self): |
---|
1240 | """ |
---|
1241 | Draw the content of the current dialog window |
---|
1242 | """ |
---|
1243 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
1244 | box_description = wx.StaticBox(self.panel, -1, str("Batch Outputs")) |
---|
1245 | hint_sizer = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1246 | selection_sizer = wx.GridBagSizer(5, 5) |
---|
1247 | button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
1248 | text = "Open with %s" % self.parent.application_name |
---|
1249 | self.local_app_selected = wx.RadioButton(self.panel, -1, text, |
---|
1250 | style=wx.RB_GROUP) |
---|
1251 | self.Bind(wx.EVT_RADIOBUTTON, self.onselect, |
---|
1252 | id=self.local_app_selected.GetId()) |
---|
1253 | text = "Open with Excel" |
---|
1254 | self.external_app_selected = wx.RadioButton(self.panel, -1, text) |
---|
1255 | self.Bind(wx.EVT_RADIOBUTTON, self.onselect, |
---|
1256 | id=self.external_app_selected.GetId()) |
---|
1257 | text = "Save to File" |
---|
1258 | self.save_to_file = wx.CheckBox(self.panel, -1, text) |
---|
1259 | self.Bind(wx.EVT_CHECKBOX, self.onselect, |
---|
1260 | id=self.save_to_file.GetId()) |
---|
1261 | self.local_app_selected.SetValue(True) |
---|
1262 | self.external_app_selected.SetValue(False) |
---|
1263 | self.save_to_file.SetValue(False) |
---|
1264 | button_close = wx.Button(self.panel, -1, "Close") |
---|
1265 | button_close.Bind(wx.EVT_BUTTON, id=button_close.GetId(), |
---|
1266 | handler=self.on_close) |
---|
1267 | button_apply = wx.Button(self.panel, -1, "Apply") |
---|
1268 | button_apply.Bind(wx.EVT_BUTTON, id=button_apply.GetId(), |
---|
1269 | handler=self.on_apply) |
---|
1270 | button_apply.SetFocus() |
---|
1271 | hint = "" |
---|
1272 | hint_sizer.Add(wx.StaticText(self.panel, -1, hint)) |
---|
1273 | hint_sizer.Add(selection_sizer) |
---|
1274 | #draw area containing radio buttons |
---|
1275 | ix = 0 |
---|
1276 | iy = 0 |
---|
1277 | selection_sizer.Add(self.local_app_selected, (iy, ix), |
---|
1278 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1279 | iy += 1 |
---|
1280 | selection_sizer.Add(self.external_app_selected, (iy, ix), |
---|
1281 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1282 | iy += 1 |
---|
1283 | selection_sizer.Add(self.save_to_file, (iy, ix), |
---|
1284 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1285 | #contruction the sizer contaning button |
---|
1286 | button_sizer.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1287 | |
---|
1288 | button_sizer.Add(button_close, 0, |
---|
1289 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1290 | button_sizer.Add(button_apply, 0, |
---|
1291 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
1292 | vbox.Add(hint_sizer, 0, wx.EXPAND|wx.ALL, 10) |
---|
1293 | vbox.Add(wx.StaticLine(self.panel, -1), 0, wx.EXPAND, 0) |
---|
1294 | vbox.Add(button_sizer, 0 , wx.TOP|wx.BOTTOM, 10) |
---|
1295 | self.SetSizer(vbox) |
---|
1296 | |
---|
1297 | def on_apply(self, event): |
---|
1298 | """ |
---|
1299 | Get the user selection and display output to the selected application |
---|
1300 | """ |
---|
1301 | if self.flag == 1: |
---|
1302 | self.parent.open_with_localapp(data_inputs=self.data_inputs, |
---|
1303 | data_outputs=self.data_outputs) |
---|
1304 | elif self.flag == 2: |
---|
1305 | self.parent.open_with_externalapp(data=self.data, |
---|
1306 | file_name=self.file_name, |
---|
1307 | details=self.details) |
---|
1308 | def on_close(self, event): |
---|
1309 | """ |
---|
1310 | close the Window |
---|
1311 | """ |
---|
1312 | self.Close() |
---|
1313 | |
---|
1314 | def onselect(self, event=None): |
---|
1315 | """ |
---|
1316 | Receive event and display data into third party application |
---|
1317 | or save data to file. |
---|
1318 | |
---|
1319 | """ |
---|
1320 | if self.save_to_file.GetValue(): |
---|
1321 | reader, ext = os.path.splitext(self.file_name) |
---|
1322 | path = None |
---|
1323 | location = os.getcwd() |
---|
1324 | if self.parent is not None: |
---|
1325 | location = os.path.dirname(self.file_name) |
---|
1326 | dlg = wx.FileDialog(self, "Save Project file", |
---|
1327 | location, self.file_name, ext, wx.SAVE) |
---|
1328 | path = None |
---|
1329 | if dlg.ShowModal() == wx.ID_OK: |
---|
1330 | path = dlg.GetPath() |
---|
1331 | dlg.Destroy() |
---|
1332 | if path != None: |
---|
1333 | if self.parent is not None and self.data is not None: |
---|
1334 | self.parent.write_batch_tofile(data=self.data, |
---|
1335 | file_name=path, |
---|
1336 | details=self.details) |
---|
1337 | if self.local_app_selected.GetValue(): |
---|
1338 | self.flag = 1 |
---|
1339 | else: |
---|
1340 | self.flag = 2 |
---|
1341 | return self.flag |
---|
1342 | |
---|
1343 | |
---|
1344 | |
---|
1345 | if __name__ == "__main__": |
---|
1346 | app = wx.App() |
---|
1347 | |
---|
1348 | try: |
---|
1349 | data = {} |
---|
1350 | j = 0 |
---|
1351 | for i in range(4): |
---|
1352 | j += 1 |
---|
1353 | data["index"+str(i)] = [i/j, i*j, i, i+j] |
---|
1354 | |
---|
1355 | data_input = copy.deepcopy(data) |
---|
1356 | data_input["index5"] = [10,20,40, 50] |
---|
1357 | frame = GridFrame(data_outputs=data, data_inputs=data_input) |
---|
1358 | frame.Show(True) |
---|
1359 | except: |
---|
1360 | print sys.exc_value |
---|
1361 | |
---|
1362 | app.MainLoop() |
---|