|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from PySide6.QtCore import Qt, QModelIndex |
| 6 | +from PySide6.QtGui import QCursor |
| 7 | +from PySide6.QtWidgets import ( |
| 8 | + QTreeView, QMenu, QFileSystemModel, QInputDialog, |
| 9 | + QMessageBox, QApplication, |
| 10 | +) |
| 11 | +from je_editor import language_wrapper |
| 12 | +from je_editor.pyside_ui.main_ui.editor.editor_widget import EditorWidget |
| 13 | + |
| 14 | + |
| 15 | +def setup_file_tree_context_menu(main_window) -> None: |
| 16 | + """ |
| 17 | + Attach a right-click context menu to every current and future |
| 18 | + EditorWidget's project_treeview. |
| 19 | + """ |
| 20 | + # Attach to existing tabs |
| 21 | + for i in range(main_window.tab_widget.count()): |
| 22 | + widget = main_window.tab_widget.widget(i) |
| 23 | + if isinstance(widget, EditorWidget) and widget.project_treeview is not None: |
| 24 | + _attach_context_menu(widget.project_treeview, main_window) |
| 25 | + |
| 26 | + # Listen for new tabs so future EditorWidgets also get the context menu |
| 27 | + original_add_tab = main_window.tab_widget.addTab |
| 28 | + |
| 29 | + def patched_add_tab(*args, **kwargs): |
| 30 | + result = original_add_tab(*args, **kwargs) |
| 31 | + widget = args[0] if args else None |
| 32 | + if isinstance(widget, EditorWidget) and widget.project_treeview is not None: |
| 33 | + if widget.project_treeview.contextMenuPolicy() != Qt.ContextMenuPolicy.CustomContextMenu: |
| 34 | + _attach_context_menu(widget.project_treeview, main_window) |
| 35 | + return result |
| 36 | + |
| 37 | + main_window.tab_widget.addTab = patched_add_tab |
| 38 | + |
| 39 | + |
| 40 | +def _attach_context_menu(tree_view: QTreeView, main_window) -> None: |
| 41 | + tree_view.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) |
| 42 | + tree_view.customContextMenuRequested.connect( |
| 43 | + lambda pos, tv=tree_view, mw=main_window: _show_context_menu(pos, tv, mw) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _get_path_from_index(tree_view: QTreeView, index: QModelIndex) -> Path | None: |
| 48 | + model: QFileSystemModel = tree_view.model() |
| 49 | + if not index.isValid(): |
| 50 | + return None |
| 51 | + return Path(model.filePath(index)) |
| 52 | + |
| 53 | + |
| 54 | +def _get_tree_root_path(tree_view: QTreeView) -> Path: |
| 55 | + """Get the root directory currently shown in the tree view.""" |
| 56 | + model: QFileSystemModel = tree_view.model() |
| 57 | + root_index = tree_view.rootIndex() |
| 58 | + if root_index.isValid(): |
| 59 | + return Path(model.filePath(root_index)) |
| 60 | + return Path.cwd() |
| 61 | + |
| 62 | + |
| 63 | +def _show_context_menu(pos, tree_view: QTreeView, main_window) -> None: |
| 64 | + word = language_wrapper.language_word_dict |
| 65 | + index = tree_view.indexAt(pos) |
| 66 | + path = _get_path_from_index(tree_view, index) |
| 67 | + |
| 68 | + menu = QMenu(tree_view) |
| 69 | + |
| 70 | + # --- File / Folder creation --- |
| 71 | + new_file_act = menu.addAction(word.get("file_tree_ctx_new_file")) |
| 72 | + new_folder_act = menu.addAction(word.get("file_tree_ctx_new_folder")) |
| 73 | + menu.addSeparator() |
| 74 | + |
| 75 | + # --- Operations on selected item --- |
| 76 | + rename_act = menu.addAction(word.get("file_tree_ctx_rename")) |
| 77 | + delete_act = menu.addAction(word.get("file_tree_ctx_delete")) |
| 78 | + rename_act.setEnabled(path is not None) |
| 79 | + delete_act.setEnabled(path is not None) |
| 80 | + menu.addSeparator() |
| 81 | + |
| 82 | + # --- Clipboard --- |
| 83 | + copy_path_act = menu.addAction(word.get("file_tree_ctx_copy_path")) |
| 84 | + copy_rel_path_act = menu.addAction(word.get("file_tree_ctx_copy_relative_path")) |
| 85 | + copy_path_act.setEnabled(path is not None) |
| 86 | + copy_rel_path_act.setEnabled(path is not None) |
| 87 | + menu.addSeparator() |
| 88 | + |
| 89 | + # --- Explorer --- |
| 90 | + reveal_act = menu.addAction(word.get("file_tree_ctx_reveal_in_explorer")) |
| 91 | + reveal_act.setEnabled(path is not None) |
| 92 | + |
| 93 | + action = menu.exec(QCursor.pos()) |
| 94 | + if action is None: |
| 95 | + return |
| 96 | + |
| 97 | + if action == new_file_act: |
| 98 | + _action_new_file(tree_view, path) |
| 99 | + elif action == new_folder_act: |
| 100 | + _action_new_folder(tree_view, path) |
| 101 | + elif action == rename_act: |
| 102 | + _action_rename(tree_view, main_window, path) |
| 103 | + elif action == delete_act: |
| 104 | + _action_delete(tree_view, main_window, path) |
| 105 | + elif action == copy_path_act: |
| 106 | + _action_copy_path(tree_view, path, relative=False) |
| 107 | + elif action == copy_rel_path_act: |
| 108 | + _action_copy_path(tree_view, path, relative=True) |
| 109 | + elif action == reveal_act: |
| 110 | + _action_reveal_in_explorer(path) |
| 111 | + |
| 112 | + |
| 113 | +# --------------- actions --------------- |
| 114 | + |
| 115 | +def _resolve_parent_dir(tree_view: QTreeView, path: Path | None) -> Path: |
| 116 | + """Return the directory where a new file/folder should be created.""" |
| 117 | + if path is not None: |
| 118 | + return path if path.is_dir() else path.parent |
| 119 | + return _get_tree_root_path(tree_view) |
| 120 | + |
| 121 | + |
| 122 | +def _action_new_file(tree_view: QTreeView, path: Path | None) -> None: |
| 123 | + word = language_wrapper.language_word_dict |
| 124 | + parent = _resolve_parent_dir(tree_view, path) |
| 125 | + name, ok = QInputDialog.getText( |
| 126 | + tree_view, |
| 127 | + word.get("file_tree_ctx_new_file"), |
| 128 | + word.get("file_tree_ctx_input_file_name"), |
| 129 | + ) |
| 130 | + if not ok or not name.strip(): |
| 131 | + return |
| 132 | + new_path = parent / name.strip() |
| 133 | + if new_path.exists(): |
| 134 | + QMessageBox.warning( |
| 135 | + tree_view, |
| 136 | + word.get("file_tree_ctx_error"), |
| 137 | + word.get("file_tree_ctx_already_exists").format(name=str(new_path)), |
| 138 | + ) |
| 139 | + return |
| 140 | + new_path.parent.mkdir(parents=True, exist_ok=True) |
| 141 | + new_path.touch() |
| 142 | + |
| 143 | + |
| 144 | +def _action_new_folder(tree_view: QTreeView, path: Path | None) -> None: |
| 145 | + word = language_wrapper.language_word_dict |
| 146 | + parent = _resolve_parent_dir(tree_view, path) |
| 147 | + name, ok = QInputDialog.getText( |
| 148 | + tree_view, |
| 149 | + word.get("file_tree_ctx_new_folder"), |
| 150 | + word.get("file_tree_ctx_input_folder_name"), |
| 151 | + ) |
| 152 | + if not ok or not name.strip(): |
| 153 | + return |
| 154 | + new_path = parent / name.strip() |
| 155 | + if new_path.exists(): |
| 156 | + QMessageBox.warning( |
| 157 | + tree_view, |
| 158 | + word.get("file_tree_ctx_error"), |
| 159 | + word.get("file_tree_ctx_already_exists").format(name=str(new_path)), |
| 160 | + ) |
| 161 | + return |
| 162 | + new_path.mkdir(parents=True) |
| 163 | + |
| 164 | + |
| 165 | +def _find_editor_for_file(main_window, file_path: Path) -> EditorWidget | None: |
| 166 | + """Find the EditorWidget that has the given file open.""" |
| 167 | + path_str = str(file_path) |
| 168 | + for i in range(main_window.tab_widget.count()): |
| 169 | + widget = main_window.tab_widget.widget(i) |
| 170 | + if isinstance(widget, EditorWidget) and widget.current_file is not None: |
| 171 | + if str(Path(widget.current_file)) == path_str: |
| 172 | + return widget |
| 173 | + return None |
| 174 | + |
| 175 | + |
| 176 | +def _action_rename(tree_view: QTreeView, main_window, path: Path | None) -> None: |
| 177 | + if path is None: |
| 178 | + return |
| 179 | + word = language_wrapper.language_word_dict |
| 180 | + new_name, ok = QInputDialog.getText( |
| 181 | + tree_view, |
| 182 | + word.get("file_tree_ctx_rename"), |
| 183 | + word.get("file_tree_ctx_input_new_name").format(name=path.name), |
| 184 | + text=path.name, |
| 185 | + ) |
| 186 | + if not ok or not new_name.strip() or new_name.strip() == path.name: |
| 187 | + return |
| 188 | + target = path.parent / new_name.strip() |
| 189 | + if target.exists(): |
| 190 | + QMessageBox.warning( |
| 191 | + tree_view, |
| 192 | + word.get("file_tree_ctx_error"), |
| 193 | + word.get("file_tree_ctx_already_exists").format(name=str(target)), |
| 194 | + ) |
| 195 | + return |
| 196 | + |
| 197 | + # If this file is currently open in an editor tab, update the tab |
| 198 | + editor = _find_editor_for_file(main_window, path) |
| 199 | + path.rename(target) |
| 200 | + if editor is not None and target.is_file(): |
| 201 | + editor.current_file = str(target) |
| 202 | + editor.code_edit.current_file = str(target) |
| 203 | + editor.rename_self_tab() |
| 204 | + |
| 205 | + |
| 206 | +def _action_delete(tree_view: QTreeView, main_window, path: Path | None) -> None: |
| 207 | + if path is None: |
| 208 | + return |
| 209 | + word = language_wrapper.language_word_dict |
| 210 | + reply = QMessageBox.question( |
| 211 | + tree_view, |
| 212 | + word.get("file_tree_ctx_confirm_delete"), |
| 213 | + word.get("file_tree_ctx_confirm_delete_message").format(name=str(path)), |
| 214 | + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, |
| 215 | + ) |
| 216 | + if reply != QMessageBox.StandardButton.Yes: |
| 217 | + return |
| 218 | + |
| 219 | + # Close editor tab if this file is open |
| 220 | + editor = _find_editor_for_file(main_window, path) |
| 221 | + if editor is not None: |
| 222 | + idx = main_window.tab_widget.indexOf(editor) |
| 223 | + if idx >= 0: |
| 224 | + editor.close() |
| 225 | + main_window.tab_widget.removeTab(idx) |
| 226 | + |
| 227 | + if path.is_dir(): |
| 228 | + shutil.rmtree(path) |
| 229 | + else: |
| 230 | + path.unlink() |
| 231 | + |
| 232 | + |
| 233 | +def _action_copy_path(tree_view: QTreeView, path: Path | None, relative: bool = False) -> None: |
| 234 | + if path is None: |
| 235 | + return |
| 236 | + if relative: |
| 237 | + base = _get_tree_root_path(tree_view) |
| 238 | + try: |
| 239 | + text = str(path.relative_to(base)) |
| 240 | + except ValueError: |
| 241 | + text = str(path) |
| 242 | + else: |
| 243 | + text = str(path) |
| 244 | + clipboard = QApplication.clipboard() |
| 245 | + clipboard.setText(text) |
| 246 | + |
| 247 | + |
| 248 | +def _action_reveal_in_explorer(path: Path | None) -> None: |
| 249 | + if path is None: |
| 250 | + return |
| 251 | + import subprocess |
| 252 | + import sys |
| 253 | + target = path if path.is_dir() else path.parent |
| 254 | + if sys.platform == "win32": |
| 255 | + os.startfile(str(target)) |
| 256 | + elif sys.platform == "darwin": |
| 257 | + subprocess.Popen(["open", str(target)]) |
| 258 | + else: |
| 259 | + subprocess.Popen(["xdg-open", str(target)]) |
0 commit comments