I'm looking for a way of preventing/warning users to select another layer without saving the current one they were using.
The only potential solution I found is an autosave plugin but it won't exactly fit to my question.
I'm using QGIS las palmas.
You could connect a function to a signal which is fired whenever the current layer changes. You could add some logic to this function which would always be selected and if you tried to select another layer, it would trigger the Toggle Editing button which if any changes were made would prompt the user to save or discard the changes.
def check_edit():
layers = iface.editableLayers()
if layers:
iface.legendInterface().setCurrentLayer(layers[0])
iface.actionToggleEditing().trigger()
iface.legendInterface().currentLayerChanged.connect(check_edit)
Edit:
You could create a startup.py script so that the above code is executed when GOMap is initiated. You will need to modify the code slightly to import the relevant module:
from qgis.utils import iface
def check_edit():
layers = iface.editableLayers()
if layers:
iface.legendInterface().setCurrentLayer(layers[0])
iface.actionToggleEditing().trigger()
iface.legendInterface().currentLayerChanged.connect(check_edit)
And save this as a python script in your C:/Users/you/.qgis2/python/ directory.
what I did: open layer properties of one layer > fields > python init function = 'provide code in this dialog" > paste your code > function name = check_edit.
Maybe what I asked go beyond my knowledge but I'm always keen to learn and for what I understood : do I need to add changes to your code?
– FranckT Oct 01 '19 at 16:52Processing > Python Consolethen copy/paste the code there. The code will work for layers already loaded and any new layers added. It might be better to test the logic of your code in the python console before adapting it to a startup script or macro. – Joseph Oct 02 '19 at 09:31