def __init__(self, parent, name, path, lyr_object=None, layer_path=''): super().__init__(QgsDataItem.Custom, parent, name, path) self.setCapabilities(QgsDataItem.Fertile | QgsDataItem.Collapse) self.layer_path = layer_path or path self.object = lyr_object if path: self.setIcon(GuiUtils.get_icon('icon.svg')) elif isinstance(self.object, (GroupLayer, BaseMapLayer)): self.setIcon(QgsDataCollectionItem.iconDir()) elif self.object.__class__.__name__ in ('WmsMapLayer', 'RasterLayer'): self.setIcon(QgsLayerItem.iconRaster()) else: wkb_type = VectorLayerConverter.layer_to_wkb_type(self.object) geometry_type = QgsWkbTypes.geometryType(wkb_type) if geometry_type == QgsWkbTypes.PointGeometry: self.setIcon(QgsLayerItem.iconPoint()) elif geometry_type == QgsWkbTypes.LineGeometry: self.setIcon(QgsLayerItem.iconLine()) elif geometry_type == QgsWkbTypes.PolygonGeometry: self.setIcon(QgsLayerItem.iconPolygon()) else: self.setIcon(QgsLayerItem.iconDefault()) if self.object: if isinstance(self.object, (GroupLayer, BaseMapLayer)): self.add_children() self.setState(QgsDataItem.Populated) self.setToolTip( QDir.toNativeSeparators(path) if not lyr_object else lyr_object. name) self.child_items = []
def __init__(self, id, name, datatype, source, ressource): # unfortunately there is no option to retrieve the layer geometry (i. e. # point/line/polygon for vectorlayers) just from the json object. # Therefore they get a default polygon icon which will be updated as soon as # layer is added as WFS to QGIS - see def addQgsLayer() if datatype == 'Raster': self.icon = QgsLayerItem.iconRaster() elif datatype == 'Vector': self.icon = QgsLayerItem.iconPolygon() else: self.icon = QgsLayerItem.iconDefault() TreeItem.__init__(self, self.icon, name) self.id = id self.actiontype = 'layer' self.ressource = ressource self.dlg = None self.name = name self.source = source self.datatype = datatype self.qgisLayers = [] self.settings = { 'name': '', 'appearance': { 'hoverTemplate': None, 'opacity': 1 } } if PYTHON_VERSION >= 3: QgsProject.instance().layerRemoved.connect(self.updateLayerList) else: QgsMapLayerRegistry.instance().layerRemoved.connect( self.updateLayerList)
def icon_for_geometry_type(geometry_type: str) -> QIcon(): """ Return the correct icon according to the geometry type. """ if geometry_type == NULL: return QgsLayerItem.iconTable() elif geometry_type == 'POINT': return QgsLayerItem.iconPoint() elif geometry_type == 'LINESTRING': return QgsLayerItem.iconLine() elif geometry_type == 'MULTIPOLYGON': return QgsLayerItem.iconPolygon() # Default icon return QIcon(resources_path('icons', 'icon.png'))
def icon_per_geometry_type(geometry_type: str): """Return the icon for a geometry type. If not found, it will return the default icon. :param geometry_type: The geometry as a string. :type geometry_type: basestring :return: The icon. :rtype: QIcon """ geometry_type = geometry_type.lower() if geometry_type == "raster": return QgsLayerItem.iconRaster() elif geometry_type == "mesh": return QgsLayerItem.iconMesh() elif geometry_type == "point": return QgsLayerItem.iconPoint() elif geometry_type == "line": return QgsLayerItem.iconLine() elif geometry_type == "polygon": return QgsLayerItem.iconPolygon() elif geometry_type == "no geometry": return QgsLayerItem.iconTable() else: return QgsLayerItem.iconDefault()
def layer_icon(layer): """Helper to get the layer icon. :param layer: A layer. :type layer: QgsMapLayer :returns: The icon for the given layer. :rtype: QIcon """ if is_raster_layer(layer): return QgsLayerItem.iconRaster() elif is_point_layer(layer): return QgsLayerItem.iconPoint() elif is_line_layer(layer): return QgsLayerItem.iconLine() elif is_polygon_layer(layer): return QgsLayerItem.iconPolygon() else: return QgsLayerItem.iconDefault()
def layer_icon(layer): """Helper to get the layer icon. :param layer: A layer. :type layer: QgsMapLayer :returns: The icon for the given layer. :rtype: QIcon """ if is_raster_layer(layer): return QgsLayerItem.iconRaster() elif is_point_layer(layer): return QgsLayerItem.iconPoint() elif is_line_layer(layer): return QgsLayerItem.iconLine() elif is_polygon_layer(layer): return QgsLayerItem.iconPolygon() else: return QgsLayerItem.iconDefault()
def __init__(self, parent, name, path, object=None, layer_path=''): super().__init__(QgsDataItem.Custom, parent, name, path) self.setCapabilities(QgsDataItem.Fertile | QgsDataItem.Collapse) self.layer_path = layer_path or path self.object = object if path: self.setIcon(GuiUtils.get_icon('icon.svg')) else: self.setIcon(QgsLayerItem.iconDefault()) if self.object: self.setState(QgsDataItem.Populated) self.setToolTip(QDir.toNativeSeparators(path) if not object else object.name) self.child_items = []
def addQgsLayer(self, iface): currentCrs = iface.mapCanvas().mapSettings().destinationCrs() if currentCrs.isValid(): epsg = currentCrs.authid() else: epsg = 'EPSG:3857' # layerutils layer = createLayer(self, epsg) if not layer: return qgisLayerItem = QgisLayerItem(layer, self, self.ressource) self.qgisLayers.append(qgisLayerItem) self.addChild(qgisLayerItem) self.setExpanded(True) #for an odd reason there appears a warning below the layer if it is a #wms layer from shogun - workaround to hide this: if layer.dataProvider().name( ) == 'wms' and self.source['url'].startswith('/shogun2-webapp/'): root = QgsProject.instance().layerTreeRoot() layerNode = root.findLayer(layer.id()) layerNode.setExpanded(False) # when the layer has loaded for the first time and it's icon # is still on iconDefault, update the icon with the correct geometry if not self.datatype == 'Raster': if layer.type() == QgsMapLayer.VectorLayer: geom = layer.geometryType() if geom == QGis.Point: self.icon = QgsLayerItem.iconPoint() elif geom == QGis.Line: self.icon = QgsLayerItem.iconLine() elif geom == QGis.Polygon: self.icon = QgsLayerItem.iconPolygon() else: self.icon = QgsLayerItem.iconRaster() self.setIcon(0, self.icon)
def createChildren(self): children = [] # Add a Python object as child pyQgsLayerItem = PyQgsLayerItem(None, "name", "", "uri", QgsLayerItem.Vector, "my_provider") pyQgsLayerItem.tabSetDestroyedFlag = self.tabSetDestroyedFlag children.append(pyQgsLayerItem) # Add a C++ object as child children.append( QgsLayerItem(None, "name2", "", "uri", QgsLayerItem.Vector, "my_provider")) return children