[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) |
---|
[0d9dae8] | 26 | #print "panel created", base |
---|
| 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) |
---|
[aa1b747] | 41 | # Bindings |
---|
[54cc36a] | 42 | #self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
[aa1b747] | 43 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 44 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 45 | |
---|
| 46 | def onEVT_SLICER(self, event): |
---|
| 47 | """ |
---|
| 48 | Process EVT_SLICER events |
---|
| 49 | When the slicer changes, update the panel |
---|
| 50 | |
---|
| 51 | @param event: EVT_SLICER event |
---|
| 52 | """ |
---|
[0d9dae8] | 53 | #print "went here panel" |
---|
[aa1b747] | 54 | event.Skip() |
---|
| 55 | if event.obj_class==None: |
---|
| 56 | self.set_slicer(None, None) |
---|
| 57 | |
---|
| 58 | else: |
---|
| 59 | print "when here not empty event",event.type, event.params |
---|
| 60 | self.set_slicer(event.type, event.params) |
---|
| 61 | |
---|
| 62 | def set_slicer(self, type, params): |
---|
| 63 | """ |
---|
| 64 | Rebuild the panel |
---|
| 65 | """ |
---|
| 66 | self.bck.Clear(True) |
---|
| 67 | self.type = type |
---|
[0d9dae8] | 68 | #print "in set slicer", type, params |
---|
[aa1b747] | 69 | if type==None: |
---|
[54cc36a] | 70 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 71 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
[aa1b747] | 72 | |
---|
[54cc36a] | 73 | else: |
---|
[aa1b747] | 74 | title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) |
---|
| 75 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 76 | |
---|
| 77 | n = 1 |
---|
| 78 | self.parameters = [] |
---|
| 79 | #params = slicer.get_params() |
---|
| 80 | keys = params.keys() |
---|
| 81 | keys.sort() |
---|
| 82 | |
---|
| 83 | for item in keys: |
---|
[0d9dae8] | 84 | if not item.lower() in ["errors", "count"]: |
---|
| 85 | n += 1 |
---|
| 86 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
| 87 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 88 | ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER) |
---|
| 89 | ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item) |
---|
| 90 | ctl.SetValue(str(format_number(params[item]))) |
---|
| 91 | #ctl.Disable() |
---|
| 92 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
| 93 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
| 94 | self.parameters.append([item, ctl]) |
---|
| 95 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
| 96 | for item in keys: |
---|
| 97 | if item.lower() in ["errors", "count"]: |
---|
| 98 | n += 1 |
---|
| 99 | text = wx.StaticText(self, -1, item+": ", style=wx.ALIGN_LEFT) |
---|
| 100 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 101 | ctl = wx.StaticText(self, -1, str(format_number(params[item])), style=wx.ALIGN_LEFT) |
---|
| 102 | ctl.SetToolTipString("Result %s" % item) |
---|
| 103 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
[aa1b747] | 104 | |
---|
| 105 | |
---|
| 106 | self.bck.Layout() |
---|
| 107 | self.bck.Fit(self) |
---|
| 108 | self.parent.GetSizer().Layout() |
---|
| 109 | def onParamChange(self, evt): |
---|
[0d9dae8] | 110 | #print "parameters changed" |
---|
[54cc36a] | 111 | evt.Skip() |
---|
| 112 | #if evt.type == "UPDATE": |
---|
| 113 | for item in self.parameters: |
---|
| 114 | if item[0] in evt.params: |
---|
[0d9dae8] | 115 | item[1].SetValue(format_number(evt.params[item[0]])) |
---|
[54cc36a] | 116 | item[1].Refresh() |
---|
[0d9dae8] | 117 | |
---|
[aa1b747] | 118 | |
---|
| 119 | def onTextEnter(self, evt): |
---|
| 120 | """ |
---|
| 121 | Parameters have changed |
---|
| 122 | """ |
---|
| 123 | params = {} |
---|
| 124 | has_error = False |
---|
| 125 | for item in self.parameters: |
---|
| 126 | try: |
---|
| 127 | params[item[0]] = float(item[1].GetValue()) |
---|
| 128 | item[1].SetBackgroundColour( |
---|
| 129 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
| 130 | item[1].Refresh() |
---|
| 131 | except: |
---|
| 132 | has_error = True |
---|
| 133 | item[1].SetBackgroundColour("pink") |
---|
| 134 | item[1].Refresh() |
---|
[1f3655a] | 135 | |
---|
[aa1b747] | 136 | if has_error==False: |
---|
| 137 | # Post parameter event |
---|
[0d9dae8] | 138 | #print "post new param" |
---|
[aa1b747] | 139 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
[0d9dae8] | 140 | wx.PostEvent(self.base, event) |
---|
| 141 | #print "panel slicer: self base ", self.base |
---|
[aa1b747] | 142 | |
---|