-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.py
256 lines (210 loc) · 8.84 KB
/
options.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from qgis.core import QgsSettings
from qgis.gui import (
QgsCollapsibleGroupBox,
QgsOptionsPageWidget,
QgsOptionsWidgetFactory,
)
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import (
QCheckBox,
QFormLayout,
QLabel,
QRadioButton,
QTextEdit,
QVBoxLayout,
)
from .resources import *
class KgrFinderOptionsFactory(QgsOptionsWidgetFactory):
def __init__(self):
super().__init__()
def icon(self):
return QIcon(":/plugins/kgr_finder/assets/greif.png")
def createWidget(self, parent):
return ConfigOptionsPage(parent)
class ConfigOptionsPage(QgsOptionsPageWidget):
osm_tags = [
"heritage",
"archaeological_site",
"historic",
"memorial",
"statue",
"tomb",
"mosque",
"place_of_worship",
"museum",
"artwork",
"castle",
"ruins",
"monastery",
]
idai_gazetteer_filter = [
"None",
"populated-place",
"archaeological-site",
"archaeological-area",
"building-institution",
]
settings_tags = ["OSM abfragen", "iDAI abfragen"]
initially_checked = {
"osm_tags": ["heritage", "historic"],
"settings_tags": ["iDAI abfragen", "OSM abfragen"],
"idai_gazetteer_filter": "archaeological-site",
}
labels = {
"settings_tags": "Which Api should be used?",
"osm_tags": "Tags included in a OSM search. Only in use if settings have checked the API",
"osm_custom_tags_textarea": "Custom OSM Tags that should be respected (each on one line)",
"idai_gazetteer_filter": "Please choose the location type that is used for a iDAI.gazetteer search",
"idai_gazetteer_tags_tagarea": "Tags that should filter the result (each on one line). Tags act with AND operator.",
}
def __init__(self, parent):
super().__init__(parent)
layout = QFormLayout()
self.setLayout(layout)
for key in self.initially_checked.keys():
current_value = QgsSettings().value(f"/KgrFinder/{key}", [])
if not current_value and not self.anyCheckboxChecked(key):
QgsSettings().setValue(f"/KgrFinder/{key}", self.initially_checked[key])
self.section_checkboxes = {}
self.text_areas = {}
self.section_radio_buttons = {}
group_box_layout_settings = self.createCheckBoxes(
layout, "Settings", self.settings_tags, "settings_tags"
)
group_box_layout_osm = self.createCheckBoxes(
layout, "OSM – Cultural Tags", self.osm_tags, "osm_tags"
)
self.createTextarea(
group_box_layout_osm,
"custom_osm_tags",
self.labels["osm_custom_tags_textarea"],
)
group_box_layout_gazetteer = self.createRadioButtons(
layout,
"IDAI Gazetteer Filter",
self.idai_gazetteer_filter,
"idai_gazetteer_filter",
)
self.createTextarea(
group_box_layout_gazetteer,
"custom_gazetteer_tags",
self.labels["idai_gazetteer_tags_tagarea"],
)
self.applyInitialSettings()
self.loadAndSetCheckboxes()
def applyInitialSettings(self):
for key in self.initially_checked.keys():
current_value = QgsSettings().value(f"/KgrFinder/{key}", [])
if not current_value and not self.anyCheckboxChecked(key):
QgsSettings().setValue(f"/KgrFinder/{key}", self.initially_checked[key])
def anyCheckboxChecked(self, settings_key):
osm_tags = QgsSettings().value(f"/KgrFinder/{settings_key}", [])
return any(tag in osm_tags for tag in self.initially_checked[settings_key])
def createCheckBoxes(self, layout, group_title, tags, settings_key):
group_box = QgsCollapsibleGroupBox(group_title)
group_box.setCollapsed(True)
group_box_layout = QVBoxLayout()
group_box.setLayout(group_box_layout)
# Add an informational label
info_label = QLabel(self.labels[settings_key])
group_box_layout.addWidget(info_label)
checkboxes = []
for tag in tags:
checkbox = QCheckBox(tag)
checkbox.setStyleSheet("margin: 10px;")
checkbox.stateChanged.connect(self.checkboxStateChanged)
checkboxes.append((tag, checkbox))
group_box_layout.addWidget(checkbox)
layout.addWidget(group_box)
# Save checkboxes in the dictionary
self.section_checkboxes[settings_key] = checkboxes
# Load selected tags from settings and set checkboxes
kgr_tags = QgsSettings().value(f"/KgrFinder/{settings_key}", [])
for tag, checkbox in checkboxes:
checkbox.setChecked(tag in kgr_tags)
return group_box_layout
def createTextarea(self, group_box_layout, key, info_label_text):
textarea = QTextEdit()
textarea.setPlaceholderText("tag1")
# Add an informational label
info_label = QLabel(info_label_text)
info_label.setStyleSheet("font-style: italic;")
group_box_layout.addWidget(info_label)
group_box_layout.addWidget(textarea)
self.text_areas[key] = textarea
def createRadioButtons(self, layout, group_title, tags, settings_key):
group_box = QgsCollapsibleGroupBox(group_title)
group_box.setCollapsed(True)
group_box_layout = QVBoxLayout()
group_box.setLayout(group_box_layout)
# Add an informational label
info_label = QLabel(self.labels[settings_key])
group_box_layout.addWidget(info_label)
radio_buttons = []
for tag in tags:
radio_button = QRadioButton(tag)
radio_button.setStyleSheet("margin: 10px;")
radio_button.toggled.connect(self.radioButtonToggled)
radio_buttons.append((tag, radio_button))
group_box_layout.addWidget(radio_button)
layout.addWidget(group_box)
# Save radio buttons in the dictionary
self.section_radio_buttons[settings_key] = radio_buttons
# Load selected tag from settings and set radio buttons
selected_tag = QgsSettings().value(f"/KgrFinder/{settings_key}", "")
if not selected_tag and not any(
button[1].isChecked() for button in radio_buttons
):
# If no saved setting and no radio button is checked, set the first one as checked
for tag, radio_button in radio_buttons:
if tag == self.initially_checked["gazetteer_filter"]:
radio_button.setChecked(
tag == self.initially_checked["gazetteer_filter"]
)
else:
for tag, radio_button in radio_buttons:
radio_button.setChecked(tag == selected_tag)
return group_box_layout
def radioButtonToggled(self, checked):
sender = self.sender()
if checked:
for settings_key, radio_buttons in self.section_radio_buttons.items():
selected_tag = next(
tag
for tag, radio_button in radio_buttons
if radio_button.isChecked()
)
QgsSettings().setValue(f"/KgrFinder/{settings_key}", selected_tag)
def apply(self):
for settings_key, checkboxes in self.section_checkboxes.items():
kgr_tags = [
tag
for tag, checkbox in checkboxes
if isinstance(checkbox, QCheckBox) and checkbox.isChecked()
]
QgsSettings().setValue(f"/KgrFinder/{settings_key}", kgr_tags)
QgsSettings().setValue(
f"/KgrFinder/custom_osm_tags",
self.text_areas["custom_osm_tags"].toPlainText().splitlines(),
)
QgsSettings().setValue(
f"/KgrFinder/custom_gazetteer_tags",
self.text_areas["custom_gazetteer_tags"].toPlainText().splitlines(),
)
def loadAndSetCheckboxes(self):
for settings_key, checkboxes in self.section_checkboxes.items():
kgr_tags = QgsSettings().value(f"/KgrFinder/{settings_key}", [])
for tag, checkbox in checkboxes:
checkbox.setChecked(tag in kgr_tags)
custom_osm_tags = QgsSettings().value(f"/KgrFinder/custom_osm_tags", [])
custom_gazetteer_tags = QgsSettings().value(
f"/KgrFinder/custom_gazetteer_tags", []
)
self.text_areas["custom_osm_tags"].setPlainText("\n".join(custom_osm_tags))
self.text_areas["custom_gazetteer_tags"].setPlainText(
"\n".join(custom_gazetteer_tags)
)
def checkboxStateChanged(self):
for settings_key, checkboxes in self.section_checkboxes.items():
kgr_tags = [tag for tag, checkbox in checkboxes if checkbox.isChecked()]
QgsSettings().setValue(f"/KgrFinder/{settings_key}", kgr_tags)