Skip to content

Commit eaa39d5

Browse files
authored
Allow changing the map extent properly when showing the map (#62)
Prevent map from panning to first layer's extent upon processing events
1 parent 3a33209 commit eaa39d5

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
* Add `clean_qgis_layer` decorator back alongside with automatic cleaning [#45](https://github.com/GispoCoding/pytest-qgis/pull/45)
66

7-
87
## Fixes
98

10-
* [#55](https://github.com/GispoCoding/pytest-qgis/pull/55) Allows using MagicMocks to mock layers without problems
9+
* [#53](https://github.com/GispoCoding/pytest-qgis/pull/53) Allow using MagicMocks to mock layers without problems
1110
* [#60](https://github.com/GispoCoding/pytest-qgis/pull/60) Allows using CRS properly again
11+
* [#62](https://github.com/GispoCoding/pytest-qgis/pull/62) Map does no longer zoom to the first added layer upon processing the events when using `qgis_show_map` marker
1212

1313
# Version 2.0.0 (29-11-2023)
1414

src/pytest_qgis/pytest_qgis.py

+11
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def qgis_app(request: "SubRequest") -> QgsApplication:
158158

159159
if not request.config._plugin_settings.qgis_init_disabled:
160160
assert _APP
161+
QgsProject.instance().legendLayersAdded.disconnect(_APP.processEvents)
161162
if not sip.isdeleted(_CANVAS) and _CANVAS is not None:
162163
_CANVAS.deleteLater()
163164
_APP.exitQgis()
@@ -303,6 +304,16 @@ def _start_and_configure_qgis_app(config: "Config") -> None:
303304

304305
mock.patch("qgis.utils.iface", _IFACE).start()
305306

307+
if _APP is not None:
308+
# QGIS zooms to the layer's extent if it
309+
# is the first layer added to the map.
310+
# If the qgis_show_map marker is used, this zooming might occur
311+
# at some later time when events are processed (e.g. at qtbot.wait call)
312+
# and this might change the extent unexpectedly.
313+
# It is better to process events right after adding the
314+
# layer to avoid these kind of problems.
315+
QgsProject.instance().legendLayersAdded.connect(_APP.processEvents)
316+
306317

307318
def _initialize_processing(qgis_app: QgsApplication) -> None:
308319
python_plugins_path = os.path.join(qgis_app.pkgDataPath(), "python", "plugins")

tests/visual/test_show_map.py

+16
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,19 @@ def test_show_map_crs_change_to_4326_2(layer_polygon, layer_points, layer_polygo
126126
QgsProject.instance().addMapLayers(
127127
[layer_points, layer_polygon_3067, layer_polygon]
128128
)
129+
130+
131+
@pytest.mark.qgis_show_map(timeout=DEFAULT_TIMEOUT, zoom_to_common_extent=False)
132+
def test_map_extent_should_not_change_to_layers_extent_when_processing_events(
133+
layer_polygon_3067, qgis_canvas, qgis_app
134+
):
135+
extent_smaller_than_layer = QgsRectangle(475804, 7145949.5, 549226, 7219371.5)
136+
137+
QgsProject.instance().addMapLayer(layer_polygon_3067)
138+
qgis_canvas.setExtent(extent_smaller_than_layer)
139+
140+
# This triggers the map to set the extent based on the layer
141+
# if events are not processed after adding the layer
142+
qgis_app.processEvents()
143+
144+
assert qgis_canvas.extent().height() == extent_smaller_than_layer.height()

0 commit comments

Comments
 (0)