def borderPath(self, rect): """ Calculate the painter path for a styled or rounded border When the canvas has no styled background or rounded borders the painter path is empty. :param QRect rect: Bounding rectangle of the canvas :return: Painter path, that can be used for clipping """ if self.testAttribute(Qt.WA_StyledBackground): recorder = QwtStyleSheetRecorder(rect.size()) painter = QPainter(recorder) opt = QStyleOption() opt.initFrom(self) opt.rect = rect self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.end() if not recorder.background.path.isEmpty(): return recorder.background.path if len(recorder.border.rectList) > 0: return qwtCombinePathList(rect, recorder.border.pathlist) elif self.__data.borderRadius > 0.0: fw2 = self.frameWidth() * 0.5 r = QRectF(rect).adjusted(fw2, fw2, -fw2, -fw2) path = QPainterPath() path.addRoundedRect(r, self.__data.borderRadius, self.__data.borderRadius) return path return QPainterPath()
def paintEvent(self, e): height = self.height() width = self.width() # draw background painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.transparent) painter.setBrush(BACKGROUND) painter.setOpacity(1) path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addRoundedRect(QRectF(8, 0, width - 8, height), 15, 15) painter.drawPath(path.simplified()) # draw sidebar painter.setBrush(SIDEBAR) painter.setOpacity(1) path = QPainterPath() path.setFillRule(Qt.WindingFill) # we draw a fixed-width toolbar background path.addRoundedRect(QRectF(8, 0, 250 - 8, height), 15, 15) path.addRect(QRectF(200, 0, 50, 50)) path.addRect(QRectF(200, height - 50, 50, 50)) painter.drawPath(path.simplified())
def paintEvent(self, event) -> None: super(ToolTip, self).paintEvent(event) x = self.rect().x() + self._anchor_width y = self.rect().y() + self._anchor_width w = self.rect().width() - self._anchor_width * 2 h = self.rect().height() - self._anchor_width * 2 # 背景 painter = QPainter(self) path = QPainterPath() path.addRoundedRect(QRectF(x, y, w, h), 4, 4) # 画锚 if self._direction == ToolTip.TOP: x1 = x + w / 2 - self._anchor_width y1 = y + h x2 = x + w / 2 + self._anchor_width y2 = y + h x3 = x + w / 2 y3 = y + h + self._anchor_width path.moveTo(x1, y1) path.lineTo(x2, y2) path.lineTo(x3, y3) elif self._direction == ToolTip.BOTTOM: x1 = x + w / 2 - self._anchor_width y1 = y x2 = x + w / 2 + self._anchor_width y2 = y x3 = x + w / 2 y3 = y - self._anchor_width path.moveTo(x1, y1) path.lineTo(x2, y2) path.lineTo(x3, y3) elif self._direction == ToolTip.RIGHT: x1 = x y1 = y + h / 2 - self._anchor_width x2 = x y2 = y + h / 2 + self._anchor_width x3 = x - self._anchor_width y3 = y + h / 2 path.moveTo(x1, y1) path.lineTo(x2, y2) path.lineTo(x3, y3) elif self._direction == ToolTip.LEFT: x1 = x + w y1 = y + h / 2 - self._anchor_width x2 = x + w y2 = y + h / 2 + self._anchor_width x3 = x + w + self._anchor_width y3 = y + h / 2 path.moveTo(x1, y1) path.lineTo(x2, y2) path.lineTo(x3, y3) painter.fillPath(path, ResourceLoader().qt_color_separator_dark)
def drawRoundedFrame(self, painter, rect, xRadius, yRadius, palette, lineWidth, frameStyle): """ Draw a rectangular frame with rounded borders :param QPainter painter: Painter :param QRectF rect: Frame rectangle :param float xRadius: x-radius of the ellipses defining the corners :param float yRadius: y-radius of the ellipses defining the corners :param QPalette palette: `QPalette.WindowText` is used for plain borders, `QPalette.Dark` and `QPalette.Light` for raised or sunken borders :param int lineWidth: Line width :param int frameStyle: bitwise OR´ed value of `QFrame.Shape` and `QFrame.Shadow` """ painter.save() painter.setRenderHint(QPainter.Antialiasing, True) painter.setBrush(Qt.NoBrush) lw2 = lineWidth * 0.5 r = rect.adjusted(lw2, lw2, -lw2, -lw2) path = QPainterPath() path.addRoundedRect(r, xRadius, yRadius) Plain, Sunken, Raised = list(range(3)) style = Plain if (frameStyle & QFrame.Sunken) == QFrame.Sunken: style = Sunken if (frameStyle & QFrame.Raised) == QFrame.Raised: style = Raised if style != Plain and path.elementCount() == 17: pathList = [QPainterPath() for _i in range(8)] for i in range(4): j = i * 4 + 1 pathList[2 * i].moveTo( path.elementAt(j - 1).x, path.elementAt(j - 1).y) pathList[2 * i].cubicTo( path.elementAt(j + 0).x, path.elementAt(j + 0).y, path.elementAt(j + 1).x, path.elementAt(j + 1).y, path.elementAt(j + 2).x, path.elementAt(j + 2).y, ) pathList[2 * i + 1].moveTo( path.elementAt(j + 2).x, path.elementAt(j + 2).y) pathList[2 * i + 1].lineTo( path.elementAt(j + 3).x, path.elementAt(j + 3).y) c1 = QColor(palette.color(QPalette.Dark)) c2 = QColor(palette.color(QPalette.Light)) if style == Raised: c1, c2 = c2, c1 for i in range(5): r = pathList[2 * i].controlPointRect() arcPen = QPen() arcPen.setCapStyle(Qt.FlatCap) arcPen.setWidth(lineWidth) linePen = QPen() linePen.setCapStyle(Qt.FlatCap) linePen.setWidth(lineWidth) if i == 0: arcPen.setColor(c1) linePen.setColor(c1) elif i == 1: gradient = QLinearGradient() gradient.setStart(r.topLeft()) gradient.setFinalStop(r.bottomRight()) gradient.setColorAt(0.0, c1) gradient.setColorAt(1.0, c2) arcPen.setBrush(gradient) linePen.setColor(c2) elif i == 2: arcPen.setColor(c2) linePen.setColor(c2) elif i == 3: gradient = QLinearGradient() gradient.setStart(r.bottomRight()) gradient.setFinalStop(r.topLeft()) gradient.setColorAt(0.0, c2) gradient.setColorAt(1.0, c1) arcPen.setBrush(gradient) linePen.setColor(c1) painter.setPen(arcPen) painter.drawPath(pathList[2 * i]) painter.setPen(linePen) painter.drawPath(pathList[2 * i + 1]) else: pen = QPen(palette.color(QPalette.WindowText), lineWidth) painter.setPen(pen) painter.drawPath(path) painter.restore()