From f883de6e03092b1e1a8a1355595970f1ea12a277 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 11 Feb 2024 19:56:46 +0100 Subject: [PATCH 1/2] Add context menu option to rename item to header text --- novelwriter/gui/projtree.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index 53fc5ce5f..24d42a32f 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -1785,6 +1785,7 @@ def buildSingleSelectMenu(self, hasChild: bool) -> None: action = self.addAction(self.tr("Rename")) action.triggered.connect(lambda: self.projTree.renameTreeItem(self._handle)) if isFile: + self._itemHeader() self._itemActive(False) self._itemStatusImport(False) @@ -1833,6 +1834,15 @@ def _itemCreation(self) -> None: menu.addAction(self.projView.projBar.aAddFolder) return + def _itemHeader(self) -> None: + """Check if there is a header that can be used for rename.""" + if hItem := SHARED.project.index.getItemHeader(self._handle, "T0001"): + action = self.addAction(self.tr("Rename to Heading")) + action.triggered.connect( + lambda: self.projTree.renameTreeItem(self._handle, hItem.title) + ) + return + def _itemActive(self, multi: bool) -> None: """Add Active/Inactive actions.""" if multi: From e6efd15b9a71b1f40af5fee8d6f7471d0c44423e Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 11 Feb 2024 19:58:51 +0100 Subject: [PATCH 2/2] Update test --- tests/test_gui/test_gui_projtree.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_gui/test_gui_projtree.py b/tests/test_gui/test_gui_projtree.py index 4e2c07522..18e41ea30 100644 --- a/tests/test_gui/test_gui_projtree.py +++ b/tests/test_gui/test_gui_projtree.py @@ -1223,8 +1223,8 @@ def getTransformSubMenu(menu: QMenu) -> list[str]: ctxMenu.buildSingleSelectMenu(True) actions = [x.text() for x in ctxMenu.actions() if x.text()] assert actions == [ - "Open Document", "View Document", "Create New ...", "Rename", "Toggle Active", - "Set Status to ...", "Transform ...", "Expand All", "Collapse All", + "Open Document", "View Document", "Create New ...", "Rename", "Rename to Heading", + "Toggle Active", "Set Status to ...", "Transform ...", "Expand All", "Collapse All", "Duplicate from Here", "Move to Trash", ] assert getTransformSubMenu(ctxMenu) == [ @@ -1239,8 +1239,9 @@ def getTransformSubMenu(menu: QMenu) -> list[str]: ctxMenu.buildSingleSelectMenu(False) actions = [x.text() for x in ctxMenu.actions() if x.text()] assert actions == [ - "Open Document", "View Document", "Create New ...", "Rename", "Toggle Active", - "Set Importance to ...", "Transform ...", "Duplicate Document", "Move to Trash", + "Open Document", "View Document", "Create New ...", "Rename", "Rename to Heading", + "Toggle Active", "Set Importance to ...", "Transform ...", "Duplicate Document", + "Move to Trash", ] assert getTransformSubMenu(ctxMenu) == [ "Split Document by Headers", @@ -1253,8 +1254,9 @@ def getTransformSubMenu(menu: QMenu) -> list[str]: ctxMenu.buildSingleSelectMenu(False) actions = [x.text() for x in ctxMenu.actions() if x.text()] assert actions == [ - "Open Document", "View Document", "Create New ...", "Rename", "Toggle Active", - "Set Status to ...", "Transform ...", "Duplicate Document", "Move to Trash", + "Open Document", "View Document", "Create New ...", "Rename", "Rename to Heading", + "Toggle Active", "Set Status to ...", "Transform ...", "Duplicate Document", + "Move to Trash", ] assert getTransformSubMenu(ctxMenu) == [ "Convert to Novel Document", "Split Document by Headers",