I've attached a couple more patches. One is quite a simple thing make the the font selector into a dropdown list (and hopefully to stop the application from falling over if none of the predefined list of truetype fonts is installed - but that needs a more general solution). The second patch is a little more interesting - it's a quick hack to provide a feature I wanted - the ability to define variables and functions in the console window and use these in function plots. It does this by adding a .locals dictionary attribute to the document which is then used as the local namespace for the evaluation of functions in plots and fits. The dictionary is pickled into the save file and unpickled on load. I'm not sure this is the right approach to the problem, but it is a feature I would use.

Reviews welcome.


--
"But if science you say still sounds too deep,
Just do what Beaker does, just shrug and 'Meep!'"

-- Dr. Bunsen Honeydew & Beaker of Muppet Labs

Index: document/commandinterpreter.py
===================================================================
RCS file: /cvs/veusz/veusz/document/commandinterpreter.py,v
retrieving revision 1.16
diff -u -r1.16 commandinterpreter.py
--- document/commandinterpreter.py	27 Feb 2005 15:15:48 -0000	1.16
+++ document/commandinterpreter.py	22 Apr 2005 08:39:05 -0000
@@ -82,6 +82,7 @@
             'ImportFile': i.ImportFile,
             'List': i.List,
             'Load': self.Load,
+            'LoadLocals': self.LoadLocals,
             'Print': i.Print,
             'Remove': i.Remove,
             'Save': i.Save,
@@ -162,7 +163,7 @@
         else:
             # execute the code
             try:
-                exec c in self.globals
+                exec c in self.globals, self.document.locals
             except Exception, e:
                 # print out the backtrace to stderr
                 i = sys.exc_info()
@@ -185,6 +186,9 @@
         self.document.setModified()
         self.document.setModified(False)
 
+    def LoadLocals(self, pickleString):
+        self.document.locals = pickle.loads(pickleString)
+        
     def runFile(self, fileobject):
         """ Run a file in the preserved environment."""
 
Index: document/doc.py
===================================================================
RCS file: /cvs/veusz/veusz/document/doc.py,v
retrieving revision 1.24
diff -u -r1.24 doc.py
--- document/doc.py	11 Apr 2005 19:39:38 -0000	1.24
+++ document/doc.py	22 Apr 2005 08:39:05 -0000
@@ -28,6 +28,7 @@
 import random
 import string
 import itertools
+import pickle
 
 import qt
 
@@ -196,6 +197,7 @@
         """Wipe out any stored data."""
 
         self.data = {}
+        self.locals={}
         self.basewidget = widgets.Root(None)
         self.basewidget.document = self
         self.setModified()
@@ -276,6 +278,11 @@
             pass
         file.write('# Date: %s\n\n' % time.strftime(
             "%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) )
+
+        #save local variables
+        file.write("LoadLocals(\"\"\"")
+        pickle.Pickler(file).dump(self.locals)
+        file.write("\"\"\")\n")
 
         # save those datasets which are linked
         # we do this first in case the datasets are overridden below

Index: widgets/fit.py
===================================================================
RCS file: /cvs/veusz/veusz/widgets/fit.py,v
retrieving revision 1.7
diff -u -r1.7 fit.py
--- widgets/fit.py	25 Mar 2005 18:11:38 -0000	1.7
+++ widgets/fit.py	22 Apr 2005 08:39:07 -0000
@@ -87,10 +87,14 @@
     def initEnviron(self):
         """Copy data into environment."""
         env = self.fnenviron.copy()
-        for name, val in self.settings.values.iteritems():
-            env[name] = val
         return env
 
+    def initLocalEnviron(self):
+        localEnv = self.document.locals.copy()
+        for name, val in self.settings.values.iteritems():
+            localEnv[name] = val
+        return localEnv
+
     def actionFit(self):
         """Fit the data."""
 
@@ -131,18 +135,19 @@
     def evalfunc(self, params, xvals):
 
         # make an environment
-        env = self.initEnviron()
+        globalEnv = self.initEnviron()
+        localEnv = self.initLocalEnviron()
         s = self.settings
-        env[s.variable] = xvals
+        localEnv[s.variable] = xvals
 
         # set values for real function
         names = s.values.keys()
         names.sort()
         for name, val in zip(names, params):
-            env[name] = val
+            localEnv[name] = val
 
         try:
-            return eval(s.function, env)
+            return eval(s.function, globalEnv, localEnv)
         except:
             return NIE.nan
 
Index: widgets/plotters.py
===================================================================
RCS file: /cvs/veusz/veusz/widgets/plotters.py,v
retrieving revision 1.36
diff -u -r1.36 plotters.py
--- widgets/plotters.py	9 Apr 2005 22:20:02 -0000	1.36
+++ widgets/plotters.py	22 Apr 2005 08:39:08 -0000
@@ -236,7 +236,7 @@
             pxpts = N.arange(x1, x2+delta, delta).astype(N.Int32)
             env['x'] = axes[0].plotterToGraphCoords(posn, pxpts)
             try:
-                y = eval( s.function + ' + 0*x', env )
+                y = eval( s.function + ' + 0*x', env , self.document.locals)
                 bad = False
             except:
                 bad = True
Index: setting/collections.py
===================================================================
RCS file: /cvs/veusz/veusz/setting/collections.py,v
retrieving revision 1.10
diff -u -r1.10 collections.py
--- setting/collections.py	9 Apr 2005 17:05:13 -0000	1.10
+++ setting/collections.py	22 Apr 2005 08:39:06 -0000
@@ -178,6 +178,7 @@
 
     # need to examine font table to see what's available
     defaultfamily=''
+    families = []
 
     def __init__(self, name, descr = ''):
         settings.Settings.__init__(self, name, descr=descr)
@@ -185,8 +186,9 @@
         if Text.defaultfamily == '':
             Text._getDefaultFamily()
 
-        self.add( setting.Str('font', Text.defaultfamily,
-                  descr = 'Font name' ) )
+        self.add( setting.ChoiceOrMore('font', Text.families,
+                                       Text.defaultfamily,
+                                       descr = 'Font name' ) )
         self.add( setting.Distance('size', '14pt',
                   descr = 'Font size' ) )
         self.add( setting.ChoiceOrMore( 'color', colors, 'black',
@@ -199,24 +201,23 @@
                                 descr = 'Underline font' ) )
         self.add( setting.Bool( 'hide', False,
                                 descr = 'Hide the text') )
+    def _getFontFamilies():
+        Text.families=[str(name) for name in qt.QFontDatabase().families()]
 
+    _getFontFamilies = staticmethod(_getFontFamilies)
+    
     def _getDefaultFamily():
         '''Choose a default font family. We check through a list until we
         get a sensible default.'''
 
-        db = qt.QFontDatabase()
-
-        # build a dict up with the list of families
-        families = {}
-        for i in db.families():
-            families[str(i)] = True
-
+        if not Text.families:
+            Text._getFontFamilies()
         for i in ['Times New Roman', 'Bitstream Vera Serif', 'Times', 'Utopia',
                   'Serif']:
-            if i in families:
+            if i in Text.families:
                 Text.defaultfamily = i
                 qf = qt.QFont(i)
                 return
-
+        Text.defaultfamily = Text.families[0]
         raise RuntimeError('Could not identify sensible default font for Veusz'
                            '. Please report this bug if you have any fonts '
                            'installed')

Répondre à