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