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