[ea290ee] | 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 |
---|
[8dfdd20] | 8 | import sys |
---|
[ea290ee] | 9 | from sans.guiframe.utils import format_number |
---|
[8dfdd20] | 10 | from sans.guicomm.events import StatusEvent ,NewPlotEvent |
---|
[eba08f1a] | 11 | |
---|
[8dfdd20] | 12 | import matplotlib |
---|
| 13 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas |
---|
| 14 | from matplotlib import pyplot, mpl, pylab |
---|
[1f1cacf] | 15 | #FONT size |
---|
| 16 | if sys.platform.count("win32")>0: |
---|
| 17 | FONT_VARIANT = 0 |
---|
| 18 | else: |
---|
| 19 | FONT_VARIANT = 1 |
---|
| 20 | |
---|
[8dfdd20] | 21 | DEFAULT_CMAP= pylab.cm.jet |
---|
[d955bf19] | 22 | |
---|
[ea290ee] | 23 | class DetectorDialog(wx.Dialog): |
---|
| 24 | """ |
---|
[d955bf19] | 25 | Dialog box to let the user edit detector settings |
---|
[ea290ee] | 26 | """ |
---|
| 27 | |
---|
[8dfdd20] | 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): |
---|
[d955bf19] | 30 | """ |
---|
| 31 | """ |
---|
[ea290ee] | 32 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
---|
[9a585d0] | 33 | wx.Dialog.__init__(self,parent,id=1, *args, **kwds) |
---|
[1f1cacf] | 34 | |
---|
| 35 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[9a585d0] | 36 | self.parent=base |
---|
[8dfdd20] | 37 | self.dpi = dpi |
---|
[9c0fe9a5] | 38 | self.cmap = cmap |
---|
[8dfdd20] | 39 | |
---|
| 40 | self.reset_zmin_ctl= reset_zmin_ctl |
---|
| 41 | self.reset_zmax_ctl= reset_zmax_ctl |
---|
| 42 | |
---|
[ea290ee] | 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)) |
---|
[8dfdd20] | 56 | self.zmin_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[ea290ee] | 57 | self.zmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
[8dfdd20] | 58 | self.zmax_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus) |
---|
[eba08f1a] | 59 | |
---|
[ea290ee] | 60 | self.static_line_3 = wx.StaticLine(self, -1) |
---|
| 61 | |
---|
| 62 | self.button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
[8dfdd20] | 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") |
---|
[ea290ee] | 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: |
---|
[d955bf19] | 74 | """ |
---|
| 75 | """ |
---|
[ea290ee] | 76 | xnpts = 0 |
---|
| 77 | ynpts = 0 |
---|
| 78 | qpax = 0 |
---|
| 79 | beam = 0 |
---|
| 80 | zmin = 0 |
---|
| 81 | zmax = 0 |
---|
[8dfdd20] | 82 | cmap= None |
---|
[ea290ee] | 83 | sym4 = False |
---|
[8dfdd20] | 84 | |
---|
| 85 | def onSetFocus(self, event): |
---|
| 86 | """ |
---|
[d955bf19] | 87 | Highlight the txtcrtl |
---|
[8dfdd20] | 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 | """ |
---|
[d955bf19] | 97 | reset detector info |
---|
[8dfdd20] | 98 | """ |
---|
| 99 | try: |
---|
[7526ad4] | 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)) |
---|
[8dfdd20] | 108 | self.cmap = DEFAULT_CMAP |
---|
[7526ad4] | 109 | self.cmap_selector.SetStringSelection("jet") |
---|
[8dfdd20] | 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 | |
---|
[ea290ee] | 115 | |
---|
| 116 | def checkValues(self, event): |
---|
[eba08f1a] | 117 | """ |
---|
[d955bf19] | 118 | Check the valitidity of zmin and zmax value |
---|
| 119 | zmax should be a float and zmin less than zmax |
---|
[eba08f1a] | 120 | """ |
---|
[ea290ee] | 121 | flag = True |
---|
| 122 | try: |
---|
| 123 | value=self.zmin_ctl.GetValue() |
---|
| 124 | if value and float( value)==0.0: |
---|
| 125 | flag = False |
---|
[9a585d0] | 126 | wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero")) |
---|
[ea290ee] | 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 |
---|
[9a585d0] | 134 | wx.PostEvent(self.parent, StatusEvent(status="Enter float value")) |
---|
[ea290ee] | 135 | self.zmin_ctl.SetBackgroundColour("pink") |
---|
| 136 | self.zmin_ctl.Refresh() |
---|
| 137 | try: |
---|
| 138 | value=self.zmax_ctl.GetValue() |
---|
[ed7384d] | 139 | if value and float(value)==0.0: |
---|
[ea290ee] | 140 | flag = False |
---|
[9a585d0] | 141 | wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero")) |
---|
[ea290ee] | 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 |
---|
[9a585d0] | 149 | wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value")) |
---|
[ea290ee] | 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): |
---|
[eba08f1a] | 157 | """ |
---|
[d955bf19] | 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 | |
---|
[eba08f1a] | 168 | """ |
---|
[80a7f77] | 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))) |
---|
[8dfdd20] | 173 | |
---|
[ea290ee] | 174 | if zmin !=None: |
---|
[80a7f77] | 175 | self.zmin_ctl.SetValue(str(format_number(zmin))) |
---|
[ea290ee] | 176 | if zmax !=None: |
---|
[80a7f77] | 177 | self.zmax_ctl.SetValue(str(format_number(zmax))) |
---|
[ea290ee] | 178 | |
---|
| 179 | def getContent(self): |
---|
[eba08f1a] | 180 | """ |
---|
[d955bf19] | 181 | return event containing value to reset the detector of a given data |
---|
[eba08f1a] | 182 | """ |
---|
[ea290ee] | 183 | event = self.Event() |
---|
[eba08f1a] | 184 | |
---|
[ea290ee] | 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 |
---|
[8dfdd20] | 204 | event.cmap= self.cmap |
---|
[ea290ee] | 205 | |
---|
| 206 | return event |
---|
| 207 | |
---|
| 208 | def __set_properties(self): |
---|
[eba08f1a] | 209 | """ |
---|
[d955bf19] | 210 | set proprieties of the dialog window |
---|
[eba08f1a] | 211 | """ |
---|
[ea290ee] | 212 | self.SetTitle("Detector parameters") |
---|
| 213 | self.SetSize((600, 595)) |
---|
| 214 | |
---|
[eba08f1a] | 215 | |
---|
[ea290ee] | 216 | def __do_layout(self): |
---|
[eba08f1a] | 217 | """ |
---|
[d955bf19] | 218 | fill the dialog window . |
---|
[eba08f1a] | 219 | """ |
---|
[ea290ee] | 220 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
| 221 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 222 | sizer_params = wx.GridBagSizer(5,5) |
---|
[8dfdd20] | 223 | sizer_colormap = wx.BoxSizer(wx.VERTICAL) |
---|
| 224 | sizer_selection= wx.BoxSizer(wx.HORIZONTAL) |
---|
| 225 | |
---|
[ea290ee] | 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 |
---|
[8dfdd20] | 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]) |
---|
[d955bf19] | 249 | |
---|
[8dfdd20] | 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")) |
---|
[eba08f1a] | 261 | |
---|
[8dfdd20] | 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 | |
---|
[ea290ee] | 270 | sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10) |
---|
[8dfdd20] | 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) |
---|
[ea290ee] | 274 | sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0) |
---|
| 275 | |
---|
[8dfdd20] | 276 | sizer_button.Add(self.button_reset,0, wx.LEFT|wx.ADJUST_MINSIZE, 100) |
---|
[ea290ee] | 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) |
---|
[d955bf19] | 279 | |
---|
[ea290ee] | 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 |
---|
[8dfdd20] | 286 | |
---|
| 287 | def _on_select_cmap(self, event): |
---|
| 288 | """ |
---|
[d955bf19] | 289 | display a new cmap |
---|
[8dfdd20] | 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() |
---|
[ea290ee] | 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) |
---|
[8dfdd20] | 308 | dialog.setContent(xnpts=128,ynpts=128, qmax=20, |
---|
| 309 | beam=20,zmin=2,zmax=60, sym=False) |
---|
[ea290ee] | 310 | print dialog.ShowModal() |
---|
| 311 | evt = dialog.getContent() |
---|
[8dfdd20] | 312 | if hasattr(evt,"npts"): |
---|
| 313 | print "number of point: ",evt.npts |
---|
| 314 | if hasattr(evt,"qmax"): |
---|
| 315 | print "qmax: ",evt.qmax |
---|
[ea290ee] | 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 ##################################################### |
---|