"""QGIS screenshots for the XYZ tile URL list (run by tools/qgis-capture/scene_shot.ps1). 1. Writes the connections in technicalgis-xyz-connections.xml into the throwaway profile. 2. Adds Esri World Imagery with the Esri road and label overlays (an open "hybrid") over Keswick. 3. Grabs the main window with the Browser panel listing the loaded connections. 4. Opens Data Source Manager > XYZ Tiles > New, fills in OpenTopoMap and grabs the dialogue. """ from pathlib import Path from qgis.core import ( QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsDataSourceUri, QgsProject, QgsRasterLayer, QgsRectangle, QgsSettings, ) from qgis.gui import QgsBrowserTreeView from qgis.PyQt.QtCore import QTimer from qgis.PyQt.QtWidgets import QAbstractButton, QApplication, QDockWidget, QLineEdit, QPushButton, QSpinBox, QWidget HERE = Path(__file__).parent XML = HERE / "technicalgis-xyz-connections.xml" def steps(iface, shot, log): st = {} def setup(): log("setup") mw = iface.mainWindow() mw.showNormal() mw.resize(1500, 900) mw.move(40, 40) # The import itself was tested headless in xyz_tile_catalogue.py; here the same XML is written # straight into settings so no confirmation prompt can block the capture. import xml.etree.ElementTree as ET s = QgsSettings() s.remove("connections/xyz") for item in ET.parse(XML).getroot(): for key in ("url", "zmin", "zmax", "authcfg", "username", "password", "referer", "tilePixelRatio"): s.setValue(f"connections/xyz/items/{item.get('name')}/{key}", item.get(key, "")) iface.reloadConnections() s = QgsSettings() s.beginGroup("connections/xyz/items") log(f"connections: {len(s.childGroups())}") s.endGroup() for dock in mw.findChildren(QDockWidget): if dock.objectName() not in ("Browser", "Layers"): dock.hide() project = QgsProject.instance() project.setCrs(QgsCoordinateReferenceSystem("EPSG:3857")) for name in ("Esri World Imagery", "Esri Transportation (roads)", "Esri Boundaries and Places (labels)"): p = f"connections/xyz/items/{name}" uri = QgsDataSourceUri() uri.setParam("type", "xyz") uri.setParam("url", s.value(f"{p}/url")) uri.setParam("zmin", "0") uri.setParam("zmax", str(s.value(f"{p}/zmax"))) layer = QgsRasterLayer(bytes(uri.encodedUri()).decode(), name, "wms") log(f"{name}: valid={layer.isValid()}") project.addMapLayer(layer) tr = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:4326"), project.crs(), project) iface.mapCanvas().setExtent(tr.transformBoundingBox(QgsRectangle(-3.20, 54.565, -3.10, 54.612))) iface.mapCanvas().refresh() def zoom(): project = QgsProject.instance() canvas = iface.mapCanvas() canvas.setDestinationCrs(project.crs()) tr = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:4326"), project.crs(), project) canvas.setExtent(tr.transformBoundingBox(QgsRectangle(-3.20, 54.565, -3.10, 54.612))) canvas.refresh() log(f"scale {canvas.scale():.0f}") def expand_browser(): tree = iface.mainWindow().findChild(QDockWidget, "Browser").findChild(QgsBrowserTreeView) model = tree.model() for r in range(model.rowCount()): idx = model.index(r, 0) if model.data(idx) == "XYZ Tiles": tree.expand(idx) tree.scrollTo(idx, tree.PositionAtTop) st["tree"] = tree log("browser expanded") def grab_main(): shot(iface.mainWindow(), "01-qgis-xyz-connections-hybrid.png") def open_dsm(): iface.openDataSourceManagerPage("XYZ") def click_new(): dsm = next(w for w in QApplication.topLevelWidgets() if w.metaObject().className() == "QgsDataSourceManagerDialog") dsm.show() dsm.raise_() QApplication.processEvents() st["dsm"] = dsm page = next((w for w in dsm.findChildren(QWidget) if w.metaObject().className() == "QgsXyzSourceSelect"), None) log(f"data source manager visible={dsm.isVisible()} xyz page={page is not None}") for b in (page or dsm).findChildren(QAbstractButton): if b.text().replace("&", "") == "New": QTimer.singleShot(0, b.click) log("clicked New") return def fill_dialog(): dlg = QApplication.activeModalWidget() log(f"modal={dlg.metaObject().className() if dlg else None}") st["dlg"] = dlg edits = {e.objectName(): e for e in dlg.findChildren(QLineEdit)} edits["mEditName"].setText("OpenTopoMap") edits["mEditUrl"].setText("https://a.tile.opentopomap.org/{z}/{x}/{y}.png") for cb in dlg.findChildren(QAbstractButton): if cb.objectName() == "mCheckBoxZMax": cb.setChecked(True) for sp in dlg.findChildren(QSpinBox): if sp.objectName() == "mSpinZMax": sp.setValue(17) dlg.resize(620, dlg.height()) log("dialog filled: " + ", ".join(sorted(edits))) def grab_dialog(): shot(st["dlg"], "02-qgis-xyz-connection-dialog.png") st["dlg"].reject() st["dsm"].close() return [ (500, setup), (2000, zoom), (1000, expand_browser), (15000, grab_main), (500, open_dsm), (2500, click_new), (2000, fill_dialog), (1000, grab_dialog), ]