Repository: napari/napari

Issue:
Insert a new layer covers all the others, no matter where it is inserted
## 🐛 Bug

Insert a new layer covers all the others, no matter where it is inserted.

## To Reproduce

Steps to reproduce the behavior:

1. Insert a new layer at a given index, see that it covers all other layers

`viewer.layers.insert(insert_index, denoised_im)`

And then I need to insert a dummy layer and remove it again to make this bug disappear.
```
dummy = napari.layers.Layer.create(np.zeros((1, 1, 1)), {'name': 'dummy'}, layer_type='image')
viewer.layers.insert(0, dummy)
viewer.layers.remove(viewer.layers['dummy'])
```

## Expected behavior

The insert method should just work as expected and the viewer should update accordingly.

## Environment

napari: 0.4.15
Platform: Linux-5.17.4-200.fc35.x86_64-x86_64-with-glibc2.34
System: Fedora Linux 35 (Workstation Edition)
Python: 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]
Qt: 5.15.2
PyQt5: 5.15.6
NumPy: 1.21.5
SciPy: 1.8.0
Dask: 2022.04.0
VisPy: 0.9.6

OpenGL:
- GL version: 4.6.0 NVIDIA 510.47.03
- MAX_TEXTURE_SIZE: 16384

Screens:
- screen 1: resolution 1920x1200, scale 1.0

## Additional context
This bug appears in the context of the development of a new plugin (this logic is triggered in a widget). I have another bug when I want to move layer from one index to another...so these bugs might be related.



Discussion:
Is this a known issue? ping @brisvag ;)
I assumed it was the same underlying issue as #4421; is this happening in a different context (i.e: not withing a callback after insertion)? If so, can you make up an example like in #4421 that shows the behaviour? I cannot reproduce it manually.
I will try to show an example, but this behaviour is happening everywhere in my plugin. 
Here the example that triggers the bug:
```
import napari
import numpy as np
v = napari.Viewer()
il = v.add_image(np.random.rand(10, 10), colormap='reds')
il2 = v.add_image(np.random.rand(10, 10), colormap='reds')
data = np.ones((10,10))
im_layer = napari.layers.Layer.create(data, {'name': 'im'}, layer_type='image')
im = v.layers.insert(0, im_layer)
```

Thanks! This looks different from the other issue... I'll take a look :)

Resolving patch:
diff --git a/napari/_qt/qt_viewer.py b/napari/_qt/qt_viewer.py
--- a/napari/_qt/qt_viewer.py
+++ b/napari/_qt/qt_viewer.py
@@ -499,8 +499,8 @@ def _add_layer(self, layer):
                 vispy_layer.events.loaded.connect(self._qt_poll.wake_up)
 
         vispy_layer.node.parent = self.view.scene
-        vispy_layer.order = len(self.viewer.layers) - 1
         self.layer_to_visual[layer] = vispy_layer
+        self._reorder_layers()
 
     def _remove_layer(self, event):
         """When a layer is removed, remove its parent.


Test patch:
diff --git a/napari/_qt/_tests/test_qt_viewer.py b/napari/_qt/_tests/test_qt_viewer.py
--- a/napari/_qt/_tests/test_qt_viewer.py
+++ b/napari/_qt/_tests/test_qt_viewer.py
@@ -18,6 +18,7 @@
     skip_on_win_ci,
 )
 from napari._vispy.utils.gl import fix_data_dtype
+from napari.layers import Points
 from napari.settings import get_settings
 from napari.utils.interactions import mouse_press_callbacks
 from napari.utils.io import imread
@@ -641,3 +642,18 @@ def test_surface_mixed_dim(make_napari_viewer):
     timeseries_values = np.vstack([values, values])
     timeseries_data = (verts, faces, timeseries_values)
     viewer.add_surface(timeseries_data)
+
+
+def test_insert_layer_ordering(make_napari_viewer):
+    """make sure layer ordering is correct in vispy when inserting layers"""
+    viewer = make_napari_viewer()
+    pl1 = Points()
+    pl2 = Points()
+
+    viewer.layers.append(pl1)
+    viewer.layers.insert(0, pl2)
+
+    pl1_vispy = viewer.window._qt_viewer.layer_to_visual[pl1].node
+    pl2_vispy = viewer.window._qt_viewer.layer_to_visual[pl2].node
+    assert pl1_vispy.order == 1
+    assert pl2_vispy.order == 0