Config: Add load ans save method, fix typo
Signed-off-by: Juraj Oravec <sgd.orava@gmail.com>
This commit is contained in:
parent
88bf2c0b13
commit
b98699c704
@ -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 }}:
|
||||
|
21
config/options.kcfg
Normal file
21
config/options.kcfg
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
|
||||
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
|
||||
<kcfgfile arg="true"/>
|
||||
<group name="Config">
|
||||
<entry name="include_load_save" type="Bool">
|
||||
<label>Include load and save methods?</label>
|
||||
<default>True</default>
|
||||
</entry>
|
||||
<entry name="settings_filepath_variable" type="String">
|
||||
<label>FilePath variable</label>
|
||||
<default>settingsFile</default>
|
||||
</entry>
|
||||
<entry name="settings_group_name" type="String">
|
||||
<label>Settings group name</label>
|
||||
<default>{{ name }}</default>
|
||||
</entry>
|
||||
</group>
|
||||
</kcfg>
|
@ -6,7 +6,7 @@ Language=Python
|
||||
Type=Class
|
||||
Files=Implementation
|
||||
BaseClasses=QObject
|
||||
# OptionsFile=options.kcfg
|
||||
OptionsFile=options.kcfg
|
||||
|
||||
[Implementation]
|
||||
Name=Implementation
|
||||
|
Loading…
Reference in New Issue
Block a user