Hi all,

I just wanted to share an idea I had today. In my plugins, I always had the problem of never knowing the default values I had set for the settings (the info being spread all over the code files), and it was always a pain to change a default value (I had to search in all my files for all the calls for the setting in question).

So, I created a class with a fake inheritance of QSettings:

class MyPluginSettings():
    def __init__(self):
        self.settings = QSettings("MyPlugin","MyPlugin")

        self.defaultValue = {    "mySettingValue1" : 0,
                                "SomeTextSetting" : "hello",
                                "Value4" : 13
                            }

    def value(self,setting):
        if self.defaultValue.has_key(setting) is False:
            raise NameError('MyPlugin has no setting %s' % setting)
        return self.settings.value(setting,self.defaultValue.get(setting))

    def setValue(self,setting,value):
        if self.defaultValue.has_key(setting) is False:
            raise NameError('MyPlugin has no setting %s' % setting)
        self.settings.setValue(setting,value)


You set self.settings = MyPluginSettings() in your plugin, and when you do self.settings.value("mySettingValue1"), you don't need to specify the default value. The class will raise an exception if you call for an undefined setting. This will avoid typos in settings calls and therefore multiple definitions of the same setting.


Don't know if it's useful for you, but I was quite happy with this solution....

Greetings,

Denis
_______________________________________________
Qgis-developer mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to