コード例 #1
0
    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHint(painter.Antialiasing)
        font = painter.font()
        font.setBold(True)
        painter.setFont(font)

        x = self.rect().x()
        y = self.rect().y()
        w = self.rect().width() - 1
        h = self.rect().height() - 1
        _rect = 5

        if self.rollout_style == AccordionStyle.ROUNDED:
            border_color = self.palette().color(QPalette.Light)
            header_color = QColor(self._theme.background_color).lighter(90)
            background_color = QColor(self._theme.background_color).lighter(135)

            painter.save()
            # pen = QPen(border_color)
            # pen.setWidthF(0.1)
            painter.setPen(Qt.NoPen)
            painter.setBrush(QBrush(background_color))
            painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, _rect, _rect)
            # pen.setColor(self.palette().color(QPalette.Shadow))
            # painter.setPen(pen)
            # painter.drawRoundedRect(x, y, w - 1, h - 1, _rect, _rect)
            path = QPainterPath()
            path.setFillRule(Qt.WindingFill)
            path.addRoundedRect(x + 1, y + 1, w - 1, 20, _rect, _rect)
            if not self.is_collapsed():
                path.addRect(x + 1, y + 16, w - 1, 5)
            painter.setBrush(QBrush(header_color))
            painter.drawPath(path.simplified())
            painter.restore()

            painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())
            self._draw_triangle(painter, x, y)
            self._draw_icon(painter, x + 22, y + 3)

        elif self.rollout_style == AccordionStyle.SQUARE:
            painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())
            self._draw_triangle(painter, x, y)
            self._draw_icon(painter, x + 22, y + 3)
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.15)
            painter.setPen(pen)
            painter.drawRect(x + 1, y + 1, w - 1, h - 1)
            pen.setColor(self.palette().color(QPalette.Shadow))
            painter.setPen(pen)
        elif self.rollout_style == AccordionStyle.MAYA:
            painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title())
            painter.setRenderHint(QPainter.Antialiasing, False)
            self._draw_triangle(painter, x, y)
            self._draw_icon(painter, x + 22, y + 3)
            header_height = 20
            header_rect = QRect(x + 1, y + 1, w - 1, header_height)
            header_rect_shadow = QRect(x - 1, y - 1, w + 1, header_height + 2)
            pen = QPen(self.palette().color(QPalette.Light))
            pen.setWidthF(0.4)
            # painter.setPen(pen)
            painter.setPen(Qt.NoPen)
            painter.drawRect(header_rect)
            painter.fillRect(header_rect, QColor(255, 255, 255, 18))
            pen.setColor(self.palette().color(QPalette.Dark))
            painter.setPen(pen)
            painter.drawRect(header_rect_shadow)
            if not self.is_collapsed():
                # pen = QPen(self.palette().color(QPalette.Dark))
                # pen.setWidthF(0.8)
                # painter.setPen(pen)
                offset = header_height + 3
                body_rect = QRect(x, y + offset, w, h - offset)
                # body_rect_shadow = QRect(x + 1, y + offset, w + 1, h - offset + 1)
                painter.drawRect(body_rect)
                # pen.setColor(self.palette().color(QPalette.Light))
                # pen.setWidth(0.4)
                # painter.setPen(pen)
                # painter.drawRect(body_rect_shadow)
        elif self.rollout_style == AccordionStyle.BOXED:
            if self.is_collapsed():
                a_rect = QRect(x + 1, y + 9, w - 1, 4)
                b_rect = QRect(x, y + 8, w - 1, 4)
                text = '+'
            else:
                a_rect = QRect(x + 1, y + 9, w - 1, h - 9)
                b_rect = QRect(x, y + 8, w - 1, h - 9)
                text = '-'

            pen = QPen(self.palette().color(QPalette.Light))
            pen = QPen(Qt.red)
            pen.setWidthF(0.6)
            painter.setPen(pen)
            painter.drawRect(a_rect)
            # pen.setColor(self.palette().color(QPalette.Shadow))
            # painter.setPen(pen)
            painter.drawRect(b_rect)
            painter.setRenderHint(painter.Antialiasing, False)
            painter.setBrush(self.palette().color(QPalette.Window).darker(120))
            painter.drawRect(x + 10, y + 1, w - 20, 16)
            painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text)
            painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title())

        if self.drag_drop_mode:
            rect = self.get_drag_drop_rect()
            _layout = rect.left()
            _rect = rect.right()
            center_y = rect.center().y()
            for y in (center_y - 3, center_y, center_y + 3):
                painter.drawLine(_layout, y, _rect, y)

        painter.end()
