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