Index: guitools/PlotPanel.py
===================================================================
--- guitools/PlotPanel.py	(revision a17ffdfe0f514c60553b596d93faab3540f2898c)
+++ guitools/PlotPanel.py	(revision 1c94a9f14dc2f6ecbfee384ec605d5ba088ee2ef)
@@ -150,4 +150,7 @@
         self.axes = [self.subplot]
         
+        ## Fit dialog
+        self._fit_dialog = None
+        
         # Interactor
         self.connect = BindArtist(self.subplot.figure)
@@ -163,4 +166,8 @@
         self.xminView=0.0
         self.xmaxView=0.0
+        self._scale_xlo = None
+        self._scale_xhi = None
+        self._scale_ylo = None
+        self._scale_yhi = None
         self.Avalue=None
         self.Bvalue=None
@@ -224,4 +231,17 @@
                 self.dragHelper(0,0)
                 
+    def _offset_graph(self):
+        """
+             Zoom and offset the graph to the last known 
+             settings 
+        """
+
+        for ax in self.axes:
+            if self._scale_xhi is not None and self._scale_xlo is not None:
+                ax.set_xlim(self._scale_xlo, self._scale_xhi)
+            if self._scale_yhi is not None and self._scale_ylo is not None:
+                ax.set_ylim(self._scale_ylo, self._scale_yhi)
+            
+            
     def dragHelper(self,xdelta,ydelta):
         """ dragging occurs here"""
@@ -238,6 +258,10 @@
                     newhi= math.log10(hi)-xdelta
             if self.xscale=='log':
+                self._scale_xlo = math.pow(10,newlo)
+                self._scale_xhi = math.pow(10,newhi)
                 ax.set_xlim(math.pow(10,newlo),math.pow(10,newhi))
             else:
+                self._scale_xlo = newlo
+                self._scale_xhi = newhi
                 ax.set_xlim(newlo,newhi)
             #print "new lo %f and new hi %f"%(newlo,newhi)
@@ -253,6 +277,10 @@
                 #print "new lo %f and new hi %f"%(newlo,newhi)
             if  self.yscale=='log':
+                self._scale_ylo = math.pow(10,newlo)
+                self._scale_yhi = math.pow(10,newhi)
                 ax.set_ylim(math.pow(10,newlo),math.pow(10,newhi))
             else:
+                self._scale_ylo = newlo
+                self._scale_yhi = newhi
                 ax.set_ylim(newlo,newhi)
         self.canvas.draw_idle()
@@ -268,4 +296,8 @@
         self.xminView=0.0
         self.xmaxView=0.0
+        self._scale_xlo = None
+        self._scale_xhi = None
+        self._scale_ylo = None
+        self._scale_yhi = None
         self.Avalue=None
         self.Bvalue=None
@@ -287,4 +319,6 @@
             lo,hi = _rescale(lo,hi,step,pt=event.xdata,scale=ax.get_xscale())
             if not self.xscale=='log' or lo>0:
+                self._scale_xlo = lo
+                self._scale_xhi = hi
                 ax.set_xlim((lo,hi))
 
@@ -292,4 +326,6 @@
             lo,hi = _rescale(lo,hi,step,pt=event.ydata,scale=ax.get_yscale())
             if not self.yscale=='log' or lo>0:
+                self._scale_ylo = lo
+                self._scale_yhi = hi
                 ax.set_ylim((lo,hi))
         else:
@@ -309,4 +345,6 @@
                 lo,hi = _rescale(lo,hi,step,bal=xdata,scale=ax.get_xscale())
                 if not self.xscale=='log' or lo>0:
+                    self._scale_xlo = lo
+                    self._scale_xhi = hi
                     ax.set_xlim((lo,hi))
             if ydata is not None:
@@ -314,4 +352,6 @@
                 lo,hi = _rescale(lo,hi,step,bal=ydata,scale=ax.get_yscale())
                 if not self.yscale=='log' or lo>0:
+                    self._scale_ylo = lo
+                    self._scale_yhi = hi
                     ax.set_ylim((lo,hi))
                
