|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsLayerTreeMapCanvasBridge. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Nyall Dawson' |
| 10 | +__date__ = '8/03/2017' |
| 11 | +__copyright__ = 'Copyright 2017, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import qgis # NOQA |
| 18 | + |
| 19 | +from qgis.core import (QgsProject, |
| 20 | + QgsApplication, |
| 21 | + QgsUnitTypes, |
| 22 | + QgsCoordinateReferenceSystem, |
| 23 | + QgsVectorLayer) |
| 24 | +from qgis.gui import (QgsLayerTreeMapCanvasBridge, |
| 25 | + QgsMapCanvas, |
| 26 | + QgsCustomLayerOrderWidget) |
| 27 | +from qgis.testing import start_app, unittest |
| 28 | +from utilities import (unitTestDataPath) |
| 29 | + |
| 30 | +app = start_app() |
| 31 | +TEST_DATA_DIR = unitTestDataPath() |
| 32 | + |
| 33 | + |
| 34 | +class TestQgsLayerTreeMapCanvasBridge(unittest.TestCase): |
| 35 | + |
| 36 | + def __init__(self, methodName): |
| 37 | + """Run once on class initialization.""" |
| 38 | + unittest.TestCase.__init__(self, methodName) |
| 39 | + |
| 40 | + def testCustomLayerOrderUpdatedFromProject(self): |
| 41 | + """ test that setting project layer order is reflected in custom layer order panel """ |
| 42 | + |
| 43 | + prj = QgsProject.instance() |
| 44 | + layer = QgsVectorLayer("Point?field=fldtxt:string", |
| 45 | + "layer1", "memory") |
| 46 | + layer2 = QgsVectorLayer("Point?field=fldtxt:string", |
| 47 | + "layer2", "memory") |
| 48 | + layer3 = QgsVectorLayer("Point?field=fldtxt:string", |
| 49 | + "layer3", "memory") |
| 50 | + prj.addMapLayers([layer, layer2, layer3]) |
| 51 | + |
| 52 | + canvas = QgsMapCanvas() |
| 53 | + bridge = QgsLayerTreeMapCanvasBridge(prj.layerTreeRoot(), canvas) |
| 54 | + custom_order_widget = QgsCustomLayerOrderWidget(bridge) |
| 55 | + |
| 56 | + #custom layer order |
| 57 | + bridge.setHasCustomLayerOrder(True) |
| 58 | + bridge.setCustomLayerOrder([layer3.id(), layer.id(), layer2.id()]) |
| 59 | + app.processEvents() |
| 60 | + self.assertEqual([l.id() for l in prj.layerOrder()], [layer3.id(), layer.id(), layer2.id()]) |
| 61 | + |
| 62 | + # no custom layer order |
| 63 | + bridge.setHasCustomLayerOrder(False) |
| 64 | + app.processEvents() |
| 65 | + self.assertEqual([l.id() for l in prj.layerOrder()], [layer.id(), layer2.id(), layer3.id()]) |
| 66 | + |
| 67 | + # mess around with the project layer order |
| 68 | + prj.setLayerOrder([layer3, layer, layer2]) |
| 69 | + app.processEvents() |
| 70 | + # make sure bridge respects this new order |
| 71 | + self.assertTrue(bridge.hasCustomLayerOrder()) |
| 72 | + self.assertEqual(bridge.customLayerOrder(), [layer3.id(), layer.id(), layer2.id()]) |
| 73 | + |
| 74 | + # try reordering through bridge |
| 75 | + bridge.setHasCustomLayerOrder(False) |
| 76 | + app.processEvents() |
| 77 | + self.assertEqual([l.id() for l in prj.layerOrder()], [layer.id(), layer2.id(), layer3.id()]) |
| 78 | + root = prj.layerTreeRoot() |
| 79 | + layer_node = root.findLayer(layer2.id()) |
| 80 | + cloned_node = layer_node.clone() |
| 81 | + parent = layer_node.parent() |
| 82 | + parent.insertChildNode(0, cloned_node) |
| 83 | + parent.removeChildNode(layer_node) |
| 84 | + app.processEvents() |
| 85 | + # make sure project respects this |
| 86 | + self.assertEqual([l.id() for l in prj.layerOrder()], [layer2.id(), layer.id(), layer3.id()]) |
| 87 | + self.assertFalse(bridge.hasCustomLayerOrder()) |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + unittest.main() |
0 commit comments