Index: src/sas/sasgui/perspectives/fitting/fitpanel.py
===================================================================
--- src/sas/sasgui/perspectives/fitting/fitpanel.py	(revision 05228b001b88187176fb9a2d1bfd9098273fb854)
+++ src/sas/sasgui/perspectives/fitting/fitpanel.py	(revision e6de6b8b31744ebe652ab6206d60d0d20979c592)
@@ -10,7 +10,7 @@
 
 from sas.sasgui.guiframe.panel_base import PanelBase
-from sas.sasgui.guiframe.events import PanelOnFocusEvent
-from sas.sasgui.guiframe.events import StatusEvent
+from sas.sasgui.guiframe.events import PanelOnFocusEvent, StatusEvent
 from sas.sasgui.guiframe.dataFitting import check_data_validity
+from sas.sasgui.perspectives.fitting.simfitpage import SimultaneousFitPage
 
 import basepage
@@ -18,4 +18,5 @@
 _BOX_WIDTH = 80
 
+
 class FitPanel(nb, PanelBase):
     """
@@ -26,7 +27,7 @@
 
     """
-    ## Internal name for the AUI manager
+    # Internal name for the AUI manager
     window_name = "Fit panel"
-    ## Title to appear on top of the window
+    # Title to appear on top of the window
     window_caption = "Fit Panel "
     CENTER_PANE = True
@@ -40,28 +41,28 @@
                     wx.CLIP_CHILDREN)
         PanelBase.__init__(self, parent)
-        #self.SetWindowStyleFlag(style=nb.FNB_FANCY_TABS)
+        # self.SetWindowStyleFlag(style=nb.FNB_FANCY_TABS)
         self._manager = manager
         self.parent = parent
         self.event_owner = None
-        #dictionary of miodel {model class name, model class}
+        # dictionary of miodel {model class name, model class}
         self.menu_mng = models.ModelManager()
         self.model_list_box = self.menu_mng.get_model_list()
-        #pageClosedEvent = nb.EVT_FLATNOTEBOOK_PAGE_CLOSING
+        # pageClosedEvent = nb.EVT_FLATNOTEBOOK_PAGE_CLOSING
         self.model_dictionary = self.menu_mng.get_model_dictionary()
         self.pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE
 
         self.Bind(self.pageClosedEvent, self.on_close_page)
-        ## save the title of the last page tab added
+        # save the title of the last page tab added
         self.fit_page_name = {}
-        ## list of existing fit page
+        # list of existing fit page
         self.opened_pages = {}
-        #index of fit page
+        # index of fit page
         self.fit_page_index = 0
-        #index of batch page
+        # index of batch page
         self.batch_page_index = 0
-        #page of simultaneous fit
+        # page of simultaneous fit
         self.sim_page = None
         self.batch_page = None
-        ## get the state of a page
+        # get the state of a page
         self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate)
         self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo)
@@ -80,30 +81,25 @@
         """
         return an xml node containing state of the panel
-         that guiframe can write to file
-        """
-        msg = ""
+        that guiframe can write to file
+        """
+        # Iterate through all pages and check for batch fitting
+        batch_state = None
+        if self.sim_page is not None:
+            batch_state = self.sim_page.set_state()
+
         for uid, page in self.opened_pages.iteritems():
-            if page.batch_on:
-                pos = self.GetPageIndex(page)
-                if pos != -1 and page not in [self.sim_page, self.batch_page]:
-                    msg += "%s .\n" % str(self.GetPageText(pos))
-            else:
-                data = page.get_data()
-                # state must be cloned
-                state = page.get_state().clone()
-                if data is not None and page.model is not None:
-                    new_doc = self._manager.state_reader.write_toXML(data,
-                                                                     state)
-                    if doc != None and hasattr(doc, "firstChild"):
-                        child = new_doc.firstChild.firstChild
-                        doc.firstChild.appendChild(child)
-                    else:
-                        doc = new_doc
-        if msg.strip() != "":
-            temp = "Save Project is not supported for Batch page.\n"
-            temp += "The following pages will not be save:\n"
-            message = temp + msg
-            wx.PostEvent(self._manager.parent, StatusEvent(status=message,
-                                                            info="warning"))
+            data = page.get_data()
+            # state must be cloned
+            state = page.get_state().clone()
+            if data is not None or page.model is not None:
+                new_doc = self._manager.state_reader.write_toXML(data,
+                                                                 state,
+                                                                 batch_state)
+                if doc is not None and hasattr(doc, "firstChild"):
+                    child = new_doc.firstChild.firstChild
+                    doc.firstChild.appendChild(child)
+                else:
+                    doc = new_doc
+
         return doc
 
@@ -179,5 +175,5 @@
     def get_state(self):
         """
