3

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.

TomazicM
  • 25,601
  • 22
  • 29
  • 39
FranckT
  • 71
  • 6

1 Answers1

3

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.

Joseph
  • 75,746
  • 7
  • 171
  • 282
  • Thanks Joseph, nonetheless my knowledge in coding this is far from advanced and from what I've done I got an error 'iface not defined'.

    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:52
  • 1
    add "from qgis.utils import iface" at the top of the file – etrimaille Oct 01 '19 at 18:15
  • 1
    But I don't think you should add this in layer properties. This code will work for all layers anyway, so if you setup this code for each layer properties, then this code might be executed many times! Use it from project macro, or python startup.py script or python console. It depends what you want to do exactly. – etrimaille Oct 01 '19 at 18:18
  • 1
    I'd like it to execute for each layers indeed. when I copy the code inside one layer, it runs an error before I open the form but indeed the code works when I want to change layer without saving. Using it a startup.py or project macro would solve this issue ? I,ve never done it, I'll try to find it out – FranckT Oct 01 '19 at 22:20
  • @FranckT - From the menubar, go to Processing > Python Console then 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
  • @FranckT - See the edited post. – Joseph Oct 08 '19 at 10:10
  • 1
    Perfect ! Thank you – FranckT Oct 08 '19 at 16:13
  • @FranckT - Most welcome! Glad it helped :) – Joseph Oct 09 '19 at 09:14