Skip to content

Commit 9b677c0

Browse files
authored
Merge pull request #15 from terrestris/feat/load-application-and-apply-mapview
Feature: load application and apply mapview
2 parents 4b98a94 + 591bd82 commit 9b677c0

14 files changed

Lines changed: 312 additions & 517 deletions

docker/docker-compose-dev.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
qgis:
33
image: qgis/qgis:3.44
4-
command: ["/bin/bash", "-c", "qgis /root/shogun_qgis_konfigurator.qgs"]
4+
command: ["/bin/bash", "-c", "qgis"]
55
environment:
66
DISPLAY: "unix${DISPLAY}"
77
RUN_IN_DOCKER: "1"
@@ -11,4 +11,3 @@ services:
1111
- /tmp/.X11-unix:/tmp/.X11-unix
1212
- ./qgis_plugins:/root/.local/share/QGIS/QGIS3/profiles/default/python/plugins
1313
- ../plugin_code:/root/.local/share/QGIS/QGIS3/profiles/default/python/plugins/qgis-shogun-editor
14-
- ./shogun_qgis_konfigurator.qgs:/root/shogun_qgis_konfigurator.qgs

docker/shogun_qgis_konfigurator.qgs

Lines changed: 0 additions & 492 deletions
This file was deleted.

plugin_code/models/Application.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
QgisShogunEditor
5+
A QGIS plugin
6+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
7+
-------------------
8+
begin : 2025-09
9+
git sha : $Format:%H$
10+
copyright : (C) 2025 by terrestris
11+
email : info@terrestris.de
12+
***************************************************************************/
13+
14+
/***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************/
22+
"""
123
from dataclasses import dataclass
224
from typing import Any, Dict, Optional
325

26+
from PyQt5.QtWidgets import QListWidgetItem
27+
428
from ..models.BaseEntity import BaseEntity
529

630

@@ -27,3 +51,8 @@ def from_dict(cls, data: Dict[str, Any]) -> 'Application':
2751
layer_config=data.get('layerConfig'),
2852
tool_config=data.get('toolConfig')
2953
)
54+
55+
def get_qt_list_item(self):
56+
item = QListWidgetItem(self.name or "Unnamed Application")
57+
item.application_id = self._id
58+
return item

plugin_code/models/BaseEntity.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
QgisShogunEditor
5+
A QGIS plugin
6+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
7+
-------------------
8+
begin : 2025-09
9+
git sha : $Format:%H$
10+
copyright : (C) 2025 by terrestris
11+
email : info@terrestris.de
12+
***************************************************************************/
13+
14+
/***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************/
22+
"""
23+
124
from dataclasses import dataclass
225
from datetime import datetime
326
from enum import Enum

plugin_code/models/Layer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
QgisShogunEditor
5+
A QGIS plugin
6+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
7+
-------------------
8+
begin : 2025-09
9+
git sha : $Format:%H$
10+
copyright : (C) 2025 by terrestris
11+
email : info@terrestris.de
12+
***************************************************************************/
13+
14+
/***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************/
22+
"""
123
from dataclasses import dataclass
224
from typing import Any, Dict, Optional
325

plugin_code/models/MutateApplication.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
QgisShogunEditor
5+
A QGIS plugin
6+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
7+
-------------------
8+
begin : 2025-09
9+
git sha : $Format:%H$
10+
copyright : (C) 2025 by terrestris
11+
email : info@terrestris.de
12+
***************************************************************************/
13+
14+
/***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************/
22+
"""
123
from dataclasses import dataclass
224
from typing import Any, Dict, Optional
325

plugin_code/models/MutateLayer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
QgisShogunEditor
5+
A QGIS plugin
6+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
7+
-------------------
8+
begin : 2025-09
9+
git sha : $Format:%H$
10+
copyright : (C) 2025 by terrestris
11+
email : info@terrestris.de
12+
***************************************************************************/
13+
14+
/***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************/
22+
"""
123
from dataclasses import dataclass
224
from typing import Any, Dict, Optional
325

plugin_code/models/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
QgisShogunEditor
5+
A QGIS plugin
6+
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
7+
-------------------
8+
begin : 2025-09
9+
git sha : $Format:%H$
10+
copyright : (C) 2025 by terrestris
11+
email : info@terrestris.de
12+
***************************************************************************/
13+
14+
/***************************************************************************
15+
* *
16+
* This program is free software; you can redistribute it and/or modify *
17+
* it under the terms of the GNU General Public License as published by *
18+
* the Free Software Foundation; either version 2 of the License, or *
19+
* (at your option) any later version. *
20+
* *
21+
***************************************************************************/
22+
"""

plugin_code/qgis_shogun_editor.py

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626
from qgis.core import (
2727
Qgis,
2828
QgsBrowserModel,
29+
QgsCoordinateReferenceSystem,
2930
QgsMessageLog,
3031
QgsNetworkAccessManager,
32+
QgsPointXY,
33+
QgsProject,
34+
QgsRectangle,
3135
QgsSettings,
3236
)
3337

@@ -37,10 +41,11 @@
3741
from qgis.PyQt.QtNetwork import QNetworkRequest, QSslSocket
3842
from qgis.PyQt.QtWidgets import QAction
3943

40-
# Import the code for the dialog
44+
from .models.Application import Application
4145
from .qgis_shogun_editor_dialog import QgisShogunEditorDialog
4246
from .service.Application import ApplicationService
4347
from .service.GraphQLClient import GraphQLClient
48+
from .service.LayerService import LayerService
4449

