source: sasview/guiframe/local_perspectives/plotting/detector_dialog.py @ 83f4445

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 83f4445 was 80a7f77, checked in by Jae Cho <jhjcho@…>, 14 years ago

fomat some nmubers

  • Property mode set to 100644
File size: 12.0 KB
Line 
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
8import sys
9from sans.guiframe.utils import format_number
10from sans.guicomm.events import StatusEvent ,NewPlotEvent
11
12import matplotlib 
13from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas
14from matplotlib import pyplot, mpl, pylab
15#FONT size
16if sys.platform.count("win32")>0:
17    FONT_VARIANT = 0
18else:
19    FONT_VARIANT = 1
20   
21DEFAULT_CMAP= pylab.cm.jet
22class DetectorDialog(wx.Dialog):
23    """
24        Dialog box to let the user edit detector settings
25    """
26   
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):
29
30        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
31        wx.Dialog.__init__(self,parent,id=1, *args, **kwds)
32       
33        self.SetWindowVariant(variant=FONT_VARIANT)
34        self.parent=base
35        self.dpi = dpi
36        self.cmap = cmap
37
38        self.reset_zmin_ctl= reset_zmin_ctl
39        self.reset_zmax_ctl= reset_zmax_ctl
40           
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))
54        self.zmin_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
55        self.zmax_ctl = wx.TextCtrl(self, -1, size=(60,20))
56        self.zmax_ctl.Bind(wx.EVT_SET_FOCUS, self.onSetFocus)
57   
58        self.static_line_3 = wx.StaticLine(self, -1)
59       
60        self.button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
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")
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
78        cmap= None
79        sym4 = False
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:
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))
106            self.cmap = DEFAULT_CMAP
107            self.cmap_selector.SetStringSelection("jet")
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       
113       
114    def checkValues(self, event):
115        """
116            Check the valitidity of zmin and zmax value
117            zmax should be a float and zmin less than zmax
118        """
119        flag = True
120        try:
121            value=self.zmin_ctl.GetValue()
122            if value and float( value)==0.0:
123                flag = False
124                wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero"))
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
132            wx.PostEvent(self.parent, StatusEvent(status="Enter float value"))
133            self.zmin_ctl.SetBackgroundColour("pink")
134            self.zmin_ctl.Refresh()
135        try:
136            value=self.zmax_ctl.GetValue()
137            if value and float(value)==0.0:
138                flag = False
139                wx.PostEvent(self.parent, StatusEvent(status="Enter number greater than zero"))
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
147            wx.PostEvent(self.parent, StatusEvent(status="Enter Integer value"))
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):
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        """
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)))
169       
170   
171       
172        if zmin !=None:
173            self.zmin_ctl.SetValue(str(format_number(zmin)))
174        if zmax !=None:
175            self.zmax_ctl.SetValue(str(format_number(zmax)))
176
177    def getContent(self):
178        """
179            @return event containing value to reset the detector of a given data
180        """
181        event = self.Event()
182       
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
202        event.cmap= self.cmap
203       
204        return event
205
206    def __set_properties(self):
207        """
208            set proprieties of the dialog window
209        """
210        self.SetTitle("Detector parameters")
211        self.SetSize((600, 595))
212
213
214    def __do_layout(self):
215        """
216            fill the dialog window .
217        """
218        sizer_main = wx.BoxSizer(wx.VERTICAL)
219        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
220        sizer_params = wx.GridBagSizer(5,5)
221        sizer_colormap = wx.BoxSizer(wx.VERTICAL)
222        sizer_selection= wx.BoxSizer(wx.HORIZONTAL)
223       
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
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"))
263       
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       
272        sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10)
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)
276        sizer_main.Add(self.static_line_3, 0, wx.EXPAND, 0)
277       
278       
279        sizer_button.Add(self.button_reset,0, wx.LEFT|wx.ADJUST_MINSIZE, 100)
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
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()
302
303
304# end of class DialogAbout
305
306##### testing code ############################################################
307class MyApp(wx.App):
308    def OnInit(self):
309        wx.InitAllImageHandlers()
310        dialog = DetectorDialog(None, -1, "")
311        self.SetTopWindow(dialog)
312        dialog.setContent(xnpts=128,ynpts=128, qmax=20,
313                           beam=20,zmin=2,zmax=60, sym=False)
314        print dialog.ShowModal()
315        evt = dialog.getContent()
316        if hasattr(evt,"npts"):
317            print "number of point: ",evt.npts
318        if hasattr(evt,"qmax"): 
319            print "qmax: ",evt.qmax
320        dialog.Destroy()
321        return 1
322
323# end of class MyApp
324
325if __name__ == "__main__":
326    app = MyApp(0)
327    app.MainLoop()
328   
329##### end of testing code #####################################################   
Note: See TracBrowser for help on using the repository browser.