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 | |
---|
13 | def format_number(value, high=False): |
---|
14 | """ |
---|
15 | Return a float in a standardized, human-readable formatted string |
---|
16 | """ |
---|
17 | try: |
---|
18 | value = float(value) |
---|
19 | except: |
---|
20 | return "NaN" |
---|
21 | |
---|
22 | if high: |
---|
23 | return "%-6.4g" % value |
---|
24 | else: |
---|
25 | return "%-5.3g" % value |
---|
26 | |
---|
27 | class SlicerParameterPanel(wx.Dialog): |
---|
28 | #TODO: show units |
---|
29 | #TODO: order parameters properly |
---|
30 | |
---|
31 | def __init__(self, parent, *args, **kwargs): |
---|
32 | wx.Dialog.__init__(self, parent, *args, **kwargs) |
---|
33 | self.params = {} |
---|
34 | self.parent = parent |
---|
35 | self.type = None |
---|
36 | self.listeners = [] |
---|
37 | self.parameters = [] |
---|
38 | self.bck = wx.GridBagSizer(5,5) |
---|
39 | self.SetSizer(self.bck) |
---|
40 | |
---|
41 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
42 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
43 | |
---|
44 | # Bindings |
---|
45 | self.parent.Bind(EVT_SLICER, self.onEVT_SLICER) |
---|
46 | self.parent.Bind(EVT_SLICER_PARS, self.onParamChange) |
---|
47 | |
---|
48 | def onEVT_SLICER(self, event): |
---|
49 | """ |
---|
50 | Process EVT_SLICER events |
---|
51 | When the slicer changes, update the panel |
---|
52 | |
---|
53 | @param event: EVT_SLICER event |
---|
54 | """ |
---|
55 | event.Skip() |
---|
56 | if event.obj_class==None: |
---|
57 | self.set_slicer(None, None) |
---|
58 | else: |
---|
59 | self.set_slicer(event.type, event.params) |
---|
60 | |
---|
61 | def set_slicer(self, type, params): |
---|
62 | """ |
---|
63 | Rebuild the panel |
---|
64 | """ |
---|
65 | self.bck.Clear(True) |
---|
66 | self.type = type |
---|
67 | |
---|
68 | if type==None: |
---|
69 | title = wx.StaticText(self, -1, "Right-click on 2D plot for slicer options", style=wx.ALIGN_LEFT) |
---|
70 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
71 | |
---|
72 | else: |
---|
73 | title = wx.StaticText(self, -1, "Slicer Parameters", style=wx.ALIGN_LEFT) |
---|
74 | self.bck.Add(title, (0,0), (1,2), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
75 | ix = 0 |
---|
76 | iy = 0 |
---|
77 | self.parameters = [] |
---|
78 | #params = slicer.get_params() |
---|
79 | keys = params.keys() |
---|
80 | keys.sort() |
---|
81 | |
---|
82 | for item in keys: |
---|
83 | iy += 1 |
---|
84 | ix = 0 |
---|
85 | if not item in ["count","errors"]: |
---|
86 | |
---|
87 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
88 | #self.bck.Add(text, (iy,ix), flag = wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border = 15) |
---|
89 | self.bck.Add(text, (iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
90 | ctl = wx.TextCtrl(self, -1, size=(80,20), style=wx.TE_PROCESS_ENTER) |
---|
91 | |
---|
92 | ctl.SetToolTipString("Modify the value of %s to change the 2D slicer" % item) |
---|
93 | |
---|
94 | ix = 1 |
---|
95 | ctl.SetValue(format_number(str(params[item]))) |
---|
96 | self.Bind(wx.EVT_TEXT_ENTER, self.onTextEnter) |
---|
97 | ctl.Bind(wx.EVT_KILL_FOCUS, self.onTextEnter) |
---|
98 | self.parameters.append([item, ctl]) |
---|
99 | #self.bck.Add(ctl, (iy,ix), flag=wx.TOP|wx.BOTTOM, border = 0) |
---|
100 | self.bck.Add(ctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
101 | |
---|
102 | ix =3 |
---|
103 | self.bck.Add((20,20), (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
104 | else: |
---|
105 | text = wx.StaticText(self, -1, item+ " : ", style=wx.ALIGN_LEFT) |
---|
106 | self.bck.Add(text, (iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
107 | ctl =wx.StaticText(self, -1, format_number(str(params[item])), style=wx.ALIGN_LEFT) |
---|
108 | ix =1 |
---|
109 | self.bck.Add(ctl, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
110 | iy +=1 |
---|
111 | ix = 1 |
---|
112 | self.bck.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
113 | self.bck.Layout() |
---|
114 | self.bck.Fit(self) |
---|
115 | self.parent.GetSizer().Layout() |
---|
116 | |
---|
117 | def onParamChange(self, evt): |
---|
118 | evt.Skip() |
---|
119 | if evt.type == "UPDATE": |
---|
120 | for item in self.parameters: |
---|
121 | if item[0] in evt.params: |
---|
122 | item[1].SetValue("%-5.3g" %evt.params[item[0]]) |
---|
123 | item[1].Refresh() |
---|
124 | |
---|
125 | def onTextEnter(self, evt): |
---|
126 | """ |
---|
127 | Parameters have changed |
---|
128 | """ |
---|
129 | params = {} |
---|
130 | has_error = False |
---|
131 | for item in self.parameters: |
---|
132 | try: |
---|
133 | params[item[0]] = float(item[1].GetValue()) |
---|
134 | item[1].SetBackgroundColour( |
---|
135 | wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) |
---|
136 | item[1].Refresh() |
---|
137 | except: |
---|
138 | has_error = True |
---|
139 | item[1].SetBackgroundColour("pink") |
---|
140 | item[1].Refresh() |
---|
141 | |
---|
142 | if has_error==False: |
---|
143 | # Post parameter event |
---|
144 | event = SlicerParameterEvent(type=self.type, params=params) |
---|
145 | wx.PostEvent(self.parent, event) |
---|
146 | |
---|