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