Discussion:
[Qgis-developer] wrapping changeAttributeValue between begin and end EditCommand
f***@libero.it
10 years ago
Permalink
Hi all,
i have a problem with beginEditCommand and endEditCommand wrapping.
In my plugin i capture the layer.featureAdded signal :
self.layer.featureAdded.connect(self.myfunction)
The scope of my function is to change attribute of the feature added according with some rules.
I wrap self.layer.changeAttributevalue between self.layer.beginEditCommand('My custom value') and self.layer.endEditCommand() but when i added a feature i can't see in Undo/redo window the label My custom value but my value is changed.
This cause a Qgis 2.8.2 crash when i try to rollback my changes.

P.S If i comment my change values function and i write the code in a python console i can see my beginEditCommand label and no crash happend.

Anyone can help me?

Thanks

Francesco
Martin Dobias
10 years ago
Permalink
Hi Francesco

Currently it is not safe to do calls that modify vector layer data in slots
connected to signals notifying about data change (such as featureAdded).
The issue is that at the point when those signals are emitted, their
underlying undo commands were not yet pushed onto the stack, so doing
further editing calls causes corruption of undo stack (undo command for
follow up operation is placed before the first operation).

As a workaround you could defer the change attribute operations - e.g. with
a QTimer. Not an elegant solution though...

Regards
Martin
...
f***@libero.it
10 years ago
Permalink
Hi Martin,
Thanks for your reply. I try to create a workaround as you suggest me.
Thanks

Francesco





----Messaggio originale----

Da: ***@gmail.com

Data: 16/03/2015 5.19

A: "***@libero.it"<***@libero.it>

Cc: "qgis-dev"<qgis-***@lists.osgeo.org>

Ogg: Re: [Qgis-developer] wrapping changeAttributeValue between begin and end EditCommand



Hi Francesco
Currently it is not safe to do calls that modify vector layer data in slots connected to signals notifying about data change (such as featureAdded). The issue is that at the point when those signals are emitted, their underlying undo commands were not yet pushed onto the stack, so doing further editing calls causes corruption of undo stack (undo command for follow up operation is placed before the first operation).
As a workaround you could defer the change attribute operations - e.g. with a QTimer. Not an elegant solution though...
RegardsMartin

On Sun, Mar 15, 2015 at 9:04 PM, ***@libero.it <***@libero.it> wrote:


Hi all,
i have a problem with beginEditCommand and endEditCommand wrapping.
In my plugin i capture the layer.featureAdded signal :
self.layer.featureAdded.connect(self.myfunction)
The scope of my function is to change attribute of the feature added according with some rules.
I wrap self.layer.changeAttributevalue between self.layer.beginEditCommand('My custom value') and self.layer.endEditCommand() but when i added a feature i can't see in Undo/redo window the label My custom value but my value is changed.
This cause a Qgis 2.8.2 crash when i try to rollback my changes.

P.S If i comment my change values function and i write the code in a python console i can see my beginEditCommand label and no crash happend.

Anyone can help me?

Thanks

Francesco


_______________________________________________

Qgis-developer mailing list

Qgis-***@lists.osgeo.org

http://lists.osgeo.org/mailman/listinfo/qgis-developer
f***@libero.it
10 years ago
Permalink
Hi Martin,
i try to implement a solution without QTimer patch.
Can you tell me if it could be work properly:

1- layer.undoStack().indexChanged.connect(self.undoStackChanged) -- get the signal from undo stack
2- layer.featureAdded.connect(self.setFeatureId) --get signal of feature added
3- def setFeatureId(self,feat_id):
##get the feature id added
self.feat_id = feat_id
4- def undoStackChanged(self,id): ### check the command from undo stack (if it is a add feature command) . If it is a dd feature command run my added_Feature function (that i bound in my previous code with featureAdded signal) passing the feature id already added
command = self.activeLayer.undoStack().command(id-1)
if command and command.actionText() == 'add feature':

self.handlelayerobject.added_Feature(self.feat_id)


I checked that feature added event is trigger before addind new command to undo stack. Is it true?
Do you think that is is a solution?


Thanks


Francesco



----Messaggio originale----

Da: ***@gmail.com

Data: 16/03/2015 5.19

A: "***@libero.it"<***@libero.it>

Cc: "qgis-dev"<qgis-***@lists.osgeo.org>

Ogg: Re: [Qgis-developer] wrapping changeAttributeValue between begin and end EditCommand



Hi Francesco
Currently it is not safe to do calls that modify vector layer data in slots connected to signals notifying about data change (such as featureAdded). The issue is that at the point when those signals are emitted, their underlying undo commands were not yet pushed onto the stack, so doing further editing calls causes corruption of undo stack (undo command for follow up operation is placed before the first operation).
As a workaround you could defer the change attribute operations - e.g. with a QTimer. Not an elegant solution though...
RegardsMartin

On Sun, Mar 15, 2015 at 9:04 PM, ***@libero.it <***@libero.it> wrote:


Hi all,
i have a problem with beginEditCommand and endEditCommand wrapping.
In my plugin i capture the layer.featureAdded signal :
self.layer.featureAdded.connect(self.myfunction)
The scope of my function is to change attribute of the feature added according with some rules.
I wrap self.layer.changeAttributevalue between self.layer.beginEditCommand('My custom value') and self.layer.endEditCommand() but when i added a feature i can't see in Undo/redo window the label My custom value but my value is changed.
This cause a Qgis 2.8.2 crash when i try to rollback my changes.

P.S If i comment my change values function and i write the code in a python console i can see my beginEditCommand label and no crash happend.

Anyone can help me?

Thanks

Francesco


_______________________________________________

Qgis-developer mailing list

Qgis-***@lists.osgeo.org

http://lists.osgeo.org/mailman/listinfo/qgis-developer
Luigi Pirelli
10 years ago
Permalink
I used a different approach attaching to end Editing action to do a
similar work.
The logic is, start editing, do modification, stop editing is
intercepted to save EditingBuffer and then roolback all modification.
Then process editing buffer to create values basing on a rule and them
I can add/change the modified records with a new editing session
without user interaction.

I don't know if this can ba applied to your use case.

regards, Luigi Pirelli

https://www.linkedin.com/in/luigipirelli
https://github.com/luipir
https://www.elance.com/s/edit/luigipirelli/
...
Matthias Kuhn
10 years ago
Permalink
Post by Luigi Pirelli
I used a different approach attaching to end Editing action to do a
similar work.
The logic is, start editing, do modification, stop editing is
intercepted to save EditingBuffer and then roolback all modification.
Then process editing buffer to create values basing on a rule and them
I can add/change the modified records with a new editing session
without user interaction.
It would be so nice to be able to be able to register a trigger that is
executed to do this kind of things and data validation directly before
it enters the edit buffer.
Andrew McClure
10 years ago
Permalink
+1 for this

I was unaware of the reason for the crashes on Undo I was seeing until this thread. Our approach in Telemetry Layer was to simply end the edit session and commit the changes programmatically denying the possibility of a user undo. A more elegant approach would be welcomed.
...
Loading...