1 | """ |
---|
2 | Widget to display a 2D map of the detector |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import sys |
---|
6 | from sans.guiframe.utils import format_number |
---|
7 | from sans.guiframe.events import StatusEvent |
---|
8 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas |
---|
9 | from matplotlib import mpl |
---|
10 | from matplotlib import pylab |
---|
11 | #FONT size |
---|
12 | if sys.platform.count("win32") > 0: |
---|
13 | FONT_VARIANT = 0 |
---|
14 | else: |
---|
15 | FONT_VARIANT = 1 |
---|
16 | |
---|
17 | DEFAULT_CMAP = pylab.cm.jet |
---|
18 | |
---|
19 | class DetectorDialog(wx.Dialog): |
---|
20 | """ |
---|
21 | Dialog box to let the user edit detector settings |
---|
22 | """ |
---|
23 | |
---|
24 | def __init__(self, parent, id=1, base=None, dpi=None, |
---|
25 | cmap=DEFAULT_CMAP, reset_zmin_ctl = None, |
---|
26 | reset_zmax_ctl = None, *args, **kwds): |
---|
27 | """ |
---|
28 | """ |
---|
29 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
30 | wx.Dialog.__init__(self, parent, id=1, *args, **kwds) |
---|
31 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
32 | self.parent = base |
---|
33 | self.dpi = dpi |
---|
34 | self.cmap = cmap |
---|
35 | self.reset_zmin_ctl = reset_zmin_ctl |
---|
36 | self.reset_zmax_ctl = reset_zmax_ctl |
---|
37 | self.label_xnpts = wx.StaticText(self, -1, "Detector width in pixels") |
---|
38 | self.label_ynpts = wx.StaticText(self, -1, "Detector Height in pixels") |
---|
39 | self.label_qmax = wx.StaticText(self, -1, "Q max") |
---|
40 | self.label_zmin = wx.StaticText(self, -1, |
---|
41 | "Min amplitude for color map (optional)") |
---|
42 | self.label_zmax = wx.StaticText(self, -1, |
---|
43 | "Max amplitude for color map (optional)") |
---|
44 | self.label_beam = wx.StaticText(self, -1, |
---|
45 | "Beam stop radius in units of q") |
---|
46 | self.xnpts_ctl = wx.StaticText(self, -1, "") |
---|
47 | self.ynpts_ctl = wx.StaticText(self, -1, "") |
---|
48 | self.qmax_ctl = wx.StaticText(self, -1, "") |
---|
49 | self.beam_ctl = wx.StaticText(self, -1, "") |
---|
50 | self.zmin_ctl = wx.TextCtrl(self, -1, size=(60, 20)) |
---|
51 | self.zmin_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
52 | self.zmax_ctl = wx.TextCtrl(self, -1, size=(60, 20)) |
---|
53 | self.zmax_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
54 | self.static_line_3 = wx.StaticLine(self, -1) |
---|
55 | self.button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
56 | self.button_reset = wx.Button(self, wx.NewId(), "Reset") |
---|
57 | self.Bind(wx.EVT_BUTTON, self.resetValues, self.button_reset) |
---|
58 | self.button_OK = wx.Button(self, wx.ID_OK, "OK") |
---|
59 | self.Bind(wx.EVT_BUTTON, self.checkValues, self.button_OK) |
---|
60 | self.__set_properties() |
---|
61 | self.__do_layout() |
---|
62 | self.Fit() |
---|
63 | |
---|
64 | class Event: |
---|
65 | """ |
---|
66 | """ |
---|
67 | xnpts = 0 |
---|
68 | ynpts = 0 |
---|
69 | qpax = 0 |
---|
70 | beam = 0 |
---|
71 | zmin = 0 |
---|
72 | zmax = 0 |
---|
73 | cmap = None |
---|
74 | sym4 = False |
---|
75 | |
---|
76 | def onSetFocus(self, event): |
---|
77 | """ |
---|
78 | Highlight the txtcrtl |
---|
79 | """ |
---|
80 | # Get a handle to the TextCtrl |
---|
81 | widget = event.GetEventObject() |
---|
82 | # Select the whole control, after this event resolves |
---|
83 | wx.CallAfter(widget.SetSelection, -1, -1) |
---|
84 | |
---|
85 | def resetValues(self, event): |
---|
86 | """ |
---|
87 | reset detector info |
---|
88 | """ |
---|
89 | try: |
---|
90 | zmin = self.reset_zmin_ctl |
---|
91 | zmax = self.reset_zmax_ctl |
---|
92 | if zmin == None: |
---|
93 | zmin = "" |
---|
94 | if zmax == None: |
---|
95 | zmax = "" |
---|
96 | self.zmin_ctl.SetValue(str(zmin)) |
---|
97 | self.zmax_ctl.SetValue(str(zmax)) |
---|
98 | self.cmap = DEFAULT_CMAP |
---|
99 | self.cmap_selector.SetStringSelection("jet") |
---|
100 | self._on_select_cmap(event=None) |
---|
101 | except: |
---|
102 | msg = "error occurs while resetting Detector: %s" % sys.exc_value |
---|
103 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
104 | |
---|
105 | def checkValues(self, event): |
---|
106 | """ |
---|
107 | Check the valitidity of zmin and zmax value |
---|
108 | zmax should be a float and zmin less than zmax |
---|
109 | """ |
---|
110 | flag = True |
---|
111 | try: |
---|
112 | value = self.zmin_ctl.GetValue() |
---|
113 | #if value and float( value) == 0.0: |
---|
114 | # flag = False |
---|
115 | # wx.PostEvent(self.parent, |
---|
116 | # StatusEvent(status="Enter number greater than zero")) |
---|
117 | # self.zmin_ctl.SetBackgroundColour("pink") |
---|
118 | # self.zmin_ctl.Refresh() |
---|
119 | #else: |
---|
120 | self.zmin_ctl.SetBackgroundColour(wx.WHITE) |
---|
121 | self.zmin_ctl.Refresh() |
---|
122 | except: |
---|
123 | flag = False |
---|
124 | wx.PostEvent(self.parent, StatusEvent(status="Enter float value")) |
---|
125 | self.zmin_ctl.SetBackgroundColour("pink") |
---|
126 | self.zmin_ctl.Refresh() |
---|
127 | try: |
---|
128 | value = self.zmax_ctl.GetValue() |
---|
129 | if value and float(value) == 0.0: |
---|
130 | flag = False |
---|
131 | wx.PostEvent(self.parent, |
---|
132 | StatusEvent(status="Enter number greater than zero")) |
---|
133 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
134 | self.zmax_ctl.Refresh() |
---|
135 | else: |
---|
136 | self.zmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
137 | self.zmax_ctl.Refresh() |
---|
138 | except: |
---|
139 | flag = False |
---|
140 | wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value")) |
---|
141 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
142 | self.zmax_ctl.Refresh() |
---|
143 | if flag: |
---|
144 | event.Skip(True) |
---|
145 | |
---|
146 | def setContent(self, xnpts, ynpts, qmax, beam, |
---|
147 | zmin=None, zmax=None, sym=False): |
---|
148 | """ |
---|
149 | received value and displayed them |
---|
150 | |
---|
151 | :param xnpts: the number of point of the x_bins of data |
---|
152 | :param ynpts: the number of point of the y_bins of data |
---|
153 | :param qmax: the maxmimum value of data pixel |
---|
154 | :param beam: the radius of the beam |
---|
155 | :param zmin: the value to get the minimum color |
---|
156 | :param zmax: the value to get the maximum color |
---|
157 | :param sym: |
---|
158 | |
---|
159 | """ |
---|
160 | self.xnpts_ctl.SetLabel(str(format_number(xnpts))) |
---|
161 | self.ynpts_ctl.SetLabel(str(format_number(ynpts))) |
---|
162 | self.qmax_ctl.SetLabel(str(format_number(qmax))) |
---|
163 | self.beam_ctl.SetLabel(str(format_number(beam))) |
---|
164 | if zmin != None: |
---|
165 | self.zmin_ctl.SetValue(str(format_number(zmin))) |
---|
166 | if zmax != None: |
---|
167 | self.zmax_ctl.SetValue(str(format_number(zmax))) |
---|
168 | |
---|
169 | def getContent(self): |
---|
170 | """ |
---|
171 | return event containing value to reset the detector of a given data |
---|
172 | """ |
---|
173 | event = self.Event() |
---|
174 | t_min = self.zmin_ctl.GetValue() |
---|
175 | t_max = self.zmax_ctl.GetValue() |
---|
176 | v_min = None |
---|
177 | v_max = None |
---|
178 | if len(t_min.lstrip()) > 0: |
---|
179 | try: |
---|
180 | v_min = float(t_min) |
---|
181 | except: |
---|
182 | v_min = None |
---|
183 | if len(t_max.lstrip()) > 0: |
---|
184 | try: |
---|
185 | v_max = float(t_max) |
---|
186 | except: |
---|
187 | v_max = None |
---|
188 | event.zmin = v_min |
---|
189 | event.zmax = v_max |
---|
190 | event.cmap = self.cmap |
---|
191 | return event |
---|
192 | |
---|
193 | def __set_properties(self): |
---|
194 | """ |
---|
195 | set proprieties of the dialog window |
---|
196 | """ |
---|
197 | self.SetTitle("2D Color Map") |
---|
198 | self.SetSize((600, 595)) |
---|
199 | |
---|
200 | def __do_layout(self): |
---|
201 | """ |
---|
202 | fill the dialog window . |
---|
203 | """ |
---|
204 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
205 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
206 | sizer_params = wx.GridBagSizer(5, 5) |
---|
207 | sizer_colormap = wx.BoxSizer(wx.VERTICAL) |
---|
208 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
209 | |
---|
210 | iy = 0 |
---|
211 | sizer_params.Add(self.label_xnpts, (iy, 0), (1, 1), |
---|
212 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
213 | sizer_params.Add(self.xnpts_ctl, (iy, 1), (1, 1), |
---|
214 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
215 | iy += 1 |
---|
216 | sizer_params.Add(self.label_ynpts, (iy, 0), (1, 1), |
---|
217 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
218 | sizer_params.Add(self.ynpts_ctl, (iy, 1), (1, 1), |
---|
219 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
220 | iy += 1 |
---|
221 | sizer_params.Add(self.label_qmax, (iy, 0), (1, 1), |
---|
222 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
223 | sizer_params.Add(self.qmax_ctl, (iy, 1), (1, 1), |
---|
224 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
225 | iy += 1 |
---|
226 | sizer_params.Add(self.label_beam, (iy, 0), (1, 1), |
---|
227 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
228 | sizer_params.Add(self.beam_ctl, (iy, 1), (1, 1), |
---|
229 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
230 | iy += 1 |
---|
231 | sizer_params.Add(self.label_zmin, (iy, 0), (1, 1), |
---|
232 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
233 | sizer_params.Add(self.zmin_ctl, (iy, 1), (1, 1), |
---|
234 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
235 | iy += 1 |
---|
236 | sizer_params.Add(self.label_zmax, (iy, 0), (1, 1), |
---|
237 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
238 | sizer_params.Add(self.zmax_ctl, (iy, 1), (1, 1), |
---|
239 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
240 | iy += 1 |
---|
241 | self.fig = mpl.figure.Figure(dpi=self.dpi, figsize=(4, 1)) |
---|
242 | self.ax1 = self.fig.add_axes([0.05, 0.65, 0.9, 0.15]) |
---|
243 | self.norm = mpl.colors.Normalize(vmin=0, vmax=100) |
---|
244 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
245 | norm= self.norm, |
---|
246 | orientation='horizontal') |
---|
247 | self.cb1.set_label('Detector Colors') |
---|
248 | self.canvas = Canvas(self, -1, self.fig) |
---|
249 | sizer_colormap.Add(self.canvas, 0, wx.LEFT | wx.EXPAND,5) |
---|
250 | self.cmap_selector = wx.ComboBox(self, -1) |
---|
251 | self.cmap_selector.SetValue(str(self.cmap.name)) |
---|
252 | maps = sorted(m for m in pylab.cm.datad if not m.endswith("_r")) |
---|
253 | |
---|
254 | for i, m in enumerate(maps): |
---|
255 | self.cmap_selector.Append(str(m), pylab.get_cmap(m)) |
---|
256 | |
---|
257 | wx.EVT_COMBOBOX(self.cmap_selector, -1, self._on_select_cmap) |
---|
258 | sizer_selection.Add(wx.StaticText(self, -1, "Select Cmap: "), 0, |
---|
259 | wx.LEFT|wx.ADJUST_MINSIZE, 5) |
---|
260 | sizer_selection.Add(self.cmap_selector, 0, wx.EXPAND|wx.ALL, 10) |
---|
261 | sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 5) |
---|
262 | sizer_main.Add(sizer_selection, 0, wx.EXPAND|wx.ALL, 5) |
---|
263 | note = " Note: This is one time option. " +\ |
---|
264 | "It will be reset on updating the image." |
---|
265 | note_txt = wx.StaticText(self, -1, note) |
---|
266 | sizer_main.Add(note_txt, 0, wx.EXPAND|wx.ALL, 5) |
---|
267 | sizer_main.Add(sizer_colormap, 1, wx.EXPAND|wx.ALL, 5) |
---|
268 | sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0) |
---|
269 | sizer_button.Add(self.button_reset, 0, wx.LEFT|wx.ADJUST_MINSIZE, 100) |
---|
270 | sizer_button.Add(self.button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
271 | sizer_button.Add(self.button_Cancel, 0, |
---|
272 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
273 | sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
274 | self.SetAutoLayout(True) |
---|
275 | self.SetSizer(sizer_main) |
---|
276 | self.Layout() |
---|
277 | self.Centre() |
---|
278 | # end wxGlade |
---|
279 | |
---|
280 | def _on_select_cmap(self, event): |
---|
281 | """ |
---|
282 | display a new cmap |
---|
283 | """ |
---|
284 | cmap_name = self.cmap_selector.GetCurrentSelection() |
---|
285 | current_cmap = self.cmap_selector.GetClientData(cmap_name) |
---|
286 | self.cmap = current_cmap |
---|
287 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
288 | norm=self.norm, orientation='horizontal') |
---|
289 | self.canvas.draw() |
---|