[Qgis-developer] QgsLegendInterface

Martin Dobias wonder.sk at gmail.com
Mon Mar 7 17:48:58 EST 2011


Hi Marco

On Thu, Mar 3, 2011 at 4:27 PM, Marco Bernasocchi
<marco at bernawebdesign.ch> wrote:
> QgsLegendInterface::groupLayerRelationship() returns this:
> [group0, group1, subGroupOfGroup0]
> BUT
> QgsLegendInterface::groups() returns:
> [group0, subGroupOfGroup0, group1]
>
> notice the difference in the order, is this done on purpose? or a bug? I
> spent couple of ours digging for the problem in my code today and it
> turned out to be because I was using the indexes from groups().

The legend interface has been evolving a bit ad-hoc. I believe first
there was just groups() function, then groupLayerRelationship() has
been added for some other purpose and probably after all that support
for sub-groups has been added. So the result is not a bug, they just a
traverse the tree in a different way.


> as well, if you want to have all the groups name, now you have to
> iterate on groupLayerRelationship() like this:
>
> groups = []
> for group in self.legend.groupLayerRelationship():
>                    groups.append(group[0])
>
> and then groups is an array with all the group names (like groups()) but
> in the same order as the groupLayerRelationship().
>
> I think that it would be sensible to have a method like groups() that
> returns the same as my iteration above. what do you think? I would of
> course do it if it is needed.

I have no strong opinion on that.

But I think that it would be much better to implement a method that
would return the complete tree.
For example, there would be a class "legend node". A node could be
either a layer or a group. Layer node would reference layer (or its
ID) and group node would contain a list of all children (again layers
and/or groups). Legend interface would then just return a list of
top-level nodes. Nothing complicated, simple for traversal and no need
for extra methods.

Regards
Martin


More information about the Qgis-developer mailing list