[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 |
---|
[ae84427] | 34 | self.parent = base.parent |
---|
| 35 | self.frame = None |
---|
[54cc36a] | 36 | self.type = type |
---|
[aa1b747] | 37 | self.listeners = [] |
---|
| 38 | self.parameters = [] |
---|
[32c0841] | 39 | self.bck = wx.GridBagSizer(5, 5) |
---|
[aa1b747] | 40 | self.SetSizer(self.bck) |
---|
[32c0841] | 41 | if type == None and params == None: |
---|
| 42 | label = "Right-click on 2D plot for slicer options" |
---|
| 43 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
| 44 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 45 | flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[54cc36a] | 46 | else: |
---|
[32c0841] | 47 | self.set_slicer(type, params) |
---|
[12aa9b5] | 48 | ## Bindings |
---|
[aa1b747] | 49 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 50 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 51 | |
---|
| 52 | def onEVT_SLICER(self, event): |
---|
| 53 | """ |
---|
[d955bf19] | 54 | Process EVT_SLICER events |
---|
| 55 | When the slicer changes, update the panel |
---|
| 56 | |
---|
| 57 | :param event: EVT_SLICER event |
---|
| 58 | |
---|
[aa1b747] | 59 | """ |
---|
| 60 | event.Skip() |
---|
[32c0841] | 61 | if event.obj_class == None: |
---|
[12aa9b5] | 62 | self.set_slicer(None, None) |
---|
[aa1b747] | 63 | else: |
---|
| 64 | self.set_slicer(event.type, event.params) |
---|
| 65 | |
---|
| 66 | def set_slicer(self, type, params): |
---|
| 67 | """ |
---|
[d955bf19] | 68 | Rebuild the panel |
---|
[aa1b747] | 69 | """ |
---|
| 70 | self.bck.Clear(True) |
---|
| 71 | self.type = type |
---|
[32c0841] | 72 | if type == None: |
---|
| 73 | label = "Right-click on 2D plot for slicer options" |
---|
| 74 | title = wx.StaticText(self, -1, label, style=wx.ALIGN_LEFT) |
---|
| 75 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 76 | flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[54cc36a] | 77 | else: |
---|
[32c0841] | 78 | title = wx.StaticText(self, -1, "Slicer Parameters", |
---|
| 79 | style=wx.ALIGN_LEFT) |
---|
| 80 | self.bck.Add(title, (0, 0), (1, 2), |
---|
| 81 | flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[aa1b747] | 82 | n = 1 |
---|
| 83 | self.parameters = [] |
---|
| 84 | keys = params.keys() |
---|
| 85 | keys.sort() |
---|
| 86 | for item in keys: |
---|
[d555416] | 87 | if not item.lower() in ["num_points", "avg", "avg_error", "sum", |
---|
| 88 | "sum_error"]: |
---|
[0d9dae8] | 89 | n += 1 |
---|
| 90 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
[32c0841] | 91 | self.bck.Add(text, (n-1, 0), |
---|
| 92 | flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 93 | ctl = wx.TextCtrl(self, -1, size=(80, 20), |
---|
| 94 | style=wx.TE_PROCESS_ENTER) |
---|
[88989768] | 95 | hint_msg = "Modify the value of %s to change " % item |
---|
| 96 | hint_msg += "the 2D slicer" |
---|
[32c0841] | 97 | ctl.SetToolTipString(hint_msg) |
---|
[0d9dae8] | 98 | ctl.SetValue(str(format_number(params[item]))) |
---|
| 99 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
[37f487e] | 100 | ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[0d9dae8] | 101 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
| 102 | self.parameters.append([item, ctl]) |
---|
[32c0841] | 103 | self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0) |
---|
[0d9dae8] | 104 | for item in keys: |
---|
[d555416] | 105 | if item.lower() in ["num_points", "avg", "avg_error", "sum", |
---|
| 106 | "sum_error"]: |
---|
[0d9dae8] | 107 | n += 1 |
---|
[32c0841] | 108 | text = wx.StaticText(self, -1, item + ": ", |
---|
| 109 | style=wx.ALIGN_LEFT) |
---|
| 110 | self.bck.Add(text, (n-1, 0), |
---|
| 111 | flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, |
---|
| 112 | border=15) |
---|
| 113 | ctl = wx.StaticText(self, -1, |
---|
| 114 | str(format_number(params[item])), |
---|
| 115 | style=wx.ALIGN_LEFT) |
---|
[0d9dae8] | 116 | ctl.SetToolTipString("Result %s" % item) |
---|
[32c0841] | 117 | self.bck.Add(ctl, (n-1, 1), flag=wx.TOP|wx.BOTTOM, border=0) |
---|
[aa1b747] | 118 | self.bck.Layout() |
---|
[ddd864e] | 119 | #self.bck.Fit(self) |
---|
| 120 | self.Layout() |
---|
[ae84427] | 121 | psizer = self.parent.GetSizer() |
---|
| 122 | if psizer != None: |
---|
| 123 | psizer.Layout() |
---|
[12aa9b5] | 124 | |
---|
[37f487e] | 125 | def onSetFocus(self, evt): |
---|
| 126 | """ |
---|
[d955bf19] | 127 | Hightlight the textcrtl |
---|
[37f487e] | 128 | """ |
---|
| 129 | # Get a handle to the TextCtrl |
---|
| 130 | widget = evt.GetEventObject() |
---|
| 131 | # Select the whole control, after this event resolves |
---|
[32c0841] | 132 | wx.CallAfter(widget.SetSelection, -1, -1) |
---|
[37f487e] | 133 | return |
---|
| 134 | |
---|
[aa1b747] | 135 | def onParamChange(self, evt): |
---|
[12aa9b5] | 136 | """ |
---|
[d955bf19] | 137 | Receive and event and reset the text field contained in self.parameters |
---|
[12aa9b5] | 138 | |
---|
| 139 | """ |
---|
[54cc36a] | 140 | evt.Skip() |
---|
| 141 | for item in self.parameters: |
---|
| 142 | if item[0] in evt.params: |
---|
[0d9dae8] | 143 | item[1].SetValue(format_number(evt.params[item[0]])) |
---|
[54cc36a] | 144 | item[1].Refresh() |
---|
[0d9dae8] | 145 | |
---|
[aa1b747] | 146 | def onTextEnter(self, evt): |
---|
| 147 | """ |
---|
[d955bf19] | 148 | Parameters have changed |
---|
[aa1b747] | 149 | """ |
---|
| 150 | params = {} |
---|
| 151 | has_error = False |
---|
| 152 | for item in self.parameters: |
---|
| 153 | try: |
---|
| 154 | params[item[0]] = float(item[1].GetValue()) |
---|
| 155 | item[1].SetBackgroundColour( |
---|
| 156 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
| 157 | item[1].Refresh() |
---|
| 158 | except: |
---|
| 159 | has_error = True |
---|
| 160 | item[1].SetBackgroundColour("pink") |
---|
| 161 | item[1].Refresh() |
---|
[1f3655a] | 162 | |
---|
[32c0841] | 163 | if has_error == False: |
---|
[aa1b747] | 164 | # Post parameter event |
---|
[12aa9b5] | 165 | ## base is guiframe is this case |
---|
[aa1b747] | 166 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
[0d9dae8] | 167 | wx.PostEvent(self.base, event) |
---|
[ae84427] | 168 | |
---|
| 169 | def on_close(self, event): |
---|
| 170 | """ |
---|
| 171 | On Close Event |
---|
| 172 | """ |
---|
| 173 | ID = self.uid |
---|
| 174 | self.parent.delete_panel(ID) |
---|
| 175 | self.frame.Destroy() |
---|