def drawViewItemText(self, style: QStyle, painter: QPainter,
                         option: QStyleOptionViewItem, rect: QRect) -> None:
        """
        Draw view item text in `rect` using `style` and `painter`.
        """
        margin = style.pixelMetric(QStyle.PM_FocusFrameHMargin, None,
                                   option.widget) + 1
        rect = rect.adjusted(margin, 0, -margin, 0)
        font = option.font
        st = self.__static_text_elided_cache(option.text, font,
                                             option.fontMetrics,
                                             option.textElideMode,
                                             rect.width())
        tsize = st.size()
        textalign = int(option.displayAlignment)
        text_pos_x = text_pos_y = 0.0

        if textalign & _Qt_AlignLeft:
            text_pos_x = rect.left()
        elif textalign & _Qt_AlignRight:
            text_pos_x = rect.x() + rect.width() - tsize.width()
        elif textalign & _Qt_AlignHCenter:
            text_pos_x = rect.x() + rect.width() / 2 - tsize.width() / 2

        if textalign & _Qt_AlignVCenter:
            text_pos_y = rect.y() + rect.height() / 2 - tsize.height() / 2
        elif textalign & _Qt_AlignTop:
            text_pos_y = rect.top()
        elif textalign & _Qt_AlignBottom:
            text_pos_y = rect.top() + rect.height() - tsize.height()

        painter.setPen(self.__pen_cache(option.palette, option.state))
        painter.setFont(font)
        painter.drawStaticText(QPointF(text_pos_x, text_pos_y), st)
Пример #2
0
    def __updateGeometry(self):
        # type: () -> None
        """
        Update the shadow geometry to fit the widget's changed
        geometry.
        """
        assert self.__widget is not None
        widget = self.__widget
        parent = self.__widgetParent
        radius = self.radius_
        offset = self.__offset
        pos = widget.pos()
        if parent is not None and parent != widget.parentWidget():
            pos = widget.parentWidget().mapTo(parent, pos)

        geom = QRect(pos, widget.size())
        geom = geom.adjusted(-radius, -radius, radius, radius)
        geom = geom.translated(offset)
        if geom != self.geometry():
            self.setGeometry(geom)

        # Set the widget mask (punch a hole through to the `widget` instance.
        rect = self.rect()
        mask = QRegion(rect)

        rect = rect.adjusted(radius, radius, -radius, -radius)
        rect = rect.translated(-offset)
        transparent = QRegion(rect)
        mask = mask.subtracted(transparent)
        self.setMask(mask)