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 | zmin= self.reset_zmin_ctl |
---|
93 | zmax= self.reset_zmax_ctl |
---|
94 | if zmin==None: |
---|
95 | zmin= "" |
---|
96 | if zmax==None: |
---|
97 | zmax= "" |
---|
98 | self.zmin_ctl.SetValue(str(zmin)) |
---|
99 | self.zmax_ctl.SetValue(str(zmax)) |
---|
100 | self.cmap = DEFAULT_CMAP |
---|
101 | self.cmap_selector.SetStringSelection("jet") |
---|
102 | self._on_select_cmap(event=None) |
---|
103 | except: |
---|
104 | msg ="error occurs while resetting Detector: %s"%sys.exc_value |
---|
105 | wx.PostEvent(self.parent,StatusEvent(status= msg )) |
---|
106 | |
---|
107 | |
---|
108 | def checkValues(self, event): |
---|
109 | """ |
---|
110 | Check the valitidity of zmin and zmax value |
---|
111 | zmax should be a float and zmin less than zmax |
---|
112 | """ |
---|
113 | flag = True |
---|
114 | try: |
---|
115 | value=self.zmin_ctl.GetValue() |
---|
116 | if value and float( value)==0.0: |
---|
117 | flag = False |
---|
118 | wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero")) |
---|
119 | self.zmin_ctl.SetBackgroundColour("pink") |
---|
120 | self.zmin_ctl.Refresh() |
---|
121 | else: |
---|
122 | self.zmin_ctl.SetBackgroundColour(wx.WHITE) |
---|
123 | self.zmin_ctl.Refresh() |
---|
124 | except: |
---|
125 | flag = False |
---|
126 | wx.PostEvent(self.parent, StatusEvent(status="Enter float value")) |
---|
127 | self.zmin_ctl.SetBackgroundColour("pink") |
---|
128 | self.zmin_ctl.Refresh() |
---|
129 | try: |
---|
130 | value=self.zmax_ctl.GetValue() |
---|
131 | if value and float(value)==0.0: |
---|
132 | flag = False |
---|
133 | wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero")) |
---|
134 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
135 | self.zmax_ctl.Refresh() |
---|
136 | else: |
---|
137 | self.zmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
138 | self.zmax_ctl.Refresh() |
---|
139 | except: |
---|
140 | flag = False |
---|
141 | wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value")) |
---|
142 | self.zmax_ctl.SetBackgroundColour("pink") |
---|
143 | self.zmax_ctl.Refresh() |
---|
144 | |
---|
145 | if flag: |
---|
146 | event.Skip(True) |
---|
147 | |
---|
148 | def setContent(self, xnpts,ynpts, qmax, beam,zmin=None,zmax=None, sym=False): |
---|
149 | """ |
---|
150 | received value and displayed them |
---|
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 | self.xnpts_ctl.SetLabel(str(float(xnpts))) |
---|
160 | self.ynpts_ctl.SetLabel(str(float(ynpts))) |
---|
161 | self.qmax_ctl.SetLabel(str(float(qmax))) |
---|
162 | self.beam_ctl.SetLabel(str(float(beam))) |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | if zmin !=None: |
---|
167 | self.zmin_ctl.SetValue(str(float(zmin))) |
---|
168 | if zmax !=None: |
---|
169 | self.zmax_ctl.SetValue(str(float(zmax))) |
---|
170 | |
---|
171 | def getContent(self): |
---|
172 | """ |
---|
173 | @return event containing value to reset the detector of a given data |
---|
174 | """ |
---|
175 | event = self.Event() |
---|
176 | |
---|
177 | t_min = self.zmin_ctl.GetValue() |
---|
178 | t_max = self.zmax_ctl.GetValue() |
---|
179 | v_min = None |
---|
180 | v_max = None |
---|
181 | |
---|
182 | if len(t_min.lstrip())>0: |
---|
183 | try: |
---|
184 | v_min = float(t_min) |
---|
185 | except: |
---|
186 | v_min = None |
---|
187 | |
---|
188 | if len(t_max.lstrip())>0: |
---|
189 | try: |
---|
190 | v_max = float(t_max) |
---|
191 | except: |
---|
192 | v_max = None |
---|
193 | |
---|
194 | event.zmin = v_min |
---|
195 | event.zmax = v_max |
---|
196 | event.cmap= self.cmap |
---|
197 | |
---|
198 | return event |
---|
199 | |
---|
200 | def __set_properties(self): |
---|
201 | """ |
---|
202 | set proprieties of the dialog window |
---|
203 | """ |
---|
204 | self.SetTitle("Detector parameters") |
---|
205 | self.SetSize((600, 595)) |
---|
206 | |
---|
207 | |
---|
208 | def __do_layout(self): |
---|
209 | """ |
---|
210 | fill the dialog window . |
---|
211 | """ |
---|
212 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
213 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
214 | sizer_params = wx.GridBagSizer(5,5) |
---|
215 | sizer_colormap = wx.BoxSizer(wx.VERTICAL) |
---|
216 | sizer_selection= wx.BoxSizer(wx.HORIZONTAL) |
---|
217 | |
---|
218 | |
---|
219 | iy = 0 |
---|
220 | sizer_params.Add(self.label_xnpts, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
221 | sizer_params.Add(self.xnpts_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
222 | iy += 1 |
---|
223 | sizer_params.Add(self.label_ynpts, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
224 | sizer_params.Add(self.ynpts_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
225 | iy += 1 |
---|
226 | sizer_params.Add(self.label_qmax, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
227 | sizer_params.Add(self.qmax_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
228 | iy += 1 |
---|
229 | sizer_params.Add(self.label_beam, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
230 | sizer_params.Add(self.beam_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
231 | iy += 1 |
---|
232 | sizer_params.Add(self.label_zmin, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
233 | sizer_params.Add(self.zmin_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
234 | iy += 1 |
---|
235 | sizer_params.Add(self.label_zmax, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
236 | sizer_params.Add(self.zmax_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
237 | iy += 1 |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | self.fig = mpl.figure.Figure(dpi=self.dpi, figsize=(4,1)) |
---|
242 | |
---|
243 | self.ax1 = self.fig.add_axes([0.05, 0.65, 0.9, 0.15]) |
---|
244 | |
---|
245 | |
---|
246 | self.norm = mpl.colors.Normalize(vmin=0, vmax=100) |
---|
247 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
248 | norm= self.norm, |
---|
249 | orientation='horizontal') |
---|
250 | self.cb1.set_label('Detector Colors') |
---|
251 | self.canvas = Canvas(self, -1, self.fig) |
---|
252 | sizer_colormap.Add(self.canvas,0, wx.LEFT | wx.EXPAND,5) |
---|
253 | |
---|
254 | self.cmap_selector = wx.ComboBox(self, -1) |
---|
255 | self.cmap_selector.SetValue(str(self.cmap.name)) |
---|
256 | maps = sorted(m for m in pylab.cm.datad if not m.endswith("_r")) |
---|
257 | |
---|
258 | for i,m in enumerate(maps): |
---|
259 | |
---|
260 | self.cmap_selector.Append(str(m), pylab.get_cmap(m)) |
---|
261 | |
---|
262 | wx.EVT_COMBOBOX(self.cmap_selector,-1, self._on_select_cmap) |
---|
263 | sizer_selection.Add(wx.StaticText(self,-1,"Select Cmap: "),0, wx.LEFT|wx.ADJUST_MINSIZE,5) |
---|
264 | sizer_selection.Add(self.cmap_selector, 0, wx.EXPAND|wx.ALL, 10) |
---|
265 | |
---|
266 | sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10) |
---|
267 | |
---|
268 | sizer_main.Add(sizer_selection, 0, wx.EXPAND|wx.ALL, 10) |
---|
269 | sizer_main.Add(sizer_colormap, 1, wx.EXPAND|wx.ALL, 10) |
---|
270 | sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0) |
---|
271 | |
---|
272 | |
---|
273 | sizer_button.Add(self.button_reset,0, wx.LEFT|wx.ADJUST_MINSIZE, 100) |
---|
274 | sizer_button.Add(self.button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
275 | sizer_button.Add(self.button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
276 | |
---|
277 | |
---|
278 | sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
279 | self.SetAutoLayout(True) |
---|
280 | self.SetSizer(sizer_main) |
---|
281 | self.Layout() |
---|
282 | self.Centre() |
---|
283 | # end wxGlade |
---|
284 | |
---|
285 | def _on_select_cmap(self, event): |
---|
286 | """ |
---|
287 | display a new cmap |
---|
288 | """ |
---|
289 | cmap_name= self.cmap_selector.GetCurrentSelection() |
---|
290 | current_cmap= self.cmap_selector.GetClientData( cmap_name ) |
---|
291 | self.cmap= current_cmap |
---|
292 | self.cb1 = mpl.colorbar.ColorbarBase(self.ax1, cmap=self.cmap, |
---|
293 | norm= self.norm, |
---|
294 | orientation='horizontal') |
---|
295 | self.canvas.draw() |
---|
296 | |
---|
297 | |
---|
298 | # end of class DialogAbout |
---|
299 | |
---|
300 | ##### testing code ############################################################ |
---|
301 | class MyApp(wx.App): |
---|
302 | def OnInit(self): |
---|
303 | wx.InitAllImageHandlers() |
---|
304 | dialog = DetectorDialog(None, -1, "") |
---|
305 | self.SetTopWindow(dialog) |
---|
306 | dialog.setContent(xnpts=128,ynpts=128, qmax=20, |
---|
307 | beam=20,zmin=2,zmax=60, sym=False) |
---|
308 | print dialog.ShowModal() |
---|
309 | evt = dialog.getContent() |
---|
310 | if hasattr(evt,"npts"): |
---|
311 | print "number of point: ",evt.npts |
---|
312 | if hasattr(evt,"qmax"): |
---|
313 | print "qmax: ",evt.qmax |
---|
314 | dialog.Destroy() |
---|
315 | return 1 |
---|
316 | |
---|
317 | # end of class MyApp |
---|
318 | |
---|
319 | if __name__ == "__main__": |
---|
320 | app = MyApp(0) |
---|
321 | app.MainLoop() |
---|
322 | |
---|
323 | ##### end of testing code ##################################################### |
---|