[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 | |
---|
| 9 | (SlicerEvent, EVT_SLICER) = wx.lib.newevent.NewEvent() |
---|
| 10 | (SlicerParameterEvent, EVT_SLICER_PARS) = wx.lib.newevent.NewEvent() |
---|
| 11 | |
---|
| 12 | class SlicerPanel(wx.Panel): |
---|
| 13 | #TODO: show units |
---|
| 14 | #TODO: order parameters properly |
---|
| 15 | ## Internal name for the AUI manager |
---|
| 16 | window_name = "Slicer panel" |
---|
| 17 | ## Title to appear on top of the window |
---|
| 18 | window_caption = "Slicer Panel" |
---|
| 19 | |
---|
| 20 | CENTER_PANE = False |
---|
| 21 | |
---|
[54cc36a] | 22 | def __init__(self, parent,id=-1,type=None, params={}, *args, **kwargs): |
---|
| 23 | wx.Panel.__init__(self, parent,id, *args, **kwargs) |
---|
| 24 | print "panel created" |
---|
| 25 | self.params = params |
---|
[aa1b747] | 26 | self.parent = parent |
---|
[54cc36a] | 27 | self.type = type |
---|
[aa1b747] | 28 | self.listeners = [] |
---|
| 29 | self.parameters = [] |
---|
| 30 | self.bck = wx.GridBagSizer(5,5) |
---|
| 31 | self.SetSizer(self.bck) |
---|
[54cc36a] | 32 | if type==None and params==None: |
---|
| 33 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
| 34 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 35 | else: |
---|
| 36 | self.set_slicer( type, params) |
---|
[aa1b747] | 37 | # Bindings |
---|
[54cc36a] | 38 | #self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
[aa1b747] | 39 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
| 40 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
| 41 | |
---|
| 42 | def onEVT_SLICER(self, event): |
---|
| 43 | """ |
---|
| 44 | Process EVT_SLICER events |
---|
| 45 | When the slicer changes, update the panel |
---|
| 46 | |
---|
| 47 | @param event: EVT_SLICER event |
---|
| 48 | """ |
---|
[54cc36a] | 49 | print "went here panel" |
---|
[aa1b747] | 50 | event.Skip() |
---|
| 51 | if event.obj_class==None: |
---|
| 52 | self.set_slicer(None, None) |
---|
| 53 | |
---|
| 54 | else: |
---|
| 55 | print "when here not empty event",event.type, event.params |
---|
| 56 | self.set_slicer(event.type, event.params) |
---|
| 57 | |
---|
| 58 | def set_slicer(self, type, params): |
---|
| 59 | """ |
---|
| 60 | Rebuild the panel |
---|
| 61 | """ |
---|
| 62 | self.bck.Clear(True) |
---|
| 63 | self.type = type |
---|
[54cc36a] | 64 | print "in set slicer", type, params |
---|
[aa1b747] | 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) |
---|
[aa1b747] | 68 | |
---|
[54cc36a] | 69 | else: |
---|
[aa1b747] | 70 | title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) |
---|
| 71 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
| 72 | |
---|
| 73 | n = 1 |
---|
| 74 | self.parameters = [] |
---|
| 75 | #params = slicer.get_params() |
---|
| 76 | keys = params.keys() |
---|
| 77 | keys.sort() |
---|
| 78 | |
---|
| 79 | for item in keys: |
---|
| 80 | n += 1 |
---|
| 81 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
| 82 | self.bck.Add(text, (n-1,0), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
| 83 | ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER) |
---|
| 84 | |
---|
| 85 | ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item) |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | ctl.SetValue(str(params[item])) |
---|
| 89 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
| 90 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
| 91 | self.parameters.append([item, ctl]) |
---|
| 92 | self.bck.Add(ctl, (n-1,1), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
| 93 | |
---|
| 94 | self.bck.Layout() |
---|
| 95 | self.bck.Fit(self) |
---|
| 96 | self.parent.GetSizer().Layout() |
---|
| 97 | def onParamChange(self, evt): |
---|
[54cc36a] | 98 | print "parameters changed" |
---|
| 99 | evt.Skip() |
---|
| 100 | #if evt.type == "UPDATE": |
---|
| 101 | for item in self.parameters: |
---|
| 102 | if item[0] in evt.params: |
---|
| 103 | item[1].SetValue("%-5.3g" %evt.params[item[0]]) |
---|
| 104 | item[1].Refresh() |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | def old_onParamChange(self, evt): |
---|
[aa1b747] | 108 | evt.Skip() |
---|
| 109 | if evt.type == "UPDATE": |
---|
| 110 | for item in self.parameters: |
---|
| 111 | if item[0] in evt.params: |
---|
| 112 | item[1].SetValue("%-5.3g" %evt.params[item[0]]) |
---|
| 113 | item[1].Refresh() |
---|
| 114 | |
---|
| 115 | def onTextEnter(self, evt): |
---|
| 116 | """ |
---|
| 117 | Parameters have changed |
---|
| 118 | """ |
---|
| 119 | params = {} |
---|
| 120 | has_error = False |
---|
| 121 | for item in self.parameters: |
---|
| 122 | try: |
---|
| 123 | params[item[0]] = float(item[1].GetValue()) |
---|
| 124 | item[1].SetBackgroundColour( |
---|
| 125 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
| 126 | item[1].Refresh() |
---|
| 127 | except: |
---|
| 128 | has_error = True |
---|
| 129 | item[1].SetBackgroundColour("pink") |
---|
| 130 | item[1].Refresh() |
---|
| 131 | |
---|
| 132 | if has_error==False: |
---|
| 133 | # Post parameter event |
---|
| 134 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
| 135 | wx.PostEvent(self.parent, event) |
---|
| 136 | |
---|