From 92122bbe03bdffe43a712691bc9f0df8e254b2aa Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 12 Aug 2020 22:23:14 +0200 Subject: [PATCH] moved icon caching to lib --- pype/tools/launcher/lib.py | 38 +++++++++++++++++++++++++++++++++++ pype/tools/launcher/models.py | 31 ++-------------------------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/pype/tools/launcher/lib.py b/pype/tools/launcher/lib.py index 8cd117074c..033ac33d66 100644 --- a/pype/tools/launcher/lib.py +++ b/pype/tools/launcher/lib.py @@ -14,7 +14,14 @@ provides a bridge between the file-based project inventory and configuration. """ +import os +from Qt import QtGui from avalon import io, lib, pipeline +from avalon.vendor import qtawesome +from pype.api import resources + +ICON_CACHE = {} +NOT_FOUND = type("NotFound", (object, ), {}) def list_project_tasks(): @@ -65,3 +72,34 @@ def get_application_actions(project): apps.append(action) return apps + + +def get_action_icon(self, action, skip_default=False): + icon_name = action.icon + if not icon_name: + return None + + global ICON_CACHE + + icon = ICON_CACHE.get(icon_name) + if icon is NOT_FOUND: + return None + elif icon: + return icon + + icon_path = resources.get_resource(icon_name) + if os.path.exists(icon_path): + icon = QtGui.QIcon(icon_path) + ICON_CACHE[icon_name] = icon + return icon + + try: + icon_color = getattr(action, "color", None) or "white" + icon = qtawesome.icon( + "fa.{}".format(icon_name), color=icon_color + ) + + except Exception: + print("Can't load icon \"{}\"".format(icon_name)) + + return icon diff --git a/pype/tools/launcher/models.py b/pype/tools/launcher/models.py index 7ad161236f..ce6e0c722e 100644 --- a/pype/tools/launcher/models.py +++ b/pype/tools/launcher/models.py @@ -7,7 +7,6 @@ from . import lib from Qt import QtCore, QtGui from avalon.vendor import qtawesome from avalon import io, style, api -from pype.api import resources log = logging.getLogger(__name__) @@ -112,8 +111,6 @@ class ActionModel(QtGui.QStandardItemModel): def __init__(self, parent=None): super(ActionModel, self).__init__(parent=parent) - self._icon_cache = {} - self._group_icon_cache = {} self._session = {} self._groups = {} self.default_icon = qtawesome.icon("fa.cube", color="white") @@ -139,33 +136,9 @@ class ActionModel(QtGui.QStandardItemModel): self._registered_actions = actions def get_icon(self, action, skip_default=False): - icon_name = action.icon - if not icon_name: - if skip_default: - return None + icon = lib.get_action_icon(action) + if not icon and not skip_default: return self.default_icon - - icon = self._icon_cache.get(icon_name) - if icon: - return icon - - icon = self.default_icon - icon_path = resources.get_resource(icon_name) - if os.path.exists(icon_path): - icon = QtGui.QIcon(icon_path) - self._icon_cache[icon_name] = icon - return icon - - try: - icon_color = getattr(action, "color", None) or "white" - icon = qtawesome.icon( - "fa.{}".format(icon_name), color=icon_color - ) - - except Exception: - print("Can't load icon \"{}\"".format(icon_name)) - - self._icon_cache[icon_name] = self.default_icon return icon def refresh(self):