On Sat, 11 Jun 2005, J. Graham wrote:

If someone fixes that we're good to go. Though if that fix involves
writing stuff to the registry you can compile your own qt :p
Probably the best way to do it is to detect what OS it's using and on
windows put text files in the veusz directory - one for global
settings and one per user.

I'm really strongly opposed to that. Windows programs that force you to run as Admin are broken-by-design. Is there a reason you're opposed to using the windows standard location for configuration information as the location for configuration information?

The other advantage of using QSettings (if it works) is that it will work on the Mac too. Given the choice between widely used cross platform code from qt and trying to roll our own solution, I'd much rather use the prewritten code.

Right, there's a patch attached that should make settings use QSettings.
It might not be ideal (the design principle was "touch as few files as
possible" rather than "create the best interface possible"). There is
probably more goodness to squeeze out of QSettings (the existing interface does have the nice property of being quite pythonic though). Safer serialization
might also be nice... (though I guess not actually that important).
Index: setting/settingdb.py
===================================================================
RCS file: /cvs/veusz/veusz/setting/settingdb.py,v
retrieving revision 1.3
diff -u -r1.3 settingdb.py
--- setting/settingdb.py	18 May 2005 16:30:58 -0000	1.3
+++ setting/settingdb.py	11 Jun 2005 23:38:59 -0000
@@ -23,6 +23,52 @@
 import sys
 import os
 import os.path
+import qt
+
+class _settings(object):
+    def __init__(self, settings):
+        self.settings = settings
+        self.basePath = "/" + settings.domain + "/" + settings.name + "/"
+        self.path = self.basePath
+    def setPath(self, path):
+        self.path = self.basePath + path
+    def resetPath(self):
+        self.path = self.basePath
+    def __getitem__(self, key):
+        #I don't like using eval here... :(
+        try:
+            value, ok = self.settings.readEntry(self.basePath + key)
+            value = eval(unicode(value))
+            return value
+        except:
+            print "Warning,  error importing setting " + key
+            print unicode(self.settings.readEntry(self.path + key)[0])
+    def __setitem__(self, key, value):
+        rv = self.settings.writeEntry(self.path+ key, repr(value))
+        self.settings.sync()
+        return rv
+    def __contains__(self, key):
+        return key in self.settings.entryList(self.basePath)
+    def __del__(self, key):
+        self.settings.removeEntry(self.basePath + key)
+    
+class _QSettingDB(qt.QSettings):
+    def __init__(self):
+        #XXX - This domain name is fictional...
+        self.domain = "veusz.org"
+        self.name = "veusz"
+        
+        qt.QSettings.__init__(self)
+        self.setPath(self.domain, self.name)
+        self.database = _settings(self)
+
+        #Hack to import old settings
+        oldSettings = _SettingDB()
+        if not 'importPerformed' in self.database:
+            for key, value in oldSettings.database.iteritems():
+                self.database[key] = str(value)
+            self.database['importPerformed'] = True
+            print "Imported settings from $HOME/.veusz.def and /etc/veusz.conf"
 
 class _SettingDB:
     """A singleton class to handle the settings file.
@@ -117,4 +163,4 @@
                 f.write( '%s=%s\n' % (key, repr(self.database[key])) )
 
 # create the SettingDB singleton
-settingdb = _SettingDB()
+settingdb = _QSettingDB()

Répondre à