Skip to content

Commit 31fcf74

Browse files
authored
Merge pull request #214 from hanjinliu/fix-roi-widget
Record recent files for each profile
2 parents 81e3053 + d04262e commit 31fcf74

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ site_description: An infinitely extensible and reusable applications framework.
33
repo_name: himena
44
repo_url: https://github.com/hanjinliu/himena
55

6-
copyright: Copyright (c) 2024 - 2024 Hanjin Liu
6+
copyright: Copyright (c) 2024 - 2026 Hanjin Liu
77

88
theme:
99
name: material

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ testing = [
8282
docs = [
8383
"mkdocs",
8484
"mkdocs-autorefs",
85-
"mkdocs-material",
85+
"mkdocs-material==9.7.1",
8686
"mkdocs-material-extensions",
8787
"mkdocstrings",
8888
"mkdocstrings-python",

src/himena/_open_recent.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@ def __init__(
5454
self,
5555
app: HimenaApplication,
5656
menu_id: MenuId = MenuId.FILE_RECENT,
57-
file_name: str = "recent.json",
57+
dir_name: str = "recent",
5858
group: str = ActionGroup.RECENT_FILE,
5959
n_history: int = 60,
6060
):
6161
self._disposer = lambda: None
6262
self._app = app
6363
self._menu_id = menu_id
64-
self._file_name = file_name
64+
self._dir_name = dir_name
6565
self._group = group
6666
self._n_history = n_history
6767

6868
def update_menu(self):
6969
"""Update the menu for the recent file list."""
7070
if self._app.name in self._MENU_UPDATED:
71-
return None
71+
return
7272
file_args = self._list_args_for_recent()[::-1]
7373
if len(file_args) == 0:
74-
return None
74+
return
7575
num_recent = self.num_recent_in_menu()
7676
actions = [
7777
self.action_for_file(path, plugin, in_menu=i < num_recent)
@@ -96,7 +96,7 @@ def default(cls, app: HimenaApplication) -> RecentFileManager:
9696
def _list_args_for_recent(self) -> list[tuple[_PathInput, str | None]]:
9797
"""List the recent files (older first)."""
9898

99-
_path = data_dir() / self._file_name
99+
_path = self.recent_json_path()
100100
if not _path.exists():
101101
return []
102102
with open(_path) as f:
@@ -120,9 +120,9 @@ def _list_args_for_recent(self) -> list[tuple[_PathInput, str | None]]:
120120
def append_recent_files(
121121
self,
122122
inputs: list[tuple[_PathInput, str | None]],
123-
) -> None:
123+
):
124124
"""Append file(s) with plugin to the user history (duplication OK)."""
125-
_path = data_dir() / self._file_name
125+
_path = self.recent_json_path()
126126
if _path.exists():
127127
with open(_path) as f:
128128
all_info = json.load(f)
@@ -141,17 +141,11 @@ def append_recent_files(
141141
if each in existing_paths:
142142
to_remove.append(existing_paths.index(each))
143143
if isinstance(each, list):
144-
all_info.append(
145-
{"type": "group", "path": each, "plugin": plugin, "time": now}
146-
)
144+
all_info.append(_make_info("group", each, plugin, now))
147145
elif Path(each).is_file():
148-
all_info.append(
149-
{"type": "file", "path": each, "plugin": plugin, "time": now}
150-
)
146+
all_info.append(_make_info("file", each, plugin, now))
151147
else:
152-
all_info.append(
153-
{"type": "folder", "path": each, "plugin": plugin, "time": now}
154-
)
148+
all_info.append(_make_info("folder", each, plugin, now))
155149
for i in sorted(to_remove, reverse=True):
156150
all_info.pop(i)
157151
if len(all_info) > self._n_history:
@@ -201,6 +195,14 @@ def id_title_for_file(self, file: _PathInput) -> tuple[str, str]:
201195
title = f"{len(file)} files such as {file[0]}"
202196
return id, title
203197

198+
def recent_json_path(self):
199+
"""Path to the JSON file that stores the recent file list."""
200+
_data_dir = data_dir()
201+
_recent_dir = _data_dir / self._dir_name
202+
if not _recent_dir.exists():
203+
_recent_dir.mkdir(parents=True, exist_ok=True)
204+
return _recent_dir / f"{self._app.name}.json"
205+
204206

205207
class RecentSessionManager(RecentFileManager):
206208
_MENU_UPDATED: set[str] = set()
@@ -209,7 +211,7 @@ class RecentSessionManager(RecentFileManager):
209211
def default(cls, app: HimenaApplication) -> RecentSessionManager:
210212
return cls(
211213
app,
212-
file_name="recent_sessions.json",
214+
dir_name="recent_sessions",
213215
group=ActionGroup.RECENT_SESSION,
214216
n_history=20,
215217
)
@@ -250,3 +252,7 @@ def _update_annotation(obj):
250252

251253
obj.__annotations__ = {"ui": MainWindow}
252254
return obj
255+
256+
257+
def _make_info(typ: str, path, plugin: str | None, now: str) -> dict:
258+
return {"type": typ, "path": path, "plugin": plugin, "time": now}

src/himena/utils/ndobject.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def extend(self, other: Self) -> None:
128128
else:
129129
self.items = np.concatenate([self.items, other.items], axis=0)
130130
self.indices = np.concatenate([self.indices, other.indices], axis=0)
131-
return None
132131

133132
def pop(self, index: int) -> _T:
134133
item = self.items[index]
@@ -202,8 +201,10 @@ def map_elements(
202201
def simplified(self) -> Self:
203202
"""Drop axis"""
204203
cannot_drop = np.all(self.indices >= 0, axis=0)
204+
if all(cannot_drop):
205+
return self
205206
indices = np.take(self.indices, cannot_drop, axis=1)
206-
axis_names = [a for i, a in enumerate(self.axis_names) if i in cannot_drop]
207+
axis_names = [a for i, a in enumerate(self.axis_names) if cannot_drop[i]]
207208
return self.__class__(
208209
items=self.items,
209210
indices=indices,

src/himena_builtins/qt/widgets/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,13 @@ def dropped_callback(self, model: WidgetDataModel):
436436
if model.type == StandardType.ROIS:
437437
# dropping ROIs to concatenate to the current ROI list
438438
if isinstance(roi_list := model.value, roi.RoiListModel):
439+
roi_list = roi_list.coerce_dimensions(self._dims_slider.axis_names())
439440
self._roi_col.extend_from_standard_roi_list(roi_list)
440441
self._update_rois()
441442
self._is_modified = True
442443
return DropResult(delete_input=False)
443444
elif model.type == StandardType.IMAGE_LABELS:
444445
raise NotImplementedError("Merging with labels is not implemented yet.")
445-
return None
446446

447447
@validate_protocol
448448
def widget_resized_callback(self, old: Size, new: Size):

0 commit comments

Comments
 (0)