Skip to content

Commit

Permalink
Allow changing the map extent properly when showing the map (#62)
Browse files Browse the repository at this point in the history
Prevent map from panning to first layer's extent upon processing events
  • Loading branch information
Joonalai authored Jun 14, 2024
1 parent 3a33209 commit eaa39d5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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


## Fixes

* [#55](https://github.com/GispoCoding/pytest-qgis/pull/55) Allows using MagicMocks to mock layers without problems
* [#53](https://github.com/GispoCoding/pytest-qgis/pull/53) Allow using MagicMocks to mock layers without problems
* [#60](https://github.com/GispoCoding/pytest-qgis/pull/60) Allows using CRS properly again
* [#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

# Version 2.0.0 (29-11-2023)

Expand Down
11 changes: 11 additions & 0 deletions src/pytest_qgis/pytest_qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def qgis_app(request: "SubRequest") -> QgsApplication:

if not request.config._plugin_settings.qgis_init_disabled:
assert _APP
QgsProject.instance().legendLayersAdded.disconnect(_APP.processEvents)
if not sip.isdeleted(_CANVAS) and _CANVAS is not None:
_CANVAS.deleteLater()
_APP.exitQgis()
Expand Down Expand Up @@ -303,6 +304,16 @@ def _start_and_configure_qgis_app(config: "Config") -> None:

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

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


def _initialize_processing(qgis_app: QgsApplication) -> None:
python_plugins_path = os.path.join(qgis_app.pkgDataPath(), "python", "plugins")
Expand Down
16 changes: 16 additions & 0 deletions tests/visual/test_show_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,19 @@ def test_show_map_crs_change_to_4326_2(layer_polygon, layer_points, layer_polygo
QgsProject.instance().addMapLayers(
[layer_points, layer_polygon_3067, layer_polygon]
)


@pytest.mark.qgis_show_map(timeout=DEFAULT_TIMEOUT, zoom_to_common_extent=False)
def test_map_extent_should_not_change_to_layers_extent_when_processing_events(
layer_polygon_3067, qgis_canvas, qgis_app
):
extent_smaller_than_layer = QgsRectangle(475804, 7145949.5, 549226, 7219371.5)

QgsProject.instance().addMapLayer(layer_polygon_3067)
qgis_canvas.setExtent(extent_smaller_than_layer)

# This triggers the map to set the extent based on the layer
# if events are not processed after adding the layer
qgis_app.processEvents()

assert qgis_canvas.extent().height() == extent_smaller_than_layer.height()

0 comments on commit eaa39d5

Please sign in to comment.