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 a context menu entry to set item name from heading #1614

Merged
merged 6 commits into from
Nov 23, 2023
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
260 changes: 147 additions & 113 deletions novelwriter/gui/doceditor.py

Large diffs are not rendered by default.

86 changes: 44 additions & 42 deletions novelwriter/gui/docviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
QTextOption
)
from PyQt5.QtWidgets import (
QAction, qApp, QFrame, QHBoxLayout, QLabel, QMenu, QTextBrowser,
QToolButton, QWidget
QAction, QFrame, QHBoxLayout, QLabel, QMenu, QTextBrowser, QToolButton,
QWidget, qApp
)

from novelwriter import CONFIG, SHARED
Expand All @@ -59,6 +59,7 @@ class GuiDocViewer(QTextBrowser):
documentLoaded = pyqtSignal(str)
loadDocumentTagRequest = pyqtSignal(str, Enum)
togglePanelVisibility = pyqtSignal()
requestProjectItemSelected = pyqtSignal(str, bool)

def __init__(self, mainGui: GuiMain) -> None:
super().__init__(parent=mainGui)
Expand All @@ -75,8 +76,8 @@ def __init__(self, mainGui: GuiMain) -> None:
self.setMinimumWidth(CONFIG.pxInt(300))
self.setAutoFillBackground(True)
self.setOpenExternalLinks(False)
self.setFocusPolicy(Qt.StrongFocus)
self.setFrameStyle(QFrame.NoFrame)
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.setFrameStyle(QFrame.Shape.NoFrame)

# Document Header and Footer
self.docHeader = GuiDocViewHeader(self)
Expand All @@ -92,7 +93,7 @@ def __init__(self, mainGui: GuiMain) -> None:
self.installEventFilter(self.wheelEventFilter)

# Context Menu
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(self._openContextMenu)

self.initViewer()
Expand Down Expand Up @@ -148,14 +149,14 @@ def initViewer(self) -> None:

# Set the widget colours to match syntax theme
mainPalette = self.palette()
mainPalette.setColor(QPalette.Window, QColor(*SHARED.theme.colBack))
mainPalette.setColor(QPalette.Base, QColor(*SHARED.theme.colBack))
mainPalette.setColor(QPalette.Text, QColor(*SHARED.theme.colText))
mainPalette.setColor(QPalette.ColorRole.Window, QColor(*SHARED.theme.colBack))
mainPalette.setColor(QPalette.ColorRole.Base, QColor(*SHARED.theme.colBack))
mainPalette.setColor(QPalette.ColorRole.Text, QColor(*SHARED.theme.colText))
self.setPalette(mainPalette)

docPalette = self.viewport().palette()
docPalette.setColor(QPalette.Base, QColor(*SHARED.theme.colBack))
docPalette.setColor(QPalette.Text, QColor(*SHARED.theme.colText))
docPalette.setColor(QPalette.ColorRole.Base, QColor(*SHARED.theme.colBack))
docPalette.setColor(QPalette.ColorRole.Text, QColor(*SHARED.theme.colText))
self.viewport().setPalette(docPalette)

self.docHeader.matchColours()
Expand All @@ -165,19 +166,19 @@ def initViewer(self) -> None:
self.document().setDocumentMargin(0)
theOpt = QTextOption()
if CONFIG.doJustify:
theOpt.setAlignment(Qt.AlignJustify)
theOpt.setAlignment(Qt.AlignmentFlag.AlignJustify)
self.document().setDefaultTextOption(theOpt)

# Scroll bars
if CONFIG.hideVScroll:
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
else:
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)

if CONFIG.hideHScroll:
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
else:
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)

# Refresh the tab stops
self.setTabStopDistance(CONFIG.getTabWidth())
Expand All @@ -196,7 +197,7 @@ def loadText(self, tHandle: str, updateHistory: bool = True) -> bool:
return False

logger.debug("Generating preview for item '%s'", tHandle)
qApp.setOverrideCursor(QCursor(Qt.WaitCursor))
qApp.setOverrideCursor(QCursor(Qt.CursorShape.WaitCursor))

