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