@@ -354,13 +394,29 @@
         """
             when clicking on linear Fit on context menu , display Fitting Dialog
+            @param plot: PlotPanel owning the graph
         """
         from fitDialog import LinearFit
         
-        dlg = LinearFit( None, plot, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit')
-       
-        if (self.xmin !=0.0 )and ( self.xmax !=0.0)\
-            and(self.xminView !=0.0 )and ( self.xmaxView !=0.0):
-            dlg.setFitRange(self.xminView,self.xmaxView,self.xmin,self.xmax)
-        dlg.ShowModal() 
+        if self._fit_dialog is not None:
+            return
+        
+        self._fit_dialog = LinearFit( None, plot, self.onFitDisplay,self.returnTrans, -1, 'Linear Fit')
+        
+        # Set the zoom area 
+        if self._scale_xhi is not None and self._scale_xlo is not None:
+            self._fit_dialog.set_fit_region(self._scale_xlo, self._scale_xhi)
+        
+        # Register the close event
+        self._fit_dialog.register_close(self._linear_fit_close)
+        
+        # Show a non-model dialog
+        self._fit_dialog.Show() 
+
+    def _linear_fit_close(self):
+        """
+            A fit dialog was closed
+        """
+        self._fit_dialog = None
+        
 
     def _onProperties(self, event):
@@ -369,4 +425,8 @@
             The user selects a transformation for x or y value and a new plot is displayed
         """
+        if self._fit_dialog is not None:
+            self._fit_dialog.Destroy()
+            self._fit_dialog = None
+            
         list =[]
         list = self.graph.returnPlottable()
@@ -641,11 +701,13 @@
         return self.symbollist[s%len(self.symbollist)]
    
-    def _replot(self):
+    def _replot(self, remove_fit=False):
         """
             Rescale the plottables according to the latest
             user selection and update the plot
+            
+            @param remove_fit: Fit line will be removed if True
         """
         self.graph.reset_scale()
-        self._onEVT_FUNC_PROPERTY(remove_fit=False)
+        self._onEVT_FUNC_PROPERTY(remove_fit=remove_fit)
         
         #TODO: Why do we have to have the following line?
@@ -660,19 +722,28 @@
               and set the scale    
         """ 
+        # The logic should be in the right order
+        # Delete first, and then get the whole list...
+        if remove_fit:
+            self.graph.delete(self.fit_result)
+            
         list =[]
         list = self.graph.returnPlottable()
         
-        if remove_fit:
-            self.fit_result.x =[]  
-            self.fit_result.y =[] 
-            self.fit_result.dx=None
-            self.fit_result.dy=None
-            self.graph.delete(self.fit_result)
+        # Changing the scale might be incompatible with
+        # currently displayed data (for instance, going 
+        # from ln to log when all plotted values have
+        # negative natural logs). 
+        # Go linear and only change the scale at the end.
+        self.set_xscale("linear")
+        self.set_yscale("linear")
+        _xscale = 'linear'
+        _yscale = 'linear'
+        
         
         for item in list:
+            print item.name
             item.setLabel(self.xLabel,self.yLabel)
             if ( self.xLabel=="x" ):
                 item.transformX(transform.toX,transform.errToX)
-                self.set_xscale("linear")
                 name, units = item.get_xaxis()
                 self.graph.xaxis("%s" % name,  "%s" % units)
@@ -681,5 +752,4 @@
             if ( self.xLabel=="x^(2)" ):
                 item.transformX(transform.toX2,transform.errToX2)
-                self.set_xscale('linear')
                 name, units = item.get_xaxis()
                 units=convertUnit(2,units) 
@@ -689,5 +759,5 @@
             if (self.xLabel=="log10(x)" ):
                 item.transformX(transform.toX_pos,transform.errToX_pos)
-                self.set_xscale("log")
+                _xscale = 'log'
                 name, units = item.get_xaxis() 
                 self.graph.xaxis("\log_{10}\ \  (%s)" % name,  "%s" % units)
@@ -696,5 +766,4 @@
             if ( self.yLabel=="ln(y)" ):
                 item.transformY(transform.toLogX,transform.errToLogX)
-                self.set_yscale("linear")
                 name, units = item.get_yaxis()
                 self.graph.yaxis("\log\ \ %s" % name,  "%s" % units)
@@ -703,5 +772,4 @@
             if ( self.yLabel=="y" ):
                 item.transformY(transform.toX,transform.errToX)
-                self.set_yscale("linear")
                 name, units = item.get_yaxis()
                 self.graph.yaxis("%s" % name,  "%s" % units)
@@ -710,5 +778,5 @@
             if ( self.yLabel=="log10(y)" ): 
                 item.transformY(transform.toX_pos,transform.errToX_pos)
-                self.set_yscale("log")  
+                _yscale = 'log'  
                 name, units = item.get_yaxis()
                 self.graph.yaxis("\log_{10}\ \ (%s)" % name,  "%s" % units)
@@ -717,5 +785,4 @@
             if ( self.yLabel=="y^(2)" ):
                 item.transformY( transform.toX2,transform.errToX2 )    
-                self.set_yscale("linear")
                 name, units = item.get_yaxis()
                 units=convertUnit(2,units) 
@@ -725,5 +792,4 @@
             if ( self.yLabel =="1/y"):
                 item.transformY(transform.toOneOverX,transform.errOneOverX )
-                self.set_yscale("linear")
                 name, units = item.get_yaxis()
                 units=convertUnit(-1,units)
@@ -732,5 +798,4 @@
             if ( self.yLabel =="1/sqrt(y)" ):
                 item.transformY(transform.toOneOverSqrtX,transform.errOneOverSqrtX )
-                self.set_yscale("linear")
                 name, units = item.get_yaxis()
                 units=convertUnit(-0.5,units)
@@ -739,5 +804,4 @@
             if ( self.yLabel =="ln(y*x)"):
                 item.transformY( transform.toLogXY,transform.errToLogXY)
-                self.set_yscale("linear")
                 yname, yunits = item.get_yaxis()
                 xname, xunits = item.get_xaxis()
@@ -747,5 +811,4 @@
             if ( self.yLabel =="ln(y*x^(2))"):
                 item.transformY( transform.toLogYX2,transform.errToLogYX2)
-                self.set_yscale("linear")
                 yname, yunits = item.get_yaxis()
                 xname, xunits = item.get_xaxis() 
@@ -756,5 +819,4 @@
             if ( self.yLabel =="ln(y*x^(4))"):
                 item.transformY(transform.toLogYX4,transform.errToLogYX4)
-                self.set_yscale("linear")
                 yname, yunits = item.get_yaxis()
                 xname, xunits = item.get_xaxis()
@@ -765,5 +827,4 @@
                 
                 item.transformX(transform.toX2,transform.errToX2)
-                self.set_xscale('linear')
                 name, units = item.get_xaxis()
                 units = convertUnit(2,units) 
@@ -772,5 +833,4 @@
                 
                 item.transformY(transform.toLogX,transform.errToLogX )
-                self.set_yscale("linear")
                 name, units = item.get_yaxis()
                 self.graph.yaxis("\log\ \ %s" % name,  "%s" % units)
@@ -779,9 +839,12 @@
             item.transformView()
             
-         
         self.resetFitView()   
         self.prevXtrans = self.xLabel 
         self.prevYtrans = self.yLabel  
         self.graph.render(self)
+        
+        self.set_xscale(_xscale)
+        self.set_yscale(_yscale)
+         
         self.subplot.figure.canvas.draw_idle()
         
@@ -824,4 +887,5 @@
         self.graph.add(self.fit_result) 
         self.graph.render(self)
+        self._offset_graph()
         self.subplot.figure.canvas.draw_idle()
         
Index: guitools/fitDialog.py
===================================================================
--- guitools/fitDialog.py	(revision ad8bcd6945b6f5a975704dfa5d1fb0894ccc99b2)
+++ guitools/fitDialog.py	(revision 1c94a9f14dc2f6ecbfee384ec605d5ba088ee2ef)
@@ -27,5 +27,5 @@
 class LinearFit(wx.Dialog):
     def __init__(self, parent, plottable, push_data,transform, id, title):
-        wx.Dialog.__init__(self, parent, id, title, size=(400, 380))
+        wx.Dialog.__init__(self, parent, id, title, size=(400, 350))
 
         """
@@ -35,4 +35,7 @@
         self.parent = parent
         self.transform = transform
+        
+        # Registered owner for close event
+        self._registered_close = None
         
         #dialog panel self call function to plot the fitting function
@@ -65,20 +68,12 @@
         self.xmaxFit  = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))
         self.xmaxFit.SetToolTipString("Enter the maximum value on the x-axis to be included in the fit.")
-        self.xminTransFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))
-        self.xminTransFit.SetToolTipString("Minimum value on the x-axis for the plotted data.")
-        self.xmaxTransFit = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))
-        self.xmaxTransFit.SetToolTipString("Maximum value on the x-axis for the plotted data.")
         self.initXmin = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))
-        self.initXmin.SetToolTipString("Minimum value available on the x-axis for the plotted data.")
+        self.initXmin.SetToolTipString("Minimum value on the x-axis for the plotted data.")
         self.initXmax = wx.TextCtrl(self,-1,size=(_BOX_WIDTH,20))
-        self.initXmax.SetToolTipString("Maximum value available on the x-axis for the plotted data.")
+        self.initXmax.SetToolTipString("Maximum value on the x-axis for the plotted data.")
 
         # Make the info box not editable
         #_BACKGROUND_COLOR = '#ffdf85'
         _BACKGROUND_COLOR = self.GetBackgroundColour()
-        self.xminTransFit.SetEditable(False)
-        self.xminTransFit.SetBackgroundColour(_BACKGROUND_COLOR)
-        self.xmaxTransFit.SetEditable(False)
-        self.xmaxTransFit.SetBackgroundColour(_BACKGROUND_COLOR)
         self.initXmin.SetEditable(False)
         self.initXmin.SetBackgroundColour(_BACKGROUND_COLOR)
@@ -93,4 +88,5 @@
         self.btFit.SetToolTipString("Perform fit.")
         self.btClose =wx.Button(self, wx.ID_CANCEL,'Close')
+        self.btClose.Bind(wx.EVT_BUTTON, self._on_close)
         
         # Intro
@@ -136,5 +132,5 @@
         iy += 1
         ix = 0
-        sizer.Add(wx.StaticText(self, -1, 'Maximum range'),(iy, ix),(1,1),\
+        sizer.Add(wx.StaticText(self, -1, 'Maximum range (linear scale)'),(iy, ix),(1,1),\
                    wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
         ix +=1
@@ -147,14 +143,4 @@
         ix = 0
         sizer.Add(wx.StaticText(self, -1, 'Fit range of '+self.xLabel),(iy, ix),(1,1),\
-                   wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
-        ix += 1
-        sizer.Add(self.xminTransFit, (iy, ix),(1,1),\
-                   wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)
-        ix += 2
-        sizer.Add(self.xmaxTransFit, (iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
-      
-        iy += 1
-        ix = 0
-        sizer.Add(wx.StaticText(self, -1, 'Fit range of x'),(iy, ix),(1,1),\
                    wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
         ix += 1
@@ -216,15 +202,13 @@
             self.tcChi.SetLabel(format_number(self.Chivalue))
         if self.plottable.x !=[]:
-            self.mini =min(self.plottable.x)
-            self.maxi =max(self.plottable.x)
             #store the values of View in self.x,self.y,self.dx,self.dy
             self.x,self.y,self.dx,self.dy= self.plottable.returnValuesOfView()
             
-            
-            self.xminTransFit.SetLabel(format_number(min(self.x)))
-            self.xmaxTransFit.SetLabel(format_number(max(self.x)))
-            
-            self.initXmin.SetValue(format_number(self.mini))
-            self.initXmax.SetValue(format_number(self.maxi))
+            self.mini =min(self.x)
+            self.maxi =max(self.x)
+            
+            
+            self.initXmin.SetValue(format_number(min(self.plottable.x)))
+            self.initXmax.SetValue(format_number(max(self.plottable.x)))
             
             self.xminFit.SetLabel(format_number(self.mini))
@@ -232,4 +216,22 @@
         
       
+    def register_close(self, owner):
+        """
+            Method to register the close event to a parent
+            window that needs notification when the dialog
+            is closed
+            @param owner: parent window
+        """
+        self._registered_close = owner
+        
+    def _on_close(self, event):
+        """
+            Close event. 
+            Notify registered owner if available.
+        """
+        event.Skip()
+        if self._registered_close is not None:
+            self._registered_close()
+        
     def _onFit(self ,event):
         """
@@ -252,14 +254,9 @@
             if(self.checkFitValues(self.xminFit) == True):
                 #Check if the field of Fit Dialog contain values and use the x max and min of the user
-                xmin,xmax = self._checkVal(self.xminFit.GetValue(),self.xmaxFit.GetValue())
-                
-                xminView=self.floatTransform(xmin)
-                xmaxView=self.floatTransform(xmax)
-                if (self.xLabel=="log10(x)"):
-                    self.xminTransFit.SetValue(format_number(math.log10(xminView)))
-                    self.xmaxTransFit.SetValue(format_number(math.log10(xmaxView)))
-                else:
-                    self.xminTransFit.SetValue(format_number(xminView))
-                    self.xmaxTransFit.SetValue(format_number(xmaxView))
+                xminView,xmaxView = self._checkVal(self.xminFit.GetValue(),self.xmaxFit.GetValue())
+                xmin = self.floatInvTransform(xminView)
+                xmax = self.floatInvTransform(xmaxView)
+                
+                
                 # Store the transformed values of view x, y,dy in variables  before the fit
                 if  self.yLabel.lower() == "log10(y)":
@@ -402,4 +399,7 @@
              not in x
         """
+        #TODO: refactor this with proper object-oriented design
+        # This code stinks.
+        
         if ( self.xLabel=="x" ):
             return transform.toX(x)
@@ -413,4 +413,21 @@
             else:
                 raise ValueError,"cannot compute log of a negative number"
+            
+    def floatInvTransform(self,x):
+        """
+             transform a float.It is use to determine the x.View min and x.View max for values
+             not in x
+        """
+        #TODO: refactor this. This is just a hack to make the
+        # functionality work without rewritting the whole code
+        # with good design (which really should be done...).
+        
+        if ( self.xLabel=="x^(2)" ): 
+            return math.sqrt(x)
+        
+        elif (self.xLabel=="log10(x)" ):
+            return math.pow(10, x)
+        
+        return x
             
     def checkFitValues(self,item):
@@ -438,7 +455,19 @@
         self.xminFit.SetValue(format_number(xmin))
         self.xmaxFit.SetValue(format_number(xmax))
-        self.xminTransFit.SetValue(format_number(xminTrans))
-        self.xmaxTransFit.SetValue(format_number(xmaxTrans))
-        
+        
+    def set_fit_region(self, xmin, xmax):
+        """
+            Set the fit region
+            @param xmin: minimum x-value to be included in fit
+            @param xmax: maximum x-value to be included in fit 
+        """
+        # Check values
+        try:
+            float(xmin)
+            float(xmax)
+        except:
+            raise ValueError, "LinearFit.set_fit_region: fit range must be floats"
+        self.xminFit.SetValue(format_number(xmin))
+        self.xmaxFit.SetValue(format_number(xmax))
   
 class MyApp(wx.App):
Index: guitools/fittings.py
===================================================================
--- guitools/fittings.py	(revision 57896544f08828746f936a04451449e4a91450e1)
+++ guitools/fittings.py	(revision 1c94a9f14dc2f6ecbfee384ec605d5ba088ee2ef)
@@ -68,5 +68,4 @@
     p = [param() for param in pars]
     out, cov_x, info, mesg, success = optimize.leastsq(f, p, full_output=1, warning=True)
-    print info, mesg, success
     # Calculate chi squared
     if len(pars)>1:
