<div dir="ltr"><div>Hello all,</div><div><br></div><div>I'm updating the <a href="http://plugins.qgis.org/plugins/splitmultipart/">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>