From b98699c70490b90b133bf8bf9524d18b50950202 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sat, 29 Jun 2019 20:29:15 +0200 Subject: [PATCH] Config: Add load ans save method, fix typo Signed-off-by: Juraj Oravec --- config/config.py | 61 +++++++++++++++++++++++++++++++++-- config/options.kcfg | 21 ++++++++++++ config/pyside2-config.desktop | 2 +- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 config/options.kcfg diff --git a/config/config.py b/config/config.py index 38d05fb..a5a0b34 100644 --- a/config/config.py +++ b/config/config.py @@ -6,7 +6,10 @@ {% endblock license_header %} -from PySide2.QtCore import QObject, Signal +{% if include_load_save %} +import os +{% endif %} +from PySide2.QtCore import QObject, Signal{% if include_load_save %}, QSettings{% endif %} class {{ name }}{% if base_classes %}({% for inh in base_classes %}{{ inh.baseType }}{% if not forloop.last %}, {% endif %}{% endfor %}){% endif %}: @@ -15,9 +18,15 @@ class {{ name }}{% if base_classes %}({% for inh in base_classes %}{{ inh.baseTy {% endfor %} + {% if include_load_save %} + configLoaded = Signal() + configSaved = Signal() + {% endif %} + + def __init__(self{% for member in members %}, {{ member.name }}=None{% endfor %}): {% for inh in base_classes %} - {{ inh.baseType }}.__init__(self) + {{ inh.baseType }}.__init__() {% endfor %} @@ -43,12 +52,60 @@ class {{ name }}{% if base_classes %}({% for inh in base_classes %}{{ inh.baseTy {% endfor %} + {% if include_load_save %} + + def load(self): + if not self.{% if settings_filepath_variable %}{{settings_filepath_variable}}{% else %}settingsFile{% endif %}: + return + if os.path.exists(self.{% if settings_filepath_variable %}{{settings_filepath_variable}}{% else %}settingsFile{% endif %}): + pass + + + settings = QSettings(self.{% if settings_filepath_variable %}{{settings_filepath_variable}}{% else %}settingsFile{% endif %}, QSettings.IniFormat) + + + settings.beginGroup("{% if settings_group_name %}{{ settings_group_name }}{% else %}{{ name }}{% endif %}") + {% for member in members %} + self.{{ member.name }} = {{ member.type }}(settings.value("{{ member.name }}", self.{{ member.name }})) + {% endfor %} + settings.endGroup() + + + self.configLoaded.emit() + + + def save(self): + if not self.{% if settings_filepath_variable %}{{settings_filepath_variable}}{% else %}settingsFile{% endif %}: + return + if os.path.exists(self.{% if settings_filepath_variable %}{{settings_filepath_variable}}{% else %}settingsFile{% endif %}): + pass + + + settings = QSettings(self.{% if settings_filepath_variable %}{{settings_filepath_variable}}{% else %}settingsFile{% endif %}, QSettings.IniFormat) + + + settings.beginGroup("{% if settings_group_name %}{{ settings_group_name }}{% else %}{{ name }}{% endif %}") + {% for member in members %} + settings.setValue("{{ member.name }}", self.{{ member.name }}) + {% endfor %} + settings.endGroup() + + + settings.sync() + + + self.configSaved.emit() + + {% endif %} + + {% for member in members %} @property def {{ member.name }}(self): return self._{{ member.name }} + @{{ member.name }}.setter def {{ member.name }}(self, {{ member.name }}): if not {{ member.name }}: diff --git a/config/options.kcfg b/config/options.kcfg new file mode 100644 index 0000000..3661719 --- /dev/null +++ b/config/options.kcfg @@ -0,0 +1,21 @@ + + + + + + + True + + + + settingsFile + + + + {{ name }} + + + diff --git a/config/pyside2-config.desktop b/config/pyside2-config.desktop index 829d6fb..3b03bba 100644 --- a/config/pyside2-config.desktop +++ b/config/pyside2-config.desktop @@ -6,7 +6,7 @@ Language=Python Type=Class Files=Implementation BaseClasses=QObject -# OptionsFile=options.kcfg +OptionsFile=options.kcfg [Implementation] Name=Implementation