[Qgis-developer] Reliable way to determine a groupIndex in the legend tree
Bernhard Ströbl
bernhard.stroebl at jena.de
Tue Oct 8 22:40:46 PDT 2013
Hi Andreas,
Am 08.10.2013 16:08, schrieb Andreas Neumann:
> Hi,
>
> Thank you Bernhard. This works better than my code with the
> legend.groupLayerRelationship()
>
> We should add this to the Python Cookbook
> (http://www.qgis.org/en/docs/pyqgis_developer_cookbook/) - Do you mind?
You're welcome
> Or should I do a pull request?
I have no idea how to contribute to the cookbook. So please go ahead.
>
> Do you have a clever strategy to make this same code available in
> multiple QGIS plugins?
All plugins that I create for my users are subclassing a class that
provides this kind of functionality for all of them.
>
> But really this should go into the core QGIS API. It is annoying taking
> care of such things.
+1
Bernhard
>
> Andreas
>
> Am 08.10.2013 13:32, schrieb Bernhard Ströbl:
>> Hi Andreas,
>>
>> this is how I do it:
>>
>> def getGroupIndex(self, groupName):
>> '''Find the index for group groupName'''
>> retValue = -1
>> groups = self.iface.legendInterface().groups()
>>
>> for i in range(len(groups)):
>> if groups[i] == groupName:
>> retValue = i
>> break
>>
>> return retValue
>>
>> def moveToGroup(self, layer, groupName):
>> '''move layer into a group'''
>>
>> grpIdx = self.getGroupIndex(groupName)
>>
>> if grpIdx == -1:
>> # add the group
>> grpIdx = self.iface.legendInterface().addGroup(groupName)
>>
>> self.iface.legendInterface().moveLayer(layer, grpIdx)
>>
>> Bernhard
>>
>> Am 08.10.2013 15:17, schrieb Andreas Neumann:
>>> Hi,
>>>
>>> In my python script I would like to append a filtered Postgis layer into
>>> a group with a given name.
>>>
>>> This works fine as long as the user did not activate a group already
>>> interactively. If a group is already active, my method to find the
>>> groupIndex fails. If not group is active, the script works fine.
>>>
>>> Here is my method:
>>>
>>> ---------------
>>> #read the legend tree to find the index of the group defined in groupName
>>> groups = []
>>> for group in self.legend.groupLayerRelationship():
>>> groups.append(group[0])
>>> groupIndex = groups.index(groupName)
>>> if QGis.QGIS_VERSION[0:3]>= "1.9":
>>> groupIndex -= 1
>>>
>>> #move new layer to existing group
>>> self.legend.moveLayer(schulkreisLayer, groupIndex)
>>> --------------
>>>
>>> Is there a more reliable way to find the groupIndex if the user selected
>>> a group already?
>>>
>>> Maybe there is some interference with the setting "New layers should be
>>> added to chosen or current group" setting in "Settings" --> "Options"
>>> --> "Map& Legend" --> "Layerlegend"?
>>>
>>> Thanks,
>>> Andreas
>>> _______________________________________________
>>> Qgis-developer mailing list
>>> Qgis-developer at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>>>
>>
>>
>
__________ Information from ESET Mail Security, version of virus signature database 8893 (20131008) __________
The message was checked by ESET Mail Security.
http://www.eset.com
More information about the Qgis-developer
mailing list