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