[QGIS-Developer] QgsProcessingLayerPostProcessorInterface and Layers Legend Icon
C Hamilton
adenaculture at gmail.com
Tue Oct 4 05:15:14 PDT 2022
I have several plugin functions that are
using QgsProcessingLayerPostProcessorInterface to style the layer after it
is created in a processing algorithm. I have largely ignored the fact that
after I style it, the legend displayed in the layers panel is its old style
and not the new one. I have a solution, but don't know if I am violating
some thread safe rule. I know I cannot use iface in the algorithm, but is
it safe to use in QgsProcessingLayerPostProcessorInterface?
In the top of my algorithm I import the following:
from qgis.utils import iface
In the processAlgorithm function I have the following.
if context.willLoadLayerOnCompletion(dest_id):
context.layerToLoadOnCompletionDetails(dest_id).setPostProcessor(StylePostProcessor.create(settings.lineColor,
settings.fontColor))
return {self.PrmOutput: dest_id}
My QgsProcessingLayerPostProcessorInterface looks like this. Notice the
line iface.layerTreeView().refreshLayerSymbology(layer.id()) which
effectively does what I need it to do. It works, but is safe or is there a
better way to do this? The output layer is a polygon in which I only want
the outline displayed. I am passing some color parameters from
processAlgorithm.
class StylePostProcessor(QgsProcessingLayerPostProcessorInterface):
instance = None
line_color = None
font_color = None
def __init__(self, line_color, font_color):
self.line_color = line_color
self.font_color = font_color
super().__init__()
def postProcessLayer(self, layer, context, feedback):
if not isinstance(layer, QgsVectorLayer):
return
sym = layer.renderer().symbol().symbolLayer(0)
sym.setBrushStyle(Qt.NoBrush)
sym.setStrokeColor(self.line_color)
* iface.layerTreeView().refreshLayerSymbology(layer.id
<http://layer.id>())*
# Hack to work around sip bug!
@staticmethod
def create(line_color, font_color) -> 'StylePostProcessor':
"""
Returns a new instance of the post processor, keeping a reference
to the sip
wrapper so that sip doesn't get confused with the Python subclass
and call
the base wrapper implementation instead... ahhh sip, you wonderful
piece of sip
"""
StylePostProcessor.instance = StylePostProcessor(line_color,
font_color)
return StylePostProcessor.instance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20221004/e4556e5f/attachment.htm>
More information about the QGIS-Developer
mailing list