[24adb89] | 1 | """ |
---|
| 2 | Implement grid used to store data |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
| 5 | import numpy |
---|
[904830e] | 6 | import math |
---|
| 7 | import re |
---|
[850525c] | 8 | import os |
---|
[24adb89] | 9 | import sys |
---|
[7ad194fa] | 10 | import copy |
---|
[24adb89] | 11 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
| 12 | import wx.aui |
---|
| 13 | from wx.aui import AuiNotebook as nb |
---|
| 14 | import wx.lib.sheet as sheet |
---|
[79492222] | 15 | from sas.guiframe.panel_base import PanelBase |
---|
[76aed53] | 16 | from sas.guiframe.events import NewPlotEvent |
---|
| 17 | from sas.guiframe.events import StatusEvent |
---|
[79492222] | 18 | from sas.plottools import plottables |
---|
| 19 | from sas.guiframe.dataFitting import Data1D |
---|
[24adb89] | 20 | |
---|
[56e99f9] | 21 | |
---|
[904830e] | 22 | FUNC_DICT = {"sqrt": "math.sqrt", |
---|
| 23 | "pow": "math.sqrt"} |
---|
[73197d0] | 24 | |
---|
[76aed53] | 25 | class BatchCell(object): |
---|
[5425990] | 26 | """ |
---|
| 27 | Object describing a cell in the grid. |
---|
[76aed53] | 28 | |
---|
[5425990] | 29 | """ |
---|
| 30 | def __init__(self): |
---|
| 31 | self.label = "" |
---|
| 32 | self.value = None |
---|
| 33 | self.col = -1 |
---|
| 34 | self.row = -1 |
---|
[75790dc] | 35 | self.object = [] |
---|
[76aed53] | 36 | |
---|
[73197d0] | 37 | |
---|
[904830e] | 38 | def parse_string(sentence, list): |
---|
| 39 | """ |
---|
| 40 | Return a dictionary of column label and index or row selected |
---|
| 41 | :param sentence: String to parse |
---|
| 42 | :param list: list of columns label |
---|
| 43 | """ |
---|
| 44 | p2 = re.compile(r'\d+') |
---|
| 45 | p = re.compile(r'[\+\-\*\%\/]') |
---|
| 46 | labels = p.split(sentence) |
---|
| 47 | col_dict = {} |
---|
| 48 | for elt in labels: |
---|
| 49 | rang = None |
---|
| 50 | temp_arr = [] |
---|
| 51 | for label in list: |
---|
[76aed53] | 52 | label_pos = elt.find(label) |
---|
| 53 | separator_pos = label_pos + len(label) |
---|
[08dc9e87] | 54 | if label_pos != -1 and len(elt) >= separator_pos and\ |
---|
[76aed53] | 55 | elt[separator_pos] == "[": |
---|
[08dc9e87] | 56 | # the label contain , meaning the range selected is not |
---|
| 57 | # continuous |
---|
[904830e] | 58 | if elt.count(',') > 0: |
---|
| 59 | new_temp = [] |
---|
| 60 | temp = elt.split(label) |
---|
| 61 | for item in temp: |
---|
| 62 | range_pos = item.find(":") |
---|
| 63 | if range_pos != -1: |
---|
| 64 | rang = p2.findall(item) |
---|
[76aed53] | 65 | for i in xrange(int(rang[0]), int(rang[1]) + 1): |
---|
[904830e] | 66 | new_temp.append(i) |
---|
| 67 | temp_arr += new_temp |
---|
| 68 | else: |
---|
[08dc9e87] | 69 | # continuous range |
---|
[904830e] | 70 | temp = elt.split(label) |
---|
| 71 | for item in temp: |
---|
[08dc9e87] | 72 | if item.strip() != "": |
---|
| 73 | range_pos = item.find(":") |
---|
| 74 | if range_pos != -1: |
---|
| 75 | rang = p2.findall(item) |
---|
[76aed53] | 76 | for i in xrange(int(rang[0]), int(rang[1]) + 1): |
---|
[08dc9e87] | 77 | temp_arr.append(i) |
---|
[904830e] | 78 | col_dict[elt] = (label, temp_arr) |
---|
| 79 | return col_dict |
---|
[24adb89] | 80 | |
---|
[76aed53] | 81 | |
---|
[f4b37d1] | 82 | class SPanel(ScrolledPanel): |
---|
| 83 | def __init__(self, parent, *args, **kwds): |
---|
[76aed53] | 84 | ScrolledPanel.__init__(self, parent, *args, **kwds) |
---|
| 85 | self.SetupScrolling() |
---|
| 86 | |
---|
[c85b0ae] | 87 | |
---|
| 88 | class GridCellEditor(sheet.CCellEditor): |
---|
| 89 | """ Custom cell editor """ |
---|
| 90 | def __init__(self, grid): |
---|
| 91 | super(GridCellEditor, self).__init__(grid) |
---|
| 92 | |
---|
| 93 | def EndEdit(self, row, col, grid, previous): |
---|
| 94 | """ |
---|
| 95 | Commit editing the current cell. Returns True if the value has changed. |
---|
| 96 | @param previous: previous value in the cell |
---|
| 97 | """ |
---|
| 98 | changed = False # Assume value not changed |
---|
| 99 | val = self._tc.GetValue() # Get value in edit control |
---|
| 100 | if val != self._startValue: # Compare |
---|
| 101 | changed = True # If different then changed is True |
---|
| 102 | grid.GetTable().SetValue(row, col, val) # Update the table |
---|
| 103 | self._startValue = '' # Clear the class' start value |
---|
| 104 | self._tc.SetValue('') # Clear contents of the edit control |
---|
| 105 | return changed |
---|
| 106 | |
---|
| 107 | |
---|
[24adb89] | 108 | class GridPage(sheet.CSheet): |
---|
[9c8f3ad] | 109 | """ |
---|
| 110 | """ |
---|
[24adb89] | 111 | def __init__(self, parent, panel=None): |
---|
| 112 | """ |
---|
| 113 | """ |
---|
[c85b0ae] | 114 | #sheet.CSheet.__init__(self, parent) |
---|
| 115 | |
---|
| 116 | # The following is the __init__ from CSheet. ########################## |
---|
| 117 | # We re-write it here because the class is broken in wx 3.0, |
---|
| 118 | # such that the cell editor is not able to receive the right |
---|
| 119 | # number of parameters when it is called. The only way to |
---|
| 120 | # pick a different cell editor is apparently to re-write the __init__. |
---|
| 121 | wx.grid.Grid.__init__(self, parent, -1) |
---|
| 122 | |
---|
| 123 | # Init variables |
---|
| 124 | self._lastCol = -1 # Init last cell column clicked |
---|
| 125 | self._lastRow = -1 # Init last cell row clicked |
---|
| 126 | self._selected = None # Init range currently selected |
---|
| 127 | # Map string datatype to default renderer/editor |
---|
| 128 | self.RegisterDataType(wx.grid.GRID_VALUE_STRING, |
---|
| 129 | wx.grid.GridCellStringRenderer(), |
---|
| 130 | GridCellEditor(self)) |
---|
| 131 | |
---|
| 132 | self.CreateGrid(4, 3) # By default start with a 4 x 3 grid |
---|
| 133 | self.SetColLabelSize(18) # Default sizes and alignment |
---|
| 134 | self.SetRowLabelSize(50) |
---|
| 135 | self.SetRowLabelAlignment(wx.ALIGN_RIGHT, wx.ALIGN_BOTTOM) |
---|
| 136 | self.SetColSize(0, 75) # Default column sizes |
---|
| 137 | self.SetColSize(1, 75) |
---|
| 138 | self.SetColSize(2, 75) |
---|
| 139 | |
---|
| 140 | # Sink events |
---|
| 141 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnLeftClick) |
---|
| 142 | self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, self.OnRightClick) |
---|
| 143 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnLeftDoubleClick) |
---|
| 144 | self.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self.OnRangeSelect) |
---|
| 145 | self.Bind(wx.grid.EVT_GRID_ROW_SIZE, self.OnRowSize) |
---|
| 146 | self.Bind(wx.grid.EVT_GRID_COL_SIZE, self.OnColSize) |
---|
| 147 | self.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnCellChange) |
---|
| 148 | self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnGridSelectCell) |
---|
| 149 | # This ends the __init__ section for CSheet. ########################## |
---|
[76aed53] | 150 | |
---|
[f4b37d1] | 151 | self.AdjustScrollbars() |
---|
| 152 | #self.SetLabelBackgroundColour('#DBD4D4') |
---|
[fd51a7c] | 153 | self.uid = wx.NewId() |
---|
[f4b37d1] | 154 | self.parent = parent |
---|
[24adb89] | 155 | self.panel = panel |
---|
| 156 | self.col_names = [] |
---|
[8523a1f2] | 157 | self.data_inputs = {} |
---|
| 158 | self.data_outputs = {} |
---|
[d03a356] | 159 | self.data = None |
---|
[71fa9028] | 160 | self.details = "" |
---|
| 161 | self.file_name = None |
---|
[9c8f3ad] | 162 | self._cols = 50 |
---|
[cbba84f] | 163 | self._rows = 3001 |
---|
[63dc6e5] | 164 | self.last_selected_row = -1 |
---|
| 165 | self.last_selected_col = -1 |
---|
[1c86a37] | 166 | self.col_width = 30 |
---|
| 167 | self.row_height = 20 |
---|
[656d65d] | 168 | self.max_row_touse = 0 |
---|
[49ad00b] | 169 | self.axis_value = [] |
---|
| 170 | self.axis_label = "" |
---|
| 171 | self.selected_cells = [] |
---|
| 172 | self.selected_cols = [] |
---|
[75790dc] | 173 | self.selected_rows = [] |
---|
| 174 | self.plottable_cells = [] |
---|
[63dc6e5] | 175 | self.plottable_flag = False |
---|
[1c86a37] | 176 | self.SetColMinimalAcceptableWidth(self.col_width) |
---|
| 177 | self.SetRowMinimalAcceptableHeight(self.row_height) |
---|
[23a1747] | 178 | self.SetNumberRows(self._rows) |
---|
| 179 | self.SetNumberCols(self._cols) |
---|
[86a9e6c] | 180 | color = self.parent.GetBackgroundColour() |
---|
| 181 | for col in range(self._cols): |
---|
| 182 | self.SetCellBackgroundColour(0, col, color) |
---|
[f4b37d1] | 183 | self.AutoSize() |
---|
[23477c6] | 184 | self.list_plot_panels = {} |
---|
[1c86a37] | 185 | self.default_col_width = 75 |
---|
[0899c82] | 186 | self.EnableEditing(True) |
---|
[1c86a37] | 187 | if self.GetNumberCols() > 0: |
---|
[76aed53] | 188 | self.default_col_width = self.GetColSize(0) |
---|
[49ad00b] | 189 | self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_left_click) |
---|
[24adb89] | 190 | self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click) |
---|
[49ad00b] | 191 | self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.on_selected_cell) |
---|
[656d65d] | 192 | self.Bind(wx.grid.EVT_GRID_CMD_CELL_CHANGE, self.on_edit_cell) |
---|
[0899c82] | 193 | self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, self.onContextMenu) |
---|
[76aed53] | 194 | |
---|
[656d65d] | 195 | def on_edit_cell(self, event): |
---|
| 196 | """ |
---|
| 197 | """ |
---|
[76aed53] | 198 | row, _ = event.GetRow(), event.GetCol() |
---|
[656d65d] | 199 | if row > self.max_row_touse: |
---|
| 200 | self.max_row_touse = row |
---|
[14e4804] | 201 | if self.data == None: |
---|
| 202 | self.data = {} |
---|
[656d65d] | 203 | event.Skip() |
---|
[76aed53] | 204 | |
---|
[49ad00b] | 205 | def on_selected_cell(self, event): |
---|
| 206 | """ |
---|
| 207 | Handler catching cell selection |
---|
| 208 | """ |
---|
| 209 | flag = event.CmdDown() or event.ControlDown() |
---|
[76aed53] | 210 | flag_shift = event.ShiftDown() |
---|
[904830e] | 211 | row, col = event.GetRow(), event.GetCol() |
---|
| 212 | cell = (row, col) |
---|
[63dc6e5] | 213 | event.Skip() |
---|
[4e0dfe4] | 214 | if not flag and not flag_shift: |
---|
[08dc9e87] | 215 | self.selected_cols = [] |
---|
| 216 | self.selected_rows = [] |
---|
[49ad00b] | 217 | self.selected_cells = [] |
---|
[904830e] | 218 | self.axis_label = "" |
---|
[08dc9e87] | 219 | self.axis_value = [] |
---|
| 220 | self.plottable_list = [] |
---|
| 221 | self.plottable_cells = [] |
---|
| 222 | self.plottable_flag = False |
---|
| 223 | self.last_selected_col = col |
---|
| 224 | self.last_selected_row = row |
---|
[63dc6e5] | 225 | if col >= 0: |
---|
[3553ad2] | 226 | if flag: |
---|
| 227 | label_row = row |
---|
| 228 | else: |
---|
| 229 | label_row = 0 |
---|
[08dc9e87] | 230 | self.axis_label = self.GetCellValue(label_row, col) |
---|
| 231 | self.selected_cols.append(col) |
---|
[4e0dfe4] | 232 | if flag_shift: |
---|
| 233 | if not self.selected_rows: |
---|
| 234 | min_r = 1 |
---|
| 235 | else: |
---|
| 236 | min_r = min(self.selected_rows) |
---|
[76aed53] | 237 | for row_s in range(min_r, row + 1): |
---|
[4e0dfe4] | 238 | cel = (row_s, col) |
---|
| 239 | if cel not in self.selected_cells: |
---|
| 240 | if row > 0: |
---|
| 241 | self.selected_cells.append(cel) |
---|
[76aed53] | 242 | self.selected_rows.append(row) |
---|
| 243 | for row_s in self.selected_rows: |
---|
[4e0dfe4] | 244 | cel = (row_s, col) |
---|
| 245 | if row_s > row: |
---|
| 246 | try: |
---|
| 247 | self.selected_cells.remove(cel) |
---|
| 248 | except: |
---|
| 249 | pass |
---|
| 250 | try: |
---|
| 251 | self.selected_rows.remove(row_s) |
---|
| 252 | except: |
---|
| 253 | pass |
---|
| 254 | elif flag: |
---|
| 255 | if cell not in self.selected_cells: |
---|
[76aed53] | 256 | if row > 0: |
---|
[4e0dfe4] | 257 | self.selected_cells.append(cell) |
---|
| 258 | self.selected_rows.append(row) |
---|
| 259 | else: |
---|
| 260 | try: |
---|
| 261 | self.selected_cells.remove(cell) |
---|
| 262 | except: |
---|
| 263 | pass |
---|
| 264 | try: |
---|
| 265 | self.selected_rows.remove(row) |
---|
| 266 | except: |
---|
| 267 | pass |
---|
[49ad00b] | 268 | else: |
---|
[4e0dfe4] | 269 | self.selected_cells.append(cell) |
---|
| 270 | self.selected_rows.append(row) |
---|
[dadf255] | 271 | self.axis_value = [] |
---|
| 272 | for cell_row, cell_col in self.selected_cells: |
---|
| 273 | if cell_row > 0 and cell_row < self.max_row_touse: |
---|
| 274 | self.axis_value.append(self.GetCellValue(cell_row, cell_col)) |
---|
[86a9e6c] | 275 | |
---|
[49ad00b] | 276 | def on_left_click(self, event): |
---|
| 277 | """ |
---|
| 278 | Catch the left click on label mouse event |
---|
| 279 | """ |
---|
[647df0d1] | 280 | event.Skip() |
---|
[49ad00b] | 281 | flag = event.CmdDown() or event.ControlDown() |
---|
[76aed53] | 282 | |
---|
[49ad00b] | 283 | col = event.GetCol() |
---|
[63dc6e5] | 284 | row = event.GetRow() |
---|
[86a9e6c] | 285 | |
---|
[76aed53] | 286 | if not flag: |
---|
[dadf255] | 287 | self.selected_cols = [] |
---|
[75790dc] | 288 | self.selected_rows = [] |
---|
[dadf255] | 289 | self.selected_cells = [] |
---|
| 290 | self.axis_label = "" |
---|
[08dc9e87] | 291 | self.axis_value = [] |
---|
[63dc6e5] | 292 | self.plottable_list = [] |
---|
[75790dc] | 293 | self.plottable_cells = [] |
---|
[63dc6e5] | 294 | self.plottable_flag = False |
---|
[76aed53] | 295 | |
---|
[63dc6e5] | 296 | self.last_selected_col = col |
---|
| 297 | self.last_selected_row = row |
---|
[75790dc] | 298 | if row != -1 and row not in self.selected_rows: |
---|
[76aed53] | 299 | self.selected_rows.append(row) |
---|
| 300 | |
---|
[647df0d1] | 301 | if col != -1: |
---|
[76aed53] | 302 | for row in range(1, self.GetNumberRows() + 1): |
---|
[647df0d1] | 303 | cell = (row, col) |
---|
| 304 | if row > 0 and row < self.max_row_touse: |
---|
| 305 | if cell not in self.selected_cells: |
---|
| 306 | self.selected_cells.append(cell) |
---|
[08dc9e87] | 307 | else: |
---|
| 308 | if flag: |
---|
[76aed53] | 309 | self.selected_cells.remove(cell) |
---|
[647df0d1] | 310 | self.selected_cols.append(col) |
---|
| 311 | self.axis_value = [] |
---|
| 312 | for cell_row, cell_col in self.selected_cells: |
---|
[4e0dfe4] | 313 | val = self.GetCellValue(cell_row, cell_col) |
---|
| 314 | if not val: |
---|
| 315 | self.axis_value.append(self.GetCellValue(cell_row, cell_col)) |
---|
[647df0d1] | 316 | self.axis_label = self.GetCellValue(0, col) |
---|
[25b7bf9] | 317 | if not self.axis_label: |
---|
| 318 | self.axis_label = " " |
---|
[76aed53] | 319 | |
---|
[24adb89] | 320 | def on_right_click(self, event): |
---|
[9c8f3ad] | 321 | """ |
---|
| 322 | Catch the right click mouse |
---|
| 323 | """ |
---|
[24adb89] | 324 | col = event.GetCol() |
---|
[c151afc] | 325 | row = event.GetRow() |
---|
[9ccb7e1] | 326 | # Ignore the index column |
---|
[c151afc] | 327 | if col < 0 or row != -1: |
---|
[9ccb7e1] | 328 | return |
---|
[656d65d] | 329 | self.selected_cols = [] |
---|
| 330 | self.selected_cols.append(col) |
---|
[24adb89] | 331 | # Slicer plot popup menu |
---|
| 332 | slicerpop = wx.Menu() |
---|
[76aed53] | 333 | col_label_menu = wx.Menu() |
---|
| 334 | c_name = self.GetCellValue(0, col) |
---|
[1c86a37] | 335 | label = "Insert column before %s " % str(c_name) |
---|
[76aed53] | 336 | slicerpop.AppendSubMenu(col_label_menu, '&%s' % str(label), str(label)) |
---|
[71fa9028] | 337 | row = 0 |
---|
| 338 | label = self.GetCellValue(row, col) |
---|
| 339 | self.insert_col_menu(col_label_menu, label, self) |
---|
[76aed53] | 340 | |
---|
| 341 | col_after_menu = wx.Menu() |
---|
[b18cf3d] | 342 | label = "Insert column after %s " % str(c_name) |
---|
[76aed53] | 343 | slicerpop.AppendSubMenu(col_after_menu, '&%s' % str(label), str(label)) |
---|
[b18cf3d] | 344 | self.insert_after_col_menu(col_after_menu, label, self) |
---|
[76aed53] | 345 | |
---|
| 346 | wx_id = wx.NewId() |
---|
[656d65d] | 347 | hint = 'Remove selected column %s' |
---|
[76aed53] | 348 | slicerpop.Append(wx_id, '&Remove Column', hint) |
---|
| 349 | wx.EVT_MENU(self, wx_id, self.on_remove_column) |
---|
| 350 | |
---|
[656d65d] | 351 | pos = wx.GetMousePosition() |
---|
[24adb89] | 352 | pos = self.ScreenToClient(pos) |
---|
| 353 | self.PopupMenu(slicerpop, pos) |
---|
[647df0d1] | 354 | event.Skip() |
---|
[76aed53] | 355 | |
---|
[71fa9028] | 356 | def insert_col_menu(self, menu, label, window): |
---|
| 357 | """ |
---|
| 358 | """ |
---|
[5531a46] | 359 | if self.data is None: |
---|
| 360 | return |
---|
[71fa9028] | 361 | id = wx.NewId() |
---|
| 362 | title = "Empty" |
---|
| 363 | hint = 'Insert empty column before %s' % str(label) |
---|
| 364 | menu.Append(id, title, hint) |
---|
| 365 | wx.EVT_MENU(window, id, self.on_insert_column) |
---|
[fb0de166] | 366 | row = 0 |
---|
[76aed53] | 367 | col_name = [self.GetCellValue(row, col) for col in range(self.GetNumberCols())] |
---|
[71fa9028] | 368 | for c_name in self.data.keys(): |
---|
[9696a10b] | 369 | if c_name not in col_name and self.data[c_name]: |
---|
[76aed53] | 370 | wx_id = wx.NewId() |
---|
[71fa9028] | 371 | hint = "Insert %s column before the " % str(c_name) |
---|
| 372 | hint += " %s column" % str(label) |
---|
[76aed53] | 373 | menu.Append(wx_id, '&%s' % str(c_name), hint) |
---|
| 374 | wx.EVT_MENU(window, wx_id, self.on_insert_column) |
---|
| 375 | |
---|
[b18cf3d] | 376 | def insert_after_col_menu(self, menu, label, window): |
---|
| 377 | """ |
---|
| 378 | """ |
---|
[5531a46] | 379 | if self.data is None: |
---|
| 380 | return |
---|
[76aed53] | 381 | wx_id = wx.NewId() |
---|
[b18cf3d] | 382 | title = "Empty" |
---|
| 383 | hint = 'Insert empty column after %s' % str(label) |
---|
[76aed53] | 384 | menu.Append(wx_id, title, hint) |
---|
| 385 | wx.EVT_MENU(window, wx_id, self.on_insert_after_column) |
---|
[b18cf3d] | 386 | row = 0 |
---|
[76aed53] | 387 | col_name = [self.GetCellValue(row, col) |
---|
[b18cf3d] | 388 | for col in range(self.GetNumberCols())] |
---|
| 389 | for c_name in self.data.keys(): |
---|
[9696a10b] | 390 | if c_name not in col_name and self.data[c_name]: |
---|
[76aed53] | 391 | wx_id = wx.NewId() |
---|
[b18cf3d] | 392 | hint = "Insert %s column after the " % str(c_name) |
---|
| 393 | hint += " %s column" % str(label) |
---|
[76aed53] | 394 | menu.Append(wx_id, '&%s' % str(c_name), hint) |
---|
| 395 | wx.EVT_MENU(window, wx_id, self.on_insert_after_column) |
---|
| 396 | |
---|
[71fa9028] | 397 | def on_remove_column(self, event=None): |
---|
[656d65d] | 398 | """ |
---|
| 399 | """ |
---|
| 400 | if self.selected_cols is not None or len(self.selected_cols) > 0: |
---|
| 401 | col = self.selected_cols[0] |
---|
[71fa9028] | 402 | self.remove_column(col=col, numCols=1) |
---|
[76aed53] | 403 | |
---|
[71fa9028] | 404 | def remove_column(self, col, numCols=1): |
---|
| 405 | """ |
---|
| 406 | Remove column to the current grid |
---|
| 407 | """ |
---|
| 408 | # add data to the grid |
---|
| 409 | row = 0 |
---|
| 410 | col_name = self.GetCellValue(row, col) |
---|
| 411 | self.data[col_name] = [] |
---|
| 412 | for row in range(1, self.GetNumberRows() + 1): |
---|
| 413 | if row < self.max_row_touse: |
---|
| 414 | value = self.GetCellValue(row, col) |
---|
| 415 | self.data[col_name].append(value) |
---|
[76aed53] | 416 | for k, value_list in self.data.iteritems(): |
---|
[71fa9028] | 417 | if k != col_name: |
---|
| 418 | length = len(value_list) |
---|
| 419 | if length < self.max_row_touse: |
---|
| 420 | diff = self.max_row_touse - length |
---|
| 421 | for i in range(diff): |
---|
| 422 | self.data[k].append("") |
---|
| 423 | self.DeleteCols(pos=col, numCols=numCols, updateLabels=True) |
---|
[76aed53] | 424 | |
---|
[656d65d] | 425 | def on_insert_column(self, event): |
---|
| 426 | """ |
---|
| 427 | """ |
---|
| 428 | if self.selected_cols is not None or len(self.selected_cols) > 0: |
---|
| 429 | col = self.selected_cols[0] |
---|
[76aed53] | 430 | # add data to the grid |
---|
| 431 | wx_id = event.GetId() |
---|
| 432 | col_name = event.GetEventObject().GetLabelText(wx_id) |
---|
[71fa9028] | 433 | self.insert_column(col=col, col_name=col_name) |
---|
[76aed53] | 434 | if not issubclass(event.GetEventObject().__class__, wx.Menu): |
---|
[71fa9028] | 435 | col += 1 |
---|
| 436 | self.selected_cols[0] += 1 |
---|
[76aed53] | 437 | |
---|
[b18cf3d] | 438 | def on_insert_after_column(self, event): |
---|
| 439 | """ |
---|
| 440 | Insert the given column after the highlighted column |
---|
| 441 | """ |
---|
| 442 | if self.selected_cols is not None or len(self.selected_cols) > 0: |
---|
| 443 | col = self.selected_cols[0] + 1 |
---|
[76aed53] | 444 | # add data to the grid |
---|
| 445 | wx_id = event.GetId() |
---|
| 446 | col_name = event.GetEventObject().GetLabelText(wx_id) |
---|
[b18cf3d] | 447 | self.insert_column(col=col, col_name=col_name) |
---|
[76aed53] | 448 | if not issubclass(event.GetEventObject().__class__, wx.Menu): |
---|
[b18cf3d] | 449 | self.selected_cols[0] += 1 |
---|
[76aed53] | 450 | |
---|
[71fa9028] | 451 | def insert_column(self, col, col_name): |
---|
| 452 | """ |
---|
[76aed53] | 453 | """ |
---|
[71fa9028] | 454 | row = 0 |
---|
| 455 | self.InsertCols(pos=col, numCols=1, updateLabels=True) |
---|
| 456 | if col_name.strip() != "Empty": |
---|
| 457 | self.SetCellValue(row, col, str(col_name.strip())) |
---|
| 458 | if col_name in self.data.keys(): |
---|
| 459 | value_list = self.data[col_name] |
---|
[76aed53] | 460 | cell_row = 1 |
---|
[71fa9028] | 461 | for value in value_list: |
---|
[6dad639] | 462 | label = value#format_number(value, high=True) |
---|
[b18cf3d] | 463 | self.SetCellValue(cell_row, col, str(label)) |
---|
[71fa9028] | 464 | cell_row += 1 |
---|
[b18cf3d] | 465 | self.AutoSizeColumn(col, True) |
---|
| 466 | width = self.GetColSize(col) |
---|
| 467 | if width < self.default_col_width: |
---|
[76aed53] | 468 | self.SetColSize(col, self.default_col_width) |
---|
[86a9e6c] | 469 | color = self.parent.GetBackgroundColour() |
---|
| 470 | self.SetCellBackgroundColour(0, col, color) |
---|
[b18cf3d] | 471 | self.ForceRefresh() |
---|
[76aed53] | 472 | |
---|
[24adb89] | 473 | def on_set_x_axis(self, event): |
---|
[9c8f3ad] | 474 | """ |
---|
| 475 | """ |
---|
[24adb89] | 476 | self.panel.set_xaxis(x=self.axis_value, label=self.axis_label) |
---|
[76aed53] | 477 | |
---|
[24adb89] | 478 | def on_set_y_axis(self, event): |
---|
[9c8f3ad] | 479 | """ |
---|
| 480 | """ |
---|
[76aed53] | 481 | self.panel.set_yaxis(y=self.axis_value, label=self.axis_label) |
---|
| 482 | |
---|
[71fa9028] | 483 | def set_data(self, data_inputs, data_outputs, details, file_name): |
---|
[24adb89] | 484 | """ |
---|
[9c8f3ad] | 485 | Add data to the grid |
---|
[dadf255] | 486 | :param data_inputs: data to use from the context menu of the grid |
---|
| 487 | :param data_ouputs: default columns deplayed |
---|
[24adb89] | 488 | """ |
---|
[71fa9028] | 489 | self.file_name = file_name |
---|
| 490 | self.details = details |
---|
[76aed53] | 491 | |
---|
[8523a1f2] | 492 | if data_outputs is None: |
---|
| 493 | data_outputs = {} |
---|
| 494 | self.data_outputs = data_outputs |
---|
[71fa9028] | 495 | if data_inputs is None: |
---|
[8523a1f2] | 496 | data_inputs = {} |
---|
| 497 | self.data_inputs = data_inputs |
---|
[656d65d] | 498 | self.data = {} |
---|
| 499 | for item in (self.data_outputs, self.data_inputs): |
---|
| 500 | self.data.update(item) |
---|
[76aed53] | 501 | |
---|
[8523a1f2] | 502 | if len(self.data_outputs) > 0: |
---|
[9c8f3ad] | 503 | self._cols = self.GetNumberCols() |
---|
| 504 | self._rows = self.GetNumberRows() |
---|
[8523a1f2] | 505 | self.col_names = self.data_outputs.keys() |
---|
[76aed53] | 506 | self.col_names.sort() |
---|
[9c8f3ad] | 507 | nbr_user_cols = len(self.col_names) |
---|
| 508 | #Add more columns to the grid if necessary |
---|
| 509 | if nbr_user_cols > self._cols: |
---|
[76aed53] | 510 | new_col_nbr = nbr_user_cols - self._cols + 1 |
---|
[9c8f3ad] | 511 | self.AppendCols(new_col_nbr, True) |
---|
[76aed53] | 512 | #Add more rows to the grid if necessary |
---|
[ed2d86e] | 513 | nbr_user_row = len(self.data_outputs.values()[0]) |
---|
[9c8f3ad] | 514 | if nbr_user_row > self._rows + 1: |
---|
[76aed53] | 515 | new_row_nbr = nbr_user_row - self._rows + 1 |
---|
[9c8f3ad] | 516 | self.AppendRows(new_row_nbr, True) |
---|
[76aed53] | 517 | # add data to the grid |
---|
[ed2d86e] | 518 | wx.CallAfter(self.set_grid_values) |
---|
[1c86a37] | 519 | self.ForceRefresh() |
---|
[76aed53] | 520 | |
---|
[ed2d86e] | 521 | def set_grid_values(self): |
---|
| 522 | """ |
---|
| 523 | Set the values in grids |
---|
| 524 | """ |
---|
[76aed53] | 525 | # add data to the grid |
---|
[ed2d86e] | 526 | row = 0 |
---|
| 527 | col = 0 |
---|
| 528 | cell_col = 0 |
---|
| 529 | for col_name in self.col_names: |
---|
| 530 | # use the first row of the grid to add user defined labels |
---|
| 531 | self.SetCellValue(row, col, str(col_name)) |
---|
| 532 | col += 1 |
---|
[76aed53] | 533 | cell_row = 1 |
---|
[ed2d86e] | 534 | value_list = self.data_outputs[col_name] |
---|
[76aed53] | 535 | |
---|
[ed2d86e] | 536 | for value in value_list: |
---|
| 537 | label = value |
---|
| 538 | if issubclass(value.__class__, BatchCell): |
---|
| 539 | label = value.label |
---|
| 540 | try: |
---|
| 541 | float(label) |
---|
| 542 | label = str(label)#format_number(label, high=True) |
---|
| 543 | except: |
---|
| 544 | label = str(label) |
---|
| 545 | self.SetCellValue(cell_row, cell_col, label) |
---|
| 546 | self.AutoSizeColumn(cell_col, True) |
---|
| 547 | width = self.GetColSize(cell_col) |
---|
| 548 | if width < self.default_col_width: |
---|
[76aed53] | 549 | self.SetColSize(cell_col, self.default_col_width) |
---|
| 550 | |
---|
[ed2d86e] | 551 | cell_row += 1 |
---|
| 552 | cell_col += 1 |
---|
| 553 | if cell_row > self.max_row_touse: |
---|
| 554 | self.max_row_touse = cell_row |
---|
[76aed53] | 555 | |
---|
[71fa9028] | 556 | def get_grid_view(self): |
---|
| 557 | """ |
---|
| 558 | Return value contained in the grid |
---|
| 559 | """ |
---|
| 560 | grid_view = {} |
---|
| 561 | for col in xrange(self.GetNumberCols()): |
---|
[76aed53] | 562 | label = self.GetCellValue(row=0, col=col) |
---|
[71fa9028] | 563 | label = label.strip() |
---|
| 564 | if label != "": |
---|
| 565 | grid_view[label] = [] |
---|
[c27a111] | 566 | for row in range(1, self.max_row_touse): |
---|
[71fa9028] | 567 | value = self.GetCellValue(row=row, col=col) |
---|
| 568 | if value != "": |
---|
[76aed53] | 569 | grid_view[label].append(value) |
---|
[71fa9028] | 570 | else: |
---|
[76aed53] | 571 | grid_view[label].append(None) |
---|
[71fa9028] | 572 | return grid_view |
---|
[76aed53] | 573 | |
---|
[86a9e6c] | 574 | def get_nofrows(self): |
---|
| 575 | """ |
---|
| 576 | Return number of total rows |
---|
| 577 | """ |
---|
| 578 | return self._rows |
---|
[76aed53] | 579 | |
---|
[0899c82] | 580 | def onContextMenu(self, event): |
---|
| 581 | """ |
---|
[76aed53] | 582 | Default context menu |
---|
[0899c82] | 583 | """ |
---|
[76aed53] | 584 | wx_id = wx.NewId() |
---|
[0899c82] | 585 | c_menu = wx.Menu() |
---|
[76aed53] | 586 | copy_menu = c_menu.Append(wx_id, '&Copy', 'Copy the selected cells') |
---|
| 587 | wx.EVT_MENU(self, wx_id, self.on_copy) |
---|
| 588 | |
---|
| 589 | wx_id = wx.NewId() |
---|
| 590 | c_menu.Append(wx_id, '&Paste', 'Paste the selected cells') |
---|
| 591 | wx.EVT_MENU(self, wx_id, self.on_paste) |
---|
| 592 | |
---|
| 593 | wx_id = wx.NewId() |
---|
| 594 | clear_menu = c_menu.Append(wx_id, '&Clear', 'Clear the selected cells') |
---|
| 595 | wx.EVT_MENU(self, wx_id, self.on_clear) |
---|
| 596 | |
---|
[0899c82] | 597 | # enable from flag |
---|
| 598 | has_selection = False |
---|
| 599 | selected_cel = self.selected_cells |
---|
| 600 | if len(selected_cel) > 0: |
---|
| 601 | _row, _col = selected_cel[0] |
---|
| 602 | has_selection = self.IsInSelection(_row, _col) |
---|
[14e4804] | 603 | if len(self.selected_cols) > 0: |
---|
| 604 | has_selection = True |
---|
| 605 | if len(self.selected_rows) > 0: |
---|
| 606 | has_selection = True |
---|
[0899c82] | 607 | copy_menu.Enable(has_selection) |
---|
[14e4804] | 608 | clear_menu.Enable(has_selection) |
---|
[0899c82] | 609 | try: |
---|
| 610 | # mouse event pos |
---|
| 611 | pos_evt = event.GetPosition() |
---|
| 612 | self.PopupMenu(c_menu, pos_evt) |
---|
| 613 | except: |
---|
| 614 | return |
---|
[76aed53] | 615 | |
---|
[0899c82] | 616 | def on_copy(self, event): |
---|
| 617 | """ |
---|
| 618 | On copy event from the contextmenu |
---|
| 619 | """ |
---|
| 620 | self.Copy() |
---|
| 621 | |
---|
| 622 | def on_paste(self, event): |
---|
| 623 | """ |
---|
| 624 | On paste event from the contextmenu |
---|
| 625 | """ |
---|
[14e4804] | 626 | if self.data == None: |
---|
| 627 | self.data = {} |
---|
| 628 | if self.file_name == None: |
---|
| 629 | self.file_name = 'copied_data' |
---|
[0899c82] | 630 | self.Paste() |
---|
[76aed53] | 631 | |
---|
[14e4804] | 632 | def on_clear(self, event): |
---|
| 633 | """ |
---|
| 634 | Clear the cells selected |
---|
| 635 | """ |
---|
| 636 | self.Clear() |
---|
[76aed53] | 637 | |
---|
[24adb89] | 638 | class Notebook(nb, PanelBase): |
---|
| 639 | """ |
---|
| 640 | ## Internal name for the AUI manager |
---|
| 641 | window_name = "Fit panel" |
---|
| 642 | ## Title to appear on top of the window |
---|
| 643 | """ |
---|
| 644 | window_caption = "Notebook " |
---|
[76aed53] | 645 | |
---|
[24adb89] | 646 | def __init__(self, parent, manager=None, data=None, *args, **kwargs): |
---|
| 647 | """ |
---|
| 648 | """ |
---|
| 649 | nb.__init__(self, parent, -1, |
---|
[76aed53] | 650 | style=wx.aui.AUI_NB_WINDOWLIST_BUTTON | |
---|
| 651 | wx.aui.AUI_BUTTON_DOWN | |
---|
| 652 | wx.aui.AUI_NB_DEFAULT_STYLE | |
---|
[9c8f3ad] | 653 | wx.CLIP_CHILDREN) |
---|
[24adb89] | 654 | PanelBase.__init__(self, parent) |
---|
[86a9e6c] | 655 | self.gpage_num = 1 |
---|
[9c8f3ad] | 656 | self.enable_close_button() |
---|
[24adb89] | 657 | self.parent = parent |
---|
| 658 | self.manager = manager |
---|
| 659 | self.data = data |
---|
[d03a356] | 660 | #add empty page |
---|
| 661 | self.add_empty_page() |
---|
[86a9e6c] | 662 | self.pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE |
---|
[76aed53] | 663 | self.Bind(self.pageClosedEvent, self.on_close_page) |
---|
| 664 | |
---|
[d03a356] | 665 | def add_empty_page(self): |
---|
| 666 | """ |
---|
| 667 | """ |
---|
| 668 | grid = GridPage(self, panel=self.parent) |
---|
| 669 | self.AddPage(grid, "", True) |
---|
| 670 | pos = self.GetPageIndex(grid) |
---|
[0899c82] | 671 | title = "Table" + str(self.gpage_num) |
---|
[d03a356] | 672 | self.SetPageText(pos, title) |
---|
| 673 | self.SetSelection(pos) |
---|
[86a9e6c] | 674 | self.enable_close_button() |
---|
| 675 | self.gpage_num += 1 |
---|
[76aed53] | 676 | return grid, pos |
---|
| 677 | |
---|
[9c8f3ad] | 678 | def enable_close_button(self): |
---|
| 679 | """ |
---|
[76aed53] | 680 | display the close button on tab for more than 1 tabs else remove the |
---|
[9c8f3ad] | 681 | close button |
---|
| 682 | """ |
---|
| 683 | if self.GetPageCount() <= 1: |
---|
[76aed53] | 684 | style = self.GetWindowStyleFlag() |
---|
[9c8f3ad] | 685 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 686 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == flag: |
---|
| 687 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 688 | self.SetWindowStyle(style) |
---|
| 689 | else: |
---|
| 690 | style = self.GetWindowStyleFlag() |
---|
| 691 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 692 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != flag: |
---|
| 693 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 694 | self.SetWindowStyle(style) |
---|
[76aed53] | 695 | |
---|
[9680906d] | 696 | def on_edit_axis(self): |
---|
| 697 | """ |
---|
[08dc9e87] | 698 | Return the select cell of a given selected column. Check that all cells |
---|
| 699 | are from the same column |
---|
[9680906d] | 700 | """ |
---|
| 701 | pos = self.GetSelection() |
---|
| 702 | grid = self.GetPage(pos) |
---|
[4e0dfe4] | 703 | #grid.selected_cols = [grid.GetSelectedRows()]# |
---|
[08dc9e87] | 704 | if len(grid.selected_cols) >= 1: |
---|
[49ad00b] | 705 | col = grid.selected_cols[0] |
---|
[08dc9e87] | 706 | for c in grid.selected_cols: |
---|
| 707 | if c != col: |
---|
[7ad194fa] | 708 | msg = "Edit axis doesn't understand this selection.\n" |
---|
[08dc9e87] | 709 | msg += "Please select only one column" |
---|
[7ad194fa] | 710 | raise ValueError, msg |
---|
[76aed53] | 711 | for (_, cell_col) in grid.selected_cells: |
---|
[08dc9e87] | 712 | if cell_col != col: |
---|
| 713 | msg = "Cannot use cells from different columns for " |
---|
| 714 | msg += "this operation.\n" |
---|
| 715 | msg += "Please select elements of the same col.\n" |
---|
| 716 | raise ValueError, msg |
---|
[76aed53] | 717 | |
---|
[86a9e6c] | 718 | # Finally check the highlighted cell if any cells missing |
---|
[8d0ec40] | 719 | self.get_highlighted_row(True) |
---|
[08dc9e87] | 720 | else: |
---|
| 721 | msg = "No item selected.\n" |
---|
| 722 | msg += "Please select only one column or one cell" |
---|
| 723 | raise ValueError, msg |
---|
[dadf255] | 724 | return grid.selected_cells |
---|
[76aed53] | 725 | |
---|
[8d0ec40] | 726 | def get_highlighted_row(self, is_number=True): |
---|
[86a9e6c] | 727 | """ |
---|
| 728 | Add highlight rows |
---|
| 729 | """ |
---|
| 730 | pos = self.GetSelection() |
---|
| 731 | grid = self.GetPage(pos) |
---|
| 732 | col = grid.selected_cols[0] |
---|
| 733 | # Finally check the highlighted cell if any cells missing |
---|
| 734 | for row in range(grid.get_nofrows()): |
---|
| 735 | if grid.IsInSelection(row, col): |
---|
| 736 | cel = (row, col) |
---|
[8d0ec40] | 737 | if row < 1 and not is_number: |
---|
[86a9e6c] | 738 | continue |
---|
[8d0ec40] | 739 | # empty cell |
---|
| 740 | if not grid.GetCellValue(row, col).lstrip().rstrip(): |
---|
| 741 | if cel in grid.selected_cells: |
---|
| 742 | grid.selected_cells.remove(cel) |
---|
[86a9e6c] | 743 | continue |
---|
[8d0ec40] | 744 | if is_number: |
---|
[76aed53] | 745 | try: |
---|
[8d0ec40] | 746 | float(grid.GetCellValue(row, col)) |
---|
| 747 | except: |
---|
| 748 | # non numeric cell |
---|
| 749 | if cel in grid.selected_cells: |
---|
| 750 | grid.selected_cells.remove(cel) |
---|
| 751 | continue |
---|
[86a9e6c] | 752 | if cel not in grid.selected_cells: |
---|
| 753 | grid.selected_cells.append(cel) |
---|
[76aed53] | 754 | |
---|
[904830e] | 755 | def get_column_labels(self): |
---|
| 756 | """ |
---|
| 757 | return dictionary of columns labels of the current page |
---|
| 758 | """ |
---|
| 759 | pos = self.GetSelection() |
---|
| 760 | grid = self.GetPage(pos) |
---|
| 761 | labels = {} |
---|
| 762 | for col in range(grid.GetNumberCols()): |
---|
[25b7bf9] | 763 | label = grid.GetColLabelValue(int(col)) |
---|
[76aed53] | 764 | if label.strip() != "": |
---|
[904830e] | 765 | labels[label.strip()] = col |
---|
| 766 | return labels |
---|
[76aed53] | 767 | |
---|
[7ad194fa] | 768 | def create_axis_label(self, cell_list): |
---|
| 769 | """ |
---|
[76aed53] | 770 | Receive a list of cells and create a string presenting the selected |
---|
| 771 | cells. |
---|
[7ad194fa] | 772 | :param cell_list: list of tuple |
---|
[76aed53] | 773 | |
---|
[7ad194fa] | 774 | """ |
---|
| 775 | pos = self.GetSelection() |
---|
| 776 | grid = self.GetPage(pos) |
---|
| 777 | label = "" |
---|
| 778 | col_name = "" |
---|
[76aed53] | 779 | def create_label(col_name, row_min=None, row_max=None): |
---|
[dadf255] | 780 | """ |
---|
| 781 | """ |
---|
[4e0dfe4] | 782 | result = " " |
---|
[dadf255] | 783 | if row_min is not None or row_max is not None: |
---|
| 784 | if row_min is None: |
---|
| 785 | result = str(row_max) + "]" |
---|
| 786 | elif row_max is None: |
---|
[76aed53] | 787 | result = str(col_name) + "[" + str(row_min) + ":" |
---|
[dadf255] | 788 | else: |
---|
[76aed53] | 789 | result = str(col_name) + "[" + str(row_min) + ":" |
---|
[08dc9e87] | 790 | result += str(row_max) + "]" |
---|
| 791 | return str(result) |
---|
[76aed53] | 792 | |
---|
[7ad194fa] | 793 | if len(cell_list) > 0: |
---|
[dadf255] | 794 | if len(cell_list) == 1: |
---|
[76aed53] | 795 | row_min, col = cell_list[0] |
---|
| 796 | col_name = grid.GetColLabelValue(int(col)) |
---|
| 797 | |
---|
[86a9e6c] | 798 | col_title = grid.GetCellValue(0, col) |
---|
[76aed53] | 799 | label = create_label(col_name, row_min + 1, row_min + 1) |
---|
| 800 | return label, col_title |
---|
[dadf255] | 801 | else: |
---|
| 802 | temp_list = copy.deepcopy(cell_list) |
---|
| 803 | temp_list.sort() |
---|
| 804 | length = len(temp_list) |
---|
[76aed53] | 805 | row_min, col = temp_list[0] |
---|
| 806 | row_max, _ = temp_list[length - 1] |
---|
[86a9e6c] | 807 | col_name = grid.GetColLabelValue(int(col)) |
---|
[25b7bf9] | 808 | col_title = grid.GetCellValue(0, col) |
---|
[86a9e6c] | 809 | |
---|
[dadf255] | 810 | index = 0 |
---|
[86a9e6c] | 811 | for row in xrange(row_min, row_max + 1): |
---|
[dadf255] | 812 | if index > 0 and index < len(temp_list): |
---|
| 813 | new_row, _ = temp_list[index] |
---|
| 814 | if row != new_row: |
---|
| 815 | temp_list.insert(index, (None, None)) |
---|
[76aed53] | 816 | if index - 1 >= 0: |
---|
| 817 | new_row, _ = temp_list[index - 1] |
---|
| 818 | if not new_row == None and new_row != ' ': |
---|
| 819 | label += create_label(col_name, None, |
---|
| 820 | int(new_row) + 1) |
---|
[4e0dfe4] | 821 | else: |
---|
| 822 | label += "]" |
---|
[dadf255] | 823 | label += "," |
---|
| 824 | if index + 1 < len(temp_list): |
---|
| 825 | new_row, _ = temp_list[index + 1] |
---|
[76aed53] | 826 | if not new_row == None: |
---|
| 827 | label += create_label(col_name, |
---|
| 828 | int(new_row) + 1, None) |
---|
[4e0dfe4] | 829 | if row_min != None and row_max != None: |
---|
| 830 | if index == 0: |
---|
[76aed53] | 831 | label += create_label(col_name, |
---|
| 832 | int(row_min) + 1, None) |
---|
| 833 | elif index == len(temp_list) - 1: |
---|
| 834 | label += create_label(col_name, None, |
---|
| 835 | int(row_max) + 1) |
---|
[dadf255] | 836 | index += 1 |
---|
[4e0dfe4] | 837 | # clean up the list |
---|
| 838 | label_out = '' |
---|
| 839 | for item in label.split(','): |
---|
[86a9e6c] | 840 | if item.split(":")[1] == "]": |
---|
[4e0dfe4] | 841 | continue |
---|
| 842 | else: |
---|
| 843 | label_out += item + "," |
---|
| 844 | |
---|
| 845 | return label_out, col_title |
---|
[76aed53] | 846 | |
---|
[9c8f3ad] | 847 | def on_close_page(self, event): |
---|
| 848 | """ |
---|
| 849 | close the page |
---|
| 850 | """ |
---|
| 851 | if self.GetPageCount() == 1: |
---|
| 852 | event.Veto() |
---|
[86a9e6c] | 853 | wx.CallAfter(self.enable_close_button) |
---|
[76aed53] | 854 | |
---|
[71fa9028] | 855 | def set_data(self, data_inputs, data_outputs, details="", file_name=None): |
---|
[8523a1f2] | 856 | if data_outputs is None or data_outputs == {}: |
---|
[24adb89] | 857 | return |
---|
[98fdccd] | 858 | inputs, outputs = self.get_odered_results(data_inputs, data_outputs) |
---|
[f4b37d1] | 859 | for pos in range(self.GetPageCount()): |
---|
| 860 | grid = self.GetPage(pos) |
---|
| 861 | if grid.data is None: |
---|
| 862 | #Found empty page |
---|
[76aed53] | 863 | grid.set_data(data_inputs=inputs, |
---|
[98fdccd] | 864 | data_outputs=outputs, |
---|
[71fa9028] | 865 | details=details, |
---|
[76aed53] | 866 | file_name=file_name) |
---|
| 867 | self.SetSelection(pos) |
---|
[f4b37d1] | 868 | return |
---|
[76aed53] | 869 | |
---|
[f4b37d1] | 870 | grid, pos = self.add_empty_page() |
---|
[76aed53] | 871 | grid.set_data(data_inputs=inputs, |
---|
[98fdccd] | 872 | data_outputs=outputs, |
---|
[a84ca2a] | 873 | file_name=file_name, |
---|
| 874 | details=details) |
---|
[76aed53] | 875 | |
---|
[98fdccd] | 876 | def get_odered_results(self, inputs, outputs=None): |
---|
| 877 | """ |
---|
| 878 | Get ordered the results |
---|
| 879 | """ |
---|
| 880 | # Let's re-order the data from the keys in 'Data' name. |
---|
| 881 | if outputs == None: |
---|
| 882 | return |
---|
[e25d908] | 883 | try: |
---|
| 884 | # For outputs from batch |
---|
| 885 | to_be_sort = [str(item.label) for item in outputs['Data']] |
---|
| 886 | except: |
---|
| 887 | # When inputs are from an external file |
---|
| 888 | return inputs, outputs |
---|
[98fdccd] | 889 | inds = numpy.lexsort((to_be_sort, to_be_sort)) |
---|
| 890 | for key in outputs.keys(): |
---|
| 891 | key_list = outputs[key] |
---|
| 892 | temp_key = [item for item in key_list] |
---|
| 893 | for ind in inds: |
---|
| 894 | temp_key[ind] = key_list[inds[ind]] |
---|
| 895 | outputs[key] = temp_key |
---|
| 896 | for key in inputs.keys(): |
---|
| 897 | key_list = inputs[key] |
---|
[4550a5a] | 898 | if len(key_list) == len(inds): |
---|
[98fdccd] | 899 | temp_key = [item for item in key_list] |
---|
| 900 | for ind in inds: |
---|
[4550a5a] | 901 | temp_key[ind] = key_list[inds[ind]] |
---|
[98fdccd] | 902 | inputs[key] = temp_key |
---|
[4550a5a] | 903 | else: |
---|
| 904 | inputs[key] = [] |
---|
[76aed53] | 905 | |
---|
[98fdccd] | 906 | return inputs, outputs |
---|
[76aed53] | 907 | |
---|
[9c8f3ad] | 908 | def add_column(self): |
---|
| 909 | """ |
---|
| 910 | Append a new column to the grid |
---|
| 911 | """ |
---|
| 912 | pos = self.GetSelection() |
---|
| 913 | grid = self.GetPage(pos) |
---|
| 914 | grid.AppendCols(1, True) |
---|
[76aed53] | 915 | |
---|
[dbb3914] | 916 | def on_remove_column(self): |
---|
| 917 | """ |
---|
| 918 | Remove the selected column from the grid |
---|
| 919 | """ |
---|
| 920 | pos = self.GetSelection() |
---|
| 921 | grid = self.GetPage(pos) |
---|
| 922 | grid.on_remove_column(event=None) |
---|
[76aed53] | 923 | |
---|
[24adb89] | 924 | class GridPanel(SPanel): |
---|
[8523a1f2] | 925 | def __init__(self, parent, data_inputs=None, |
---|
| 926 | data_outputs=None, *args, **kwds): |
---|
[76aed53] | 927 | SPanel.__init__(self, parent, *args, **kwds) |
---|
| 928 | |
---|
[24adb89] | 929 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[76aed53] | 930 | |
---|
[9680906d] | 931 | self.plotting_sizer = wx.FlexGridSizer(3, 7, 10, 5) |
---|
[75790dc] | 932 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[24adb89] | 933 | self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 934 | self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0), |
---|
| 935 | (wx.StaticLine(self, -1), 0, wx.EXPAND, 0), |
---|
[75790dc] | 936 | (self.plotting_sizer), |
---|
[3e1177d] | 937 | (self.button_sizer, 0, wx.BOTTOM, 10)]) |
---|
[24adb89] | 938 | self.parent = parent |
---|
[8523a1f2] | 939 | self._data_inputs = data_inputs |
---|
| 940 | self._data_outputs = data_outputs |
---|
[24adb89] | 941 | self.x = [] |
---|
[76aed53] | 942 | self.y = [] |
---|
| 943 | self.dy = [] |
---|
[24adb89] | 944 | self.x_axis_label = None |
---|
| 945 | self.y_axis_label = None |
---|
[9130227] | 946 | self.dy_axis_label = None |
---|
[904830e] | 947 | self.x_axis_title = None |
---|
| 948 | self.y_axis_title = None |
---|
[24adb89] | 949 | self.x_axis_unit = None |
---|
| 950 | self.y_axis_unit = None |
---|
[75790dc] | 951 | self.view_button = None |
---|
[24adb89] | 952 | self.plot_button = None |
---|
[904830e] | 953 | self.notebook = None |
---|
[86a9e6c] | 954 | self.plot_num = 1 |
---|
[76aed53] | 955 | |
---|
[24adb89] | 956 | self.layout_grid() |
---|
| 957 | self.layout_plotting_area() |
---|
| 958 | self.SetSizer(self.vbox) |
---|
[98fdccd] | 959 | |
---|
[904830e] | 960 | def set_xaxis(self, label="", x=None): |
---|
| 961 | """ |
---|
| 962 | """ |
---|
[24adb89] | 963 | if x is None: |
---|
| 964 | x = [] |
---|
| 965 | self.x = x |
---|
[904830e] | 966 | self.x_axis_label.SetValue("%s[:]" % str(label)) |
---|
| 967 | self.x_axis_title.SetValue(str(label)) |
---|
[76aed53] | 968 | |
---|
[904830e] | 969 | def set_yaxis(self, label="", y=None): |
---|
| 970 | """ |
---|
| 971 | """ |
---|
[24adb89] | 972 | if y is None: |
---|
| 973 | y = [] |
---|
| 974 | self.y = y |
---|
[904830e] | 975 | self.y_axis_label.SetValue("%s[:]" % str(label)) |
---|
| 976 | self.y_axis_title.SetValue(str(label)) |
---|
[76aed53] | 977 | |
---|
[9130227] | 978 | def set_dyaxis(self, label="", dy=None): |
---|
| 979 | """ |
---|
| 980 | """ |
---|
| 981 | if dy is None: |
---|
| 982 | dy = [] |
---|
| 983 | self.dy = dy |
---|
| 984 | self.dy_axis_label.SetValue("%s[:]" % str(label)) |
---|
[76aed53] | 985 | |
---|
[904830e] | 986 | def get_plot_axis(self, col, list): |
---|
| 987 | """ |
---|
[76aed53] | 988 | |
---|
[904830e] | 989 | """ |
---|
| 990 | axis = [] |
---|
| 991 | pos = self.notebook.GetSelection() |
---|
| 992 | grid = self.notebook.GetPage(pos) |
---|
| 993 | for row in list: |
---|
[c911f34] | 994 | label = grid.GetCellValue(0, col) |
---|
| 995 | value = grid.GetCellValue(row - 1, col).strip() |
---|
| 996 | if value != "": |
---|
| 997 | if label.lower().strip() == "data": |
---|
| 998 | axis.append(float(row - 1)) |
---|
| 999 | else: |
---|
[dadf255] | 1000 | try: |
---|
| 1001 | axis.append(float(value)) |
---|
| 1002 | except: |
---|
[76aed53] | 1003 | msg = "Invalid data in row %s column %s" % (str(row), str(col)) |
---|
| 1004 | wx.PostEvent(self.parent.parent, |
---|
| 1005 | StatusEvent(status=msg, info="error")) |
---|
[0899c82] | 1006 | return None |
---|
[c911f34] | 1007 | else: |
---|
[76aed53] | 1008 | axis.append(None) |
---|
[904830e] | 1009 | return axis |
---|
[76aed53] | 1010 | |
---|
[75790dc] | 1011 | def on_view(self, event): |
---|
| 1012 | """ |
---|
| 1013 | Get object represented buy the given cell and plot them. |
---|
| 1014 | """ |
---|
| 1015 | pos = self.notebook.GetSelection() |
---|
| 1016 | grid = self.notebook.GetPage(pos) |
---|
| 1017 | title = self.notebook.GetPageText(pos) |
---|
[8d0ec40] | 1018 | self.notebook.get_highlighted_row(False) |
---|
[9400de6] | 1019 | if len(grid.selected_cells) == 0: |
---|
| 1020 | msg = "Highlight a Data or Chi2 column first..." |
---|
[76aed53] | 1021 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[9400de6] | 1022 | return |
---|
[23155ba] | 1023 | elif len(grid.selected_cells) > 20: |
---|
| 1024 | msg = "Too many data (> 20) to plot..." |
---|
| 1025 | msg += "\n Please select no more than 20 data." |
---|
[76aed53] | 1026 | wx.MessageDialog(self, msg, 'Plotting', wx.OK) |
---|
| 1027 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[23155ba] | 1028 | return |
---|
| 1029 | |
---|
[75790dc] | 1030 | for cell in grid.selected_cells: |
---|
| 1031 | row, col = cell |
---|
| 1032 | label_row = 0 |
---|
| 1033 | label = grid.GetCellValue(label_row, col) |
---|
| 1034 | if label in grid.data: |
---|
| 1035 | values = grid.data[label] |
---|
[344c5d8] | 1036 | if row > len(values) or row < 1: |
---|
[76aed53] | 1037 | msg = "Invalid cell was chosen." |
---|
| 1038 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[9400de6] | 1039 | continue |
---|
[344c5d8] | 1040 | else: |
---|
[76aed53] | 1041 | value = values[row - 1] |
---|
[75790dc] | 1042 | if issubclass(value.__class__, BatchCell): |
---|
[fe98127] | 1043 | if value.object is None or len(value.object) == 0: |
---|
| 1044 | msg = "Row %s , " % str(row) |
---|
[c27a111] | 1045 | msg += "Column %s is NOT " % str(label) |
---|
| 1046 | msg += "the results of fits to view..." |
---|
[76aed53] | 1047 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[fe98127] | 1048 | return |
---|
[75790dc] | 1049 | for new_plot in value.object: |
---|
[fe98127] | 1050 | if new_plot is None or \ |
---|
[76aed53] | 1051 | not issubclass(new_plot.__class__, |
---|
[fe98127] | 1052 | plottables.Plottable): |
---|
[75790dc] | 1053 | msg = "Row %s , " % str(row) |
---|
[c27a111] | 1054 | msg += "Column %s is NOT " % str(label) |
---|
| 1055 | msg += "the results of fits to view..." |
---|
[76aed53] | 1056 | wx.PostEvent(self.parent.parent, |
---|
| 1057 | StatusEvent(status=msg, info="error")) |
---|
[d560a37] | 1058 | return |
---|
[75790dc] | 1059 | if issubclass(new_plot.__class__, Data1D): |
---|
[23477c6] | 1060 | if label in grid.list_plot_panels.keys(): |
---|
| 1061 | group_id = grid.list_plot_panels[label] |
---|
[fd51a7c] | 1062 | else: |
---|
[5d192cd] | 1063 | group_id = str(new_plot.group_id) + str(grid.uid) |
---|
[23477c6] | 1064 | grid.list_plot_panels[label] = group_id |
---|
[fd51a7c] | 1065 | if group_id not in new_plot.list_group_id: |
---|
| 1066 | new_plot.group_id = group_id |
---|
| 1067 | new_plot.list_group_id.append(group_id) |
---|
[75790dc] | 1068 | else: |
---|
[18a6556] | 1069 | if label.lower() in ["data", "chi2"]: |
---|
[75790dc] | 1070 | if len(grid.selected_cells) != 1: |
---|
[b18cf3d] | 1071 | msg = "2D View: Please select one data set" |
---|
[4e0dfe4] | 1072 | msg += " at a time for View Fit Results." |
---|
[76aed53] | 1073 | wx.PostEvent(self.parent.parent, |
---|
| 1074 | StatusEvent(status=msg, info="error")) |
---|
[d560a37] | 1075 | return |
---|
[86a9e6c] | 1076 | |
---|
[76aed53] | 1077 | wx.PostEvent(self.parent.parent, |
---|
| 1078 | NewPlotEvent(plot=new_plot, |
---|
| 1079 | group_id=str(new_plot.group_id), |
---|
| 1080 | title=title)) |
---|
[4e0dfe4] | 1081 | msg = "Plotting the View Fit Results completed!" |
---|
[76aed53] | 1082 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
[75790dc] | 1083 | else: |
---|
| 1084 | msg = "Row %s , " % str(row) |
---|
[c27a111] | 1085 | msg += "Column %s is NOT " % str(label) |
---|
| 1086 | msg += "the results of fits to view..." |
---|
[76aed53] | 1087 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[d560a37] | 1088 | return |
---|
[76aed53] | 1089 | |
---|
[24adb89] | 1090 | def on_plot(self, event): |
---|
| 1091 | """ |
---|
[904830e] | 1092 | Evaluate the contains of textcrtl and plot result |
---|
[76aed53] | 1093 | """ |
---|
[904830e] | 1094 | pos = self.notebook.GetSelection() |
---|
| 1095 | grid = self.notebook.GetPage(pos) |
---|
| 1096 | column_names = {} |
---|
| 1097 | if grid is not None: |
---|
| 1098 | column_names = self.notebook.get_column_labels() |
---|
[08dc9e87] | 1099 | #evaluate x |
---|
[904830e] | 1100 | sentence = self.x_axis_label.GetValue() |
---|
[1c86a37] | 1101 | try: |
---|
| 1102 | if sentence.strip() == "": |
---|
[08dc9e87] | 1103 | msg = "Select column values for x axis" |
---|
[1c86a37] | 1104 | raise ValueError, msg |
---|
| 1105 | except: |
---|
[86a9e6c] | 1106 | msg = "X axis value error." |
---|
[76aed53] | 1107 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[86a9e6c] | 1108 | return |
---|
[904830e] | 1109 | dict = parse_string(sentence, column_names.keys()) |
---|
[76aed53] | 1110 | |
---|
[9130227] | 1111 | try: |
---|
[86a9e6c] | 1112 | sentence = self.get_sentence(dict, sentence, column_names) |
---|
[9130227] | 1113 | x = eval(sentence) |
---|
| 1114 | except: |
---|
| 1115 | msg = "Need a proper x-range." |
---|
[76aed53] | 1116 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[9130227] | 1117 | return |
---|
[904830e] | 1118 | #evaluate y |
---|
| 1119 | sentence = self.y_axis_label.GetValue() |
---|
[86a9e6c] | 1120 | try: |
---|
| 1121 | if sentence.strip() == "": |
---|
| 1122 | msg = "select value for y axis" |
---|
| 1123 | raise ValueError, msg |
---|
| 1124 | except: |
---|
| 1125 | msg = "Y axis value error." |
---|
[76aed53] | 1126 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[86a9e6c] | 1127 | return |
---|
[904830e] | 1128 | dict = parse_string(sentence, column_names.keys()) |
---|
[9130227] | 1129 | try: |
---|
[86a9e6c] | 1130 | sentence = self.get_sentence(dict, sentence, column_names) |
---|
[9130227] | 1131 | y = eval(sentence) |
---|
| 1132 | except: |
---|
| 1133 | msg = "Need a proper y-range." |
---|
[76aed53] | 1134 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[9130227] | 1135 | return |
---|
| 1136 | #evaluate y |
---|
| 1137 | sentence = self.dy_axis_label.GetValue() |
---|
| 1138 | dy = None |
---|
| 1139 | if sentence.strip() != "": |
---|
| 1140 | dict = parse_string(sentence, column_names.keys()) |
---|
| 1141 | sentence = self.get_sentence(dict, sentence, column_names) |
---|
| 1142 | try: |
---|
| 1143 | dy = eval(sentence) |
---|
| 1144 | except: |
---|
| 1145 | msg = "Need a proper dy-range." |
---|
[76aed53] | 1146 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[9130227] | 1147 | return |
---|
| 1148 | if len(x) != len(y) or (len(x) == 0 or len(y) == 0): |
---|
[08dc9e87] | 1149 | msg = "Need same length for X and Y axis and both greater than 0" |
---|
| 1150 | msg += " to plot.\n" |
---|
[76aed53] | 1151 | msg += "Got X length = %s, Y length = %s" % (str(len(x)), str(len(y))) |
---|
| 1152 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[08dc9e87] | 1153 | return |
---|
[76aed53] | 1154 | if dy != None and (len(y) != len(dy)): |
---|
[9130227] | 1155 | msg = "Need same length for Y and dY axis and both greater than 0" |
---|
| 1156 | msg += " to plot.\n" |
---|
[76aed53] | 1157 | msg += "Got Y length = %s, dY length = %s" % (str(len(y)), str(len(dy))) |
---|
| 1158 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
[9130227] | 1159 | return |
---|
[c2e5898] | 1160 | if dy == None: |
---|
| 1161 | dy = numpy.zeros(len(y)) |
---|
[904830e] | 1162 | #plotting |
---|
[9130227] | 1163 | new_plot = Data1D(x=x, y=y, dy=dy) |
---|
[76aed53] | 1164 | new_plot.id = wx.NewId() |
---|
[c2e5898] | 1165 | new_plot.is_data = False |
---|
[24adb89] | 1166 | new_plot.group_id = wx.NewId() |
---|
[d560a37] | 1167 | y_title = self.y_axis_title.GetValue() |
---|
| 1168 | x_title = self.x_axis_title.GetValue() |
---|
[76aed53] | 1169 | title = "%s_vs_%s" % (y_title, x_title) |
---|
| 1170 | new_plot.xaxis(x_title, self.x_axis_unit.GetValue()) |
---|
| 1171 | new_plot.yaxis(y_title, self.y_axis_unit.GetValue()) |
---|
[904830e] | 1172 | try: |
---|
[d560a37] | 1173 | title = y_title.strip() |
---|
[86a9e6c] | 1174 | title += "_" + self.notebook.GetPageText(pos) |
---|
| 1175 | title += "_" + str(self.plot_num) |
---|
| 1176 | self.plot_num += 1 |
---|
[dadf255] | 1177 | new_plot.name = title |
---|
[1c86a37] | 1178 | new_plot.xtransform = "x" |
---|
[76aed53] | 1179 | new_plot.ytransform = "y" |
---|
| 1180 | wx.PostEvent(self.parent.parent, |
---|
| 1181 | NewPlotEvent(plot=new_plot, |
---|
| 1182 | group_id=str(new_plot.group_id), title=title)) |
---|
[c27a111] | 1183 | msg = "Plotting completed!" |
---|
[76aed53] | 1184 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
| 1185 | self.parent.parent.update_theory(data_id=new_plot.id, theory=new_plot) |
---|
[904830e] | 1186 | except: |
---|
[76aed53] | 1187 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
| 1188 | |
---|
[56e99f9] | 1189 | def on_help(self, event): |
---|
| 1190 | """ |
---|
| 1191 | Bring up the Batch Grid Panel Usage Documentation whenever |
---|
| 1192 | the HELP button is clicked. |
---|
| 1193 | |
---|
| 1194 | Calls DocumentationWindow with the path of the location within the |
---|
| 1195 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 1196 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 1197 | installers, the help comes up at the top level of the file as |
---|
| 1198 | webbrowser does not pass anything past the # to the browser when it is |
---|
| 1199 | running "file:///...." |
---|
| 1200 | |
---|
| 1201 | :param evt: Triggers on clicking the help button |
---|
| 1202 | """ |
---|
| 1203 | #import documentation window here to avoid circular imports |
---|
| 1204 | #if put at top of file with rest of imports. |
---|
| 1205 | from documentation_window import DocumentationWindow |
---|
| 1206 | |
---|
| 1207 | _TreeLocation = "user/perspectives/fitting/fitting_help.html" |
---|
| 1208 | _PageAnchor = "#batch-fit-mode" |
---|
| 1209 | _doc_viewer = DocumentationWindow(self, -1, _TreeLocation, _PageAnchor, |
---|
| 1210 | "Batch Mode Help") |
---|
| 1211 | |
---|
[9130227] | 1212 | def get_sentence(self, dict, sentence, column_names): |
---|
| 1213 | """ |
---|
| 1214 | Get sentence from dict |
---|
| 1215 | """ |
---|
| 1216 | for tok, (col_name, list) in dict.iteritems(): |
---|
| 1217 | col = column_names[col_name] |
---|
| 1218 | axis = self.get_plot_axis(col, list) |
---|
[0899c82] | 1219 | if axis == None: |
---|
| 1220 | return None |
---|
[76aed53] | 1221 | sentence = sentence.replace(tok, "numpy.array(%s)" % str(axis)) |
---|
[9130227] | 1222 | for key, value in FUNC_DICT.iteritems(): |
---|
| 1223 | sentence = sentence.replace(key.lower(), value) |
---|
| 1224 | return sentence |
---|
[76aed53] | 1225 | |
---|
[24adb89] | 1226 | def layout_grid(self): |
---|
| 1227 | """ |
---|
| 1228 | Draw the area related to the grid |
---|
| 1229 | """ |
---|
[904830e] | 1230 | self.notebook = Notebook(parent=self) |
---|
[8523a1f2] | 1231 | self.notebook.set_data(self._data_inputs, self._data_outputs) |
---|
[904830e] | 1232 | self.grid_sizer.Add(self.notebook, 1, wx.EXPAND, 0) |
---|
[76aed53] | 1233 | |
---|
[24adb89] | 1234 | def layout_plotting_area(self): |
---|
| 1235 | """ |
---|
| 1236 | Draw area containing options to plot |
---|
| 1237 | """ |
---|
[3e1177d] | 1238 | view_description = wx.StaticBox(self, -1, 'Plot Fits/Residuals') |
---|
| 1239 | note = "To plot the fits (or residuals), click the 'View Fits' button" |
---|
| 1240 | note += "\n after highlighting the Data names (or Chi2 values)." |
---|
| 1241 | note_text = wx.StaticText(self, -1, note) |
---|
| 1242 | boxsizer1 = wx.StaticBoxSizer(view_description, wx.HORIZONTAL) |
---|
[904830e] | 1243 | self.x_axis_title = wx.TextCtrl(self, -1) |
---|
| 1244 | self.y_axis_title = wx.TextCtrl(self, -1) |
---|
| 1245 | self.x_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
| 1246 | self.y_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
[9130227] | 1247 | self.dy_axis_label = wx.TextCtrl(self, -1, size=(200, -1)) |
---|
[9680906d] | 1248 | self.x_axis_add = wx.Button(self, -1, "Add") |
---|
[76aed53] | 1249 | self.x_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
| 1250 | id=self.x_axis_add.GetId()) |
---|
[9680906d] | 1251 | self.y_axis_add = wx.Button(self, -1, "Add") |
---|
[76aed53] | 1252 | self.y_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
| 1253 | id=self.y_axis_add.GetId()) |
---|
[9130227] | 1254 | self.dy_axis_add = wx.Button(self, -1, "Add") |
---|
[76aed53] | 1255 | self.dy_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, |
---|
| 1256 | id=self.dy_axis_add.GetId()) |
---|
[24adb89] | 1257 | self.x_axis_unit = wx.TextCtrl(self, -1) |
---|
| 1258 | self.y_axis_unit = wx.TextCtrl(self, -1) |
---|
[4e0dfe4] | 1259 | self.view_button = wx.Button(self, -1, "View Fits") |
---|
[7d47789] | 1260 | view_tip = "Highlight the data set or the Chi2 column first." |
---|
[b18cf3d] | 1261 | self.view_button.SetToolTipString(view_tip) |
---|
[75790dc] | 1262 | wx.EVT_BUTTON(self, self.view_button.GetId(), self.on_view) |
---|
[24adb89] | 1263 | self.plot_button = wx.Button(self, -1, "Plot") |
---|
[7d47789] | 1264 | plot_tip = "Highlight a column for each axis and \n" |
---|
| 1265 | plot_tip += "click the Add buttons first." |
---|
[76aed53] | 1266 | |
---|
[b18cf3d] | 1267 | self.plot_button.SetToolTipString(plot_tip) |
---|
[56e99f9] | 1268 | |
---|
| 1269 | self.help_button = wx.Button(self, -1, "HELP") |
---|
| 1270 | self.help_button.SetToolTipString("Get Help for Batch Mode") |
---|
| 1271 | self.help_button.Bind(wx.EVT_BUTTON, self.on_help) |
---|
| 1272 | |
---|
[3e1177d] | 1273 | boxsizer1.AddMany([(note_text, 0, wx.LEFT, 10), |
---|
[76aed53] | 1274 | (self.view_button, 0, wx.LEFT | wx.RIGHT, 10)]) |
---|
| 1275 | self.button_sizer.AddMany([(boxsizer1, 0, |
---|
| 1276 | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10), |
---|
| 1277 | (self.plot_button, 0, |
---|
[56e99f9] | 1278 | wx.LEFT | wx.TOP | wx.BOTTOM, 12), |
---|
| 1279 | (self.help_button,0, |
---|
| 1280 | wx.LEFT | wx.TOP | wx.BOTTOM, 12)]) |
---|
[76aed53] | 1281 | |
---|
[24adb89] | 1282 | wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot) |
---|
[76aed53] | 1283 | self.plotting_sizer.AddMany(\ |
---|
| 1284 | [(wx.StaticText(self, -1, "X-axis Label\nSelection Range"), 1, |
---|
| 1285 | wx.TOP | wx.BOTTOM | wx.LEFT, 10), |
---|
| 1286 | (self.x_axis_label, 1, wx.TOP | wx.BOTTOM, 10), |
---|
| 1287 | (self.x_axis_add, 1, wx.TOP | wx.BOTTOM | wx.RIGHT, 10), |
---|
| 1288 | (wx.StaticText(self, -1, "X-axis Label"), 1, wx.TOP | wx.BOTTOM | wx.LEFT, 10), |
---|
| 1289 | (self.x_axis_title, 1, wx.TOP | wx.BOTTOM, 10), |
---|
| 1290 | (wx.StaticText(self, -1, "X-axis Unit"), 1, wx.TOP | wx.BOTTOM, 10), |
---|
| 1291 | (self.x_axis_unit, 1, wx.TOP | wx.BOTTOM, 10), |
---|
| 1292 | (wx.StaticText(self, -1, "Y-axis Label\nSelection Range"), 1, |
---|
| 1293 | wx.BOTTOM | wx.LEFT, 10), |
---|
[904830e] | 1294 | (self.y_axis_label, wx.BOTTOM, 10), |
---|
[76aed53] | 1295 | (self.y_axis_add, 1, wx.BOTTOM | wx.RIGHT, 10), |
---|
| 1296 | (wx.StaticText(self, -1, "Y-axis Label"), 1, |
---|
| 1297 | wx.BOTTOM | wx.LEFT, 10), |
---|
| 1298 | (self.y_axis_title, wx.BOTTOM, 10), |
---|
| 1299 | (wx.StaticText(self, -1, "Y-axis Unit"), 1, wx.BOTTOM, 10), |
---|
[904830e] | 1300 | (self.y_axis_unit, 1, wx.BOTTOM, 10), |
---|
[76aed53] | 1301 | (wx.StaticText(self, -1, "dY-Bar (Optional)\nSelection Range"), |
---|
| 1302 | 1, wx.BOTTOM | wx.LEFT, 10), |
---|
[9130227] | 1303 | (self.dy_axis_label, wx.BOTTOM, 10), |
---|
[76aed53] | 1304 | (self.dy_axis_add, 1, wx.BOTTOM | wx.RIGHT, 10), |
---|
| 1305 | (-1, -1), |
---|
| 1306 | (-1, -1), |
---|
| 1307 | (-1, -1), |
---|
| 1308 | (-1, 1)]) |
---|
| 1309 | |
---|
[9680906d] | 1310 | def on_edit_axis(self, event): |
---|
| 1311 | """ |
---|
| 1312 | Get the selected column on the visible grid and set values for axis |
---|
| 1313 | """ |
---|
[08dc9e87] | 1314 | try: |
---|
| 1315 | cell_list = self.notebook.on_edit_axis() |
---|
[8d0ec40] | 1316 | label, title = self.create_axis_label(cell_list) |
---|
[08dc9e87] | 1317 | except: |
---|
| 1318 | msg = str(sys.exc_value) |
---|
[76aed53] | 1319 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info="error")) |
---|
| 1320 | return |
---|
[904830e] | 1321 | tcrtl = event.GetEventObject() |
---|
| 1322 | if tcrtl == self.x_axis_add: |
---|
[76aed53] | 1323 | self.edit_axis_helper(self.x_axis_label, self.x_axis_title, label, title) |
---|
[904830e] | 1324 | elif tcrtl == self.y_axis_add: |
---|
[76aed53] | 1325 | self.edit_axis_helper(self.y_axis_label, self.y_axis_title, label, title) |
---|
[9130227] | 1326 | elif tcrtl == self.dy_axis_add: |
---|
[76aed53] | 1327 | self.edit_axis_helper(self.dy_axis_label, None, label, None) |
---|
| 1328 | |
---|
[7ad194fa] | 1329 | def create_axis_label(self, cell_list): |
---|
| 1330 | """ |
---|
[76aed53] | 1331 | Receive a list of cells and create a string presenting the selected |
---|
| 1332 | cells. |
---|
[7ad194fa] | 1333 | :param cell_list: list of tuple |
---|
[76aed53] | 1334 | |
---|
[7ad194fa] | 1335 | """ |
---|
[904830e] | 1336 | if self.notebook is not None: |
---|
| 1337 | return self.notebook.create_axis_label(cell_list) |
---|
[76aed53] | 1338 | |
---|
[904830e] | 1339 | def edit_axis_helper(self, tcrtl_label, tcrtl_title, label, title): |
---|
[9680906d] | 1340 | """ |
---|
[904830e] | 1341 | get controls to modify |
---|
[9680906d] | 1342 | """ |
---|
[9130227] | 1343 | if label != None: |
---|
| 1344 | tcrtl_label.SetValue(str(label)) |
---|
| 1345 | if title != None: |
---|
| 1346 | tcrtl_title.SetValue(str(title)) |
---|
[76aed53] | 1347 | |
---|
[24adb89] | 1348 | def add_column(self): |
---|
[9c8f3ad] | 1349 | """ |
---|
| 1350 | """ |
---|
[904830e] | 1351 | if self.notebook is not None: |
---|
| 1352 | self.notebook.add_column() |
---|
[76aed53] | 1353 | |
---|
[9c8f3ad] | 1354 | def on_remove_column(self): |
---|
| 1355 | """ |
---|
| 1356 | """ |
---|
[904830e] | 1357 | if self.notebook is not None: |
---|
| 1358 | self.notebook.on_remove_column() |
---|
[76aed53] | 1359 | |
---|
| 1360 | |
---|
[24adb89] | 1361 | class GridFrame(wx.Frame): |
---|
[76aed53] | 1362 | def __init__(self, parent=None, data_inputs=None, data_outputs=None, id=-1, |
---|
[d560a37] | 1363 | title="Grid Window", size=(800, 500)): |
---|
[24adb89] | 1364 | wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size) |
---|
| 1365 | self.parent = parent |
---|
[8523a1f2] | 1366 | self.panel = GridPanel(self, data_inputs, data_outputs) |
---|
[24adb89] | 1367 | menubar = wx.MenuBar() |
---|
| 1368 | self.SetMenuBar(menubar) |
---|
[76aed53] | 1369 | |
---|
[71fa9028] | 1370 | self.curr_col = None |
---|
| 1371 | self.curr_grid = None |
---|
| 1372 | self.curr_col_name = "" |
---|
[14e4804] | 1373 | self.file = wx.Menu() |
---|
| 1374 | menubar.Append(self.file, "&File") |
---|
[76aed53] | 1375 | |
---|
[71fa9028] | 1376 | hint = "Open file containing batch results" |
---|
[14e4804] | 1377 | open_menu = self.file.Append(wx.NewId(), 'Open ', hint) |
---|
[71fa9028] | 1378 | wx.EVT_MENU(self, open_menu.GetId(), self.on_open) |
---|
[76aed53] | 1379 | |
---|
[71fa9028] | 1380 | hint = "Open the the current grid into excel" |
---|
[14e4804] | 1381 | self.open_excel_menu = self.file.Append(wx.NewId(), 'Open with Excel', hint) |
---|
| 1382 | wx.EVT_MENU(self, self.open_excel_menu.GetId(), self.open_with_excel) |
---|
| 1383 | self.file.AppendSeparator() |
---|
| 1384 | self.save_menu = self.file.Append(wx.NewId(), 'Save As', 'Save into File') |
---|
| 1385 | wx.EVT_MENU(self, self.save_menu.GetId(), self.on_save_page) |
---|
[76aed53] | 1386 | |
---|
[71fa9028] | 1387 | self.edit = wx.Menu() |
---|
[76aed53] | 1388 | |
---|
| 1389 | add_table_menu = self.edit.Append(-1, 'New Table', |
---|
[14e4804] | 1390 | 'Add a New Table') |
---|
| 1391 | self.edit.AppendSeparator() |
---|
| 1392 | wx.EVT_MENU(self, add_table_menu.GetId(), self.add_table) |
---|
[76aed53] | 1393 | |
---|
| 1394 | self.copy_menu = self.edit.Append(-1, 'Copy', |
---|
[14e4804] | 1395 | 'Copy the selected cells') |
---|
[0899c82] | 1396 | wx.EVT_MENU(self, self.copy_menu.GetId(), self.on_copy) |
---|
[76aed53] | 1397 | self.paste_menu = self.edit.Append(-1, 'Paste', |
---|
[14e4804] | 1398 | 'Paste the selected Cells') |
---|
[0899c82] | 1399 | wx.EVT_MENU(self, self.paste_menu.GetId(), self.on_paste) |
---|
[76aed53] | 1400 | self.clear_menu = self.edit.Append(-1, 'Clear', |
---|
[95b513c] | 1401 | 'Clear the selected Cells') |
---|
| 1402 | wx.EVT_MENU(self, self.clear_menu.GetId(), self.on_clear) |
---|
| 1403 | |
---|
[14e4804] | 1404 | self.edit.AppendSeparator() |
---|
[71fa9028] | 1405 | hint = "Insert column before the selected column" |
---|
| 1406 | self.insert_before_menu = wx.Menu() |
---|
[76aed53] | 1407 | self.insertb_sub_menu = self.edit.AppendSubMenu(self.insert_before_menu, |
---|
| 1408 | 'Insert Before', hint) |
---|
[0899c82] | 1409 | hint = "Insert column after the selected column" |
---|
| 1410 | self.insert_after_menu = wx.Menu() |
---|
[76aed53] | 1411 | self.inserta_sub_menu = self.edit.AppendSubMenu(self.insert_after_menu, |
---|
| 1412 | 'Insert After', hint) |
---|
[71fa9028] | 1413 | hint = "Remove the selected column" |
---|
| 1414 | self.remove_menu = self.edit.Append(-1, 'Remove Column', hint) |
---|
| 1415 | wx.EVT_MENU(self, self.remove_menu.GetId(), self.on_remove_column) |
---|
[76aed53] | 1416 | |
---|
[71fa9028] | 1417 | self.Bind(wx.EVT_MENU_OPEN, self.on_menu_open) |
---|
| 1418 | menubar.Append(self.edit, "&Edit") |
---|
[cb26857] | 1419 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
[76aed53] | 1420 | |
---|
[0899c82] | 1421 | def on_copy(self, event): |
---|
| 1422 | """ |
---|
| 1423 | On Copy |
---|
| 1424 | """ |
---|
[14e4804] | 1425 | if event != None: |
---|
| 1426 | event.Skip() |
---|
[0899c82] | 1427 | pos = self.panel.notebook.GetSelection() |
---|
| 1428 | grid = self.panel.notebook.GetPage(pos) |
---|
| 1429 | grid.Copy() |
---|
[76aed53] | 1430 | |
---|
[0899c82] | 1431 | def on_paste(self, event): |
---|
| 1432 | """ |
---|
| 1433 | On Paste |
---|
| 1434 | """ |
---|
[14e4804] | 1435 | if event != None: |
---|
| 1436 | event.Skip() |
---|
[0899c82] | 1437 | pos = self.panel.notebook.GetSelection() |
---|
| 1438 | grid = self.panel.notebook.GetPage(pos) |
---|
[14e4804] | 1439 | grid.on_paste(None) |
---|
[95b513c] | 1440 | |
---|
| 1441 | def on_clear(self, event): |
---|
| 1442 | """ |
---|
| 1443 | On Clear |
---|
| 1444 | """ |
---|
| 1445 | pos = self.panel.notebook.GetSelection() |
---|
| 1446 | grid = self.panel.notebook.GetPage(pos) |
---|
| 1447 | grid.Clear() |
---|
[76aed53] | 1448 | |
---|
[71fa9028] | 1449 | def GetLabelText(self, id): |
---|
[656d65d] | 1450 | """ |
---|
[0899c82] | 1451 | Get Label Text |
---|
[656d65d] | 1452 | """ |
---|
[71fa9028] | 1453 | for item in self.insert_before_menu.GetMenuItems(): |
---|
[76aed53] | 1454 | m_id = item.GetId() |
---|
[71fa9028] | 1455 | if m_id == id: |
---|
[76aed53] | 1456 | return item.GetLabel() |
---|
| 1457 | |
---|
[71fa9028] | 1458 | def on_remove_column(self, event): |
---|
[cb26857] | 1459 | """ |
---|
[0899c82] | 1460 | On remove column |
---|
[cb26857] | 1461 | """ |
---|
[71fa9028] | 1462 | pos = self.panel.notebook.GetSelection() |
---|
| 1463 | grid = self.panel.notebook.GetPage(pos) |
---|
| 1464 | grid.on_remove_column(event=None) |
---|
[76aed53] | 1465 | |
---|
[71fa9028] | 1466 | def on_menu_open(self, event): |
---|
[9c8f3ad] | 1467 | """ |
---|
[0899c82] | 1468 | On menu open |
---|
[9c8f3ad] | 1469 | """ |
---|
[14e4804] | 1470 | if self.file == event.GetMenu(): |
---|
| 1471 | pos = self.panel.notebook.GetSelection() |
---|
| 1472 | grid = self.panel.notebook.GetPage(pos) |
---|
| 1473 | has_data = (grid.data != None and grid.data != {}) |
---|
[76aed53] | 1474 | self.open_excel_menu.Enable(has_data) |
---|
| 1475 | self.save_menu.Enable(has_data) |
---|
| 1476 | |
---|
[71fa9028] | 1477 | if self.edit == event.GetMenu(): |
---|
| 1478 | #get the selected column |
---|
| 1479 | pos = self.panel.notebook.GetSelection() |
---|
| 1480 | grid = self.panel.notebook.GetPage(pos) |
---|
| 1481 | col_list = grid.GetSelectedCols() |
---|
[0899c82] | 1482 | has_selection = False |
---|
| 1483 | selected_cel = grid.selected_cells |
---|
| 1484 | if len(selected_cel) > 0: |
---|
| 1485 | _row, _col = selected_cel[0] |
---|
| 1486 | has_selection = grid.IsInSelection(_row, _col) |
---|
[14e4804] | 1487 | if len(grid.selected_cols) > 0: |
---|
| 1488 | has_selection = True |
---|
| 1489 | if len(grid.selected_rows) > 0: |
---|
| 1490 | has_selection = True |
---|
[0899c82] | 1491 | self.copy_menu.Enable(has_selection) |
---|
[95b513c] | 1492 | self.clear_menu.Enable(has_selection) |
---|
[76aed53] | 1493 | |
---|
[71fa9028] | 1494 | if len(col_list) > 0: |
---|
| 1495 | self.remove_menu.Enable(True) |
---|
| 1496 | else: |
---|
| 1497 | self.remove_menu.Enable(False) |
---|
[a5e749f] | 1498 | if len(col_list) == 0 or len(col_list) > 1: |
---|
[0899c82] | 1499 | self.insertb_sub_menu.Enable(False) |
---|
| 1500 | self.inserta_sub_menu.Enable(False) |
---|
[71fa9028] | 1501 | label = "Insert Column Before" |
---|
[0899c82] | 1502 | self.insertb_sub_menu.SetText(label) |
---|
| 1503 | label = "Insert Column After" |
---|
| 1504 | self.inserta_sub_menu.SetText(label) |
---|
[71fa9028] | 1505 | else: |
---|
[0899c82] | 1506 | self.insertb_sub_menu.Enable(True) |
---|
| 1507 | self.inserta_sub_menu.Enable(True) |
---|
[76aed53] | 1508 | |
---|
[71fa9028] | 1509 | col = col_list[0] |
---|
| 1510 | col_name = grid.GetCellValue(row=0, col=col) |
---|
| 1511 | label = "Insert Column Before " + str(col_name) |
---|
[0899c82] | 1512 | self.insertb_sub_menu.SetText(label) |
---|
[71fa9028] | 1513 | for item in self.insert_before_menu.GetMenuItems(): |
---|
| 1514 | self.insert_before_menu.DeleteItem(item) |
---|
[76aed53] | 1515 | grid.insert_col_menu(menu=self.insert_before_menu, |
---|
[71fa9028] | 1516 | label=col_name, window=self) |
---|
[0899c82] | 1517 | label = "Insert Column After " + str(col_name) |
---|
| 1518 | self.inserta_sub_menu.SetText(label) |
---|
| 1519 | for item in self.insert_after_menu.GetMenuItems(): |
---|
| 1520 | self.insert_after_menu.DeleteItem(item) |
---|
[76aed53] | 1521 | grid.insert_after_col_menu(menu=self.insert_after_menu, |
---|
| 1522 | label=col_name, window=self) |
---|
[71fa9028] | 1523 | event.Skip() |
---|
[76aed53] | 1524 | |
---|
| 1525 | |
---|
| 1526 | |
---|
[71fa9028] | 1527 | def on_save_page(self, event): |
---|
| 1528 | """ |
---|
| 1529 | """ |
---|
| 1530 | if self.parent is not None: |
---|
| 1531 | pos = self.panel.notebook.GetSelection() |
---|
| 1532 | grid = self.panel.notebook.GetPage(pos) |
---|
[7b48b08] | 1533 | if grid.file_name is None or grid.file_name.strip() == "" or \ |
---|
| 1534 | grid.data is None or len(grid.data) == 0: |
---|
| 1535 | name = self.panel.notebook.GetPageText(pos) |
---|
| 1536 | msg = " %s has not data to save" % str(name) |
---|
[76aed53] | 1537 | wx.PostEvent(self.parent, |
---|
| 1538 | StatusEvent(status=msg, info="error")) |
---|
| 1539 | |
---|
[7b48b08] | 1540 | return |
---|
[71fa9028] | 1541 | reader, ext = os.path.splitext(grid.file_name) |
---|
| 1542 | path = None |
---|
[76aed53] | 1543 | if self.parent is not None: |
---|
[71fa9028] | 1544 | location = os.path.dirname(grid.file_name) |
---|
| 1545 | dlg = wx.FileDialog(self, "Save Project file", |
---|
[76aed53] | 1546 | location, grid.file_name, ext, wx.SAVE) |
---|
[71fa9028] | 1547 | path = None |
---|
| 1548 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 1549 | path = dlg.GetPath() |
---|
| 1550 | dlg.Destroy() |
---|
| 1551 | if path != None: |
---|
| 1552 | if self.parent is not None: |
---|
| 1553 | data = grid.get_grid_view() |
---|
[76aed53] | 1554 | self.parent.write_batch_tofile(data=data, |
---|
| 1555 | file_name=path, |
---|
| 1556 | details=grid.details) |
---|
| 1557 | |
---|
[71fa9028] | 1558 | def on_open(self, event): |
---|
[656d65d] | 1559 | """ |
---|
[71fa9028] | 1560 | Open file containg batch result |
---|
[656d65d] | 1561 | """ |
---|
[71fa9028] | 1562 | if self.parent is not None: |
---|
[86a9e6c] | 1563 | self.parent.on_read_batch_tofile(self) |
---|
[76aed53] | 1564 | |
---|
[71fa9028] | 1565 | def open_with_excel(self, event): |
---|
| 1566 | """ |
---|
| 1567 | open excel and display batch result in Excel |
---|
| 1568 | """ |
---|
| 1569 | if self.parent is not None: |
---|
| 1570 | pos = self.panel.notebook.GetSelection() |
---|
| 1571 | grid = self.panel.notebook.GetPage(pos) |
---|
| 1572 | data = grid.get_grid_view() |
---|
[7b48b08] | 1573 | if grid.file_name is None or grid.file_name.strip() == "" or \ |
---|
| 1574 | grid.data is None or len(grid.data) == 0: |
---|
| 1575 | name = self.panel.notebook.GetPageText(pos) |
---|
| 1576 | msg = " %s has not data to open on excel" % str(name) |
---|
[76aed53] | 1577 | wx.PostEvent(self.parent, |
---|
| 1578 | StatusEvent(status=msg, info="error")) |
---|
| 1579 | |
---|
[7b48b08] | 1580 | return |
---|
[71fa9028] | 1581 | self.parent.open_with_externalapp(data=data, |
---|
[76aed53] | 1582 | file_name=grid.file_name, |
---|
[a5e749f] | 1583 | details=grid.details) |
---|
[76aed53] | 1584 | |
---|
[71fa9028] | 1585 | def on_close(self, event): |
---|
| 1586 | """ |
---|
| 1587 | """ |
---|
| 1588 | self.Hide() |
---|
[76aed53] | 1589 | |
---|
[656d65d] | 1590 | def on_append_column(self, event): |
---|
[cb26857] | 1591 | """ |
---|
[9c8f3ad] | 1592 | Append a new column to the grid |
---|
[cb26857] | 1593 | """ |
---|
[24adb89] | 1594 | self.panel.add_column() |
---|
[76aed53] | 1595 | |
---|
[71fa9028] | 1596 | def set_data(self, data_inputs, data_outputs, details="", file_name=None): |
---|
[cb26857] | 1597 | """ |
---|
[14e4804] | 1598 | Set data |
---|
[cb26857] | 1599 | """ |
---|
[76aed53] | 1600 | self.panel.notebook.set_data(data_inputs=data_inputs, |
---|
| 1601 | file_name=file_name, |
---|
| 1602 | details=details, |
---|
| 1603 | data_outputs=data_outputs) |
---|
[14e4804] | 1604 | |
---|
| 1605 | def add_table(self, event): |
---|
| 1606 | """ |
---|
| 1607 | Add a new table |
---|
| 1608 | """ |
---|
| 1609 | # DO not event.Skip(): it will make 2 pages |
---|
[76aed53] | 1610 | self.panel.notebook.add_empty_page() |
---|
| 1611 | |
---|
[83eb1b52] | 1612 | class BatchOutputFrame(wx.Frame): |
---|
[73197d0] | 1613 | """ |
---|
| 1614 | Allow to select where the result of batch will be displayed or stored |
---|
| 1615 | """ |
---|
[8523a1f2] | 1616 | def __init__(self, parent, data_inputs, data_outputs, file_name="", |
---|
[850525c] | 1617 | details="", *args, **kwds): |
---|
[73197d0] | 1618 | """ |
---|
| 1619 | :param parent: Window instantiating this dialog |
---|
[76aed53] | 1620 | :param result: result to display in a grid or export to an external |
---|
[73197d0] | 1621 | application. |
---|
| 1622 | """ |
---|
[76aed53] | 1623 | #kwds['style'] = wx.CAPTION|wx.SYSTEM_MENU |
---|
[83eb1b52] | 1624 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
[73197d0] | 1625 | self.parent = parent |
---|
[83eb1b52] | 1626 | self.panel = wx.Panel(self) |
---|
[850525c] | 1627 | self.file_name = file_name |
---|
| 1628 | self.details = details |
---|
[8523a1f2] | 1629 | self.data_inputs = data_inputs |
---|
| 1630 | self.data_outputs = data_outputs |
---|
| 1631 | self.data = {} |
---|
| 1632 | for item in (self.data_outputs, self.data_inputs): |
---|
| 1633 | self.data.update(item) |
---|
[73197d0] | 1634 | self.flag = 1 |
---|
| 1635 | self.SetSize((300, 200)) |
---|
| 1636 | self.local_app_selected = None |
---|
| 1637 | self.external_app_selected = None |
---|
| 1638 | self.save_to_file = None |
---|
| 1639 | self._do_layout() |
---|
[76aed53] | 1640 | |
---|
[73197d0] | 1641 | def _do_layout(self): |
---|
| 1642 | """ |
---|
| 1643 | Draw the content of the current dialog window |
---|
| 1644 | """ |
---|
| 1645 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[83eb1b52] | 1646 | box_description = wx.StaticBox(self.panel, -1, str("Batch Outputs")) |
---|
[73197d0] | 1647 | hint_sizer = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
[83eb1b52] | 1648 | selection_sizer = wx.GridBagSizer(5, 5) |
---|
[73197d0] | 1649 | button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[76aed53] | 1650 | text = "Open with %s" % self.parent.application_name |
---|
| 1651 | self.local_app_selected = wx.RadioButton(self.panel, -1, text, style=wx.RB_GROUP) |
---|
[73197d0] | 1652 | self.Bind(wx.EVT_RADIOBUTTON, self.onselect, |
---|
[76aed53] | 1653 | id=self.local_app_selected.GetId()) |
---|
[73197d0] | 1654 | text = "Open with Excel" |
---|
[76aed53] | 1655 | self.external_app_selected = wx.RadioButton(self.panel, -1, text) |
---|
| 1656 | self.Bind(wx.EVT_RADIOBUTTON, self.onselect, id=self.external_app_selected.GetId()) |
---|
[caf3a08f] | 1657 | text = "Save to File" |
---|
[83eb1b52] | 1658 | self.save_to_file = wx.CheckBox(self.panel, -1, text) |
---|
[76aed53] | 1659 | self.Bind(wx.EVT_CHECKBOX, self.onselect, id=self.save_to_file.GetId()) |
---|
[73197d0] | 1660 | self.local_app_selected.SetValue(True) |
---|
| 1661 | self.external_app_selected.SetValue(False) |
---|
| 1662 | self.save_to_file.SetValue(False) |
---|
[83eb1b52] | 1663 | button_close = wx.Button(self.panel, -1, "Close") |
---|
[76aed53] | 1664 | button_close.Bind(wx.EVT_BUTTON, id=button_close.GetId(), handler=self.on_close) |
---|
[83eb1b52] | 1665 | button_apply = wx.Button(self.panel, -1, "Apply") |
---|
[76aed53] | 1666 | button_apply.Bind(wx.EVT_BUTTON, id=button_apply.GetId(), handler=self.on_apply) |
---|
[83eb1b52] | 1667 | button_apply.SetFocus() |
---|
[73197d0] | 1668 | hint = "" |
---|
[83eb1b52] | 1669 | hint_sizer.Add(wx.StaticText(self.panel, -1, hint)) |
---|
[73197d0] | 1670 | hint_sizer.Add(selection_sizer) |
---|
| 1671 | #draw area containing radio buttons |
---|
| 1672 | ix = 0 |
---|
| 1673 | iy = 0 |
---|
| 1674 | selection_sizer.Add(self.local_app_selected, (iy, ix), |
---|
[76aed53] | 1675 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[73197d0] | 1676 | iy += 1 |
---|
| 1677 | selection_sizer.Add(self.external_app_selected, (iy, ix), |
---|
[76aed53] | 1678 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[73197d0] | 1679 | iy += 1 |
---|
| 1680 | selection_sizer.Add(self.save_to_file, (iy, ix), |
---|
[76aed53] | 1681 | (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[73197d0] | 1682 | #contruction the sizer contaning button |
---|
[76aed53] | 1683 | button_sizer.Add((20, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
[caf3a08f] | 1684 | |
---|
| 1685 | button_sizer.Add(button_close, 0, |
---|
[76aed53] | 1686 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
[83eb1b52] | 1687 | button_sizer.Add(button_apply, 0, |
---|
[76aed53] | 1688 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
| 1689 | vbox.Add(hint_sizer, 0, wx.EXPAND | wx.ALL, 10) |
---|
| 1690 | vbox.Add(wx.StaticLine(self.panel, -1), 0, wx.EXPAND, 0) |
---|
| 1691 | vbox.Add(button_sizer, 0, wx.TOP | wx.BOTTOM, 10) |
---|
[73197d0] | 1692 | self.SetSizer(vbox) |
---|
[76aed53] | 1693 | |
---|
[83eb1b52] | 1694 | def on_apply(self, event): |
---|
| 1695 | """ |
---|
| 1696 | Get the user selection and display output to the selected application |
---|
| 1697 | """ |
---|
| 1698 | if self.flag == 1: |
---|
[8523a1f2] | 1699 | self.parent.open_with_localapp(data_inputs=self.data_inputs, |
---|
[a5e749f] | 1700 | data_outputs=self.data_outputs) |
---|
[83eb1b52] | 1701 | elif self.flag == 2: |
---|
[76aed53] | 1702 | self.parent.open_with_externalapp(data=self.data, |
---|
[a5e749f] | 1703 | file_name=self.file_name, |
---|
| 1704 | details=self.details) |
---|
[83eb1b52] | 1705 | def on_close(self, event): |
---|
| 1706 | """ |
---|
| 1707 | close the Window |
---|
| 1708 | """ |
---|
| 1709 | self.Close() |
---|
[76aed53] | 1710 | |
---|
[73197d0] | 1711 | def onselect(self, event=None): |
---|
| 1712 | """ |
---|
| 1713 | Receive event and display data into third party application |
---|
| 1714 | or save data to file. |
---|
[76aed53] | 1715 | |
---|
[73197d0] | 1716 | """ |
---|
| 1717 | if self.save_to_file.GetValue(): |
---|
[76aed53] | 1718 | _, ext = os.path.splitext(self.file_name) |
---|
[850525c] | 1719 | path = None |
---|
| 1720 | location = os.getcwd() |
---|
[76aed53] | 1721 | if self.parent is not None: |
---|
[83eb1b52] | 1722 | location = os.path.dirname(self.file_name) |
---|
[850525c] | 1723 | dlg = wx.FileDialog(self, "Save Project file", |
---|
[76aed53] | 1724 | location, self.file_name, ext, wx.SAVE) |
---|
[850525c] | 1725 | path = None |
---|
| 1726 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 1727 | path = dlg.GetPath() |
---|
| 1728 | dlg.Destroy() |
---|
| 1729 | if path != None: |
---|
| 1730 | if self.parent is not None and self.data is not None: |
---|
[76aed53] | 1731 | self.parent.write_batch_tofile(data=self.data, |
---|
[a5e749f] | 1732 | file_name=path, |
---|
| 1733 | details=self.details) |
---|
[850525c] | 1734 | if self.local_app_selected.GetValue(): |
---|
[73197d0] | 1735 | self.flag = 1 |
---|
| 1736 | else: |
---|
| 1737 | self.flag = 2 |
---|
| 1738 | return self.flag |
---|
[76aed53] | 1739 | |
---|
| 1740 | |
---|
| 1741 | |
---|
[24adb89] | 1742 | if __name__ == "__main__": |
---|
| 1743 | app = wx.App() |
---|
[76aed53] | 1744 | |
---|
[24adb89] | 1745 | try: |
---|
| 1746 | data = {} |
---|
| 1747 | j = 0 |
---|
| 1748 | for i in range(4): |
---|
| 1749 | j += 1 |
---|
[76aed53] | 1750 | data["index" + str(i)] = [i / j, i * j, i, i + j] |
---|
| 1751 | |
---|
| 1752 | data_input = copy.deepcopy(data) |
---|
[a5e749f] | 1753 | data_input["index5"] = [10, 20, 40, 50] |
---|
[656d65d] | 1754 | frame = GridFrame(data_outputs=data, data_inputs=data_input) |
---|
[24adb89] | 1755 | frame.Show(True) |
---|
| 1756 | except: |
---|
| 1757 | print sys.exc_value |
---|
[76aed53] | 1758 | |
---|
| 1759 | app.MainLoop() |
---|