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