Example #1
0
    def testGettersSetters(self):
        """
        Test provider list
        """
        d = QgsProviderSublayerDetails()
        d.setProviderKey('key')
        self.assertEqual(d.providerKey(), 'key')

        d.setType(QgsMapLayerType.MeshLayer)
        self.assertEqual(d.type(), QgsMapLayerType.MeshLayer)

        d.setUri('some uri')
        self.assertEqual(d.uri(), 'some uri')

        d.setName('name')
        self.assertEqual(d.name(), 'name')

        d.setDescription('desc')
        self.assertEqual(d.description(), 'desc')

        d.setPath(['a', 'b', 'c'])
        self.assertEqual(d.path(), ['a', 'b', 'c'])

        self.assertEqual(d.featureCount(), Qgis.FeatureCountState.UnknownCount)
        d.setFeatureCount(1000)
        self.assertEqual(d.featureCount(), 1000)

        self.assertEqual(d.wkbType(), QgsWkbTypes.Unknown)
        d.setWkbType(QgsWkbTypes.Point)
        self.assertEqual(d.wkbType(), QgsWkbTypes.Point)

        d.setGeometryColumnName('geom_col')
        self.assertEqual(d.geometryColumnName(), 'geom_col')

        d.setLayerNumber(13)
        self.assertEqual(d.layerNumber(), 13)

        d.setDriverName('drv')
        self.assertEqual(d.driverName(), 'drv')

        d.setSkippedContainerScan(True)
        self.assertTrue(d.skippedContainerScan())
        d.setSkippedContainerScan(False)
        self.assertFalse(d.skippedContainerScan())

        d.setFlags(Qgis.SublayerFlag.SystemTable)
        self.assertEqual(d.flags(),
                         Qgis.SublayerFlags(Qgis.SublayerFlag.SystemTable))
        d.setFlags(Qgis.SublayerFlags())
        self.assertEqual(d.flags(), Qgis.SublayerFlags())
    def testGettersSetters(self):
        """
        Test provider list
        """
        d = QgsProviderSublayerDetails()
        d.setProviderKey('key')
        self.assertEqual(d.providerKey(), 'key')

        d.setType(QgsMapLayerType.MeshLayer)
        self.assertEqual(d.type(), QgsMapLayerType.MeshLayer)

        d.setUri('some uri')
        self.assertEqual(d.uri(), 'some uri')

        d.setName('name')
        self.assertEqual(d.name(), 'name')

        d.setDescription('desc')
        self.assertEqual(d.description(), 'desc')

        d.setPath(['a', 'b', 'c'])
        self.assertEqual(d.path(), ['a', 'b', 'c'])

        self.assertEqual(d.featureCount(), Qgis.FeatureCountState.UnknownCount)
        d.setFeatureCount(1000)
        self.assertEqual(d.featureCount(), 1000)

        self.assertEqual(d.wkbType(), QgsWkbTypes.Unknown)
        d.setWkbType(QgsWkbTypes.Point)
        self.assertEqual(d.wkbType(), QgsWkbTypes.Point)

        d.setGeometryColumnName('geom_col')
        self.assertEqual(d.geometryColumnName(), 'geom_col')

        d.setLayerNumber(13)
        self.assertEqual(d.layerNumber(), 13)
    def test_equality(self):
        """
        Test equality operator
        """
        d = QgsProviderSublayerDetails()
        d2 = QgsProviderSublayerDetails()
        d.setProviderKey('key')
        self.assertNotEqual(d, d2)
        d2.setProviderKey('key')
        self.assertEqual(d, d2)

        d.setType(QgsMapLayerType.MeshLayer)
        self.assertNotEqual(d, d2)
        d2.setType(QgsMapLayerType.MeshLayer)
        self.assertEqual(d, d2)

        d.setUri('some uri')
        self.assertNotEqual(d, d2)
        d2.setUri('some uri')
        self.assertEqual(d, d2)

        d.setName('name')
        self.assertNotEqual(d, d2)
        d2.setName('name')
        self.assertEqual(d, d2)

        d.setDescription('desc')
        self.assertNotEqual(d, d2)
        d2.setDescription('desc')
        self.assertEqual(d, d2)

        d.setPath(['a', 'b', 'c'])
        self.assertNotEqual(d, d2)
        d2.setPath(['a', 'b', 'c'])
        self.assertEqual(d, d2)

        d.setFeatureCount(1000)
        self.assertNotEqual(d, d2)
        d2.setFeatureCount(1000)
        self.assertEqual(d, d2)

        d.setWkbType(QgsWkbTypes.Point)
        self.assertNotEqual(d, d2)
        d2.setWkbType(QgsWkbTypes.Point)
        self.assertEqual(d, d2)

        d.setGeometryColumnName('geom_col')
        self.assertNotEqual(d, d2)
        d2.setGeometryColumnName('geom_col')
        self.assertEqual(d, d2)

        d.setLayerNumber(13)
        self.assertNotEqual(d, d2)
        d2.setLayerNumber(13)
        self.assertEqual(d, d2)

        d.setDriverName('drv')
        self.assertNotEqual(d, d2)
        d2.setDriverName('drv')
        self.assertEqual(d, d2)

        d.setSkippedContainerScan(True)
        self.assertNotEqual(d, d2)
        d2.setSkippedContainerScan(True)
        self.assertEqual(d, d2)
