-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdlg_settings.py
More file actions
183 lines (142 loc) · 5.52 KB
/
dlg_settings.py
File metadata and controls
183 lines (142 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! python3
"""Plugin settings form integrated into QGIS 'Options' menu."""
# standard
import platform
from functools import partial
from pathlib import Path
from urllib.parse import quote
# PyQGIS
from qgis.core import Qgis, QgsApplication
from qgis.gui import QgsOptionsPageWidget, QgsOptionsWidgetFactory
from qgis.PyQt import uic
from qgis.PyQt.Qt import QUrl
from qgis.PyQt.QtGui import QDesktopServices, QIcon
# project
from loopstructural.__about__ import (
__icon_path__,
__title__,
__uri_homepage__,
__uri_tracker__,
__version__,
)
from loopstructural.toolbelt import PlgLogger, PlgOptionsManager
from loopstructural.toolbelt.preferences import PlgSettingsStructure
# ############################################################################
# ########## Globals ###############
# ##################################
FORM_CLASS, _ = uic.loadUiType(Path(__file__).parent / "{}.ui".format(Path(__file__).stem))
# ############################################################################
# ########## Classes ###############
# ##################################
class ConfigOptionsPage(FORM_CLASS, QgsOptionsPageWidget):
"""Settings form embedded into QGIS 'options' menu."""
def __init__(self, parent):
super().__init__(parent)
self.log = PlgLogger().log
self.plg_settings = PlgOptionsManager()
# load UI and set objectName
self.setupUi(self)
self.setObjectName("mOptionsPage{}".format(__title__))
_report_context_message = quote(
"> Reported from plugin settings\n\n"
f"- operating system: {platform.system()} "
f"{platform.release()}_{platform.version()}\n"
f"- QGIS: {Qgis.QGIS_VERSION}"
f"- plugin version: {__version__}\n"
)
# header
self.lbl_title.setText(f"{__title__} - Version {__version__}")
# customization
self.btn_help.setIcon(QIcon(QgsApplication.iconPath("mActionHelpContents.svg")))
self.btn_help.pressed.connect(partial(QDesktopServices.openUrl, QUrl(__uri_homepage__)))
self.btn_report.setIcon(
QIcon(QgsApplication.iconPath("console/iconSyntaxErrorConsole.svg"))
)
self.btn_report.pressed.connect(
partial(QDesktopServices.openUrl, QUrl(f"{__uri_tracker__}new/choose"))
)
self.btn_reset.setIcon(QIcon(QgsApplication.iconPath("mActionUndo.svg")))
self.btn_reset.pressed.connect(self.reset_settings)
# load previously saved settings
self.load_settings()
def apply(self):
"""Apply settings from the form and persist them to QgsSettings."""
settings = self.plg_settings.get_plg_settings()
# misc
settings.debug_mode = self.opt_debug.isChecked()
settings.interpolator_nelements = self.n_elements_spin_box.value()
settings.interpolator_npw = self.npw_spin_box.value()
settings.interpolator_cpw = self.cpw_spin_box.value()
settings.interpolator_regularisation = self.regularisation_spin_box.value()
settings.version = __version__
# dump new settings into QgsSettings
self.plg_settings.save_from_object(settings)
if __debug__:
self.log(
message="DEBUG - Settings successfully saved.",
log_level=4,
)
def load_settings(self):
"""Load options from QgsSettings into the UI form."""
settings = self.plg_settings.get_plg_settings()
# global
self.opt_debug.setChecked(settings.debug_mode)
self.lbl_version_saved_value.setText(settings.version)
# self.interpolator_type_combo.setCurrentText(settings.interpolator_type)
self.n_elements_spin_box.setValue(settings.interpolator_nelements)
self.regularisation_spin_box.setValue(settings.interpolator_regularisation)
self.cpw_spin_box.setValue(settings.interpolator_cpw)
self.npw_spin_box.setValue(settings.interpolator_npw)
def reset_settings(self):
"""Reset settings in the UI and persisted settings to plugin defaults."""
default_settings = PlgSettingsStructure()
# dump default settings into QgsSettings
self.plg_settings.save_from_object(default_settings)
# update the form
self.load_settings()
class PlgOptionsFactory(QgsOptionsWidgetFactory):
"""Factory for options widget."""
def __init__(self, *args, **kwargs):
"""Initialize the options factory.
Parameters
----------
*args, **kwargs
Forwarded to base factory initializer.
"""
super().__init__()
def icon(self):
"""Return the icon used for the options page.
Returns
-------
QIcon
Icon for the options page.
"""
return QIcon(str(__icon_path__))
def createWidget(self, parent) -> ConfigOptionsPage:
"""Create settings widget.
Parameters
----------
parent : QObject
Qt parent where to include the options page.
Returns
-------
ConfigOptionsPage
Instantiated options page.
"""
return ConfigOptionsPage(parent)
def title(self) -> str:
"""Plugin title used to name options tab.
Returns
-------
str
Plugin title string.
"""
return __title__
def helpId(self) -> str:
"""Plugin help URL.
Returns
-------
str
URL of the plugin homepage.
"""
return __uri_homepage__