moved few widgets to tools/utils and modified asset/task widgets to easily change source model

This commit is contained in:
iLLiCiTiT 2022-01-18 17:22:08 +01:00
parent 396bdfde8a
commit fbdd1d8ab5
10 changed files with 146 additions and 122 deletions

View file

@ -3,6 +3,8 @@ from .widgets import (
BaseClickableFrame,
ClickableFrame,
ExpandBtn,
PixmapLabel,
IconButton,
)
from .error_dialog import ErrorMessageBox
@ -11,15 +13,22 @@ from .lib import (
paint_image_with_color
)
from .models import (
RecursiveSortFilterProxyModel,
)
__all__ = (
"PlaceholderLineEdit",
"BaseClickableFrame",
"ClickableFrame",
"ExpandBtn",
"PixmapLabel",
"IconButton",
"ErrorMessageBox",
"WrappedCallbackItem",
"paint_image_with_color",
"RecursiveSortFilterProxyModel",
)

View file

@ -635,9 +635,10 @@ class AssetsWidget(QtWidgets.QWidget):
selection_model = view.selectionModel()
selection_model.selectionChanged.connect(self._on_selection_change)
refresh_btn.clicked.connect(self.refresh)
current_asset_btn.clicked.connect(self.set_current_session_asset)
current_asset_btn.clicked.connect(self._on_current_asset_click)
view.doubleClicked.connect(self.double_clicked)
self._refresh_btn = refresh_btn
self._current_asset_btn = current_asset_btn
self._model = model
self._proxy = proxy
@ -668,11 +669,30 @@ class AssetsWidget(QtWidgets.QWidget):
def stop_refresh(self):
self._model.stop_refresh()
def _get_current_session_asset(self):
return self.dbcon.Session.get("AVALON_ASSET")
def _on_current_asset_click(self):
"""Trigger change of asset to current context asset.
This separation gives ability to override this method and use it
in differnt way.
"""
self.set_current_session_asset()
def set_current_session_asset(self):
asset_name = self.dbcon.Session.get("AVALON_ASSET")
asset_name = self._get_current_session_asset()
if asset_name:
self.select_asset_by_name(asset_name)
def set_refresh_btn_visibility(self, visible=None):
"""Hide set refresh button.
Some tools may have their global refresh button or do not support
refresh at all.
"""
if visible is None:
visible = not self._refresh_btn.isVisible()
self._refresh_btn.setVisible(visible)
def set_current_asset_btn_visibility(self, visible=None):
"""Hide set current asset button.
@ -727,6 +747,10 @@ class AssetsWidget(QtWidgets.QWidget):
def _set_loading_state(self, loading, empty):
self._view.set_loading_state(loading, empty)
def _clear_selection(self):
selection_model = self._view.selectionModel()
selection_model.clearSelection()
def _select_indexes(self, indexes):
valid_indexes = [
index

View file

@ -199,31 +199,37 @@ class Item(dict):
class RecursiveSortFilterProxyModel(QtCore.QSortFilterProxyModel):
"""Filters to the regex if any of the children matches allow parent"""
def filterAcceptsRow(self, row, parent):
"""Recursive proxy model.
Item is not filtered if any children match the filter.
Use case: Filtering by string - parent won't be filtered if does not match
the filter string but first checks if any children does.
"""
def filterAcceptsRow(self, row, parent_index):
regex = self.filterRegExp()
if not regex.isEmpty():
pattern = regex.pattern()
model = self.sourceModel()
source_index = model.index(row, self.filterKeyColumn(), parent)
source_index = model.index(
row, self.filterKeyColumn(), parent_index
)
if source_index.isValid():
pattern = regex.pattern()
# Check current index itself
key = model.data(source_index, self.filterRole())
if re.search(pattern, key, re.IGNORECASE):
value = model.data(source_index, self.filterRole())
if re.search(pattern, value, re.IGNORECASE):
return True
# Check children
rows = model.rowCount(source_index)
for i in range(rows):
if self.filterAcceptsRow(i, source_index):
for idx in range(rows):
if self.filterAcceptsRow(idx, source_index):
return True
# Otherwise filter it
return False
return super(
RecursiveSortFilterProxyModel, self
).filterAcceptsRow(row, parent)
return super(RecursiveSortFilterProxyModel, self).filterAcceptsRow(
row, parent_index
)
class ProjectModel(QtGui.QStandardItemModel):

View file

@ -255,6 +255,10 @@ class TasksWidget(QtWidgets.QWidget):
# Force a task changed emit.
self.task_changed.emit()
def _clear_selection(self):
selection_model = self._tasks_view.selectionModel()
selection_model.clearSelection()
def select_task_name(self, task_name):
"""Select a task by name.
@ -285,6 +289,10 @@ class TasksWidget(QtWidgets.QWidget):
self._tasks_view.setCurrentIndex(index)
break
last_selected_task_name = self.get_selected_task_name()
if last_selected_task_name:
self._last_selected_task_name = last_selected_task_name
def get_selected_task_name(self):
"""Return name of task at current index (selected)

View file

@ -148,6 +148,65 @@ class ImageButton(QtWidgets.QPushButton):
return self.iconSize()
class IconButton(QtWidgets.QPushButton):
"""PushButton with icon and size of font.
Using font metrics height as icon size reference.
"""
def __init__(self, *args, **kwargs):
super(IconButton, self).__init__(*args, **kwargs)
self.setObjectName("IconButton")
def sizeHint(self):
result = super(IconButton, self).sizeHint()
icon_h = self.iconSize().height()
font_height = self.fontMetrics().height()
text_set = bool(self.text())
if not text_set and icon_h < font_height:
new_size = result.height() - icon_h + font_height
result.setHeight(new_size)
result.setWidth(new_size)
return result
class PixmapLabel(QtWidgets.QLabel):
"""Label resizing image to height of font."""
def __init__(self, pixmap, parent):
super(PixmapLabel, self).__init__(parent)
self._empty_pixmap = QtGui.QPixmap(0, 0)
self._source_pixmap = pixmap
def set_source_pixmap(self, pixmap):
"""Change source image."""
self._source_pixmap = pixmap
self._set_resized_pix()
def _get_pix_size(self):
size = self.fontMetrics().height()
size += size % 2
return size, size
def _set_resized_pix(self):
if self._source_pixmap is None:
self.setPixmap(self._empty_pixmap)
return
width, height = self._get_pix_size()
self.setPixmap(
self._source_pixmap.scaled(
width,
height,
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation
)
)
def resizeEvent(self, event):
self._set_resized_pix()
super(PixmapLabel, self).resizeEvent(event)
class OptionalMenu(QtWidgets.QMenu):
"""A subclass of `QtWidgets.QMenu` to work with `OptionalAction`