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