[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() |
---|
[ddd864e] | 76 | |
---|
[aa1b747] | 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) |
---|
[37f487e] | 87 | ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[0d9dae8] | 88 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
[ddd864e] | 89 | |
---|
[0d9dae8] | 90 | self.parameters.append([item, ctl]) |
---|
| 91 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
| 92 | for item in keys: |
---|
| 93 | if item.lower() in ["errors", "count"]: |
---|
| 94 | n += 1 |
---|
| 95 | text = wx.StaticText(self, -1, item+": ", style=wx.ALIGN_LEFT) |
---|
| 96 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 97 | ctl = wx.StaticText(self, -1, str(format_number(params[item])), style=wx.ALIGN_LEFT) |
---|
| 98 | ctl.SetToolTipString("Result %s" % item) |
---|
| 99 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
[ddd864e] | 100 | |
---|
[aa1b747] | 101 | self.bck.Layout() |
---|
[ddd864e] | 102 | #self.bck.Fit(self) |
---|
| 103 | self.Layout() |
---|
[aa1b747] | 104 | self.parent.GetSizer().Layout() |
---|
[12aa9b5] | 105 | |
---|
[37f487e] | 106 | def onSetFocus(self, evt): |
---|
| 107 | """ |
---|
| 108 | Hightlight the textcrtl |
---|
| 109 | """ |
---|
| 110 | # Get a handle to the TextCtrl |
---|
| 111 | widget = evt.GetEventObject() |
---|
| 112 | # Select the whole control, after this event resolves |
---|
| 113 | wx.CallAfter(widget.SetSelection, -1,-1) |
---|
| 114 | return |
---|
| 115 | |
---|
| 116 | |
---|
[aa1b747] | 117 | def onParamChange(self, evt): |
---|
[12aa9b5] | 118 | """ |
---|
| 119 | Receive and event and reset the text field contained in self.parameters |
---|
| 120 | |
---|
| 121 | """ |
---|
[54cc36a] | 122 | evt.Skip() |
---|
| 123 | for item in self.parameters: |
---|
| 124 | if item[0] in evt.params: |
---|
[0d9dae8] | 125 | item[1].SetValue(format_number(evt.params[item[0]])) |
---|
[54cc36a] | 126 | item[1].Refresh() |
---|
[0d9dae8] | 127 | |
---|
[aa1b747] | 128 | |
---|
| 129 | def onTextEnter(self, evt): |
---|
| 130 | """ |
---|
| 131 | Parameters have changed |
---|
| 132 | """ |
---|
| 133 | params = {} |
---|
| 134 | has_error = False |
---|
| 135 | for item in self.parameters: |
---|
| 136 | try: |
---|
| 137 | params[item[0]] = float(item[1].GetValue()) |
---|
| 138 | item[1].SetBackgroundColour( |
---|
| 139 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
| 140 | item[1].Refresh() |
---|
| 141 | except: |
---|
| 142 | has_error = True |
---|
| 143 | item[1].SetBackgroundColour("pink") |
---|
| 144 | item[1].Refresh() |
---|
[1f3655a] | 145 | |
---|
[aa1b747] | 146 | if has_error==False: |
---|
| 147 | # Post parameter event |
---|
[12aa9b5] | 148 | ## base is guiframe is this case |
---|
[aa1b747] | 149 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
[0d9dae8] | 150 | wx.PostEvent(self.base, event) |
---|
[12aa9b5] | 151 | |
---|
[aa1b747] | 152 | |
---|