-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_dialog.py
More file actions
538 lines (461 loc) · 22.4 KB
/
main_dialog.py
File metadata and controls
538 lines (461 loc) · 22.4 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import os
from typing import Optional
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QSettings, QRegularExpression, Qt
from qgis.PyQt.QtGui import QRegularExpressionValidator, QPixmap, QIcon
from qgis.PyQt.QtWidgets import QMessageBox, QTableWidgetItem, QHeaderView, QWidget, QTabWidget, QRadioButton, QPushButton, \
QTableWidget, QComboBox, QDialogButtonBox, QToolButton, QLabel, QApplication
from qgis.core import Qgis, QgsSettings, QgsMessageLog
from .api_request import ApiRequest
from .qgis_server_api_upload import QgisServerApiUpload
from .mapbender_api_upload import MapbenderApiUpload
from .dialogs.server_config_dialog import ServerConfigDialog
from .helpers import qgis_project_is_saved, check_if_qgis_project_is_dirty_and_save, \
show_fail_box, show_success_box, show_success_link_box, \
list_qgs_settings_child_groups, show_question_box, \
update_mb_slug_in_settings
from .paths import Paths
from .server_config import ServerConfig
from .settings import PLUGIN_SETTINGS_SERVER_CONFIG_KEY, TAG
# Dialog from .ui file
WIDGET, BASE = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'dialogs/ui/main_dialog.ui'))
class MainDialog(BASE, WIDGET):
"""
Main dialog window for the QGIS2Mapbender plugin.
Handles user interactions for server configuration, project publishing, and updating Mapbender applications.
"""
tabWidget: QTabWidget
serverUploadTab: QWidget
serverConfigTab: QWidget
publishRadioButton: QRadioButton
cloneTemplateRadioButton: QRadioButton
serverTableWidget: QTableWidget
warningFirstServerLabel: QLabel
serverConfigComboBox: QComboBox
mbSlugComboBox: QComboBox
buttonBoxTab1: QDialogButtonBox
publishButton: QPushButton
updateButton: QPushButton
addServerConfigButton: QToolButton
duplicateServerConfigButton: QToolButton
editServerConfigButton: QToolButton
removeServerConfigButton: QToolButton
buttonBoxTab2: QDialogButtonBox
def __init__(self, parent=None):
"""
Initializes the main dialog and sets up the UI and signal connections.
Args:
parent: Optional parent widget.
"""
super().__init__(parent)
self.setupUi(self)
self.setupConnections()
def setupUi(self, widget) -> None:
"""
Sets up the user interface for the main dialog.
Args:
widget: The parent widget for the dialog.
Returns:
None
"""
super().setupUi(widget)
self.warningFirstServerLabel.setPixmap(QPixmap(':/images/themes/default/mIconWarning.svg'))
# Tabs
self.tabWidget.setCurrentWidget(self.serverUploadTab)
# Tab
self.publishButton.setIcon(QIcon(':/images/themes/default/mActionSharingExport.svg'))
self.updateButton.setIcon(QIcon(':/images/themes/default/mActionRefresh.svg'))
self.update_server_combo_box()
self.publishRadioButton.setChecked(True)
self.update_slug_combo_box()
self.mbSlugComboBox.setCurrentIndex(-1)
self.cloneTemplateRadioButton.setChecked(True)
self.publishButton.setEnabled(False) # Enabled only if mbSlugComboBox has a value
self.updateButton.setEnabled(False)
# QLineValidator for slug:
regex_slug_url = QRegularExpression("[^\\s;\\\\/]*")
regex_layer_set = QRegularExpression("^(?!\\s)[^;/\\\\]*$")
regex_slug_url_validator = QRegularExpressionValidator(regex_slug_url)
regex_layer_set_validator = QRegularExpressionValidator(regex_layer_set)
self.mbSlugComboBox.setValidator(regex_slug_url_validator)
self.layerSetLineEdit.setValidator(regex_layer_set_validator)
# Tab2
self.addServerConfigButton.setIcon(QIcon(':/images/themes/default/mActionAdd.svg'))
self.duplicateServerConfigButton.setIcon(QIcon(':/images/themes/default/mActionEditCopy.svg'))
self.removeServerConfigButton.setIcon(QIcon(':/images/themes/default/mIconDelete.svg'))
self.editServerConfigButton.setIcon(QIcon(':/images/themes/default/mActionAllEdits.svg'))
server_table_headers = [self.tr("Name"),
self.tr("Mapbender URL")] # "QGIS Server path" ,
self.serverTableWidget.setColumnCount(len(server_table_headers))
self.serverTableWidget.setHorizontalHeaderLabels(server_table_headers)
self.serverTableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
self.update_server_table()
# Buttons
self.addServerConfigButton.setToolTip(self.tr("Add server configuration"))
self.duplicateServerConfigButton.setToolTip(self.tr("Duplicate selected server configuration"))
self.editServerConfigButton.setToolTip(self.tr("Edit selected server configuration"))
self.removeServerConfigButton.setToolTip(self.tr("Remove selected server configuration"))
self.buttonBoxTab2.rejected.connect(self.reject)
# Set Button Tab2 to english
button_close_tab2 = self.buttonBoxTab2.button(QDialogButtonBox.Close)
button_close_tab2.setText(self.tr("Close"))
def setupConnections(self) -> None:
"""
Connects UI signals to their respective slots for user interaction.
Returns:
None
"""
self.tabWidget.currentChanged.connect(self.update_server_combo_box)
self.publishRadioButton.clicked.connect(self.enable_publish_parameters)
self.updateRadioButton.clicked.connect(self.disable_publish_parameters)
self.mbSlugComboBox.lineEdit().textChanged.connect(self.validate_slug_not_empty)
self.mbSlugComboBox.currentIndexChanged.connect(self.validate_slug_not_empty)
self.publishButton.clicked.connect(self.run)
self.updateButton.clicked.connect(self.run)
self.buttonBoxTab1.rejected.connect(self.reject)
self.addServerConfigButton.clicked.connect(self.on_add_server_config_clicked)
self.duplicateServerConfigButton.clicked.connect(self.on_duplicate_server_config_clicked)
self.editServerConfigButton.clicked.connect(self.on_edit_server_config_clicked)
self.removeServerConfigButton.clicked.connect(self.on_remove_server_config_clicked)
self.serverTableWidget.doubleClicked.connect(self.on_edit_server_config_clicked)
# Set Button Tab1 to english
button_close_tab1 = self.buttonBoxTab1.button(QDialogButtonBox.Close)
button_close_tab1.setText(self.tr("Close"))
# Button had a blue background
button_close_tab1.setAutoDefault(False)
button_close_tab1.setDefault(False)
def update_server_table(self) -> None:
"""
Updates the server configuration table with current settings.
Returns:
None
"""
server_config_list = list_qgs_settings_child_groups(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/connection")
self.serverTableWidget.setRowCount(len(server_config_list))
for i, name in enumerate(server_config_list):
server_config = ServerConfig.getParamsFromSettings(name)
item_name = QTableWidgetItem(server_config.name)
self.serverTableWidget.setItem(i, 0, item_name)
server_config = ServerConfig.getParamsFromSettings(name)
item_mb_basis_url = QTableWidgetItem()
item_mb_basis_url.setText(server_config.mb_basis_url)
self.serverTableWidget.setItem(i, 1, item_mb_basis_url)
# Further columns
# item_qgis_server_path = QTableWidgetItem()
# item_qgis_server_path.setText(server_config.qgis_server_path)
# self.serverTableWidget.setItem(i, 2, item_qgis_server_path)
self.update_server_combo_box()
def update_server_combo_box(self) -> None:
"""
Updates the server configuration dropdown menu
Returns:
None
"""
# Read server configurations
server_config_list = list_qgs_settings_child_groups(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/connection")
if len(server_config_list) == 0:
self.warningFirstServerLabel.show()
self.serverComboBoxLabel.setText(self.tr("Please add a server"))
self.serverConfigComboBox.clear()
return
# fetch the original names for each config
original_names = []
for key in server_config_list:
config = ServerConfig.getParamsFromSettings(key)
original_names.append(config.name) # .name is original name
# Update server configuration-combobox
self.serverComboBoxLabel.setText(self.tr("Server"))
self.warningFirstServerLabel.hide()
self.serverConfigComboBox.clear()
self.serverConfigComboBox.addItems(original_names)
def update_slug_combo_box(self) -> None:
"""
Updates the Mapbender slug combo box with available slugs from settings.
Returns:
None
"""
s = QgsSettings()
if not s.contains(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/mb_templates"):
return
s.beginGroup(PLUGIN_SETTINGS_SERVER_CONFIG_KEY)
mb_slugs = s.value('mb_templates')
s.endGroup()
if isinstance(mb_slugs, str):
mb_slugs_list = mb_slugs.split(", ")
else:
mb_slugs_list = mb_slugs
self.mbSlugComboBox.clear()
if len(mb_slugs) > 0:
self.mbSlugComboBox.addItems(mb_slugs_list)
self.mbSlugComboBox.setCurrentIndex(-1)
def disable_publish_parameters(self) -> None:
"""
Disables the Mapbender parameters input fields and toggles button states for update mode.
Returns:
None
"""
self.mbParamsFrame.setEnabled(False)
self.updateButton.setEnabled(True)
self.publishButton.setEnabled(False)
def enable_publish_parameters(self) -> None:
"""
Enables the Mapbender parameters input fields and toggles button states for publish mode.
Returns:
None
"""
self.mbParamsFrame.setEnabled(True)
self.updateButton.setEnabled(False)
self.publishButton.setEnabled(True)
def validate_slug_not_empty(self) -> None:
"""
Enables the publish button only if the Mapbender slug field is not empty.
Returns:
None
"""
self.publishButton.setEnabled(self.mbSlugComboBox.currentText() != '')
def open_server_config_dialog(self, config_name: Optional[str] = None, mode: Optional[str] = None) -> None:
"""
Opens the server configuration dialog for adding, editing, or duplicating a server config.
Args:
config_name (Optional[str]): The name of the server configuration to edit or duplicate.
mode (Optional[str]): The mode for the dialog ('edit', 'duplicate', or None for new).
Returns:
None
"""
new_server_config_dialog = ServerConfigDialog(server_config_name=config_name, mode=mode) #, parent=iface.mainWindow())
new_server_config_dialog.exec()
self.update_server_table()
self.update_server_combo_box()
def on_add_server_config_clicked(self) -> None:
"""
Slot for adding a new server configuration.
Returns:
None
"""
self.open_server_config_dialog()
def get_selected_server_config(self) -> Optional[str]:
"""
Returns the name of the currently selected server configuration in the table.
Returns:
Optional[str]: The selected server configuration name, or None if none is selected.
"""
selected_row = self.serverTableWidget.currentRow()
if selected_row == -1:
return None
return self.serverTableWidget.item(selected_row, 0).text()
def on_duplicate_server_config_clicked(self) -> None:
"""
Slot for duplicating the selected server configuration.
Returns:
None
"""
selected_server_config = self.get_selected_server_config()
self.open_server_config_dialog(selected_server_config, mode='duplicate')
def on_edit_server_config_clicked(self) -> None:
"""
Slot for editing the selected server configuration.
Returns:
None
"""
selected_server_config = self.get_selected_server_config()
self.open_server_config_dialog(selected_server_config, mode='edit')
def on_remove_server_config_clicked(self) -> None:
"""
Slot for removing the selected server configuration after user confirmation.
Returns:
None
"""
selected_row = self.serverTableWidget.currentRow()
if selected_row == -1:
return
selected_server_config = self.serverTableWidget.item(selected_row, 0).text()
if show_question_box(self.tr(
"Are you sure you want to remove the server configuration '{selected_server_config}'?").format(
selected_server_config=selected_server_config)) != QMessageBox.StandardButton.Yes:
return
s = QSettings()
s.remove(f"{PLUGIN_SETTINGS_SERVER_CONFIG_KEY}/connection/{selected_server_config}")
show_success_box(self.tr('Success'),
self.tr('Server configuration successfully removed'))
self.update_server_table()
self.update_server_combo_box()
def initialize_api_request(self) -> tuple[ServerConfig, ApiRequest]:
"""
Initializes and returns the server configuration and ApiRequest instance.
Returns:
tuple[ServerConfig, ApiRequest]: The server configuration and API request objects.
"""
server_config = ServerConfig.getParamsFromSettings(self.serverConfigComboBox.currentText())
api_request = ApiRequest(server_config)
return server_config, api_request
def run(self) -> None:
"""
Executes the publishing or updating process for the current QGIS project.
Handles project validation, API initialization, upload, and Mapbender operations.
Provides user feedback and error handling.
Returns:
None
"""
if not qgis_project_is_saved():
return
if not check_if_qgis_project_is_dirty_and_save():
QgsMessageLog.logMessage("Publish/Update cancelled by the user (unsaved changes).", TAG, level=Qgis.MessageLevel.Info)
return
# Set waiting cursor
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
wms_url = None
api_request = None
try:
action = "publish" if self.publishRadioButton.isChecked() else "update"
if action == "publish" and self.mbSlugComboBox.currentText() == '':
show_fail_box(self.tr("Please complete Mapbender parameters"),
self.tr("Please enter a valid Mapbender URL title"))
return
server_config, api_request = self.initialize_api_request()
if not api_request.token:
return
QgsMessageLog.logMessage("Preparing upload to QGIS server...", TAG, level=Qgis.MessageLevel.Info)
# Get server config: project paths
paths = Paths.get_paths()
qgis_server_upload = QgisServerApiUpload(api_request, paths)
status_code_server_upload, upload_dir = qgis_server_upload.process_and_upload_project()
if status_code_server_upload == 200:
wms_url = qgis_server_upload.get_wms_url(server_config, upload_dir)
if not wms_url:
return
if action == "publish":
self.mb_publish(server_config, api_request, wms_url)
else:
self.mb_update(server_config, api_request, wms_url)
finally:
# Restore default cursor
QApplication.restoreOverrideCursor()
if api_request is not None:
api_request.mark_api_requests_done()
def mb_publish(self, server_config: ServerConfig, api_request: ApiRequest, wms_url: str) -> None:
"""
Publishes a WMS to Mapbender and assigns it to an application.
Args:
server_config (ServerConfig): The server configuration object.
api_request (ApiRequest): The API request object.
wms_url (str): The URL of the WMS to be published.
Returns:
None
"""
# Parameters
is_clone_app = self.cloneTemplateRadioButton.isChecked()
layer_set = self.layerSetLineEdit.text()
input_slug = self.mbSlugComboBox.currentText()
try:
mb_upload = MapbenderApiUpload(server_config, api_request, wms_url)
exit_status_mb_upload, source_ids, is_reloaded = mb_upload.mb_upload()
if exit_status_mb_upload != 0 or not source_ids:
QgsMessageLog.logMessage(f"FAILED mb_upload", TAG, level=Qgis.MessageLevel.Info)
return
if is_clone_app:
exit_status_app_clone, slug = mb_upload.clone_app_and_get_slug(input_slug)
if exit_status_app_clone != 200:
update_mb_slug_in_settings(input_slug, is_mb_slug=False)
self.update_slug_combo_box()
return
QgsMessageLog.logMessage(f"Application was cloned to {slug}", TAG,
level=Qgis.MessageLevel.Info)
update_mb_slug_in_settings(input_slug, is_mb_slug=True)
self.update_slug_combo_box()
else:
slug = input_slug
exit_status_wms_assign = mb_upload.assign_wms_to_source(slug, source_ids[0], layer_set)
if exit_status_wms_assign != 200:
return
if is_reloaded:
QgsMessageLog.logMessage(
f"WMS {wms_url} already existed as a Mapbender source(s) and was successfully reloaded (source(s) {source_ids}) and added to Mapbender application : {slug}", TAG,
level=Qgis.MessageLevel.Info)
name_source = ', '.join(f'#{i}' for i in source_ids if i)
link = f"{server_config.mb_basis_url}/application/{slug}"
show_success_link_box(
self.tr("Success report"),
self.tr("""
WMS already existed as a Mapbender source(s) and was successfully reloaded: {name}
<br><br>
Link to Capabilities:
<br><br>
<a href="{wms_url}" style="color:black;">{wms_url}</a>
<br><br>
Link to Mapbender application:
<br><br>
<a href="{link}" style="color:black;">{link}</a>
""").format(
name=name_source,
wms_url=wms_url,
link=link
)
)
else:
QgsMessageLog.logMessage(
f"WMS successfully created: {wms_url} and added to Mapbender application : {slug}", TAG,
level=Qgis.MessageLevel.Info)
link = f"{server_config.mb_basis_url}/application/{slug}"
show_success_link_box(
self.tr("Success report"),
self.tr("""
WMS successfully created
<br><br>
Link to Capabilities:
<br><br>
<a href="{wms_url}" style="color:black;">{wms_url}</a>
<br><br>
Link to Mapbender application:
<br><br>
<a href="{link}" style="color:black;">{link}</a>
""").format(
wms_url=wms_url,
link=link
)
)
#self.close()
except Exception as e:
show_fail_box(self.tr("Failed"), f"An error occurred during Mapbender publish: {e}")
QgsMessageLog.logMessage(f"Error in mb_publish: {e}", TAG, level=Qgis.MessageLevel.Critical)
return
def mb_update(self, server_config: ServerConfig, api_request: ApiRequest, wms_url: str)-> None:
"""
Updates an existing WMS in Mapbender by reloading its source.
Args:
server_config (ServerConfig): The server configuration object.
api_request (ApiRequest): The API request object.
wms_url (str): The URL of the WMS to be updated.
Returns:
None
"""
try:
mb_reload = MapbenderApiUpload(server_config, api_request, wms_url)
exit_status, source_ids = mb_reload.mb_reload()
if exit_status != 0 or not source_ids:
show_fail_box(self.tr("Failed"), f"No source to update. WMS {wms_url} is not an existing source in Mapbender.")
QgsMessageLog.logMessage(f"FAILED mb_update: No source to update. WMS {wms_url} is not an existing source in Mapbender.", TAG, level=Qgis.MessageLevel.Info)
return
else:
source_ids_msg = ", ".join(map(str, source_ids))
QgsMessageLog.logMessage(
f"WMS successfully updated and successfully updated in Mapbender source(s): {source_ids_msg}!", TAG,
level=Qgis.MessageLevel.Info)
name_source = ', '.join(f'#{i}' for i in source_ids if i)
show_success_link_box(
self.tr("Success report"),
self.tr("""
WMS successfully updated in QGIS Server and successfully updated in Mapbender source(s): {name_source}
<br><br>
Link to Capabilities:
<br><br>
<a href="{wms_url}" style="color:black;">{wms_url}</a>
""").format(
name_source=name_source,
wms_url=wms_url
)
)
except Exception as e:
show_fail_box(self.tr("Failed"), f"An error occurred during Mapbender update: {e}")
QgsMessageLog.logMessage(f"Error in mb_update: {e}", TAG, level=Qgis.MessageLevel.Critical)
return