[Qgis-user] Undo\Redo edits using Python

Alexandre Neto senhor.neto at gmail.com
Wed Apr 3 01:50:30 PDT 2013


Sometimes a night of sleep helps to clears the ideas. In case anyone has
the same issue, here's the "solution".

I believe that the issue was caused by using openFeatureForm() inside the
begin\end EditCommand. I could not confirm, but came to my mind that
openFeatureForm would probably create its own beginEditCommand at its
beginning and End or destroy it at the end, overcoming the one I create.

Since the real changes to my layer was done outside the FeatureForm, all I
needed to do was move the beginEditComand a bit down (see the code below).

Hope it will help someone else.

Alexandre Neto

On Tue, Apr 2, 2013 at 5:10 PM, Alexandre Neto <senhor.neto at gmail.com>wrote:

> 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()]
>
> +        * # start edit command to allow undo\redo*
*+         l_acao.beginEditCommand("Add new actions")*

>         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/20130403/9f1403f1/attachment.html>


More information about the Qgis-user mailing list