aboutsummaryrefslogtreecommitdiff
path: root/scripts/artv2/tools/rigging/rig_builder/available_components_widget.py
blob: 109bb598cb76c6ebe3bcc1dc7e6c51313e219b09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-

"""
:author:
    Jeremy Ernst
:description:
    This module contains a class that is a menu widget that attaches to the "Add Component" button in the RigBuilderUI
    class. This menu widget searches for component classes, and adds them to the menu.
"""

from functools import partial
from artv2.third_party.Qt import QtWidgets, QtCore, QtGui
import artv2.tools.rigging.rig_builder.add_component_widget as add_component
import artv2.utilities.component_utilities as component_utils
import artv2.tools.system.logger.output_logger as logger


class AvailableComponentsWidget(QtWidgets.QMenu):
    """
    This class is a custom QMenu widget that attaches the to "Add Component" button in the RigBuilderUI instance. It
    is populated with available component classes as found in the artv2.components directory.

    :param parent: the parent widget of this class, which will be a RigBuilderUI instance.

    .. figure:: /images/available_components_widget.png
        :width: 197px
        :align: center
        :height: 334px
        :figclass: align-center

        The area in red is the AvailableComponentsWidget class. It is instantiated from the RigBuilderUI and attached
        to the QPushButton at the top.
    """

    component_added = QtCore.Signal()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def __init__(self, parent=None):

        super(AvailableComponentsWidget, self).__init__(parent)

        self.logger = logger.OutputLogger(self.__class__.__name__)
        self.setMinimumWidth(200)
        self.setMaximumWidth(200)

        self._build_menu()

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _build_menu(self):

        components = component_utils.get_available_components()

        categories = {}
        for each in components:
            data = components.get(each)

            if data[0] not in categories.keys():
                categories[data[0]] = [each]
            else:
                existing_data = list(categories.get(data[0]))
                existing_data.append(each)
                categories[data[0]] = existing_data

        for category in categories:

            header = QtWidgets.QAction(category, self)
            header.setDisabled(True)
            self.addAction(header)
            self.addSeparator()

            category_data = categories.get(category)
            for each in category_data:

                component_data = components.get(each)
                component = QtWidgets.QAction("    {0}".format(each), self)
                component.setData(component_data)
                component.triggered.connect(partial(self._add_component, component))
                self.addAction(component)

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    def _add_component(self, action):

        data = action.data()
        self.logger.interface("{0}._add_component: Adding component {1}".format(self.__class__.__name__,
                                                                                data[2].__name__))

        widget = add_component.AddComponentWidget(data, self, self.parent())
        pos = QtGui.QCursor.pos()
        widget.move(pos.x() + 10, pos.y() + 10)
        widget.show()