Skip to content

Commit

Permalink
Sort elements in widgets (#285)
Browse files Browse the repository at this point in the history
* Sort elements and coordinate systems in widgets

* Sort table names

* fix test, update changelog

---------

Co-authored-by: Luca Marconato <[email protected]>
  • Loading branch information
aeisenbarth and LucaMarconato authored Dec 17, 2024
1 parent e19e94c commit 0c6abf2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning][].
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## incoming release

- Global setting variables for showing circles as points and point size for scatter widget #309 #334
- Sorting the element names and coordinate system names alphabetically #285

## [0.5.4] - 2024-11-26

### Changed
Expand Down
10 changes: 8 additions & 2 deletions src/napari_spatialdata/_sdata_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import platform
from collections.abc import Iterable
from importlib.metadata import version
from operator import itemgetter
from pathlib import Path
from typing import TYPE_CHECKING, cast

Expand All @@ -16,6 +17,7 @@
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import QLabel, QListWidget, QListWidgetItem, QProgressBar, QVBoxLayout, QWidget
from spatialdata import SpatialData
from spatialdata.models._utils import DEFAULT_COORDINATE_SYSTEM

from napari_spatialdata._viewer import SpatialDataViewer
from napari_spatialdata.constants.config import N_CIRCLES_WARNING_THRESHOLD, N_SHAPES_WARNING_THRESHOLD
Expand Down Expand Up @@ -59,7 +61,7 @@ def _onItemChange(self, selected_coordinate_system: QListWidgetItem | int | Iter
self._elements = elements

def _set_element_widget_items(self, elements: dict[str, dict[str, str | int]]) -> None:
for key, dict_val in elements.items():
for key, dict_val in sorted(elements.items(), key=itemgetter(0)):
sdata = self._sdata[dict_val["sdata_index"]]
element_type = dict_val["element_type"]
element_name = dict_val["original_name"]
Expand Down Expand Up @@ -95,7 +97,11 @@ def __init__(self, sdata: EventedList):
self._sdata = sdata
self._system: None | str = None

coordinate_systems = {cs for sdata in self._sdata for cs in sdata.coordinate_systems}
# Sort alphabetically, but keep default "global" at the top.
coordinate_systems = sorted(cs for sdata in self._sdata for cs in sdata.coordinate_systems)
if DEFAULT_COORDINATE_SYSTEM in coordinate_systems:
coordinate_systems.remove(DEFAULT_COORDINATE_SYSTEM)
coordinate_systems.insert(0, DEFAULT_COORDINATE_SYSTEM)
self.addItems(coordinate_systems)

def _select_coord_sys(self, selected_coordinate_system: QListWidgetItem | int | Iterable[str]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/napari_spatialdata/_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def inherit_metadata(self, layers: list[Layer], ref_layer: Layer) -> None:
def _get_table_data(
self, sdata: SpatialData, element_name: str
) -> tuple[AnnData | None, str | None, list[str] | None]:
table_names: list[str] = list(get_element_annotators(sdata, element_name))
table_names: list[str] = sorted(get_element_annotators(sdata, element_name))
table_name = table_names[0] if len(table_names) > 0 else None
adata = _get_init_metadata_adata(sdata, table_name, element_name)
return adata, table_name, table_names
Expand Down
5 changes: 3 additions & 2 deletions tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ def test_save_layer(qtbot, tmp_path: str, make_napari_viewer: any):
n = len(widget.elements_widget)
# I would have expected Shapes to be in position -2 and Points in position -1, but we have the following order
# because elements of the same type are grouped together
assert widget.elements_widget.item(n - 1).text() == "Shapes"
assert widget.elements_widget.item(n - 5).text() == "Points"
all_elements = [widget.elements_widget.item(i).text() for i in range(n)]
assert "Shapes" in all_elements
assert "Points" in all_elements

# add a new layer to the viewer with the newly saved shapes element
widget._onClick("Shapes")
Expand Down

0 comments on commit 0c6abf2

Please sign in to comment.