-         return the state of the current selected page
+        return the state of the current selected page
         """
         pos = self.GetSelection()
@@ -191,9 +187,9 @@
         """
 
-        #get number of pages
+        # get number of pages
         nop = self.GetPageCount()
-        #use while-loop, for-loop will not do the job well.
+        # use while-loop, for-loop will not do the job well.
         while (nop > 0):
-            #delete the first page until no page exists
+            # delete the first page until no page exists
             page = self.GetPage(0)
             if self._manager.parent.panel_on_focus == page:
@@ -201,10 +197,11 @@
             self._close_helper(selected_page=page)
             self.DeletePage(0)
-            nop = nop - 1
-
-        ## save the title of the last page tab added
+            nop = self.GetPageCount()
+        # Clear list of names
         self.fit_page_name = {}
-        ## list of existing fit page
+        # Clear list of opened pages
         self.opened_pages = {}
+        self.fit_page_index = 0
+        self.batch_page_index = 0
 
     def set_state(self, state):
@@ -215,5 +212,5 @@
         if state is not None:
             for uid, panel in self.opened_pages.iteritems():
-                #Don't return any panel is the exact same page is created
+                # Don't return any panel is the exact same page is created
                 if uid == panel.uid and panel.data == state.data:
                     # the page is still opened
@@ -223,8 +220,8 @@
             if not page_is_opened:
                 if state.data.__class__.__name__ != 'list':
-                    #To support older state file format
+                    # To support older state file format
                     list_data = [state.data]
                 else:
-                    #Todo: need new file format for the list
+                    # Todo: need new file format for the list
                     list_data = state.data
                 panel = self._manager.add_fit_page(data=list_data)
@@ -241,8 +238,9 @@
         Clear and close all panels, used by guimanager
         """
-
-        #close all panels only when svs file opened
+        # close all panels only when svs file opened
         self.close_all()
         self._manager.mypanels = []
+        self.sim_page = None
+        self.batch_page = None
 
     def on_close_page(self, event=None):
@@ -253,5 +251,5 @@
         selected_page = self.GetPage(self.GetSelection())
         if nbr_page == 1:
-            if selected_page.get_data() == None:
+            if selected_page.get_data() is not None:
                 if event is not None:
                     event.Veto()
@@ -271,6 +269,6 @@
 
                 if data is None:
-                    #the fitpanel exists and only the initial fit page is open
-                    #with no selected data
+                    # the fitpanel exists and only the initial fit page is open
+                    # with no selected data
                     return
                 if data.id == deleted_data.id:
@@ -284,5 +282,4 @@
 
         :param manager: instance of plugin fitting
-
         """
         self._manager = manager
@@ -333,5 +330,5 @@
         else:
             self.batch_page = SimultaneousFitPage(self, batch_on=True,
-                                                   page_finder=page_finder)
+                                                  page_finder=page_finder)
             self.batch_page.window_caption = caption
             self.batch_page.window_name = caption
@@ -346,16 +343,6 @@
         add an empty page
         """
-        """
         if self.batch_on:
             from batchfitpage import BatchFitPage
