def paint(self, painter, option, widget=None): path = QPainterPath() path.addRoundedRect(self.m_rect, 5, 5) anchor = self.mapFromParent(self.m_chart.mapToPosition(self.m_anchor)) if not self.m_rect.contains(anchor): point1 = QPointF() point2 = QPointF() # establish the position of the anchor point in relation to m_rect above = anchor.y() <= self.m_rect.top() aboveCenter = (anchor.y() > self.m_rect.top() and anchor.y() <= self.m_rect.center().y()) belowCenter = (anchor.y() > self.m_rect.center().y() and anchor.y() <= self.m_rect.bottom()) below = anchor.y() > self.m_rect.bottom() onLeft = anchor.x() <= self.m_rect.left() leftOfCenter = (anchor.x() > self.m_rect.left() and anchor.x() <= self.m_rect.center().x()) rightOfCenter = (anchor.x() > self.m_rect.center().x() and anchor.x() <= self.m_rect.right()) onRight = anchor.x() > self.m_rect.right() # get the nearest m_rect corner. x = (onRight + rightOfCenter) * self.m_rect.width() y = (below + belowCenter) * self.m_rect.height() cornerCase = ((above and onLeft) or (above and onRight) or (below and onLeft) or (below and onRight)) vertical = abs(anchor.x() - x) > abs(anchor.y() - y) x1 = (x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * int(not vertical) * (onLeft * 10 - onRight * 20)) y1 = (y + aboveCenter * 10 - belowCenter * 20 + cornerCase * int(vertical) * (above * 10 - below * 20)) point1.setX(x1) point1.setY(y1) x2 = (x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * int(not vertical) * (onLeft * 20 - onRight * 10)) y2 = (y + aboveCenter * 20 - belowCenter * 10 + cornerCase * int(vertical) * (above * 20 - below * 10)) point2.setX(x2) point2.setY(y2) path.moveTo(point1) path.lineTo(anchor) path.lineTo(point2) path = path.simplified() painter.setBrush(QColor(255, 255, 255)) painter.drawPath(path) painter.drawText(self.m_textRect, self.m_text)
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()