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