[aa1b747] | 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 SlicerParameterEvent,EVT_SLICER_PARS,EVT_SLICER |
---|
| 11 | |
---|
| 12 | |
---|
[aa1b747] | 13 | |
---|
| 14 | class SlicerPanel(wx.Panel): |
---|
| 15 | #TODO: show units |
---|
| 16 | #TODO: order parameters properly |
---|
| 17 | ## Internal name for the AUI manager |
---|
| 18 | window_name = "Slicer panel" |
---|
| 19 | ## Title to appear on top of the window |
---|
| 20 | window_caption = "Slicer Panel" |
---|
| 21 | |
---|
| 22 | CENTER_PANE = False |
---|
| 23 | |
---|
[0d9dae8] | 24 | def __init__(self, parent,id=-1,type=None,base=None, params={}, *args, **kwargs): |
---|
[54cc36a] | 25 | wx.Panel.__init__(self, parent,id, *args, **kwargs) |
---|
[12aa9b5] | 26 | ## Initialization of the class |
---|
[0d9dae8] | 27 | self.base= base |
---|
[54cc36a] | 28 | self.params = params |
---|
[aa1b747] | 29 | self.parent = parent |
---|
[54cc36a] | 30 | self.type = type |
---|
[aa1b747] | 31 | self.listeners = [] |
---|
| 32 | self.parameters = [] |
---|
[0d9dae8] | 33 | |
---|
[aa1b747] | 34 | self.bck = wx.GridBagSizer(5,5) |
---|
| 35 | self.SetSizer(self.bck) |
---|
[54cc36a] | 36 | if type==None and params==None: |
---|
| 37 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 38 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 39 | else: |
---|
| 40 | self.set_slicer( type, params) |
---|
[12aa9b5] | 41 | ## Bindings |
---|
[aa1b747] | 42 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 43 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 44 | |
---|
[12aa9b5] | 45 | |
---|
[aa1b747] | 46 | def onEVT_SLICER(self, event): |
---|
| 47 | """ |
---|
| 48 | Process EVT_SLICER events |
---|
| 49 | When the slicer changes, update the panel |
---|
| 50 | @param event: EVT_SLICER event |
---|
| 51 | """ |
---|
| 52 | event.Skip() |
---|
| 53 | if event.obj_class==None: |
---|
[12aa9b5] | 54 | self.set_slicer(None, None) |
---|
[aa1b747] | 55 | else: |
---|
| 56 | self.set_slicer(event.type, event.params) |
---|
| 57 | |
---|
[12aa9b5] | 58 | |
---|
[aa1b747] | 59 | def set_slicer(self, type, params): |
---|
| 60 | """ |
---|
| 61 | Rebuild the panel |
---|
| 62 | """ |
---|
| 63 | self.bck.Clear(True) |
---|
| 64 | self.type = type |
---|
| 65 | if type==None: |
---|
[54cc36a] | 66 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 67 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 68 | else: |
---|
[aa1b747] | 69 | title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) |
---|
| 70 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 71 | |
---|
| 72 | n = 1 |
---|
| 73 | self.parameters = [] |
---|
| 74 | keys = params.keys() |
---|
| 75 | keys.sort() |
---|
| 76 | |
---|
| 77 | for item in keys: |
---|
[0d9dae8] | 78 | if not item.lower() in ["errors", "count"]: |
---|
| 79 | n += 1 |
---|
| 80 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
| 81 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 82 | ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER) |
---|
| 83 | ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item) |
---|
| 84 | ctl.SetValue(str(format_number(params[item]))) |
---|
[12aa9b5] | 85 | |
---|
[0d9dae8] | 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, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
| 90 | for item in keys: |
---|
| 91 | if item.lower() in ["errors", "count"]: |
---|
| 92 | n += 1 |
---|
| 93 | text = wx.StaticText(self, -1, item+": ", style=wx.ALIGN_LEFT) |
---|
| 94 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 95 | ctl = wx.StaticText(self, -1, str(format_number(params[item])), style=wx.ALIGN_LEFT) |
---|
| 96 | ctl.SetToolTipString("Result %s" % item) |
---|
| 97 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
[aa1b747] | 98 | |
---|
| 99 | self.bck.Layout() |
---|
| 100 | self.bck.Fit(self) |
---|
| 101 | self.parent.GetSizer().Layout() |
---|
[12aa9b5] | 102 | |
---|
| 103 | |
---|
[aa1b747] | 104 | def onParamChange(self, evt): |
---|
[12aa9b5] | 105 | """ |
---|
| 106 | Receive and event and reset the text field contained in self.parameters |
---|
| 107 | |
---|
| 108 | """ |
---|
[54cc36a] | 109 | evt.Skip() |
---|
| 110 | for item in self.parameters: |
---|
| 111 | if item[0] in evt.params: |
---|
[0d9dae8] | 112 | item[1].SetValue(format_number(evt.params[item[0]])) |
---|
[54cc36a] | 113 | item[1].Refresh() |
---|
[0d9dae8] | 114 | |
---|
[aa1b747] | 115 | |
---|
| 116 | def onTextEnter(self, evt): |
---|
| 117 | """ |
---|
| 118 | Parameters have changed |
---|
| 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() |
---|
[1f3655a] | 132 | |
---|
[aa1b747] | 133 | if has_error==False: |
---|
| 134 | # Post parameter event |
---|
[12aa9b5] | 135 | ## base is guiframe is this case |
---|
[aa1b747] | 136 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
[0d9dae8] | 137 | wx.PostEvent(self.base, event) |
---|
[12aa9b5] | 138 | |
---|
[aa1b747] | 139 | |
---|