Hi Ricardo, <div><br></div><div>what if you use the legendInterface class for getting the loaded layers?</div><div><br></div><div>Try this (in the Python console): </div><div>-------------------------------------------------------</div>
<div><div>from PyQt4.QtCore import SIGNAL</div><div><br></div><div>q = qgis.utils.iface</div><div>l = q.legendInterface()</div><div>def f( layerId ):</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>for i in l.layers():</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>print <a href="http://i.name">i.name</a>(), i.geometryType()</div><div><br></div><div>q.connect( QgsMapLayerRegistry.instance(), SIGNAL( "layerWillBeRemoved(QString)" ), f )</div>
<div>-------------------------------------------------------</div></div><div><br></div><div>I'm not sure it works if the user starts a new project...</div><div><br></div><div>Regards, </div><div><br></div><div>Germán<br>
<br><div class="gmail_quote">On Sat, Sep 4, 2010 at 7:53 AM, Ricardo Filipe Soares Garcia da <span dir="ltr"><<a href="mailto:ricardo.garcia.silva@gmail.com">ricardo.garcia.silva@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi list<br>
<br>
I'm trying to toggle my plugin's availability based on the presence or<br>
absence of loaded vector layers of type 'line'.<br>
For this purpose I have a _get_loaded_layers() and a<br>
toggle_availability() methods (code follows in the end of this<br>
message).<br>
<br>
After searching the online API docs, it seems that the<br>
QgsMapLayerRegistry class is the one that notifies the rest of the<br>
application about layers being added or removed.<br>
So I connect my plugin's toggle_availability() method to the<br>
layerWasAdded() and layerWillBeRemoved() signals of the<br>
QgsMapLayerRegistry.instance() object.<br>
<br>
When the user adds a new layer everything goes fine, my plugin's<br>
availability is toggled based on the presence of vector lines,<br>
including the new layer that was added.<br>
Also, if the user removes just one layer at a time, everything works<br>
as expected as well.<br>
<br>
Unfortunately, when several layers are removed at the same time (for<br>
example, if the user had an open project and decides to create a new<br>
one), my plugin is causing Qgis to segfault (very ugly!).<br>
<br>
It seems to be connected to what is happening with the<br>
QgsmapLayerRegistry instance when the layerWillBeRemoved() signal is<br>
emmited repeatedly. It seems to me that I am crashing Qgis because I<br>
am trying to call QgsMapLayerRegistry.instance().mapLayers() while the<br>
layers are being deleted... and that causes the segfault.<br>
I don't know what happens to the mapLayerRegistry instance, but its<br>
mapLayers() method seems to be the source of the error.<br>
<br>
Reading the pyQgis cookbook, I realized that when layers are removed<br>
from the mapLayerRegistry they are also deleted from Qgis, which seems<br>
a bit inconsistent, compared with the loading procedure. What I mean<br>
is, if layers can be loaded without being automatically added to the<br>
mapLayerRegistry, maybe they could also be removed from the<br>
mapLayerRegistry without being deleted from Qgis (Maybe this is the<br>
reason for having a layerWillBeRemoved() signal instead of a<br>
layerWasRemoved()?).<br>
<br>
Anyway, I don't understand why it works OK if layers are removed one<br>
at a time and not if they all get removed.<br>
I've tried to use the QgsMapLayerRegistry.removedAll() signal to catch<br>
those situations when all layers get removed at once. It does work,<br>
but the problem is that the QgsMapLayerRegistry.layerWillBeRemoved()<br>
is still geting called at the same time and will crash Qgis.<br>
<br>
So, I'd like to request your help in determining the best way to be<br>
notified when a layer is removed from the map canvas.<br>
<br>
Thanks<br>
<br>
#(python code follows)<br>
<br>
<br>
def initGui(self):<br>
self.action = QAction(QIcon(":/plugins/ProfileFromLine/icon.png"), \<br>
"Profile from line", self.iface.mainWindow())<br>
self.toggle_plugin_availability()<br>
QObject.connect(self.action, SIGNAL("activated()"), self.run)<br>
self.iface.addToolBarIcon(self.action)<br>
self.iface.addPluginToMenu("&Profile from line", self.action)<br>
QObject.connect(self.layerRegistry,<br>
SIGNAL("layerWasAdded(QgsMapLayer *)"),<br>
self.toggle_plugin_availability)<br>
QObject.connect(self.layerRegistry,<br>
SIGNAL("layerWillBeRemoved(QString)"),<br>
self.toggle_plugin_availability)<br>
<br>
<br>
def _get_loaded_layers(self):<br>
availableLayers = {"vectorLine" : [], "raster" : []}<br>
loadedLayers = self.layerRegistry.mapLayers()<br>
for layer in loadedLayers.itervalues():<br>
layerType = layer.type()<br>
if layerType == 0: # it's a vector layer<br>
if layer.geometryType() == 1: # it's a line<br>
availableLayers["vectorLine"].append(layer)<br>
elif layerType == 1: #it's a raster layer<br>
availableLayers["raster"].append(layer)<br>
return availableLayers<br>
<br>
def toggle_plugin_availability(self, arg=None):<br>
print("toggle_plugin_availability method called")<br>
numLinesLayers = len(self._get_loaded_layers()["vectorLine"])<br>
if numLinesLayers == 0:<br>
self.action.setEnabled(False)<br>
elif numLinesLayers == 1 and type(arg) == QString:<br>
layerAboutToGo = self.layerRegistry.mapLayer(arg)<br>
if layerAboutToGo.type() == 0:<br>
if layerAboutToGo.geometryType() == 1:<br>
self.action.setEnabled(False)<br>
<font color="#888888"><br>
<br>
<br>
--<br>
___________________________ ___ __<br>
Ricardo Garcia Silva<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>
</font></blockquote></div><br>-- <br>-----------<br> |\__ <br>(:>__)(<br> |/ <br><br>Soluciones Geoinformáticas Libres <br><a href="http://geotux.tuxfamily.org/">http://geotux.tuxfamily.org/</a><br>
</div>