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