Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loader: Add submenu to group loaders in load context menu: #1146

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 64 additions & 27 deletions client/ayon_core/tools/loader/ui/actions_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import itertools
import uuid
from typing import List

from qtpy import QtWidgets, QtGui
from qtpy import QtWidgets, QtGui, QtCore
import qtawesome

from ayon_core.lib.attribute_definitions import AbstractAttrDef
Expand All @@ -10,13 +13,21 @@
OptionalAction,
OptionDialog,
)
from ayon_core.tools.loader.models.actions import ActionItem
from ayon_core.tools.utils import get_qt_icon


def show_actions_menu(action_items, global_point, one_item_selected, parent):
def show_actions_menu(
action_items: List[ActionItem],
global_point: QtCore.QPoint,
one_item_selected: bool,
parent: QtCore.QObject):
selected_action_item = None
selected_options = None

# Can be any of `never`, `multiples`, `always`
grouping_mode = os.getenv("AYON_LOADER_GROUP_MODE", "always")

if not action_items:
menu = QtWidgets.QMenu(parent)
action = _get_no_loader_action(menu, one_item_selected)
Expand All @@ -27,30 +38,56 @@ def show_actions_menu(action_items, global_point, one_item_selected, parent):
menu = OptionalMenu(parent)

action_items_by_id = {}
for action_item in action_items:
item_id = uuid.uuid4().hex
action_items_by_id[item_id] = action_item
item_options = action_item.options
icon = get_qt_icon(action_item.icon)
use_option = bool(item_options)
action = OptionalAction(
action_item.label,
icon,
use_option,
menu
)
if use_option:
# Add option box tip
action.set_option_tip(item_options)

tip = action_item.tooltip
if tip:
action.setToolTip(tip)
action.setStatusTip(tip)

action.setData(item_id)

menu.addAction(action)
for key, group in itertools.groupby(
action_items, key=lambda x: x.identifier):
group_action_items: List[ActionItem] = list(group)

target_menu = menu

is_grouped = True
if grouping_mode == "never":
# Never group
is_grouped = False
if grouping_mode == "multiples":
# Only group if more than one action item from a loader
is_grouped = len(group_action_items) > 1
elif grouping_mode == "always":
# Always
is_grouped = True

if is_grouped:
# Attach to a submenu
label = group_action_items[0].label.rsplit(" (", 1)[0]
icon = get_qt_icon(group_action_items[0].icon)
target_menu = menu.addMenu(icon, label)

for action_item in group_action_items:
item_id = uuid.uuid4().hex
action_items_by_id[item_id] = action_item
item_options = action_item.options
icon = get_qt_icon(action_item.icon)
use_option = bool(item_options)
label = action_item.label
if is_grouped:
label = label.rsplit(" (", 1)[-1].rstrip(") ")
action = OptionalAction(
label,
icon,
use_option,
target_menu
)
if use_option:
# Add option box tip
action.set_option_tip(item_options)

tip = action_item.tooltip
if tip:
action.setToolTip(tip)
action.setStatusTip(tip)

action.setData(item_id)

target_menu.addAction(action)

action = menu.exec_(global_point)
if action is not None:
Expand Down Expand Up @@ -103,7 +140,7 @@ def _get_options(action, action_item, parent):
return dialog.get_values()


def _get_no_loader_action(menu, one_item_selected):
def _get_no_loader_action(menu: QtWidgets.QMenu, one_item_selected: bool):
"""Creates dummy no loader option in 'menu'"""

if one_item_selected:
Expand Down
6 changes: 4 additions & 2 deletions client/ayon_core/tools/utils/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,13 +830,15 @@ def mouseMoveEvent(self, event):
"""Add highlight to active action"""
active = self.actionAt(event.pos())
for action in self.actions():
action.set_highlight(action is active, event.globalPos())
if isinstance(action, OptionalAction):
action.set_highlight(action is active, event.globalPos())
super(OptionalMenu, self).mouseMoveEvent(event)

def leaveEvent(self, event):
"""Remove highlight from all actions"""
for action in self.actions():
action.set_highlight(False)
if isinstance(action, OptionalAction):
action.set_highlight(False)
super(OptionalMenu, self).leaveEvent(event)


Expand Down