|
1 | 1 | from PySide6.QtWidgets import QMessageBox, QMenu |
2 | 2 | from PySide6.QtCore import QObject, Signal, QSettings |
3 | 3 | from PySide6.QtGui import QAction |
| 4 | +from collections import Counter |
| 5 | +from pathlib import Path |
4 | 6 |
|
5 | 7 |
|
6 | 8 | def prompt_overwrite_or_append(controller): |
@@ -55,13 +57,27 @@ def save_recent_files(self): |
55 | 57 | def update_tool_bar_menu(self): |
56 | 58 | """Create a menu for the tool bar.""" |
57 | 59 | 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)) |
61 | 75 | self.tool_bar_menu.addAction(action) |
| 76 | + |
62 | 77 | self.tool_bar_menu.addSeparator() |
63 | 78 | clear_action = QAction("Clear Recent Files", self.tool_bar_menu) |
64 | 79 | clear_action.triggered.connect(self.clear_recent_files) |
| 80 | + self.tool_bar_menu.addAction(clear_action) |
65 | 81 |
|
66 | 82 | def clear_recent_files(self): |
67 | 83 | """Clear the recent files list.""" |
|
0 commit comments