[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 | |
---|
[0d9dae8] | 9 | from sans.guiframe.utils import format_number |
---|
| 10 | from sans.guicomm.events import EVT_SLICER,EVT_SLICER_PARS,SlicerParameterEvent |
---|
| 11 | |
---|
[b06ef8c] | 12 | |
---|
[ef0c170] | 13 | |
---|
| 14 | |
---|
| 15 | class SlicerParameterPanel(wx.Dialog): |
---|
[b06ef8c] | 16 | #TODO: show units |
---|
| 17 | #TODO: order parameters properly |
---|
| 18 | |
---|
[cd84dca] | 19 | def __init__(self, parent, *args, **kwargs): |
---|
[ef0c170] | 20 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
[12aa9b5] | 21 | """ |
---|
| 22 | Dialog window that allow to edit parameters slicer |
---|
| 23 | by entering new values |
---|
| 24 | """ |
---|
[b06ef8c] | 25 | self.params = {} |
---|
| 26 | self.parent = parent |
---|
| 27 | self.type = None |
---|
| 28 | self.listeners = [] |
---|
| 29 | self.parameters = [] |
---|
| 30 | self.bck = wx.GridBagSizer(5,5) |
---|
| 31 | self.SetSizer(self.bck) |
---|
| 32 | |
---|
| 33 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 34 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 35 | |
---|
| 36 | # Bindings |
---|
| 37 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 38 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 39 | |
---|
| 40 | def onEVT_SLICER(self, event): |
---|
| 41 | """ |
---|
| 42 | Process EVT_SLICER events |
---|
| 43 | When the slicer changes, update the panel |
---|
| 44 | |
---|
| 45 | @param event: EVT_SLICER event |
---|
| 46 | """ |
---|
| 47 | event.Skip() |
---|
| 48 | if event.obj_class==None: |
---|
| 49 | self.set_slicer(None, None) |
---|
| 50 | else: |
---|
| 51 | self.set_slicer(event.type, event.params) |
---|
| 52 | |
---|
| 53 | def set_slicer(self, type, params): |
---|
| 54 | """ |
---|
| 55 | Rebuild the panel |
---|
| 56 | """ |
---|
| 57 | self.bck.Clear(True) |
---|
| 58 | self.type = type |
---|
| 59 | |
---|
| 60 | if type==None: |
---|
| 61 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 62 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 63 | |
---|
| 64 | else: |
---|
| 65 | title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) |
---|
| 66 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[ef0c170] | 67 | ix = 0 |
---|
| 68 | iy = 0 |
---|
[b06ef8c] | 69 | self.parameters = [] |
---|
| 70 | keys = params.keys() |
---|
| 71 | keys.sort() |
---|
| 72 | |
---|
| 73 | for item in keys: |
---|
[ef0c170] | 74 | iy += 1 |
---|
| 75 | ix = 0 |
---|
[0f6d05f8] | 76 | if not item in ["count","errors"]: |
---|
| 77 | |
---|
| 78 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
| 79 | self.bck.Add(text, (iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 80 | ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER) |
---|
| 81 | |
---|
| 82 | ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item) |
---|
| 83 | |
---|
| 84 | ix = 1 |
---|
| 85 | ctl.SetValue(format_number(str(params[item]))) |
---|
| 86 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
| 87 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
| 88 | self.parameters.append([item, ctl]) |
---|
| 89 | self.bck.Add(ctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 90 | |
---|
| 91 | ix =3 |
---|
| 92 | self.bck.Add((20,20), (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 93 | else: |
---|
| 94 | text = wx.StaticText(self, -1, item+ " : ", style=wx.ALIGN_LEFT) |
---|
| 95 | self.bck.Add(text, (iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 96 | ctl =wx.StaticText(self, -1, format_number(str(params[item])), style=wx.ALIGN_LEFT) |
---|
| 97 | ix =1 |
---|
| 98 | self.bck.Add(ctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[ef0c170] | 99 | iy +=1 |
---|
| 100 | ix = 1 |
---|
| 101 | self.bck.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[b06ef8c] | 102 | self.bck.Layout() |
---|
| 103 | self.bck.Fit(self) |
---|
| 104 | self.parent.GetSizer().Layout() |
---|
| 105 | |
---|
| 106 | def onParamChange(self, evt): |
---|
[12aa9b5] | 107 | """ |
---|
| 108 | receive an event end reset value text fields |
---|
| 109 | inside self.parameters |
---|
| 110 | """ |
---|
[b06ef8c] | 111 | evt.Skip() |
---|
| 112 | if evt.type == "UPDATE": |
---|
| 113 | for item in self.parameters: |
---|
| 114 | if item[0] in evt.params: |
---|
| 115 | item[1].SetValue("%-5.3g" %evt.params[item[0]]) |
---|
| 116 | item[1].Refresh() |
---|
| 117 | |
---|
| 118 | def onTextEnter(self, evt): |
---|
| 119 | """ |
---|
| 120 | Parameters have changed |
---|
| 121 | """ |
---|
| 122 | params = {} |
---|
| 123 | has_error = False |
---|
| 124 | for item in self.parameters: |
---|
| 125 | try: |
---|
| 126 | params[item[0]] = float(item[1].GetValue()) |
---|
| 127 | item[1].SetBackgroundColour( |
---|
| 128 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
| 129 | item[1].Refresh() |
---|
| 130 | except: |
---|
| 131 | has_error = True |
---|
| 132 | item[1].SetBackgroundColour("pink") |
---|
| 133 | item[1].Refresh() |
---|
| 134 | |
---|
| 135 | if has_error==False: |
---|
| 136 | # Post parameter event |
---|
[12aa9b5] | 137 | ##parent hier is plotter2D |
---|
[b06ef8c] | 138 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
| 139 | wx.PostEvent(self.parent, event) |
---|
| 140 | |
---|