-            panel = BatchFitPage(parent=self)
-            #Increment index of batch page
-            self.batch_page_index += 1
-            index = self.batch_page_index
-        else:
-        """
-        from fitpage import FitPage
-        from batchfitpage import BatchFitPage
-        if self.batch_on:
             panel = BatchFitPage(parent=self)
             self.batch_page_index += 1
@@ -363,5 +350,6 @@
             panel.set_index_model(self.batch_page_index)
         else:
-            #Increment index of fit page
+            # Increment index of fit page
+            from fitpage import FitPage
             panel = FitPage(parent=self)
             self.fit_page_index += 1
@@ -441,9 +429,9 @@
         page = None
         for p in self.opened_pages.values():
-            #check if there is an empty page to fill up
+            # check if there is an empty page to fill up
             if not check_data_validity(p.get_data()) and p.batch_on:
 
-                #make sure data get placed in 1D empty tab if data is 1D
-                #else data get place on 2D tab empty tab
+                # make sure data get placed in 1D empty tab if data is 1D
+                # else data get place on 2D tab empty tab
                 enable2D = p.get_view_mode()
                 if (data.__class__.__name__ == "Data2D" and enable2D)\
@@ -458,13 +446,13 @@
                 data_type = dlg.get_data()
                 dlg.Destroy()
-                if page  is None:
+                if page is None:
                     page = self.add_empty_page()
                 if data_type == 1:
-                    #user has selected only data1D
+                    # user has selected only data1D
                     page.fill_data_combobox(data_1d_list)
                 elif data_type == 2:
                     page.fill_data_combobox(data_2d_list)
             else:
-                #the batch analysis is canceled
+                # the batch analysis is canceled
                 dlg.Destroy()
                 return None
@@ -503,5 +491,5 @@
             try:
                 data = data_list[0]
-            except:
+            except Exception:
                 # for 'fitv' files
                 data_list = [data]
@@ -511,20 +499,20 @@
                 return None
         for page in self.opened_pages.values():
-            #check if the selected data existing in the fitpanel
+            # check if the selected data existing in the fitpanel
             pos = self.GetPageIndex(page)
             if not check_data_validity(page.get_data()) and not page.batch_on:
-                #make sure data get placed in 1D empty tab if data is 1D
-                #else data get place on 2D tab empty tab
+                # make sure data get placed in 1D empty tab if data is 1D
+                # else data get place on 2D tab empty tab
                 enable2D = page.get_view_mode()
                 if (data.__class__.__name__ == "Data2D" and enable2D)\
-                or (data.__class__.__name__ == "Data1D" and not enable2D):
+                   or (data.__class__.__name__ == "Data1D" and not enable2D):
                     page.batch_on = self.batch_on
                     page._set_save_flag(not page.batch_on)
                     page.fill_data_combobox(data_list)
-                    #caption = "FitPage" + str(self.fit_page_index)
+                    # caption = "FitPage" + str(self.fit_page_index)
                     self.SetPageText(pos, page.window_caption)
                     self.SetSelection(pos)
                     return page
-        #create new page and add data
+        # create new page and add data
         page = self.add_empty_page()
         pos = self.GetPageIndex(page)
@@ -574,8 +562,8 @@
         Delete the given page from the notebook
         """
-        #remove hint page
-        #if selected_page == self.hint_page:
+        # remove hint page
+        # if selected_page == self.hint_page:
         #    return
-        ## removing sim_page
+        # removing sim_page
         if selected_page == self.sim_page:
             self._manager.sim_page = None
@@ -584,25 +572,17 @@
             self._manager.batch_page = None
             return
-        """
-        # The below is not working when delete #5 and still have #6.
-        if selected_page.__class__.__name__ == "FitPage":
-            self.fit_page_index -= 1
-        else:
-            self.batch_page_index -= 1
-        """
-        ## closing other pages
+        # closing other pages
         state = selected_page.createMemento()
         page_finder = self._manager.get_page_finder()
-        ## removing fit page
+        # removing fit page
         data = selected_page.get_data()
-        #Don' t remove plot for 2D
+        # Don' t remove plot for 2D
         flag = True
         if data.__class__.__name__ == 'Data2D':
             flag = False
         if selected_page in page_finder:
-            #Delete the name of the page into the list of open page
+            # Delete the name of the page into the list of open page
             for uid, list in self.opened_pages.iteritems():
-                #Don't return any panel is the exact same page is created
-
+                # Don't return any panel is the exact same page is created
                 if flag and selected_page.uid == uid:
                     self._manager.remove_plot(uid, theory=False)
@@ -610,21 +590,21 @@
             del page_finder[selected_page]
 
-        #Delete the name of the page into the list of open page
+        # Delete the name of the page into the list of open page
         for uid, list in self.opened_pages.iteritems():
-            #Don't return any panel is the exact same page is created
+            # Don't return any panel is the exact same page is created
             if selected_page.uid == uid:
                 del self.opened_pages[selected_page.uid]
                 break
-        ##remove the check box link to the model name of this page (selected_page)
+        # remove the check box link to the model name of the selected_page
         try:
             self.sim_page.draw_page()
         except:
-            ## that page is already deleted no need to remove check box on
-            ##non existing page
+            # that page is already deleted no need to remove check box on
+            # non existing page
             pass
         try:
             self.batch_page.draw_page()
         except:
-            ## that page is already deleted no need to remove check box on
-            ##non existing page
+            # that page is already deleted no need to remove check box on
+            # non existing page
             pass