4550

4651
class QgisShogunEditor:
@@ -55,6 +60,9 @@ def __init__(self, iface):
5560
:type iface: QgsInterface
5661
"""
5762
# Save reference to the QGIS interface
63+
self.layer_service = None
64+
self.app_service = None
65+
self.graphql_client = None
5866
self.iface = iface
5967
# initialize plugin directory
6068
self.plugin_dir = os.path.dirname(__file__)
@@ -208,14 +216,16 @@ def run(self):
208216
"""Run method that performs all the real work"""
209217

210218
# Create the dialog with elements (after translation) and keep reference
211-
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
219+
# Only create GUI ONCE in callback, so that it will only load when thie plugin is started
212220
if self.first_start:
213221
self.first_start = False
214222
self.dlg = QgisShogunEditorDialog()
215223

216224
self.dlg.entryUrl.setPlaceholderText('Please enter an URL')
217225

218-
self.dlg.loadButton.clicked.connect(lambda: self.load_applications_graphql())
226+
self.dlg.loadButton.clicked.connect(lambda: self.initialize_graphql_and_load_app_list())
227+
228+
self.dlg.applicationsList.itemDoubleClicked.connect(self._handle_double_click)
219229

220230
# add logo
221231
logo_path = os.path.join(os.path.dirname(__file__), "shogun_logo.png")
@@ -241,6 +251,51 @@ def run(self):
241251
# substitute with your code.
242252
pass
243253

254+
def _handle_double_click(self, item):
255+
QgsMessageLog.logMessage(
256+
f"You selected: {item.text()} - application id is: {item.application_id}", 'QgisShogunEditor',
257+
level=Qgis.Info
258+
)
259+
QgsProject.instance().setTitle(item.text())
260+
application = self.app_service.get_application_by_id(item.application_id)
261+
self.apply_mapview(application)
262+
if application is not None:
263+
if application.layer_tree is not None:
264+
try:
265+
QgsMessageLog.logMessage(
266+
f"Found layer tree {application.layer_tree}:",
267+
'QgisShogunEditor',
268+
level=Qgis.Info
269+
)
270+
except json.JSONDecodeError as e:
271+
QgsMessageLog.logMessage(
272+
f"Could not decode layer tree json: {e}", 'QgisShogunEditor',
273+
level=Qgis.Critical
274+
)
275+
else:
276+
QgsMessageLog.logMessage("Application has no layer tree", 'QgisShogunEditor', level=Qgis.Warning)
277+
278+
def apply_mapview(self, application: Application):
279+
qgis_project = QgsProject.instance()
280+
client_config = application.client_config
281+
if client_config is not None and 'mapView' in client_config:
282+
map_view = client_config.get('mapView')
283+
projection = map_view.get('projection')
284+
center = map_view.get('center')
285+
crs = QgsCoordinateReferenceSystem(projection)
286+
qgis_project.setCrs(crs)
287+
288+
map_canvas = self.iface.mapCanvas()
289+
map_canvas.setCenter(QgsPointXY(center[0], center[1]))
290+
291+
zoom = map_view.get('zoom')
292+
resolutions = map_view.get('resolutions')
293+
if resolutions is not None and zoom is not None and zoom < len(resolutions):
294+
resolution = resolutions[zoom]
295+
dpi = 25.4 / 0.28
296+
inches_per_meter = 39.37
297+
map_canvas.zoomScale(resolution * dpi * inches_per_meter)
298+
244299
def open_project_link(self, event):
245300
if event.button() == Qt.LeftButton:
246301
QDesktopServices.openUrl(QUrl("https://www.terrestris.de/de/"))
@@ -292,14 +347,17 @@ def sanitize_shogun_url(self, shogun_url):
292347
shogun_url += '/graphql'
293348
return shogun_url
294349

295-
# Example usage in your code
296-
def load_applications_graphql(self):
297-
shogun_endpoint_url = self.dlg.entryUrl.text()
298-
client = GraphQLClient(shogun_endpoint_url)
350+
def initialize_graphql_client(self):
351+
sanitized_url = self.sanitize_shogun_url(self.dlg.entryUrl.text().strip())
352+
self.graphql_client = GraphQLClient(sanitized_url)
353+
self.app_service = ApplicationService(self.graphql_client)
354+
self.layer_service = LayerService(self.graphql_client)
299355

300-
app_service = ApplicationService(client)
301-
applications = app_service.get_all_applications()
356+
# Example usage in your code
357+
def initialize_graphql_and_load_app_list(self):
358+
self.initialize_graphql_client()
359+
applications = self.app_service.get_all_applications_simple()
302360
if applications is not None:
303361
self.dlg.applicationsList.clear()
304362
for app in applications:
305-
self.dlg.applicationsList.addItem(app.name)
363+
self.dlg.applicationsList.addItem(app.get_qt_list_item())

plugin_code/qgis_shogun_editor_dialog.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,4 @@ def __init__(self, parent=None):
4141
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
4242
# #widgets-and-dialogs-with-auto-connect
4343

44-
if self.is_pip_available():
45-
print('pip is available')
46-
4744
self.setupUi(self)
48-
49-
def is_pip_available(self) -> bool:
50-
try:
51-
import_module("pip") # noqa F401
52-
return True
53-
except ImportError:
54-
return False

0 commit comments

Comments
 (0)