source: sasview/prview/perspectives/pr/inversion_panel.py @ 43c0a8e

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 43c0a8e was 119a11d, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Added help and updated requirements

  • Property mode set to 100644
File size: 26.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
8from sans.guicomm.events import StatusEvent   
9
10class InversionDlg(wx.Dialog):
11    def __init__(self, parent, id, title, plots, file=False, pars=True):
12       
13        # Estimate size
14        nplots = len(plots)
15        # y size for data set only
16        ysize  = 110 + nplots*20
17        # y size including parameters
18        if pars:
19            ysize  += 90
20       
21        wx.Dialog.__init__(self, parent, id, title, size=(250, ysize))
22        self.SetTitle(title)
23
24        # Data set
25        self.datasets = InversionPanel(self, -1, plots)
26        vbox = wx.BoxSizer(wx.VERTICAL)
27
28        vbox.Add(self.datasets)
29
30        # Parameters
31        self.pars_flag = False
32        if pars==True:
33            self.pars_flag = True
34            self.pars = ParsDialog(self, -1, file=file)
35            vbox.Add(self.pars)
36
37        static_line = wx.StaticLine(self, -1)
38        vbox.Add(static_line, 0, wx.EXPAND, 0)
39       
40        button_OK = wx.Button(self, wx.ID_OK, "OK")
41        button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
42       
43        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
44        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
45        sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10)
46        sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)       
47        vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
48
49        self.SetSizer(vbox)
50        self.SetAutoLayout(True)
51       
52        self.Layout()
53        self.Centre()
54
55    def get_content(self):
56        dataset = self.datasets.get_selected()
57        if self.pars_flag:
58            nfunc, alpha, dmax, file = self.pars.getContent()
59            return dataset, nfunc, alpha, dmax
60        else:
61            return dataset
62   
63    def set_content(self, dataset, nfunc, alpha, dmax):
64        if not dataset==None and dataset in self.datasets.radio_buttons.keys():
65            self.datasets.radio_buttons[dataset].SetValue(True)
66        if self.pars_flag:
67            self.pars.setContent(nfunc, alpha, dmax, None)
68
69class InversionPanel(wx.Panel):
70   
71    def __init__(self, parent, id = -1, plots = None, **kwargs):
72        wx.Panel.__init__(self, parent, id = id, **kwargs)
73       
74        self.plots = plots
75        self.radio_buttons = {}
76       
77        self._do_layout()
78       
79    def _do_layout(self):
80        panel = wx.Panel(self, -1)
81        vbox = wx.BoxSizer(wx.VERTICAL)
82
83        ysize = 30+20*len(self.plots)
84        wx.StaticBox(panel, -1, 'Choose a data set', (5, 5), (230, ysize))
85        ypos = 30
86        self.radio_buttons = {}
87        for item in self.plots.keys():
88            self.radio_buttons[self.plots[item].name] = wx.RadioButton(panel, -1, self.plots[item].name, (15, ypos))
89            ypos += 20
90       
91        vbox.Add(panel)
92
93        self.SetSizer(vbox)
94       
95    def get_selected(self):
96        for item in self.radio_buttons:
97            if self.radio_buttons[item].GetValue():
98                return item
99        return None
100
101class InversionControl(wx.Panel):
102    window_name = 'pr_control'
103    window_caption = "P(r) control panel"
104    CENTER_PANE = True
105   
106    # Figure of merit parameters [default]
107    oscillation_max = 1.5
108   
109    def __init__(self, parent, id = -1, plots = None, **kwargs):
110        wx.Panel.__init__(self, parent, id = id, **kwargs)
111       
112        self.plots = plots
113        self.radio_buttons = {}
114       
115        ## Data file TextCtrl
116        self.data_file = None
117        self.plot_data = None
118        self.nfunc_ctl = None
119        self.alpha_ctl = None
120        self.dmax_ctl  = None
121        self.time_ctl  = None
122        self.chi2_ctl  = None
123        self.osc_ctl  = None
124        self.file_radio = None
125        self.plot_radio = None
126        self.label_sugg = None
127        self.qmin_ctl = None
128        self.qmax_ctl = None
129       
130        ## Estimates
131        self.alpha_estimate_ctl = None
132       
133        ## Data manager
134        self.manager   = None
135       
136        self._do_layout()
137       
138    def __setattr__(self, name, value):
139        """
140            Allow direct hooks to text boxes
141        """
142        if name=='nfunc':
143            self.nfunc_ctl.SetValue(str(value))
144        elif name=='d_max':
145            self.dmax_ctl.SetValue(str(value))
146        elif name=='alpha':
147            self.alpha_ctl.SetValue(str(value))
148        elif name=='chi2':
149            self.chi2_ctl.SetValue("%-5.3g" % value)
150        elif name=='q_min':
151            self.qmin_ctl.SetValue("%-5.3g" % value)
152        elif name=='q_max':
153            self.qmax_ctl.SetValue("%-5.3g" % value)
154        elif name=='elapsed':
155            self.time_ctl.SetValue("%-5.2g" % value)
156        elif name=='oscillation':
157            self.osc_ctl.SetValue("%-5.2g" % value)
158        elif name=='alpha_estimate':
159            self.alpha_estimate_ctl.SetToolTipString("Click to accept value.")
160            self.alpha_estimate_ctl.Enable(True)
161            self.alpha_estimate_ctl.SetLabel("%-3.1g" % value)
162            #self.alpha_estimate_ctl.Show()
163            #self.label_sugg.Show()
164        elif name=='plotname':
165            self.plot_data.SetValue(str(value))
166            self.plot_radio.SetValue(True)
167            self._on_pars_changed(None)
168        else:
169            wx.Panel.__setattr__(self, name, value)
170       
171    def __getattr__(self, name):
172        """
173            Allow direct hooks to text boxes
174        """
175        if name=='nfunc':
176            int(self.nfunc_ctl.GetValue())
177        elif name=='d_max':
178            self.dmax_ctl.GetValue()
179        elif name=='alpha':
180            self.alpha_ctl.GetValue()
181        elif name=='chi2':
182            self.chi2_ctl.GetValue()
183        elif name=='q_min':
184            self.qmin_ctl.GetValue()
185        elif name=='q_max':
186            self.qmax_ctl.GetValue()
187        elif name=='elapsed':
188            self.time_ctl.GetValue()
189        elif name=='oscillation':
190            self.osc_ctl.GetValue()
191        elif name=='alpha_estimate':
192            self.alpha_estimate_ctl.GetValue()
193        elif name=='plotname':
194            self.plot_data.GetValue()
195        else:
196            wx.Panel.__getattr__(self, name)
197       
198    def set_manager(self, manager):
199        self.manager = manager
200        # Get data
201       
202        # Push data to form
203       
204       
205    def _do_layout(self):
206        #panel = wx.Panel(self, -1)
207        vbox = wx.BoxSizer(wx.VERTICAL)
208
209        # ----- I(q) data -----
210        databox = wx.StaticBox(self, -1, "I(q) data")
211       
212        boxsizer1 = wx.StaticBoxSizer(databox, wx.VERTICAL)
213        boxsizer1.SetMinSize((320,50))
214        pars_sizer = wx.GridBagSizer(5,5)
215
216        iy = 0
217        self.file_radio = wx.RadioButton(self, -1, "File data:")
218        self.data_file = wx.TextCtrl(self, -1, size=(100,20))
219        self.data_file.SetEditable(False)
220        self.data_file.SetValue("")
221        id = wx.NewId()
222        choose_button = wx.Button(self, id, "Choose file")
223        self.Bind(wx.EVT_BUTTON, self._change_file, id = id)   
224        pars_sizer.Add(self.file_radio, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
225        pars_sizer.Add(self.data_file, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 15)
226        pars_sizer.Add(choose_button, (iy,3), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
227       
228        iy += 1
229        self.plot_radio = wx.RadioButton(self, -1, "Plot data:")
230        self.plot_data = wx.TextCtrl(self, -1, size=(100,20))
231        self.plot_data.SetEditable(False)
232        pars_sizer.Add(self.plot_radio, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
233        pars_sizer.Add(self.plot_data, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 15)
234       
235        boxsizer1.Add(pars_sizer, 0, wx.EXPAND)
236        vbox.Add(boxsizer1)
237
238        # ----- Parameters -----
239        parsbox = wx.StaticBox(self, -1, "Parameters")
240        boxsizer2 = wx.StaticBoxSizer(parsbox, wx.VERTICAL)
241        boxsizer2.SetMinSize((320,50))
242       
243        explanation  = "P(r) is found by fitting a set of base functions to I(Q). "
244        explanation += "The minimization involves a regularization term to ensure "
245        explanation += "a smooth P(r). The alpha parameter gives the size of that " 
246        explanation += "term. The suggested value is the value above which the "
247        explanation += "output P(r) will have only one peak."
248        label_explain = wx.StaticText(self, -1, explanation, size=(280,80))
249        boxsizer2.Add(label_explain,  wx.LEFT|wx.BOTTOM, 5)
250       
251       
252       
253        label_nfunc = wx.StaticText(self, -1, "Number of terms")
254        label_nfunc.SetMinSize((120,20))
255        label_alpha = wx.StaticText(self, -1, "Regularization constant")
256        label_dmax  = wx.StaticText(self, -1, "Max distance [A]")
257        self.label_sugg  = wx.StaticText(self, -1, "Suggested value")
258        #self.label_sugg.Hide()
259       
260        self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20))
261        self.nfunc_ctl.SetToolTipString("Number of terms in the expansion.")
262        self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20))
263        self.alpha_ctl.SetToolTipString("Control parameter for the size of the regularization term.")
264        self.dmax_ctl  = wx.TextCtrl(self, -1, size=(60,20))
265        self.dmax_ctl.SetToolTipString("Maximum distance between any two points in the system.")
266        id = wx.NewId()
267        self.alpha_estimate_ctl  = wx.Button(self, id, "")
268        #self.alpha_estimate_ctl.Hide()
269        self.Bind(wx.EVT_BUTTON, self._on_accept_alpha, id = id)   
270        self.alpha_estimate_ctl.Enable(False)
271        #self.alpha_estimate_ctl.SetBackgroundColour('#ffdf85')
272        #self.alpha_estimate_ctl.SetBackgroundColour(self.GetBackgroundColour())
273        self.alpha_estimate_ctl.SetToolTipString("Waiting for estimate...")
274       
275        # EVT_TEXT would trigger an event for each character entered
276        self.nfunc_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed)
277        self.alpha_ctl.Bind(wx.EVT_KILL_FOCUS, self._read_pars)
278        self.dmax_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed)
279        self.Bind(wx.EVT_TEXT_ENTER, self._on_pars_changed)
280       
281        sizer_params = wx.GridBagSizer(5,5)
282
283        iy = 0
284        sizer_params.Add(self.label_sugg,       (iy,2), (1,1), wx.LEFT, 15)
285        iy += 1
286        sizer_params.Add(label_nfunc,      (iy,0), (1,1), wx.LEFT, 15)
287        sizer_params.Add(self.nfunc_ctl,   (iy,1), (1,1), wx.RIGHT, 0)
288        iy += 1
289        sizer_params.Add(label_alpha,      (iy,0), (1,1), wx.LEFT, 15)
290        sizer_params.Add(self.alpha_ctl,   (iy,1), (1,1), wx.RIGHT, 0)
291        sizer_params.Add(self.alpha_estimate_ctl, (iy,2), (1,1), wx.LEFT, 15)
292        iy += 1
293        sizer_params.Add(label_dmax, (iy,0), (1,1), wx.LEFT, 15)
294        sizer_params.Add(self.dmax_ctl,   (iy,1), (1,1), wx.RIGHT, 0)
295
296        boxsizer2.Add(sizer_params, 0)
297       
298        vbox.Add(boxsizer2)
299
300        # ----- Q range -----
301        qbox = wx.StaticBox(self, -1, "Q range")
302        qboxsizer = wx.StaticBoxSizer(qbox, wx.VERTICAL)
303        qboxsizer.SetMinSize((320,20))
304       
305        sizer_q = wx.GridBagSizer(5,5)
306
307        label_qmin = wx.StaticText(self, -1, "Q min")
308        label_qmax = wx.StaticText(self, -1, "Q max")
309        self.qmin_ctl = wx.TextCtrl(self, -1, size=(60,20))
310        self.qmax_ctl = wx.TextCtrl(self, -1, size=(60,20))
311       
312        iy = 0
313        sizer_q.Add(label_qmin, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15)
314        sizer_q.Add(self.qmin_ctl, (iy,1), (1,1), wx.LEFT|wx.EXPAND, 10)
315        sizer_q.Add(label_qmax, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 15)
316        sizer_q.Add(self.qmax_ctl, (iy,3), (1,1), wx.LEFT|wx.EXPAND, 10)
317        qboxsizer.Add(sizer_q, wx.TOP, 15)
318        vbox.Add(qboxsizer)
319       
320       
321
322        # ----- Results -----
323        resbox = wx.StaticBox(self, -1, "Outputs")
324        ressizer = wx.StaticBoxSizer(resbox, wx.VERTICAL)
325        ressizer.SetMinSize((320,50))
326       
327        label_time = wx.StaticText(self, -1, "Computation time")
328        label_time_unit = wx.StaticText(self, -1, "secs")
329        label_time.SetMinSize((120,20))
330        label_chi2 = wx.StaticText(self, -1, "Chi2/dof")
331        label_osc = wx.StaticText(self, -1, "Oscillations")
332       
333        self.time_ctl = wx.TextCtrl(self, -1, size=(60,20))
334        self.time_ctl.SetEditable(False)
335        self.time_ctl.SetToolTipString("Computation time for the last inversion, in seconds.")
336       
337        self.chi2_ctl = wx.TextCtrl(self, -1, size=(60,20))
338        self.chi2_ctl.SetEditable(False)
339        self.chi2_ctl.SetToolTipString("Chi^2 over degrees of freedom.")
340       
341        self.osc_ctl = wx.TextCtrl(self, -1, size=(60,20))
342        self.osc_ctl.SetEditable(False)
343        self.osc_ctl.SetToolTipString("Oscillation parameter. P(r) for a sphere has an oscillation parameter of 1.1.")
344       
345        sizer_res = wx.GridBagSizer(5,5)
346
347        iy = 0
348        sizer_res.Add(label_time, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15)
349        sizer_res.Add(self.time_ctl,   (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15)
350        sizer_res.Add(label_time_unit,   (iy,2), (1,1), wx.RIGHT|wx.EXPAND, 15)
351        iy += 1
352        sizer_res.Add(label_chi2, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15)
353        sizer_res.Add(self.chi2_ctl,   (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15)
354        iy += 1
355        sizer_res.Add(label_osc, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15)
356        sizer_res.Add(self.osc_ctl,   (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15)
357
358        ressizer.Add(sizer_res, 0)
359        vbox.Add(ressizer)
360
361        # ----- Buttons -----
362        static_line = wx.StaticLine(self, -1)
363        vbox.Add(static_line, 0, wx.EXPAND|wx.TOP, 10)
364       
365        id = wx.NewId()
366        button_OK = wx.Button(self, id, "Compute")
367        button_OK.SetToolTipString("Perform P(r) inversion.")
368        self.Bind(wx.EVT_BUTTON, self._on_invert, id = id)   
369        #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
370       
371        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
372        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
373        sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10)
374        #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)       
375        vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
376
377
378        self.SetSizer(vbox)
379       
380
381       
382    def _on_accept_alpha(self, evt):
383        """
384            User has accepted the estimated alpha,
385            set it as part of the input parameters
386        """
387        try:
388            alpha = self.alpha_estimate_ctl.GetLabel()
389            tmp = float(alpha)
390            self.alpha_ctl.SetValue(alpha)
391        except:
392            # No estimate or bad estimate, either do nothing
393            import sys
394            print "InversionControl._on_accept_alpha: %s" % sys.exc_value
395            pass
396       
397       
398    def _on_pars_changed(self, evt):
399        """
400            Called when an input parameter has changed
401            We will estimate the alpha parameter behind the
402            scenes.
403        """
404        flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars()
405       
406        # If the pars are valid, estimate alpha
407        if flag:
408            if self.plot_radio.GetValue():
409                dataset = self.plot_data.GetValue()
410                self.manager.estimate_plot_inversion(alpha=alpha, nfunc=nfunc, 
411                                                     d_max=dmax,
412                                                     q_min=qmin, q_max=qmax)
413            else:
414                path = self.data_file.GetValue()
415                self.manager.estimate_file_inversion(alpha=alpha, nfunc=nfunc, 
416                                                     d_max=dmax, path=path,
417                                                     q_min=qmin, q_max=qmax)
418       
419       
420    def _read_pars(self, evt=None):   
421        alpha = 0
422        nfunc = 5
423        dmax  = 120
424       
425        flag = True
426       
427        # Read alpha
428        try:
429            alpha = float(self.alpha_ctl.GetValue())
430            self.alpha_ctl.SetBackgroundColour(wx.WHITE)
431            self.alpha_ctl.Refresh()
432        except:
433            flag = False
434            self.alpha_ctl.SetBackgroundColour("pink")
435            self.alpha_ctl.Refresh()
436       
437        # Read d_max   
438        try:
439            dmax = float(self.dmax_ctl.GetValue())
440            self.dmax_ctl.SetBackgroundColour(wx.WHITE)
441            self.dmax_ctl.Refresh()
442        except:
443            flag = False
444            self.dmax_ctl.SetBackgroundColour("pink")
445            self.dmax_ctl.Refresh()
446           
447        # Read nfunc
448        try:
449            nfunc = int(self.nfunc_ctl.GetValue())
450            self.nfunc_ctl.SetBackgroundColour(wx.WHITE)
451            self.nfunc_ctl.Refresh()
452        except:
453            flag = False
454            self.nfunc_ctl.SetBackgroundColour("pink")
455            self.nfunc_ctl.Refresh()
456       
457        # Read qmin
458        try:
459            qmin_str = self.qmin_ctl.GetValue()
460            if len(qmin_str.lstrip().rstrip())==0:
461                qmin = None
462            else:
463                qmin = float(qmin_str)
464                self.qmin_ctl.SetBackgroundColour(wx.WHITE)
465                self.qmin_ctl.Refresh()
466        except:
467            flag = False
468            self.qmin_ctl.SetBackgroundColour("pink")
469            self.qmin_ctl.Refresh()
470       
471        # Read qmax
472        try:
473            qmax_str = self.qmax_ctl.GetValue()
474            if len(qmax_str.lstrip().rstrip())==0:
475                qmax = None
476            else:
477                qmax = float(qmax_str)
478                self.qmax_ctl.SetBackgroundColour(wx.WHITE)
479                self.qmax_ctl.Refresh()
480        except:
481            flag = False
482            self.qmax_ctl.SetBackgroundColour("pink")
483            self.qmax_ctl.Refresh()
484       
485        return flag, alpha, dmax, nfunc, qmin, qmax
486   
487    def _on_invert(self, evt):
488        """
489            Perform inversion
490            @param silent: when True, there will be no output for the user
491        """
492        # Get the data from the form
493        # Push it to the manager
494       
495        flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars()
496       
497        if flag:
498            if self.plot_radio.GetValue():
499                dataset = self.plot_data.GetValue()
500                self.manager.setup_plot_inversion(alpha=alpha, nfunc=nfunc, 
501                                                  d_max=dmax,
502                                                  q_min=qmin, q_max=qmax)
503            else:
504                path = self.data_file.GetValue()
505                self.manager.setup_file_inversion(alpha=alpha, nfunc=nfunc, 
506                                                  d_max=dmax, path=path,
507                                                  q_min=qmin, q_max=qmax
508                                                  )
509               
510        else:
511            message = "The P(r) form contains invalid values: please submit it again."
512            wx.PostEvent(self.parent, StatusEvent(status=message))
513       
514    def _change_file(self, evt):
515        """
516            Choose a new input file for I(q)
517        """
518        import os
519        if not self.manager==None:
520            path = self.manager.choose_file()
521           
522            if path and os.path.isfile(path):
523                self.data_file.SetValue(str(path))
524                self.file_radio.SetValue(True)
525                self._on_pars_changed(None)
526       
527
528
529
530class HelpDialog(wx.Dialog):
531    def __init__(self, parent, id):
532        from sans.pr.invertor import help
533        wx.Dialog.__init__(self, parent, id, size=(400, 400))
534        self.SetTitle("P(r) help") 
535       
536
537        vbox = wx.BoxSizer(wx.VERTICAL)
538
539        explanation = help()
540           
541        label_explain = wx.StaticText(self, -1, explanation, size=(350,300))
542           
543        vbox.Add(label_explain, 0, wx.ALL|wx.EXPAND, 15)
544
545
546        static_line = wx.StaticLine(self, -1)
547        vbox.Add(static_line, 0, wx.EXPAND, 0)
548       
549        button_OK = wx.Button(self, wx.ID_OK, "OK")
550        #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
551       
552        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
553        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
554        sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)
555        #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)       
556        vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
557
558        self.SetSizer(vbox)
559        self.SetAutoLayout(True)
560       
561        self.Layout()
562        self.Centre()
563
564
565class ParsDialog(wx.Panel):
566    """
567        Dialog box to let the user edit detector settings
568    """
569   
570    def __init__(self, parent, id = id, file=True, **kwargs):
571
572        wx.Panel.__init__(self, parent, id = id, **kwargs)
573        self.file = file
574       
575        self.label_nfunc = wx.StaticText(self, -1, "Number of terms")
576        self.label_alpha = wx.StaticText(self, -1, "Regularization constant")
577        self.label_dmax  = wx.StaticText(self, -1, "Max distance [A]")
578       
579        # Npts, q max
580        self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20))
581        self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20))
582        self.dmax_ctl  = wx.TextCtrl(self, -1, size=(60,20))
583
584        self.label_file = None
585        self.file_ctl   = None
586
587        self.static_line_3 = wx.StaticLine(self, -1)
588       
589       
590
591        self.__do_layout()
592
593        self.Fit()
594       
595    def _load_file(self, evt):
596        import os
597        path = None
598        dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.OPEN)
599        if dlg.ShowModal() == wx.ID_OK:
600            path = dlg.GetPath()
601            mypath = os.path.basename(path)
602        dlg.Destroy()
603       
604        if path and os.path.isfile(path):
605            self.file_ctl.SetValue(str(path))
606
607       
608    def checkValues(self, event):
609        flag = True
610        try:
611            float(self.alpha_ctl.GetValue())
612            self.alpha_ctl.SetBackgroundColour(wx.WHITE)
613            self.alpha_ctl.Refresh()
614        except:
615            flag = False
616            self.alpha_ctl.SetBackgroundColour("pink")
617            self.alpha_ctl.Refresh()
618           
619        try:
620            float(self.dmax_ctl.GetValue())
621            self.dmax_ctl.SetBackgroundColour(wx.WHITE)
622            self.dmax_ctl.Refresh()
623        except:
624            flag = False
625            self.dmax_ctl.SetBackgroundColour("pink")
626            self.dmax_ctl.Refresh()
627           
628        try:
629            int(self.nfunc_ctl.GetValue())
630            self.nfunc_ctl.SetBackgroundColour(wx.WHITE)
631            self.nfunc_ctl.Refresh()
632        except:
633            flag = False
634            self.nfunc_ctl.SetBackgroundColour("pink")
635            self.nfunc_ctl.Refresh()
636       
637        if flag:
638            event.Skip(True)
639   
640    def setContent(self, nfunc, alpha, dmax, file):
641        self.nfunc_ctl.SetValue(str(nfunc))
642        self.alpha_ctl.SetValue(str(alpha))
643        self.dmax_ctl.SetValue(str(dmax))
644        if self.file:
645            self.file_ctl.SetValue(str(file))
646
647    def getContent(self):
648        nfunc = int(self.nfunc_ctl.GetValue())
649        alpha = float(self.alpha_ctl.GetValue())
650        dmax = float(self.dmax_ctl.GetValue())
651        file = None
652        if self.file:
653            file = self.file_ctl.GetValue()
654        return nfunc, alpha, dmax, file
655
656
657    def __do_layout(self):
658        sizer_main = wx.BoxSizer(wx.VERTICAL)
659        sizer_params = wx.GridBagSizer(5,5)
660
661        iy = 0
662        sizer_params.Add(self.label_nfunc, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
663        sizer_params.Add(self.nfunc_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
664        iy += 1
665        sizer_params.Add(self.label_alpha, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
666        sizer_params.Add(self.alpha_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
667        iy += 1
668        sizer_params.Add(self.label_dmax, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
669        sizer_params.Add(self.dmax_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
670        iy += 1
671        if self.file:
672            self.label_file  = wx.StaticText(self, -1, "Input file")
673            self.file_ctl  = wx.TextCtrl(self, -1, size=(120,20))
674            sizer_params.Add(self.label_file, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
675            sizer_params.Add(self.file_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
676
677        sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10)
678       
679       
680        if self.file:
681            sizer_button = wx.BoxSizer(wx.HORIZONTAL)
682            self.button_load = wx.Button(self, 1, "Choose file")
683            self.Bind(wx.EVT_BUTTON, self._load_file, id = 1)       
684            sizer_button.Add(self.button_load, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10)
685       
686       
687            sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
688        self.SetAutoLayout(True)
689        self.SetSizer(sizer_main)
690        self.Layout()
691        self.Centre()
692        # end wxGlade
693
694
695# end of class DialogAbout
696
697##### testing code ############################################################
698class TestPlot:
699    def __init__(self, text):
700        self.name = text
701   
702class MyApp(wx.App):
703    def OnInit(self):
704        wx.InitAllImageHandlers()
705        dialog = HelpDialog(None, -1)
706        if dialog.ShowModal() == wx.ID_OK:
707            pass
708        dialog.Destroy()
709       
710        return 1
711
712# end of class MyApp
713
714if __name__ == "__main__":
715    app = MyApp(0)
716    app.MainLoop()
717   
718##### end of testing code #####################################################   
Note: See TracBrowser for help on using the repository browser.