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