-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.py
More file actions
287 lines (225 loc) · 8.54 KB
/
helpers.py
File metadata and controls
287 lines (225 loc) · 8.54 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from urllib.parse import urlparse
from PyQt5.QtWidgets import QApplication
from qgis.PyQt.QtCore import Qt
from contextlib import contextmanager
from qgis.PyQt.QtGui import QPixmap
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.core import QgsApplication, QgsProject, QgsSettings
from .settings import PLUGIN_SETTINGS_SERVER_CONFIG_KEY
from qgis.PyQt.QtWidgets import QDialog, QVBoxLayout, QLabel, QDialogButtonBox
def get_project_layer_names() -> list:
"""
Returns a list of all layer names in the current QGIS project.
Returns:
list: List of layer names.
"""
return [layer.name() for layer in QgsProject.instance().mapLayers().values()]
def check_if_qgis_project_is_dirty_and_save() -> bool:
"""
Checks if the current QGIS project has unsaved changes and prompts the user to save.
Returns:
bool: True if the project is saved or user chose to continue, False if cancelled.
"""
if QgsProject.instance().isDirty():
msgBox = QMessageBox()
msgBox.setWindowTitle("")
msgBox.setText("There are unsaved changes.")
msgBox.setInformativeText("Do you want to save your changes before continuing?")
msgBox.setStandardButtons(QMessageBox.StandardButton.Save | QMessageBox.StandardButton.Cancel)
msgBox.button(QMessageBox.Save).setText("Save")
msgBox.button(QMessageBox.Cancel).setText("Cancel")
msgBox.setDefaultButton(QMessageBox.StandardButton.Save)
ret = msgBox.exec()
if ret == QMessageBox.StandardButton.Save:
QgsProject.instance().write()
return True
elif ret == QMessageBox.StandardButton.Cancel:
return False
return True
def qgis_project_is_saved() -> bool:
"""
Checks if the current QGIS project is saved.
If the project is not saved, display a message box to inform the user.
Returns:
bool: True if the project is saved, False otherwise.
"""
source_project_file_path = QgsProject.instance().fileName()
if not source_project_file_path:
show_fail_box('Failed', "Please use the QGIS2Mapbender from a saved QGIS-Project")
return False
return True
def create_fail_box(title: str, text: str) -> QMessageBox:
"""
Creates a QMessageBox with a failure icon.
Args:
title (str): The title of the message box.
text (str): The text to display in the message box.
Returns:
QMessageBox: The created message box.
"""
failBox = QMessageBox()
failBox.setIconPixmap(QPixmap(':/images/themes/default/mIconWarning.svg'))
failBox.setWindowTitle(title)
failBox.setText(text)
return failBox
def show_fail_box(title: str, text: str) -> int:
"""
Displays a failure message box with an OK button.
Args:
title (str): The title of the message box.
text (str): The text to display in the message box.
Returns:
int: The button clicked by the user.
"""
QApplication.restoreOverrideCursor()
failBox = create_fail_box(title, text)
failBox.setStandardButtons(QMessageBox.StandardButton.Ok)
return failBox.exec()
def show_success_box(title: str, text: str) -> int:
"""
Displays a success message box with an OK button.
Args:
title (str): The title of the message box.
text (str): The text to display in the message box.
Returns:
int: The button clicked by the user.
"""
QApplication.restoreOverrideCursor()
successBox = QMessageBox()
successBox.setIconPixmap(QPixmap(':/images/themes/default/mIconSuccess.svg'))
successBox.setWindowTitle(title)
successBox.setText(text)
successBox.setStandardButtons(QMessageBox.StandardButton.Ok)
return successBox.exec()
def show_success_link_box(title: str, text: str) -> int:
"""
Displays a success message box with a clickable link and an OK button.
Args:
title (str): The title of the message box.
text (str): The text to display in the message box, which can include a link.
Returns:
int: The button clicked by the user.
"""
QApplication.restoreOverrideCursor()
dialog = QDialog()
dialog.setWindowTitle(title)
layout = QVBoxLayout(dialog)
icon_label = QLabel()
icon_label.setPixmap(QPixmap(':/images/themes/default/mIconSuccess.svg'))
layout.addWidget(icon_label)
message_label = QLabel()
message_label.setTextFormat(Qt.TextFormat.RichText)
message_label.setTextInteractionFlags(Qt.TextInteractionFlag.TextBrowserInteraction)
message_label.setOpenExternalLinks(True)
message_label.setWordWrap(True)
message_label.setText(text)
layout.addWidget(message_label)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
button_box.accepted.connect(dialog.accept)
layout.addWidget(button_box)
return dialog.exec()
def show_question_box(text: str) -> int:
"""
Displays a question message box with Yes and No buttons.
Args:
text (str): The question to display in the message box.
Returns:
int: The button clicked by the user.
"""
questionBox = QMessageBox()
questionBox.setIcon(QMessageBox.Icon.Question)
questionBox.setText(text)
questionBox.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
questionBox.button(QMessageBox.Yes).setText("Yes")
questionBox.button(QMessageBox.No).setText("No")
return questionBox.exec()
def list_qgs_settings_child_groups(key: str) -> list:
"""
Lists the child groups of a given key in QGIS settings.
Args:
key (str): The key to search for child groups.
Returns:
list: A list of child group names.
"""
s = QgsSettings()
s.beginGroup(key)
subkeys = s.childGroups()
s.endGroup
return subkeys
@contextmanager
def waitCursor() -> None:
"""
A context manager to set the cursor to a wait state during a long-running operation.
Returns:
None
"""
try:
QgsApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
yield
except Exception as ex:
raise ex
finally:
QgsApplication.restoreOverrideCursor()
def update_mb_slug_in_settings(mb_slug: str, is_mb_slug: bool) -> None:
"""
Updates the Mapbender slug in QGIS settings.
Args:
mb_slug (str): The Mapbender slug to update.
is_mb_slug (bool): Whether to add or remove the slug.
Returns:
None
"""
s = QgsSettings()
if s.contains(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/mb_templates"):
s.beginGroup(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/")
mb_slugs = s.value('mb_templates')
s.endGroup()
if isinstance(mb_slugs, str) and mb_slugs != '':
mb_slugs_list = mb_slugs.split(", ")
elif isinstance(mb_slugs, str) and mb_slugs == '':
mb_slugs_list = []
elif isinstance(mb_slugs, list):
mb_slugs_list = mb_slugs
if is_mb_slug and mb_slug not in mb_slugs_list:
mb_slugs_list.append(mb_slug)
updated_mb_slugs = ", ".join(mb_slugs_list)
s.setValue(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/mb_templates", updated_mb_slugs)
if not is_mb_slug and mb_slug in mb_slugs_list:
mb_slugs_list.remove(mb_slug)
updated_mb_slugs = ", ".join(mb_slugs_list)
s.setValue(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/mb_templates", updated_mb_slugs)
elif is_mb_slug:
s.setValue(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/mb_templates", mb_slug)
def uri_validator(url: str) -> bool:
"""
Validates a URL to check if it is well-formed.
Args:
url (str): The URL to validate.
Returns:
bool: True if the URL is well-formed, False otherwise.
"""
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except AttributeError:
return False
def get_size_and_unit(bytes_size) -> tuple:
"""
Converts a byte size to a human-readable value and unit.
Args:
bytes_size (int or float): Size in bytes.
Returns:
tuple: (size, unit) where size is float/int and unit is str.
"""
units = ["B", "KB", "MB", "GB", "TB"]
size = float(bytes_size)
unit_index = 0
while size >= 1024 and unit_index < len(units) - 1:
size /= 1024
unit_index += 1
# Round to two decimals for units KB and above, no decimals for bytes
if unit_index == 0:
size = int(size)
else:
size = round(size, 2)
return size, units[unit_index]