Example #4
0
    def test_model_with_paths(self):
        model = QgsProviderSublayerModel()

        layer1 = QgsProviderSublayerDetails()
        layer1.setType(QgsMapLayerType.RasterLayer)
        layer1.setName('layer 1')
        layer1.setDescription('description 1')
        layer1.setProviderKey('gdal')
        layer1.setUri('uri 1')
        layer1.setPath(['my', 'path'])

        model.setSublayerDetails([layer1])
        self.assertEqual(model.rowCount(QModelIndex()), 1)
        my_group_index = model.index(0, 0)
        self.assertEqual(model.data(my_group_index, Qt.DisplayRole), 'my')
        self.assertEqual(model.rowCount(my_group_index), 1)
        path_group_index = model.index(0, 0, my_group_index)
        self.assertEqual(model.data(path_group_index, Qt.DisplayRole), 'path')
        self.assertEqual(model.rowCount(path_group_index), 1)
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index), Qt.DisplayRole),
            'layer 1')
        self.assertEqual(
            model.data(model.index(0, 1, path_group_index), Qt.DisplayRole),
            'description 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.ProviderKey), 'gdal')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.RasterLayer)
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Uri), 'uri 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Name), 'layer 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Description),
            'description 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Flags), 0)

        # different path
        layer2 = QgsProviderSublayerDetails()
        layer2.setType(QgsMapLayerType.VectorLayer)
        layer2.setName('layer 2')
        layer2.setDescription('description 2')
        layer2.setProviderKey('ogr')
        layer2.setUri('uri 2')
        layer2.setFeatureCount(-1)
        layer2.setWkbType(QgsWkbTypes.LineString)
        layer2.setFlags(Qgis.SublayerFlags(Qgis.SublayerFlag.SystemTable))
        layer2.setPath(['my'])

        model.setSublayerDetails([layer1, layer2])

        self.assertEqual(model.rowCount(QModelIndex()), 1)
        my_group_index = model.index(0, 0)
        self.assertEqual(model.data(my_group_index, Qt.DisplayRole), 'my')
        self.assertEqual(model.rowCount(my_group_index), 2)
        path_group_index = model.index(0, 0, my_group_index)
        self.assertEqual(model.data(path_group_index, Qt.DisplayRole), 'path')
        self.assertEqual(model.rowCount(path_group_index), 1)
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index), Qt.DisplayRole),
            'layer 1')
        self.assertEqual(
            model.data(model.index(0, 1, path_group_index), Qt.DisplayRole),
            'description 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.ProviderKey), 'gdal')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.RasterLayer)
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Uri), 'uri 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Name), 'layer 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Description),
            'description 1')
        self.assertEqual(
            model.data(model.index(0, 0, path_group_index),
                       QgsProviderSublayerModel.Role.Flags), 0)
        layer2_index = model.index(1, 0, my_group_index)
        self.assertEqual(model.data(layer2_index, Qt.DisplayRole), 'layer 2')
        self.assertEqual(
            model.data(model.index(1, 1, my_group_index), Qt.DisplayRole),
            'description 2 - LineString (Uncounted)')
        self.assertEqual(
            model.data(layer2_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'ogr')
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.VectorLayer)
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.Uri),
            'uri 2')
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.Name),
            'layer 2')
        self.assertEqual(
            model.data(layer2_index,
                       QgsProviderSublayerModel.Role.Description),
            'description 2')
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.Flags), 1)

        # no path
        layer3 = QgsProviderSublayerDetails()
        layer3.setType(QgsMapLayerType.VectorLayer)
        layer3.setName('layer 3')
        layer3.setProviderKey('ogr')
        layer3.setUri('uri 3')
        layer3.setFeatureCount(1001)
        layer3.setWkbType(QgsWkbTypes.Polygon)

        model.setSublayerDetails([layer1, layer2, layer3])

        self.assertEqual(model.rowCount(QModelIndex()), 2)
        my_group_index = model.index(0, 0)
        self.assertEqual(model.data(my_group_index, Qt.DisplayRole), 'my')
        self.assertEqual(model.rowCount(my_group_index), 2)
        path_group_index = model.index(0, 0, my_group_index)
        self.assertEqual(model.data(path_group_index, Qt.DisplayRole), 'path')
        self.assertEqual(model.rowCount(path_group_index), 1)
        layer1_index = model.index(0, 0, path_group_index)
        self.assertEqual(model.data(layer1_index, Qt.DisplayRole), 'layer 1')
        self.assertEqual(
            model.data(model.index(0, 1, path_group_index), Qt.DisplayRole),
            'description 1')
        self.assertEqual(
            model.data(layer1_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'gdal')
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.RasterLayer)
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.Uri),
            'uri 1')
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.Name),
            'layer 1')
        self.assertEqual(
            model.data(layer1_index,
                       QgsProviderSublayerModel.Role.Description),
            'description 1')
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.Flags), 0)
        layer2_index = model.index(1, 0, my_group_index)
        self.assertEqual(model.data(layer2_index, Qt.DisplayRole), 'layer 2')
        self.assertEqual(
            model.data(model.index(1, 1, my_group_index), Qt.DisplayRole),
            'description 2 - LineString (Uncounted)')
        self.assertEqual(
            model.data(layer2_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'ogr')
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.VectorLayer)
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.Uri),
            'uri 2')
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.Name),
            'layer 2')
        self.assertEqual(
            model.data(layer2_index,
                       QgsProviderSublayerModel.Role.Description),
            'description 2')
        self.assertEqual(
            model.data(layer2_index, QgsProviderSublayerModel.Role.Flags), 1)
        layer3_index = model.index(1, 0)
        self.assertEqual(model.data(layer3_index, Qt.DisplayRole), 'layer 3')
        self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole),
                         'Polygon (1,001)')
        self.assertEqual(
            model.data(layer3_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'ogr')
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.VectorLayer)
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.Uri),
            'uri 3')
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.Name),
            'layer 3')
        self.assertEqual(
            model.data(layer3_index,
                       QgsProviderSublayerModel.Role.Description), None)

        self.assertEqual(model.indexToSublayer(layer1_index), layer1)
        self.assertEqual(model.indexToSublayer(layer2_index), layer2)
        self.assertEqual(model.indexToSublayer(layer3_index), layer3)
        self.assertFalse(
            model.indexToSublayer(model.index(3, 0, QModelIndex())).name())
        self.assertFalse(
            model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name())

        # remove a layer
        model.setSublayerDetails([layer3, layer1])

        self.assertEqual(model.rowCount(QModelIndex()), 2)
        my_group_index = model.index(0, 0)
        self.assertEqual(model.data(my_group_index, Qt.DisplayRole), 'my')
        self.assertEqual(model.rowCount(my_group_index), 1)
        path_group_index = model.index(0, 0, my_group_index)
        self.assertEqual(model.data(path_group_index, Qt.DisplayRole), 'path')
        self.assertEqual(model.rowCount(path_group_index), 1)
        layer1_index = model.index(0, 0, path_group_index)
        self.assertEqual(model.data(layer1_index, Qt.DisplayRole), 'layer 1')
        self.assertEqual(
            model.data(model.index(0, 1, path_group_index), Qt.DisplayRole),
            'description 1')
        self.assertEqual(
            model.data(layer1_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'gdal')
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.RasterLayer)
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.Uri),
            'uri 1')
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.Name),
            'layer 1')
        self.assertEqual(
            model.data(layer1_index,
                       QgsProviderSublayerModel.Role.Description),
            'description 1')
        self.assertEqual(
            model.data(layer1_index, QgsProviderSublayerModel.Role.Flags), 0)
        layer3_index = model.index(1, 0)
        self.assertEqual(model.data(layer3_index, Qt.DisplayRole), 'layer 3')
        self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole),
                         'Polygon (1,001)')
        self.assertEqual(
            model.data(layer3_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'ogr')
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.VectorLayer)
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.Uri),
            'uri 3')
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.Name),
            'layer 3')
        self.assertEqual(
            model.data(layer3_index,
                       QgsProviderSublayerModel.Role.Description), None)

        # remove another layer
        model.setSublayerDetails([layer3])

        self.assertEqual(model.rowCount(QModelIndex()), 2)
        my_group_index = model.index(0, 0)
        self.assertEqual(model.data(my_group_index, Qt.DisplayRole), 'my')
        self.assertEqual(model.rowCount(my_group_index), 1)
        path_group_index = model.index(0, 0, my_group_index)
        self.assertEqual(model.data(path_group_index, Qt.DisplayRole), 'path')
        self.assertEqual(model.rowCount(path_group_index), 0)
        layer3_index = model.index(1, 0)
        self.assertEqual(model.data(layer3_index, Qt.DisplayRole), 'layer 3')
        self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole),
                         'Polygon (1,001)')
        self.assertEqual(
            model.data(layer3_index,
                       QgsProviderSublayerModel.Role.ProviderKey), 'ogr')
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.LayerType),
            QgsMapLayerType.VectorLayer)
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.Uri),
            'uri 3')
        self.assertEqual(
            model.data(layer3_index, QgsProviderSublayerModel.Role.Name),
            'layer 3')
        self.assertEqual(
            model.data(layer3_index,
                       QgsProviderSublayerModel.Role.Description), None)