Fix icons not updating on theme switch in viewer panel

This commit is contained in:
Veronica Berglyd Olsen
2023-11-27 16:08:34 +01:00
parent ccc444f355
commit 1f9b6d7624
2 changed files with 49 additions and 4 deletions
+31 -4
View File
@@ -73,7 +73,7 @@ class GuiDocViewerPanel(QWidget):
self.outerBox.setContentsMargins(0, 0, 0, 0) self.outerBox.setContentsMargins(0, 0, 0, 0)
self.setLayout(self.outerBox) self.setLayout(self.outerBox)
self.updateTheme() self.updateTheme(updateTabs=False)
logger.debug("Ready: GuiDocViewerPanel") logger.debug("Ready: GuiDocViewerPanel")
@@ -83,7 +83,7 @@ class GuiDocViewerPanel(QWidget):
# Methods # Methods
## ##
def updateTheme(self) -> None: def updateTheme(self, updateTabs: bool = True) -> None:
"""Update theme elements.""" """Update theme elements."""
vPx = CONFIG.pxInt(4) vPx = CONFIG.pxInt(4)
lPx = CONFIG.pxInt(2) lPx = CONFIG.pxInt(2)
@@ -102,6 +102,11 @@ class GuiDocViewerPanel(QWidget):
self.mainTabs.setStyleSheet(styleSheet) self.mainTabs.setStyleSheet(styleSheet)
self.updateHandle(self._lastHandle) self.updateHandle(self._lastHandle)
if updateTabs:
self.tabBackRefs.updateTheme()
for tab in self.kwTabs.values():
tab.updateTheme()
return return
def openProjectTasks(self) -> None: def openProjectTasks(self) -> None:
@@ -237,6 +242,16 @@ class _ViewPanelBackRefs(QTreeWidget):
return return
def updateTheme(self) -> None:
"""Update theme elements."""
self._editIcon = SHARED.theme.getIcon("edit")
self._viewIcon = SHARED.theme.getIcon("view")
for i in range(self.topLevelItemCount()):
if item := self.topLevelItem(i):
item.setIcon(self.C_EDIT, self._editIcon)
item.setIcon(self.C_VIEW, self._viewIcon)
return
def clearContent(self) -> None: def clearContent(self) -> None:
"""Clear the widget.""" """Clear the widget."""
self.clear() self.clear()
@@ -332,6 +347,7 @@ class _ViewPanelKeyWords(QTreeWidget):
super().__init__(parent=parent) super().__init__(parent=parent)
self._parent = parent self._parent = parent
self._class = itemClass
self._treeMap: dict[str, QTreeWidgetItem] = {} self._treeMap: dict[str, QTreeWidgetItem] = {}
iPx = SHARED.theme.baseIconSize iPx = SHARED.theme.baseIconSize
@@ -372,6 +388,17 @@ class _ViewPanelKeyWords(QTreeWidget):
return return
def updateTheme(self) -> None:
"""Update theme elements."""
self._classIcon = SHARED.theme.getIcon(nwLabels.CLASS_ICON[self._class])
self._editIcon = SHARED.theme.getIcon("edit")
self._viewIcon = SHARED.theme.getIcon("view")
for i in range(self.topLevelItemCount()):
if item := self.topLevelItem(i):
item.setIcon(self.C_EDIT, self._editIcon)
item.setIcon(self.C_VIEW, self._viewIcon)
return
def countEntries(self) -> int: def countEntries(self) -> int:
"""Return the number of items in the list.""" """Return the number of items in the list."""
return self.topLevelItemCount() return self.topLevelItemCount()
@@ -392,8 +419,8 @@ class _ViewPanelKeyWords(QTreeWidget):
iLevel = nwHeaders.H_LEVEL.get(hItem.level, 0) if nwItem.isDocumentLayout() else 5 iLevel = nwHeaders.H_LEVEL.get(hItem.level, 0) if nwItem.isDocumentLayout() else 5
hDec = SHARED.theme.getHeaderDecorationNarrow(iLevel) hDec = SHARED.theme.getHeaderDecorationNarrow(iLevel)
# This can not use a get call to the dictionary as that creates # This can not use a get call to the dictionary as that would create an
# some weird issue with Qt, so we need to do this with an if # instance of the QTreeWidgetItem, which has some weird side effects
trItem = self._treeMap[tag] if tag in self._treeMap else QTreeWidgetItem() trItem = self._treeMap[tag] if tag in self._treeMap else QTreeWidgetItem()
trItem.setText(self.C_NAME, name) trItem.setText(self.C_NAME, name)
+18
View File
@@ -24,6 +24,8 @@ import pytest
from tools import C, buildTestProject from tools import C, buildTestProject
from PyQt5.QtGui import QIcon
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
from novelwriter.constants import nwLists from novelwriter.constants import nwLists
from novelwriter.dialogs.editlabel import GuiEditLabel from novelwriter.dialogs.editlabel import GuiEditLabel
@@ -91,6 +93,13 @@ def testGuiViewerPanel_BackRefs(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
SHARED.project.index.rebuildIndex() SHARED.project.index.rebuildIndex()
assert tabBackRefs.topLevelItemCount() == 1 assert tabBackRefs.topLevelItemCount() == 1
# Test Update Theme
tabBackRefs._editIcon = None
tabBackRefs._viewIcon = None
tabBackRefs.updateTheme()
assert isinstance(tabBackRefs._editIcon, QIcon)
assert isinstance(tabBackRefs._viewIcon, QIcon)
# Click the Edit Button # Click the Edit Button
nwGUI.openDocument(C.hChapterDoc) nwGUI.openDocument(C.hChapterDoc)
assert nwGUI.docEditor.docHandle == C.hChapterDoc assert nwGUI.docEditor.docHandle == C.hChapterDoc
@@ -182,6 +191,15 @@ def testGuiViewerPanel_Tags(qtbot, monkeypatch, caplog, nwGUI, projPath, mockRnd
SHARED.project.index.rebuildIndex() SHARED.project.index.rebuildIndex()
assert charTab.topLevelItemCount() == 2 assert charTab.topLevelItemCount() == 2
# Test Update Theme
charTab._classIcon = None
charTab._editIcon = None
charTab._viewIcon = None
charTab.updateTheme()
assert isinstance(charTab._classIcon, QIcon)
assert isinstance(charTab._editIcon, QIcon)
assert isinstance(charTab._viewIcon, QIcon)
# Remove Non-Existing Tag # Remove Non-Existing Tag
caplog.clear() caplog.clear()
viewPanel.updateChangedTags(["foo"], ["bar"]) viewPanel.updateChangedTags(["foo"], ["bar"])