Skip to content

tooltip

TooltipDrawer: custom painting for QToolTip.

TooltipDrawer

Source code in client/ayon_ui_qt/drawers/tooltip.py
 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
class TooltipDrawer:
    def __init__(self, style_inst: AYONStyle) -> None:
        self.style_inst = style_inst
        self.model = style_inst.model

    @property
    def _super(self):
        """Return proxy for calling QCommonStyle methods on style_inst."""
        from ..style import AYONStyle as _AYONStyle

        return super(_AYONStyle, self.style_inst)

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

    def register_drawers(self):
        return {
            enum_to_str(
                QStyle.ControlElement,
                QStyle.ControlElement.CE_ShapedFrame,
                "QToolTip",
            ): self.draw_control,
            enum_to_str(
                QStyle.PrimitiveElement,
                QStyle.PrimitiveElement.PE_PanelTipLabel,
                "QToolTip",
            ): partial(
                self.draw_primitive, QStyle.PrimitiveElement.PE_PanelTipLabel
            ),
            enum_to_str(
                QStyle.PrimitiveElement,
                QStyle.PrimitiveElement.PE_Frame,
                "QToolTip",
            ): partial(self.draw_primitive, QStyle.PrimitiveElement.PE_Frame),
        }

    def register_sizers(self):
        return {
            enum_to_str(
                QStyle.SubElement,
                QStyle.SubElement.SE_ShapedFrameContents,
                "QToolTip",
            ): self.get_rect,
            enum_to_str(
                QStyle.SubElement,
                QStyle.SubElement.SE_FrameLayoutItem,
                "QToolTip",
            ): self.get_rect,
        }

    def draw_control(
        self,
        option: QStyleOptionFrame,
        painter: QPainter,
        widget: QWidget,
    ):
        option.frameShadow = QFrame.Shadow.Plain
        option.frameShape = QFrame.Shape.StyledPanel
        self._super.drawControl(
            QStyle.ControlElement.CE_ShapedFrame, option, painter, widget
        )

    def draw_primitive(
        self,
        prim: QStyle.PrimitiveElement,
        option: QStyleOption,
        painter: QPainter,
        w: QWidget,
    ) -> None:
        if prim == QStyle.PrimitiveElement.PE_Frame:
            painter.save()
            painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
            style = self.model.get_style("QToolTip")
            style.set_context(w)
            pen = QPen(style["border-color"])
            pen.setWidth(style["border-width"])
            painter.setBrush(Qt.BrushStyle.NoBrush)
            painter.setPen(pen)
            radius = int(style["border-radius"])
            painter.drawRoundedRect(
                option.rect,
                radius,
                radius,
            )
            painter.restore()

        elif prim == QStyle.PrimitiveElement.PE_PanelTipLabel:
            painter.save()
            painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
            style = self.model.get_style("QToolTip")
            style.set_context(w)
            brush = QBrush(style["background-color"])
            painter.setBrush(brush)
            painter.setPen(Qt.PenStyle.NoPen)
            radius = int(style["border-radius"])
            painter.drawRoundedRect(
                option.rect,
                radius,
                radius,
            )
            painter.restore()

    def get_rect(
        self,
        element: QStyle.SubElement,
        option: QStyleOption,
        widget: QWidget,
    ) -> QRect:
        tt_style = self.model.get_style("QToolTip")
        tt_style.set_context(widget)
        tt_pad_x, tt_pad_y = tt_style["padding"]

        if element == QStyle.SubElement.SE_ShapedFrameContents:
            if isinstance(option, QStyleOptionFrame):
                option.features = QStyleOptionFrame.FrameFeature.Rounded
                option.frameShape = QFrame.Shape.StyledPanel
                widget.setContentsMargins(
                    tt_pad_x, tt_pad_y, tt_pad_x, tt_pad_y
                )

        elif element == QStyle.SubElement.SE_FrameLayoutItem:
            if isinstance(option, QStyleOptionFrame):
                option.features = QStyleOptionFrame.FrameFeature.Rounded
                option.frameShape = QFrame.Shape.StyledPanel
                widget.setContentsMargins(
                    tt_pad_x, tt_pad_y, tt_pad_x, tt_pad_y
                )

        return self._super.subElementRect(element, option, widget)