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