Skip to content

Commit

Permalink
Prevent map from panning to first layer's extent upon processing events
Browse files Browse the repository at this point in the history
  • Loading branch information
Joonalai committed May 13, 2024
1 parent 973176a commit f1c42ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Fixes

* [#53](https://github.com/GispoCoding/pytest-qgis/pull/53) Allow using MagicMocks to mock layers without problems
* [#62](https://github.com/GispoCoding/pytest-qgis/pull/62) Allow changing the map extent properly when showing the map
* [#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
16 changes: 11 additions & 5 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 All @@ -317,11 +328,6 @@ def _show_qgis_dlg(common_settings: Settings, qgis_parent: QWidget) -> None:
if not common_settings.qgis_init_disabled:
qgis_parent.setWindowTitle("Test QGIS dialog opened by Pytest-qgis")
qgis_parent.show()

# Process events each time layer a visible layer is added to
# be able to change the extent properly
assert _APP
QgsProject.instance().legendLayersAdded.connect(_APP.processEvents)
elif common_settings.qgis_init_disabled:
warnings.warn(
"Cannot show QGIS map because QGIS is not initialized. "
Expand Down
10 changes: 7 additions & 3 deletions tests/visual/test_show_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ def test_show_map_crs_change_to_4326_2(layer_polygon, layer_points, layer_polygo


@pytest.mark.qgis_show_map(timeout=DEFAULT_TIMEOUT, zoom_to_common_extent=False)
def test_show_map_should_not_keep_the_set_extent(
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(QgsRectangle(475804, 7145949.5, 549226, 7219371.5))
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().area() < 1e11 # noqa: PLR2004

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

0 comments on commit f1c42ef

Please sign in to comment.