From e6a396ba3f5d4f61b4f98aadefeed32bae051508 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 24 Nov 2021 21:46:14 +0100 Subject: [PATCH] removed unused asset related classes --- openpype/tools/utils/delegates.py | 172 +------------------ openpype/tools/utils/models.py | 277 ------------------------------ openpype/tools/utils/views.py | 23 --- openpype/tools/utils/widgets.py | 4 +- 4 files changed, 2 insertions(+), 474 deletions(-) diff --git a/openpype/tools/utils/delegates.py b/openpype/tools/utils/delegates.py index 96353c44c6..1caed732d8 100644 --- a/openpype/tools/utils/delegates.py +++ b/openpype/tools/utils/delegates.py @@ -8,10 +8,7 @@ from Qt import QtWidgets, QtGui, QtCore from avalon.lib import HeroVersionType from openpype.style import get_objected_colors -from .models import ( - AssetModel, - TreeModel -) +from .models import TreeModel from . import lib if Qt.__binding__ == "PySide": @@ -22,173 +19,6 @@ elif Qt.__binding__ == "PyQt4": log = logging.getLogger(__name__) -class AssetDelegate(QtWidgets.QItemDelegate): - bar_height = 3 - - def __init__(self, *args, **kwargs): - super(AssetDelegate, self).__init__(*args, **kwargs) - asset_view_colors = get_objected_colors()["loader"]["asset-view"] - self._selected_color = ( - asset_view_colors["selected"].get_qcolor() - ) - self._hover_color = ( - asset_view_colors["hover"].get_qcolor() - ) - self._selected_hover_color = ( - asset_view_colors["selected-hover"].get_qcolor() - ) - - def sizeHint(self, option, index): - result = super(AssetDelegate, self).sizeHint(option, index) - height = result.height() - result.setHeight(height + self.bar_height) - - return result - - def paint(self, painter, option, index): - # Qt4 compat - if Qt.__binding__ in ("PySide", "PyQt4"): - option = QStyleOptionViewItemV4(option) - - painter.save() - - item_rect = QtCore.QRect(option.rect) - item_rect.setHeight(option.rect.height() - self.bar_height) - - subset_colors = index.data(AssetModel.subsetColorsRole) - subset_colors_width = 0 - if subset_colors: - subset_colors_width = option.rect.width() / len(subset_colors) - - subset_rects = [] - counter = 0 - for subset_c in subset_colors: - new_color = None - new_rect = None - if subset_c: - new_color = QtGui.QColor(*subset_c) - - new_rect = QtCore.QRect( - option.rect.left() + (counter * subset_colors_width), - option.rect.top() + ( - option.rect.height() - self.bar_height - ), - subset_colors_width, - self.bar_height - ) - subset_rects.append((new_color, new_rect)) - counter += 1 - - # Background - if option.state & QtWidgets.QStyle.State_Selected: - if len(subset_colors) == 0: - item_rect.setTop(item_rect.top() + (self.bar_height / 2)) - - if option.state & QtWidgets.QStyle.State_MouseOver: - bg_color = self._selected_hover_color - else: - bg_color = self._selected_color - else: - item_rect.setTop(item_rect.top() + (self.bar_height / 2)) - if option.state & QtWidgets.QStyle.State_MouseOver: - bg_color = self._hover_color - else: - bg_color = QtGui.QColor() - bg_color.setAlpha(0) - - # When not needed to do a rounded corners (easier and without - # painter restore): - # painter.fillRect( - # item_rect, - # QtGui.QBrush(bg_color) - # ) - pen = painter.pen() - pen.setStyle(QtCore.Qt.NoPen) - pen.setWidth(0) - painter.setPen(pen) - painter.setBrush(QtGui.QBrush(bg_color)) - painter.drawRoundedRect(option.rect, 3, 3) - - if option.state & QtWidgets.QStyle.State_Selected: - for color, subset_rect in subset_rects: - if not color or not subset_rect: - continue - painter.fillRect(subset_rect, QtGui.QBrush(color)) - - painter.restore() - painter.save() - - # Icon - icon_index = index.model().index( - index.row(), index.column(), index.parent() - ) - # - Default icon_rect if not icon - icon_rect = QtCore.QRect( - item_rect.left(), - item_rect.top(), - # To make sure it's same size all the time - option.rect.height() - self.bar_height, - option.rect.height() - self.bar_height - ) - icon = index.model().data(icon_index, QtCore.Qt.DecorationRole) - - if icon: - mode = QtGui.QIcon.Normal - if not (option.state & QtWidgets.QStyle.State_Enabled): - mode = QtGui.QIcon.Disabled - elif option.state & QtWidgets.QStyle.State_Selected: - mode = QtGui.QIcon.Selected - - if isinstance(icon, QtGui.QPixmap): - icon = QtGui.QIcon(icon) - option.decorationSize = icon.size() / icon.devicePixelRatio() - - elif isinstance(icon, QtGui.QColor): - pixmap = QtGui.QPixmap(option.decorationSize) - pixmap.fill(icon) - icon = QtGui.QIcon(pixmap) - - elif isinstance(icon, QtGui.QImage): - icon = QtGui.QIcon(QtGui.QPixmap.fromImage(icon)) - option.decorationSize = icon.size() / icon.devicePixelRatio() - - elif isinstance(icon, QtGui.QIcon): - state = QtGui.QIcon.Off - if option.state & QtWidgets.QStyle.State_Open: - state = QtGui.QIcon.On - actualSize = option.icon.actualSize( - option.decorationSize, mode, state - ) - option.decorationSize = QtCore.QSize( - min(option.decorationSize.width(), actualSize.width()), - min(option.decorationSize.height(), actualSize.height()) - ) - - state = QtGui.QIcon.Off - if option.state & QtWidgets.QStyle.State_Open: - state = QtGui.QIcon.On - - icon.paint( - painter, icon_rect, - QtCore.Qt.AlignLeft, mode, state - ) - - # Text - text_rect = QtCore.QRect( - icon_rect.left() + icon_rect.width() + 2, - item_rect.top(), - item_rect.width(), - item_rect.height() - ) - - painter.drawText( - text_rect, QtCore.Qt.AlignVCenter, - index.data(QtCore.Qt.DisplayRole) - ) - - painter.restore() - - class VersionDelegate(QtWidgets.QStyledItemDelegate): """A delegate that display version integer formatted as version string.""" diff --git a/openpype/tools/utils/models.py b/openpype/tools/utils/models.py index ffcdc7a820..94694483ab 100644 --- a/openpype/tools/utils/models.py +++ b/openpype/tools/utils/models.py @@ -200,283 +200,6 @@ class Item(dict): self._children.append(child) -class AssetModel(TreeModel): - """A model listing assets in the silo in the active project. - - The assets are displayed in a treeview, they are visually parented by - a `visualParent` field in the database containing an `_id` to a parent - asset. - - """ - - Columns = ["label"] - Name = 0 - Deprecated = 2 - ObjectId = 3 - - DocumentRole = QtCore.Qt.UserRole + 2 - ObjectIdRole = QtCore.Qt.UserRole + 3 - subsetColorsRole = QtCore.Qt.UserRole + 4 - - doc_fetched = QtCore.Signal(bool) - refreshed = QtCore.Signal(bool) - - # Asset document projection - asset_projection = { - "type": 1, - "schema": 1, - "name": 1, - "silo": 1, - "data.visualParent": 1, - "data.label": 1, - "data.tags": 1, - "data.icon": 1, - "data.color": 1, - "data.deprecated": 1 - } - - def __init__(self, dbcon=None, parent=None, asset_projection=None): - super(AssetModel, self).__init__(parent=parent) - if dbcon is None: - dbcon = io - self.dbcon = dbcon - self.asset_colors = {} - - # Projections for Mongo queries - # - let ability to modify them if used in tools that require more than - # defaults - if asset_projection: - self.asset_projection = asset_projection - - self.asset_projection = asset_projection - - self._doc_fetching_thread = None - self._doc_fetching_stop = False - self._doc_payload = {} - - self.doc_fetched.connect(self.on_doc_fetched) - - self.refresh() - - def _add_hierarchy(self, assets, parent=None, silos=None): - """Add the assets that are related to the parent as children items. - - This method does *not* query the database. These instead are queried - in a single batch upfront as an optimization to reduce database - queries. Resulting in up to 10x speed increase. - - Args: - assets (dict): All assets in the currently active silo stored - by key/value - - Returns: - None - - """ - # Reset colors - self.asset_colors = {} - - if silos: - # WARNING: Silo item "_id" is set to silo value - # mainly because GUI issue with perserve selection and expanded row - # and because of easier hierarchy parenting (in "assets") - for silo in silos: - item = Item({ - "_id": silo, - "name": silo, - "label": silo, - "type": "silo" - }) - self.add_child(item, parent=parent) - self._add_hierarchy(assets, parent=item) - - parent_id = parent["_id"] if parent else None - current_assets = assets.get(parent_id, list()) - - for asset in current_assets: - # get label from data, otherwise use name - data = asset.get("data", {}) - label = data.get("label", asset["name"]) - tags = data.get("tags", []) - - # store for the asset for optimization - deprecated = "deprecated" in tags - - item = Item({ - "_id": asset["_id"], - "name": asset["name"], - "label": label, - "type": asset["type"], - "tags": ", ".join(tags), - "deprecated": deprecated, - "_document": asset - }) - self.add_child(item, parent=parent) - - # Add asset's children recursively if it has children - if asset["_id"] in assets: - self._add_hierarchy(assets, parent=item) - - self.asset_colors[asset["_id"]] = [] - - def on_doc_fetched(self, was_stopped): - if was_stopped: - self.stop_fetch_thread() - return - - self.beginResetModel() - - assets_by_parent = self._doc_payload.get("assets_by_parent") - silos = self._doc_payload.get("silos") - if assets_by_parent is not None: - # Build the hierarchical tree items recursively - self._add_hierarchy( - assets_by_parent, - parent=None, - silos=silos - ) - - self.endResetModel() - - has_content = bool(assets_by_parent) or bool(silos) - self.refreshed.emit(has_content) - - self.stop_fetch_thread() - - def fetch(self): - self._doc_payload = self._fetch() or {} - # Emit doc fetched only if was not stopped - self.doc_fetched.emit(self._doc_fetching_stop) - - def _fetch(self): - if not self.dbcon.Session.get("AVALON_PROJECT"): - return - - project_doc = self.dbcon.find_one( - {"type": "project"}, - {"_id": True} - ) - if not project_doc: - return - - # Get all assets sorted by name - db_assets = self.dbcon.find( - {"type": "asset"}, - self.asset_projection - ).sort("name", 1) - - # Group the assets by their visual parent's id - assets_by_parent = collections.defaultdict(list) - for asset in db_assets: - if self._doc_fetching_stop: - return - parent_id = asset.get("data", {}).get("visualParent") - assets_by_parent[parent_id].append(asset) - - return { - "assets_by_parent": assets_by_parent, - "silos": None - } - - def stop_fetch_thread(self): - if self._doc_fetching_thread is not None: - self._doc_fetching_stop = True - while self._doc_fetching_thread.isRunning(): - time.sleep(0.001) - self._doc_fetching_thread = None - - def refresh(self, force=False): - """Refresh the data for the model.""" - # Skip fetch if there is already other thread fetching documents - if self._doc_fetching_thread is not None: - if not force: - return - self.stop_fetch_thread() - - # Clear model items - self.clear() - - # Fetch documents from mongo - # Restart payload - self._doc_payload = {} - self._doc_fetching_stop = False - self._doc_fetching_thread = lib.create_qthread(self.fetch) - self._doc_fetching_thread.start() - - def flags(self, index): - return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable - - def setData(self, index, value, role=QtCore.Qt.EditRole): - if not index.isValid(): - return False - - if role == self.subsetColorsRole: - asset_id = index.data(self.ObjectIdRole) - self.asset_colors[asset_id] = value - - if Qt.__binding__ in ("PyQt4", "PySide"): - self.dataChanged.emit(index, index) - else: - self.dataChanged.emit(index, index, [role]) - - return True - - return super(AssetModel, self).setData(index, value, role) - - def data(self, index, role): - if not index.isValid(): - return - - item = index.internalPointer() - if role == QtCore.Qt.DecorationRole: - column = index.column() - if column == self.Name: - # Allow a custom icon and custom icon color to be defined - data = item.get("_document", {}).get("data", {}) - icon = data.get("icon", None) - if icon is None and item.get("type") == "silo": - icon = "database" - color = data.get("color", style.colors.default) - - if icon is None: - # Use default icons if no custom one is specified. - # If it has children show a full folder, otherwise - # show an open folder - has_children = self.rowCount(index) > 0 - icon = "folder" if has_children else "folder-o" - - # Make the color darker when the asset is deprecated - if item.get("deprecated", False): - color = QtGui.QColor(color).darker(250) - - try: - key = "fa.{0}".format(icon) # font-awesome key - icon = qtawesome.icon(key, color=color) - return icon - except Exception as exception: - # Log an error message instead of erroring out completely - # when the icon couldn't be created (e.g. invalid name) - log.error(exception) - - return - - if role == QtCore.Qt.ForegroundRole: # font color - if "deprecated" in item.get("tags", []): - return QtGui.QColor(style.colors.light).darker(250) - - if role == self.ObjectIdRole: - return item.get("_id", None) - - if role == self.DocumentRole: - return item.get("_document", None) - - if role == self.subsetColorsRole: - asset_id = item.get("_id", None) - return self.asset_colors.get(asset_id) or [] - - return super(AssetModel, self).data(index, role) - - class RecursiveSortFilterProxyModel(QtCore.QSortFilterProxyModel): """Filters to the regex if any of the children matches allow parent""" def filterAcceptsRow(self, row, parent): diff --git a/openpype/tools/utils/views.py b/openpype/tools/utils/views.py index 89e49fe142..97aaf622a4 100644 --- a/openpype/tools/utils/views.py +++ b/openpype/tools/utils/views.py @@ -61,26 +61,3 @@ class TreeViewSpinner(QtWidgets.QTreeView): self.paint_empty(event) else: super(TreeViewSpinner, self).paintEvent(event) - - -class AssetsView(TreeViewSpinner, DeselectableTreeView): - """Item view. - This implements a context menu. - """ - - def __init__(self, parent=None): - super(AssetsView, self).__init__(parent) - self.setIndentation(15) - self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) - self.setHeaderHidden(True) - - def mousePressEvent(self, event): - index = self.indexAt(event.pos()) - if not index.isValid(): - modifiers = QtWidgets.QApplication.keyboardModifiers() - if modifiers == QtCore.Qt.ShiftModifier: - return - elif modifiers == QtCore.Qt.ControlModifier: - return - - super(AssetsView, self).mousePressEvent(event) diff --git a/openpype/tools/utils/widgets.py b/openpype/tools/utils/widgets.py index 8346db211b..ea0d23470c 100644 --- a/openpype/tools/utils/widgets.py +++ b/openpype/tools/utils/widgets.py @@ -9,9 +9,7 @@ from avalon.vendor import qtawesome, qargparse from avalon import style from openpype.style import get_objected_colors -from .models import AssetModel, RecursiveSortFilterProxyModel -from .views import AssetsView -from .delegates import AssetDelegate +from .models import RecursiveSortFilterProxyModel log = logging.getLogger(__name__)