2626from qgis .core import (
2727 Qgis ,
2828 QgsBrowserModel ,
29+ QgsCoordinateReferenceSystem ,
2930 QgsMessageLog ,
3031 QgsNetworkAccessManager ,
32+ QgsPointXY ,
33+ QgsProject ,
34+ QgsRectangle ,
3135 QgsSettings ,
3236)
3337
3741from qgis .PyQt .QtNetwork import QNetworkRequest , QSslSocket
3842from qgis .PyQt .QtWidgets import QAction
3943
40- # Import the code for the dialog
44+ from . models . Application import Application
4145from .qgis_shogun_editor_dialog import QgisShogunEditorDialog
4246from .service .Application import ApplicationService
4347from .service .GraphQLClient import GraphQLClient
48+ from .service .LayerService import LayerService
4449
4550
4651class 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 () )
0 commit comments