[b06ef8c] | 1 | """ |
---|
| 2 | Panel class to show the slicer parameters |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import wx |
---|
| 6 | import wx.lib.newevent |
---|
| 7 | from copy import deepcopy |
---|
| 8 | |
---|
| 9 | (SlicerEvent, EVT_SLICER) = wx.lib.newevent.NewEvent() |
---|
| 10 | (SlicerParameterEvent, EVT_SLICER_PARS) = wx.lib.newevent.NewEvent() |
---|
| 11 | |
---|
[cd84dca] | 12 | class SlicerParameterPanel(wx.Panel): |
---|
[b06ef8c] | 13 | #TODO: show units |
---|
| 14 | #TODO: order parameters properly |
---|
| 15 | |
---|
[cd84dca] | 16 | def __init__(self, parent, *args, **kwargs): |
---|
| 17 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
[b06ef8c] | 18 | self.params = {} |
---|
| 19 | self.parent = parent |
---|
| 20 | self.type = None |
---|
| 21 | self.listeners = [] |
---|
| 22 | self.parameters = [] |
---|
| 23 | self.bck = wx.GridBagSizer(5,5) |
---|
| 24 | self.SetSizer(self.bck) |
---|
| 25 | |
---|
| 26 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 27 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 28 | |
---|
| 29 | # Bindings |
---|
| 30 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 31 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 32 | |
---|
| 33 | def onEVT_SLICER(self, event): |
---|
| 34 | """ |
---|
| 35 | Process EVT_SLICER events |
---|
| 36 | When the slicer changes, update the panel |
---|
| 37 | |
---|
| 38 | @param event: EVT_SLICER event |
---|
| 39 | """ |
---|
| 40 | event.Skip() |
---|
| 41 | if event.obj_class==None: |
---|
| 42 | self.set_slicer(None, None) |
---|
| 43 | else: |
---|
| 44 | self.set_slicer(event.type, event.params) |
---|
| 45 | |
---|
| 46 | def set_slicer(self, type, params): |
---|
| 47 | """ |
---|
| 48 | Rebuild the panel |
---|
| 49 | """ |
---|
| 50 | self.bck.Clear(True) |
---|
| 51 | self.type = type |
---|
| 52 | |
---|
| 53 | if type==None: |
---|
| 54 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 55 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 56 | |
---|
| 57 | else: |
---|
| 58 | title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) |
---|
| 59 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 60 | |
---|
| 61 | n = 1 |
---|
| 62 | self.parameters = [] |
---|
| 63 | #params = slicer.get_params() |
---|
| 64 | keys = params.keys() |
---|
| 65 | keys.sort() |
---|
| 66 | |
---|
| 67 | for item in keys: |
---|
| 68 | n += 1 |
---|
| 69 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
| 70 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 71 | ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER) |
---|
| 72 | |
---|
| 73 | ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item) |
---|
| 74 | |
---|
| 75 | |
---|
[cd84dca] | 76 | ctl.SetValue(str(params[item])) |
---|
[b06ef8c] | 77 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
| 78 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
| 79 | self.parameters.append([item, ctl]) |
---|
| 80 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
| 81 | |
---|
| 82 | self.bck.Layout() |
---|
| 83 | self.bck.Fit(self) |
---|
| 84 | self.parent.GetSizer().Layout() |
---|
| 85 | |
---|
| 86 | def onParamChange(self, evt): |
---|
| 87 | evt.Skip() |
---|
| 88 | if evt.type == "UPDATE": |
---|
| 89 | for item in self.parameters: |
---|
| 90 | if item[0] in evt.params: |
---|
| 91 | item[1].SetValue("%-5.3g" %evt.params[item[0]]) |
---|
| 92 | item[1].Refresh() |
---|
| 93 | |
---|
| 94 | def onTextEnter(self, evt): |
---|
| 95 | """ |
---|
| 96 | Parameters have changed |
---|
| 97 | """ |
---|
| 98 | params = {} |
---|
| 99 | has_error = False |
---|
| 100 | for item in self.parameters: |
---|
| 101 | try: |
---|
| 102 | params[item[0]] = float(item[1].GetValue()) |
---|
| 103 | item[1].SetBackgroundColour( |
---|
| 104 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
| 105 | item[1].Refresh() |
---|
| 106 | except: |
---|
| 107 | has_error = True |
---|
| 108 | item[1].SetBackgroundColour("pink") |
---|
| 109 | item[1].Refresh() |
---|
| 110 | |
---|
| 111 | if has_error==False: |
---|
| 112 | # Post parameter event |
---|
| 113 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
| 114 | wx.PostEvent(self.parent, event) |
---|
| 115 | |
---|