def querySublayers(self, uri: str, flags=Qgis.SublayerQueryFlags(), feedback=None): res = QgsProviderSublayerDetails() res.setProviderKey(self.key()) return [res]
def querySublayers(self, uri: str, flags=Qgis.SublayerQueryFlags(), feedback=None): res = QgsProviderSublayerDetails() res.setProviderKey(self.key()) res.setUri(uri) res.setName(QgsProviderUtils.suggestLayerNameFromFilePath(uri)) return [res]
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)
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())
def test_to_layer(self): """ Test converting sub layer details to a layer """ details = QgsProviderSublayerDetails() details.setUri(os.path.join(unitTestDataPath(), 'lines.shp')) details.setName('my sub layer') details.setType(QgsMapLayerType.VectorLayer) details.setProviderKey('ogr') options = QgsProviderSublayerDetails.LayerOptions(QgsCoordinateTransformContext()) ml = details.toLayer(options) self.assertTrue(ml.isValid()) self.assertIsInstance(ml, QgsVectorLayer) self.assertEqual(ml.name(), 'my sub layer')
def test_sublayerDetailsAreIncomplete(self): """ Test sublayerDetailsAreIncomplete """ uri = unitTestDataPath() + '/mixed_types.TAB' # surface scan only sublayers = QgsProviderRegistry.instance().querySublayers(uri) self.assertEqual(len(sublayers), 1) self.assertEqual(sublayers[0].wkbType(), QgsWkbTypes.Unknown) # need to resolve geometry types for complete details about this uri! self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers)) self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownGeometryType))) # ...unless we are ignoring both unknown feature count and unknown geometry types self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount | QgsProviderUtils. SublayerCompletenessFlag.IgnoreUnknownGeometryType))) # fake feature count, now we have complete details if we ignore unknown geometry type sublayers[0].setFeatureCount(5) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownGeometryType))) # retry with retrieving geometry types sublayers = QgsProviderRegistry.instance().querySublayers( uri, Qgis.SublayerQueryFlag.ResolveGeometryType) # now we have all the details self.assertEqual(len(sublayers), 3) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers)) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownGeometryType))) # this geopackage file requires manually requesting feature counts uri = unitTestDataPath() + '/mixed_layers.gpkg' # surface scan only sublayers = QgsProviderRegistry.instance().querySublayers(uri) self.assertEqual(len(sublayers), 4) self.assertEqual(sublayers[0].name(), 'band1') self.assertEqual(sublayers[1].name(), 'band2') self.assertEqual(sublayers[2].name(), 'points') self.assertEqual(sublayers[2].featureCount(), Qgis.FeatureCountState.Uncounted) self.assertEqual(sublayers[3].name(), 'lines') self.assertEqual(sublayers[3].featureCount(), Qgis.FeatureCountState.Uncounted) # need to count features for complete details about this uri! self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers)) self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownGeometryType))) # ...unless we are ignoring unknown feature counts, that is... self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) # retry with retrieving feature count sublayers = QgsProviderRegistry.instance().querySublayers( uri, Qgis.SublayerQueryFlag.CountFeatures) # now we have all the details self.assertEqual(len(sublayers), 4) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( sublayers, QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownGeometryType))) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers)) self.assertEqual(sublayers[0].name(), 'band1') self.assertEqual(sublayers[1].name(), 'band2') self.assertEqual(sublayers[2].name(), 'points') self.assertEqual(sublayers[2].featureCount(), 0) self.assertEqual(sublayers[3].name(), 'lines') self.assertEqual(sublayers[3].featureCount(), 6) # test with sublayer with skippedContainerScan flag sl1 = QgsProviderSublayerDetails() sl1.setProviderKey('ogr') sl1.setType(QgsMapLayerType.VectorLayer) sl1.setWkbType(QgsWkbTypes.Point) sl1.setFeatureCount(1) sl1.setSkippedContainerScan(False) self.assertFalse( QgsProviderUtils.sublayerDetailsAreIncomplete( [sl1], QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) self.assertFalse(QgsProviderUtils.sublayerDetailsAreIncomplete([sl1])) sl2 = QgsProviderSublayerDetails() sl2.setProviderKey('ogr') sl2.setType(QgsMapLayerType.VectorLayer) sl2.setWkbType(QgsWkbTypes.Point) sl2.setFeatureCount(1) sl2.setSkippedContainerScan(True) self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete( [sl2], QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) self.assertTrue(QgsProviderUtils.sublayerDetailsAreIncomplete([sl2])) self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete( [sl1, sl2], QgsProviderUtils.SublayerCompletenessFlags( QgsProviderUtils.SublayerCompletenessFlag. IgnoreUnknownFeatureCount))) self.assertTrue( QgsProviderUtils.sublayerDetailsAreIncomplete([sl1, sl2]))
def test_to_mime(self): """ Test converting sub layer details to mime URIs """ details = QgsProviderSublayerDetails() details.setUri(os.path.join(unitTestDataPath(), 'lines.shp')) details.setName('my sub layer') details.setType(QgsMapLayerType.VectorLayer) details.setProviderKey('ogr') uri = details.toMimeUri() self.assertEqual(uri.layerType, 'vector') self.assertEqual(uri.providerKey, 'ogr') self.assertEqual(uri.name, 'my sub layer') self.assertEqual(uri.uri, os.path.join(unitTestDataPath(), 'lines.shp')) details.setType(QgsMapLayerType.RasterLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'raster') details.setType(QgsMapLayerType.MeshLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'mesh') details.setType(QgsMapLayerType.VectorTileLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'vector-tile') details.setType(QgsMapLayerType.PointCloudLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'pointcloud') details.setType(QgsMapLayerType.PluginLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'plugin') details.setType(QgsMapLayerType.GroupLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'group') details.setType(QgsMapLayerType.AnnotationLayer) uri = details.toMimeUri() self.assertEqual(uri.layerType, 'annotation')
def test_proxy(self): """ Test QgsProviderSublayerProxyModel """ model = QgsProviderSublayerModel() proxy = QgsProviderSublayerProxyModel() proxy.setSourceModel(model) self.assertEqual(model.rowCount(QModelIndex()), 0) self.assertEqual(proxy.columnCount(QModelIndex()), 2) self.assertEqual(proxy.headerData(0, Qt.Horizontal, Qt.DisplayRole), 'Item') self.assertEqual(proxy.headerData(0, Qt.Horizontal, Qt.ToolTipRole), 'Item') self.assertEqual(proxy.headerData(1, Qt.Horizontal, Qt.DisplayRole), 'Description') self.assertEqual(proxy.headerData(1, Qt.Horizontal, Qt.ToolTipRole), 'Description') layer1 = QgsProviderSublayerDetails() layer1.setType(QgsMapLayerType.RasterLayer) layer1.setName('layer 1') layer1.setDescription('description 1') layer1.setProviderKey('gdal') layer1.setUri('uri 1') layer2 = QgsProviderSublayerDetails() layer2.setType(QgsMapLayerType.VectorLayer) layer2.setName('another layer 2') layer2.setDescription('description 2') layer2.setProviderKey('ogr') layer2.setUri('uri 2') layer2.setFeatureCount(-1) layer2.setWkbType(QgsWkbTypes.LineString) model.setSublayerDetails([layer1, layer2]) item1 = QgsProviderSublayerModel.NonLayerItem() item1.setUri('item uri 1') item1.setName('item name 1') item1.setType('item type 1') item1.setDescription('item desc 1') model.addNonLayerItem(item1) self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(0, 1), Qt.DisplayRole), 'item desc 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'item uri 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.Name), 'item name 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.Description), 'item desc 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True) self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'another layer 2') self.assertEqual(proxy.data(proxy.index(1, 1), Qt.DisplayRole), 'description 2 - LineString (Uncounted)') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'uri 2') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.Name), 'another layer 2') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.Description), 'description 2') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) self.assertEqual(proxy.data(proxy.index(2, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(proxy.data(proxy.index(2, 1), Qt.DisplayRole), 'description 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) proxy.setFilterString(' 1') self.assertEqual(proxy.rowCount(QModelIndex()), 2) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'layer 1') proxy.setFilterString(' 2') self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'another layer 2') proxy.setFilterString('ITEM') self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') # should also allow filtering by vector layer wkb type strings proxy.setFilterString('LineSTRING') self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'another layer 2') proxy.setFilterString('') self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'another layer 2') self.assertEqual(proxy.data(proxy.index(2, 0), Qt.DisplayRole), 'layer 1') # add a system table layer3 = QgsProviderSublayerDetails() layer3.setType(QgsMapLayerType.VectorLayer) layer3.setName('system table') layer3.setFlags(Qgis.SublayerFlags(Qgis.SublayerFlag.SystemTable)) model.setSublayerDetails([layer1, layer2, layer3]) # system tables should be hidden by default self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'another layer 2') self.assertEqual(proxy.data(proxy.index(2, 0), Qt.DisplayRole), 'layer 1') proxy.setIncludeSystemTables(True) self.assertEqual(proxy.rowCount(QModelIndex()), 4) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'another layer 2') self.assertEqual(proxy.data(proxy.index(2, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(proxy.data(proxy.index(3, 0), Qt.DisplayRole), 'system table')
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)
def test_model_with_non_layer_items(self): model = QgsProviderSublayerModel() self.assertEqual(model.rowCount(QModelIndex()), 0) self.assertEqual(model.columnCount(QModelIndex()), 2) self.assertEqual(model.headerData(0, Qt.Horizontal, Qt.DisplayRole), 'Item') self.assertEqual(model.headerData(0, Qt.Horizontal, Qt.ToolTipRole), 'Item') self.assertEqual(model.headerData(1, Qt.Horizontal, Qt.DisplayRole), 'Description') self.assertEqual(model.headerData(1, Qt.Horizontal, Qt.ToolTipRole), 'Description') layer1 = QgsProviderSublayerDetails() layer1.setType(QgsMapLayerType.RasterLayer) layer1.setName('layer 1') layer1.setDescription('description 1') layer1.setProviderKey('gdal') layer1.setUri('uri 1') model.setSublayerDetails([layer1]) self.assertEqual(model.rowCount(QModelIndex()), 1) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Flags), 0) self.assertEqual( model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1) self.assertFalse( model.indexToSublayer(model.index(1, 0, QModelIndex())).name()) self.assertFalse( model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name()) item1 = QgsProviderSublayerModel.NonLayerItem() item1.setUri('item uri 1') item1.setName('item name 1') item1.setType('item type 1') item1.setDescription('item desc 1') model.addNonLayerItem(item1) self.assertEqual(model.rowCount(QModelIndex()), 2) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole), 'item desc 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'item uri 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Name), 'item name 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Description), 'item desc 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True) self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.NonLayerItemType), 'item type 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Flags), None) self.assertEqual( model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1) self.assertFalse( model.indexToSublayer(model.index(1, 0, QModelIndex())).name()) self.assertFalse( model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name()) self.assertEqual( model.indexToNonLayerItem(model.index(1, 0, QModelIndex())), item1) self.assertFalse( model.indexToNonLayerItem(model.index(2, 0, QModelIndex())).name()) item2 = QgsProviderSublayerModel.NonLayerItem() item2.setUri('item uri 2') item2.setName('item name 2') item2.setType('item type 2') item2.setDescription('item desc 2') model.addNonLayerItem(item2) self.assertEqual(model.rowCount(QModelIndex()), 3) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole), 'item desc 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'item uri 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Name), 'item name 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Description), 'item desc 1') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True) self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.NonLayerItemType), 'item type 1') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'item name 2') self.assertEqual(model.data(model.index(2, 1), Qt.DisplayRole), 'item desc 2') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Uri), 'item uri 2') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Name), 'item name 2') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Description), 'item desc 2') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True) self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.NonLayerItemType), 'item type 2') self.assertEqual( model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1) self.assertFalse( model.indexToSublayer(model.index(1, 0, QModelIndex())).name()) self.assertFalse( model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name()) self.assertEqual( model.indexToNonLayerItem(model.index(1, 0, QModelIndex())), item1) self.assertEqual( model.indexToNonLayerItem(model.index(2, 0, QModelIndex())), item2) self.assertFalse( model.indexToNonLayerItem(model.index(3, 0, QModelIndex())).name())
def test_model(self): model = QgsProviderSublayerModel() self.assertEqual(model.rowCount(QModelIndex()), 0) self.assertEqual(model.columnCount(QModelIndex()), 2) self.assertEqual(model.headerData(0, Qt.Horizontal, Qt.DisplayRole), 'Item') self.assertEqual(model.headerData(0, Qt.Horizontal, Qt.ToolTipRole), 'Item') self.assertEqual(model.headerData(1, Qt.Horizontal, Qt.DisplayRole), 'Description') self.assertEqual(model.headerData(1, Qt.Horizontal, Qt.ToolTipRole), 'Description') # no crash, should return invalid results self.assertFalse( model.indexToSublayer(model.index(0, 0, QModelIndex())).name()) self.assertFalse( model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name()) layer1 = QgsProviderSublayerDetails() layer1.setType(QgsMapLayerType.RasterLayer) layer1.setName('layer 1') layer1.setDescription('description 1') layer1.setProviderKey('gdal') layer1.setUri('uri 1') model.setSublayerDetails([layer1]) self.assertEqual(model.rowCount(QModelIndex()), 1) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Flags), 0) self.assertEqual( model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1) self.assertFalse( model.indexToSublayer(model.index(1, 0, QModelIndex())).name()) self.assertFalse( model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name()) 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)) model.setSublayerDetails([layer1, layer2]) self.assertEqual(model.rowCount(QModelIndex()), 2) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Flags), 0) self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'layer 2') self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole), 'description 2 - LineString (Uncounted)') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'uri 2') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Name), 'layer 2') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Description), 'description 2') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Flags), 1) self.assertEqual( model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1) self.assertEqual( model.indexToSublayer(model.index(1, 0, QModelIndex())), layer2) self.assertFalse( model.indexToSublayer(model.index(2, 0, QModelIndex())).name()) self.assertFalse( model.indexToNonLayerItem(model.index(0, 0, QModelIndex())).name()) 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()), 3) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'layer 2') self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole), 'description 2 - LineString (Uncounted)') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'uri 2') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Name), 'layer 2') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Description), 'description 2') self.assertEqual(model.data(model.index(2, 0), Qt.DisplayRole), 'layer 3') self.assertEqual(model.data(model.index(2, 1), Qt.DisplayRole), 'Polygon (1,001)') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Uri), 'uri 3') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Name), 'layer 3') self.assertEqual( model.data(model.index(2, 0), QgsProviderSublayerModel.Role.Description), None) self.assertEqual( model.indexToSublayer(model.index(0, 0, QModelIndex())), layer1) self.assertEqual( model.indexToSublayer(model.index(1, 0, QModelIndex())), layer2) self.assertEqual( model.indexToSublayer(model.index(2, 0, QModelIndex())), 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) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'description 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual(model.data(model.index(1, 0), Qt.DisplayRole), 'layer 3') self.assertEqual(model.data(model.index(1, 1), Qt.DisplayRole), 'Polygon (1,001)') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'uri 3') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Name), 'layer 3') self.assertEqual( model.data(model.index(1, 0), QgsProviderSublayerModel.Role.Description), None) # remove another layer model.setSublayerDetails([layer3]) self.assertEqual(model.rowCount(QModelIndex()), 1) self.assertEqual(model.data(model.index(0, 0), Qt.DisplayRole), 'layer 3') self.assertEqual(model.data(model.index(0, 1), Qt.DisplayRole), 'Polygon (1,001)') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'uri 3') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Name), 'layer 3') self.assertEqual( model.data(model.index(0, 0), QgsProviderSublayerModel.Role.Description), None)
def test_proxy(self): """ Test QgsProviderSublayerProxyModel """ model = QgsProviderSublayerModel() proxy = QgsProviderSublayerProxyModel() proxy.setSourceModel(model) self.assertEqual(model.rowCount(QModelIndex()), 0) self.assertEqual(proxy.columnCount(QModelIndex()), 2) self.assertEqual(proxy.headerData(0, Qt.Horizontal, Qt.DisplayRole), 'Item') self.assertEqual(proxy.headerData(0, Qt.Horizontal, Qt.ToolTipRole), 'Item') self.assertEqual(proxy.headerData(1, Qt.Horizontal, Qt.DisplayRole), 'Description') self.assertEqual(proxy.headerData(1, Qt.Horizontal, Qt.ToolTipRole), 'Description') layer1 = QgsProviderSublayerDetails() layer1.setType(QgsMapLayerType.RasterLayer) layer1.setName('layer 1') layer1.setDescription('description 1') layer1.setProviderKey('gdal') layer1.setUri('uri 1') layer2 = QgsProviderSublayerDetails() layer2.setType(QgsMapLayerType.VectorLayer) layer2.setName('another layer 2') layer2.setDescription('description 2') layer2.setProviderKey('ogr') layer2.setUri('uri 2') layer2.setFeatureCount(-1) layer2.setWkbType(QgsWkbTypes.LineString) model.setSublayerDetails([layer1, layer2]) item1 = QgsProviderSublayerModel.NonLayerItem() item1.setUri('item uri 1') item1.setName('item name 1') item1.setType('item type 1') item1.setDescription('item desc 1') model.addNonLayerItem(item1) self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(0, 1), Qt.DisplayRole), 'item desc 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.Uri), 'item uri 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.Name), 'item name 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.Description), 'item desc 1') self.assertEqual( proxy.data(proxy.index(0, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), True) self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'another layer 2') self.assertEqual(proxy.data(proxy.index(1, 1), Qt.DisplayRole), 'description 2 - LineString (Uncounted)') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.ProviderKey), 'ogr') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.VectorLayer) self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.Uri), 'uri 2') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.Name), 'another layer 2') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.Description), 'description 2') self.assertEqual( proxy.data(proxy.index(1, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) self.assertEqual(proxy.data(proxy.index(2, 0), Qt.DisplayRole), 'layer 1') self.assertEqual(proxy.data(proxy.index(2, 1), Qt.DisplayRole), 'description 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.ProviderKey), 'gdal') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.LayerType), QgsMapLayerType.RasterLayer) self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.Uri), 'uri 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.Name), 'layer 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.Description), 'description 1') self.assertEqual( proxy.data(proxy.index(2, 0), QgsProviderSublayerModel.Role.IsNonLayerItem), False) proxy.setFilterString(' 1') self.assertEqual(proxy.rowCount(QModelIndex()), 2) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'layer 1') proxy.setFilterString(' 2') self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'another layer 2') proxy.setFilterString('ITEM') self.assertEqual(proxy.rowCount(QModelIndex()), 1) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') proxy.setFilterString('') self.assertEqual(proxy.rowCount(QModelIndex()), 3) self.assertEqual(proxy.data(proxy.index(0, 0), Qt.DisplayRole), 'item name 1') self.assertEqual(proxy.data(proxy.index(1, 0), Qt.DisplayRole), 'another layer 2') self.assertEqual(proxy.data(proxy.index(2, 0), Qt.DisplayRole), 'layer 1')