Skip to content

Commit 8a9ac92

Browse files
Shorter Filenames for Recent Files #74
1 parent c064fb3 commit 8a9ac92

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/petab_gui/controllers/utils.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from PySide6.QtWidgets import QMessageBox, QMenu
22
from PySide6.QtCore import QObject, Signal, QSettings
33
from PySide6.QtGui import QAction
4+
from collections import Counter
5+
from pathlib import Path
46

57

68
def prompt_overwrite_or_append(controller):
@@ -55,13 +57,27 @@ def save_recent_files(self):
5557
def update_tool_bar_menu(self):
5658
"""Create a menu for the tool bar."""
5759
self.tool_bar_menu.clear()
58-
for idx, file_path in enumerate(self.recent_files):
59-
action = QAction(file_path, self.tool_bar_menu)
60-
action.triggered.connect(lambda: self.open_file.emit(file_path))
60+
61+
# Generate shortened names
62+
def short_name(path):
63+
p = Path(path)
64+
if p.parent.name:
65+
return f"{p.parent.name}/{p.name}"
66+
return p.name
67+
68+
short_paths = [short_name(f) for f in self.recent_files]
69+
counts = Counter(short_paths)
70+
71+
for full_path, short in zip(self.recent_files, short_paths):
72+
display = full_path if counts[short] > 1 else short
73+
action = QAction(display, self.tool_bar_menu)
74+
action.triggered.connect(lambda _, p=full_path: self.open_file.emit(p))
6175
self.tool_bar_menu.addAction(action)
76+
6277
self.tool_bar_menu.addSeparator()
6378
clear_action = QAction("Clear Recent Files", self.tool_bar_menu)
6479
clear_action.triggered.connect(self.clear_recent_files)
80+
self.tool_bar_menu.addAction(clear_action)
6581

6682
def clear_recent_files(self):
6783
"""Clear the recent files list."""

0 commit comments

Comments
 (0)