Commit 2faffa72 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][assetbrowser] Made the browser more focused on the prefix content

Now:

- Only shows know assets subfolder
- Only show the version of the asset to edit
parent 2375ed9d
......@@ -24,12 +24,63 @@
###############################################################################
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import QSortFilterProxyModel
from PyQt5.QtWidgets import QHeaderView
from PyQt5.QtWidgets import QFileSystemModel
from PyQt5.QtWidgets import QStyledItemDelegate
from PyQt5.QtWidgets import QTreeView
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from ..backend.assetmodel import AssetType
class AssetFilterProxyModel(QSortFilterProxyModel):
"""Filter proxy model showing only asset subfolder"""
def filterAcceptsRow(self, source_row, source_parent):
"""Filters out all sub-folders that are not from a know asset type"""
filesystem_model = self.sourceModel()
root_path = filesystem_model.rootPath()
index = filesystem_model.index(
source_row, self.filterKeyColumn(), source_parent
)
path = filesystem_model.filePath(index)
asset_folder = path[len(root_path) + 1 :].split("/")[0] # noqa E203
if path.startswith(root_path) and len(asset_folder) > 0:
try:
AssetType.from_path(asset_folder)
except RuntimeError:
return False
return super(AssetFilterProxyModel, self).filterAcceptsRow(
source_row, source_parent
)
class AssetItemDelegate(QStyledItemDelegate):
"""Delegate to remove file extension"""
def __init__(self, file_extension, parent=None):
"""Constructor
:param file_extension str: file extension to filter the text from
"""
super(AssetItemDelegate, self).__init__(parent)
self.file_extension = file_extension
def displayText(self, value, locale):
"""Returns the text to display without the configured extension"""
if value.endswith(self.file_extension):
return value.split(".")[0]
return super(AssetItemDelegate, self).displayText(value, locale)
class AssetBrowser(QWidget):
"""
......@@ -42,9 +93,17 @@ class AssetBrowser(QWidget):
"""Constructor"""
super(AssetBrowser, self).__init__(parent)
self.filesystemModel = QFileSystemModel()
self.filesystem_model = QFileSystemModel()
self.filesystem_model.setNameFilters(["*.json"])
self.filesystem_model.setNameFilterDisables(False)
self.proxy_model = AssetFilterProxyModel()
self.proxy_model.setSourceModel(self.filesystem_model)
self.view = QTreeView()
self.view.setModel(self.filesystemModel)
self.view.setModel(self.proxy_model)
self.view.setColumnHidden(1, True)
self.view.setColumnHidden(2, True)
self.view.header().setSectionResizeMode(0, QHeaderView.Stretch)
self.view.setItemDelegateForColumn(0, AssetItemDelegate(".json"))
layout = QVBoxLayout(self)
layout.addWidget(self.view)
......@@ -56,11 +115,13 @@ class AssetBrowser(QWidget):
the corresponding JSON file path.
"""
if self.filesystemModel.type(index).lower() == "json file":
self.json_selected.emit(self.filesystemModel.filePath(index))
source_index = self.proxy_model.mapToSource(index)
if self.filesystem_model.type(source_index).lower() == "json file":
self.json_selected.emit(self.filesystem_model.filePath(source_index))
def set_prefix_root(self, prefix_root_path):
"""Sets the root path of the prefix to edit"""
self.filesystemModel.setRootPath(prefix_root_path)
self.view.setRootIndex(self.filesystemModel.index(prefix_root_path))
self.filesystem_model.setRootPath(prefix_root_path)
root_index = self.filesystem_model.index(prefix_root_path)
self.view.setRootIndex(self.proxy_model.mapFromSource(root_index))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment