Skip to content

tree_view

TreeViewDrawer: branch indicators and indentation for QTreeView.

TreeViewDrawer

AYONStyle drawer for QTreeView.

Handles branch expand/collapse indicators and the indentation metric using colours from the QTreeView style data in ayon_style.json.

Source code in client/ayon_ui_qt/drawers/tree_view.py
 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class TreeViewDrawer:
    """AYONStyle drawer for QTreeView.

    Handles branch expand/collapse indicators and the indentation metric
    using colours from the QTreeView style data in ayon_style.json.
    """

    def __init__(self, style_inst: AYONStyle) -> None:
        self.style_inst = style_inst
        self.model = style_inst.model
        self._icon_cache = {}

    @property
    def base_class(self):
        return {"QTreeView": QTreeView}

    def register_drawers(self) -> dict:
        """Register drawing functions for QTreeView primitives."""
        return {
            enum_to_str(
                QStyle.PrimitiveElement,
                QStyle.PrimitiveElement.PE_IndicatorBranch,
                "QTreeView",
            ): self.draw_branch_indicator,
            enum_to_str(
                QStyle.PrimitiveElement,
                QStyle.PrimitiveElement.PE_PanelScrollAreaCorner,
                "QTreeView",
            ): self.draw_scrollbar_corner,
        }

    def register_metrics(self) -> dict:
        """Register pixel metric functions for QTreeView."""
        return {
            enum_to_str(
                QStyle.PixelMetric,
                QStyle.PixelMetric.PM_TreeViewIndentation,
                "QTreeView",
            ): self.get_metric,
        }

    def get_metric(
        self,
        metric: QStyle.PixelMetric,
        opt: QStyleOption | None = None,
        widget: QWidget | None = None,
    ) -> int:
        """Return indent width from style data.

        Args:
            metric: The pixel metric being queried.
            opt: Optional style option.
            widget: The target widget.

        Returns:
            The indent size in pixels.
        """
        if metric == QStyle.PixelMetric.PM_TreeViewIndentation:
            variant = getattr(widget, "_variant_str", "default")
            style = self.model.get_style("QTreeView", variant)
            return int(style.get("indent", 20))
        return 0

    def _draw_cell_border(
        self,
        painter: QPainter,
        rect: QRect,
        style: dict,
    ) -> None:
        """Draw top and bottom border lines for an AYTableView cell."""
        painter.setPen(
            QPen(
                QColor(style.get("border-color")), style.get("border-width", 1)
            )
        )
        painter.setBrush(Qt.BrushStyle.NoBrush)
        painter.drawLines(
            [
                rect.topLeft(),
                rect.topRight(),
                rect.bottomLeft(),
                rect.bottomRight(),
            ]
        )

    def _resolve_tree_view(self, widget: QWidget | None) -> QWidget | None:
        """Resolve widget to the actual QTreeView/AYTableView."""
        if widget is not None and not isinstance(widget, QTreeView):
            return widget.parent() or widget
        return widget

    def _paint_cell_background(
        self,
        painter: QPainter,
        rect: QRect,
        style: dict,
        is_table: bool,
        is_base_state: bool = False,
    ) -> None:
        """Paint background fill and optional cell borders.

        Args:
            painter: The QPainter to draw on.
            rect: The rectangle to fill.
            style: The style data dictionary.
            is_table: Whether this is an AYTableView cell.
            is_base_state: If True and is_table, use 'background-color-item'.
        """
        painter.save()
        if is_table and is_base_state:
            bg_key = "background-color-item"
        else:
            bg_key = "background-color"
        painter.fillRect(rect, QColor(style.get(bg_key, "transparent")))
        if is_table:
            self._draw_cell_border(painter, rect, style)
        painter.restore()

    def _paint_icon(
        self,
        painter: QPainter,
        rect: QRect,
        icon,
        icon_size: int | None,
    ) -> None:
        """Paint a cached icon, optionally resizing and repositioning it."""
        draw_rect = QRect(rect)
        if icon_size is not None:
            center = rect.center()
            draw_rect.setSize(QSize(icon_size, icon_size))
            draw_rect.moveTo(
                rect.right() - icon_size, center.y() - icon_size // 2
            )
        icon.paint(painter, draw_rect)

    def _paint_fallback_arrow(
        self,
        painter: QPainter,
        rect: QRect,
        color: QColor,
        is_open: bool,
    ) -> None:
        """Paint a geometric triangle arrow when no icon is configured."""
        painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(QBrush(color))

        cx, cy = rect.center().x(), rect.center().y()
        size = max(4, min(rect.width(), rect.height()) // 3)

        path = QPainterPath()
        if is_open:
            path.moveTo(cx - size, cy - size // 2)
            path.lineTo(cx + size, cy - size // 2)
            path.lineTo(cx, cy + size // 2)
        else:
            path.moveTo(cx - size // 2, cy - size)
            path.lineTo(cx - size // 2, cy + size)
            path.lineTo(cx + size // 2, cy)
        path.closeSubpath()
        painter.drawPath(path)

    def draw_branch_indicator(
        self,
        option: QStyleOption,
        painter: QPainter,
        widget: QWidget | None = None,
    ) -> None:
        """Draw expand / collapse arrows for tree branch items.

        Args:
            option: The primitive element style option.
            painter: The QPainter to draw on.
            widget: The QTreeView widget (may be the viewport).
        """
        has_children = bool(option.state & QStyle.StateFlag.State_Children)
        tv = self._resolve_tree_view(widget)
        is_table = type(tv).__name__ == "AYTableView"
        is_selected = bool(option.state & QStyle.StateFlag.State_Selected)
        is_hovered = bool(option.state & QStyle.StateFlag.State_MouseOver)
        variant = getattr(tv, "_variant_str", "default")

        state_name = (
            "selected" if is_selected else "hover" if is_hovered else "base"
        )

        widget_class = "AYTableView" if is_table else "QTreeView"
        t_style = self.model.get_style(
            widget_class, variant=variant, state=state_name
        )

        # Items without children only need background/border painting
        if not has_children:
            self._paint_cell_background(
                painter,
                option.rect,
                t_style,
                is_table,
                is_base_state=(state_name == "base"),
            )
            return

        is_open = bool(option.state & QStyle.StateFlag.State_Open)
        color = QColor(t_style.get("branch-indicator-color", "#8b9198"))
        icon_name = t_style.get(
            "expanded-icon-name" if is_open else "expand-icon-name"
        )

        # Paint background for items with children
        self._paint_cell_background(painter, option.rect, t_style, is_table)

        if icon_name:
            key = f"{icon_name}-{color.name()}"
            if key not in self._icon_cache:
                self._icon_cache[key] = get_icon(icon_name, color=color)
            icon_size = t_style.get("expand-icon-size")
            self._paint_icon(
                painter, option.rect, self._icon_cache[key], icon_size
            )
        else:
            self._paint_fallback_arrow(painter, option.rect, color, is_open)

    def draw_scrollbar_corner(
        self,
        option: QStyleOption,
        painter: QPainter,
        widget: QWidget | None = None,
    ) -> None:
        style = self.model.get_style("QScrollArea", variant="default")
        style.set_context(widget)
        painter.save()
        # Draw corner background
        bg = style.get("background-color", "transparent")
        painter.fillRect(option.rect, QColor(bg))

        painter.restore()

draw_branch_indicator(option, painter, widget=None)

Draw expand / collapse arrows for tree branch items.

Parameters:

Name Type Description Default
option QStyleOption

The primitive element style option.

required
painter QPainter

The QPainter to draw on.

required
widget QWidget | None

The QTreeView widget (may be the viewport).

None
Source code in client/ayon_ui_qt/drawers/tree_view.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
def draw_branch_indicator(
    self,
    option: QStyleOption,
    painter: QPainter,
    widget: QWidget | None = None,
) -> None:
    """Draw expand / collapse arrows for tree branch items.

    Args:
        option: The primitive element style option.
        painter: The QPainter to draw on.
        widget: The QTreeView widget (may be the viewport).
    """
    has_children = bool(option.state & QStyle.StateFlag.State_Children)
    tv = self._resolve_tree_view(widget)
    is_table = type(tv).__name__ == "AYTableView"
    is_selected = bool(option.state & QStyle.StateFlag.State_Selected)
    is_hovered = bool(option.state & QStyle.StateFlag.State_MouseOver)
    variant = getattr(tv, "_variant_str", "default")

    state_name = (
        "selected" if is_selected else "hover" if is_hovered else "base"
    )

    widget_class = "AYTableView" if is_table else "QTreeView"
    t_style = self.model.get_style(
        widget_class, variant=variant, state=state_name
    )

    # Items without children only need background/border painting
    if not has_children:
        self._paint_cell_background(
            painter,
            option.rect,
            t_style,
            is_table,
            is_base_state=(state_name == "base"),
        )
        return

    is_open = bool(option.state & QStyle.StateFlag.State_Open)
    color = QColor(t_style.get("branch-indicator-color", "#8b9198"))
    icon_name = t_style.get(
        "expanded-icon-name" if is_open else "expand-icon-name"
    )

    # Paint background for items with children
    self._paint_cell_background(painter, option.rect, t_style, is_table)

    if icon_name:
        key = f"{icon_name}-{color.name()}"
        if key not in self._icon_cache:
            self._icon_cache[key] = get_icon(icon_name, color=color)
        icon_size = t_style.get("expand-icon-size")
        self._paint_icon(
            painter, option.rect, self._icon_cache[key], icon_size
        )
    else:
        self._paint_fallback_arrow(painter, option.rect, color, is_open)

get_metric(metric, opt=None, widget=None)

Return indent width from style data.

Parameters:

Name Type Description Default
metric PixelMetric

The pixel metric being queried.

required
opt QStyleOption | None

Optional style option.

None
widget QWidget | None

The target widget.

None

Returns:

Type Description
int

The indent size in pixels.

Source code in client/ayon_ui_qt/drawers/tree_view.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_metric(
    self,
    metric: QStyle.PixelMetric,
    opt: QStyleOption | None = None,
    widget: QWidget | None = None,
) -> int:
    """Return indent width from style data.

    Args:
        metric: The pixel metric being queried.
        opt: Optional style option.
        widget: The target widget.

    Returns:
        The indent size in pixels.
    """
    if metric == QStyle.PixelMetric.PM_TreeViewIndentation:
        variant = getattr(widget, "_variant_str", "default")
        style = self.model.get_style("QTreeView", variant)
        return int(style.get("indent", 20))
    return 0

register_drawers()

Register drawing functions for QTreeView primitives.

Source code in client/ayon_ui_qt/drawers/tree_view.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def register_drawers(self) -> dict:
    """Register drawing functions for QTreeView primitives."""
    return {
        enum_to_str(
            QStyle.PrimitiveElement,
            QStyle.PrimitiveElement.PE_IndicatorBranch,
            "QTreeView",
        ): self.draw_branch_indicator,
        enum_to_str(
            QStyle.PrimitiveElement,
            QStyle.PrimitiveElement.PE_PanelScrollAreaCorner,
            "QTreeView",
        ): self.draw_scrollbar_corner,
    }

register_metrics()

Register pixel metric functions for QTreeView.

Source code in client/ayon_ui_qt/drawers/tree_view.py
48
49
50
51
52
53
54
55
56
def register_metrics(self) -> dict:
    """Register pixel metric functions for QTreeView."""
    return {
        enum_to_str(
            QStyle.PixelMetric,
            QStyle.PixelMetric.PM_TreeViewIndentation,
            "QTreeView",
        ): self.get_metric,
    }