[aa1b747] | 1 | import wx |
---|
| 2 | import wx.lib.newevent |
---|
[37d461c] | 3 | from parameters_panel_slicer import SlicerParameterPanel |
---|
[d85c194] | 4 | from sas.sasgui.guiframe.utils import format_number |
---|
| 5 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
[37d461c] | 6 | from sas.sasgui.guiframe.events import (SlicerParameterEvent, EVT_SLICER_PARS, |
---|
| 7 | EVT_SLICER) |
---|
| 8 | |
---|
[aa1b747] | 9 | |
---|
[691643c] | 10 | class SlicerPanel(wx.Panel, PanelBase): |
---|
[d955bf19] | 11 | """ |
---|
[b40ad40] | 12 | Panel class to show the slicer parameters |
---|
[d955bf19] | 13 | """ |
---|
[37d461c] | 14 | # Internal name for the AUI manager |
---|
[aa1b747] | 15 | window_name = "Slicer panel" |
---|
[37d461c] | 16 | # Title to appear on top of the window |
---|
[aa1b747] | 17 | window_caption = "Slicer Panel" |
---|
| 18 | CENTER_PANE = False |
---|
[b40ad40] | 19 | |
---|
[32c0841] | 20 | def __init__(self, parent, id=-1, type=None, base=None, |
---|
[b40ad40] | 21 | params=None, *args, **kwargs): |
---|
[32c0841] | 22 | wx.Panel.__init__(self, parent, id, *args, **kwargs) |
---|
[691643c] | 23 | PanelBase.__init__(self) |
---|
[37d461c] | 24 | # Initialization of the class |
---|
[32c0841] | 25 | self.base = base |
---|
| 26 | if params is None: |
---|
| 27 | params = {} |
---|
[54cc36a] | 28 | self.params = params |
---|
[ae84427] | 29 | self.parent = base.parent |
---|
| 30 | self.frame = None |
---|
[54cc36a] | 31 | self.type = type |
---|
[aa1b747] | 32 | self.listeners = [] |
---|
| 33 | self.parameters = [] |
---|
[32c0841] | 34 | self.bck = wx.GridBagSizer(5, 5) |
---|
[aa1b747] | 35 | self.SetSizer(self.bck) |
---|
[37d461c] | 36 | if type is None and params is None: |
---|
[b40ad40] | 37 | label = "Right-click on 2D plot for slicer options" |
---|
[32c0841] | 38 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 39 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 40 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[54cc36a] | 41 | else: |
---|
[32c0841] | 42 | self.set_slicer(type, params) |
---|
[37d461c] | 43 | # Bindings |
---|
| 44 | self.parent.Bind(EVT_SLICER, SlicerParameterPanel.on_evt_slicer) |
---|
| 45 | self.parent.Bind(EVT_SLICER_PARS, SlicerParameterPanel.on_param_change) |
---|
[b40ad40] | 46 | |
---|
[aa1b747] | 47 | def set_slicer(self, type, params): |
---|
| 48 | """ |
---|
[d955bf19] | 49 | Rebuild the panel |
---|
[aa1b747] | 50 | """ |
---|
[b40ad40] | 51 | self.bck.Clear(True) |
---|
| 52 | self.type = type |
---|
[37d461c] | 53 | if type is None: |
---|
[32c0841] | 54 | label = "Right-click on 2D plot for slicer options" |
---|
| 55 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 56 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 57 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[54cc36a] | 58 | else: |
---|
[27ab091] | 59 | title_text = str(type) + "Parameters" |
---|
[b40ad40] | 60 | title = wx.StaticText(self, -1, title_text, |
---|
[32c0841] | 61 | style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 62 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 63 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[aa1b747] | 64 | n = 1 |
---|
| 65 | self.parameters = [] |
---|
| 66 | keys = params.keys() |
---|
| 67 | keys.sort() |
---|
| 68 | for item in keys: |
---|
[37d461c] | 69 | if not item.lower() in ["num_points", "avg", "avg_error", "sum", |
---|
| 70 | "sum_error"]: |
---|
[0d9dae8] | 71 | n += 1 |
---|
| 72 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 73 | self.bck.Add(text, (n - 1, 0), |
---|
[37d461c] | 74 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, |
---|
| 75 | border=15) |
---|
[b40ad40] | 76 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
[32c0841] | 77 | style=wx.TE_PROCESS_ENTER) |
---|
[88989768] | 78 | hint_msg = "Modify the value of %s to change " % item |
---|
| 79 | hint_msg += "the 2D slicer" |
---|
[32c0841] | 80 | ctl.SetToolTipString(hint_msg) |
---|
[0d9dae8] | 81 | ctl.SetValue(str(format_number(params[item]))) |
---|
[37d461c] | 82 | self.Bind(wx.EVT_TEXT_ENTER, self.on_text_enter) |
---|
| 83 | ctl.Bind(wx.EVT_SET_FOCUS, self.on_set_focus) |
---|
| 84 | ctl.Bind(wx.EVT_KILL_FOCUS, self.on_text_enter) |
---|
[0d9dae8] | 85 | self.parameters.append([item, ctl]) |
---|
[37d461c] | 86 | self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, |
---|
| 87 | border=0) |
---|
[0d9dae8] | 88 | for item in keys: |
---|
[37d461c] | 89 | if item.lower() in ["num_points", "avg", "avg_error", "sum", |
---|
| 90 | "sum_error"]: |
---|
[0d9dae8] | 91 | n += 1 |
---|
[37d461c] | 92 | text = wx.StaticText(self, -1, item + ": ", |
---|
| 93 | style=wx.ALIGN_LEFT) |
---|
| 94 | self.bck.Add(text, (n - 1, 0), |
---|
| 95 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, |
---|
[32c0841] | 96 | border=15) |
---|
[b40ad40] | 97 | ctl = wx.StaticText(self, -1, |
---|
[32c0841] | 98 | str(format_number(params[item])), |
---|
| 99 | style=wx.ALIGN_LEFT) |
---|
[0d9dae8] | 100 | ctl.SetToolTipString("Result %s" % item) |
---|
[37d461c] | 101 | self.bck.Add(ctl, (n - 1, 1), flag=wx.TOP | wx.BOTTOM, |
---|
| 102 | border=0) |
---|
[aa1b747] | 103 | self.bck.Layout() |
---|
[ddd864e] | 104 | self.Layout() |
---|
[37d461c] | 105 | p_sizer = self.parent.GetSizer() |
---|
| 106 | if p_sizer is not None: |
---|
| 107 | p_sizer.Layout() |
---|
[b40ad40] | 108 | |
---|
[37d461c] | 109 | def on_set_focus(self, evt): |
---|
[37f487e] | 110 | """ |
---|
[27ab091] | 111 | Highlight the txtcrtl |
---|
[37f487e] | 112 | """ |
---|
[27ab091] | 113 | evt.Skip() |
---|
[37f487e] | 114 | # Get a handle to the TextCtrl |
---|
| 115 | widget = evt.GetEventObject() |
---|
| 116 | # Select the whole control, after this event resolves |
---|
[32c0841] | 117 | wx.CallAfter(widget.SetSelection, -1, -1) |
---|
[b40ad40] | 118 | |
---|
[37d461c] | 119 | def on_text_enter(self, evt): |
---|
[aa1b747] | 120 | """ |
---|
[d955bf19] | 121 | Parameters have changed |
---|
[27ab091] | 122 | """ |
---|
| 123 | evt.Skip() |
---|
[aa1b747] | 124 | params = {} |
---|
| 125 | has_error = False |
---|
| 126 | for item in self.parameters: |
---|
| 127 | try: |
---|
| 128 | params[item[0]] = float(item[1].GetValue()) |
---|
[37d461c] | 129 | item[1].SetBackgroundColour(wx.SystemSettings_GetColour( |
---|
| 130 | wx.SYS_COLOUR_WINDOW)) |
---|
[aa1b747] | 131 | item[1].Refresh() |
---|
| 132 | except: |
---|
| 133 | has_error = True |
---|
| 134 | item[1].SetBackgroundColour("pink") |
---|
| 135 | item[1].Refresh() |
---|
[37d461c] | 136 | if not has_error: |
---|
[aa1b747] | 137 | # Post parameter event |
---|
[37d461c] | 138 | # base is guiframe is this case |
---|
[aa1b747] | 139 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
[0d9dae8] | 140 | wx.PostEvent(self.base, event) |
---|
[ae84427] | 141 | |
---|
| 142 | def on_close(self, event): |
---|
| 143 | """ |
---|
| 144 | On Close Event |
---|
| 145 | """ |
---|
[37d461c] | 146 | uid = self.uid |
---|
| 147 | self.parent.delete_panel(uid) |
---|
[b40ad40] | 148 | self.frame.Destroy() |
---|