napari / napari

napari: a fast, interactive, multi-dimensional image viewer for python
https://napari.org
BSD 3-Clause "New" or "Revised" License
2.07k stars 410 forks source link

implement copying spatial information via clipboard #6864

Closed Czaki closed 2 weeks ago

Czaki commented 3 weeks ago

References and relevant issues

alternative to #6862

Description

This PR implements coping spatial information between layers using a clipboard.

https://github.com/napari/napari/assets/3826210/e6a476a3-45f9-4f6e-bcfc-462a7ff7662d

In this PR I have implemented coping spatial information between layers using clipboard. When coping data to the clipboard, it copies data in two formats:

  1. Pickled dict as mime type application/octet-stream
  2. same dict serialized using json.

When pasting, the code first checks if proper binary data are available in the buffer if yes, then it is using unpickled dict. If there is no binary buffer, the code tries to deserialize JSON into dict and use values from here.

I kept JSON as fallback as it simplifies debug.

These approaches allow copying information between two viewer instances, even from different process.

This may be improved by using cbor instead of JSON, but it will add additional dependency. https://pypi.org/project/cbor2/

It is part of #6780

codecov[bot] commented 3 weeks ago

Codecov Report

Attention: Patch coverage is 95.12195% with 12 lines in your changes are missing coverage. Please review.

Project coverage is 92.43%. Comparing base (4d33e78) to head (2521f4e). Report is 1 commits behind head on main.

Files Patch % Lines
napari/layers/base/_test_util_sample_layer.py 73.52% 9 Missing :warning:
napari/_qt/_qapp_model/qactions/_layer.py 96.42% 3 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #6864 +/- ## ======================================== Coverage 92.43% 92.43% ======================================== Files 614 617 +3 Lines 54892 55148 +256 ======================================== + Hits 50738 50976 +238 - Misses 4154 4172 +18 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Czaki commented 3 weeks ago

For now, it is not only scale/transform but also shear, rotate and affine.

Czaki commented 2 weeks ago

I have updated attached video to current state

lucyleeow commented 1 day ago

@Czaki in the interest of only having one file per menu, would you consider putting these actions with the non-Qt layer actions? I know clipboard does not make sense headless, ~but also the actions do not require anything Qt~. Darn, I just saw clipboard needs Qt. I guess we should think about whether the non-Qt layer actions are actually useful to have headless.

The reason for one file: is easier to find, and we can put the submenus with the single action file (see #6848)

cc @DragaDoncila

Czaki commented 1 day ago

Darn, I just saw clipboard needs Qt.

It needs Qt or additional dependecy

I guess we should think about whether the non-Qt layer actions are actually useful to have headless.

When we finally start implementing another fronted, such split will allow to notice which actions need to be reimplemented.

The reason for one file: is easier to find, and we can put the submenus with the single action file (see #6848)

I do not have a strong opinion about organization. I think that you may have better intuition. We may refactor it.