Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add project tree option to rename item to first heading #1692

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions novelwriter/gui/projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
14 changes: 8 additions & 6 deletions tests/test_gui/test_gui_projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == [
Expand All @@ -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",
Expand All @@ -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",
Expand Down
Loading