[Qgis-user] Undo\Redo edits using Python

Alexandre Neto senhor.neto at gmail.com
Tue Apr 2 09:10:54 PDT 2013


In a local python plugin I'm trying to allow Undo\Redo edits. For that I
set a *beginEditCommand()*, *endEditCommand() and destroyEditCommand().*
*
*
But I'm not being able to make it work. If you press undo, all features
added by the plugin are still there. And worst, If I stop editing and
discard changes, features disappear from attribute table, but as I start
edit again they reappear.

Please note that "l_acao" layer is a non-spatial table, and l_uni_ges is a
polygon vector layer.

I you will, please see my code below.

Thanks,

Alexandre Neto

----

def run(self):
    # Define "acções" and "unidades de gestão" Layers from project
    l_acao =
QgsMapLayerRegistry.instance().mapLayer(u'acoes20130312143950563')
    l_uni_ges =
QgsMapLayerRegistry.instance().mapLayer(u'unidadesdegestao20130312143304288')

    # Get layer default values from provider
    # (this avoids problems with unique keys)
    provider = l_acao.dataProvider()
    temp_feature = QgsFeature()
    attributes = {}

    for j in l_acao.pendingAllAttributesList():
        if not provider.defaultValue(j).isNull():
            attributes[j] = provider.defaultValue(j)
        else:
            attributes[j] = None

    temp_feature.setAttributeMap(attributes)

   * # start edit command to allow undo\redo*
*    l_acao.beginEditCommand("Add new actions")*

    # open feature form and waits for edits
    if self.iface.openFeatureForm(l_acao, temp_feature):
        new_attributes = temp_feature.attributeMap()

        # replicate "acção" record for each
        # of the selected "Unidades de gestão"
        uniges_ids = [feature.attributeMap()[1] for feature in
l_uni_ges.selectedFeatures()]

        for uniges_id in uniges_ids:
            new_attributes[1] = uniges_id
            temp_feature.setAttributeMap(new_attributes)
            new_feature = QgsFeature(temp_feature)
            l_acao.addFeature( new_feature )

        *l_acao.endEditCommand()*
    else:
        # print "Cancelled"
        *l_acao.destroyEditCommand()*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20130402/69977427/attachment.html>


More information about the Qgis-user mailing list