[aa1b747] | 1 | import wx |
---|
| 2 | import wx.lib.newevent |
---|
[5251ec6] | 3 | |
---|
[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 | |
---|
[5251ec6] | 9 | from .parameters_panel_slicer import SlicerParameterPanel |
---|
[aa1b747] | 10 | |
---|
[691643c] | 11 | class SlicerPanel(wx.Panel, PanelBase): |
---|
[d955bf19] | 12 | """ |
---|
[b40ad40] | 13 | Panel class to show the slicer parameters |
---|
[d955bf19] | 14 | """ |
---|
[37d461c] | 15 | # Internal name for the AUI manager |
---|
[aa1b747] | 16 | window_name = "Slicer panel" |
---|
[37d461c] | 17 | # Title to appear on top of the window |
---|
[aa1b747] | 18 | window_caption = "Slicer Panel" |
---|
| 19 | CENTER_PANE = False |
---|
[b40ad40] | 20 | |
---|
[32c0841] | 21 | def __init__(self, parent, id=-1, type=None, base=None, |
---|
[b40ad40] | 22 | params=None, *args, **kwargs): |
---|
[32c0841] | 23 | wx.Panel.__init__(self, parent, id, *args, **kwargs) |
---|
[691643c] | 24 | PanelBase.__init__(self) |
---|
[37d461c] | 25 | # Initialization of the class |
---|
[32c0841] | 26 | self.base = base |
---|
| 27 | if params is None: |
---|
| 28 | params = {} |
---|
[54cc36a] | 29 | self.params = params |
---|
[ae84427] | 30 | self.parent = base.parent |
---|
| 31 | self.frame = None |
---|
[54cc36a] | 32 | self.type = type |
---|
[aa1b747] | 33 | self.listeners = [] |
---|
| 34 | self.parameters = [] |
---|
[32c0841] | 35 | self.bck = wx.GridBagSizer(5, 5) |
---|
[aa1b747] | 36 | self.SetSizer(self.bck) |
---|
[37d461c] | 37 | if type is None and params is None: |
---|
[b40ad40] | 38 | label = "Right-click on 2D plot for slicer options" |
---|
[32c0841] | 39 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 40 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 41 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[54cc36a] | 42 | else: |
---|
[32c0841] | 43 | self.set_slicer(type, params) |
---|
[37d461c] | 44 | # Bindings |
---|
| 45 | self.parent.Bind(EVT_SLICER, SlicerParameterPanel.on_evt_slicer) |
---|
| 46 | self.parent.Bind(EVT_SLICER_PARS, SlicerParameterPanel.on_param_change) |
---|
[b40ad40] | 47 | |
---|
[aa1b747] | 48 | def set_slicer(self, type, params): |
---|
| 49 | """ |
---|
[d955bf19] | 50 | Rebuild the panel |
---|
[aa1b747] | 51 | """ |
---|
[b40ad40] | 52 | self.bck.Clear(True) |
---|
| 53 | self.type = type |
---|
[37d461c] | 54 | if type is None: |
---|
[32c0841] | 55 | label = "Right-click on 2D plot for slicer options" |
---|
| 56 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 57 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 58 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[54cc36a] | 59 | else: |
---|
[27ab091] | 60 | title_text = str(type) + "Parameters" |
---|
[b40ad40] | 61 | title = wx.StaticText(self, -1, title_text, |
---|
[32c0841] | 62 | style=wx.ALIGN_LEFT) |
---|
[b40ad40] | 63 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 64 | flag=wx.LEFT | wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[aa1b747] | 65 | n = 1 |
---|
| 66 | self.parameters = [] |
---|
[5251ec6] | 67 | keys = list(sorted(params.keys())) |
---|
[aa1b747] | 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() |
---|