sPos = self.verticalScrollBar().value()
aDoc = ToHtml(SHARED.project)
Expand Down Expand Up @@ -272,9 +273,9 @@ def docAction(self, action: nwDocAction) -> bool:
elif action == nwDocAction.COPY:
self.copy()
elif action == nwDocAction.SEL_ALL:
self._makeSelection(QTextCursor.Document)
self._makeSelection(QTextCursor.SelectionType.Document)
elif action == nwDocAction.SEL_PARA:
self._makeSelection(QTextCursor.BlockUnderCursor)
self._makeSelection(QTextCursor.SelectionType.BlockUnderCursor)
else:
logger.debug("Unknown or unsupported document action '%s'", str(action))
return False
Expand Down Expand Up @@ -392,13 +393,13 @@ def _openContextMenu(self, point: QPoint) -> None:

mnuSelWord = QAction(self.tr("Select Word"), mnuContext)
mnuSelWord.triggered.connect(
lambda: self._makePosSelection(QTextCursor.WordUnderCursor, point)
lambda: self._makePosSelection(QTextCursor.SelectionType.WordUnderCursor, point)
)
mnuContext.addAction(mnuSelWord)

mnuSelPara = QAction(self.tr("Select Paragraph"), mnuContext)
mnuSelPara.triggered.connect(
lambda: self._makePosSelection(QTextCursor.BlockUnderCursor, point)
lambda: self._makePosSelection(QTextCursor.SelectionType.BlockUnderCursor, point)
)
mnuContext.addAction(mnuSelPara)

Expand All @@ -419,9 +420,9 @@ def resizeEvent(self, event: QResizeEvent) -> None:

def mouseReleaseEvent(self, event: QMouseEvent) -> None:
"""Capture mouse click events on the document."""
if event.button() == Qt.BackButton:
if event.button() == Qt.MouseButton.BackButton:
self.navBackward()
elif event.button() == Qt.ForwardButton:
elif event.button() == Qt.MouseButton.ForwardButton:
self.navForward()
else:
super().mouseReleaseEvent(event)
Expand All @@ -437,15 +438,15 @@ def _makeSelection(self, selType: QTextCursor.SelectionType) -> None:
cursor.clearSelection()
cursor.select(selType)

if selType == QTextCursor.BlockUnderCursor:
if selType == QTextCursor.SelectionType.BlockUnderCursor:
# This selection mode also selects the preceding paragraph
# separator, which we want to avoid.
posS = cursor.selectionStart()
posE = cursor.selectionEnd()
selTxt = cursor.selectedText()
if selTxt.startswith(nwUnicode.U_PSEP):
cursor.setPosition(posS+1, QTextCursor.MoveAnchor)
cursor.setPosition(posE, QTextCursor.KeepAnchor)
cursor.setPosition(posS+1, QTextCursor.MoveMode.MoveAnchor)
cursor.setPosition(posE, QTextCursor.MoveMode.KeepAnchor)

self.setTextCursor(cursor)

Expand Down Expand Up @@ -663,7 +664,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.docTitle.setMargin(0)
self.docTitle.setContentsMargins(0, 0, 0, 0)
self.docTitle.setAutoFillBackground(True)
self.docTitle.setAlignment(Qt.AlignHCenter | Qt.AlignTop)
self.docTitle.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignTop)
self.docTitle.setFixedHeight(fPx)

