diff --git a/travel_time_platform_plugin/main.py b/travel_time_platform_plugin/main.py index c5aef43..16d0bcc 100644 --- a/travel_time_platform_plugin/main.py +++ b/travel_time_platform_plugin/main.py @@ -21,9 +21,7 @@ QLineEdit, QMessageBox, QPushButton, - QSplitter, QTreeView, - QWidget, ) from . import express, resources, tests, tiles, ui @@ -212,22 +210,37 @@ def show_tiles(self): browser.raise_() # Get the XYZ item of the treeview - treeview = ( - self.iface.mainWindow() - .findChild(QDockWidget, "Browser") - .findChild(QWidget, "mContents") - .findChild(QSplitter) - .widget(0) - .findChild(QTreeView) - ) + treeview = browser.findChild(QTreeView) model = treeview.model() + xyz_tiles_group_idx = model.match( + model.index(0, 0), + Qt.DisplayRole, + "XYZ Tiles", + ) + if len(xyz_tiles_group_idx) == 0: + # Shouldn't happen, but let's play safe + return - match = model.match(model.index(0, 0), Qt.DisplayRole, "XYZ Tiles")[0] + # Fold everything and display it treeview.collapseAll() treeview.clearSelection() - treeview.expand(match) - treeview.setCurrentIndex(match) - treeview.scrollTo(match) + treeview.expand(xyz_tiles_group_idx[0]) + treeview.setCurrentIndex(xyz_tiles_group_idx[0]) + treeview.scrollTo(xyz_tiles_group_idx[0]) + + # If tiles were loaded, selected it + lux_tiles_idx = model.match( + model.index(0, 0, xyz_tiles_group_idx[0]), + Qt.DisplayRole, + "TravelTime - Lux", + ) + if len(lux_tiles_idx) == 0: + # If not loaded (because missing API), good enough, we showed background tiles + return + + # Display it + treeview.setCurrentIndex(lux_tiles_idx[0]) + treeview.scrollTo(lux_tiles_idx[0]) def show_config(self): self.config_dialog.exec_() diff --git a/travel_time_platform_plugin/tests/__init__.py b/travel_time_platform_plugin/tests/__init__.py index f90df44..603cdfc 100644 --- a/travel_time_platform_plugin/tests/__init__.py +++ b/travel_time_platform_plugin/tests/__init__.py @@ -16,6 +16,7 @@ def run_suite(stream) -> unittest.TestResult: [ "travel_time_platform_plugin.tests.tests_express_tools", "travel_time_platform_plugin.tests.tests_algorithms", + "travel_time_platform_plugin.tests.tests_misc", ] ) runner = unittest.TextTestRunner(stream=stream, verbosity=2) diff --git a/travel_time_platform_plugin/tests/tests_misc.py b/travel_time_platform_plugin/tests/tests_misc.py new file mode 100644 index 0000000..10ea02f --- /dev/null +++ b/travel_time_platform_plugin/tests/tests_misc.py @@ -0,0 +1,41 @@ +from qgis.PyQt.QtWidgets import QDockWidget, QTreeView +from qgis.utils import iface + +from .base import TestCaseBase + + +class MiscTest(TestCaseBase): + """Testing other features""" + + def test_loading_map_tiles(self): + + browser = iface.mainWindow().findChild(QDockWidget, "Browser") + treeview = browser.findChild(QTreeView) + model = treeview.model() + + # Hide the browser + browser.setVisible(False) + # Select the first item + treeview.setCurrentIndex(model.index(0, 0)) + + self._feedback() + + # Ensure the browser is hidden + self.assertFalse(browser.isVisible()) + # Ensure the XYZ layer is not selected + self.assertNotEqual( + model.data(treeview.currentIndex()), + "TravelTime - Lux", + ) + + # Use the action + self.plugin.action_show_tiles.trigger() + self._feedback() + + # Ensure the panel got shown + self.assertTrue(browser.isVisible()) + # Ensure the XYZ layer got selected + self.assertEqual( + model.data(treeview.currentIndex()), + "TravelTime - Lux", + )