Fix a few remaining issues with icon refresh

This commit is contained in:
Veronica Berglyd Olsen
2025-01-08 01:16:31 +01:00
parent 553958b952
commit f4bdf3be71
2 changed files with 24 additions and 34 deletions
+23 -34
View File
@@ -235,6 +235,7 @@ class GuiTheme:
# Reset Palette # Reset Palette
self._guiPalette = QApplication.style().standardPalette() self._guiPalette = QApplication.style().standardPalette()
self._resetGuiColors() self._resetGuiColors()
self.iconCache.clear()
# Main # Main
sec = "Main" sec = "Main"
@@ -486,33 +487,17 @@ class GuiTheme:
class GuiIcons: class GuiIcons:
"""The icon class manages the content of the assets/icons folder, """The icon class manages the content of the assets/icons folder,
and provides a simple interface for requesting icons. Only icons and provides a simple interface for requesting icons.
listed in the ICON_KEYS are handled.
Icons are loaded on first request, and then cached for further Icons are generated from SVG on first request, and then cached for
requests. Each icon key in the ICON_KEYS set has standard icon set further requests. If the icon is not defined, a placeholder icon is
in the icon theme conf file. The existence of the file, and the returned instead.
definition of all keys are checked when the theme is loaded.
When an icon is requested, the icon is loaded and cached. If it is
missing, a blank icon is returned and a warning issued.
""" """
ICON_KEYS: set[str] = {
# Project and GUI Icons
"novelwriter", "proj_nwx"
# Decorations
"deco_doc_h0", "deco_doc_h1", "deco_doc_h2", "deco_doc_h3", "deco_doc_h4", "deco_doc_more",
"deco_doc_h0_n", "deco_doc_h1_n", "deco_doc_h2_n", "deco_doc_h3_n", "deco_doc_h4_n",
"deco_doc_nt_n",
}
TOGGLE_ICON_KEYS: dict[str, tuple[str, str]] = { TOGGLE_ICON_KEYS: dict[str, tuple[str, str]] = {
"bullet": ("bullet-on", "bullet-off"), "bullet": ("bullet-on", "bullet-off"),
"unfold": ("unfold-show", "unfold-hide"), "unfold": ("unfold-show", "unfold-hide"),
} }
IMAGE_MAP: dict[str, tuple[str, str]] = { IMAGE_MAP: dict[str, tuple[str, str]] = {
"welcome": ("welcome-light.jpg", "welcome-dark.jpg"), "welcome": ("welcome-light.jpg", "welcome-dark.jpg"),
"nw-text": ("novelwriter-text-light.svg", "novelwriter-text-dark.svg"), "nw-text": ("novelwriter-text-light.svg", "novelwriter-text-dark.svg"),
@@ -542,6 +527,18 @@ class GuiIcons:
return return
def clear(self) -> None:
"""Clear the icon cache."""
self._svgData = {}
self._svgColours = {}
self._qIcons = {}
self._headerDec = []
self._headerDecNarrow = []
self.themeName = ""
self.themeAuthor = ""
self.themeLicense = ""
return
## ##
# Actions # Actions
## ##
@@ -569,19 +566,10 @@ class GuiIcons:
elif key == "meta:license": elif key == "meta:license":
self.themeLicense = value self.themeLicense = value
except Exception: except Exception:
logger.error("Could not load icon theme settings from: %s", themePath) logger.error("Could not load icon theme from: %s", themePath)
logException() logException()
return False return False
# Refresh icons
for iconKey in self._qIcons:
logger.debug("Reloading icon: '%s'", iconKey)
qIcon = self._loadIcon(iconKey)
self._qIcons[iconKey] = qIcon
self._headerDec = []
self._headerDecNarrow = []
return True return True
def setIconColor(self, key: str, color: QColor) -> None: def setIconColor(self, key: str, color: QColor) -> None:
@@ -631,7 +619,7 @@ class GuiIcons:
return icon return icon
def getToggleIcon(self, name: str, size: tuple[int, int], color: str | None = None) -> QIcon: def getToggleIcon(self, name: str, size: tuple[int, int], color: str | None = None) -> QIcon:
"""Return a toggle icon from the icon buffer. or load it.""" """Return a toggle icon from the icon buffer, or load it."""
if name in self.TOGGLE_ICON_KEYS: if name in self.TOGGLE_ICON_KEYS:
pOne = self.getPixmap(self.TOGGLE_ICON_KEYS[name][0], size, color) pOne = self.getPixmap(self.TOGGLE_ICON_KEYS[name][0], size, color)
pTwo = self.getPixmap(self.TOGGLE_ICON_KEYS[name][1], size, color) pTwo = self.getPixmap(self.TOGGLE_ICON_KEYS[name][1], size, color)
@@ -648,8 +636,9 @@ class GuiIcons:
w, h = size w, h = size
return self.getIcon(name, color, w, h).pixmap(w, h, QIcon.Mode.Normal) return self.getIcon(name, color, w, h).pixmap(w, h, QIcon.Mode.Normal)
def getItemIcon(self, tType: nwItemType, tClass: nwItemClass, def getItemIcon(
tLayout: nwItemLayout, hLevel: str = "H0") -> QIcon: self, tType: nwItemType, tClass: nwItemClass, tLayout: nwItemLayout, hLevel: str = "H0"
) -> QIcon:
"""Get the correct icon for a project item based on type, class """Get the correct icon for a project item based on type, class
and heading level and heading level
""" """
@@ -772,7 +761,7 @@ def _loadInternalName(confParser: NWConfigParser, confFile: str | Path) -> str:
def _loadIconName(path: Path) -> str: def _loadIconName(path: Path) -> str:
"""Open an icons file and read the 'name' setting.""" """Open an icons file and read the name setting."""
try: try:
with open(path, mode="r", encoding="utf-8") as icons: with open(path, mode="r", encoding="utf-8") as icons:
for icon in icons: for icon in icons:
+1
View File
@@ -1064,6 +1064,7 @@ class GuiMain(QMainWindow):
self.outlineView.updateTheme() self.outlineView.updateTheme()
self.itemDetails.updateTheme() self.itemDetails.updateTheme()
self.mainStatus.updateTheme() self.mainStatus.updateTheme()
SHARED.project.tree.refreshAllItems()
if syntax: if syntax:
SHARED.theme.loadSyntax() SHARED.theme.loadSyntax()