1 | """ |
---|
2 | Widget to display a 2D map of the detector |
---|
3 | """ |
---|
4 | import wx |
---|
5 | import sys |
---|
6 | from sas.sasgui.guiframe.utils import format_number |
---|
7 | from sas.sasgui.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.get_cmap('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(object): |
---|
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 | self.zmin_ctl.SetBackgroundColour(wx.WHITE) |
---|
114 | self.zmin_ctl.Refresh() |
---|
115 | except: |
---|
116 | flag = False |
---|
117 | wx.PostEvent(self.parent, StatusEvent(status="Enter float value")) |
---|
118 | self.zmin_ctl.SetBackgroundColour("pink") |
---|
119 | self.zmin_ctl.Refresh() |
---|
120 | try: |
---|
121 | value = self.zmax_ctl.GetValue() |
---|
122 | if value and float(value) == 0.0: |
---|
123 | flag = False |
---|
124 | wx.PostEvent(self.parent, |
---|
125 | StatusEvent(status="Enter number greater than zero")) |
---|
126 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
127 | self.zmax_ctl.Refresh() |
---|
128 | else: |
---|
129 | self.zmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
130 | self.zmax_ctl.Refresh() |
---|
131 | except: |
---|
132 | flag = False |
---|
133 | wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value")) |
---|
134 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
135 | self.zmax_ctl.Refresh() |
---|
136 | if flag: |
---|
137 | event.Skip(True) |
---|
138 | |
---|
139 | def setContent(self, xnpts, ynpts, qmax, beam, |
---|
140 | zmin=None, zmax=None, sym=False): |
---|
141 | """ |
---|
142 | received value and displayed them |
---|
143 | |
---|
144 | :param xnpts: the number of point of the x_bins of data |
---|
145 | :param ynpts: the number of point of the y_bins of data |
---|
146 | :param qmax: the maxmimum value of data pixel |
---|
147 | :param beam: the radius of the beam |
---|
148 | :param zmin: the value to get the minimum color |
---|
149 | :param zmax: the value to get the maximum color |
---|
150 | :param sym: |
---|
151 | |
---|
152 | """ |
---|
153 | self.xnpts_ctl.SetLabel(str(format_number(xnpts))) |
---|
154 | self.ynpts_ctl.SetLabel(str(format_number(ynpts))) |
---|
155 | self.qmax_ctl.SetLabel(str(format_number(qmax))) |
---|
156 | self.beam_ctl.SetLabel(str(format_number(beam))) |
---|
157 | if zmin != None: |
---|
158 | self.zmin_ctl.SetValue(str(format_number(zmin))) |
---|
159 | if zmax != None: |
---|
160 | self.zmax_ctl.SetValue(str(format_number(zmax))) |
---|
161 | |
---|
162 | def getContent(self): |
---|
163 | """ |
---|
164 | return event containing value to reset the detector of a given data |
---|
165 | """ |
---|
166 | event = self.Event() |
---|
167 | t_min = self.zmin_ctl.GetValue() |
---|
168 | t_max = self.zmax_ctl.GetValue() |
---|
169 | v_min = None |
---|
170 | v_max = None |
---|
171 | if len(t_min.lstrip()) > 0: |
---|
172 | try: |
---|
173 | v_min = float(t_min) |
---|
174 | except: |
---|
175 | v_min = None |
---|
176 | if len(t_max.lstrip()) > 0: |
---|
177 | try: |
---|
178 | v_max = float(t_max) |
---|
179 | except: |
---|
180 | v_max = None |
---|
181 | event.zmin = v_min |
---|
182 | event.zmax = v_max |
---|
183 | event.cmap = self.cmap |
---|
184 | return event |
---|
185 | |
---|
186 | def __set_properties(self): |
---|
187 | """ |
---|
188 | set proprieties of the dialog window |
---|
189 | """ |
---|
190 | self.SetTitle("2D Color Map") |
---|
191 | self.SetSize((600, 595)) |
---|
192 | |
---|
193 | def __do_layout(self): |
---|
194 | """ |
---|
195 | fill the dialog window . |
---|
196 | """ |
---|
197 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
198 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
199 | sizer_params = wx.GridBagSizer(5, 5) |
---|
200 | sizer_colormap = wx.BoxSizer(wx.VERTICAL) |
---|
201 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
202 | |
---|
203 | iy = 0 |
---|
204 | sizer_params.Add(self.label_xnpts, (iy, 0), (1, 1), |
---|
205 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
206 | sizer_params.Add(self.xnpts_ctl, (iy, 1), (1, 1), |
---|
207 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
208 | iy += 1 |
---|
209 | sizer_params.Add(self.label_ynpts, (iy, 0), (1, 1), |
---|
210 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
211 | sizer_params.Add(self.ynpts_ctl, (iy, 1), (1, 1), |
---|
212 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
213 | iy += 1 |
---|
214 | sizer_params.Add(self.label_qmax, (iy, 0), (1, 1), |
---|
215 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
216 | sizer_params.Add(self.qmax_ctl, (iy, 1), (1, 1), |
---|
217 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
218 | iy += 1 |
---|
219 | sizer_params.Add(self.label_beam, (iy, 0), (1, 1), |
---|
220 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
221 | sizer_params.Add(self.beam_ctl, (iy, 1), (1, 1), |
---|
222 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
223 | iy += 1 |
---|
224 | sizer_params.Add(self.label_zmin, (iy, 0), (1, 1), |
---|
225 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
226 | sizer_params.Add(self.zmin_ctl, (iy, 1), (1, 1), |
---|
227 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
228 | iy += 1 |
---|
229 | sizer_params.Add(self.label_zmax, (iy, 0), (1, 1), |
---|
230 | wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) |
---|
231 | sizer_params.Add(self.zmax_ctl, (iy, 1), (1, 1), |
---|
232 | wx.EXPAND | wx.ADJUST_MINSIZE, 0) |
---|
233 | iy += 1 |
---|
234 | self.fig = mpl.figure.Figure(dpi=self.dpi, figsize=(4, 1)) |
---|
235 | self.ax1 = self.fig.add_axes([0.05, 0.65, 0.9, 0.15]) |
---|
236 | self.norm = mpl.colors.Normalize(vmin=0, vmax=100) |
---|
237 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
238 | norm=self.norm, |
---|
239 | orientation='horizontal') |
---|
240 | self.cb1.set_label('Detector Colors') |
---|
241 | self.canvas = Canvas(self, -1, self.fig) |
---|
242 | sizer_colormap.Add(self.canvas, 0, wx.LEFT | wx.EXPAND, 5) |
---|
243 | self.cmap_selector = wx.ComboBox(self, -1) |
---|
244 | self.cmap_selector.SetValue(str(self.cmap.name)) |
---|
245 | maps = sorted(m for m in pylab.cm.datad if not m.endswith("_r")) |
---|
246 | |
---|
247 | for i, m in enumerate(maps): |
---|
248 | self.cmap_selector.Append(str(m), pylab.get_cmap(m)) |
---|
249 | |
---|
250 | wx.EVT_COMBOBOX(self.cmap_selector, -1, self._on_select_cmap) |
---|
251 | sizer_selection.Add(wx.StaticText(self, -1, "Select Cmap: "), 0, |
---|
252 | wx.LEFT | wx.ADJUST_MINSIZE, 5) |
---|
253 | sizer_selection.Add(self.cmap_selector, 0, wx.EXPAND | wx.ALL, 10) |
---|
254 | sizer_main.Add(sizer_params, 0, wx.EXPAND | wx.ALL, 5) |
---|
255 | sizer_main.Add(sizer_selection, 0, wx.EXPAND | wx.ALL, 5) |
---|
256 | note = " Note: This is one time option. " + \ |
---|
257 | "It will be reset on updating the image." |
---|
258 | note_txt = wx.StaticText(self, -1, note) |
---|
259 | sizer_main.Add(note_txt, 0, wx.EXPAND | wx.ALL, 5) |
---|
260 | sizer_main.Add(sizer_colormap, 1, wx.EXPAND | wx.ALL, 5) |
---|
261 | sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0) |
---|
262 | sizer_button.Add(self.button_reset, 0, wx.LEFT | wx.ADJUST_MINSIZE, 100) |
---|
263 | sizer_button.Add(self.button_ok, 0, wx.LEFT | wx.ADJUST_MINSIZE, 10) |
---|
264 | sizer_button.Add(self.button_cancel, 0, |
---|
265 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) |
---|
266 | sizer_main.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) |
---|
267 | self.SetAutoLayout(True) |
---|
268 | self.SetSizer(sizer_main) |
---|
269 | self.Layout() |
---|
270 | self.Centre() |
---|
271 | # end wxGlade |
---|
272 | |
---|
273 | def _on_select_cmap(self, event): |
---|
274 | """ |
---|
275 | display a new cmap |
---|
276 | """ |
---|
277 | cmap_name = self.cmap_selector.GetCurrentSelection() |
---|
278 | current_cmap = self.cmap_selector.GetClientData(cmap_name) |
---|
279 | self.cmap = current_cmap |
---|
280 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
281 | norm=self.norm, orientation='horizontal') |
---|
282 | self.canvas.draw() |
---|