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