[Qgis-developer] Need help with Plugin button behavior connected to SIGNAL("selectionChanged()")

Nathan Woodrow madmanwoo at gmail.com
Thu Aug 22 05:29:50 PDT 2013


This is not going to solve you problem yet but this is a quick refactor to
use the new style signal slot syntax:

def initGui(self):
        # Create action that will start plugin configuration
        self.action = QAction(
            QIcon(":/plugins/splitmultipart/icon.svg"),
            QCoreApplication.translate('Multipart split', u"Split Selected
Multipart features"), self.iface.mainWindow())
        self.action.setEnabled(False)

        # connect to signals for button behavior
        self.action.triggered.connect(self.run)
        self.iface.currentLayerChanged.connect(self.toggle)

        # Add toolbar button and menu item
        self.iface.advancedDigitizeToolBar().addAction(self.action)
        self.iface.editMenu().addAction(self.action)

    def toggle(self, layer):
        mc = self.canvas
        layer = mc.currentLayer()

        # Decide whether the plugin button is enable or disable
        if layer:
            # set enable
            if layer.isEditable():
                layer.editingStopped.connect(self.toggle)
                layer.editingStarted.disconnect(self.toggle)
                layer.selectionChanged.connect(self.toggle)

                self.iface.messageBar().pushMessage("Debug"," Connect
SIGNAL(selectionChanged()",0)
                if layer.selectedFeatureCount() > 0:
                    self.action.setEnabled(True)
                else:
                    self.action.setEnabled(False)
            # set disable
            else:
                self.action.setEnabled(False)
                layer.editingStarted.connect(self.toggle)
                layer.editingStopped.disconnect(self.toggle)
                layer.selectionChanged.disconnect(self.toggle)
                self.iface.messageBar().pushMessage("Debug"," Disconnect
SIGNAL(selectionChanged()",0)


On Thu, Aug 22, 2013 at 10:11 PM, Alexandre Neto <senhor.neto at gmail.com>wrote:

> Hello all,
>
> I'm updating the Multipart split plugin<http://plugins.qgis.org/plugins/splitmultipart/> to
> make it work with the 2.0 API.
>
> 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.
>
> 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()").
>
> Can someone please, point me in the right direction, or tell me where can
> I find some code example doing this?
>
> Thank you very much,
>
> Alexandre Neto
>
> Part of the code bellow:
>
> def initGui(self):
>         # Create action that will start plugin configuration
>         self.action = QAction(
>             QIcon(":/plugins/splitmultipart/icon.svg"),
>             QCoreApplication.translate('Multipart split', u"Split Selected
> Multipart features"), self.iface.mainWindow())
>         self.action.setEnabled(False)
>
>         # connect to signals for button behavior
>         QObject.connect(self.action, SIGNAL("triggered()"), self.run)
>         QObject.connect(self.iface,
> SIGNAL("currentLayerChanged(QgsMapLayer*)"), self.toggle)
>         #QObject.connect(self.iface, SIGNAL("selectionChanged(QgsMapLayer
> *)"), self.toggle)
>
>         # Add toolbar button and menu item
>         self.iface.advancedDigitizeToolBar().addAction(self.action)
>         self.iface.editMenu().addAction(self.action)
>
>     def toggle(self):
>         mc = self.canvas
>         layer = mc.currentLayer()
>
>         # Decide whether the plugin button is enable or disable
>         if layer <> None:
>             # set enable
>             if layer.isEditable():
>
> QObject.connect(layer,SIGNAL("editingStopped()"),self.toggle)
>                  QObject.connect(layer, SIGNAL("selectionChanged()"),
> self.toggle)
>
> QObject.disconnect(layer,SIGNAL("editingStarted()"),self.toggle)
>                 self.iface.messageBar().pushMessage("Debug"," Connect
> SIGNAL(selectionChanged()",0)
>                 if layer.selectedFeatureCount() > 0:
>                     self.action.setEnabled(True)
>                 else:
>                     self.action.setEnabled(False)
>
>             # set disable
>             else:
>                 self.action.setEnabled(False)
>
> QObject.connect(layer,SIGNAL("editingStarted()"),self.toggle)
>
> QObject.disconnect(layer,SIGNAL("editingStopped()"),self.toggle)
>                 QObject.disconnect(layer, SIGNAL("selectionChanged()"),
> self.toggle)
>                 self.iface.messageBar().pushMessage("Debug"," Disconnect
> SIGNAL(selectionChanged()",0)
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130822/64ae7c45/attachment.html>


More information about the Qgis-developer mailing list