From ec01336c212dd7c9733812da0e13991e657aee31 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:06:49 +0100 Subject: [PATCH] Fix active status icon and tooltip --- novelwriter/constants.py | 5 +++++ novelwriter/core/item.py | 8 +++++++ novelwriter/core/itemmodel.py | 12 ++++------- novelwriter/gui/projtree.py | 40 +++++++++++++++++------------------ novelwriter/guimain.py | 18 +++++++++++++--- 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/novelwriter/constants.py b/novelwriter/constants.py index 237b715b..1477ddbd 100644 --- a/novelwriter/constants.py +++ b/novelwriter/constants.py @@ -274,6 +274,11 @@ class nwLabels: "doc_h4": QT_TRANSLATE_NOOP("Constant", "Novel Section"), "note": QT_TRANSLATE_NOOP("Constant", "Project Note"), } + ACTIVE_NAME = { + "checked": QT_TRANSLATE_NOOP("Constant", "Active"), + "unchecked": QT_TRANSLATE_NOOP("Constant", "Inactive"), + "noncheckable": QT_TRANSLATE_NOOP("Constant", "Not Available"), + } KEY_NAME = { nwKeyWords.TAG_KEY: QT_TRANSLATE_NOOP("Constant", "Tag"), nwKeyWords.POV_KEY: QT_TRANSLATE_NOOP("Constant", "Point of View"), diff --git a/novelwriter/core/item.py b/novelwriter/core/item.py index cdfd4a38..2dcc7664 100644 --- a/novelwriter/core/item.py +++ b/novelwriter/core/item.py @@ -29,6 +29,7 @@ from typing import TYPE_CHECKING, Any from PyQt5.QtGui import QIcon +from novelwriter import SHARED from novelwriter.common import ( checkInt, isHandle, isItemClass, isItemLayout, isItemType, simplified, yesNo @@ -330,6 +331,13 @@ class NWItem: entry = self._project.data.itemImport[self._import] return entry.name, entry.icon + def getActiveStatus(self) -> tuple[str, QIcon]: + """Return the relevant active status label and icon for + the current item based on its type. + """ + key = ("checked" if self._active else "unchecked") if self.isFileType() else "noncheckable" + return trConst(nwLabels.ACTIVE_NAME[key]), SHARED.theme.getIcon(key) + ## # Checker Methods ## diff --git a/novelwriter/core/itemmodel.py b/novelwriter/core/itemmodel.py index 87795cb2..86cebc3a 100644 --- a/novelwriter/core/itemmodel.py +++ b/novelwriter/core/itemmodel.py @@ -113,18 +113,14 @@ class ProjectNode: self._cache[C_COUNT_ALIGN] = QtAlignRight # Active - if self._item.isFileType(): - if self._item.isActive: - self._cache[C_ACTIVE_ICON] = SHARED.theme.getIcon("checked") - else: - self._cache[C_ACTIVE_ICON] = SHARED.theme.getIcon("unchecked") - else: - self._cache[C_ACTIVE_ICON] = SHARED.theme.getIcon("noncheckable") + aText, aIcon = self._item.getActiveStatus() + self._cache[C_ACTIVE_TIP] = aText + self._cache[C_ACTIVE_ICON] = aIcon # Status sText, sIcon = self._item.getImportStatus() - self._cache[C_STATUS_ICON] = sIcon self._cache[C_STATUS_TIP] = sText + self._cache[C_STATUS_ICON] = sIcon self.updateCount() diff --git a/novelwriter/gui/projtree.py b/novelwriter/gui/projtree.py index ab723d43..f5a30c6c 100644 --- a/novelwriter/gui/projtree.py +++ b/novelwriter/gui/projtree.py @@ -515,22 +515,8 @@ class GuiProjectTree(QTreeView): self.projView = projView # Internal Variables - # self._treeMap: dict[str, QTreeWidgetItem] = {} - # self._popAlert = None # self._actHandle = None - # Cached Translations - self.trActive = self.tr("Active") - self.trInactive = self.tr("Inactive") - self.trPermDelete = self.tr("Permanently delete {0} file(s) from Trash?") - - # Build GUI - # ========= - - # Context Menu - # self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) - # self.customContextMenuRequested.connect(self.openContextMenu) - # Tree Settings iPx = SHARED.theme.baseIconHeight @@ -551,19 +537,19 @@ class GuiProjectTree(QTreeView): # releases (see #1561) and instead use our own implementation # self.setAutoScroll(False) - # But don't allow drop on root level - # Due to a bug, this stops working somewhere between Qt 5.15.3 - # and 5.15.8, so this is also blocked in dropEvent (see #1569) - # trRoot = self.invisibleRootItem() - # trRoot.setFlags(trRoot.flags() ^ Qt.ItemFlag.ItemIsDropEnabled) - # Set selection options self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) self.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) + # Context Menu + # self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + # self.customContextMenuRequested.connect(self.openContextMenu) + # Connect signals self.clicked.connect(self._onSingleClick) self.doubleClicked.connect(self._onDoubleClick) + self.collapsed.connect(self._onNodeCollapsed) + self.expanded.connect(self._onNodeExpanded) # Auto Scroll # self._scrollMargin = SHARED.theme.baseIconHeight @@ -865,6 +851,20 @@ class GuiProjectTree(QTreeView): self.setExpanded(index, not self.isExpanded(index)) return + @pyqtSlot(QModelIndex) + def _onNodeCollapsed(self, index: QModelIndex) -> None: + """Capture a node collapse, and pass it to the model.""" + if node := self._getNode(index): + node.item.setExpanded(False) + return + + @pyqtSlot(QModelIndex) + def _onNodeExpanded(self, index: QModelIndex) -> None: + """Capture a node expand, and pass it to the model.""" + if node := self._getNode(index): + node.item.setExpanded(True) + return + ## # Internal Functions ## diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index adda022d..2dc5aec6 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -44,7 +44,7 @@ from novelwriter.dialogs.about import GuiAbout from novelwriter.dialogs.preferences import GuiPreferences from novelwriter.dialogs.projectsettings import GuiProjectSettings from novelwriter.dialogs.wordlist import GuiWordList -from novelwriter.enum import nwDocAction, nwDocInsert, nwDocMode, nwFocus, nwView +from novelwriter.enum import nwDocAction, nwDocInsert, nwDocMode, nwFocus, nwItemType, nwView from novelwriter.gui.doceditor import GuiDocEditor from novelwriter.gui.docviewer import GuiDocViewer from novelwriter.gui.docviewerpanel import GuiDocViewerPanel @@ -62,6 +62,7 @@ from novelwriter.tools.manuscript import GuiManuscript from novelwriter.tools.noveldetails import GuiNovelDetails from novelwriter.tools.welcome import GuiWelcome from novelwriter.tools.writingstats import GuiWritingStats +from novelwriter.types import QtModShift logger = logging.getLogger(__name__) @@ -298,10 +299,18 @@ class GuiMain(QMainWindow): self.keyReturn.setKey("Return") self.keyReturn.activated.connect(self._keyPressReturn) + self.keyShiftReturn = QShortcut(self) + self.keyShiftReturn.setKey("Shift+Return") + self.keyShiftReturn.activated.connect(self._keyPressReturn) + self.keyEnter = QShortcut(self) self.keyEnter.setKey("Enter") self.keyEnter.activated.connect(self._keyPressReturn) + self.keyShiftEnter = QShortcut(self) + self.keyShiftEnter.setKey("Shift+Enter") + self.keyShiftEnter.activated.connect(self._keyPressReturn) + self.keyEscape = QShortcut(self) self.keyEscape.setKey("Esc") self.keyEscape.activated.connect(self._keyPressEscape) @@ -719,8 +728,11 @@ class GuiMain(QMainWindow): logger.warning("No item selected") return - if tHandle: - self.openDocument(tHandle, sTitle=sTitle, changeFocus=False, doScroll=False) + if tHandle and SHARED.project.tree.checkType(tHandle, nwItemType.FILE): + if QApplication.keyboardModifiers() == QtModShift: + self.viewDocument(tHandle) + else: + self.openDocument(tHandle, sTitle=sTitle, changeFocus=False, doScroll=False) return