Welcome to mirror list, hosted at ThFree Co, Russian Federation.

generic_ui_list.py « operators « rigify - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46f093b3b6180f95244c7b1094323f41cb02fe04 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# SPDX-License-Identifier: GPL-2.0-or-later

from bpy.types import Operator, UILayout, Context
from bpy.props import EnumProperty, StringProperty


def get_context_attr(context: Context, data_path):
    return context.path_resolve(data_path)


def set_context_attr(context: Context, data_path, value):
    items = data_path.split('.')
    setattr(context.path_resolve('.'.join(items[:-1])), items[-1], value)


class GenericUIListOperator(Operator):
    bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}

    list_context_path: StringProperty()
    active_idx_context_path: StringProperty()

    def get_list(self, context):
        return get_context_attr(context, self.list_context_path)

    def get_active_index(self, context):
        return get_context_attr(context, self.active_idx_context_path)

    def set_active_index(self, context, index):
        set_context_attr(context, self.active_idx_context_path, index)


class UILIST_OT_entry_remove(GenericUIListOperator):
    """Remove the selected entry from the list"""

    bl_idname = "ui.rigify_list_entry_remove"
    bl_label = "Remove Selected Entry"

    def execute(self, context):
        my_list = self.get_list(context)
        active_index = self.get_active_index(context)

        my_list.remove(active_index)

        to_index = min(active_index, len(my_list) - 1)

        self.set_active_index(context, to_index)

        return {'FINISHED'}


class UILIST_OT_entry_add(GenericUIListOperator):
    """Add an entry to the list"""

    bl_idname = "ui.rigify_list_entry_add"
    bl_label = "Add Entry"

    def execute(self, context):
        my_list = self.get_list(context)
        active_index = self.get_active_index(context)

        to_index = min(len(my_list), active_index + 1)

        my_list.add()
        my_list.move(len(my_list) - 1, to_index)
        self.set_active_index(context, to_index)

        return {'FINISHED'}


class UILIST_OT_entry_move(GenericUIListOperator):
    """Move an entry in the list up or down"""

    bl_idname = "ui.rigify_list_entry_move"
    bl_label = "Move Entry"

    direction: EnumProperty(
        name="Direction",
        items=[('UP', 'UP', 'UP'),
               ('DOWN', 'DOWN', 'DOWN')],
        default='UP'
    )

    def execute(self, context):
        my_list = self.get_list(context)
        active_index = self.get_active_index(context)

        to_index = active_index + (1 if self.direction == 'DOWN' else -1)

        if to_index > len(my_list) - 1:
            to_index = 0
        elif to_index < 0:
            to_index = len(my_list) - 1

        my_list.move(active_index, to_index)
        self.set_active_index(context, to_index)

        return {'FINISHED'}


def draw_ui_list(
        layout, context, class_name="UI_UL_list", *,
        list_context_path: str,  # Eg. "object.vertex_groups".
        active_idx_context_path: str,  # Eg., "object.vertex_groups.active_index".
        insertion_operators=True,
        move_operators=True,
        menu_class_name="",
        **kwargs) -> UILayout:
    """
    This is intended as a replacement for row.template_list().
    By changing the requirements of the parameters, we can provide the Add, Remove and Move Up/Down
    operators without the person implementing the UIList having to worry about that stuff.
    """
    row = layout.row()

    list_owner = get_context_attr(context, ".".join(list_context_path.split(".")[:-1]))
    list_prop_name = list_context_path.split(".")[-1]
    idx_owner = get_context_attr(context, ".".join(active_idx_context_path.split(".")[:-1]))
    idx_prop_name = active_idx_context_path.split(".")[-1]

    my_list = get_context_attr(context, list_context_path)

    row.template_list(
        class_name,
        list_context_path if class_name == 'UI_UL_list' else "",
        list_owner, list_prop_name,
        idx_owner, idx_prop_name,
        rows=4 if len(my_list) > 0 else 1,
        **kwargs
    )

    col = row.column()

    if insertion_operators:
        add_op = col.operator(UILIST_OT_entry_add.bl_idname, text="", icon='ADD')
        add_op.list_context_path = list_context_path
        add_op.active_idx_context_path = active_idx_context_path

        row = col.row()
        row.enabled = len(my_list) > 0
        remove_op = row.operator(UILIST_OT_entry_remove.bl_idname, text="", icon='REMOVE')
        remove_op.list_context_path = list_context_path
        remove_op.active_idx_context_path = active_idx_context_path

        col.separator()

    if menu_class_name != '':
        # noinspection SpellCheckingInspection
        col.menu(menu_class_name, icon='DOWNARROW_HLT', text="")
        col.separator()

    if move_operators and len(my_list) > 0:
        col = col.column()
        col.enabled = len(my_list) > 1
        move_up_op = col.operator(UILIST_OT_entry_move.bl_idname, text="", icon='TRIA_UP')
        move_up_op.direction = 'UP'
        move_up_op.list_context_path = list_context_path
        move_up_op.active_idx_context_path = active_idx_context_path

        move_down_op = col.operator(UILIST_OT_entry_move.bl_idname, text="", icon='TRIA_DOWN')
        move_down_op.direction = 'DOWN'
        move_down_op.list_context_path = list_context_path
        move_down_op.active_idx_context_path = active_idx_context_path

    # Return the right-side column.
    return col


# =============================================
# Registration

classes = (
    UILIST_OT_entry_remove,
    UILIST_OT_entry_add,
    UILIST_OT_entry_move,
)


def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)


def unregister():
    from bpy.utils import unregister_class
    for cls in classes:
        unregister_class(cls)