lblFont = self.docTitle.font()
Expand All @@ -675,7 +676,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.backButton.setContentsMargins(0, 0, 0, 0)
self.backButton.setIconSize(QSize(fPx, fPx))
self.backButton.setFixedSize(fPx, fPx)
self.backButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.backButton.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.backButton.setVisible(False)
self.backButton.setToolTip(self.tr("Go Backward"))
self.backButton.clicked.connect(self.docViewer.navBackward)
Expand All @@ -684,7 +685,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.forwardButton.setContentsMargins(0, 0, 0, 0)
self.forwardButton.setIconSize(QSize(fPx, fPx))
self.forwardButton.setFixedSize(fPx, fPx)
self.forwardButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.forwardButton.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.forwardButton.setVisible(False)
self.forwardButton.setToolTip(self.tr("Go Forward"))
self.forwardButton.clicked.connect(self.docViewer.navForward)
Expand All @@ -693,7 +694,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.refreshButton.setContentsMargins(0, 0, 0, 0)
self.refreshButton.setIconSize(QSize(fPx, fPx))
self.refreshButton.setFixedSize(fPx, fPx)
self.refreshButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.refreshButton.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.refreshButton.setVisible(False)
self.refreshButton.setToolTip(self.tr("Reload"))
self.refreshButton.clicked.connect(self._refreshDocument)
Expand All @@ -702,7 +703,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.closeButton.setContentsMargins(0, 0, 0, 0)
self.closeButton.setIconSize(QSize(fPx, fPx))
self.closeButton.setFixedSize(fPx, fPx)
self.closeButton.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.closeButton.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.closeButton.setVisible(False)
self.closeButton.setToolTip(self.tr("Close"))
self.closeButton.clicked.connect(self._closeDocument)
Expand Down Expand Up @@ -761,9 +762,9 @@ def matchColours(self) -> None:
theme rather than the main GUI.
"""
palette = QPalette()
palette.setColor(QPalette.Window, QColor(*SHARED.theme.colBack))
palette.setColor(QPalette.WindowText, QColor(*SHARED.theme.colText))
palette.setColor(QPalette.Text, QColor(*SHARED.theme.colText))
palette.setColor(QPalette.ColorRole.Window, QColor(*SHARED.theme.colBack))
palette.setColor(QPalette.ColorRole.WindowText, QColor(*SHARED.theme.colText))
palette.setColor(QPalette.ColorRole.Text, QColor(*SHARED.theme.colText))
self.setPalette(palette)
self.docTitle.setPalette(palette)
return
Expand Down Expand Up @@ -834,7 +835,8 @@ def mousePressEvent(self, event: QMouseEvent) -> None:
"""Capture a click on the title and ensure that the item is
selected in the project tree.
"""
self.mainGui.projView.setSelectedHandle(self._docHandle, doScroll=True)
if event.button() == Qt.MouseButton.LeftButton:
self.docViewer.requestProjectItemSelected.emit(self._docHandle, True)
return

# END Class GuiDocViewHeader
Expand Down Expand Up @@ -868,7 +870,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:

# Show/Hide Details
self.showHide = QToolButton(self)
self.showHide.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.showHide.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.showHide.setIconSize(QSize(fPx, fPx))
self.showHide.setFixedSize(QSize(fPx, fPx))
self.showHide.clicked.connect(lambda: self.docViewer.togglePanelVisibility.emit())
Expand All @@ -878,7 +880,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.showComments = QToolButton(self)
self.showComments.setCheckable(True)
self.showComments.setChecked(CONFIG.viewComments)
self.showComments.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.showComments.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.showComments.setIconSize(QSize(fPx, fPx))
self.showComments.setFixedSize(QSize(fPx, fPx))
self.showComments.toggled.connect(self._doToggleComments)
Expand All @@ -888,7 +890,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.showSynopsis = QToolButton(self)
self.showSynopsis.setCheckable(True)
self.showSynopsis.setChecked(CONFIG.viewSynopsis)
self.showSynopsis.setToolButtonStyle(Qt.ToolButtonIconOnly)
self.showSynopsis.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
self.showSynopsis.setIconSize(QSize(fPx, fPx))
self.showSynopsis.setFixedSize(QSize(fPx, fPx))
self.showSynopsis.toggled.connect(self._doToggleSynopsis)
Expand All @@ -902,7 +904,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.lblComments.setContentsMargins(0, 0, 0, 0)
self.lblComments.setAutoFillBackground(True)
self.lblComments.setFixedHeight(fPx)
self.lblComments.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.lblComments.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)

self.lblSynopsis = QLabel(self.tr("Synopsis"))
self.lblSynopsis.setBuddy(self.showSynopsis)
Expand All @@ -911,7 +913,7 @@ def __init__(self, docViewer: GuiDocViewer) -> None:
self.lblSynopsis.setContentsMargins(0, 0, 0, 0)
self.lblSynopsis.setAutoFillBackground(True)
self.lblSynopsis.setFixedHeight(fPx)
self.lblSynopsis.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.lblSynopsis.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop)

lblFont = self.font()
lblFont.setPointSizeF(0.9*SHARED.theme.fontPointSize)
Expand Down Expand Up @@ -977,9 +979,9 @@ def matchColours(self) -> None:
theme rather than the main GUI.
"""
palette = QPalette()
palette.setColor(QPalette.Window, QColor(*SHARED.theme.colBack))
palette.setColor(QPalette.WindowText, QColor(*SHARED.theme.colText))
palette.setColor(QPalette.Text, QColor(*SHARED.theme.colText))
palette.setColor(QPalette.ColorRole.Window, QColor(*SHARED.theme.colBack))
palette.setColor(QPalette.ColorRole.WindowText, QColor(*SHARED.theme.colText))
palette.setColor(QPalette.ColorRole.Text, QColor(*SHARED.theme.colText))
self.setPalette(palette)
self.lblComments.setPalette(palette)
self.lblSynopsis.setPalette(palette)
Expand Down
39 changes: 22 additions & 17 deletions novelwriter/gui/projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def __init__(self, mainGui: GuiMain) -> None:
self.emptyTrash = self.projTree.emptyTrash
self.requestDeleteItem = self.projTree.requestDeleteItem
self.getSelectedHandle = self.projTree.getSelectedHandle
self.setSelectedHandle = self.projTree.setSelectedHandle
self.changedSince = self.projTree.changedSince
self.createNewNote = self.projTree.createNewNote

