source: sasview/guiframe/local_perspectives/plotting/detector_dialog.py @ 095ab1b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 095ab1b was 7526ad4, checked in by Gervaise Alina <gervyh@…>, 15 years ago

detector fix for reset button

  • Property mode set to 100644
File size: 11.8 KB
RevLine 
[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
7import wx
[8dfdd20]8import sys
[ea290ee]9from sans.guiframe.utils import format_number
[8dfdd20]10from sans.guicomm.events import StatusEvent ,NewPlotEvent
[eba08f1a]11
[8dfdd20]12import matplotlib 
13from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
14from matplotlib import pyplot, mpl, pylab
[eba08f1a]15
[8dfdd20]16DEFAULT_CMAP= pylab.cm.jet
[ea290ee]17class DetectorDialog(wx.Dialog):
18    """
19        Dialog box to let the user edit detector settings
20    """
21   
[8dfdd20]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):
[ea290ee]24
25        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
[9a585d0]26        wx.Dialog.__init__(self,parent,id=1, *args, **kwds)
[eba08f1a]27   
[9a585d0]28        self.parent=base
[8dfdd20]29        self.dpi = dpi
[9c0fe9a5]30        self.cmap = cmap
[8dfdd20]31
32        self.reset_zmin_ctl= reset_zmin_ctl
33        self.reset_zmax_ctl= reset_zmax_ctl
34           
[ea290ee]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))
[8dfdd20]48        self.zmin_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[ea290ee]49        self.zmax_ctl = wx.TextCtrl(self, -1, size=(60,20))
[8dfdd20]50        self.zmax_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
[eba08f1a]51   
[ea290ee]52        self.static_line_3 = wx.StaticLine(self, -1)
53       
54        self.button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
[8dfdd20]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")
[ea290ee]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
[8dfdd20]72        cmap= None
[ea290ee]73        sym4 = False
[8dfdd20]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:
[7526ad4]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))
[8dfdd20]100            self.cmap = DEFAULT_CMAP
[7526ad4]101            self.cmap_selector.SetStringSelection("jet")
[8dfdd20]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       
[ea290ee]107       
108    def checkValues(self, event):
[eba08f1a]109        """
110            Check the valitidity of zmin and zmax value
111            zmax should be a float and zmin less than zmax
112        """
[ea290ee]113        flag = True
114        try:
115            value=self.zmin_ctl.GetValue()
116            if value and float( value)==0.0:
117                flag = False
[9a585d0]118                wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero"))
[ea290ee]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
[9a585d0]126            wx.PostEvent(self.parent, StatusEvent(status="Enter float value"))
[ea290ee]127            self.zmin_ctl.SetBackgroundColour("pink")
128            self.zmin_ctl.Refresh()
129        try:
130            value=self.zmax_ctl.GetValue()
[ed7384d]131            if value and float(value)==0.0:
[ea290ee]132                flag = False
[9a585d0]133                wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero"))
[ea290ee]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
[9a585d0]141            wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value"))
[ea290ee]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):
[eba08f1a]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        """
[8dfdd20]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   
[eba08f1a]165       
[ea290ee]166        if zmin !=None:
[8dfdd20]167            self.zmin_ctl.SetValue(str(float(zmin)))
[ea290ee]168        if zmax !=None:
[8dfdd20]169            self.zmax_ctl.SetValue(str(float(zmax)))
[ea290ee]170
171    def getContent(self):
[eba08f1a]172        """
173            @return event containing value to reset the detector of a given data
174        """
[ea290ee]175        event = self.Event()
[eba08f1a]176       
[ea290ee]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
[8dfdd20]196        event.cmap= self.cmap
[ea290ee]197       
198        return event
199
200    def __set_properties(self):
[eba08f1a]201        """
202            set proprieties of the dialog window
203        """
[ea290ee]204        self.SetTitle("Detector parameters")
205        self.SetSize((600, 595))
206
[eba08f1a]207
[ea290ee]208    def __do_layout(self):
[eba08f1a]209        """
210            fill the dialog window .
211        """
[ea290ee]212        sizer_main = wx.BoxSizer(wx.VERTICAL)
213        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
214        sizer_params = wx.GridBagSizer(5,5)
[8dfdd20]215        sizer_colormap = wx.BoxSizer(wx.VERTICAL)
216        sizer_selection= wx.BoxSizer(wx.HORIZONTAL)
217       
[ea290ee]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
[8dfdd20]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"))
[eba08f1a]257       
[8dfdd20]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       
[ea290ee]266        sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10)
[8dfdd20]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)
[ea290ee]270        sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0)
271       
272       
[8dfdd20]273        sizer_button.Add(self.button_reset,0, wx.LEFT|wx.ADJUST_MINSIZE, 100)
[ea290ee]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
[8dfdd20]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()
[ea290ee]296
297
298# end of class DialogAbout
299
300##### testing code ############################################################
301class MyApp(wx.App):
302    def OnInit(self):
303        wx.InitAllImageHandlers()
304        dialog = DetectorDialog(None, -1, "")
305        self.SetTopWindow(dialog)
[8dfdd20]306        dialog.setContent(xnpts=128,ynpts=128, qmax=20,
307                           beam=20,zmin=2,zmax=60, sym=False)
[ea290ee]308        print dialog.ShowModal()
309        evt = dialog.getContent()
[8dfdd20]310        if hasattr(evt,"npts"):
311            print "number of point: ",evt.npts
312        if hasattr(evt,"qmax"): 
313            print "qmax: ",evt.qmax
[ea290ee]314        dialog.Destroy()
315        return 1
316
317# end of class MyApp
318
319if __name__ == "__main__":
320    app = MyApp(0)
321    app.MainLoop()
322   
323##### end of testing code #####################################################   
Note: See TracBrowser for help on using the repository browser.