Ejemplo n.º 1
0
    def testFilterByLayer(self):
        """ test filtering by layer"""
        QgsProject.instance().clear()
        m = QgsMapLayerComboBox()
        l1 = QgsVectorLayer(
            "Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'layer 1', "memory")
        QgsProject.instance().addMapLayer(l1)
        l2 = QgsVectorLayer(
            "Polygon?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'lAyEr 2', "memory")
        QgsProject.instance().addMapLayer(l2)
        l3 = QgsVectorLayer("None?field=fldtxt:string&field=fldint:integer",
                            'another', "memory")
        QgsProject.instance().addMapLayer(l3)
        l4 = QgsVectorLayer(
            "LineString?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'final layer', "memory")
        QgsProject.instance().addMapLayer(l4)

        self.assertEqual(m.count(), 4)
        self.assertEqual(m.itemText(0), 'another')
        self.assertEqual(m.itemText(1), 'final layer')
        self.assertEqual(m.itemText(2), 'layer 1')
        self.assertEqual(m.itemText(3), 'lAyEr 2')

        m.setExceptedLayerList([l1, l3])
        self.assertEqual(m.count(), 2)
        self.assertEqual(m.itemText(0), 'final layer')
        self.assertEqual(m.itemText(1), 'lAyEr 2')

        m.setExceptedLayerList([l2, l4])
        self.assertEqual(m.count(), 2)
        self.assertEqual(m.itemText(0), 'another')
        self.assertEqual(m.itemText(1), 'layer 1')
Ejemplo n.º 2
0
    def testFilterGeometryType(self):
        """ test filtering by geometry type """
        QgsProject.instance().clear()
        m = QgsMapLayerComboBox()
        l1 = QgsVectorLayer(
            "Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'layer 1', "memory")
        QgsProject.instance().addMapLayer(l1)
        l2 = QgsVectorLayer(
            "Polygon?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'layer 2', "memory")
        QgsProject.instance().addMapLayer(l2)
        l3 = QgsVectorLayer("None?field=fldtxt:string&field=fldint:integer",
                            'layer 3', "memory")
        QgsProject.instance().addMapLayer(l3)
        l4 = QgsVectorLayer(
            "LineString?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
            'layer 4', "memory")
        QgsProject.instance().addMapLayer(l4)

        m.setFilters(QgsMapLayerProxyModel.PolygonLayer)
        self.assertEqual(m.count(), 1)
        self.assertEqual(m.itemText(0), 'layer 2')

        m.setFilters(QgsMapLayerProxyModel.PointLayer)
        self.assertEqual(m.count(), 1)
        self.assertEqual(m.itemText(0), 'layer 1')

        m.setFilters(QgsMapLayerProxyModel.LineLayer)
        self.assertEqual(m.count(), 1)
        self.assertEqual(m.itemText(0), 'layer 4')

        m.setFilters(QgsMapLayerProxyModel.NoGeometry)
        self.assertEqual(m.count(), 1)
        self.assertEqual(m.itemText(0), 'layer 3')

        m.setFilters(QgsMapLayerProxyModel.HasGeometry)
        self.assertEqual(m.count(), 3)
        self.assertEqual(m.itemText(0), 'layer 1')
        self.assertEqual(m.itemText(1), 'layer 2')
        self.assertEqual(m.itemText(2), 'layer 4')

        m.setFilters(QgsMapLayerProxyModel.VectorLayer)
        self.assertEqual(m.count(), 4)
        self.assertEqual(m.itemText(0), 'layer 1')
        self.assertEqual(m.itemText(1), 'layer 2')
        self.assertEqual(m.itemText(2), 'layer 3')
        self.assertEqual(m.itemText(3), 'layer 4')

        m.setFilters(QgsMapLayerProxyModel.PluginLayer)
        self.assertEqual(m.count(), 0)

        m.setFilters(QgsMapLayerProxyModel.RasterLayer)
        self.assertEqual(m.count(), 0)
Ejemplo n.º 3
0
    def testMeshLayer(self):
        m = QgsMapLayerComboBox()
        l1 = create_mesh_layer("l1")
        QgsProject.instance().addMapLayer(l1)
        l2 = create_layer('l2')
        QgsProject.instance().addMapLayer(l2)

        m.setFilters(QgsMapLayerProxyModel.MeshLayer)
        self.assertEqual(m.filters(), QgsMapLayerProxyModel.MeshLayer)

        self.assertEqual(m.count(), 1)
        self.assertEqual(m.itemText(0), 'l1')
Ejemplo n.º 4
0
    def testAdditionalLayers(self):
        QgsProject.instance().clear()
        l1 = create_layer('l1')
        l2 = create_layer('l2')
        QgsProject.instance().addMapLayers([l1, l2])
        m = QgsMapLayerComboBox()
        self.assertEqual(m.count(), 2)
        l3 = create_layer('l3')
        l4 = create_layer('l4')
        m.setAdditionalLayers([l3, l4])
        self.assertEqual(m.count(), 4)

        m.setAdditionalItems(['a', 'b'])
        self.assertEqual(m.count(), 6)
        self.assertEqual(m.itemText(0), 'l1')
        self.assertEqual(m.itemText(1), 'l2')
        self.assertEqual(m.itemText(2), 'l3')
        self.assertEqual(m.itemText(3), 'l4')
        self.assertEqual(m.itemText(4), 'a')
        self.assertEqual(m.itemText(5), 'b')

        m.setAllowEmptyLayer(True)
        self.assertEqual(m.count(), 7)
        self.assertFalse(m.itemText(0))
        self.assertEqual(m.itemText(1), 'l1')
        self.assertEqual(m.itemText(2), 'l2')
        self.assertEqual(m.itemText(3), 'l3')
        self.assertEqual(m.itemText(4), 'l4')
        self.assertEqual(m.itemText(5), 'a')
        self.assertEqual(m.itemText(6), 'b')

        l3.deleteLater()
        QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
        self.assertEqual(m.count(), 6)
        self.assertFalse(m.itemText(0))
        self.assertEqual(m.itemText(1), 'l1')
        self.assertEqual(m.itemText(2), 'l2')
        self.assertEqual(m.itemText(3), 'l4')
        self.assertEqual(m.itemText(4), 'a')
        self.assertEqual(m.itemText(5), 'b')

        l5 = create_layer('l5')
        l6 = create_layer('l6')
        m.setAdditionalLayers([l5, l6, l4])
        self.assertEqual(m.count(), 8)
        self.assertFalse(m.itemText(0))
        self.assertEqual(m.itemText(1), 'l1')
        self.assertEqual(m.itemText(2), 'l2')
        self.assertEqual(m.itemText(3), 'l4')
        self.assertEqual(m.itemText(4), 'l5')
        self.assertEqual(m.itemText(5), 'l6')
        self.assertEqual(m.itemText(6), 'a')
        self.assertEqual(m.itemText(7), 'b')

        m.setAdditionalLayers([l5, l4])
        self.assertEqual(m.count(), 7)
        self.assertFalse(m.itemText(0))
        self.assertEqual(m.itemText(1), 'l1')
        self.assertEqual(m.itemText(2), 'l2')
        self.assertEqual(m.itemText(3), 'l4')
        self.assertEqual(m.itemText(4), 'l5')
        self.assertEqual(m.itemText(5), 'a')
        self.assertEqual(m.itemText(6), 'b')

        QgsProject.instance().removeMapLayers([l1.id(), l2.id()])

        self.assertEqual(m.count(), 5)
        self.assertFalse(m.itemText(0))
        self.assertEqual(m.itemText(1), 'l4')
        self.assertEqual(m.itemText(2), 'l5')
        self.assertEqual(m.itemText(3), 'a')
        self.assertEqual(m.itemText(4), 'b')