Expand Down Expand Up @@ -191,17 +190,26 @@ def treeHasFocus(self) -> bool:
"""Check if the project tree has focus."""
return self.projTree.hasFocus()

def renameTreeItem(self, tHandle: str | None = None) -> bool:
##
# Public Slots
##

@pyqtSlot(str, str)
def renameTreeItem(self, tHandle: str | None = None, name: str = "") -> None:
"""External request to rename an item or the currently selected
item. This is triggered by the global menu or keyboard shortcut.
"""
if tHandle is None:
tHandle = self.projTree.getSelectedHandle()
return self.projTree.renameTreeItem(tHandle) if tHandle else False
if tHandle:
self.projTree.renameTreeItem(tHandle, name=name)
return

##
# Public Slots
##
@pyqtSlot(str, bool)
def setSelectedHandle(self, tHandle: str, doScroll: bool = False) -> None:
"""Select an item and optionally scroll it into view."""
self.projTree.setSelectedHandle(tHandle, doScroll=doScroll)
return

@pyqtSlot(str)
def updateItemValues(self, tHandle: str) -> None:
Expand Down Expand Up @@ -761,19 +769,16 @@ def moveToLevel(self, step: int) -> None:
self.setCurrentItem(tItem.child(0))
return

def renameTreeItem(self, tHandle: str) -> bool:
def renameTreeItem(self, tHandle: str, name: str = "") -> None:
"""Open a dialog to edit the label of an item."""
tItem = SHARED.project.tree[tHandle]
if tItem is None:
return False

newLabel, dlgOk = GuiEditLabel.getLabel(self, text=tItem.itemName)
if dlgOk:
tItem.setName(newLabel)
self.setTreeItemValues(tHandle)
self._alertTreeChange(tHandle, flush=False)

return True
if tItem:
newLabel, dlgOk = GuiEditLabel.getLabel(self, text=name or tItem.itemName)
if dlgOk:
tItem.setName(newLabel)
self.setTreeItemValues(tHandle)
self._alertTreeChange(tHandle, flush=False)
return

def saveTreeOrder(self) -> None:
"""Build a list of the items in the project tree and send them
Expand Down
3 changes: 3 additions & 0 deletions novelwriter/guimain.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,13 @@ def __init__(self) -> None:
self.docEditor.spellCheckStateChanged.connect(self.mainMenu.setSpellCheckState)
self.docEditor.closeDocumentRequest.connect(self.closeDocEditor)
self.docEditor.toggleFocusModeRequest.connect(self.toggleFocusMode)
self.docEditor.requestProjectItemSelected.connect(self.projView.setSelectedHandle)
self.docEditor.requestProjectItemRenamed.connect(self.projView.renameTreeItem)

self.docViewer.documentLoaded.connect(self.docViewerPanel.updateHandle)
self.docViewer.loadDocumentTagRequest.connect(self._followTag)
self.docViewer.togglePanelVisibility.connect(self._toggleViewerPanelVisibility)
self.docViewer.requestProjectItemSelected.connect(self.projView.setSelectedHandle)

self.docViewerPanel.loadDocumentTagRequest.connect(self._followTag)
self.docViewerPanel.openDocumentRequest.connect(self._openDocument)
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

import sys
import pytest
Expand Down
1 change: 1 addition & 0 deletions tests/mocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget
Expand Down
1 change: 1 addition & 0 deletions tests/test_base/test_base_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

import time
from PyQt5.QtCore import QUrl
Expand Down
1 change: 1 addition & 0 deletions tests/test_base/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

import sys
import pytest
Expand Down
1 change: 1 addition & 0 deletions tests/test_base/test_base_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations

import pytest

Expand Down
Loading