Sometimes a night of sleep helps to clears the ideas. In case anyone has the same issue, here's the "solution".<div><br></div><div>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.</div>
<div><br></div><div>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).</div><div><br></div><div>Hope it will help someone else.</div>
<div><br></div><div>Alexandre Neto<br><br><div class="gmail_quote">On Tue, Apr 2, 2013 at 5:10 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>In a local python plugin I'm trying to allow Undo\Redo edits. For that I set a <b>beginEditCommand()</b>, <b>endEditCommand() and destroyEditCommand().</b></div>
<div><b><br></b></div><div>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.</div>
<div><br></div><div>Please note that "l_acao" layer is a non-spatial table, and l_uni_ges is a polygon vector layer.</div><div><br></div><div>I you will, please see my code below.</div><div><br></div><div>Thanks,</div>
<div><br></div><div>Alexandre Neto</div><div><br></div><div>----</div><div><br></div><div>def run(self):</div><div> # Define "acções" and "unidades de gestão" Layers from project</div><div> l_acao = QgsMapLayerRegistry.instance().mapLayer(u'acoes20130312143950563')</div>
<div> l_uni_ges = QgsMapLayerRegistry.instance().mapLayer(u'unidadesdegestao20130312143304288')</div><div> </div><div> # Get layer default values from provider</div><div> # (this avoids problems with unique keys)</div>
<div> provider = l_acao.dataProvider()</div><div> temp_feature = QgsFeature()</div><div> attributes = {}</div><div><br></div><div> for j in l_acao.pendingAllAttributesList():</div><div> if not provider.defaultValue(j).isNull():</div>
<div> attributes[j] = provider.defaultValue(j)</div><div> else:</div><div> attributes[j] = None</div><div><br></div><div> temp_feature.setAttributeMap(attributes)</div><div><span style="background-color:rgb(255,204,204)">- <b> # start edit command to allow undo\redo</b></span></div>
<div><b style="background-color:rgb(255,204,204)">- l_acao.beginEditCommand("Add new actions")</b></div><div> </div><div> # open feature form and waits for edits</div><div> if self.iface.openFeatureForm(l_acao, temp_feature):</div>
<div> new_attributes = temp_feature.attributeMap()</div>
<div> </div><div> # replicate "acção" record for each</div><div> # of the selected "Unidades de gestão"</div><div> uniges_ids = [feature.attributeMap()[1] for feature in l_uni_ges.selectedFeatures()]</div>
<div><br></div></blockquote><div><span style="background-color:rgb(153,255,153)">+ <b> # start edit command to allow undo\redo</b></span></div><div><b style="background-color:rgb(153,255,153)">+ l_acao.beginEditCommand("Add new actions")</b></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div></div><div> for uniges_id in uniges_ids:</div><div> new_attributes[1] = uniges_id</div><div> temp_feature.setAttributeMap(new_attributes)</div>
<div> new_feature = QgsFeature(temp_feature)</div>
<div> l_acao.addFeature( new_feature )</div><div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><span style="background-color:rgb(255,204,204)">- <b>l_acao.endEditCommand()</b></span></div>
<div><span style="background-color:rgb(255,204,204)">- else:</span></div><div><span style="background-color:rgb(255,204,204)">- # print "Cancelled"</span></div><div><span style="background-color:rgb(255,204,204)">- <b>l_acao.destroyEditCommand()</b></span></div>
</blockquote></div><br></div>