Easily manage the settings in your QGIS plugin.

This module can:

The main setting class

All your settings shall be saved in a single class, which will subclass SettingManager.

from qgissettingmanager import *

class MySettings(SettingManager):
    def __init__(self):
        SettingManager.__init__(self, my_plugin_name)
        self.add_setting( Bool("my_setting", Scope.Global, True) )

You may add as many settings as you want using add_setting method:

add_setting( SettingClass( name, scope, default_value, options={} ) )

Access the settings

Instantiate your settings class in your current class:

import MySettings
self.settings = MySettings()

The settings are easily accessed using the value and setValue methods:

myVariable = self.settings.value("myVariable")
self.settings.set_value("myVariable", False)

Remove settings

Settings can be removed from registry (local or project wise) using MySettings().remove('my_setting').

Match settings with widgets of a dialog

Quick start

You can associate a setting to a defined widget in a dialog (or a dockable window). The first condition is to name the widget as the setting name. Then, your dialog class shall subclass the SettingDialog class:

class MyDialog(QDialog, Ui_myDialog, SettingDialog):
    def __init__(self):
        self.settings = MySettings()
        SettingDialog.__init__(self, self.settings)

Hence, when the dialog is shown, all widgets which are named according to a setting will be set to the corresponding value. On dialog acceptance, the settings will be set according to the value read from their widget.

To control which setting has been associated to a widget, you can print self.widget_list().

Settings' update behavior

You can have a different behavior using SettingDialog parameters:

SettingDialog(settingManager, mode=UpdateMode.DialogAccept)

mode can take the following values:

Check something before updating the settings

You can override the SettingDialog.before_accept_dialog() method to check your settings. Settings will be saved only if the method returns True.

Using showEvent

Be warned that SettingDialog implements showEvent method. Therefore, if you redefine it in your dialog class, you have to write:

def showEvent(self, e):
    settingDialog.showEvent(self, e)
    # do your own stuff

Possible widgets

The widgets are automatically detected by the manager. If the type of widget is not handled, an error is raised.







New types of widget are easily added, if one is missing, do not hesitate to ask!

Using git submodules

To use this module, you can easily copy the files and put them in your project. A more elegant way is to use git submodule. Hence, you can keep up with latest improvements. In you plugin directory, do

git submodule add https://github.com/3nids/qgissettingmanager.git

A folder qgissettingmanager will be added to your plugin directory. However, git only references the module, and you can git pull in this folder to get the last changes.