<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi all (specially Martin Dobias)</div><div><br></div><div>I'm working on a plugin that will use embedded widgets in the layer tree. I am using the following as reference:</div><div><br></div><div>-  Martin's PR that brought this feature into existence (thanks for that BTW): <a href="https://github.com/qgis/QGIS/pull/3170/files#diff-3b9be845f2ea37602ca451f18acceab4">https://github.com/qgis/QGIS/pull/3170/files#diff-3b9be845f2ea37602ca451f18acceab4</a></div><div><br></div><div>-  This QGIS dev thread where Martin provides some more detail on how to refresh the legend: <a href="http://osgeo-org.1560.x6.nabble.com/QgsLayerTreeViewMenuProvider-and-default-values-tt5296424.html#a5296695">http://osgeo-org.1560.x6.nabble.com/QgsLayerTreeViewMenuProvider-and-default-values-tt5296424.html#a5296695</a></div><div><br></div><div>-  This stackexchange thread where the transparency slider is discussed: <a href="https://gis.stackexchange.com/questions/232367/showing-transparency-slider-by-default-in-layers-panel-of-qgis">https://gis.stackexchange.com/questions/232367/showing-transparency-slider-by-default-in-layers-panel-of-qgis</a></div><div><br></div><div>-  The master branch of QGIS' repo</div><div><br></div><div>I'm starting out by just showing a simple QLabel in the layer's legend, just as a proof of concept. I want my plugin to work with OGC services layers (WMS, WFS, etc), meaning it shall only try to add widgets to layers that are of these types.<br></div><div><br></div><div>I have roughly the following python code:</div><div><br></div><div>```</div><div>logger = partial(qgis.utils.QgsMessageLog.logMessage, tag=__name__)<br><br></div><div>class MyWidgetProvider(<br>        qgis.gui.QgsLayerTreeEmbeddedWidgetProvider):<br><br>    SUPPORTED_DATA_PROVIDERS = [<br>        "wms",<br>    ]<br><br>    def __init__(self, *args, **kwargs):<br>        logger("Instantiating provider...")<br>        super().__init__(*args, **kwargs)<br>        self.creation_timestamp = int(time.time())<br><br>    def id(self):<br>        logger("id called")<br>        return "{}_{}".format(self.__class__.__name__, self.creation_timestamp)<br><br>    def name(self):<br>        logger("name called")<br>        return "My Widgetss"<br><br>    def createWidget(self, map_layer, widget_index):<br>        logger("createWidget called")<br>        widget = widgets.QLabel("hi world!")<br>        widget.setAutoFillBackground(True)<br>        return widget<br><br>    def supportsLayer(self, map_layer):<br>        logger("supportsLayer called")<br>        provider = map_layer.dataProvider()<br>        name = <a href="http://provider.name">provider.name</a>()<br>        result = True if name in self.SUPPORTED_DATA_PROVIDERS else False<br>        logger("supportsLayer: {}".format(result), level=qgis.core.Qgis.Debug)<br>        return result</div><div>```<br></div><div><br></div><div><br></div><div>This code kind of works, but I'm facing two problems at the moment:</div><div><br></div><div>1. The `MyWidgetProvider.supportsLayer()` method is never called. The logging call does not show up on the console and I also see that my provider is offered on WMS layers, but also on some local vector layers (which is not what I intend). grep'ping around the QGIS' source code I could not find some place where this method would be called and used, so I'm wondering if this is implemented. I would expect this method to be called when the layer properties dialog is opened, so that my provider would get filtered out for layers that were of the wrong type;<br></div><div><br></div><div>2. This code does not update the legend for the layer, UNLESS it is a vector layer. If I try to add this provider to a WMS layer, the legend is not updated. If I add it to a shapefile, it works as expected. In order to get my QLabel to show up on WMS layers, I have to manually run this in the QGIS python shell:<br></div><div><br></div><div>```<br></div><div>view = iface.layerTreeView()</div><div>view.model().refreshLayerLegend(view.currentNode())<br></div><div>```<br></div><div><br></div><div>However, I am not seeing where in my plugin's code can I add this snippet. I only want the layer tree to be updated AFTER my provider creates the widget, but this moment seems to be out of my control. Also, I am puzzled by the fact that vector layers work just fine while my raster (WMS) does not.</div><div><br></div><div><br></div><div>Thanks in advance for your help :)</div><div><br></div><div><br></div><div>Best regards<br></div><br><div>-- <br><div dir="ltr" class="gmail_signature">___________________________ ___ __<br>Ricardo Garcia Silva</div></div></div></div></div></div></div></div>