Index: calculatorview/src/sans/perspectives/calculator/model_editor.py
===================================================================
--- calculatorview/src/sans/perspectives/calculator/model_editor.py	(revision 6f140f2b7ee93e52dce795ce3d1b3010a48f3268)
+++ calculatorview/src/sans/perspectives/calculator/model_editor.py	(revision 6f140f2b7ee93e52dce795ce3d1b3010a48f3268)
@@ -0,0 +1,904 @@
+
+################################################################################
+#This software was developed by the University of Tennessee as part of the
+#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
+#project funded by the US National Science Foundation. 
+#
+#See the license text in license.txt
+#
+#copyright 2009, University of Tennessee
+################################################################################
+
+
+import wx
+import sys
+import os
+from wx.py.editwindow import EditWindow
+
+if sys.platform.count("win32") > 0:
+    FONT_VARIANT = 0
+    PNL_WIDTH = 460
+    PNL_HITE = 210
+else:
+    FONT_VARIANT = 1
+    PNL_WIDTH = 500
+    PNL_HITE = 250
+
+EDITOR_WIDTH = 800
+EDITOR_HEIGTH = 800
+PANEL_WIDTH = 500
+_BOX_WIDTH = 51
+
+    
+def _compileFile(path):
+    """
+    Compile the file in the path
+    """
+    try:
+        import py_compile
+        py_compile.compile(file=path, doraise=True)
+        return ''
+    except:
+        type, value, traceback = sys.exc_info()
+        return value
+    
+def _deleteFile(path):
+    """
+    Delete file in the path
+    """
+    try:
+        os.remove(path)
+    except:
+        raise
+
+ 
+class TextDialog(wx.Dialog):
+    """
+    Dialog for easy custom sum models  
+    """
+    def __init__(self, parent=None, id=None, title='', model_list=[]):
+        """
+        Dialog window popup when selecting 'Easy Custom Sum' on the menu
+        """
+        wx.Dialog.__init__(self, parent=parent, id=id, 
+                           title=title, size=(PNL_WIDTH, PNL_HITE))
+        self.parent = parent
+        #Font
+        self.SetWindowVariant(variant=FONT_VARIANT)
+        # default
+        self.model_list = model_list
+        self.model1_string = "SphereModel"
+        self.model2_string = "CylinderModel"
+        self._build_sizer()
+        self.model1_name = str(self.model1.GetValue())
+        self.model2_name = str(self.model2.GetValue())
+        
+    def _build_sizer(self):
+        """
+        Build gui
+        """
+        _BOX_WIDTH = 195 # combobox width
+        vbox  = wx.BoxSizer(wx.VERTICAL)
+        sizer = wx.GridBagSizer(1, 3)
+        sum_description= wx.StaticBox(self, -1, 'Select', 
+                                       size=(PNL_WIDTH-30, 70))
+        sum_box = wx.StaticBoxSizer(sum_description, wx.VERTICAL)
+        model1_box = wx.BoxSizer(wx.HORIZONTAL)
+        model2_box = wx.BoxSizer(wx.HORIZONTAL)
+        model_vbox = wx.BoxSizer(wx.VERTICAL)
+        self.model1 =  wx.ComboBox(self, -1, style=wx.CB_READONLY)
+        wx.EVT_COMBOBOX(self.model1, -1, self.on_model1)
+        self.model1.SetMinSize((_BOX_WIDTH, -1))
+        self.model1.SetToolTipString("model1")
+        self.model2 =  wx.ComboBox(self, -1, style=wx.CB_READONLY)
+        wx.EVT_COMBOBOX(self.model2, -1, self.on_model2)
+        self.model2.SetMinSize((_BOX_WIDTH, -1))
+        self.model2.SetToolTipString("model2")
+        self._set_model_list()
+        
+         # Buttons on the bottom
+        self.static_line_1 = wx.StaticLine(self, -1)
+        self.okButton = wx.Button(self,wx.ID_OK, 'OK', size=(_BOX_WIDTH/2, 25))
+        self.closeButton = wx.Button(self,wx.ID_CANCEL, 'Cancel', 
+                                     size=(_BOX_WIDTH/2, 25))
+        # Intro
+        explanation  = "  custom model = scale_factor * (model1 + model2)\n"
+        explanation  += "  Note: This will overwrite the previous sum model.\n"
+        model_string = " Model%s (p%s):"
+        vbox.Add(sizer)
+        ix = 0
+        iy = 1
+        sizer.Add(wx.StaticText(self, -1, explanation), (iy, ix),
+                 (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
+        model1_box.Add(wx.StaticText(self,-1, model_string% (1, 1)), -1, 0)
+        model1_box.Add((_BOX_WIDTH-35,10))
+        model1_box.Add(wx.StaticText(self, -1, model_string% (2, 2)), -1, 0)
+        model2_box.Add(self.model1, -1, 0)
+        model2_box.Add((20,10))
+        model2_box.Add(self.model2, -1, 0)
+        model_vbox.Add(model1_box, -1, 0)
+        model_vbox.Add(model2_box, -1, 0)
+        sum_box.Add(model_vbox, -1, 10)
+        iy += 1
+        ix = 0
+        sizer.Add(sum_box, (iy, ix),
+                  (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
+        vbox.Add((10,10))
+        vbox.Add(self.static_line_1, 0, wx.EXPAND, 10)
+        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
+        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
+        sizer_button.Add(self.okButton, 0, 
+                         wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 0)
+        sizer_button.Add(self.closeButton, 0,
+                          wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 15)        
+        vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
+        self.SetSizer(vbox)
+        self.Centre()
+                 
+    def _set_model_list(self):
+        """
+        Set the list of models
+        """
+        # list of model names
+        list = self.model_list
+        if len(list) > 1:
+            list.sort()
+        for idx in range(len(list)):
+            self.model1.Append(list[idx],idx) 
+            self.model2.Append(list[idx],idx)
+        self.model1.SetStringSelection(self.model1_string)
+        self.model2.SetStringSelection(self.model2_string)
+           
+    def on_model1(self, event):
+        """
+        Set model1
+        """
+        event.Skip()
+        self.model1_name = str(self.model1.GetValue())
+        self.model1_string = self.model1_name
+            
+    def on_model2(self, event):
+        """
+        Set model2
+        """
+        event.Skip()
+        self.model2_name = str(self.model2.GetValue())
+        self.model2_string = self.model2_name
+ 
+    def getText(self):
+        """
+        Returns model name string as list
+        """
+        return [self.model1_name, self.model2_name]
+    
+    def write_string(self, fname, name1, name2):
+        """
+        Write and Save file
+        """
+        try:
+            out_f =  open(fname,'w')
+        except :
+            raise
+        lines = SUM_TEMPLATE.split('\n')
+        for line in lines:
+            if line.count("import %s as P1"):
+                out_f.write(line % (name1, name1) + "\n")
+            elif line.count("import %s as P2"):
+                out_f.write(line % (name2, name2) + "\n")
+            else:
+                out_f.write(line + "\n")
+        out_f.close() 
+        
+    def compile_file(self, path):
+        """
+        Compile the file in the path
+        """
+        _compileFile(path)
+        
+    def delete_file(self, path):
+        """
+        Delete file in the path
+        """
+        _deleteFile(path)
+
+
+class EditorPanel(wx.ScrolledWindow):
+    """
+    Custom model function editor
+    """
+    def __init__(self, parent, base, path, title, *args, **kwds):
+        kwds['name'] = title
+        kwds["size"] = (EDITOR_WIDTH, EDITOR_HEIGTH)
+        kwds["style"]= wx.FULL_REPAINT_ON_RESIZE
+        wx.ScrolledWindow.__init__(self, parent, *args, **kwds)
+        #self.SetupScrolling()
+        self.parent = parent
+        self.base = base
+        self.path = path
+        self.font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
+        self.font.SetPointSize(10)
+        self.reader = None
+        self.name = 'untitled'
+        self.overwrite_name = True
+        self.fname = None
+        self.param_strings = ''
+        self.function_strings = ''
+        self._notes = ""
+        self.warning = ""
+        self._description = "New Custom Model"
+        #self._default_save_location = os.getcwd()
+        self._do_layout()
+        #self.bt_apply.Disable()
+
+             
+    def _define_structure(self):
+        """
+        define initial sizer 
+        """
+        #w, h = self.parent.GetSize()
+        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.name_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.name_hsizer = wx.BoxSizer(wx.HORIZONTAL)
+        self.desc_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.param_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.function_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
+        
+    def _layout_name(self):
+        """
+        Do the layout for file/function name related widgets
+        """
+        #title name [string]
+        name_txt = wx.StaticText(self, -1, 'Function Name : ')  
+        overwrite_cb = wx.CheckBox(self, -1, "Overwrite?", (10, 10))
+        overwrite_cb.SetValue(True)
+        overwrite_cb.SetToolTipString("Overwrite it if already exists?")
+        wx.EVT_CHECKBOX(self, overwrite_cb.GetId(), self.on_over_cb)
+        self.name_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH*3/5, -1)) 
+        self.name_tcl.Bind(wx.EVT_TEXT_ENTER, self.on_change_name)
+        self.name_tcl.SetValue('untitled')
+        self.name_tcl.SetFont(self.font)
+        hint_name = "Unique Model Function Name."
+        self.name_tcl.SetToolTipString(hint_name)
+        self.name_hsizer.AddMany([(self.name_tcl, 0, wx.LEFT|wx.TOP, 0),
+                                       (overwrite_cb, 0, wx.LEFT, 20)])
+        self.name_sizer.AddMany([(name_txt, 0, wx.LEFT|wx.TOP, 10),
+                                       (self.name_hsizer, 0, 
+                                        wx.LEFT|wx.TOP|wx.BOTTOM, 10)])
+        
+        
+    def _layout_description(self):
+        """
+        Do the layout for description related widgets
+        """
+        #title name [string]
+        desc_txt = wx.StaticText(self, -1, 'Description (optional) : ')  
+        self.desc_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH*3/5, -1)) 
+        self.desc_tcl.SetValue('')
+        #self.name_tcl.SetFont(self.font)
+        hint_desc = "Write a short description of the model function."
+        self.desc_tcl.SetToolTipString(hint_desc)
+        self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT|wx.TOP, 10),
+                                       (self.desc_tcl, 0, 
+                                        wx.LEFT|wx.TOP|wx.BOTTOM, 10)])     
+    def _layout_param(self):
+        """
+        Do the layout for parameter related widgets
+        """
+        param_txt = wx.StaticText(self, -1, 'Parameters : ') 
+        param_tip = "#Set the parameters and initial values.\n"
+        param_tip += "#Example:\n"
+        param_tip += "A = 1\nB = 1"
+        #param_txt.SetToolTipString(param_tip)
+        id  = wx.NewId() 
+        self.param_tcl = EditWindow(self, id, wx.DefaultPosition, 
+                            wx.DefaultSize, wx.CLIP_CHILDREN|wx.SUNKEN_BORDER)
+        self.param_tcl.SetToolTipString(param_tip)
+        self.param_sizer.AddMany([(param_txt, 0, wx.LEFT, 10),
+                        (self.param_tcl, 1, wx.EXPAND|wx.ALL, 10)])
+
+   
+    def _layout_function(self):
+        """
+        Do the layout for function related widgets
+        """
+        function_txt = wx.StaticText(self, -1, 'Function(x) : ') 
+        hint_function = "#Example:\n"
+        hint_function += "y = A + B * numpy.sin(x * math.pi)\n"
+        hint_function += "return y"
+        id  = wx.NewId() 
+        self.function_tcl = EditWindow(self, id, wx.DefaultPosition, 
+                            wx.DefaultSize, wx.CLIP_CHILDREN|wx.SUNKEN_BORDER)
+        self.function_tcl.SetToolTipString(hint_function)
+        self.function_sizer.Add(function_txt, 0, wx.LEFT, 10)
+        self.function_sizer.Add( self.function_tcl, 1, wx.EXPAND|wx.ALL, 10)
+
+                    
+    def _layout_button(self):  
+        """
+        Do the layout for the button widgets
+        """         
+        self.bt_apply = wx.Button(self, -1, "Apply", size=(_BOX_WIDTH, -1))
+        self.bt_apply.SetToolTipString("Save changes into the imported data.")
+        self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply)
+        
+        self.bt_close = wx.Button(self, -1, 'Close', size=(_BOX_WIDTH, -1))
+        self.bt_close.Bind(wx.EVT_BUTTON, self.on_close)
+        self.bt_close.SetToolTipString("Close this panel.")
+        
+        self.button_sizer.AddMany([(self.bt_apply, 0, 
+                                    wx.LEFT, EDITOR_WIDTH * 0.8),
+                                   (self.bt_close, 0, 
+                                    wx.LEFT|wx.BOTTOM, 15)])
+        
+    def _do_layout(self):
+        """
+        Draw the current panel
+        """
+        self._define_structure()
+        self._layout_name()
+        self._layout_description()
+        self._layout_param()
+        self._layout_function()
+        self._layout_button()
+        self.main_sizer.AddMany([(self.name_sizer, 0,  
+                                        wx.EXPAND|wx.ALL, 5),
+                                 (wx.StaticLine(self), 0, 
+                                       wx.ALL|wx.EXPAND, 5),
+                                 (self.desc_sizer, 0,  
+                                        wx.EXPAND|wx.ALL, 5),
+                                 (wx.StaticLine(self), 0, 
+                                       wx.ALL|wx.EXPAND, 5),
+                                (self.param_sizer, 0,
+                                         wx.EXPAND|wx.ALL, 5),
+                                 (wx.StaticLine(self), 0, 
+                                       wx.ALL|wx.EXPAND, 5),
+                                (self.function_sizer, 1,
+                                         wx.EXPAND|wx.ALL, 5),
+                                 (wx.StaticLine(self), 0, 
+                                       wx.ALL|wx.EXPAND, 5),
+                                (self.button_sizer, 0,
+                                         wx.EXPAND|wx.ALL, 5)])
+        self.SetSizer(self.main_sizer)
+        self.SetAutoLayout(True)
+    
+    def get_notes(self):
+        """
+        return notes
+        """
+        return self._notes
+                 
+    def on_change_name(self, event=None):
+        """
+        Change name
+        """
+        if event is not None:
+            event.Skip()
+        self.name_tcl.SetBackgroundColour('white')
+        self.Refresh()
+    
+    def check_name(self):
+        """
+        Check name if exist already
+        """
+        self.on_change_name(None)
+        plugin_dir = self.path
+        list_fnames = os.listdir(plugin_dir)
+        # function/file name
+        title = self.name_tcl.GetValue().lstrip().rstrip()
+        self.name = title
+        t_fname = title + '.py'
+        if not self.overwrite_name:
+            if t_fname in list_fnames:
+                self.name_tcl.SetBackgroundColour('pink')
+                return False
+        self.fname = os.path.join(plugin_dir, t_fname)
+        self._notes += "Model function name set "
+        self._notes += "to %s. \n" % str(title)
+        return True
+    
+    def on_over_cb(self, event):
+        """
+        Set overwrite name flag on cb event
+        """
+        if event is not None:
+            event.Skip()
+        cb = event.GetEventObject()
+        self.overwrite_name = cb.GetValue()
+        
+    def on_click_apply(self, event):
+        """   
+        Changes are saved in data object imported to edit
+        """
+        #must post event here
+        event.Skip()
+        info = 'Info'
+        # Sort out the errors if occur
+        if self.check_name():
+            description = self.desc_tcl.GetValue()
+            param_str = self.param_tcl.GetText()
+            func_str = self.function_tcl.GetText()
+            # No input for the model function
+            if func_str.lstrip().rstrip():
+                self.write_file(self.fname, description, param_str, func_str)
+                tr_msg = _compileFile(self.fname)
+                msg = tr_msg.__str__()
+                # Compile error
+                if msg:
+                    _deleteFile(self.fname)
+                    msg +=  "\nCompile Failed"
+                else:
+                    msg = ''
+            else:
+                msg = 'Error: Function is not defined.'
+        else:
+            msg = "Name exists already."
+        # Prepare for the messagebox
+        if not msg:
+            if self.base != None:
+                self.base.update_custom_combo()
+            msg = "Successful!!!"
+            info = 'Info'
+        else:
+            info = 'Error'
+            wx.MessageBox(msg, info)  
+        # Send msg to the top window  
+        if self.base != None:
+                from sans.guiframe.events import StatusEvent 
+                wx.PostEvent(self.base.parent, StatusEvent(status = msg, 
+                                                      info=info))
+        self.warning = msg
+
+                
+    def write_file(self, fname, desc_str, param_str, func_str):  
+        """
+        Write content in file
+        
+        :param fname: full file path
+        :param desc_str: content of the description strings
+        :param param_str: content of params; Strings  
+        :param func_str: content of func; Strings
+        """ 
+        try:
+            out_f =  open(fname,'w')
+        except :
+            raise
+        lines = CUSTOM_TEMPLATE.split('\n')
+        has_scipy = func_str.count("scipy.")
+        local_params = ''
+        spaces = '        '#8spaces
+        for line in lines:
+            # The location where to put the strings is 
+            # hard-coded in the template as shown below.
+            if line.count("#self.params here"):
+                for param_line in param_str.split('\n'):
+                    p_line = param_line.lstrip().rstrip()
+                    if p_line:
+                        p0_line = self.set_param_helper(p_line)
+                        local_params += self.set_function_helper(p_line)
+                        out_f.write(p0_line)
+            elif line.count("#local params here"):
+                if local_params:
+                    out_f.write(local_params)
+            elif line.count("self.description = "):
+                desc = str(desc_str.lstrip().rstrip().replace('\"', ''))
+                if not desc:
+                    desc= self.name
+                out_f.write(line% desc + "\n")
+            elif line.count("#function here"):
+                for func_line in func_str.split('\n'):
+                    f_line = func_line.lstrip().rstrip()
+                    if f_line:
+                        out_f.write(spaces + f_line + "\n")
+                if not func_str:
+                    out_f.write(spaces + 'return y' + "\n")
+            elif line.count("#import scipy?"):
+                if has_scipy:
+                    out_f.write("import scipy" + "\n")
+            elif line.count("name = "):
+                out_f.write(line % self.name + "\n")
+            elif line:
+                out_f.write(line + "\n")
+        out_f.close() 
+    
+    def set_param_helper(self, line):   
+        """
+        Get string in line to define the params dictionary
+        
+        :param line: one line of string got from the param_str
+        """
+        flag = True
+        params_str = ''
+        spaces = '        '#8spaces
+        items = line.split(";")
+        for item in items:
+            name = item.split("=")[0].lstrip().rstrip()
+            value = item.split("=")[1].lstrip().rstrip()
+            try:
+                float(value)
+            except:
+               raise
+            params_str += spaces + "self.params['%s'] = %s\n"% (name, value)
+            
+        return params_str
+
+    def set_function_helper(self, line):   
+        """
+        Get string in line to define the local params
+        
+        :param line: one line of string got from the param_str
+        """
+        flag = True
+        params_str = ''
+        spaces = '        '#8spaces
+        items = line.split(";")
+        for item in items:
+            name = item.split("=")[0].lstrip().rstrip()
+            params_str += spaces + "%s = self.params['%s']\n"% (name, name)
+        return params_str
+    
+    def get_warning(self):
+        """
+        Get the warning msg 
+        """
+        return self.warning
+        
+    def on_close(self, event):
+        """
+        leave data as it is and close
+        """
+        self.parent.Close()
+        event.Skip()
+        
+class EditorWindow(wx.Frame):
+    def __init__(self, parent, base, path, title, 
+                 size=(EDITOR_WIDTH, EDITOR_HEIGTH), *args, **kwds):
+        kwds["title"] = title
+        kwds["size"] = size
+        wx.Frame.__init__(self, parent=None, *args, **kwds)
+        self.parent = parent
+        self.panel = EditorPanel(parent=self, base=parent, 
+                                 path=path, title=title)
+        self.Show(True)
+        wx.EVT_CLOSE(self, self.OnClose)
+    
+    def OnClose(self, event):  
+        """
+        On close event
+        """
+        self.parent.new_model_frame = None
+        self.Destroy()  
+
+## Templates for custom models
+CUSTOM_TEMPLATE = """
+from sans.models.pluginmodel import Model1DPlugin
+import math
+import numpy
+#import scipy?
+class Model(Model1DPlugin):
+    name = "%s"                             
+    def __init__(self):
+        Model1DPlugin.__init__(self, name=self.name)                                                     
+        #self.params here
+        self.description = "%s"
+        self.set_details()
+    def function(self, x = 0.0):
+        #local params here
+        #function here  
+######################################################################
+## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!!       
+if __name__ == "__main__": 
+    m= Model() 
+    out1 = m.runXY(0.0)
+    out2 = m.runXY(0.01)
+    isfine1 = numpy.isfinite(out1)
+    isfine2 = numpy.isfinite(out2)
+    print "Testing the value at Q = 0.0:"
+    print out1, " : finite? ", isfine1
+    print "Testing the value at Q = 0.01:"
+    print out2, " : finite? ", isfine2
+    if isfine1 and isfine2:
+        print "===> Simple Test: Passed!"
+    else:
+        print "===> Simple Test: Failed!"
+"""        
+        
+SUM_TEMPLATE = """
+# A sample of an experimental model function for Sum(Pmodel1,Pmodel2)
+import copy
+from sans.models.pluginmodel import Model1DPlugin
+# User can change the name of the model (only with single functional model)
+#P1_model: 
+from sans.models.%s import %s as P1
+
+#P2_model: 
+from sans.models.%s import %s as P2
+
+class Model(Model1DPlugin):
+    name = ""
+    def __init__(self):
+        Model1DPlugin.__init__(self, name='')
+        p_model1 = P1()
+        p_model2 = P2()
+        ## Setting  model name model description
+        self.description=""
+        self.name = self._get_name(p_model1.name, p_model2.name)
+        self.description = p_model1.name
+        self.description += p_model2.name
+        self.fill_description(p_model1, p_model2)
+
+        ## Define parameters
+        self.params = {}
+
+        ## Parameter details [units, min, max]
+        self.details = {}
+        
+        # non-fittable parameters
+        self.non_fittable = p_model1.non_fittable  
+        self.non_fittable += p_model2.non_fittable  
+            
+        ##models 
+        self.p_model1= p_model1
+        self.p_model2= p_model2
+        
+       
+        ## dispersion
+        self._set_dispersion()
+        ## Define parameters
+        self._set_params()
+        ## New parameter:Scaling factor
+        self.params['scale_factor'] = 1
+        
+        ## Parameter details [units, min, max]
+        self._set_details()
+        self.details['scale_factor'] = ['', None, None]
+
+        
+        #list of parameter that can be fitted
+        self._set_fixed_params()  
+        ## parameters with orientation
+        for item in self.p_model1.orientation_params:
+            new_item = "p1_" + item
+            if not new_item in self.orientation_params:
+                self.orientation_params.append(new_item)
+            
+        for item in self.p_model2.orientation_params:
+            new_item = "p2_" + item
+            if not new_item in self.orientation_params:
+                self.orientation_params.append(new_item)
+        # get multiplicity if model provide it, else 1.
+        try:
+            multiplicity1 = p_model1.multiplicity
+            try:
+                multiplicity2 = p_model2.multiplicity
+            except:
+                multiplicity2 = 1
+        except:
+            multiplicity1 = 1
+            multiplicity2 = 1
+        ## functional multiplicity of the model
+        self.multiplicity1 = multiplicity1  
+        self.multiplicity2 = multiplicity2    
+        self.multiplicity_info = []   
+        
+    def _clone(self, obj):
+        obj.params     = copy.deepcopy(self.params)
+        obj.description     = copy.deepcopy(self.description)
+        obj.details    = copy.deepcopy(self.details)
+        obj.dispersion = copy.deepcopy(self.dispersion)
+        obj.p_model1  = self.p_model1.clone()
+        obj.p_model2  = self.p_model2.clone()
+        #obj = copy.deepcopy(self)
+        return obj
+    
+    def _get_name(self, name1, name2):
+        name = self._get_upper_name(name1)
+        name += "+"
+        name += self._get_upper_name(name2)
+        return name
+    
+    def _get_upper_name(self, name=None):
+        if name == None:
+            return ""
+        upper_name = ""
+        str_name = str(name)
+        for index in range(len(str_name)):
+            if str_name[index].isupper():
+                upper_name += str_name[index]
+        return upper_name
+        
+    def _set_dispersion(self):
+        ##set dispersion only from p_model 
+        for name , value in self.p_model1.dispersion.iteritems():
+            #if name.lower() not in self.p_model1.orientation_params:
+            new_name = "p1_" + name
+            self.dispersion[new_name]= value 
+        for name , value in self.p_model2.dispersion.iteritems():
+            #if name.lower() not in self.p_model2.orientation_params:
+            new_name = "p2_" + name
+            self.dispersion[new_name]= value 
+            
+    def function(self, x=0.0): 
+        return 0
+                               
+    def getProfile(self):
+        try:
+            x,y = self.p_model1.getProfile()
+        except:
+            x = None
+            y = None
+            
+        return x, y
+    
+    def _set_params(self):
+        for name , value in self.p_model1.params.iteritems():
+            # No 2D-supported
+            #if name not in self.p_model1.orientation_params:
+            new_name = "p1_" + name
+            self.params[new_name]= value
+            
+        for name , value in self.p_model2.params.iteritems():
+            # No 2D-supported
+            #if name not in self.p_model2.orientation_params:
+            new_name = "p2_" + name
+            self.params[new_name]= value
+                
+        # Set "scale" as initializing
+        self._set_scale_factor()
+      
+            
+    def _set_details(self):
+        for name ,detail in self.p_model1.details.iteritems():
+            new_name = "p1_" + name
+            #if new_name not in self.orientation_params:
+            self.details[new_name]= detail
+            
+        for name ,detail in self.p_model2.details.iteritems():
+            new_name = "p2_" + name
+            #if new_name not in self.orientation_params:
+            self.details[new_name]= detail
+    
+    def _set_scale_factor(self):
+        pass
+        
+                
+    def setParam(self, name, value):
+        # set param to p1+p2 model
+        self._setParamHelper(name, value)
+        
+        ## setParam to p model 
+        model_pre = name.split('_', 1)[0]
+        new_name = name.split('_', 1)[1]
+        if model_pre == "p1":
+            if new_name in self.p_model1.getParamList():
+                self.p_model1.setParam(new_name, value)
+        elif model_pre == "p2":
+             if new_name in self.p_model2.getParamList():
+                self.p_model2.setParam(new_name, value)
+        elif name.lower() == 'scale_factor':
+            self.params['scale_factor'] = value
+        else:
+            raise ValueError, "Model does not contain parameter %s" % name
+            
+    def getParam(self, name):
+        # Look for dispersion parameters
+        toks = name.split('.')
+        if len(toks)==2:
+            for item in self.dispersion.keys():
+                # 2D not supported
+                if item.lower()==toks[0].lower():
+                    for par in self.dispersion[item]:
+                        if par.lower() == toks[1].lower():
+                            return self.dispersion[item][par]
+        else:
+            # Look for standard parameter
+            for item in self.params.keys():
+                if item.lower()==name.lower():
+                    return self.params[item]
+        return  
+        #raise ValueError, "Model does not contain parameter %s" % name
+       
+    def _setParamHelper(self, name, value):
+        # Look for dispersion parameters
+        toks = name.split('.')
+        if len(toks)== 2:
+            for item in self.dispersion.keys():
+                if item.lower()== toks[0].lower():
+                    for par in self.dispersion[item]:
+                        if par.lower() == toks[1].lower():
+                            self.dispersion[item][par] = value
+                            return
+        else:
+            # Look for standard parameter
+            for item in self.params.keys():
+                if item.lower()== name.lower():
+                    self.params[item] = value
+                    return
+            
+        raise ValueError, "Model does not contain parameter %s" % name
+             
+   
+    def _set_fixed_params(self):
+        for item in self.p_model1.fixed:
+            new_item = "p1" + item
+            self.fixed.append(new_item)
+        for item in self.p_model2.fixed:
+            new_item = "p2" + item
+            self.fixed.append(new_item)
+
+        self.fixed.sort()
+                
+                    
+    def run(self, x = 0.0):
+        self._set_scale_factor()
+        return self.params['scale_factor'] * \
+(self.p_model1.run(x) + self.p_model2.run(x))
+    
+    def runXY(self, x = 0.0):
+        self._set_scale_factor()
+        return self.params['scale_factor'] * \
+(self.p_model1.runXY(x) + self.p_model2.runXY(x))
+    
+    ## Now (May27,10) directly uses the model eval function 
+    ## instead of the for-loop in Base Component.
+    def evalDistribution(self, x = []):
+        self._set_scale_factor()
+        return self.params['scale_factor'] * \
+(self.p_model1.evalDistribution(x) + \
+self.p_model2.evalDistribution(x))
+
+    def set_dispersion(self, parameter, dispersion):
+        value= None
+        new_pre = parameter.split("_", 1)[0]
+        new_parameter = parameter.split("_", 1)[1]
+        try:
+            if new_pre == 'p1' and \
+new_parameter in self.p_model1.dispersion.keys():
+                value= self.p_model1.set_dispersion(new_parameter, dispersion)
+            if new_pre == 'p2' and \
+new_parameter in self.p_model2.dispersion.keys():
+                value= self.p_model2.set_dispersion(new_parameter, dispersion)
+            self._set_dispersion()
+            return value
+        except:
+            raise 
+
+    def fill_description(self, p_model1, p_model2):
+        description = ""
+        description +="This model gives the summation of  %s and %s. "% \
+( p_model1.name, p_model2.name )
+        self.description += description
+        
+if __name__ == "__main__": 
+    m1= Model() 
+    #m1.setParam("p1_scale", 25)  
+    #m1.setParam("p1_length", 1000)
+    #m1.setParam("p2_scale", 100) 
+    #m1.setParam("p2_rg", 100) 
+    out1 = m1.runXY(0.01)
+
+    m2= Model()
+    #m2.p_model1.setParam("scale", 25) 
+    #m2.p_model1.setParam("length", 1000) 
+    #m2.p_model2.setParam("scale", 100)
+    #m2.p_model2.setParam("rg", 100)
+    out2 = m2.p_model1.runXY(0.01) + m2.p_model2.runXY(0.01)
+    print out1, " = ", out2
+    if out1 == out2:
+        print "===> Simple Test: Passed!"
+    else:
+        print "===> Simple Test: Failed!"
+"""
+      
+#if __name__ == "__main__": 
+#    app = wx.PySimpleApp()
+#    frame = TextDialog(id=1, model_list=["SphereModel", "CylinderModel"])   
+#    frame.Show(True)
+#    app.MainLoop()             
+
+if __name__ == "__main__":
+    from sans.perspectives.fitting import models
+    dir_path = models.find_plugins_dir()
+    app  = wx.App()
+    window = EditorWindow(parent=None, base=None, path=dir_path, title="AAA")
+    app.MainLoop()         
