Hi massimo<br><br>I completely agree, and would like to avoid the "patch to a fragile system" you mentionned. <br><br>Having the QgsAppLegendItem with properties and methods like you described it the right way to go IMHO.<br>
The children property will help to re-build the hierarchical tree (instead of using my algo based on the flat object returned by groupLayerRelationship, a bit hard to read and understand.). <br><br>So + 1 for me :)<br><br>
Michael<br><br><div class="gmail_quote">2012/10/4 Massimo <span dir="ltr"><<a href="mailto:massimo.endrighi@geopartner.it" target="_blank">massimo.endrighi@geopartner.it</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Michael,<br>
<br>
the group Id could be the groupIndex. For example:<br>
GroupA                               [groupIndex=0, itemIndex = 0]<br>
        Layer1                        [groupIndex=0, itemIndex = 1]<br>
        GroupB                       [groupIndex=1, itemIndex = 2]<br>
                GroupC               [groupIndex=2, itemIndex = 3]<br>
                        Layer2       [groupIndex=2, itemIndex = 4]<br>
        GroupA                       [groupIndex=3, itemIndex = 5]<br>
GroupB                               [groupIndex=4, itemIndex = 6]<br>
        GroupC                       [groupIndex=5, itemIndex = 7]<br>
                Layer3                [groupIndex=5, itemIndex = 8]<br>
<br>
In this way you obtain:<br>
- the groupIndex correspond to legend.layers() list<br>
['0.GroupA','1.GroupB','2.GroupC','3.GroupA','4.GroupB','5.GroupC']<br>
- you can use the groupLayerRelationship to reconstruct the exact hierarchy<br>
in the same way you are doing now<br>
<br>
But, from my point of view, this is like a patch to a fragile system.<br>
It would be much more useful to expose a property to access the legend items<br>
in terms of QgsAppLegendItem.<br>
<br>
The QgsAppLegendItem should be a sort of QgsLegendItem<br>
(src\app\legend\qgslegenditem.h) that expose the following<br>
 - type (layer or group)<br>
 - parent (QgsAppLegendItem)<br>
 - children (list of QgsAppLegendItem)<br>
 - expanded<br>
 - visible<br>
<br>
Once you have an item you can do all your business logic.<br>
<br>
Please let me know your opinion.<br>
<br>
Regard<br>
Massimo<br>
<br>
<br>
kimaidou wrote<br>
<div class="im">> Hi massimo,<br>
><br>
> You are right, we must add a id for each group in Qgis. For now, the group<br>
> is defined only with its name, which leads to this kind of behaviour. At<br>
> the contrary, layers do have an id in Qgis, so you can use the same name<br>
> for 2 different layers.<br>
><br>
> I think we must enhance a bit the way how groups are handled and provide<br>
> appropriate public methods like<br>
> group->id(), ĝetGroup(id), etc.<br>
><br>
> In the lizmap example, I could only use  QgsLegendInterface::groups  wich<br>
> returns a string list of groups, and groupLayerRelationship which provide<br>
> a<br>
> flat list of groups and layers. The latter returns ids of layers, but name<br>
> of groups, so there is no easy way to distinguish to groups with the same<br>
> name.<br>
><br>
> Michael<br>
><br>
</div>> 2012/10/4 Massimo &lt;<br>
<br>
> massimo.endrighi@<br>
<br>
> &gt;<br>
<div class="im">><br>
>> Michael,<br>
>><br>
>> thank you for the tip but your code fails under some circumstances. See<br>
>> attached image.<br>
</div>>> &lt;<a href="http://osgeo-org.1560.n6.nabble.com/file/n5006430/lizmap_error.png&gt" target="_blank">http://osgeo-org.1560.n6.nabble.com/file/n5006430/lizmap_error.png&gt</a>;<br>
<div class="im">>><br>
>> If you think that you can fix it I will be happy to adopt your algorithm,<br>
>> otherwise we can find a solution adding something to QGis API.<br>
>><br>
>> Regards<br>
>> Massimo<br>
>><br>
>><br>
>> kimaidou wrote<br>
>> > Hi all,<br>
>> ><br>
>> > In Lizmap plugin, I need to display the layer and group tree. You can<br>
>> find<br>
>> > some code here :<br>
>> > <a href="https://bitbucket.org/mdouchin/lizmapplugin/src/9cdfa856b8f0/lizmap.py" target="_blank">https://bitbucket.org/mdouchin/lizmapplugin/src/9cdfa856b8f0/lizmap.py</a><br>
>> > Line 451 : populateLayerTree<br>
>> ><br>
>> > There is some specific lizmap code in there, but you can probably use<br>
>> some<br>
>> > of it.<br>
>> ><br>
>> > Michael<br>
>><br>
>><br>
>><br>
>><br>
>><br>
>> --<br>
>> View this message in context:<br>
>> <a href="http://osgeo-org.1560.n6.nabble.com/index-in-self-legend-groupLayerRelationship-different-behavior-between-QGIS-1-8-and-master-tp5004337p5006430.html" target="_blank">http://osgeo-org.1560.n6.nabble.com/index-in-self-legend-groupLayerRelationship-different-behavior-between-QGIS-1-8-and-master-tp5004337p5006430.html</a><br>

>> Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.<br>
>> _______________________________________________<br>
>> Qgis-developer mailing list<br>
>><br>
<br>
</div>> Qgis-developer@.osgeo<br>
<div class="im"><br>
>> <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
>><br>
><br>
> _______________________________________________<br>
> Qgis-developer mailing list<br>
<br>
</div>> Qgis-developer@.osgeo<br>
<br>
> <a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://osgeo-org.1560.n6.nabble.com/index-in-self-legend-groupLayerRelationship-different-behavior-between-QGIS-1-8-and-master-tp5004337p5006467.html" target="_blank">http://osgeo-org.1560.n6.nabble.com/index-in-self-legend-groupLayerRelationship-different-behavior-between-QGIS-1-8-and-master-tp5004337p5006467.html</a><br>

<div class="HOEnZb"><div class="h5">Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.<br>
_______________________________________________<br>
Qgis-developer mailing list<br>
<a href="mailto:Qgis-developer@lists.osgeo.org">Qgis-developer@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/qgis-developer" target="_blank">http://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</div></div></blockquote></div><br>