コード例 #2
0
    def create_drop_indicator_pixmap(palette, size, drop_area):
        border_color = palette.color(QPalette.Active, QPalette.Highlight)
        background_color = palette.color(QPalette.Active, QPalette.Base)
        area_background_color = palette.color(QPalette.Active,
                                              QPalette.Highlight).lighter(150)

        pm = QPixmap(size.width(), size.height())
        pm.fill(QColor(0, 0, 0, 0))

        painter = QPainter(pm)
        pen = painter.pen()
        base_rect = QRectF(pm.rect())

        painter.fillRect(base_rect, background_color)

        painter.save()
        area_rect = QRectF()
        area_line = QLineF()
        gradient = QLinearGradient()
        if drop_area == DropArea.TopDropArea:
            area_rect = QRectF(base_rect.x(), base_rect.y(), base_rect.width(),
                               base_rect.height() * 0.5)
            area_line = QLineF(area_rect.bottomLeft(), area_rect.bottomRight())
            gradient.setStart(area_rect.topLeft())
            gradient.setFinalStop(area_rect.bottomLeft())
            gradient.setColorAt(0, area_background_color)
            gradient.setColorAt(1, area_background_color.lighter(120))
        elif drop_area == DropArea.RightDropArea:
            area_rect = QRectF(base_rect.width() * 0.5, base_rect.y(),
                               base_rect.width() * 0.5, base_rect.height())
            area_line = QLineF(area_rect.topLeft(), area_rect.bottomLeft())
            gradient.setStart(area_rect.topLeft())
            gradient.setFinalStop(area_rect.topRight())
            gradient.setColorAt(0, area_background_color.lighter(120))
            gradient.setColorAt(1, area_background_color)
        elif drop_area == DropArea.BottomDropArea:
            area_rect = QRectF(base_rect.x(),
                               base_rect.height() * 0.5, base_rect.width(),
                               base_rect.height() * 0.5)
            area_line = QLineF(area_rect.topLeft(), area_rect.topRight())
            gradient.setStart(area_rect.topLeft())
            gradient.setFinalStop(area_rect.bottomLeft())
            gradient.setColorAt(0, area_background_color.lighter(120))
            gradient.setColorAt(1, area_background_color)
        elif drop_area == DropArea.LeftDropArea:
            area_rect = QRectF(base_rect.x(), base_rect.y(),
                               base_rect.width() * 0.5, base_rect.height())
            area_line = QLineF(area_rect.topRight(), area_rect.bottomRight())
            gradient.setStart(area_rect.topLeft())
            gradient.setFinalStop(area_rect.topRight())
            gradient.setColorAt(0, area_background_color)
            gradient.setColorAt(1, area_background_color.lighter(120))

        if area_rect.isValid():
            painter.fillRect(area_rect, gradient)
            pen = painter.pen()
            pen.setColor(border_color)
            pen.setStyle(Qt.DashLine)
            painter.setPen(pen)
            painter.drawLine(area_line)

        painter.restore()

        painter.save()
        pen = painter.pen()
        pen.setColor(border_color)
        pen.setWidth(1)

        painter.setPen(pen)
        painter.drawRect(base_rect.adjusted(0, 0, -pen.width(), -pen.width()))
        painter.restore()

        return pm
