2626from qgis .core import (
2727 Qgis ,
2828 QgsBrowserModel ,
29+ QgsCoordinateReferenceSystem ,
2930 QgsMessageLog ,
3031 QgsNetworkAccessManager ,
32+ QgsPointXY ,
33+ QgsProject ,
34+ QgsRectangle ,
3135 QgsSettings ,
3236 QgsRasterLayer ,
3337 QgsProject ,
4044from qgis .PyQt .QtNetwork import QNetworkRequest , QSslSocket
4145from qgis .PyQt .QtWidgets import QAction , QTreeWidgetItem , QMessageBox
4246
43- # Import the code for the dialog
47+ from . models . Application import Application
4448from .qgis_shogun_editor_dialog import QgisShogunEditorDialog
4549from .service .Application import ApplicationService
4650from .service .GraphQLClient import GraphQLClient
51+ from .service .LayerService import LayerService
4752
4853
4954class QgisShogunEditor :
@@ -58,6 +63,9 @@ def __init__(self, iface):
5863 :type iface: QgsInterface
5964 """
6065 # Save reference to the QGIS interface
66+ self .layer_service = None
67+ self .app_service = None
68+ self .graphql_client = None
6169 self .iface = iface
6270 # initialize plugin directory
6371 self .plugin_dir = os .path .dirname (__file__ )
@@ -211,15 +219,16 @@ def run(self):
211219 """Run method that performs all the real work"""
212220
213221 # Create the dialog with elements (after translation) and keep reference
214- # Only create GUI ONCE in callback, so that it will only load when the plugin is started
222+ # Only create GUI ONCE in callback, so that it will only load when thie plugin is started
215223 if self .first_start :
216224 self .first_start = False
217225 self .dlg = QgisShogunEditorDialog ()
218226
219227 self .dlg .entryUrl .setPlaceholderText ('Please enter an URL' )
220- self .dlg .entryUrl .setText ('http://192.168.100.106:8080/' )
221228
222- self .dlg .loadButton .clicked .connect (lambda : self .load_applications_graphql ())
229+ self .dlg .loadButton .clicked .connect (lambda : self .initialize_graphql_and_load_app_list ())
230+
231+ self .dlg .loadButton .clicked .connect (lambda : self ._handle_double_click )
223232
224233 # add logo
225234 logo_path = os .path .join (os .path .dirname (__file__ ), "shogun_logo.png" )
@@ -243,6 +252,51 @@ def run(self):
243252 # substitute with your code.
244253 pass
245254
255+ def _handle_double_click (self , item ):
256+ QgsMessageLog .logMessage (
257+ f"You selected: { item .text ()} - application id is: { item .application_id } " , 'QgisShogunEditor' ,
258+ level = Qgis .Info
259+ )
260+ QgsProject .instance ().setTitle (item .text ())
261+ application = self .app_service .get_application_by_id (item .application_id )
262+ self .apply_mapview (application )
263+ if application is not None :
264+ if application .layer_tree is not None :
265+ try :
266+ QgsMessageLog .logMessage (
267+ f"Found layer tree { application .layer_tree } :" ,
268+ 'QgisShogunEditor' ,
269+ level = Qgis .Info
270+ )
271+ except json .JSONDecodeError as e :
272+ QgsMessageLog .logMessage (
273+ f"Could not decode layer tree json: { e } " , 'QgisShogunEditor' ,
274+ level = Qgis .Critical
275+ )
276+ else :
277+ QgsMessageLog .logMessage ("Application has no layer tree" , 'QgisShogunEditor' , level = Qgis .Warning )
278+
279+ def apply_mapview (self , application : Application ):
280+ qgis_project = QgsProject .instance ()
281+ client_config = application .client_config
282+ if client_config is not None and 'mapView' in client_config :
283+ map_view = client_config .get ('mapView' )
284+ projection = map_view .get ('projection' )
285+ center = map_view .get ('center' )
286+ crs = QgsCoordinateReferenceSystem (projection )
287+ qgis_project .setCrs (crs )
288+
289+ map_canvas = self .iface .mapCanvas ()
290+ map_canvas .setCenter (QgsPointXY (center [0 ], center [1 ]))
291+
292+ zoom = map_view .get ('zoom' )
293+ resolutions = map_view .get ('resolutions' )
294+ if resolutions is not None and zoom is not None and zoom < len (resolutions ):
295+ resolution = resolutions [zoom ]
296+ dpi = 25.4 / 0.28
297+ inches_per_meter = 39.37
298+ map_canvas .zoomScale (resolution * dpi * inches_per_meter )
299+
246300 def open_project_link (self , event ):
247301 if event .button () == Qt .LeftButton :
248302 QDesktopServices .openUrl (QUrl ("https://www.terrestris.de/de/" ))
@@ -462,17 +516,20 @@ def sanitize_shogun_url(self, shogun_url):
462516 shogun_url += '/graphql'
463517 return shogun_url
464518
465- # Example usage in your code
466- def load_applications_graphql (self ):
467- shogun_endpoint_url = self .dlg .entryUrl .text ()
468- client = GraphQLClient (shogun_endpoint_url )
519+ def initialize_graphql_client (self ):
520+ sanitized_url = self .sanitize_shogun_url (self .dlg .entryUrl .text ().strip ())
521+ self .graphql_client = GraphQLClient (sanitized_url )
522+ self .app_service = ApplicationService (self .graphql_client )
523+ self .layer_service = LayerService (self .graphql_client )
469524
470- app_service = ApplicationService (client )
471- applications = app_service .get_all_applications ()
525+ # Example usage in your code
526+ def initialize_graphql_and_load_app_list (self ):
527+ self .initialize_graphql_client ()
528+ applications = self .app_service .get_all_applications_simple ()
472529 if applications is not None :
473530 self .dlg .applicationsList .clear ()
474531 for app in applications :
475- self .dlg .applicationsList .addItem (app .name )
532+ self .dlg .applicationsList .addItem (app .get_qt_list_item () )
476533
477534def load_applications (self ):
478535 inputUrl = self .dlg .entryUrl .text ()
0 commit comments