<div dir="ltr">This is not going to solve you problem yet but this is a quick refactor to use the new style signal slot syntax:<div><br></div><div><div>def initGui(self):</div><div> # Create action that will start plugin configuration</div>
<div> self.action = QAction(</div><div> QIcon(":/plugins/splitmultipart/icon.svg"),</div><div> QCoreApplication.translate('Multipart split', u"Split Selected Multipart features"), self.iface.mainWindow())</div>
<div> self.action.setEnabled(False)</div><div> </div><div> # connect to signals for button behavior</div><div> self.action.triggered.connect(self.run)</div><div> self.iface.currentLayerChanged.connect(self.toggle)</div>
<div><br></div><div> # Add toolbar button and menu item</div><div> self.iface.advancedDigitizeToolBar().addAction(self.action)</div><div> self.iface.editMenu().addAction(self.action)</div><div> </div>
<div> def toggle(self, layer):</div><div> mc = self.canvas</div><div> layer = mc.currentLayer()</div><div> </div><div> # Decide whether the plugin button is enable or disable</div><div> if layer:</div>
<div> # set enable</div><div> if layer.isEditable():</div><div> layer.editingStopped.connect(self.toggle)</div><div> layer.editingStarted.disconnect(self.toggle)</div><div>
layer.selectionChanged.connect(self.toggle)</div><div><br></div><div> self.iface.messageBar().pushMessage("Debug"," Connect SIGNAL(selectionChanged()",0)</div><div> if layer.selectedFeatureCount() > 0:</div>
<div> self.action.setEnabled(True)</div><div> else:</div><div> self.action.setEnabled(False) </div><div> # set disable </div><div> else:</div>
<div>
self.action.setEnabled(False)</div><div> layer.editingStarted.connect(self.toggle)</div><div> layer.editingStopped.disconnect(self.toggle)</div><div> layer.selectionChanged.disconnect(self.toggle)</div>
<div> self.iface.messageBar().pushMessage("Debug"," Disconnect SIGNAL(selectionChanged()",0)</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 22, 2013 at 10:11 PM, Alexandre Neto <span dir="ltr"><<a href="mailto:senhor.neto@gmail.com" target="_blank">senhor.neto@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hello all,</div><div><br></div><div>I'm updating the <a href="http://plugins.qgis.org/plugins/splitmultipart/" target="_blank">Multipart split plugin</a> to make it work with the 2.0 API.</div>
<div><br></div><div>Has I have successfully updated the plugin to make it work in 2.0, I would like to improve it's button behavior to work like the rest of the advanced editing toolbar. I am able to enable and disable it if the current layer is editable. </div>
<div><br></div><div>Now I'm trying to get the same behavior if there are selected features. I have made it work, but QGIS starts to get really slow... I found out that's because lots of connections are being created to SIGNAL("selectionChanged()").</div>
<div><br></div><div>Can someone please, point me in the right direction, or tell me where can I find some code example doing this?</div><div><br></div><div>Thank you very much,</div><div><br></div><div>Alexandre Neto</div>
<div><br></div><div>Part of the code bellow:</div><div><br></div><div><div>def initGui(self):</div><div> # Create action that will start plugin configuration</div><div> self.action = QAction(</div><div> QIcon(":/plugins/splitmultipart/icon.svg"),</div>
<div> QCoreApplication.translate('Multipart split', u"Split Selected Multipart features"), self.iface.mainWindow())</div><div> self.action.setEnabled(False)</div><div> </div><div>
# connect to signals for button behavior</div><div> QObject.connect(self.action, SIGNAL("triggered()"), self.run)</div><div> QObject.connect(self.iface, SIGNAL("currentLayerChanged(QgsMapLayer*)"), self.toggle)</div>
<div> #QObject.connect(self.iface, SIGNAL("selectionChanged(QgsMapLayer *)"), self.toggle)</div><div><br></div><div> # Add toolbar button and menu item</div><div> self.iface.advancedDigitizeToolBar().addAction(self.action)</div>
<div> self.iface.editMenu().addAction(self.action)</div><div> </div><div> def toggle(self):</div><div> mc = self.canvas</div><div> layer = mc.currentLayer()</div><div> </div><div> # Decide whether the plugin button is enable or disable</div>
<div> if layer <> None:</div><div> # set enable</div><div> if layer.isEditable():</div><div> QObject.connect(layer,SIGNAL("editingStopped()"),self.toggle)</div>
<div>
QObject.connect(layer, SIGNAL("selectionChanged()"), self.toggle)</div><div> QObject.disconnect(layer,SIGNAL("editingStarted()"),self.toggle)</div><div> self.iface.messageBar().pushMessage("Debug"," Connect SIGNAL(selectionChanged()",0)</div>
<div> if layer.selectedFeatureCount() > 0:</div><div> self.action.setEnabled(True)</div><div> else:</div><div> self.action.setEnabled(False)</div><div>
</div><div> # set disable </div><div> else:</div><div> self.action.setEnabled(False)</div><div> QObject.connect(layer,SIGNAL("editingStarted()"),self.toggle)</div>
<div> QObject.disconnect(layer,SIGNAL("editingStopped()"),self.toggle)</div><div> QObject.disconnect(layer, SIGNAL("selectionChanged()"), self.toggle)</div><div> self.iface.messageBar().pushMessage("Debug"," Disconnect SIGNAL(selectionChanged()",0)</div>
</div></div>
<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>
<br></blockquote></div><br></div>