source: sasview/fittingview/src/sans/perspectives/fitting/basepage.py @ 2ada16c

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 2ada16c was ea8283d, checked in by Jae Cho <jhjcho@…>, 13 years ago

small fix for loading a state file

  • Property mode set to 100644
File size: 124.1 KB
Line 
1
2import sys
3import os
4import wx
5import numpy
6import time
7import copy 
8import math
9import string
10from wx.lib.scrolledpanel import ScrolledPanel
11from sans.guiframe.panel_base import PanelBase
12from sans.guiframe.utils import format_number,check_float
13from sans.guiframe.events import PanelOnFocusEvent
14from sans.guiframe.events import StatusEvent
15from sans.guiframe.events import AppendBookmarkEvent
16from sans.guiframe.dataFitting import Data2D
17from sans.guiframe.dataFitting import Data1D
18from sans.guiframe.dataFitting import check_data_validity
19from sans.dataloader.data_info import Detector
20from sans.dataloader.data_info import Source
21import pagestate
22from pagestate import PageState
23
24(PageInfoEvent, EVT_PAGE_INFO)   = wx.lib.newevent.NewEvent()
25(PreviousStateEvent, EVT_PREVIOUS_STATE)   = wx.lib.newevent.NewEvent()
26(NextStateEvent, EVT_NEXT_STATE)   = wx.lib.newevent.NewEvent()
27
28_BOX_WIDTH = 76
29_QMIN_DEFAULT = 0.0005
30_QMAX_DEFAULT = 0.5
31_NPTS_DEFAULT = 50
32#Control panel width
33if sys.platform.count("win32")> 0:
34    PANEL_WIDTH = 450
35    FONT_VARIANT = 0
36    ON_MAC = False
37else:
38    PANEL_WIDTH = 500
39    FONT_VARIANT = 1
40    ON_MAC = True
41
42
43
44class BasicPage(ScrolledPanel, PanelBase):
45    """
46    This class provide general structure of  fitpanel page
47    """
48     ## Internal name for the AUI manager
49    window_name = "Fit Page"
50    ## Title to appear on top of the window
51    window_caption = "Fit Page "
52   
53    def __init__(self, parent,color='blue', **kwargs):
54        """
55        """
56        ScrolledPanel.__init__(self, parent, **kwargs)
57        PanelBase.__init__(self, parent)
58        self.SetupScrolling()
59        #Set window's font size
60        self.SetWindowVariant(variant=FONT_VARIANT)
61     
62        self.SetBackgroundColour(color)
63        ## parent of the page
64        self.parent = parent
65        ## manager is the fitting plugin
66        ## owner of the page (fitting plugin)
67        self.event_owner = None
68         ## current model
69        self.model = None
70        self.index_model = None
71        ## data
72        self.data = None
73        #list of available data
74        self.data_list = []
75        self.mask = None
76        self.uid = wx.NewId()
77        self.graph_id = None
78        #Q range for data set
79        self.qmin_data_set = numpy.inf
80        self.qmax_data_set = None
81        self.npts_data_set = 0
82        ## Q range
83        self.qmin = None
84        self.qmax = None
85        self.qmax_x = _QMAX_DEFAULT
86        self.qmin_x = _QMIN_DEFAULT
87        self.npts_x = _NPTS_DEFAULT
88        ## total number of point: float
89        self.npts = None
90        ## default fitengine type
91        self.engine_type = 'scipy'
92        ## smear default
93        self.current_smearer = None
94        ## 2D smear accuracy default
95        self.smear2d_accuracy = 'Low'
96        ## slit smear:
97        self.dxl = None
98        self.dxw = None
99        ## pinhole smear
100        self.dx_min = None
101        self.dx_max = None
102       
103        self.disp_cb_dict = {}
104   
105        self.state = PageState(parent=parent)
106        ## dictionary containing list of models
107        self.model_list_box = {}
108       
109        ## Data member to store the dispersion object created
110        self._disp_obj_dict = {}
111        ## selected parameters to apply dispersion
112        self.disp_cb_dict ={}
113        ## smearer object
114        self.enable2D = False
115        self.is_mac = ON_MAC
116       
117        ##list of model parameters. each item must have same length
118        ## each item related to a given parameters
119        ##[cb state, name, value, "+/-", error of fit, min, max , units]
120        self.parameters = []
121        # non-fittable parameter whose value is astring
122        self.str_parameters = []
123        ## list of parameters to fit , must be like self.parameters
124        self.param_toFit = []
125        ## list of looking like parameters but with non fittable parameters info
126        self.fixed_param = []
127        ## list of looking like parameters but with  fittable parameters info
128        self.fittable_param = []
129        ##list of dispersion parameters
130        self.disp_list = []
131        self.disp_name = ""
132       
133        ## list of orientation parameters
134        self.orientation_params = []
135        self.orientation_params_disp = []
136        if self.model != None:
137            self.disp_list = self.model.getDispParamList()
138        self.temp_multi_functional = False
139        ##enable model 2D draw
140        self.enable2D = False
141        ## check that the fit range is correct to plot the model again
142        self.fitrange = True
143        ## Create memento to save the current state
144        self.state = PageState(parent=self.parent,
145                               model=self.model, data=self.data)
146        ## flag to determine if state has change
147        self.state_change = False
148        ## save customized array
149        self.values = []
150        self.weights = []
151        ## retrieve saved state
152        self.number_saved_state = 0
153        ## dictionary of saved state
154        self.saved_states = {} 
155        ## Create context menu for page
156        self.popUpMenu = wx.Menu()
157   
158        id = wx.NewId()
159        self._keep = wx.MenuItem(self.popUpMenu,id,"Add bookmark",
160                                 " Keep the panel status to recall it later")
161        self.popUpMenu.AppendItem(self._keep)
162        self._keep.Enable(False)
163        self._set_bookmark_flag(False)
164        self._set_save_flag(False)
165        wx.EVT_MENU(self, id, self.on_bookmark)
166        self.popUpMenu.AppendSeparator()
167   
168        ## Default locations
169        self._default_save_location = os.getcwd()     
170        ## save initial state on context menu
171        #self.onSave(event=None)
172        self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu)
173       
174        # bind key event
175        self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
176       
177        ## create the basic structure of the panel with empty sizer
178        self.define_page_structure()
179        ## drawing Initial dispersion parameters sizer
180        self.set_dispers_sizer()
181       
182        ## layout
183        self.set_layout()
184   
185    def set_index_model(self, index):
186        """
187        Index related to this page
188        """
189        self.index_model = index
190       
191    def create_default_data(self):
192        """
193        Given the user selection, creates a 1D or 2D data
194        Only when the page is on theory mode.
195        """
196        if not hasattr(self, "model_view"):
197            return
198        toggle_mode_on = self.model_view.IsEnabled()
199        if toggle_mode_on:
200            if self.enable2D and not check_data_validity(self.data):
201                self._create_default_2d_data()
202            else:
203                self._create_default_1d_data()
204       
205    def _create_default_1d_data(self):
206        """
207        Create default data for fitting perspective
208        Only when the page is on theory mode.
209        :warning: This data is never plotted.
210       
211        """
212        x = numpy.linspace(start=self.qmin_x, stop=self.qmax_x, 
213                           num=self.npts_x, endpoint=True)
214        self.data = Data1D(x=x)
215        self.data.xaxis('\\rm{Q}',"A^{-1}")
216        self.data.yaxis('\\rm{Intensity}', "cm^{-1}")
217        self.data.is_data = False
218        self.data.id = str(self.uid) + " data" 
219        self.data.group_id = str(self.uid) + " Model1D" 
220       
221    def _create_default_2d_data(self):
222        """
223        Create 2D data by default
224        Only when the page is on theory mode.
225        :warning: This data is never plotted.
226        """
227        self.data = Data2D()
228        qmax = self.qmax_x / math.sqrt(2)
229        self.data.xaxis('\\rm{Q_{x}}', 'A^{-1}')
230        self.data.yaxis('\\rm{Q_{y}}', 'A^{-1}')
231        self.data.is_data = False
232        self.data.id = str(self.uid) + " data" 
233        self.data.group_id = str(self.uid) + " Model2D" 
234        ## Default values   
235        self.data.detector.append(Detector()) 
236        index = len(self.data.detector) - 1
237        self.data.detector[index].distance = 8000   # mm       
238        self.data.source.wavelength= 6         # A     
239        self.data.detector[index].pixel_size.x = 5  # mm
240        self.data.detector[index].pixel_size.y = 5  # mm
241        self.data.detector[index].beam_center.x = qmax
242        self.data.detector[index].beam_center.y = qmax
243        ## create x_bins and y_bins of the model 2D
244        pixel_width_x = self.data.detector[index].pixel_size.x
245        pixel_width_y = self.data.detector[index].pixel_size.y
246        center_x = self.data.detector[index].beam_center.x/pixel_width_x
247        center_y = self.data.detector[index].beam_center.y/pixel_width_y
248        # theory default: assume the beam
249        #center is located at the center of sqr detector
250        xmax = qmax
251        xmin = -qmax
252        ymax = qmax
253        ymin = -qmax
254        qstep = self.npts_x
255
256        x = numpy.linspace(start=xmin, stop=xmax, num=qstep, endpoint=True) 
257        y = numpy.linspace(start=ymin, stop=ymax, num=qstep, endpoint=True)
258        ## use data info instead
259        new_x = numpy.tile(x, (len(y), 1))
260        new_y = numpy.tile(y, (len(x), 1))
261        new_y = new_y.swapaxes(0,1)
262        # all data reuire now in 1d array
263        qx_data = new_x.flatten()
264        qy_data = new_y.flatten()
265        q_data = numpy.sqrt(qx_data*qx_data + qy_data*qy_data)
266        # set all True (standing for unmasked) as default
267        mask = numpy.ones(len(qx_data), dtype=bool)
268        # calculate the range of qx and qy: this way,
269        # it is a little more independent
270        x_size = xmax - xmin
271        y_size = ymax - ymin
272        # store x and y bin centers in q space
273        x_bins  = x
274        y_bins  = y
275        # bin size: x- & y-directions
276        xstep = x_size/len(x_bins-1)
277        ystep = y_size/len(y_bins-1)
278 
279        self.data.source = Source()
280        self.data.data = numpy.ones(len(mask))
281        self.data.err_data = numpy.ones(len(mask))
282        self.data.qx_data = qx_data
283        self.data.qy_data = qy_data 
284        self.data.q_data = q_data
285        self.data.mask = mask           
286        self.data.x_bins = x_bins 
287        self.data.y_bins = y_bins   
288        # max and min taking account of the bin sizes
289        self.data.xmin = xmin
290        self.data.xmax = xmax
291        self.data.ymin = ymin
292        self.data.ymax = ymax
293
294    def on_set_focus(self, event):
295        """
296        """
297        if self._manager is not None:
298            wx.PostEvent(self._manager.parent, PanelOnFocusEvent(panel=self))
299       
300    class ModelTextCtrl(wx.TextCtrl):
301        """
302        Text control for model and fit parameters.
303        Binds the appropriate events for user interactions.
304        Default callback methods can be overwritten on initialization
305       
306        :param kill_focus_callback: callback method for EVT_KILL_FOCUS event
307        :param set_focus_callback:  callback method for EVT_SET_FOCUS event
308        :param mouse_up_callback:   callback method for EVT_LEFT_UP event
309        :param text_enter_callback: callback method for EVT_TEXT_ENTER event
310       
311        """
312        ## Set to True when the mouse is clicked while the whole string is selected
313        full_selection = False
314        ## Call back for EVT_SET_FOCUS events
315        _on_set_focus_callback = None
316       
317        def __init__(self, parent, id=-1, 
318                     value=wx.EmptyString, 
319                     pos=wx.DefaultPosition, 
320                     size=wx.DefaultSize,
321                     style=0, 
322                     validator=wx.DefaultValidator,
323                     name=wx.TextCtrlNameStr,
324                     kill_focus_callback=None,
325                     set_focus_callback=None,
326                     mouse_up_callback=None,
327                     text_enter_callback = None):
328             
329            wx.TextCtrl.__init__(self, parent, id, value, pos,
330                                  size, style, validator, name)
331           
332            # Bind appropriate events
333            self._on_set_focus_callback = parent.onSetFocus \
334                      if set_focus_callback is None else set_focus_callback
335            self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
336            self.Bind(wx.EVT_KILL_FOCUS, self._silent_kill_focus \
337                      if kill_focus_callback is None else kill_focus_callback)               
338            self.Bind(wx.EVT_TEXT_ENTER, parent._onparamEnter \
339                      if text_enter_callback is None else text_enter_callback)
340            if not ON_MAC :
341                self.Bind(wx.EVT_LEFT_UP,    self._highlight_text \
342                          if mouse_up_callback is None else mouse_up_callback)
343           
344        def _on_set_focus(self, event):
345            """
346            Catch when the text control is set in focus to highlight the whole
347            text if necessary
348           
349            :param event: mouse event
350           
351            """
352            event.Skip()
353            self.full_selection = True
354            return self._on_set_focus_callback(event)
355       
356 
357           
358        def _highlight_text(self, event):
359            """
360            Highlight text of a TextCtrl only of no text has be selected
361           
362            :param event: mouse event
363           
364            """
365            # Make sure the mouse event is available to other listeners
366            event.Skip()
367            control  = event.GetEventObject()
368            if self.full_selection:
369                self.full_selection = False
370                # Check that we have a TextCtrl
371                if issubclass(control.__class__, wx.TextCtrl):
372                    # Check whether text has been selected,
373                    # if not, select the whole string
374                    (start, end) = control.GetSelection()
375                    if start==end:
376                        control.SetSelection(-1,-1)
377                       
378        def _silent_kill_focus(self,event):
379            """
380            Save the state of the page
381            """
382           
383            event.Skip()
384            pass
385   
386    def set_page_info(self, page_info):
387        """
388        set some page important information at once
389        """
390       ##window_name
391        self.window_name = page_info.window_name
392        ##window_caption
393        self.window_caption = page_info.window_caption
394        ## manager is the fitting plugin
395        self._manager= page_info.manager
396        ## owner of the page (fitting plugin)
397        self.event_owner= page_info.event_owner
398         ## current model
399        self.model = page_info.model
400        ## data
401        self.data = page_info.data
402        ## dictionary containing list of models
403        self.model_list_box = page_info.model_list_box
404        ## Data member to store the dispersion object created
405        self.populate_box(dict=self.model_list_box)
406       
407    def onContextMenu(self, event): 
408        """
409        Retrieve the state selected state
410        """
411        # Skipping the save state functionality for release 0.9.0
412        #return
413   
414        pos = event.GetPosition()
415        pos = self.ScreenToClient(pos)
416       
417        self.PopupMenu(self.popUpMenu, pos) 
418     
419       
420    def onUndo(self, event):
421        """
422        Cancel the previous action
423        """
424        event = PreviousStateEvent(page = self)
425        wx.PostEvent(self.parent, event)
426       
427    def onRedo(self, event):
428        """
429        Restore the previous action cancelled
430        """
431        event = NextStateEvent(page= self)
432        wx.PostEvent(self.parent, event)
433   
434    def define_page_structure(self):
435        """
436        Create empty sizer for a panel
437        """
438        self.vbox  = wx.BoxSizer(wx.VERTICAL)
439        self.sizer0 = wx.BoxSizer(wx.VERTICAL)
440        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
441        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
442        self.sizer3 = wx.BoxSizer(wx.VERTICAL)
443        self.sizer4 = wx.BoxSizer(wx.VERTICAL)
444        self.sizer5 = wx.BoxSizer(wx.VERTICAL)
445        self.sizer6 = wx.BoxSizer(wx.VERTICAL)
446       
447        self.sizer0.SetMinSize((PANEL_WIDTH,-1))
448        self.sizer1.SetMinSize((PANEL_WIDTH,-1))
449        self.sizer2.SetMinSize((PANEL_WIDTH,-1))
450        self.sizer3.SetMinSize((PANEL_WIDTH,-1))
451        self.sizer4.SetMinSize((PANEL_WIDTH,-1))
452        self.sizer5.SetMinSize((PANEL_WIDTH,-1))
453        self.sizer6.SetMinSize((PANEL_WIDTH,-1))
454       
455        self.vbox.Add(self.sizer0)
456        self.vbox.Add(self.sizer1)
457        self.vbox.Add(self.sizer2)
458        self.vbox.Add(self.sizer3)
459        self.vbox.Add(self.sizer4)
460        self.vbox.Add(self.sizer5)
461        self.vbox.Add(self.sizer6)
462       
463    def set_layout(self):
464        """
465        layout
466        """
467        self.vbox.Layout()
468        self.vbox.Fit(self) 
469        self.SetSizer(self.vbox)
470        self.Centre()
471 
472    def set_owner(self,owner):
473        """
474        set owner of fitpage
475       
476        :param owner: the class responsible of plotting
477       
478        """
479        self.event_owner = owner   
480        self.state.event_owner = owner
481       
482    def get_state(self):
483        """
484        """
485        return self.state
486   
487    def get_data(self):
488        """
489        return the current data
490        """
491        return self.data 
492   
493    def get_data_list(self):
494        """
495        return the current data
496        """
497        return self.data_list 
498   
499    def set_manager(self, manager):
500        """
501        set panel manager
502       
503        :param manager: instance of plugin fitting
504       
505        """
506        self._manager = manager 
507        self.state.manager = manager
508       
509    def populate_box(self, dict):
510        """
511        Store list of model
512       
513        :param dict: dictionary containing list of models
514       
515        """
516        self.model_list_box = dict
517        self.state.model_list_box = self.model_list_box
518        self.initialize_combox()
519       
520    def initialize_combox(self): 
521        """
522        put default value in the combobox
523        """ 
524        ## fill combox box
525        if self.model_list_box is None:
526            return
527        if len(self.model_list_box) > 0:
528            self._populate_box(self.formfactorbox,
529                               self.model_list_box["Shapes"])
530       
531        if len(self.model_list_box) > 0:
532            self._populate_box(self.structurebox,
533                                self.model_list_box["Structure Factors"])
534            self.structurebox.Insert("None", 0, None)
535            self.structurebox.SetSelection(0)
536            self.structurebox.Hide()
537            self.text2.Hide()
538            self.structurebox.Disable()
539            self.text2.Disable()
540             
541            if self.model.__class__ in self.model_list_box["P(Q)*S(Q)"]:
542                self.structurebox.Show()
543                self.text2.Show()
544                self.structurebox.Enable()
545                self.text2.Enable()           
546               
547    def set_dispers_sizer(self):
548        """
549        fill sizer containing dispersity info
550        """
551        self.sizer4.Clear(True)
552        name="Polydispersity and Orientational Distribution"
553        box_description= wx.StaticBox(self, -1,name)
554        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
555        #----------------------------------------------------
556        self.disable_disp = wx.RadioButton(self, -1, 'Off', (10, 10),
557                                            style=wx.RB_GROUP)
558        self.enable_disp = wx.RadioButton(self, -1, 'On', (10, 30))
559        # best size for MAC and PC
560        if ON_MAC:
561            size_q = (30, 20)     
562        else:
563            size_q = (20, 15)   
564        self.disp_help_bt = wx.Button(self,wx.NewId(),'?', 
565                                      style = wx.BU_EXACTFIT,
566                                      size=size_q)
567        self.disp_help_bt.Bind(wx.EVT_BUTTON, 
568                        self.on_pd_help_clicked,id= self.disp_help_bt.GetId())
569        self.disp_help_bt.SetToolTipString("Helps for Polydispersion.")       
570       
571        self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param,
572                     id=self.disable_disp.GetId())
573        self.Bind(wx.EVT_RADIOBUTTON, self._set_dipers_Param,
574                   id=self.enable_disp.GetId())
575        #MAC needs SetValue
576        self.disable_disp.SetValue(True)
577        sizer_dispersion = wx.BoxSizer(wx.HORIZONTAL)
578        sizer_dispersion.Add((20,20))
579        name=""#Polydispersity and \nOrientational Distribution "
580        sizer_dispersion.Add(wx.StaticText(self,-1,name))
581        sizer_dispersion.Add(self.enable_disp )
582        sizer_dispersion.Add((20,20))
583        sizer_dispersion.Add(self.disable_disp )
584        sizer_dispersion.Add((25,20))
585        sizer_dispersion.Add(self.disp_help_bt)
586       
587        ## fill a sizer for dispersion         
588        boxsizer1.Add( sizer_dispersion,0,
589                wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
590        #boxsizer1.Add( (10,10) )
591        #boxsizer1.Add( sizer_select_dispers )
592        self.sizer4_4 = wx.GridBagSizer(6,5)
593
594        boxsizer1.Add( self.sizer4_4  )
595        #-----------------------------------------------------
596        self.sizer4.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
597        self.sizer4_4.Layout()
598        self.sizer4.Layout()
599        self.Layout()
600     
601        self.Refresh()
602        ## saving the state of enable dispersity button
603        self.state.enable_disp= self.enable_disp.GetValue()
604        self.state.disable_disp= self.disable_disp.GetValue()
605        self.SetupScrolling()
606
607   
608    def onResetModel(self, event):
609        """
610        Reset model state
611        """
612        menu = event.GetEventObject()
613        ## post help message for the selected model
614        msg = menu.GetHelpString(event.GetId())
615        msg +=" reloaded"
616        wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
617        self.Show(False)
618        name = menu.GetLabel(event.GetId())
619        self._on_select_model_helper()
620        if name in self.saved_states.keys():
621            previous_state = self.saved_states[name]
622            ## reset state of checkbox,textcrtl  and  regular parameters value
623           
624            self.reset_page(previous_state)   
625        self.Show(True)
626               
627    def on_preview(self, event):
628        """
629        Report the current fit results
630        """   
631        # Get plot image from plotpanel
632        images, canvases = self.get_images()
633        # get the report dialog
634        self.state.report(images, canvases)
635       
636         
637    def on_save(self, event):   
638        """
639        Save the current state into file
640        """ 
641        self.save_current_state()
642        new_state = self.state.clone()
643        # Ask the user the location of the file to write to.
644        path = None
645        if self.parent !=  None:
646            self._default_save_location = \
647                        self.parent.parent._default_save_location
648        dlg = wx.FileDialog(self, "Choose a file", self._default_save_location,
649                                        self.window_caption, "*.fitv", wx.SAVE)
650
651        if dlg.ShowModal() == wx.ID_OK:
652            path = dlg.GetPath()
653            self._default_save_location = os.path.dirname(path)
654            self.parent.parent._default_save_location =\
655                                 self._default_save_location
656        else:
657            return None
658        # MAC always needs the extension for saving
659        extens = ".fitv"
660        # Make sure the ext included in the file name
661        fName = os.path.splitext(path)[0] + extens
662        #the manager write the state into file
663        self._manager.save_fit_state(filepath=fName, fitstate=new_state)
664        return new_state 
665   
666    def on_copy(self, event):
667        """
668        Copy Parameter values to the clipboad
669        """
670        if event != None:
671            event.Skip()
672        # It seems MAC needs wxCallAfter
673        wx.CallAfter(self.get_copy)
674       
675        # messages depending on the flag
676        #self._copy_info(None)
677       
678    def on_paste(self, event):
679        """
680        Paste Parameter values to the panel if possible
681        """
682        if event != None:
683            event.Skip()
684        # It seems MAC needs wxCallAfter for the setvalues
685        # for multiple textctrl items, otherwise it tends to crash once a while.
686        wx.CallAfter(self.get_paste)
687        # messages depending on the flag
688        #self._copy_info(True)
689       
690    def _copy_info(self, flag):
691        """
692        Send event dpemding on flag
693       
694        : Param flag: flag that distinguish event
695        """
696        # messages depending on the flag
697        if flag == None:
698            msg = " Parameter values are copied to the clipboard..."
699            infor = 'warning'
700        elif flag:
701            msg = " Parameter values are pasted from the clipboad..."
702            infor = "warning"
703        else:
704            msg = "Error was occured "
705            msg += ": No valid parameter values to paste from the clipboard..."
706            infor = "error"
707        # inform msg to wx
708        wx.PostEvent( self.parent.parent, 
709                      StatusEvent(status= msg, info=infor))
710       
711    def _get_time_stamp(self):
712        """
713        return time and date stings
714        """
715        # date and time
716        year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime()
717        current_time= str(hour)+":"+str(minute)+":"+str(second)
718        current_date= str( month)+"/"+str(day)+"/"+str(year)
719        return current_time, current_date
720     
721    def on_bookmark(self, event):
722        """
723        save history of the data and model
724        """
725        if self.model==None:
726            msg="Can not bookmark; Please select Data and Model first..."
727            wx.MessageBox(msg, 'Info')
728            return 
729        self.save_current_state()
730        new_state = self.state.clone()
731        ##Add model state on context menu
732        self.number_saved_state += 1
733        current_time, current_date = self._get_time_stamp()
734        #name= self.model.name+"[%g]"%self.number_saved_state
735        name = "Fitting: %g]" % self.number_saved_state
736        name += self.model.__class__.__name__
737        name += "bookmarked at %s on %s" % (current_time, current_date)
738        self.saved_states[name]= new_state
739       
740        ## Add item in the context menu
741        msg =  "Model saved at %s on %s"%(current_time, current_date)
742         ## post help message for the selected model
743        msg +=" Saved! right click on this page to retrieve this model"
744        wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
745       
746        id = wx.NewId()
747        self.popUpMenu.Append(id,name,str(msg))
748        wx.EVT_MENU(self, id, self.onResetModel)
749        wx.PostEvent(self.parent.parent, 
750                     AppendBookmarkEvent(title=name, 
751                                         hint=str(msg), handler=self._back_to_bookmark))
752    def _back_to_bookmark(self, event): 
753        """
754        Back to bookmark
755        """
756        self._manager.on_perspective(event)
757        self.onResetModel(event)
758    def old_on_bookmark(self, event):
759        """
760        save history of the data and model
761        """
762        if self.model==None:
763            msg="Can not bookmark; Please select Data and Model first..."
764            wx.MessageBox(msg, 'Info')
765            return 
766        if hasattr(self,"enable_disp"):
767            self.state.enable_disp = copy.deepcopy(self.enable_disp.GetValue())
768        if hasattr(self, "disp_box"):
769            self.state.disp_box = copy.deepcopy(self.disp_box.GetSelection())
770
771        self.state.model.name= self.model.name
772       
773        #Remember fit engine_type for fit panel
774        if self.engine_type == None: 
775            self.engine_type = "scipy"
776        if self._manager !=None:
777            self._manager._on_change_engine(engine=self.engine_type)
778       
779            self.state.engine_type = self.engine_type
780
781        new_state = self.state.clone()
782        new_state.model.name = self.state.model.name
783       
784        new_state.enable2D = copy.deepcopy(self.enable2D)
785        ##Add model state on context menu
786        self.number_saved_state += 1
787        #name= self.model.name+"[%g]"%self.number_saved_state
788        name= self.model.__class__.__name__+"[%g]"%self.number_saved_state
789        self.saved_states[name]= new_state
790       
791        ## Add item in the context menu
792       
793        year, month, day,hour,minute,second,tda,ty,tm_isdst= time.localtime()
794        my_time= str(hour)+" : "+str(minute)+" : "+str(second)+" "
795        date= str( month)+"|"+str(day)+"|"+str(year)
796        msg=  "Model saved at %s on %s"%(my_time, date)
797         ## post help message for the selected model
798        msg +=" Saved! right click on this page to retrieve this model"
799        wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
800       
801        id = wx.NewId()
802        self.popUpMenu.Append(id,name,str(msg))
803        wx.EVT_MENU(self, id, self.onResetModel)
804       
805    def onSetFocus(self, evt):
806        """
807        highlight the current textcrtl and hide the error text control shown
808        after fitting
809        """
810        return
811   
812    def read_file(self, path):
813        """
814        Read two columns file
815       
816        :param path: the path to the file to read
817       
818        """
819        try:
820            if path==None:
821                wx.PostEvent(self.parent.parent, StatusEvent(status=\
822                            " Selected Distribution was not loaded: %s"%path))
823                return None, None
824            input_f = open(path, 'r')
825            buff = input_f.read()
826            lines = buff.split('\n')
827           
828            angles = []
829            weights=[]
830            for line in lines:
831                toks = line.split()
832                try:
833                    angle = float(toks[0])
834                    weight = float(toks[1])
835                except:
836                    # Skip non-data lines
837                    pass
838                angles.append(angle)
839                weights.append(weight)
840            return numpy.array(angles), numpy.array(weights)
841        except:
842            raise 
843
844    def createMemento(self):
845        """
846        return the current state of the page
847        """
848        return self.state.clone()
849   
850   
851    def save_current_state(self):
852        """
853        Store current state
854        """
855        self.state.engine_type = copy.deepcopy(self.engine_type)
856        ## save model option
857        if self.model!= None:
858            self.disp_list= self.model.getDispParamList()
859            self.state.disp_list= copy.deepcopy(self.disp_list)
860            self.state.model = self.model.clone()
861        #save radiobutton state for model selection
862        self.state.shape_rbutton = self.shape_rbutton.GetValue()
863        self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue()
864        self.state.struct_rbutton = self.struct_rbutton.GetValue()
865        self.state.plugin_rbutton = self.plugin_rbutton.GetValue()
866        #model combobox
867        self.state.structurebox = self.structurebox.GetSelection()
868        self.state.formfactorbox = self.formfactorbox.GetSelection()
869       
870        self.state.enable2D = copy.deepcopy(self.enable2D)
871        self.state.values= copy.deepcopy(self.values)
872        self.state.weights = copy.deepcopy( self.weights)
873        ## save data   
874        self.state.data= copy.deepcopy(self.data)
875        self.state.qmax_x = self.qmax_x
876        self.state.qmin_x = self.qmin_x
877        self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue())
878        self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue())
879        self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue())
880        self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue())
881        if hasattr(self,"enable_disp"):
882            self.state.enable_disp= self.enable_disp.GetValue()
883            self.state.disable_disp = self.disable_disp.GetValue()
884           
885        self.state.smearer = copy.deepcopy(self.current_smearer)
886        if hasattr(self,"enable_smearer"):
887            self.state.enable_smearer = \
888                                copy.deepcopy(self.enable_smearer.GetValue())
889            self.state.disable_smearer = \
890                                copy.deepcopy(self.disable_smearer.GetValue())
891
892        self.state.pinhole_smearer = \
893                                copy.deepcopy(self.pinhole_smearer.GetValue())
894        self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) 
895                 
896        if len(self._disp_obj_dict)>0:
897            for k , v in self._disp_obj_dict.iteritems():
898                self.state._disp_obj_dict[k]= v
899                       
900           
901            self.state.values = copy.deepcopy(self.values)
902            self.state.weights = copy.deepcopy(self.weights)
903        ## save plotting range
904        self._save_plotting_range()
905       
906        self.state.orientation_params = []
907        self.state.orientation_params_disp = []
908        self.state.parameters = []
909        self.state.fittable_param = []
910        self.state.fixed_param = []
911        self.state.str_parameters = []
912
913       
914        ## save checkbutton state and txtcrtl values
915        self._copy_parameters_state(self.str_parameters, 
916                                    self.state.str_parameters)
917        self._copy_parameters_state(self.orientation_params,
918                                     self.state.orientation_params)
919        self._copy_parameters_state(self.orientation_params_disp,
920                                     self.state.orientation_params_disp)
921       
922        self._copy_parameters_state(self.parameters, self.state.parameters)
923        self._copy_parameters_state(self.fittable_param,
924                                     self.state.fittable_param)
925        self._copy_parameters_state(self.fixed_param, self.state.fixed_param)
926        #save chisqr
927        self.state.tcChi = self.tcChi.GetValue()
928       
929    def save_current_state_fit(self):
930        """
931        Store current state for fit_page
932        """
933        ## save model option
934        if self.model!= None:
935            self.disp_list= self.model.getDispParamList()
936            self.state.disp_list= copy.deepcopy(self.disp_list)
937            self.state.model = self.model.clone()
938        if hasattr(self, "engine_type"):
939            self.state.engine_type = copy.deepcopy(self.engine_type)
940           
941        self.state.enable2D = copy.deepcopy(self.enable2D)
942        self.state.values= copy.deepcopy(self.values)
943        self.state.weights = copy.deepcopy( self.weights)
944        ## save data   
945        self.state.data= copy.deepcopy(self.data)
946       
947        if hasattr(self,"enable_disp"):
948            self.state.enable_disp= self.enable_disp.GetValue()
949            self.state.disable_disp = self.disable_disp.GetValue()
950           
951        self.state.smearer = copy.deepcopy(self.current_smearer)
952        if hasattr(self,"enable_smearer"):
953            self.state.enable_smearer = \
954                                copy.deepcopy(self.enable_smearer.GetValue())
955            self.state.disable_smearer = \
956                                copy.deepcopy(self.disable_smearer.GetValue())
957           
958        self.state.pinhole_smearer = \
959                                copy.deepcopy(self.pinhole_smearer.GetValue())
960        self.state.slit_smearer = copy.deepcopy(self.slit_smearer.GetValue()) 
961        self.state.dI_noweight = copy.deepcopy(self.dI_noweight.GetValue())
962        self.state.dI_didata = copy.deepcopy(self.dI_didata.GetValue())
963        self.state.dI_sqrdata = copy.deepcopy(self.dI_sqrdata.GetValue())
964        self.state.dI_idata = copy.deepcopy(self.dI_idata.GetValue()) 
965        if hasattr(self,"disp_box"):
966            self.state.disp_box = self.disp_box.GetCurrentSelection()
967
968            if len(self.disp_cb_dict) > 0:
969                for k, v in self.disp_cb_dict.iteritems():
970         
971                    if v == None :
972                        self.state.disp_cb_dict[k] = v
973                    else:
974                        try:
975                            self.state.disp_cb_dict[k] = v.GetValue()
976                        except:
977                            self.state.disp_cb_dict[k] = None
978           
979            if len(self._disp_obj_dict) > 0:
980                for k , v in self._disp_obj_dict.iteritems():
981     
982                    self.state._disp_obj_dict[k] = v
983                       
984           
985            self.state.values = copy.deepcopy(self.values)
986            self.state.weights = copy.deepcopy(self.weights)
987           
988        ## save plotting range
989        self._save_plotting_range()
990       
991        ## save checkbutton state and txtcrtl values
992        self._copy_parameters_state(self.orientation_params,
993                                     self.state.orientation_params)
994        self._copy_parameters_state(self.orientation_params_disp,
995                                     self.state.orientation_params_disp)
996        self._copy_parameters_state(self.parameters, self.state.parameters)
997        self._copy_parameters_state(self.fittable_param,
998                                             self.state.fittable_param)
999        self._copy_parameters_state(self.fixed_param, self.state.fixed_param)
1000   
1001         
1002    def check_invalid_panel(self): 
1003        """
1004        check if the user can already perform some action with this panel
1005        """ 
1006        flag = False
1007        if self.data is None:
1008            self.disable_smearer.SetValue(True)
1009            self.disable_disp.SetValue(True)
1010            msg = "Please load Data and select Model to start..."
1011            wx.MessageBox(msg, 'Info')
1012            return  True
1013       
1014    def set_model_state(self, state):
1015        """
1016        reset page given a model state
1017        """
1018        self.disp_cb_dict = state.disp_cb_dict
1019        self.disp_list = state.disp_list
1020     
1021        ## set the state of the radio box
1022        self.shape_rbutton.SetValue(state.shape_rbutton )
1023        self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton)
1024        self.struct_rbutton.SetValue(state.struct_rbutton)
1025        self.plugin_rbutton.SetValue(state.plugin_rbutton)
1026       
1027        ## fill model combobox
1028        self._show_combox_helper()
1029        #select the current model
1030        self.formfactorbox.Select(int(state.formfactorcombobox))
1031        self.structurebox.SetSelection(state.structurecombobox )
1032        if state.multi_factor != None:
1033            self.multifactorbox.SetSelection(state.multi_factor)
1034           
1035         ## reset state of checkbox,textcrtl  and  regular parameters value
1036        self._reset_parameters_state(self.orientation_params_disp,
1037                                     state.orientation_params_disp)
1038        self._reset_parameters_state(self.orientation_params,
1039                                     state.orientation_params)
1040        self._reset_parameters_state(self.str_parameters,
1041                                     state.str_parameters)
1042        self._reset_parameters_state(self.parameters,state.parameters)
1043         ## display dispersion info layer       
1044        self.enable_disp.SetValue(state.enable_disp)
1045        self.disable_disp.SetValue(state.disable_disp)
1046       
1047        if hasattr(self, "disp_box"):
1048           
1049            self.disp_box.SetSelection(state.disp_box) 
1050            n= self.disp_box.GetCurrentSelection()
1051            dispersity= self.disp_box.GetClientData(n)
1052            name = dispersity.__name__     
1053
1054            self._set_dipers_Param(event=None)
1055       
1056            if name == "ArrayDispersion":
1057               
1058                for item in self.disp_cb_dict.keys():
1059                   
1060                    if hasattr(self.disp_cb_dict[item], "SetValue") :
1061                        self.disp_cb_dict[item].SetValue(\
1062                                                    state.disp_cb_dict[item])
1063                        # Create the dispersion objects
1064                        from sans.models.dispersion_models import ArrayDispersion
1065                        disp_model = ArrayDispersion()
1066                        if hasattr(state,"values")and\
1067                                 self.disp_cb_dict[item].GetValue() == True:
1068                            if len(state.values)>0:
1069                                self.values=state.values
1070                                self.weights=state.weights
1071                                disp_model.set_weights(self.values,
1072                                                        state.weights)
1073                            else:
1074                                self._reset_dispersity()
1075                       
1076                        self._disp_obj_dict[item] = disp_model
1077                        # Set the new model as the dispersion object
1078                        #for the selected parameter
1079                        self.model.set_dispersion(item, disp_model)
1080                   
1081                        self.model._persistency_dict[item] = \
1082                                                [state.values, state.weights]
1083                   
1084            else:
1085                keys = self.model.getParamList()
1086                for item in keys:
1087                    if item in self.disp_list and \
1088                        not self.model.details.has_key(item):
1089                        self.model.details[item] = ["", None, None]
1090                for k,v in self.state.disp_cb_dict.iteritems():
1091                    self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) 
1092                    self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict)
1093         ## smearing info  restore
1094        if hasattr(self, "enable_smearer"):
1095            ## set smearing value whether or not the data
1096            #contain the smearing info
1097            self.enable_smearer.SetValue(state.enable_smearer)
1098            self.disable_smearer.SetValue(state.disable_smearer)
1099            self.onSmear(event=None)           
1100        self.pinhole_smearer.SetValue(state.pinhole_smearer)
1101        self.slit_smearer.SetValue(state.slit_smearer)
1102       
1103        self.dI_noweight.SetValue(state.dI_noweight)
1104        self.dI_didata.SetValue(state.dI_didata)
1105        self.dI_sqrdata.SetValue(state.dI_sqrdata)
1106        self.dI_idata.SetValue(state.dI_idata)
1107       
1108        ## we have two more options for smearing
1109        if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None)
1110        elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None)
1111       
1112        ## reset state of checkbox,textcrtl  and dispersity parameters value
1113        self._reset_parameters_state(self.fittable_param,state.fittable_param)
1114        self._reset_parameters_state(self.fixed_param,state.fixed_param)
1115       
1116        ## draw the model with previous parameters value
1117        self._onparamEnter_helper()
1118        self.select_param(event=None) 
1119        #Save state_fit
1120        self.save_current_state_fit()
1121        self._lay_out()
1122        self.Refresh()
1123       
1124    def reset_page_helper(self, state):
1125        """
1126        Use page_state and change the state of existing page
1127       
1128        :precondition: the page is already drawn or created
1129       
1130        :postcondition: the state of the underlying data change as well as the
1131            state of the graphic interface
1132        """
1133        if state == None:
1134            #self._undo.Enable(False)
1135            return 
1136        # set data, etc. from the state
1137        # reset page between theory and fitting from bookmarking
1138        #if state.data == None:
1139        #    data = None
1140        #else:
1141        data = state.data
1142
1143        #if data != None:
1144       
1145        if data == None:
1146            data_min = state.qmin
1147            data_max = state.qmax
1148            self.qmin_x = data_min
1149            self.qmax_x = data_max
1150            #self.minimum_q.SetValue(str(data_min))
1151            #self.maximum_q.SetValue(str(data_max))
1152            self.qmin.SetValue(str(data_min))
1153            self.qmax.SetValue(str(data_max))
1154
1155            self.state.data = data
1156            self.state.qmin = self.qmin_x
1157            self.state.qmax = self.qmax_x
1158        else:
1159            self.set_data(data)
1160           
1161        self.enable2D= state.enable2D
1162        self.engine_type = state.engine_type
1163
1164        self.disp_cb_dict = state.disp_cb_dict
1165        self.disp_list = state.disp_list
1166     
1167        ## set the state of the radio box
1168        self.shape_rbutton.SetValue(state.shape_rbutton )
1169        self.shape_indep_rbutton.SetValue(state.shape_indep_rbutton)
1170        self.struct_rbutton.SetValue(state.struct_rbutton)
1171        self.plugin_rbutton.SetValue(state.plugin_rbutton)
1172       
1173        ## fill model combobox
1174        self._show_combox_helper()
1175        #select the current model
1176        self.formfactorbox.Select(int(state.formfactorcombobox))
1177        self.structurebox.SetSelection(state.structurecombobox )
1178        if state.multi_factor != None:
1179            self.multifactorbox.SetSelection(state.multi_factor)
1180
1181        #reset the fitting engine type
1182        self.engine_type = state.engine_type
1183        #draw the pnael according to the new model parameter
1184        self._on_select_model(event=None)
1185        # take care of 2D button
1186        if data == None and self.model_view.IsEnabled():
1187            if self.enable2D:
1188                self.model_view.SetLabel("2D Mode")
1189            else:
1190                self.model_view.SetLabel("1D Mode")
1191        # else:
1192               
1193        if self._manager !=None:
1194            self._manager._on_change_engine(engine=self.engine_type)
1195        ## set the select all check box to the a given state
1196        self.cb1.SetValue(state.cb1)
1197     
1198        ## reset state of checkbox,textcrtl  and  regular parameters value
1199        self._reset_parameters_state(self.orientation_params_disp,
1200                                     state.orientation_params_disp)
1201        self._reset_parameters_state(self.orientation_params,
1202                                     state.orientation_params)
1203        self._reset_parameters_state(self.str_parameters,
1204                                     state.str_parameters)
1205        self._reset_parameters_state(self.parameters,state.parameters)   
1206         ## display dispersion info layer       
1207        self.enable_disp.SetValue(state.enable_disp)
1208        self.disable_disp.SetValue(state.disable_disp)
1209        # If the polydispersion is ON
1210        if state.enable_disp:
1211            # reset dispersion according the state
1212            self._set_dipers_Param(event=None)
1213            self._reset_page_disp_helper(state)
1214        ##plotting range restore   
1215        self._reset_plotting_range(state)
1216        ## smearing info  restore
1217        if hasattr(self, "enable_smearer"):
1218            ## set smearing value whether or not the data
1219            #contain the smearing info
1220            self.enable_smearer.SetValue(state.enable_smearer)
1221            self.disable_smearer.SetValue(state.disable_smearer)
1222            self.onSmear(event=None)           
1223        self.pinhole_smearer.SetValue(state.pinhole_smearer)
1224        self.slit_smearer.SetValue(state.slit_smearer)
1225        try:
1226            self.dI_noweight.SetValue(state.dI_noweight)
1227            self.dI_didata.SetValue(state.dI_didata)
1228            self.dI_sqrdata.SetValue(state.dI_sqrdata)
1229            self.dI_idata.SetValue(state.dI_idata)
1230        except:
1231            # to support older state file formats
1232            self.dI_noweight.SetValue(False)
1233            self.dI_didata.SetValue(True)
1234            self.dI_sqrdata.SetValue(False)
1235            self.dI_idata.SetValue(False)
1236 
1237        ## we have two more options for smearing
1238        if self.pinhole_smearer.GetValue(): self.onPinholeSmear(event=None)
1239        elif self.slit_smearer.GetValue(): self.onSlitSmear(event=None)
1240       
1241        ## reset state of checkbox,textcrtl  and dispersity parameters value
1242        self._reset_parameters_state(self.fittable_param,state.fittable_param)
1243        self._reset_parameters_state(self.fixed_param,state.fixed_param)
1244       
1245        ## draw the model with previous parameters value
1246        self._onparamEnter_helper()
1247        #reset the value of chisqr when not consistent with the value computed
1248        self.tcChi.SetValue(str(self.state.tcChi))
1249        ## reset context menu items
1250        self._reset_context_menu()
1251       
1252        ## set the value of the current state to the state given as parameter
1253        self.state = state.clone() 
1254   
1255    def _reset_page_disp_helper(self, state):
1256        """
1257        Help to rest page for dispersions
1258        """
1259        keys = self.model.getParamList()
1260        for item in keys:
1261            if item in self.disp_list and \
1262                not self.model.details.has_key(item):
1263                self.model.details[item] = ["", None, None]
1264        #for k,v in self.state.disp_cb_dict.iteritems():
1265        self.disp_cb_dict = copy.deepcopy(state.disp_cb_dict) 
1266        self.state.disp_cb_dict = copy.deepcopy(state.disp_cb_dict)
1267        self.values = copy.deepcopy(state.values)
1268        self.weights = copy.deepcopy(state.weights)
1269       
1270        for key, disp in state._disp_obj_dict.iteritems():
1271            # From saved file, disp_model can not be sent in model obj.
1272            # it will be sent as a string here, then converted to model object.
1273            if disp.__class__.__name__ == 'str':
1274                com_str  = "from sans.models.dispersion_models "
1275                com_str += "import %s as disp_func"
1276                exec com_str % disp
1277                disp_model = disp_func()
1278            else:
1279                disp_model = disp
1280
1281            self._disp_obj_dict[key] = disp_model
1282            param_name = key.split('.')[0]
1283            # Try to set dispersion only when available
1284            # for eg., pass the orient. angles for 1D Cal
1285            try:
1286                self.model.set_dispersion(param_name, disp_model)
1287                self.model._persistency_dict[key] = \
1288                                 [state.values, state.weights]
1289            except:
1290                pass
1291            selection = self._find_polyfunc_selection(disp_model)
1292            for list in self.fittable_param:
1293                if list[1] == key and list[7] != None:
1294                    list[7].SetSelection(selection)
1295                    # For the array disp_model, set the values and weights
1296                    if selection == 1:
1297                        disp_model.set_weights(self.values[key], 
1298                                              self.weights[key])
1299                        try:
1300                            # Diables all fittable params for array
1301                            list[0].SetValue(False)
1302                            list[0].Disable()
1303                            list[2].Disable()
1304                            list[5].Disable()
1305                            list[6].Disable()
1306                        except:
1307                            pass
1308            # For array, disable all fixed params
1309            if selection == 1:
1310                for item in self.fixed_param:
1311                    if item[1].split(".")[0] == key.split(".")[0]:
1312                        # try it and pass it for the orientation for 1D
1313                        try:
1314                            item[2].Disable()
1315                        except:
1316                            pass
1317   
1318        # Make sure the check box updated when all checked
1319        if self.cb1.GetValue():
1320            self.select_all_param(None)       
1321     
1322    def _selectDlg(self):
1323        """
1324        open a dialog file to selected the customized dispersity
1325        """
1326        import os
1327        if self.parent !=  None:
1328            self._default_save_location = \
1329                        self.parent.parent._default_save_location
1330        dlg = wx.FileDialog(self, "Choose a weight file",
1331                                self._default_save_location , "", 
1332                                "*.*", wx.OPEN)
1333        path = None
1334        if dlg.ShowModal() == wx.ID_OK:
1335            path = dlg.GetPath()
1336        dlg.Destroy()
1337        return path
1338
1339    def _reset_context_menu(self):
1340        """
1341        reset the context menu
1342        """
1343        for name, state in self.state.saved_states.iteritems():
1344            self.number_saved_state += 1
1345            ## Add item in the context menu
1346            id = wx.NewId()
1347            msg = 'Save model and state %g' % self.number_saved_state
1348            self.popUpMenu.Append(id, name, msg)
1349            wx.EVT_MENU(self, id, self.onResetModel)
1350   
1351    def _reset_plotting_range(self, state):
1352        """
1353        Reset the plotting range to a given state
1354        """
1355        # if self.check_invalid_panel():
1356        #    return
1357        self.qmin.SetValue(str(state.qmin))
1358        self.qmax.SetValue(str(state.qmax)) 
1359
1360    def _save_typeOfmodel(self):
1361        """
1362        save radiobutton containing the type model that can be selected
1363        """
1364        self.state.shape_rbutton = self.shape_rbutton.GetValue()
1365        self.state.shape_indep_rbutton = self.shape_indep_rbutton.GetValue()
1366        self.state.struct_rbutton = self.struct_rbutton.GetValue()
1367        self.state.plugin_rbutton = self.plugin_rbutton.GetValue()
1368        self.state.structurebox= self.structurebox.GetCurrentSelection()
1369        self.state.formfactorbox = self.formfactorbox.GetCurrentSelection()
1370       
1371        #self._undo.Enable(True)
1372        ## post state to fit panel
1373        event = PageInfoEvent(page = self)
1374        wx.PostEvent(self.parent, event)
1375       
1376    def _save_plotting_range(self ):
1377        """
1378        save the state of plotting range
1379        """
1380        self.state.qmin = self.qmin_x
1381        self.state.qmax = self.qmax_x
1382        self.state.npts = self.npts_x
1383           
1384    def _onparamEnter_helper(self):
1385        """
1386        check if values entered by the user are changed and valid to replot
1387        model
1388        """
1389        # Flag to register when a parameter has changed.   
1390        is_modified = False
1391        self.fitrange = True
1392        is_2Ddata = False
1393        #self._undo.Enable(True)
1394        # check if 2d data
1395        if self.data.__class__.__name__ == "Data2D":
1396            is_2Ddata = True
1397        if self.model !=None:
1398            try:
1399                is_modified = self._check_value_enter(self.fittable_param,
1400                                                     is_modified)
1401                is_modified = self._check_value_enter(self.fixed_param,
1402                                                      is_modified)
1403                is_modified = self._check_value_enter(self.parameters,
1404                                                      is_modified) 
1405            except:
1406                pass
1407            #if is_modified:
1408
1409            # Here we should check whether the boundaries have been modified.
1410            # If qmin and qmax have been modified, update qmin and qmax and
1411            # set the is_modified flag to True
1412            if self._validate_qrange(self.qmin, self.qmax):
1413                tempmin = float(self.qmin.GetValue())
1414                if tempmin != self.qmin_x:
1415                    self.qmin_x = tempmin
1416                    is_modified = True
1417                tempmax = float(self.qmax.GetValue())
1418                if tempmax != self.qmax_x:
1419                    self.qmax_x = tempmax
1420                    is_modified = True
1421           
1422                if is_2Ddata:
1423                    # set mask   
1424                    is_modified = self._validate_Npts()
1425                   
1426            else:
1427                self.fitrange = False   
1428           
1429            ## if any value is modify draw model with new value
1430            if not self.fitrange:
1431                #self.btFit.Disable()
1432                if is_2Ddata: self.btEditMask.Disable()
1433            else:
1434                #self.btFit.Enable(True)
1435                if is_2Ddata: self.btEditMask.Enable(True)
1436            if is_modified and self.fitrange:
1437                #if self.data == None:
1438                # Theory case: need to get npts value to draw
1439                self.npts_x = float(self.Npts_total.GetValue())
1440                self.create_default_data()
1441                self.state_change= True
1442                self._draw_model() 
1443                self.Refresh()
1444        return is_modified
1445   
1446    def _update_paramv_on_fit(self):
1447        """
1448        make sure that update param values just before the fitting
1449        """
1450        #flag for qmin qmax check values
1451        flag = True
1452        self.fitrange = True
1453        is_modified = False
1454
1455        #wx.PostEvent(self._manager.parent, StatusEvent(status=" \
1456        #updating ... ",type="update"))
1457
1458        ##So make sure that update param values on_Fit.
1459        #self._undo.Enable(True)
1460        if self.model !=None:           
1461            ##Check the values
1462            self._check_value_enter( self.fittable_param ,is_modified)
1463            self._check_value_enter( self.fixed_param ,is_modified)
1464            self._check_value_enter( self.parameters ,is_modified)
1465
1466            # If qmin and qmax have been modified, update qmin and qmax and
1467             # Here we should check whether the boundaries have been modified.
1468            # If qmin and qmax have been modified, update qmin and qmax and
1469            # set the is_modified flag to True
1470            self.fitrange = self._validate_qrange(self.qmin, self.qmax)
1471            if self.fitrange:
1472                tempmin = float(self.qmin.GetValue())
1473                if tempmin != self.qmin_x:
1474                    self.qmin_x = tempmin
1475                tempmax = float(self.qmax.GetValue())
1476                if tempmax != self.qmax_x:
1477                    self.qmax_x = tempmax
1478                if tempmax == tempmin:
1479                    flag = False   
1480                temp_smearer = None
1481                if not self.disable_smearer.GetValue():
1482                    temp_smearer= self.current_smearer
1483                    if self.slit_smearer.GetValue():
1484                        flag = self.update_slit_smear()
1485                    elif self.pinhole_smearer.GetValue():
1486                        flag = self.update_pinhole_smear()
1487                    else:
1488                        self._manager.set_smearer(smearer=temp_smearer,
1489                                                  uid=self.uid,
1490                                                  fid=self.data.id,
1491                                                     qmin=float(self.qmin_x),
1492                                                      qmax=float(self.qmax_x),
1493                            enable_smearer=not self.disable_smearer.GetValue(),
1494                                                      draw=False)
1495                elif not self._is_2D():
1496                    self._manager.set_smearer(smearer=temp_smearer,
1497                                              qmin=float(self.qmin_x),
1498                                              uid=self.uid, 
1499                                              fid=self.data.id,
1500                                                 qmax= float(self.qmax_x),
1501                            enable_smearer=not self.disable_smearer.GetValue(),
1502                                                 draw=False)
1503                    if self.data != None:
1504                        index_data = ((self.qmin_x <= self.data.x)&\
1505                                      (self.data.x <= self.qmax_x))
1506                        val = str(len(self.data.x[index_data==True]))
1507                        self.Npts_fit.SetValue(val)
1508                    else:
1509                        # No data in the panel
1510                        try:
1511                            self.npts_x = float(self.Npts_total.GetValue())
1512                        except:
1513                            flag = False
1514                            return flag
1515                    flag = True
1516                if self._is_2D():
1517                    # only 2D case set mask 
1518                    flag = self._validate_Npts()
1519                    if not flag:
1520                        return flag
1521            else: flag = False
1522        else: 
1523            flag = False
1524
1525        #For invalid q range, disable the mask editor and fit button, vs.   
1526        if not self.fitrange:
1527            #self.btFit.Disable()
1528            if self._is_2D():
1529                self.btEditMask.Disable()
1530        else:
1531            #self.btFit.Enable(True)
1532            if self._is_2D() and  self.data != None:
1533                self.btEditMask.Enable(True)
1534
1535        if not flag:
1536            msg = "Cannot Plot or Fit :Must select a "
1537            msg += " model or Fitting range is not valid!!!  "
1538            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
1539       
1540        self.save_current_state()
1541   
1542        return flag                           
1543               
1544    def _is_modified(self, is_modified):
1545        """
1546        return to self._is_modified
1547        """
1548        return is_modified
1549                       
1550    def _reset_parameters_state(self, listtorestore, statelist):
1551        """
1552        Reset the parameters at the given state
1553        """
1554        if len(statelist) == 0 or len(listtorestore) == 0:
1555            return
1556        if len(statelist) !=  len(listtorestore):
1557            return
1558
1559        for j in range(len(listtorestore)):
1560            item_page = listtorestore[j]
1561            item_page_info = statelist[j]
1562            ##change the state of the check box for simple parameters
1563            if item_page[0]!=None:   
1564                item_page[0].SetValue(item_page_info[0])
1565            if item_page[2]!=None:
1566                item_page[2].SetValue(item_page_info[2])
1567                if item_page[2].__class__.__name__ == "ComboBox":
1568                   if self.model.fun_list.has_key(item_page_info[2]):
1569                       fun_val = self.model.fun_list[item_page_info[2]]
1570                       self.model.setParam(item_page_info[1],fun_val)
1571            if item_page[3]!=None:
1572                ## show or hide text +/-
1573                if item_page_info[2]:
1574                    item_page[3].Show(True)
1575                else:
1576                    item_page[3].Hide()
1577            if item_page[4]!=None:
1578                ## show of hide the text crtl for fitting error
1579                if item_page_info[4][0]:
1580                    item_page[4].Show(True)
1581                    item_page[4].SetValue(item_page_info[4][1])
1582                else:
1583                    item_page[3].Hide()
1584            if item_page[5]!=None:
1585                ## show of hide the text crtl for fitting error
1586                item_page[5].Show(item_page_info[5][0])
1587                item_page[5].SetValue(item_page_info[5][1])
1588               
1589            if item_page[6]!=None:
1590                ## show of hide the text crtl for fitting error
1591                item_page[6].Show(item_page_info[6][0])
1592                item_page[6].SetValue(item_page_info[6][1])
1593
1594                   
1595    def _reset_strparam_state(self, listtorestore, statelist):
1596        """
1597        Reset the string parameters at the given state
1598        """
1599        if len(statelist) == 0:
1600            return
1601
1602        listtorestore = copy.deepcopy(statelist)
1603       
1604        for j in range(len(listtorestore)):
1605            item_page = listtorestore[j]
1606            item_page_info = statelist[j]
1607            ##change the state of the check box for simple parameters
1608           
1609            if item_page[0] != None:   
1610                item_page[0].SetValue(format_number(item_page_info[0], True))
1611
1612            if item_page[2] != None:
1613                param_name = item_page_info[1]
1614                value = item_page_info[2]
1615                selection = value
1616                if self.model.fun_list.has_key(value):
1617                    selection = self.model.fun_list[value]
1618                item_page[2].SetValue(selection)
1619                self.model.setParam(param_name, selection)
1620                                     
1621    def _copy_parameters_state(self, listtocopy, statelist):
1622        """
1623        copy the state of button
1624       
1625        :param listtocopy: the list of check button to copy
1626        :param statelist: list of state object to store the current state
1627       
1628        """
1629        if len(listtocopy)==0:
1630            return
1631       
1632        for item in listtocopy:
1633 
1634            checkbox_state = None
1635            if item[0]!= None:
1636                checkbox_state= item[0].GetValue()
1637            parameter_name = item[1]
1638            parameter_value = None
1639            if item[2]!=None:
1640                parameter_value = item[2].GetValue()
1641            static_text = None
1642            if item[3]!=None:
1643                static_text = item[3].IsShown()
1644            error_value = None
1645            error_state = None
1646            if item[4]!= None:
1647                error_value = item[4].GetValue()
1648                error_state = item[4].IsShown()
1649               
1650            min_value = None
1651            min_state = None
1652            if item[5]!= None:
1653                min_value = item[5].GetValue()
1654                min_state = item[5].IsShown()
1655               
1656            max_value = None
1657            max_state = None
1658            if item[6]!= None:
1659                max_value = item[6].GetValue()
1660                max_state = item[6].IsShown()
1661            unit=None
1662            if item[7]!=None:
1663                unit = item[7].GetLabel()
1664               
1665            statelist.append([checkbox_state, parameter_name, parameter_value,
1666                              static_text ,[error_state, error_value],
1667                                [min_state, min_value],
1668                                [max_state, max_value], unit])
1669           
1670    def _set_model_sizer_selection(self, model):
1671        """
1672        Display the sizer according to the type of the current model
1673        """
1674        if model == None:
1675            return
1676        if hasattr(model ,"s_model"):
1677           
1678            class_name = model.s_model.__class__
1679            name = model.s_model.name
1680            flag = (name != "NoStructure")
1681            if flag and \
1682                (class_name in self.model_list_box["Structure Factors"]):
1683                self.structurebox.Show()
1684                self.text2.Show()               
1685                self.structurebox.Enable()
1686                self.text2.Enable()
1687                items = self.structurebox.GetItems()
1688                self.sizer1.Layout()
1689               
1690                for i in range(len(items)):
1691                    if items[i]== str(name):
1692                        self.structurebox.SetSelection(i)
1693                        break
1694                   
1695        if hasattr(model ,"p_model"):
1696            class_name = model.p_model.__class__
1697            name = model.p_model.name
1698            self.formfactorbox.Clear()
1699           
1700            for k, list in self.model_list_box.iteritems():
1701                if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]:
1702                    self.shape_rbutton.SetValue(True)
1703                    ## fill the form factor list with new model
1704                    self._populate_box(self.formfactorbox,self.model_list_box["Shapes"])
1705                    items = self.formfactorbox.GetItems()
1706                    ## set comboxbox to the selected item
1707                    for i in range(len(items)):
1708                        if items[i]== str(name):
1709                            self.formfactorbox.SetSelection(i)
1710                            break
1711                    return
1712                elif k == "Shape-Independent":
1713                    self.shape_indep_rbutton.SetValue(True)
1714                elif k == "Structure Factors":
1715                     self.struct_rbutton.SetValue(True)
1716                elif  k == "Multi-Functions":
1717                    continue
1718                else:
1719                    self.plugin_rbutton.SetValue(True)
1720               
1721                if class_name in list:
1722                    ## fill the form factor list with new model
1723                    self._populate_box(self.formfactorbox, list)
1724                    items = self.formfactorbox.GetItems()
1725                    ## set comboxbox to the selected item
1726                    for i in range(len(items)):
1727                        if items[i]== str(name):
1728                            self.formfactorbox.SetSelection(i)
1729                            break
1730                    break
1731        else:
1732
1733            ## Select the model from the menu
1734            class_name = model.__class__
1735            name = model.name
1736            self.formfactorbox.Clear()
1737            items = self.formfactorbox.GetItems()
1738   
1739            for k, list in self.model_list_box.iteritems():         
1740                if k in["P(Q)*S(Q)","Shapes" ] and class_name in self.model_list_box["Shapes"]:
1741                    if class_name in self.model_list_box["P(Q)*S(Q)"]:
1742                        self.structurebox.Show()
1743                        self.text2.Show()
1744                        self.structurebox.Enable()
1745                        self.structurebox.SetSelection(0)
1746                        self.text2.Enable()
1747                    else:
1748                        self.structurebox.Hide()
1749                        self.text2.Hide()
1750                        self.structurebox.Disable()
1751                        self.structurebox.SetSelection(0)
1752                        self.text2.Disable()
1753                       
1754                    self.shape_rbutton.SetValue(True)
1755                    ## fill the form factor list with new model
1756                    self._populate_box(self.formfactorbox,self.model_list_box["Shapes"])
1757                    items = self.formfactorbox.GetItems()
1758                    ## set comboxbox to the selected item
1759                    for i in range(len(items)):
1760                        if items[i]== str(name):
1761                            self.formfactorbox.SetSelection(i)
1762                            break
1763                    return
1764                elif k == "Shape-Independent":
1765                    self.shape_indep_rbutton.SetValue(True)
1766                elif k == "Structure Factors":
1767                    self.struct_rbutton.SetValue(True)
1768                elif  k == "Multi-Functions":
1769                    continue
1770                else:
1771                    self.plugin_rbutton.SetValue(True)
1772                if class_name in list:
1773                    self.structurebox.SetSelection(0)
1774                    self.structurebox.Disable()
1775                    self.text2.Disable()                   
1776                    ## fill the form factor list with new model
1777                    self._populate_box(self.formfactorbox, list)
1778                    items = self.formfactorbox.GetItems()
1779                    ## set comboxbox to the selected item
1780                    for i in range(len(items)):
1781                        if items[i]== str(name):
1782                            self.formfactorbox.SetSelection(i)
1783                            break
1784                    break
1785   
1786    def _draw_model(self, update_chisqr=True, source='model'):
1787        """
1788        Method to draw or refresh a plotted model.
1789        The method will use the data member from the model page
1790        to build a call to the fitting perspective manager.
1791       
1792        :param chisqr: update chisqr value [bool]
1793        """
1794        #if self.check_invalid_panel():
1795        #    return
1796        if self.model !=None:
1797            temp_smear=None
1798            if hasattr(self, "enable_smearer"):
1799                if not self.disable_smearer.GetValue():
1800                    temp_smear= self.current_smearer
1801            # compute weight for the current data
1802            from .utils import get_weight
1803            flag = self.get_weight_flag()
1804            weight = get_weight(data=self.data, is2d=self._is_2D(), flag=flag)
1805            toggle_mode_on = self.model_view.IsEnabled()
1806            is_2d = self._is_2D()
1807            self._manager.draw_model(self.model, 
1808                                    data=self.data,
1809                                    smearer= temp_smear,
1810                                    qmin=float(self.qmin_x), 
1811                                    qmax=float(self.qmax_x),
1812                                    page_id=self.uid,
1813                                    toggle_mode_on=toggle_mode_on, 
1814                                    state = self.state,
1815                                    enable2D=is_2d,
1816                                    update_chisqr=update_chisqr,
1817                                    source='model',
1818                                    weight=weight)
1819       
1820       
1821    def _on_show_sld(self, event=None):
1822        """
1823        Plot SLD profile
1824        """
1825        # get profile data
1826        x,y=self.model.getProfile()
1827
1828        from danse.common.plottools import Data1D
1829        #from sans.perspectives.theory.profile_dialog import SLDPanel
1830        from sans.guiframe.local_perspectives.plotting.profile_dialog \
1831        import SLDPanel
1832        sld_data = Data1D(x,y)
1833        sld_data.name = 'SLD'
1834        sld_data.axes = self.sld_axes
1835        self.panel = SLDPanel(self, data=sld_data, axes =self.sld_axes,id =-1)
1836        self.panel.ShowModal()   
1837       
1838    def _set_multfactor_combobox(self, multiplicity=10):   
1839        """
1840        Set comboBox for muitfactor of CoreMultiShellModel
1841        :param multiplicit: no. of multi-functionality
1842        """
1843        # build content of the combobox
1844        for idx in range(0,multiplicity):
1845            self.multifactorbox.Append(str(idx),int(idx))
1846            #self.multifactorbox.SetSelection(1)
1847        self._hide_multfactor_combobox()
1848       
1849    def _show_multfactor_combobox(self):   
1850        """
1851        Show the comboBox of muitfactor of CoreMultiShellModel
1852        """ 
1853        if not self.mutifactor_text.IsShown():
1854            self.mutifactor_text.Show(True)
1855            self.mutifactor_text1.Show(True)
1856        if not self.multifactorbox.IsShown():
1857            self.multifactorbox.Show(True) 
1858             
1859    def _hide_multfactor_combobox(self):   
1860        """
1861        Hide the comboBox of muitfactor of CoreMultiShellModel
1862        """ 
1863        if self.mutifactor_text.IsShown():
1864            self.mutifactor_text.Hide()
1865            self.mutifactor_text1.Hide()
1866        if self.multifactorbox.IsShown():
1867            self.multifactorbox.Hide()   
1868
1869       
1870    def _show_combox_helper(self):
1871        """
1872        Fill panel's combo box according to the type of model selected
1873        """
1874        if self.shape_rbutton.GetValue():
1875            ##fill the combobox with form factor list
1876            self.structurebox.SetSelection(0)
1877            self.structurebox.Disable()
1878            self.formfactorbox.Clear()
1879            self._populate_box( self.formfactorbox,self.model_list_box["Shapes"])
1880        if self.shape_indep_rbutton.GetValue():
1881            ##fill the combobox with shape independent  factor list
1882            self.structurebox.SetSelection(0)
1883            self.structurebox.Disable()
1884            self.formfactorbox.Clear()
1885            self._populate_box( self.formfactorbox,
1886                                self.model_list_box["Shape-Independent"])
1887        if self.struct_rbutton.GetValue():
1888            ##fill the combobox with structure factor list
1889            self.structurebox.SetSelection(0)
1890            self.structurebox.Disable()
1891            self.formfactorbox.Clear()
1892            self._populate_box( self.formfactorbox,
1893                                self.model_list_box["Structure Factors"])
1894        if self.plugin_rbutton.GetValue():
1895            ##fill the combobox with form factor list
1896            self.structurebox.Disable()
1897            self.formfactorbox.Clear()
1898            self._populate_box( self.formfactorbox,
1899                                self.model_list_box["Customized Models"])
1900       
1901    def _show_combox(self, event=None):
1902        """
1903        Show combox box associate with type of model selected
1904        """
1905        #if self.check_invalid_panel():
1906        #    self.shape_rbutton.SetValue(True)
1907        #    return
1908
1909        self._show_combox_helper()
1910        self._on_select_model(event=None)
1911        self._save_typeOfmodel()
1912        self.sizer4_4.Layout()
1913        self.sizer4.Layout()
1914        self.Layout()
1915        self.Refresh()
1916 
1917    def _populate_box(self, combobox, list):
1918        """
1919        fill combox box with dict item
1920       
1921        :param list: contains item to fill the combox
1922            item must model class
1923        """
1924        for models in list:
1925            model= models()
1926            name = model.__class__.__name__
1927            if models.__name__!="NoStructure":
1928                if hasattr(model, "name"):
1929                    name = model.name
1930                combobox.Append(name,models)
1931        return 0
1932   
1933    def _onQrangeEnter(self, event):
1934        """
1935        Check validity of value enter in the Q range field
1936       
1937        """
1938        tcrtl = event.GetEventObject()
1939        #Clear msg if previously shown.
1940        msg = ""
1941        wx.PostEvent(self.parent, StatusEvent(status=msg))
1942        # Flag to register when a parameter has changed.
1943        is_modified = False
1944        if tcrtl.GetValue().lstrip().rstrip() != "":
1945            try:
1946                value = float(tcrtl.GetValue())
1947                tcrtl.SetBackgroundColour(wx.WHITE)
1948                # If qmin and qmax have been modified, update qmin and qmax
1949                if self._validate_qrange(self.qmin, self.qmax):
1950                    tempmin = float(self.qmin.GetValue())
1951                    if tempmin != self.qmin_x:
1952                        self.qmin_x = tempmin
1953                    tempmax = float(self.qmax.GetValue())
1954                    if tempmax != self.qmax_x:
1955                        self.qmax_x = tempmax
1956                else:
1957                    tcrtl.SetBackgroundColour("pink")
1958                    msg = "Model Error:wrong value entered : %s" % sys.exc_value
1959                    wx.PostEvent(self.parent, StatusEvent(status=msg))
1960                    return 
1961            except:
1962                tcrtl.SetBackgroundColour("pink")
1963                msg = "Model Error:wrong value entered : %s" % sys.exc_value
1964                wx.PostEvent(self.parent, StatusEvent(status=msg))
1965                return 
1966            #Check if # of points for theory model are valid(>0).
1967            if self.npts != None:
1968                if check_float(self.npts):
1969                    temp_npts = float(self.npts.GetValue())
1970                    if temp_npts !=  self.num_points:
1971                        self.num_points = temp_npts
1972                        is_modified = True
1973                else:
1974                    msg = "Cannot Plot :No npts in that Qrange!!!  "
1975                    wx.PostEvent(self.parent, StatusEvent(status=msg))
1976        else:
1977           tcrtl.SetBackgroundColour("pink")
1978           msg = "Model Error:wrong value entered!!!"
1979           wx.PostEvent(self.parent, StatusEvent(status=msg))
1980        #self._undo.Enable(True)
1981        self.save_current_state()
1982        event = PageInfoEvent(page=self)
1983        wx.PostEvent(self.parent, event)
1984        self.state_change = False
1985        #Draw the model for a different range
1986        self.create_default_data()
1987        self._draw_model()
1988                   
1989    def _theory_qrange_enter(self, event):
1990        """
1991        Check validity of value enter in the Q range field
1992        """
1993       
1994        tcrtl= event.GetEventObject()
1995        #Clear msg if previously shown.
1996        msg= ""
1997        wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
1998        # Flag to register when a parameter has changed.
1999        is_modified = False
2000        if tcrtl.GetValue().lstrip().rstrip()!="":
2001            try:
2002                value = float(tcrtl.GetValue())
2003                tcrtl.SetBackgroundColour(wx.WHITE)
2004
2005                # If qmin and qmax have been modified, update qmin and qmax
2006                if self._validate_qrange(self.theory_qmin, self.theory_qmax):
2007                    tempmin = float(self.theory_qmin.GetValue())
2008                    if tempmin != self.theory_qmin_x:
2009                        self.theory_qmin_x = tempmin
2010                    tempmax = float(self.theory_qmax.GetValue())
2011                    if tempmax != self.qmax_x:
2012                        self.theory_qmax_x = tempmax
2013                else:
2014                    tcrtl.SetBackgroundColour("pink")
2015                    msg= "Model Error:wrong value entered : %s"% sys.exc_value
2016                    wx.PostEvent(self._manager.parent, StatusEvent(status = msg ))
2017                    return 
2018            except:
2019                tcrtl.SetBackgroundColour("pink")
2020                msg= "Model Error:wrong value entered : %s"% sys.exc_value
2021                wx.PostEvent(self._manager.parent, StatusEvent(status = msg ))
2022                return 
2023            #Check if # of points for theory model are valid(>0).
2024            if self.Npts_total.IsEditable() :
2025                if check_float(self.Npts_total):
2026                    temp_npts = float(self.Npts_total.GetValue())
2027                    if temp_npts !=  self.num_points:
2028                        self.num_points = temp_npts
2029                        is_modified = True
2030                else:
2031                    msg= "Cannot Plot :No npts in that Qrange!!!  "
2032                    wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
2033        else:
2034           tcrtl.SetBackgroundColour("pink")
2035           msg = "Model Error:wrong value entered!!!"
2036           wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
2037        #self._undo.Enable(True)
2038        self.save_current_state()
2039        event = PageInfoEvent(page = self)
2040        wx.PostEvent(self.parent, event)
2041        self.state_change= False
2042        #Draw the model for a different range
2043        self.create_default_data()
2044        self._draw_model()
2045                   
2046    def _on_select_model_helper(self): 
2047        """
2048        call back for model selection
2049        """
2050        ## reset dictionary containing reference to dispersion
2051        self._disp_obj_dict = {}
2052        self.disp_cb_dict ={}
2053        self.temp_multi_functional = False
2054        f_id = self.formfactorbox.GetCurrentSelection()
2055        #For MAC
2056        form_factor = None
2057        if f_id >= 0:
2058            form_factor = self.formfactorbox.GetClientData(f_id)
2059
2060        if not form_factor in  self.model_list_box["multiplication"]:
2061            self.structurebox.Hide()
2062            self.text2.Hide()           
2063            self.structurebox.Disable()
2064            self.structurebox.SetSelection(0)
2065            self.text2.Disable()
2066        else:
2067            self.structurebox.Show()
2068            self.text2.Show()
2069            self.structurebox.Enable()
2070            self.text2.Enable()
2071           
2072        if form_factor != None:   
2073            # set multifactor for Mutifunctional models   
2074            if form_factor().__class__ in self.model_list_box["Multi-Functions"]:
2075                m_id = self.multifactorbox.GetCurrentSelection()
2076                multiplicity = form_factor().multiplicity_info[0]
2077                self.multifactorbox.Clear()
2078                #self.mutifactor_text.SetLabel(form_factor().details[])
2079                self._set_multfactor_combobox(multiplicity)
2080                self._show_multfactor_combobox()
2081                #ToDo:  this info should be called directly from the model
2082                text = form_factor().multiplicity_info[1]#'No. of Shells: '
2083
2084                #self.mutifactor_text.Clear()
2085                self.mutifactor_text.SetLabel(text)
2086                if m_id > multiplicity -1:
2087                    # default value
2088                    m_id = 1
2089                   
2090                self.multi_factor = self.multifactorbox.GetClientData(m_id)
2091                if self.multi_factor == None: self.multi_factor =0
2092                form_factor = form_factor(int(self.multi_factor))
2093                self.multifactorbox.SetSelection(m_id)
2094                # Check len of the text1 and max_multiplicity
2095                text = ''
2096                if form_factor.multiplicity_info[0] == len(form_factor.multiplicity_info[2]):
2097                    text = form_factor.multiplicity_info[2][self.multi_factor]
2098                self.mutifactor_text1.SetLabel(text)
2099                # Check if model has  get sld profile.
2100                if len(form_factor.multiplicity_info[3]) > 0:
2101                    self.sld_axes = form_factor.multiplicity_info[3]
2102                    self.show_sld_button.Show(True)
2103                else:
2104                    self.sld_axes = ""
2105
2106            else:
2107                self._hide_multfactor_combobox()
2108                self.show_sld_button.Hide()
2109                form_factor = form_factor()
2110                self.multi_factor = None
2111        else:
2112            self._hide_multfactor_combobox()
2113            self.show_sld_button.Hide()
2114            self.multi_factor = None 
2115             
2116        s_id = self.structurebox.GetCurrentSelection()
2117        struct_factor = self.structurebox.GetClientData( s_id )
2118       
2119        if  struct_factor !=None:
2120            from sans.models.MultiplicationModel import MultiplicationModel
2121            self.model= MultiplicationModel(form_factor,struct_factor())
2122            # multifunctional form factor
2123            if len(form_factor.non_fittable) > 0:
2124                self.temp_multi_functional = True
2125        else:
2126            if form_factor != None:
2127                self.model= form_factor
2128            else:
2129                self.model = None
2130                return self.model
2131           
2132
2133        ## post state to fit panel
2134        self.state.parameters =[]
2135        self.state.model =self.model
2136        self.state.qmin = self.qmin_x
2137        self.state.multi_factor = self.multi_factor
2138        self.disp_list =self.model.getDispParamList()
2139        self.state.disp_list = self.disp_list
2140        self.on_set_focus(None)
2141        self.Layout()     
2142       
2143    def _validate_qrange(self, qmin_ctrl, qmax_ctrl):
2144        """
2145        Verify that the Q range controls have valid values
2146        and that Qmin < Qmax.
2147       
2148        :param qmin_ctrl: text control for Qmin
2149        :param qmax_ctrl: text control for Qmax
2150       
2151        :return: True is the Q range is value, False otherwise
2152       
2153        """
2154        qmin_validity = check_float(qmin_ctrl)
2155        qmax_validity = check_float(qmax_ctrl)
2156        if not (qmin_validity and qmax_validity):
2157            return False
2158        else:
2159            qmin = float(qmin_ctrl.GetValue())
2160            qmax = float(qmax_ctrl.GetValue())
2161            if qmin < qmax:
2162                #Make sure to set both colours white. 
2163                qmin_ctrl.SetBackgroundColour(wx.WHITE)
2164                qmin_ctrl.Refresh()
2165                qmax_ctrl.SetBackgroundColour(wx.WHITE)
2166                qmax_ctrl.Refresh()
2167            else:
2168                qmin_ctrl.SetBackgroundColour("pink")
2169                qmin_ctrl.Refresh()
2170                qmax_ctrl.SetBackgroundColour("pink")
2171                qmax_ctrl.Refresh()
2172                msg= "Invalid Q range: Q min must be smaller than Q max"
2173                wx.PostEvent(self.parent.parent, StatusEvent(status = msg))
2174                return False
2175        return True
2176   
2177    def _validate_Npts(self): 
2178        """
2179        Validate the number of points for fitting is more than 10 points.
2180        If valid, setvalues Npts_fit otherwise post msg.
2181        """
2182        #default flag
2183        flag = True
2184        # Theory
2185        if self.data == None and self.enable2D:
2186            return flag
2187
2188        # q value from qx and qy
2189        radius= numpy.sqrt( self.data.qx_data * self.data.qx_data + 
2190                            self.data.qy_data * self.data.qy_data )
2191        #get unmasked index
2192        index_data = (float(self.qmin.GetValue()) <= radius) & \
2193                        (radius <= float(self.qmax.GetValue()))
2194        index_data = (index_data) & (self.data.mask) 
2195        index_data = (index_data) & (numpy.isfinite(self.data.data))
2196
2197        if len(index_data[index_data]) < 10:
2198            # change the color pink.
2199            self.qmin.SetBackgroundColour("pink")
2200            self.qmin.Refresh()
2201            self.qmax.SetBackgroundColour("pink")
2202            self.qmax.Refresh()
2203            msg= "Cannot Plot :No or too little npts in that data range!!!  "
2204            wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
2205            self.fitrange = False
2206            flag = False
2207        else:
2208            self.Npts_fit.SetValue(str(len(self.data.mask[index_data==True])))
2209            self.fitrange = True
2210           
2211        return flag
2212   
2213    def _check_value_enter(self, list, modified):
2214        """
2215        :param list: model parameter and panel info
2216        :Note: each item of the list should be as follow:
2217            item=[check button state, parameter's name,
2218                paramater's value, string="+/-",
2219                parameter's error of fit,
2220                parameter's minimum value,
2221                parrameter's maximum value ,
2222                parameter's units]
2223        """ 
2224        is_modified =  modified
2225        if len(list)==0:
2226            return is_modified
2227        for item in list:
2228            #skip angle parameters for 1D
2229            if not self.enable2D:#self.data.__class__.__name__ !="Data2D":
2230                if item in self.orientation_params:
2231                    continue
2232            #try:
2233            name = str(item[1])
2234           
2235            if string.find(name,".npts") ==-1 and \
2236                                        string.find(name,".nsigmas")==-1:     
2237                ## check model parameters range             
2238                param_min= None
2239                param_max= None
2240               
2241                ## check minimun value
2242                if item[5]!= None and item[5]!= "":
2243                    if item[5].GetValue().lstrip().rstrip()!="":
2244                        try:
2245                           
2246                            param_min = float(item[5].GetValue())
2247                            if not self._validate_qrange(item[5],item[2]):
2248                                if numpy.isfinite(param_min):
2249                                    item[2].SetValue(format_number(param_min))
2250                           
2251                            item[5].SetBackgroundColour(wx.WHITE)
2252                            item[2].SetBackgroundColour(wx.WHITE)
2253                                           
2254                        except:
2255                            msg = "Wrong Fit parameter range entered "
2256                            wx.PostEvent(self.parent.parent, 
2257                                         StatusEvent(status = msg))
2258                            raise ValueError, msg
2259                        is_modified = True
2260                ## check maximum value
2261                if item[6]!= None and item[6]!= "":
2262                    if item[6].GetValue().lstrip().rstrip()!="":
2263                        try:                         
2264                            param_max = float(item[6].GetValue())
2265                            if not self._validate_qrange(item[2],item[6]):
2266                                if numpy.isfinite(param_max):
2267                                    item[2].SetValue(format_number(param_max)) 
2268                           
2269                            item[6].SetBackgroundColour(wx.WHITE)
2270                            item[2].SetBackgroundColour(wx.WHITE)
2271                        except:
2272                            msg = "Wrong Fit parameter range entered "
2273                            wx.PostEvent(self.parent.parent, 
2274                                         StatusEvent(status = msg))
2275                            raise ValueError, msg
2276                        is_modified = True
2277               
2278
2279                if param_min != None and param_max !=None:
2280                    if not self._validate_qrange(item[5], item[6]):
2281                        msg= "Wrong Fit range entered for parameter "
2282                        msg+= "name %s of model %s "%(name, self.model.name)
2283                        wx.PostEvent(self.parent.parent, 
2284                                     StatusEvent(status = msg))
2285               
2286                if name in self.model.details.keys():   
2287                        self.model.details[name][1:3] = param_min, param_max
2288                        is_modified = True
2289             
2290                else:
2291                        self.model.details [name] = ["", param_min, param_max] 
2292                        is_modified = True
2293            try:   
2294                # Check if the textctr is enabled
2295                if item[2].IsEnabled():
2296                    value= float(item[2].GetValue())
2297                    item[2].SetBackgroundColour("white")
2298                    # If the value of the parameter has changed,
2299                    # +update the model and set the is_modified flag
2300                    if value != self.model.getParam(name) and \
2301                                                numpy.isfinite(value):
2302                        self.model.setParam(name, value)
2303                       
2304            except:
2305                item[2].SetBackgroundColour("pink")
2306                msg = "Wrong Fit parameter value entered "
2307                wx.PostEvent(self.parent.parent, StatusEvent(status = msg))
2308               
2309        return is_modified
2310       
2311 
2312    def _set_dipers_Param(self, event):
2313        """
2314        respond to self.enable_disp and self.disable_disp radio box.
2315        The dispersity object is reset inside the model into Gaussian.
2316        When the user select yes , this method display a combo box for more selection
2317        when the user selects No,the combo box disappears.
2318        Redraw the model with the default dispersity (Gaussian)
2319        """
2320        #if self.check_invalid_panel():
2321        #    return
2322        ## On selction if no model exists.
2323        if self.model ==None:
2324            self.disable_disp.SetValue(True)
2325            msg="Please select a Model first..."
2326            wx.MessageBox(msg, 'Info')
2327            wx.PostEvent(self._manager.parent, StatusEvent(status=\
2328                            "Polydispersion: %s"%msg))
2329            return
2330
2331        self._reset_dispersity()
2332   
2333        if self.model ==None:
2334            self.model_disp.Hide()
2335            self.sizer4_4.Clear(True)
2336            return
2337
2338        if self.enable_disp.GetValue():
2339            ## layout for model containing no dispersity parameters
2340           
2341            self.disp_list= self.model.getDispParamList()
2342             
2343            if len(self.disp_list)==0 and len(self.disp_cb_dict)==0:
2344                self._layout_sizer_noDipers() 
2345            else:
2346                ## set gaussian sizer
2347                self._on_select_Disp(event=None)
2348        else:
2349            self.sizer4_4.Clear(True)
2350           
2351        ## post state to fit panel
2352        self.save_current_state()
2353        if event !=None:
2354            #self._undo.Enable(True)
2355            event = PageInfoEvent(page = self)
2356            wx.PostEvent(self.parent, event)
2357        #draw the model with the current dispersity
2358        self._draw_model()
2359        self.sizer4_4.Layout()
2360        self.sizer5.Layout()
2361        self.Layout()
2362        self.Refresh()     
2363         
2364       
2365    def _layout_sizer_noDipers(self):
2366        """
2367        Draw a sizer with no dispersity info
2368        """
2369        ix=0
2370        iy=1
2371        self.fittable_param=[]
2372        self.fixed_param=[]
2373        self.orientation_params_disp=[]
2374       
2375        self.sizer4_4.Clear(True)
2376        text = "No polydispersity available for this model"
2377        text = "No polydispersity available for this model"
2378        model_disp = wx.StaticText(self, -1, text)
2379        self.sizer4_4.Add(model_disp,( iy, ix),(1,1), 
2380                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
2381        self.sizer4_4.Layout()
2382        self.sizer4.Layout()
2383   
2384    def _reset_dispersity(self):
2385        """
2386        put gaussian dispersity into current model
2387        """
2388        if len(self.param_toFit)>0:
2389            for item in self.fittable_param:
2390                if item in self.param_toFit:
2391                    self.param_toFit.remove(item)
2392
2393            for item in self.orientation_params_disp:
2394                if item in self.param_toFit:
2395                    self.param_toFit.remove(item)
2396         
2397        self.fittable_param=[]
2398        self.fixed_param=[]
2399        self.orientation_params_disp=[]
2400        self.values={}
2401        self.weights={}
2402     
2403        from sans.models.dispersion_models import GaussianDispersion, ArrayDispersion
2404        if len(self.disp_cb_dict)==0:
2405            self.save_current_state()
2406            self.sizer4_4.Clear(True)
2407            self.Layout()
2408 
2409            return 
2410        if (len(self.disp_cb_dict)>0) :
2411            for p in self.disp_cb_dict:
2412                # The parameter was un-selected. Go back to Gaussian model (with 0 pts)                   
2413                disp_model = GaussianDispersion()
2414               
2415                self._disp_obj_dict[p] = disp_model
2416                # Set the new model as the dispersion object for the selected parameter
2417                try:
2418                   self.model.set_dispersion(p, disp_model)
2419                except:
2420
2421                    pass
2422
2423        ## save state into
2424        self.save_current_state()
2425        self.Layout() 
2426        self.Refresh()
2427                 
2428    def _on_select_Disp(self,event):
2429        """
2430        allow selecting different dispersion
2431        self.disp_list should change type later .now only gaussian
2432        """
2433        self._set_sizer_dispersion()
2434
2435        ## Redraw the model
2436        self._draw_model() 
2437        #self._undo.Enable(True)
2438        event = PageInfoEvent(page = self)
2439        wx.PostEvent(self.parent, event)
2440       
2441        self.sizer4_4.Layout()
2442        self.sizer4.Layout()
2443        self.SetupScrolling()
2444   
2445    def _on_disp_func(self, event=None): 
2446        """
2447        Select a distribution function for the polydispersion
2448       
2449        :Param event: ComboBox event
2450        """
2451        # get ready for new event
2452        if event != None:
2453            event.Skip()
2454        # Get event object
2455        disp_box =  event.GetEventObject() 
2456
2457        # Try to select a Distr. function
2458        try:   
2459            disp_box.SetBackgroundColour("white")
2460            selection = disp_box.GetCurrentSelection()
2461            param_name = disp_box.Name.split('.')[0]
2462            disp_name = disp_box.GetValue()
2463            dispersity= disp_box.GetClientData(selection)
2464   
2465            #disp_model =  GaussianDispersion()
2466            disp_model = dispersity()
2467            # Get param names to reset the values of the param
2468            name1 = param_name + ".width"
2469            name2 = param_name + ".npts"
2470            name3 = param_name + ".nsigmas"
2471            # Check Disp. function whether or not it is 'array'
2472            if disp_name.lower() == "array":
2473                value2= ""
2474                value3= ""
2475                value1 = self._set_array_disp(name=name1, disp=disp_model)
2476            else:
2477                self._del_array_values(name1)
2478                #self._reset_array_disp(param_name)
2479                self._disp_obj_dict[name1] = disp_model
2480                self.model.set_dispersion(param_name, disp_model)
2481                self.state._disp_obj_dict[name1]= disp_model
2482 
2483                value1= str(format_number(self.model.getParam(name1), True))
2484                value2= str(format_number(self.model.getParam(name2)))
2485                value3= str(format_number(self.model.getParam(name3)))
2486            # Reset fittable polydispersin parameter value
2487            for item in self.fittable_param:
2488                 if item[1] == name1:
2489                    item[2].SetValue(value1) 
2490                    item[5].SetValue("")
2491                    item[6].SetValue("")
2492                    # Disable for array
2493                    if disp_name.lower() == "array":
2494                        item[0].SetValue(False)
2495                        item[0].Disable()
2496                        item[2].Disable()
2497                        item[3].Show(False)
2498                        item[4].Show(False)
2499                        item[5].Disable()
2500                        item[6].Disable()
2501                    else:
2502                        item[0].Enable()
2503                        item[2].Enable()
2504                        item[5].Enable()
2505                        item[6].Enable()                       
2506                    break
2507            # Reset fixed polydispersion params
2508            for item in self.fixed_param:
2509                if item[1] == name2:
2510                    item[2].SetValue(value2) 
2511                    # Disable Npts for array
2512                    if disp_name.lower() == "array":
2513                        item[2].Disable()
2514                    else:
2515                        item[2].Enable()
2516                if item[1] == name3:
2517                    item[2].SetValue(value3) 
2518                    # Disable Nsigs for array
2519                    if disp_name.lower() == "array":
2520                        item[2].Disable()
2521                    else:
2522                        item[2].Enable()
2523               
2524            # Make sure the check box updated when all checked
2525            if self.cb1.GetValue():
2526                self.select_all_param(None)
2527
2528            # update params
2529            self._update_paramv_on_fit() 
2530            # draw
2531            self._draw_model()
2532            self.Refresh()
2533        except:
2534            # Error msg
2535            msg = "Error occurred:"
2536            msg += " Could not select the distribution function..."
2537            msg += " Please select another distribution function."
2538            disp_box.SetBackgroundColour("pink")
2539            # Focus on Fit button so that users can see the pinky box
2540            self.btFit.SetFocus()
2541            wx.PostEvent(self.parent.parent, 
2542                         StatusEvent(status=msg, info="error"))
2543       
2544       
2545    def _set_array_disp(self, name=None, disp=None):
2546        """
2547        Set array dispersion
2548       
2549        :param name: name of the parameter for the dispersion to be set
2550        :param disp: the polydisperion object
2551        """
2552        # The user wants this parameter to be averaged.
2553        # Pop up the file selection dialog.
2554        path = self._selectDlg()
2555        # Array data
2556        values = []
2557        weights = []
2558        # If nothing was selected, just return
2559        if path is None:
2560            self.disp_cb_dict[name].SetValue(False)
2561            #self.noDisper_rbox.SetValue(True)
2562            return
2563        self._default_save_location = os.path.dirname(path)
2564        if self.parent != None:
2565            self.parent.parent._default_save_location =\
2566                             self._default_save_location
2567
2568        basename  = os.path.basename(path)
2569        values,weights = self.read_file(path)
2570       
2571        # If any of the two arrays is empty, notify the user that we won't
2572        # proceed
2573        if len(self.param_toFit)>0:
2574            if name in self.param_toFit:
2575                self.param_toFit.remove(name)
2576
2577        # Tell the user that we are about to apply the distribution
2578        msg = "Applying loaded %s distribution: %s" % (name, path)
2579        wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) 
2580
2581        disp.set_weights(values, weights)
2582        self._disp_obj_dict[name] = disp
2583        self.model.set_dispersion(name.split('.')[0], disp)
2584        self.state._disp_obj_dict[name]= disp
2585        self.values[name] = values
2586        self.weights[name] = weights
2587        # Store the object to make it persist outside the
2588        # scope of this method
2589        #TODO: refactor model to clean this up?
2590        self.state.values = {}
2591        self.state.weights = {}
2592        self.state.values = copy.deepcopy(self.values)
2593        self.state.weights = copy.deepcopy(self.weights)
2594
2595        # Set the new model as the dispersion object for the
2596        #selected parameter
2597        #self.model.set_dispersion(p, disp_model)
2598        # Store a reference to the weights in the model object
2599        #so that
2600        # it's not lost when we use the model within another thread.
2601        #TODO: total hack - fix this
2602        self.state.model= self.model.clone()
2603        self.model._persistency_dict[name.split('.')[0]] = \
2604                                        [values, weights]
2605        self.state.model._persistency_dict[name.split('.')[0]] = \
2606                                        [values,weights]
2607        return basename
2608   
2609    def _del_array_values(self, name=None): 
2610        """
2611        Reset array dispersion
2612       
2613        :param name: name of the parameter for the dispersion to be set
2614        """
2615        # Try to delete values and weight of the names array dic if exists
2616        try:
2617            del self.values[name]
2618            del self.weights[name]
2619            # delete all other dic
2620            del self.state.values[name]
2621            del self.state.weights[name]
2622            del self.model._persistency_dict[name.split('.')[0]] 
2623            del self.state.model._persistency_dict[name.split('.')[0]]
2624        except:
2625            pass
2626                                           
2627    def _lay_out(self):
2628        """
2629        returns self.Layout
2630       
2631        :Note: Mac seems to like this better when self.
2632            Layout is called after fitting.
2633        """
2634        self._sleep4sec()
2635        self.Layout()
2636        return 
2637   
2638    def _sleep4sec(self):
2639        """
2640            sleep for 1 sec only applied on Mac
2641            Note: This 1sec helps for Mac not to crash on self.:ayout after self._draw_model
2642        """
2643        if ON_MAC == True:
2644            time.sleep(1)
2645           
2646    def _find_polyfunc_selection(self, disp_func = None):
2647        """
2648        FInd Comboox selection from disp_func
2649       
2650        :param disp_function: dispersion distr. function
2651        """
2652        # List of the poly_model name in the combobox
2653        list = ["RectangleDispersion", "ArrayDispersion", 
2654                    "LogNormalDispersion", "GaussianDispersion", 
2655                    "SchulzDispersion"]
2656
2657        # Find the selection
2658        try:
2659            selection = list.index(disp_func.__class__.__name__)
2660            return selection
2661        except:
2662             return 3
2663                           
2664    def on_reset_clicked(self,event):
2665        """
2666        On 'Reset' button  for Q range clicked
2667        """
2668        flag = True
2669        #if self.check_invalid_panel():
2670        #    return
2671        ##For 3 different cases: Data2D, Data1D, and theory
2672        if self.model == None:
2673            msg="Please select a model first..."
2674            wx.MessageBox(msg, 'Info')
2675            flag = False
2676            return
2677           
2678        elif self.data.__class__.__name__ == "Data2D":
2679            data_min= 0
2680            x= max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) 
2681            y= max(math.fabs(self.data.ymin), math.fabs(self.data.ymax))
2682            self.qmin_x = data_min
2683            self.qmax_x = math.sqrt(x*x + y*y)
2684            # check smearing
2685            if not self.disable_smearer.GetValue():
2686                temp_smearer= self.current_smearer
2687                ## set smearing value whether or not the data contain the smearing info
2688                if self.pinhole_smearer.GetValue():
2689                    flag = self.update_pinhole_smear()
2690                else:
2691                    flag = True
2692                   
2693        elif self.data == None:
2694            self.qmin_x = _QMIN_DEFAULT
2695            self.qmax_x = _QMAX_DEFAULT
2696            self.num_points = _NPTS_DEFAULT           
2697            self.state.npts = self.num_points
2698           
2699        elif self.data.__class__.__name__ != "Data2D":
2700            self.qmin_x = min(self.data.x)
2701            self.qmax_x = max(self.data.x)
2702            # check smearing
2703            if not self.disable_smearer.GetValue():
2704                temp_smearer= self.current_smearer
2705                ## set smearing value whether or not the data contain the smearing info
2706                if self.slit_smearer.GetValue():
2707                    flag = self.update_slit_smear()
2708                elif self.pinhole_smearer.GetValue():
2709                    flag = self.update_pinhole_smear()
2710                else:
2711                    flag = True
2712        else:
2713            flag = False
2714           
2715        if flag == False:
2716            msg= "Cannot Plot :Must enter a number!!!  "
2717            wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))
2718        else:
2719            # set relative text ctrs.
2720            self.qmin.SetValue(str(self.qmin_x))
2721            self.qmax.SetValue(str(self.qmax_x))
2722            self.set_npts2fit()
2723            # At this point, some button and variables satatus (disabled?) should be checked
2724            # such as color that should be reset to white in case that it was pink.
2725            self._onparamEnter_helper()
2726
2727        self.save_current_state()
2728        self.state.qmin = self.qmin_x
2729        self.state.qmax = self.qmax_x
2730       
2731        #reset the q range values
2732        self._reset_plotting_range(self.state)
2733        #self.compute_chisqr(smearer=self.current_smearer)
2734        #Re draw plot
2735        self._draw_model()
2736       
2737    def get_images(self):
2738        """
2739        Get the images of the plots corresponding this panel for report
2740       
2741        : return graphs: list of figures
2742        : TODO: Move to guiframe
2743        """
2744        # set list of graphs
2745        graphs = []
2746        canvases = []
2747        # call gui_manager
2748        gui_manager = self.parent.parent
2749        # loops through the panels [dic]
2750        for item1, item2 in gui_manager.plot_panels.iteritems():
2751             data_title = self.data.group_id
2752             data_name = str(self.data.name).split(" [")[0]
2753            # try to get all plots belonging to this control panel
2754             try:
2755                 title = ''
2756                 # check titles (main plot)
2757                 if hasattr(item2,"data2D"):
2758                     title = item2.data2D.title
2759                 # and data_names (model plot[2D], and residuals)
2760                 if item2.group_id == data_title or \
2761                        item2.group_id.count("res" + str(self.graph_id)) or \
2762                        item2.group_id.count(str(self.uid)) > 0:
2763                     #panel = gui_manager._mgr.GetPane(item2.window_name)
2764                     # append to the list
2765                     graphs.append(item2.figure) 
2766                     canvases.append(item2.canvas)     
2767             except:
2768                 # Not for control panels
2769                 pass
2770        # return the list of graphs
2771        return graphs, canvases
2772
2773    def on_model_help_clicked(self,event):
2774        """
2775        on 'More details' button
2776        """
2777        from help_panel import  HelpWindow
2778        from sans.models import get_data_path
2779       
2780        # Get models help model_function path
2781        path = get_data_path(media='media')
2782        model_path = os.path.join(path,"model_functions.html")
2783        if self.model == None:
2784            name = 'FuncHelp'
2785        else:
2786            name = self.formfactorbox.GetValue()
2787            #name = self.model.__class__.__name__
2788        frame = HelpWindow(None, -1,  pageToOpen=model_path)   
2789        frame.Show(True)
2790        if frame.rhelp.HasAnchor(name):
2791            frame.rhelp.ScrollToAnchor(name)
2792        else:
2793           msg= "Model does not contains an available description "
2794           msg +="Please try searching in the Help window"
2795           wx.PostEvent(self.parent.parent, StatusEvent(status = msg ))     
2796   
2797    def on_pd_help_clicked(self, event):
2798        """
2799        Button event for PD help
2800        """
2801        from help_panel import  HelpWindow
2802        import sans.models as models 
2803       
2804        # Get models help model_function path
2805        path = models.get_data_path(media='media')
2806        pd_path = os.path.join(path,"pd_help.html")
2807
2808        frame = HelpWindow(None, -1,  pageToOpen=pd_path)   
2809        frame.Show(True)
2810       
2811    def on_left_down(self, event):
2812        """
2813        Get key stroke event
2814        """
2815        # Figuring out key combo: Cmd for copy, Alt for paste
2816        if event.CmdDown() and event.ShiftDown():
2817            flag = self.get_paste()
2818        elif event.CmdDown():
2819            flag = self.get_copy()
2820        else:
2821            event.Skip()
2822            return
2823        # make event free
2824        event.Skip()
2825       
2826    def get_copy(self):
2827        """
2828        Get copy params to clipboard
2829        """
2830        content = self.get_copy_params() 
2831        flag = self.set_clipboard(content)
2832        self._copy_info(flag) 
2833        return flag
2834           
2835    def get_copy_params(self): 
2836        """
2837        Get the string copies of the param names and values in the tap
2838        """ 
2839        content = 'sansview_parameter_values:'
2840        # Do it if params exist       
2841        if  self.parameters !=[]:
2842           
2843            # go through the parameters
2844            string = self._get_copy_helper(self.parameters, 
2845                                           self.orientation_params)
2846            content += string
2847           
2848            # go through the fittables
2849            string = self._get_copy_helper(self.fittable_param, 
2850                                           self.orientation_params_disp)
2851            content += string
2852
2853            # go through the fixed params
2854            string = self._get_copy_helper(self.fixed_param, 
2855                                           self.orientation_params_disp)
2856            content += string
2857               
2858            # go through the str params
2859            string = self._get_copy_helper(self.str_parameters, 
2860                                           self.orientation_params)
2861            content += string
2862            return content
2863        else:
2864            return False
2865   
2866    def set_clipboard(self, content=None): 
2867        """
2868        Put the string to the clipboard
2869        """   
2870        if not content:
2871            return False
2872        if wx.TheClipboard.Open():
2873            wx.TheClipboard.SetData(wx.TextDataObject(str(content)))
2874            data = wx.TextDataObject()
2875            success = wx.TheClipboard.GetData(data)
2876            text = data.GetText()
2877            wx.TheClipboard.Close()
2878            return True
2879        return None
2880   
2881    def _get_copy_helper(self, param, orient_param):
2882        """
2883        Helping get value and name of the params
2884       
2885        : param param:  parameters
2886        : param orient_param = oritational params
2887        : return content: strings [list] [name,value:....]
2888        """
2889        content = ''
2890        # go through the str params
2891        for item in param: 
2892            disfunc = ''
2893            try:
2894                if item[7].__class__.__name__ == 'ComboBox':
2895                    disfunc = str(item[7].GetValue())
2896            except:
2897                pass
2898           
2899            # 2D
2900            if self.data.__class__.__name__== "Data2D":
2901                name = item[1]
2902                value = item[2].GetValue()
2903            # 1D
2904            else:
2905                ## for 1D all parameters except orientation
2906                if not item[1] in orient_param:
2907                    name = item[1]
2908                    value = item[2].GetValue()
2909
2910            # add to the content
2911            if disfunc != '':
2912               
2913                disfunc = ',' + disfunc
2914            # TODO: to support array func for copy/paste
2915            try:
2916                if disfunc.count('array') > 0:
2917                    disfunc += ','
2918                    for val in self.values[name]:
2919                        disfunc += ' ' + str(val)
2920                    disfunc += ','
2921                    for weight in self.weights[name]:
2922                        disfunc += ' ' + str(weight)
2923            except:
2924                pass
2925            #if disfunc.count('array') == 0:
2926            content += name + ',' + value + disfunc + ':'
2927
2928        return content
2929   
2930    def get_clipboard(self):   
2931        """
2932        Get strings in the clipboard
2933        """
2934        text = "" 
2935        # Get text from the clip board       
2936        if wx.TheClipboard.Open():
2937           if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
2938               data = wx.TextDataObject()
2939               # get wx dataobject
2940               success = wx.TheClipboard.GetData(data)
2941               # get text
2942               text = data.GetText()
2943           # close clipboard
2944           wx.TheClipboard.Close()
2945           
2946        return text
2947   
2948    def get_paste(self):
2949        """
2950        Paste params from the clipboard
2951        """
2952        text = self.get_clipboard()
2953        flag = self.get_paste_params(text)
2954        self._copy_info(flag)
2955        return flag
2956       
2957    def get_paste_params(self, text=''): 
2958        """
2959        Get the string copies of the param names and values in the tap
2960        """ 
2961        context = {}   
2962        # put the text into dictionary   
2963        lines = text.split(':')
2964        if lines[0] != 'sansview_parameter_values':
2965            self._copy_info(False)
2966            return False
2967        for line in lines[1:-1]:
2968            if len(line) != 0:
2969                item =line.split(',')
2970                name = item[0]
2971                value = item[1]
2972                # Transfer the text to content[dictionary]
2973                context[name] = [value]
2974            # ToDo: PlugIn this poly disp function for pasting
2975            try:
2976                poly_func = item[2]
2977                context[name].append(poly_func)
2978                try:
2979                    # take the vals and weights for  array
2980                    array_values = item[3].split(' ')
2981                    array_weights = item[4].split(' ')
2982                    val = [float(a_val) for a_val in array_values[1:]]
2983                    weit = [float(a_weit) for a_weit in array_weights[1:]]
2984                   
2985                    context[name].append(val)
2986                    context[name].append(weit)
2987                except:
2988                    raise
2989            except:
2990                poly_func = ''
2991                context[name].append(poly_func)
2992
2993        # Do it if params exist       
2994        if  self.parameters != []:
2995            # go through the parameters 
2996            self._get_paste_helper(self.parameters, 
2997                                   self.orientation_params, context)
2998
2999            # go through the fittables
3000            self._get_paste_helper(self.fittable_param, 
3001                                   self.orientation_params_disp, 
3002                                   context)
3003
3004            # go through the fixed params
3005            self._get_paste_helper(self.fixed_param, 
3006                                   self.orientation_params_disp, context)
3007           
3008            # go through the str params
3009            self._get_paste_helper(self.str_parameters, 
3010                                   self.orientation_params, context)
3011            return True
3012        return None
3013   
3014    def _get_paste_helper(self, param, orient_param, content):
3015        """
3016        Helping set values of the params
3017       
3018        : param param:  parameters
3019        : param orient_param: oritational params
3020        : param content: dictionary [ name, value: name1.value1,...]
3021        """
3022        # go through the str params
3023        for item in param: 
3024            # 2D
3025            if self.data.__class__.__name__== "Data2D":
3026                name = item[1]
3027                if name in content.keys():
3028                    pd = content[name][0]
3029                    if name.count('.') > 0:
3030                        try:
3031                            float(pd)
3032                        except:
3033                            #continue
3034                            if not pd and pd != '':
3035                                continue
3036                    item[2].SetValue(str(pd))
3037                    if item in self.fixed_param and pd == '':
3038                        # Only array func has pd == '' case.
3039                        item[2].Enable(False)
3040                    if item[2].__class__.__name__ == "ComboBox":
3041                        if self.model.fun_list.has_key(content[name][0]):
3042                            fun_val = self.model.fun_list[content[name][0]]
3043                            self.model.setParam(name,fun_val)
3044                   
3045                    value = content[name]
3046                    self._paste_poly_help(item, value)
3047        # 1D
3048            else:
3049                ## for 1D all parameters except orientation
3050                if not item[1] in orient_param:
3051                    name = item[1]
3052                    if name in content.keys():
3053                        # Avoid changing combox content which needs special care
3054                        value = content[name]
3055                        pd = value[0]
3056                        if name.count('.') > 0:
3057                            try:
3058                                pd = float(pd)
3059                            except:
3060                                #continue
3061                                if not pd and pd != '':
3062                                    continue
3063                        item[2].SetValue(str(pd))
3064                        if item in self.fixed_param and pd == '':
3065                            # Only array func has pd == '' case.
3066                            item[2].Enable(False)
3067                        if item[2].__class__.__name__ == "ComboBox":
3068                            if self.model.fun_list.has_key(value[0]):
3069                                fun_val = self.model.fun_list[value[0]]
3070                                self.model.setParam(name,fun_val)
3071                                # save state
3072                                #self._copy_parameters_state(self.str_parameters,
3073                                #    self.state.str_parameters)
3074                        self._paste_poly_help(item, value)
3075                       
3076    def _paste_poly_help(self, item, value):
3077        """
3078        Helps get paste for poly function
3079       
3080        :param item: Gui param items
3081        :param value: the values for parameter ctrols
3082        """
3083        if len(value[1]) > 0:
3084            # Only for dispersion func.s
3085            try:
3086                item[7].SetValue(value[1])
3087                selection = item[7].GetCurrentSelection()
3088                name = item[7].Name
3089                param_name = name.split('.')[0]
3090                disp_name = item[7].GetValue()
3091                dispersity= item[7].GetClientData(selection)
3092                disp_model = dispersity()
3093                # Only for array disp
3094                try:
3095                    pd_vals = numpy.array(value[2])
3096                    pd_weights = numpy.array(value[3])
3097                    if len(pd_vals) > 0 and len(pd_vals) > 0:
3098                        if len(pd_vals) == len(pd_weights):
3099                            disp_model.set_weights(pd_vals, 
3100                                                   pd_weights)
3101                            self.values[name] = pd_vals
3102                            self.weights[name] = pd_weights
3103                            # disable the fixed items for array
3104                            item[0].SetValue(False)
3105                            item[0].Enable(False)
3106                            item[2].Enable(False)
3107                            item[3].Show(False)
3108                            item[4].Show(False)
3109                            item[5].SetValue('')
3110                            item[5].Enable(False)
3111                            item[6].SetValue('')
3112                            item[6].Enable(False)
3113                except:
3114                    pass 
3115                self._disp_obj_dict[name] = disp_model
3116                self.model.set_dispersion(name, 
3117                                          disp_model)
3118                self.state._disp_obj_dict[name] = \
3119                                          disp_model
3120                self.model.set_dispersion(param_name, disp_model)
3121                state.values = self.values
3122                state.weights = self.weights   
3123                self.model._persistency_dict[param_name] = \
3124                                        [state.values, state.weights]
3125                   
3126                   
3127            except:
3128                pass 
Note: See TracBrowser for help on using the repository browser.