def configure(self, parent, filepath): self.filepath = filepath self.parent = parent # Init panel controls def bind_btn(btnid, fn): self.Bind(wx.EVT_BUTTON, fn, id=xrc.get(btnid)) bind_btn("btn_load_cancel", self.on_cancel) # Start loading thread self.thread_load = LoadThread(self, self.on_load_result, False) fn = paply(self.thread_load.default_progress_fn, wxgauge=xrc.get("progress_gauge", self)) self.thread_load.configure(filepath, fn) self.thread_load.start() self.Bind(wx.EVT_WINDOW_DESTROY, self.on_destroy)
class LoadPanel(wx.Panel): def __init__(self, *args, **kwargs): pre = wx.PrePanel() # create step is handled by XRC self.PostCreate(pre) def configure(self, parent, filepath): self.filepath = filepath self.parent = parent # Init panel controls def bind_btn(btnid, fn): self.Bind(wx.EVT_BUTTON, fn, id=xrc.get(btnid)) bind_btn("btn_load_cancel", self.on_cancel) # Start loading thread self.thread_load = LoadThread(self, self.on_load_result, False) fn = paply(self.thread_load.default_progress_fn, wxgauge=xrc.get("progress_gauge", self)) self.thread_load.configure(filepath, fn) self.thread_load.start() self.Bind(wx.EVT_WINDOW_DESTROY, self.on_destroy) def on_cancel(self, event=None): self.thread_load.abort() wx.CallAfter(self.parent.DeletePage, self.parent.GetSelection()) def on_load_result(self, event=None): logging.debug("Load result") if event.data is None: logging.debug("Load failure") dial = wx.MessageDialog(self, "Error loading file, check console", "Error", wx.OK | wx.ICON_ERROR) dial.ShowModal() dial.Destroy() self.on_cancel() else: # plot logging.info("Data loaded, plotting") self.plot(event.data) self.thread_update = UpdateThread(self, self.on_update, False) self.thread_update.configure( self.filepath, event.data["_last_red"], event.data["_last_mappings"], event.data["_bounds"], event.data["_time_bounds"], ) self.thread_update.start() def on_update(self, event=None): # event.data = data to be merged with current plot data self.plot.data["_bounds"] = event.data.pop("_bounds") self.plot.data["_time_bounds"] = event.data.pop("_time_bounds") self.plot.data["_times"] += event.data.pop("_times") for dtype, value in event.data.iteritems(): self.plot.data[dtype] += value if hasattr(self.plot, dtype): line = getattr(self.plot, dtype) for index, _line in enumerate(self.plot.axes.lines): if _line == line: break self.plot.axes.lines[index].set_xdata(self.plot.data["_times"]) self.plot.axes.lines[index].set_ydata(self.plot.data[dtype]) live_mode = self.plot.controls.live_mode.GetSelection() xmax = self.plot.data["_times"][-1] if live_mode == 1: # follow # TODO (normal): configurable live window size xmin = xmax - timedelta(minutes=10) self.plot.axes.set_xbound(lower=xmin, upper=xmax) elif live_mode == 2: # scale x xmin = self.plot.data["_times"][0] self.plot.axes.set_xbound(lower=xmin, upper=xmax) self.plot.redraw() def on_destroy(self, event=None): """ End our threads gracefully """ if hasattr(self, "thread_load") and self.thread_load is not None: self.thread_load.abort() if hasattr(self, "thread_update") and self.thread_update is not None: self.thread_update.abort() def plot(self, data): xrc.get("load_progress", self).Hide() chart = xrc.get("chart_panel", self) if data["_times"] != []: title = data["_times"][int(len(data["_times"]) / 2)].strftime("%d %b %Y") else: title = os.path.basename(self.filepath) self.plot = PlotPanel(chart, data, title, self) sizer = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(self.plot, 1, wx.EXPAND) chart.SetSizer(sizer) self.Fit()