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