[Qgis-developer] Composer Legend API

Akbar Gumbira akbargumbira at gmail.com
Wed Nov 12 02:44:19 PST 2014


Hi All,

QGIS 2.6 has surely a lot of improvement on composer, but I find myself
stuck on a simple problem on composer legend manipulation. Instead of
showing from all the layers, I want to just show a specific layer. This is
a simple code that you can run in QGIS Python Console:

from qgis.core import QgsComposition, QgsLayerTreeGroup
> from PyQt4 import QtXml, QtCore
>
> canvas = iface.mapCanvas()
> renderer = canvas.mapRenderer()
> composition = QgsComposition(renderer)
>
> composer = iface.createNewComposer()
> composer.setComposition(composition)
>
> template_file = QtCore.QFile('/tmp/inasafe-portrait-a4.qpt')
> template_file.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
> template_content = template_file.readAll()
> template_file.close()
>
> document = QtXml.QDomDocument()
> document.setContent(template_content)
>
> status_load = composition.loadFromTemplate(document)
>
> legend = composition.getComposerItemById('impact-legend')
> legend.setTitle('Hoho')
>
> group = QgsLayerTreeGroup()
> group.addLayer(iface.activeLayer())
>
> # 1 Somehow crashes QGIS in inasafe, but not in qgis console
> #legend.modelV2().setRootGroup(group)
> #legend.synchronizeWithModel()
>
> # 2 No effect on legend, sigh
> #legend.model().setLayerSetAndGroups(group)
> #legend.synchronizeWithModel()
>
> # 3 No effect on legend, sigh. Using QgsLegendModel basically obsolete in
> 2.6, not deprecated
> #legend.model().setLayerSet(iface.activeLayer().id())
> #legend.synchronizeWithModel()
>
> # 4 Unfortunately setCustomLayerTree is private method
> #legend.setCustomLayerTree(group)
> #legend.synchronizeWithModel()
> #
>
> # 5 legendmodel linked directly to layer registry in map canvas, so this
> will delete layer in canvas
> #legend.updateLegend()
> #model = legend.modelV2()
> #for r in range(0, model.rowCount()):
> #    for c in range(0, model.columnCount()):
> #        if model.index(r, c).data() != iface.activeLayer().name():
> #            model.removeRows(r, 1)
> #legend.synchronizeWithModel()
>
> composition.exportAsPDF('/tmp/result.pdf')
>

I have tried several possibilities to do that by going through the c++ code
itself. The 1st possibility above is working fine actually in python
console, but somehow it crashes QGIS implementing it in the plugin. The 2nd
and 3rd are giving no effect (It says the QgsLegendModel deprecated, but if
it's not working, shouldn't it say obsolete?). The method
setCustomlayerTree in the 4th unfortunately not available in the SIP. The
5th will delete layer in map canvas.

>From those 5 possibilities, is there any other way to do this simple task?

Regards
-- 

*-------------------*

*Akbar Gumbira*
*Software Engineer*
*Geospatial, NLP, Data Mining, Machine Learning, Artificial Intelligence*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20141112/31786dad/attachment-0001.html>


More information about the Qgis-developer mailing list