コード例 #3
0
ファイル: chip.py プロジェクト: spauka/PyQtTests
    def paint(self, p: QtGui.QPainter, option: QtWidgets.QGraphicsItem.ItemIsSelectable,
              widget: QtWidgets.QWidget): # pylint: disable=unused-argument

        # Set color when selected
        if option.state & QtWidgets.QStyle.State_Selected:
            fillColor = self.color.darker(150)
        else:
            fillColor = self.color

        # Lighten if mouse over
        if option.state & QtWidgets.QStyle.State_MouseOver:
            fillColor = fillColor.lighter(125)

        # Calculate level of detail
        lod = option.levelOfDetailFromTransform(p.worldTransform())
        if lod < 0.125:
            p.fillRect(QtCore.QRectF(0, 0, 110, 70), fillColor)
            return
        if lod < 0.2:
            oldBrush = p.brush()
            p.setBrush(fillColor)
            p.drawRect(13, 13, 97, 57)
            p.setBrush(oldBrush)
            return

        oldPen = p.pen()
        oldBrush = p.brush()

        # Use a copy of the old pen
        pen = QtGui.QPen(oldPen)
        if option.state & QtWidgets.QStyle.State_Selected:
            pen.setWidth(2)
        else:
            pen.setWidth(0)
        p.setPen(pen)

        # Draw the inside of the chip
        if option.state & QtWidgets.QStyle.State_Sunken:
            p.setBrush(QtGui.QBrush(fillColor.darker(120)))
        else:
            p.setBrush(QtGui.QBrush(fillColor.darker(100)))
        p.drawRect(QtCore.QRect(14, 14, 79, 39))
        p.setBrush(oldBrush)

        if lod >= 1:
            p.setPen(QtGui.QPen(QtCore.Qt.gray, 1))
            p.drawLine(15, 54, 94, 54)
            p.drawLine(94, 53, 94, 15)
            p.setPen(QtGui.QPen(QtCore.Qt.black, 0))

        if lod >= 2:
            font = QtGui.QFont("Times", 10)
            font.setStyleStrategy(QtGui.QFont.ForceOutline)
            p.setFont(font)
            p.save()
            p.scale(0.1, 0.1)
            p.drawText(170, 180, f"Model: VSC-2000 (Very Small Chip) at {self.x}x{self.y}")
            p.drawText(170, 200, "Serial number: DLWR-WEER-123L-ZZ33-SDSJ")
            p.drawText(170, 220, "Manufacturer: Chip Manufacturer")
            p.restore()

        lines = []
        if lod >= 0.5:
            for i in range(0, 11, 1 if lod > 0.5 else 2):
                lines.append(QtCore.QLineF(18 + 7*i, 13, 18 + 7*i, 5))
                lines.append(QtCore.QLineF(18 + 7*i, 54, 18 + 7*i, 62))
            for i in range(0, 7, 1 if lod > 0.5 else 2):
                lines.append(QtCore.QLineF(5, 18 + i*5, 13, 18 + i*5))
                lines.append(QtCore.QLineF(94, 18 + i*5, 102, 18 + i*5))
        if lod >= 0.4:
            lines.append(QtCore.QLineF(25, 35, 35, 35))
            lines.append(QtCore.QLineF(35, 30, 35, 40))
            lines.append(QtCore.QLineF(35, 30, 45, 35))
            lines.append(QtCore.QLineF(35, 40, 45, 35))
            lines.append(QtCore.QLineF(45, 30, 45, 40))
            lines.append(QtCore.QLineF(45, 35, 55, 35))
        p.drawLines(lines)

        # Draw red ink
        if self.stuff:
            p.setPen(QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine,
                                QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
            p.setBrush(QtCore.Qt.NoBrush)
            path = QtGui.QPainterPath()
            path.moveTo(self.stuff[0])
            for point in self.stuff[1:]:
                path.lineTo(point)
            p.drawPath(path)

        p.setPen(oldPen)
        p.setBrush(oldBrush)