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