def paintEvent(self, event): a = self.height() / 2 * 0.4 painter = QPainter(self) painter.setOpacity(self.opacity) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setPen(Qt.NoPen) painter.save() path = QPainterPath() brush = QBrush(self.color1) painter.setBrush(brush) path.moveTo(self.p1) rect = QRectF(self.p1.x() - a, 0, a * 2, self.height()) path.arcTo(rect, 90, 360) painter.drawPath(path) painter.restore() if self.p1 == self.p2: return path = QPainterPath() brush = QBrush(self.color2) painter.setBrush(brush) path.moveTo(self.p2) rect = QRectF(self.p2.x() - a, 0, a * 2, self.height()) path.arcTo(rect, 90, 360) painter.drawPath(path)
def itemRegion(self, index): if not index.isValid(): return QRegion() if index.column() != 1: return QRegion(self.itemRect(index)) if self.model().data(index) <= 0.0: return QRegion() startAngle = 0.0 for row in range(self.model().rowCount(self.rootIndex())): sliceIndex = self.model().index(row, 1, self.rootIndex()) value = self.model().data(sliceIndex) if value > 0.0: angle = 360*value/self.totalValue if sliceIndex == index: slicePath = QPainterPath() slicePath.moveTo(self.totalSize/2, self.totalSize/2) slicePath.arcTo(self.margin, self.margin, self.margin+self.pieSize, self.margin+self.pieSize, startAngle, angle) slicePath.closeSubpath() return QRegion(slicePath.toFillPolygon().toPolygon()) startAngle += angle return QRegion()
def update(self, eventPos, fase, point=None): # If user is setting the final point if fase == 1: if fase == 1: path = QPainterPath() path.moveTo(self.initialPoint.position) path.lineTo(eventPos) self.setPath(path) if point: self.finalPoint = point # If user is finishing the path (setting centerPoint) elif fase == 2: path = QPainterPath() path.moveTo(self.initialPoint.position) r = ((eventPos.x() - self.initialPoint.position.x())**2 + (eventPos.y() - self.initialPoint.position.y())**2)**(1 / 2) x, y, w, h = eventPos.x() - r, eventPos.y() - r, 2 * r, 2 * r line1 = QLineF(eventPos, self.initialPoint.position) line2 = QLineF(eventPos, self.finalPoint.position) startAngle, arcLenghth = line1.angle(), -(line1.angle() - line2.angle()) if arcLenghth == 0: arcLenghth = 360 path.arcTo(x, y, w, h, startAngle, arcLenghth) self.setPath(path) if point: self.centerPoint = point
def drawBg(self, event, painter): painter.save() painter.setPen(Qt.NoPen) if self.checked: painter.setBrush(self.bgColorOn) else: painter.setBrush(self.bgColorOff) rect = QRect(0, 0, self.width(), self.height()) # 半径为高度的一半 radius = rect.height() / 2 # 圆的宽度为高度 circleWidth = rect.height() path = QPainterPath() path.moveTo(radius, rect.left()) path.arcTo(QRectF(rect.left(), rect.top(), circleWidth, circleWidth), 90, 180) path.lineTo(rect.width() - radius, rect.height()) path.arcTo( QRectF(rect.width() - rect.height(), rect.top(), circleWidth, circleWidth), 270, 180) path.lineTo(radius, rect.top()) painter.drawPath(path) painter.restore()
def createRotateArrow(): arrowHeadPos = 12 arrowHeadLength = 4.5 arrowHeadWidth = 5 bodyWidth = 1.5 outerArcSize = arrowHeadPos + bodyWidth - arrowHeadLength innerArcSize = arrowHeadPos - bodyWidth - arrowHeadLength path = QPainterPath() path.moveTo(arrowHeadPos, 0) path.lineTo(arrowHeadPos + arrowHeadWidth, arrowHeadLength) path.lineTo(arrowHeadPos + bodyWidth, arrowHeadLength) path.arcTo(QRectF(arrowHeadLength - outerArcSize, arrowHeadLength - outerArcSize, outerArcSize * 2, outerArcSize * 2), 0, -90) path.lineTo(arrowHeadLength, arrowHeadPos + arrowHeadWidth) path.lineTo(0, arrowHeadPos) path.lineTo(arrowHeadLength, arrowHeadPos - arrowHeadWidth) path.lineTo(arrowHeadLength, arrowHeadPos - bodyWidth) path.arcTo(QRectF(arrowHeadLength - innerArcSize, arrowHeadLength - innerArcSize, innerArcSize * 2, innerArcSize * 2), -90, 90) path.lineTo(arrowHeadPos - arrowHeadWidth, arrowHeadLength) path.closeSubpath() return path
def get_path(self): path = QPainterPath() rect = QRectF(self.rect()) path.arcMoveTo(rect, 0) path.arcTo(rect, 0, 360) path.closeSubpath() return path
class ModCrossButton(QPushButton): def __init__(self,parent,path=None): QPushButton.__init__(self,parent) self.parent=parent #self.setAttribute(Qt.WA_TranslucentBackground, True) self.backgroundColor = QPalette().light().color() self.backgroundColor.setRgb(157,157,157) #(220,203,231) #self.backgroundColor.setAlpha(100) self.brush=QBrush(Qt.SolidPattern) def paintEvent(self,event): self.wide=self.width() self.high=self.height() self.xdis=self.wide/7 self.ydis=self.xdis self.path=QPainterPath() self.path.setFillRule(Qt.OddEvenFill) self.path.moveTo(self.wide/2, self.high/2-self.xdis) self.path.arcTo(0,0, self.wide, self.high,0,360) #self.path.closeSubpath() self.path.moveTo(self.wide/2-self.xdis/2, self.ydis) self.path.lineTo(self.wide/2-self.xdis/2, self.high/2-self.xdis/2) self.path.lineTo(self.ydis, self.high/2-self.xdis/2) self.path.lineTo(self.ydis, self.high/2+self.xdis/2) self.path.lineTo(self.wide/2-self.xdis/2, self.high/2+self.xdis/2) self.path.lineTo(self.wide/2-self.xdis/2, self.high-self.ydis) self.path.lineTo(self.wide/2+self.xdis/2, self.high-self.ydis) self.path.lineTo(self.wide/2+self.xdis/2, self.high/2+self.xdis/2) self.path.lineTo(self.wide-self.ydis, self.high/2+self.xdis/2) self.path.lineTo(self.wide-self.ydis, self.high/2-self.xdis/2) self.path.lineTo(self.wide/2+self.xdis/2, self.high/2-self.xdis/2) self.path.lineTo(self.wide/2+self.xdis/2, self.ydis) self.path.closeSubpath() self.brush.setColor(self.backgroundColor) self.painter=QPainter(self) self.painter.setRenderHint(QPainter.Antialiasing) self.painter.setPen(Qt.NoPen) self.painter.setBrush(self.brush) self.painter.drawPath(self.path) self.painter.end() #def mousePressEvent(self,ev): # self.parent.close() def enterEvent(self,ev): self.backgroundColor.setRgb(242,146,52) self.update() def leaveEvent(self,ev): self.backgroundColor.setRgb(157,157,157) self.update()
def drawValue(self, p: QPainter, baseRect: QRectF, value: float, delta: float): if value == self.m_min: return if self.m_barStyle == self.BarStyle.EXPAND: p.setBrush(self.palette().highlight()) p.setPen(QPen(self.palette().shadow().color(), self.m_dataPenWidth)) radius = (baseRect.height() / 2) / delta p.drawEllipse(baseRect.center(), radius, radius) return if self.m_barStyle == self.BarStyle.LINE: p.setPen(QPen(self.palette().highlight().color(), self.m_dataPenWidth)) p.setBrush(Qt.NoBrush) if value == self.m_max: p.drawEllipse(baseRect.adjusted(self.m_outlinePenWidth / 2, self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2)) else: arcLength = 360 / delta p.drawArc(baseRect.adjusted(self.m_outlinePenWidth / 2, self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2, -self.m_outlinePenWidth / 2), int(self.m_nullPosition * 16), int(-arcLength * 16)) return dataPath = QPainterPath() dataPath.setFillRule(Qt.WindingFill) if value == self.m_max: dataPath.addEllipse(baseRect) else: arcLength = 360 / delta dataPath.moveTo(baseRect.center()) dataPath.arcTo(baseRect, self.m_nullPosition, -arcLength) dataPath.lineTo(baseRect.center()) p.setBrush(self.palette().highlight()) p.setPen(QPen(self.palette().shadow().color(), self.m_dataPenWidth)) p.drawPath(dataPath)
def setPath(self): path = QPainterPath() path.moveTo(self._wrs * 0.97, self._hrs / 2) # artTo (startx, starty, width, height, arcdeg start, arcdeg end) path.arcTo(self._wrs * 0.03, self._hrs * 0.03, self._wrs * 0.94, self._hrs * 0.94, 0.0, 360.0) return path
def drawFace(self, qp): #画脸外轮廓 path = QPainterPath() outdiameter = 376.0 outre = QRectF(261.0, 30.0, outdiameter, outdiameter) outre_right_x = 261 + outdiameter / 2 + outdiameter / 2 / math.sqrt(2) outre_right_y = 30 + outdiameter / 2 + outdiameter / 2 / math.sqrt(2) path.moveTo(outre_right_x, outre_right_y) # 将painter路径的起始位置迁移到我们指定的坐标。 path.arcTo(outre, -45, 270) # arcTo()创建一个占据给定矩形的弧,从指定的startAngle(-45度)开始并逆时针延伸sweepLength(270度) path.closeSubpath() # 通过在子路径的开头绘制一条线来关闭当前子路径,自动启动一个新路径 qp.setBrush(QColor(156, 214, 239)) # 给这个图形填充我们设置的颜色 qp.drawPath(path) # 画出我们刚才设置的图形 #画脸内轮廓 path2 = QPainterPath() indiameter = 311.0 inre = QRectF(301.0, 86.0, indiameter, indiameter) # 画矩形 inre_right_x = 301 + indiameter / 2 + indiameter / 2 / math.sqrt(2) inre_right_y = 86 + indiameter / 2 + indiameter / 2 / math.sqrt(2) path2.moveTo(inre_right_x, inre_right_y) path2.arcTo(inre, -45, 270) path2.closeSubpath() qp.setBrush(Qt.white) qp.drawPath(path2)
def updateAngle(self, startAngle, spanAngle): self._startAngle = startAngle self._spanAngle = spanAngle path = QPainterPath() path.arcMoveTo(self._parent._rect, startAngle) path.arcTo(self._parent._rect, startAngle, spanAngle) self.setPath(path)
def itemRegion(self, index): if not index.isValid(): return QRegion() if index.column() != 1: return QRegion(self.itemRect(index)) if self.model().data(index) <= 0.0: return QRegion() startAngle = 0.0 for row in range(self.model().rowCount(self.rootIndex())): sliceIndex = self.model().index(row, 1, self.rootIndex()) value = self.model().data(sliceIndex) if value > 0.0: angle = 360 * value / self.totalValue if sliceIndex == index: slicePath = QPainterPath() slicePath.moveTo(self.totalSize / 2, self.totalSize / 2) slicePath.arcTo(self.margin, self.margin, self.margin + self.pieSize, self.margin + self.pieSize, startAngle, angle) slicePath.closeSubpath() return QRegion(slicePath.toFillPolygon().toPolygon()) startAngle += angle return QRegion()
def get_circle_path(center_pos: list, radius: int): rect = QRectF(center_pos[0] - radius, center_pos[1] - radius, radius * 2, radius * 2) seed_path = QPainterPath() seed_path.arcMoveTo(rect, 0) seed_path.arcTo(rect, 0, 360) seed_path.closeSubpath() return seed_path
def create_circle_path(x_center: int, y_center: int, radius: int) -> QPainterPath: rect = QRectF(x_center - radius, y_center - radius, radius * 2, radius * 2) circle_path = QPainterPath() circle_path.arcMoveTo(rect, 0) circle_path.arcTo(rect, 0, 360) circle_path.closeSubpath() return circle_path
def draw_circular_bar( painter: QPainter, radius: int, angle: int, span: int, width: int, fill_brush: QBrush, line_brush: QBrush, line_width: int ): """ Draw a circular bar using the QPainter handler given by the outer caller. :param painter: Painter handler :param radius: Circular radius :param angle: Starting angle :param span: Span angle :param width: Width between circular :param fill_brush: Color pattern used to fill the path :param line_brush: Color pattern used to draw lines :param line_width: Line width """ painter.save() path = QPainterPath() path.moveTo( QPointF( (radius - width / 2) * cos(radians(angle)) + radius, -(radius - width / 2) * sin(radians(angle)) + radius ) ) path.lineTo( QPointF( radius * cos(radians(angle)) + radius, -radius * sin(radians(angle)) + radius ) ) path.arcTo(QRectF(0, 0, radius * 2, radius * 2), angle, span) path.lineTo( QPointF( (radius - width / 2) * cos(radians(angle + span)) + radius, -(radius - width / 2) * sin(radians(angle + span)) + radius ) ) path.arcTo( QRectF( width // 2, width // 2, radius * 2 - width, radius * 2 - width ), angle + span, -span ) pen = QPen(line_brush, line_width) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen) painter.fillPath(path, fill_brush) painter.drawPath(path) painter.restore()
def get_circle_seed_path(circle_seed) -> QPainterPath: rect = QRectF(circle_seed.position.x() - circle_seed.radius, circle_seed.position.y() - circle_seed.radius, circle_seed.radius * 2, circle_seed.radius * 2) seed_path = QPainterPath() seed_path.arcMoveTo(rect, 0) seed_path.arcTo(rect, 0, 360) seed_path.closeSubpath() return seed_path
def drawBody(self, qp): #画身体 path4 = QPainterPath() body_polygon = QPolygonF() body_polygon << QPointF(316.0, 349.0) << QPointF( 580.0, 349.0) << QPointF(577.0, 496.0) << QPointF(319.0, 496.0) # 绘制一个多边形 path4.addPolygon(body_polygon) path4.closeSubpath() qp.setBrush(QColor(156, 214, 239)) qp.drawPath(path4) #画肚子 path5 = QPainterPath() belly_width = 180.0 belly_length = 235.0 bellyre = QRectF(365.0, 240.0, belly_width, belly_length) belly_center_x = 365.0 + belly_width / 2 belly_center_y = 240.0 + belly_length / 2 path5.moveTo(belly_center_x, belly_center_y) path5.arcTo(bellyre, 180, 180) path5.closeSubpath() qp.setBrush(Qt.white) qp.drawPath(path5) #画围兜 path6 = QPainterPath() scarf_polygon = QPolygonF() scarf_polygon << QPointF(316.0, 349.0) << QPointF( 580.0, 349.0) << QPointF(450.0, 379.0) << QPointF(319.0, 349.0) path6.addPolygon(scarf_polygon) path6.closeSubpath() qp.setBrush(Qt.red) qp.drawPath(path6) #画铃铛 bell_re = QRectF(442.0, 364.0, 30, 30) qp.setBrush(QColor(250, 221, 81)) qp.drawEllipse(bell_re) qp.drawLine(455, 370, 465, 380) qp.drawLine(465, 380, 455, 385) #画口袋 path7 = QPainterPath() pocket_width = 112.0 pocket_length = 104.0 pocketre = QRectF(400.0, 352.0, pocket_width, pocket_length) pocket_center_x = 412.0 + pocket_width / 2 pocket_center_y = 352.0 + pocket_length / 2 path7.moveTo(pocket_center_x, pocket_center_y) path7.arcTo(pocketre, 180, 180) path7.closeSubpath() qp.setBrush(Qt.white) qp.drawPath(path7)
def drawMouth(self, qp): #画嘴巴 path3 = QPainterPath() mouth_diameter = 203.0 mouthre = QRectF(352.0, 129.0, mouth_diameter, mouth_diameter) mouth_center_x = 352.0 + mouth_diameter / 2 mouth_center_y = 129.0 + mouth_diameter / 2 path3.moveTo(mouth_center_x, mouth_center_y) path3.arcTo(mouthre, 180, 180) path3.closeSubpath() qp.setBrush(QColor(219, 36, 28)) qp.drawPath(path3)
def _getRightTopCornerPath(self): width = self.width() borderRadius = self.borderRadius shadowMargin = self.shadowMargin shadowRadius = self.shadowRadius path = QPainterPath() path.moveTo(width - shadowRadius, 0) path.arcTo(width - 2 * shadowRadius, 0, 2 * shadowRadius, 2 * shadowRadius, 90, -90) path.lineTo(width - shadowMargin, shadowRadius) path.arcTo(width - shadowRadius - borderRadius, shadowMargin, 2 * borderRadius, 2 * borderRadius, 0, 90) path.lineTo(width - shadowRadius, 0) return path
def _getLeftBottomCornerPath(self): height = self.height() borderRadius = self.borderRadius shadowMargin = self.shadowMargin shadowRadius = self.shadowRadius path = QPainterPath() path.moveTo(0, height - shadowRadius) path.arcTo(0, height - 2 * shadowRadius, 2 * shadowRadius, 2 * shadowRadius, 180, 90) path.lineTo(shadowRadius, height - shadowMargin) path.arcTo(shadowMargin, height - shadowRadius - borderRadius, 2 * borderRadius, 2 * borderRadius, 270, -90) path.lineTo(0, height - shadowRadius) return path
def paintArc(self, st, p, coord, color): r1 = self.grid_size // 2 - self.radius r2 = self.grid_size // 2 + self.radius if st == 0: ang1 = 0 ang2 = -90 span = -90 rect1 = QRectF(coord[0] - r1, coord[1] - r1, 2 * r1, 2 * r1) rect2 = QRectF(coord[0] - r2, coord[1] - r2, 2 * r2, 2 * r2) kp = QPoint(coord[0] + r2, coord[1]) if st == 1: ang1 = -90 ang2 = -180 span = -90 rect1 = QRectF(coord[0] + self.grid_size - r1, coord[1] - r1, 2 * r1, 2 * r1) rect2 = QRectF(coord[0] + self.grid_size - r2, coord[1] - r2, 2 * r2, 2 * r2) kp = QPoint(coord[0] + self.grid_size, coord[1] + r2) if st == 2: ang1 = 90 ang2 = 180 span = 90 rect1 = QRectF(coord[0] + self.grid_size - r1, coord[1] + self.grid_size - r1, 2 * r1, 2 * r1) rect2 = QRectF(coord[0] + self.grid_size - r2, coord[1] + self.grid_size - r2, 2 * r2, 2 * r2) kp = QPoint(coord[0] + self.grid_size, coord[1] + self.grid_size + r2) if st == 3: ang1 = 0 ang2 = 90 span = 90 rect1 = QRectF(coord[0] - r1, coord[1] + self.grid_size - r1, 2 * r1, 2 * r1) rect2 = QRectF(coord[0] - r2, coord[1] + self.grid_size - r2, 2 * r2, 2 * r2) kp = QPoint(coord[0] + r2, coord[1] + self.grid_size) path = QPainterPath() path.moveTo(kp) path.arcTo(rect1, ang1, span) path.arcTo(rect2, ang2, -span) brush = QBrush() brush.setColor(color) brush.setStyle(Qt.SolidPattern) p.drawArc(rect1, ang1 * 16, span * 16) p.drawArc(rect2, ang2 * 16, -span * 16) p.fillPath(path, brush)
def _getRightBottomCornerPath(self): width = self.width() height = self.height() borderRadius = self.borderRadius shadowMargin = self.shadowMargin shadowRadius = self.shadowRadius path = QPainterPath() path.moveTo(width - shadowRadius, height) path.arcTo(width - 2 * shadowRadius, height - 2 * shadowRadius, 2 * shadowRadius, 2 * shadowRadius, 270, 90) path.lineTo(width - shadowMargin, height - shadowRadius) path.arcTo(width - shadowRadius - borderRadius, height - shadowRadius - borderRadius, 2 * borderRadius, 2 * borderRadius, 0, -90) path.lineTo(width - shadowRadius, height) return path
def paint(self, painter, option, widget): color = Qt.red if self.isSelected() else Qt.black painter.setPen(QPen(color, 2, Qt.SolidLine)) path = QPainterPath(self.startPoint) # start path # iterating over all points of line for i in range(len(self.points) - 1): x1, y1 = self.points[i].x(), self.points[i].y() x2, y2 = self.points[i + 1].x(), self.points[i + 1].y() for point in sorted(self.commonPathsCenters, key=lambda x: x.x() + x.y(), reverse=x2 < x1 or y2 < y1): x, y = point.x(), point.y() if x == x1 == x2: # vertical if min(y1, y2) + 8 <= y < max(y1, y2) - 8: if y2 > y1: path.lineTo(point - QPointF(0, 8)) path.arcTo(QRectF(x - 8, y - 8, 16, 16), 90, -180) path.moveTo(point + QPointF(0, 8)) else: path.lineTo(point + QPointF(0, 8)) path.arcTo(QRectF(x - 8, y - 8, 16, 16), -90, 180) path.moveTo(point - QPointF(0, 8)) elif y == y1 == y2: # horizontal if min(x1, x2) + 8 <= x < max(x1, x2) - 8: if x2 > x1: path.lineTo(point - QPointF(8, 0)) path.arcTo(QRectF(x - 8, y - 8, 16, 16), 180, 180) path.moveTo(point + QPointF(8, 0)) else: path.lineTo(point + QPointF(8, 0)) path.arcTo(QRectF(x - 8, y - 8, 16, 16), 0, -180) path.lineTo(point - QPointF(8, 0)) path.lineTo(self.points[i + 1]) # draw arrow in last segment if i == len(self.points) - 2: arrow_size = 20.0 line = QLineF(self.points[i], self.points[i + 1]) if line.length() < 20: continue angle = math.acos(line.dx() / line.length()) if line.dy() >= 0: angle = (math.pi * 2) - angle arrow_p1 = line.p2() - QPointF(math.sin(angle + math.pi / 2.5) * arrow_size, math.cos(angle + math.pi / 2.5) * arrow_size) arrow_p2 = line.p2() - QPointF(math.sin(angle + math.pi - math.pi / 2.5) * arrow_size, math.cos(angle + math.pi - math.pi / 2.5) * arrow_size) arrowHead = QPolygonF() arrowHead.append(line.p2()) arrowHead.append(arrow_p1) arrowHead.append(arrow_p2) painter.save() painter.setBrush(Qt.black) painter.drawPolygon(arrowHead) painter.restore() painter.drawPath(path) # draw final path
def _getLeftTopCornerPath(self): borderRadius = self.borderRadius shadowMargin = self.shadowMargin shadowRadius = self.shadowRadius path = QPainterPath() path.moveTo(self.shadowRadius, 0) path.arcTo(0, 0, 2 * shadowRadius, 2 * shadowRadius, 90, 90) path.lineTo(shadowMargin, shadowRadius) path.arcTo(shadowMargin, shadowMargin, 2 * borderRadius, 2 * borderRadius, 180, -90) path.lineTo(shadowRadius, 0) return path
def set_shape(self, width, height): ''' ANSWER has round, disjoint sides - does not fit in a polygon ''' self.width, self.height = width, height point = 20 #width / 2.85 path = QPainterPath() left = QRect(0, 0, point, height) right = QRect(width - point, 0, point, height) path.arcMoveTo(left, 125) path.arcTo(left, 125, 110) path.arcMoveTo(right, -55) path.arcTo(right, -55, 110) path.moveTo(width, height) self.setPath(path) super(DecisionAnswer, self).set_shape(width, height)
class ModCloseButton(QPushButton): def __init__(self,parent,wide,high,ppath=None): QPushButton.__init__(self,parent) self.parent=parent self.wide=wide self.high=high self.resize(self.wide,self.high) self.xdis=self.wide/10 #self.setAttribute(Qt.WA_TranslucentBackground, True) self.backgroundColor = QPalette().light().color() self.backgroundColor.setRgb(157,157,157) #(220,203,231) #self.backgroundColor.setAlpha(100) self.brush=QBrush(Qt.SolidPattern) if ppath : self.path=ppath else : self.path=QPainterPath() self.path.moveTo(self.wide/2, self.high/2-self.xdis) self.path.arcTo(0,0, self.wide-2*self.xdis, self.high-2*self.xdis,45,90) self.path.closeSubpath() self.path.moveTo(self.wide/2-self.xdis, self.high/2) self.path.arcTo(0,0,self.wide-2*self.xdis,self.high-2*self.xdis,135,90) self.path.closeSubpath() self.path.moveTo(self.wide/2, self.high/2+self.xdis) self.path.arcTo(0,0,self.wide-2*self.xdis, self.high-2*self.xdis,225,90) self.path.closeSubpath() self.path.moveTo(self.wide/2+self.xdis, self.high/2) self.path.arcTo(0,0,self.wide-2*self.xdis, self.high-2*self.xdis,315,90) self.path.closeSubpath() def paintEvent(self,event): self.brush.setColor(self.backgroundColor) self.painter=QPainter(self) self.painter.setRenderHint(QPainter.Antialiasing) self.painter.setPen(Qt.NoPen) self.painter.setBrush(self.brush) self.painter.drawPath(self.path) self.painter.end() def mousePressEvent(self,ev): self.parent.close() def enterEvent(self,ev): self.backgroundColor.setRgb(234,39,13) self.update() def leaveEvent(self,ev): self.backgroundColor.setRgb(157,157,157) self.update()
def updatePath(self, margin=2): r, R, a, A = self.boundings() sgm = SegmentShapeEngine(r, a, R-r, A-a) pts = tuple(map(lambda p: (p[0], -p[1]), sgm.points_ra(margin))) (r, al), (_, aL), (R, AL), (_, Al) = sgm.points_RA(margin) p = QPainterPath() p.moveTo(*pts[0]) p.lineTo(*pts[1]) # p.lineTo(*pts[2]) p.arcTo(QRectF(-R, -R, 2*R, 2*R), aL, AL-aL) p.lineTo(*pts[3]) # p.lineTo(*pts[0]) p.arcTo(QRectF(-r, -r, 2*r, 2*r), Al, -(Al-al)) self._path = p self._text_rf = QRectF(*sgm.text_bbox_SCR())
def drawSupport(self, p: QPainter, dir=0): # 0 down, 1 left, 2 up, 3 right p.save() #p.setTransform(QTransform().translate(self.x, self.y).rotate(90*dir)) # p.rotate(90*dir) p.translate(self.x, self.y) p.rotate(90 * dir) path = QPainterPath() path.moveTo(self.radius, 0) path.lineTo(self.radius, -self.radius) path.lineTo(-self.radius, -self.radius) path.lineTo(-self.radius, 0) # path.cubicTo(self.x+self.radius, self.y+self.radius*, self.x - # self.radius, self.y+self.radius*math.sqrt(2), self.x-self.radius, self.y) path.arcTo(-self.radius, -self.radius, self.radius * 2, self.radius * 2, 180, 180) path.closeSubpath() p.drawPath(path) p.restore()
def paintEvent(self, event: QtGui.QPaintEvent) -> None: super().paintEvent(event) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) pen = QPen(Qt.red) pen.setWidth(2) painter.setPen(pen) if self._bezier_path is None and self._init_line: painter.drawLine(self._init_line[0], self._init_line[1]) if self._bezier_path is not None: self._bezier_path.paint(painter) p = QPainterPath() p.arcMoveTo(100, 100, 100, 50, 30) p.arcTo(100, 100, 100, 50, 30, 120) n = QPainterPath() n.moveTo(100, 110) n.lineTo(200, 110)
def shape(self): # print(2) if self.centerPoint: r = ((self.centerPoint.x() - self.initialPoint.position.x())**2 + (self.centerPoint.y() - self.initialPoint.position.y())**2)**( 1 / 2) w, h = 2 * r, 2 * r pf = self.path().pointAtPercent(1 - 4 / self.path().length()) pi = self.path().pointAtPercent(4 / self.path().length()) line1 = QLineF(self.centerPoint.position, pi) line2 = QLineF(self.centerPoint.position, pf) startAngle, arcLength = line1.angle(), -(line1.angle() - line2.angle()) if arcLength == 0: arcLength = 360 path = QPainterPath() path.moveTo(pi) path.arcTo(self.path().boundingRect(), startAngle, arcLength) path.arcTo(self.path().boundingRect(), line2.angle(), -arcLength) return path else: return super(arcEdge, self).shape()
def createRotateArrow(): arrowHeadPos = 12 arrowHeadLength = 4.5 arrowHeadWidth = 5 bodyWidth = 1.5 outerArcSize = arrowHeadPos + bodyWidth - arrowHeadLength innerArcSize = arrowHeadPos - bodyWidth - arrowHeadLength path = QPainterPath() path.moveTo(arrowHeadPos, 0) path.lineTo(arrowHeadPos + arrowHeadWidth, arrowHeadLength) path.lineTo(arrowHeadPos + bodyWidth, arrowHeadLength) path.arcTo( QRectF(arrowHeadLength - outerArcSize, arrowHeadLength - outerArcSize, outerArcSize * 2, outerArcSize * 2), 0, -90) path.lineTo(arrowHeadLength, arrowHeadPos + arrowHeadWidth) path.lineTo(0, arrowHeadPos) path.lineTo(arrowHeadLength, arrowHeadPos - arrowHeadWidth) path.lineTo(arrowHeadLength, arrowHeadPos - bodyWidth) path.arcTo( QRectF(arrowHeadLength - innerArcSize, arrowHeadLength - innerArcSize, innerArcSize * 2, innerArcSize * 2), -90, 90) path.lineTo(arrowHeadPos - arrowHeadWidth, arrowHeadLength) path.closeSubpath() return path
def _drawAutoencoder(self, painter: QPainter, rect: QRect) -> None: """Draw a given portion of this widget. Parameters ---------- painter: rect: """ pen = QPen(Qt.red) penWidth = 1 pen.setWidth(penWidth) painter.setPen(pen) painter.drawLine(rect.topLeft(), rect.bottomRight()) path = QPainterPath() path.moveTo(rect.topLeft()) centerRect = QRectF(rect.left() + (rect.width() *.3), rect.top() + (rect.height() *.4), rect.width() *.2, rect.height() *.2) # path.lineTo(rect.left() + (rect.width() / 2), # rect.top() + (rect.height() / 2)) path.arcTo(centerRect, 45, -90) path.lineTo(rect.bottomLeft()) path.lineTo(rect.topLeft()) path.closeSubpath() painter.fillPath(path, QBrush(QColor("blue"))) path = QPainterPath() path.moveTo(rect.topRight()) path.lineTo(rect.left() + (rect.width() / 2), rect.top() + (rect.height() / 2)) path.lineTo(rect.bottomRight()) path.lineTo(rect.bottomRight()) path.closeSubpath() painter.fillPath(path, QBrush(QColor("blue")))
def paintEvent(self, event): a = self.height() / 2 * 0.4 painter = QPainter(self) painter.setOpacity(self.opacity) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setPen(Qt.NoPen) painter.save() brush = QBrush(self.color1) painter.setBrush(brush) path = QPainterPath() path.setFillRule(Qt.OddEvenFill) path.moveTo(self.p1) rect = QRectF(0, 0, a * 2, self.height()) path.arcTo(rect, 90, 180) path.lineTo(self.p2) path.lineTo(self.p3) path.lineTo(self.p4) path.moveTo(self.p5) rect = QRectF(self.p5.x() - a, 0, a * 2, self.height()) path.arcTo(rect, 90, 180) painter.drawPath(path) painter.restore() if self.p5 == self.p9: return brush = QBrush(self.color2) painter.setBrush(brush) path = QPainterPath() path.setFillRule(Qt.OddEvenFill) path.moveTo(self.p5) rect = QRectF(self.p5.x() - a, 0, a * 2, self.height()) path.arcTo(rect, 90, 180) path.lineTo(self.p6) path.lineTo(self.p7) path.lineTo(self.p8) path.moveTo(self.p9) rect = QRectF(self.p9.x() - a, 0, a * 2, self.height()) path.arcTo(rect, 90, 180) painter.drawPath(path) path = QPainterPath() path.setFillRule(Qt.OddEvenFill) path.moveTo(self.p1)
def __init__(self, diagramType, contextMenu, parent=None): super(DiagramItem, self).__init__(parent) self.arrows = [] self.diagramType = diagramType self.contextMenu = contextMenu path = QPainterPath() if self.diagramType == self.StartEnd: path.moveTo(200, 50) path.arcTo(150, 0, 50, 50, 0, 90) path.arcTo(50, 0, 50, 50, 90, 90) path.arcTo(50, 50, 50, 50, 180, 90) path.arcTo(150, 50, 50, 50, 270, 90) path.lineTo(200, 25) self.myPolygon = path.toFillPolygon() elif self.diagramType == self.Conditional: self.myPolygon = QPolygonF( [ QPointF(-100, 0), QPointF(0, 100), QPointF(100, 0), QPointF(0, -100), QPointF(-100, 0), ] ) elif self.diagramType == self.Step: self.myPolygon = QPolygonF( [ QPointF(-100, -100), QPointF(100, -100), QPointF(100, 100), QPointF(-100, 100), QPointF(-100, -100), ] ) else: self.myPolygon = QPolygonF( [ QPointF(-120, -80), QPointF(-70, 80), QPointF(120, 80), QPointF(70, -80), QPointF(-120, -80), ] ) self.setPolygon(self.myPolygon) self.setFlag(QGraphicsItem.ItemIsMovable, True) self.setFlag(QGraphicsItem.ItemIsSelectable, True)
class ControlButton(QWidget): def __init__(self, index, name, func, parent): super().__init__(parent) self.index = index self.name = name self.function = func self.resize(TILE_WIDTH << 2, TILE_HEIGHT << 2) self.show() self.opacity = None self.setMouseTracking(True) self.color = QColor(50 * self.index, 255, 255 - 10 * self.index) self.gradient = QRadialGradient( 50 + 10 * self.index, 50 - 10 * self.index, 500 - 50 * self.index, 50 + 10 * self.index, 100 - 10 * self.index) self.gradient.setColorAt(0, QColor(200, 200, 200)) self.gradient.setColorAt(0.5, QColor(50 * self.index, 255, 255 - 10 * self.index)) self.gradient.setColorAt(1, QColor(200, 200, 200)) self.brush = QBrush(self.gradient) self.path = QPainterPath() rect = QRectF(TILE_WIDTH * 1.5, TILE_HEIGHT * 1.5, TILE_WIDTH * 1.5, TILE_HEIGHT * 1.5) total = self.parent().total - 1 if total < 3: total = 3 if self.index == 0: self.path.arcMoveTo(rect, 90) self.path.arcTo(rect, 90, 360) else: self.path.arcMoveTo(QRectF(self.rect()), 90 + self.index * 360 // total) self.path.arcTo(QRectF(self.rect()), 90 + self.index * 360 // total, 360 // total) self.path.arcTo(rect, 90 + self.index * 360 // total + 360 // total, -360 // total) self.path.arcTo(QRectF(self.rect()), 90 + self.index * 360 // total, 0) def paintEvent(self, event): painter = QPainter(self) painter.setBrush(self.brush) if self.opacity is None or self.parent().vanishing: painter.setOpacity(self.parent().opacity / 255) else: painter.setOpacity(self.opacity / 255) pen = QPen(QColor(100, 100, 100, 150)) pen.setWidth(10) painter.setPen(pen) painter.drawPath(self.path) painter.setPen(QColor(0, 0, 0)) painter.drawText(self.path.controlPointRect(), Qt.AlignCenter, self.name)
def __init__(self, diagramType, contextMenu, parent=None): super(DiagramItem, self).__init__(parent) self.arrows = [] self.diagramType = diagramType self.contextMenu = contextMenu path = QPainterPath() if self.diagramType == self.StartEnd: path.moveTo(200, 50) path.arcTo(150, 0, 50, 50, 0, 90) path.arcTo(50, 0, 50, 50, 90, 90) path.arcTo(50, 50, 50, 50, 180, 90) path.arcTo(150, 50, 50, 50, 270, 90) path.lineTo(200, 25) self.myPolygon = path.toFillPolygon() elif self.diagramType == self.Conditional: self.myPolygon = QPolygonF([ QPointF(-100, 0), QPointF(0, 100), QPointF(100, 0), QPointF(0, -100), QPointF(-100, 0)]) elif self.diagramType == self.Step: self.myPolygon = QPolygonF([ QPointF(-100, -100), QPointF(100, -100), QPointF(100, 100), QPointF(-100, 100), QPointF(-100, -100)]) else: self.myPolygon = QPolygonF([ QPointF(-120, -80), QPointF(-70, 80), QPointF(120, 80), QPointF(70, -80), QPointF(-120, -80)]) self.setPolygon(self.myPolygon) self.setFlag(QGraphicsItem.ItemIsMovable, True) self.setFlag(QGraphicsItem.ItemIsSelectable, True)
def paintEvent(self, evt): x1 = QPoint(0, -70) x2 = QPoint(0, -90) x3 = QPoint(-90, 0) x4 = QPoint(-70, 0) extRect = QRectF(-90, -90, 180, 180) intRect = QRectF(-70, -70, 140, 140) midRect = QRectF(-44, -80, 160, 160) unitRect = QRectF(-50, 60, 110, 50) speedInt = self.speed #speedDec = (self.speed * 10.0) - (speedInt * 10) s_SpeedInt = speedInt.__str__()[0:4] powerAngle = self.power * 270.0 / 100.0 dummyPath = QPainterPath() dummyPath.moveTo(x1) dummyPath.arcMoveTo(intRect, 90 - powerAngle) powerPath = QPainterPath() powerPath.moveTo(x1) powerPath.lineTo(x2) powerPath.arcTo(extRect, 90, -1 * powerAngle) powerPath.lineTo(dummyPath.currentPosition()) powerPath.arcTo(intRect, 90 - powerAngle, powerAngle) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) side = min(self.width(), self.height()) painter.scale(side / 200.0, side / 200.0) painter.save() painter.rotate(-135) if self.displayPowerPath: externalPath = QPainterPath() externalPath.moveTo(x1) externalPath.lineTo(x2) externalPath.arcTo(extRect, 90, -270) externalPath.lineTo(x4) externalPath.arcTo(intRect, 180, 270) painter.setPen(self.powerPathColor) painter.drawPath(externalPath) painter.setBrush(self.powerGradient) painter.setPen(Qt.NoPen) painter.drawPath(powerPath) painter.restore() painter.save() painter.translate(QPointF(0, -50)) painter.setPen(self.unitTextColor) fontFamily = self.font().family() unitFont = QFont(fontFamily, 9) painter.setFont(unitFont) painter.drawText(unitRect, Qt.AlignCenter, "{}".format(self.unit)) painter.restore() painter.setPen(self.unitTextColor) fontFamily = self.font().family() unitFont = QFont(fontFamily, 12) painter.setFont(unitFont) painter.drawText(unitRect, Qt.AlignCenter, "{}".format(self.title)) speedColor = QColor(0, 0, 0) speedFont = QFont(fontFamily, 30) fm1 = QFontMetrics(speedFont) speedWidth = fm1.width(s_SpeedInt) #speedDecFont = QFont(fontFamily, 23) #fm2 = QFontMetrics(speedDecFont) #speedDecWidth = fm2.width(s_SpeedDec) leftPos = -1 * speedWidth + 40 leftDecPos = leftPos + speedWidth topPos = 10 topDecPos = 10 painter.setPen(self.speedTextColor) painter.setFont(speedFont) painter.drawText(leftPos, topPos, s_SpeedInt)
def _drawGradientShadow(self, painter, width, height): shadowRadius = self.shadowRadius shadowMargin = self.shadowMargin borderRadius = self.borderRadius painter.setPen(Qt.NoPen) # Draw top left radius radial = self._createRadialGradient(shadowRadius, shadowRadius, shadowRadius) painter.setBrush(radial) path = QPainterPath() path.moveTo(shadowRadius, 0) path.arcTo(0, 0, 2 * shadowRadius, 2 * shadowRadius, 90, 90) path.lineTo(shadowMargin, shadowRadius) path.arcTo(shadowMargin, shadowMargin, 2 * borderRadius, 2 * borderRadius, 180, -90) path.lineTo(shadowRadius, 0) painter.drawPath(path) # Draw top right radius path = QPainterPath() radial = self._createRadialGradient(width - shadowRadius, shadowRadius, shadowRadius) painter.setBrush(radial) path.moveTo(width - shadowRadius, 0) path.arcTo(width - 2 * shadowRadius, 0, 2 * shadowRadius, 2 * shadowRadius, 90, -90) path.lineTo(width - shadowMargin, shadowRadius) path.arcTo(width - shadowRadius - borderRadius, shadowMargin, 2 * borderRadius, 2 * borderRadius, 0, 90) path.lineTo(width - shadowRadius, 0) painter.drawPath(path) # Draw bottom left radius path = QPainterPath() radial = self._createRadialGradient(shadowRadius, height - shadowRadius, shadowRadius) painter.setBrush(radial) path.moveTo(0, height - shadowRadius) path.arcTo(0, height - 2 * shadowRadius, 2 * shadowRadius, 2 * shadowRadius, 180, 90) path.lineTo(shadowRadius, height - shadowMargin) path.arcTo(shadowMargin, height - shadowRadius - borderRadius, 2 * borderRadius, 2 * borderRadius, 270, -90) path.lineTo(0, height - shadowRadius) painter.drawPath(path) # Draw bottom right radius path = QPainterPath() radial = self._createRadialGradient(width - shadowRadius, height - shadowRadius, shadowRadius) painter.setBrush(radial) path.moveTo(width - shadowRadius, height) path.arcTo(width - 2 * shadowRadius, height - 2 * shadowRadius, 2 * shadowRadius, 2 * shadowRadius, 270, 90) path.lineTo(width - shadowMargin, height - shadowRadius) path.arcTo(width - shadowRadius - borderRadius, height - shadowRadius - borderRadius, 2 * borderRadius, 2 * borderRadius, 0, -90) path.lineTo(width - shadowRadius, height) painter.drawPath(path) # Draw left margin linear = self._createLinearGradient(shadowRadius, shadowRadius, 0, shadowRadius) painter.setBrush(linear) painter.drawRect(0, shadowRadius, shadowMargin, height - 2 * shadowRadius) # Draw right margin linear = self._createLinearGradient(width - shadowRadius, shadowRadius, width, shadowRadius) painter.setBrush(linear) painter.drawRect(width - shadowMargin, shadowRadius, shadowMargin, height - 2 * shadowRadius) # Draw top margin linear = self._createLinearGradient(shadowRadius, shadowRadius, shadowRadius, 0) painter.setBrush(linear) painter.drawRect(shadowRadius, 0, width - 2 * shadowRadius, shadowMargin) # Draw bottom margin linear = self._createLinearGradient(shadowRadius, height - shadowRadius, shadowRadius, height) painter.setBrush(linear) painter.drawRect(shadowRadius, height - shadowMargin, width - 2 * shadowRadius, shadowMargin)
def drawGlyphPoints( painter, glyph, scale, rect, drawStartPoints=True, drawOnCurves=True, drawOffCurves=True, drawCoordinates=False, drawSelection=True, onCurveColor=None, otherColor=None, backgroundColor=None): if onCurveColor is None: layer = glyph.layer if layer is not None and layer.color is not None: onCurveColor = colorToQColor(layer.color) else: onCurveColor = defaultColor("glyphOnCurvePoints") if otherColor is None: otherColor = defaultColor("glyphOtherPoints") if backgroundColor is None: backgroundColor = defaultColor("background") # get the outline data outlineData = glyph.getRepresentation("defconQt.OutlineInformation") points = [] # start points if drawStartPoints and outlineData["startPoints"]: startWidth = startHeight = 15 * scale startHalf = startWidth / 2.0 path = QPainterPath() for point, angle in outlineData["startPoints"]: x, y = point if angle is not None: path.moveTo(x, y) path.arcTo(x - startHalf, y - startHalf, startWidth, startHeight, 180 - angle, 180) path.closeSubpath() else: path.addEllipse( x - startHalf, y - startHalf, startWidth, startHeight) startPointColor = QColor(otherColor) aF = startPointColor.alphaF() startPointColor.setAlphaF(aF * .3) painter.fillPath(path, startPointColor) # handles if drawOffCurves and outlineData["offCurvePoints"]: painter.save() painter.setPen(otherColor) for pt1, pt2 in outlineData["bezierHandles"]: x1, y1 = pt1 x2, y2 = pt2 # TODO: should lineWidth account scale by default drawLine(painter, x1, y1, x2, y2, 1.0 * scale) painter.restore() # on curve if drawOnCurves and outlineData["onCurvePoints"]: width = 7 * scale half = width / 2.0 smoothWidth = 8 * scale smoothHalf = smoothWidth / 2.0 painter.save() path = QPainterPath() selectedPath = QPainterPath() for point in outlineData["onCurvePoints"]: x, y = point["point"] points.append((x, y)) pointPath = QPainterPath() if point["smooth"]: x -= smoothHalf y -= smoothHalf pointPath.addEllipse(x, y, smoothWidth, smoothWidth) else: x -= half y -= half pointPath.addRect(x, y, width, width) if drawSelection and point["selected"]: selectedPath.addPath(pointPath) path.addPath(pointPath) pen = QPen(onCurveColor) pen.setWidthF(1.5 * scale) painter.setPen(pen) painter.fillPath(selectedPath, onCurveColor) painter.drawPath(path) painter.restore() # off curve if drawOffCurves and outlineData["offCurvePoints"]: # lines # points offWidth = 5 * scale offHalf = offWidth / 2.0 path = QPainterPath() selectedPath = QPainterPath() for point in outlineData["offCurvePoints"]: x, y = point["point"] points.append((x, y)) pointPath = QPainterPath() x -= offHalf y -= offHalf pointPath.addEllipse(x, y, offWidth, offWidth) if drawSelection and point["selected"]: selectedPath.addPath(pointPath) else: path.addPath(pointPath) pen = QPen(otherColor) pen.setWidthF(3.0 * scale) painter.save() painter.setPen(pen) painter.drawPath(path) painter.fillPath(path, QBrush(backgroundColor)) painter.drawPath(selectedPath) painter.fillPath(selectedPath, QBrush(otherColor)) painter.restore() # coordinates if drawCoordinates: otherColor = QColor(otherColor) otherColor.setAlphaF(otherColor.alphaF() * .6) painter.save() painter.setPen(otherColor) # TODO: decision + color font = painter.font() font.setPointSize(7) painter.setFont(font) for x, y in points: posX = x # TODO: We use + here because we align on top. Consider abstracting # yOffset. posY = y + 3 x = round(x, 1) if int(x) == x: x = int(x) y = round(y, 1) if int(y) == y: y = int(y) text = "%d %d" % (x, y) drawTextAtPoint(painter, text, posX, posY, scale, xAlign="center", yAlign="top") painter.restore()
def drawGlyphPoints(painter, glyph, scale, rect, drawStartPoints=True, drawOnCurves=True, drawOffCurves=True, drawCoordinates=True, onCurveColor=None, otherColor=None, backgroundColor=None): layer = glyph.layer onCurveColor = None if layer is not None: if layer.color is not None: onCurveColor = colorToQColor(layer.color) if onCurveColor is None: onCurveColor = defaultColor("glyphOnCurvePoints") if otherColor is None: otherColor = defaultColor("glyphOtherPoints") if backgroundColor is None: backgroundColor = defaultColor("background") # get the outline data outlineData = glyph.getRepresentation("defconQt.OutlineInformation") points = [] # start points if drawStartPoints and outlineData["startPoints"]: startWidth = startHeight = 15 * scale startHalf = startWidth / 2.0 path = QPainterPath() for point, angle in outlineData["startPoints"]: x, y = point if angle is not None: path.moveTo(x, y) path.arcTo(x - startHalf, y - startHalf, startWidth, startHeight, 180 - angle, 180) path.closeSubpath() else: path.addEllipse(x - startHalf, y - startHalf, startWidth, startHeight) startPointColor = QColor(otherColor) aF = startPointColor.alphaF() startPointColor.setAlphaF(aF * .3) painter.fillPath(path, startPointColor) # off curve if drawOffCurves and outlineData["offCurvePoints"]: # lines painter.save() painter.setPen(otherColor) for pt1, pt2 in outlineData["bezierHandles"]: x1, y1 = pt1 x2, y2 = pt2 # TODO: should lineWidth account scale by default drawLine(painter, x1, y1, x2, y2, 1.0 * scale) # points offWidth = 5 * scale offHalf = offWidth / 2.0 path = QPainterPath() selectedPath = QPainterPath() for point in outlineData["offCurvePoints"]: x, y = point["point"] points.append((x, y)) pointPath = QPainterPath() x -= offHalf y -= offHalf pointPath.addEllipse(x, y, offWidth, offWidth) if point["selected"]: selectedPath.addPath(pointPath) else: path.addPath(pointPath) pen = QPen(otherColor) pen.setWidthF(3.0 * scale) painter.setPen(pen) painter.drawPath(path) painter.fillPath(path, QBrush(backgroundColor)) painter.drawPath(selectedPath) painter.fillPath(selectedPath, QBrush(otherColor)) painter.restore() # on curve if drawOnCurves and outlineData["onCurvePoints"]: width = 7 * scale half = width / 2.0 smoothWidth = 8 * scale smoothHalf = smoothWidth / 2.0 painter.save() path = QPainterPath() selectedPath = QPainterPath() for point in outlineData["onCurvePoints"]: x, y = point["point"] points.append((x, y)) pointPath = QPainterPath() if point["smooth"]: x -= smoothHalf y -= smoothHalf pointPath.addEllipse(x, y, smoothWidth, smoothWidth) else: x -= half y -= half pointPath.addRect(x, y, width, width) if point["selected"]: selectedPath.addPath(pointPath) path.addPath(pointPath) pen = QPen(onCurveColor) pen.setWidthF(1.5 * scale) painter.setPen(pen) painter.fillPath(selectedPath, onCurveColor) painter.drawPath(path) painter.restore() # coordinates if drawCoordinates: otherColor = QColor(otherColor) otherColor.setAlphaF(otherColor.alphaF() * .6) painter.save() painter.setPen(otherColor) for x, y in points: posX = x # TODO: We use + here because we align on top. Consider abstracting # yOffset. posY = y + 3 x = round(x, 1) if int(x) == x: x = int(x) y = round(y, 1) if int(y) == y: y = int(y) text = "%d %d" % (x, y) drawTextAtPoint(painter, text, posX, posY, scale, xAlign="center", yAlign="top") painter.restore()
def paintEvent(self, pe): p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) pen = QPen() pen.setWidth(1) pen.setColor(QColor(0x8c, 0xa3, 0xb0)) p.setPen(pen) p.setBrush(QColor(0xe4, 0xec, 0xf4)) rx = 6 space = self.space w = self.usable_width kw = self.key_w def drawRow(row, sx, sy, last_end=False): x = sx y = sy keys = row rw = w - sx i = 0 for k in keys: rect = QRectF(x, y, kw, kw) if i == len(keys) - 1 and last_end: rect.setWidth(rw) p.drawRoundedRect(rect, rx, rx) p.setPen(Qt.black) rect.adjust(5, 1, 0, 0) p.setFont(self.lowerFont) p.drawText( rect, Qt.AlignLeft | Qt.AlignBottom, self.regular_text(k)) p.setFont(self.upperFont) p.drawText( rect, Qt.AlignLeft | Qt.AlignTop, self.shift_text(k)) rw = rw - space - kw x = x + space + kw i = i + 1 p.setPen(pen) return (x, rw) x = .5 y = .5 keys = self.kb["keys"] ext_return = self.kb["extended_return"] first_key_w = 0 rows = 4 remaining_x = [0, 0, 0, 0] remaining_widths = [0, 0, 0, 0] for i in range(0, rows): if first_key_w > 0: first_key_w = first_key_w * 1.375 if self.kb == self.kb_105 and i == 3: first_key_w = kw * 1.275 rect = QRectF(x, y, first_key_w, kw) p.drawRoundedRect(rect, rx, rx) x = x + first_key_w + space else: first_key_w = kw x, rw = drawRow(keys[i], x, y, i == 1 and not ext_return) remaining_x[i] = x remaining_widths[i] = rw if i != 1 and i != 2: rect = QRectF(x, y, rw, kw) p.drawRoundedRect(rect, rx, rx) x = .5 y = y + space + kw if ext_return: rx = rx * 2 x1 = remaining_x[1] y1 = .5 + kw * 1 + space * 1 w1 = remaining_widths[1] x2 = remaining_x[2] y2 = .5 + kw * 2 + space * 2 # this is some serious crap... but it has to be so # maybe one day keyboards won't look like this... # one can only hope pp = QPainterPath() pp.moveTo(x1, y1 + rx) pp.arcTo(x1, y1, rx, rx, 180, -90) pp.lineTo(x1 + w1 - rx, y1) pp.arcTo(x1 + w1 - rx, y1, rx, rx, 90, -90) pp.lineTo(x1 + w1, y2 + kw - rx) pp.arcTo(x1 + w1 - rx, y2 + kw - rx, rx, rx, 0, -90) pp.lineTo(x2 + rx, y2 + kw) pp.arcTo(x2, y2 + kw - rx, rx, rx, -90, -90) pp.lineTo(x2, y1 + kw) pp.lineTo(x1 + rx, y1 + kw) pp.arcTo(x1, y1 + kw - rx, rx, rx, -90, -90) pp.closeSubpath() p.drawPath(pp) else: x = remaining_x[2] y = .5 + kw * 2 + space * 2 rect = QRectF(x, y, remaining_widths[2], kw) p.drawRoundedRect(rect, rx, rx) QWidget.paintEvent(self, pe)
def __init__(self): super(Window, self).__init__() rectPath = QPainterPath() rectPath.moveTo(20.0, 30.0) rectPath.lineTo(80.0, 30.0) rectPath.lineTo(80.0, 70.0) rectPath.lineTo(20.0, 70.0) rectPath.closeSubpath() roundRectPath = QPainterPath() roundRectPath.moveTo(80.0, 35.0) roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0) roundRectPath.lineTo(25.0, 30.0) roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0) roundRectPath.lineTo(20.0, 65.0) roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0) roundRectPath.lineTo(75.0, 70.0) roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0) roundRectPath.closeSubpath() ellipsePath = QPainterPath() ellipsePath.moveTo(80.0, 50.0) ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0) piePath = QPainterPath() piePath.moveTo(50.0, 50.0) piePath.lineTo(65.0, 32.6795) piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0) piePath.closeSubpath() polygonPath = QPainterPath() polygonPath.moveTo(10.0, 80.0) polygonPath.lineTo(20.0, 10.0) polygonPath.lineTo(80.0, 30.0) polygonPath.lineTo(90.0, 70.0) polygonPath.closeSubpath() groupPath = QPainterPath() groupPath.moveTo(60.0, 40.0) groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0) groupPath.moveTo(40.0, 40.0) groupPath.lineTo(40.0, 80.0) groupPath.lineTo(80.0, 80.0) groupPath.lineTo(80.0, 40.0) groupPath.closeSubpath() textPath = QPainterPath() timesFont = QFont('Times', 50) timesFont.setStyleStrategy(QFont.ForceOutline) textPath.addText(10, 70, timesFont, "Qt") bezierPath = QPainterPath() bezierPath.moveTo(20, 30) bezierPath.cubicTo(80, 0, 50, 50, 80, 80) starPath = QPainterPath() starPath.moveTo(90, 50) for i in range(1, 5): starPath.lineTo(50 + 40 * cos(0.8 * i * pi), 50 + 40 * sin(0.8 * i * pi)) starPath.closeSubpath() self.renderAreas = [RenderArea(rectPath), RenderArea(roundRectPath), RenderArea(ellipsePath), RenderArea(piePath), RenderArea(polygonPath), RenderArea(groupPath), RenderArea(textPath), RenderArea(bezierPath), RenderArea(starPath)] assert len(self.renderAreas) == 9 self.fillRuleComboBox = QComboBox() self.fillRuleComboBox.addItem("Odd Even", Qt.OddEvenFill) self.fillRuleComboBox.addItem("Winding", Qt.WindingFill) fillRuleLabel = QLabel("Fill &Rule:") fillRuleLabel.setBuddy(self.fillRuleComboBox) self.fillColor1ComboBox = QComboBox() self.populateWithColors(self.fillColor1ComboBox) self.fillColor1ComboBox.setCurrentIndex( self.fillColor1ComboBox.findText("mediumslateblue")) self.fillColor2ComboBox = QComboBox() self.populateWithColors(self.fillColor2ComboBox) self.fillColor2ComboBox.setCurrentIndex( self.fillColor2ComboBox.findText("cornsilk")) fillGradientLabel = QLabel("&Fill Gradient:") fillGradientLabel.setBuddy(self.fillColor1ComboBox) fillToLabel = QLabel("to") fillToLabel.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.penWidthSpinBox = QSpinBox() self.penWidthSpinBox.setRange(0, 20) penWidthLabel = QLabel("&Pen Width:") penWidthLabel.setBuddy(self.penWidthSpinBox) self.penColorComboBox = QComboBox() self.populateWithColors(self.penColorComboBox) self.penColorComboBox.setCurrentIndex( self.penColorComboBox.findText('darkslateblue')) penColorLabel = QLabel("Pen &Color:") penColorLabel.setBuddy(self.penColorComboBox) self.rotationAngleSpinBox = QSpinBox() self.rotationAngleSpinBox.setRange(0, 359) self.rotationAngleSpinBox.setWrapping(True) self.rotationAngleSpinBox.setSuffix(u'\N{DEGREE SIGN}') rotationAngleLabel = QLabel("&Rotation Angle:") rotationAngleLabel.setBuddy(self.rotationAngleSpinBox) self.fillRuleComboBox.activated.connect(self.fillRuleChanged) self.fillColor1ComboBox.activated.connect(self.fillGradientChanged) self.fillColor2ComboBox.activated.connect(self.fillGradientChanged) self.penColorComboBox.activated.connect(self.penColorChanged) for i in range(Window.NumRenderAreas): self.penWidthSpinBox.valueChanged.connect(self.renderAreas[i].setPenWidth) self.rotationAngleSpinBox.valueChanged.connect(self.renderAreas[i].setRotationAngle) topLayout = QGridLayout() for i in range(Window.NumRenderAreas): topLayout.addWidget(self.renderAreas[i], i / 3, i % 3) mainLayout = QGridLayout() mainLayout.addLayout(topLayout, 0, 0, 1, 4) mainLayout.addWidget(fillRuleLabel, 1, 0) mainLayout.addWidget(self.fillRuleComboBox, 1, 1, 1, 3) mainLayout.addWidget(fillGradientLabel, 2, 0) mainLayout.addWidget(self.fillColor1ComboBox, 2, 1) mainLayout.addWidget(fillToLabel, 2, 2) mainLayout.addWidget(self.fillColor2ComboBox, 2, 3) mainLayout.addWidget(penWidthLabel, 3, 0) mainLayout.addWidget(self.penWidthSpinBox, 3, 1, 1, 3) mainLayout.addWidget(penColorLabel, 4, 0) mainLayout.addWidget(self.penColorComboBox, 4, 1, 1, 3) mainLayout.addWidget(rotationAngleLabel, 5, 0) mainLayout.addWidget(self.rotationAngleSpinBox, 5, 1, 1, 3) self.setLayout(mainLayout) self.fillRuleChanged() self.fillGradientChanged() self.penColorChanged() self.penWidthSpinBox.setValue(2) self.setWindowTitle("Painter Paths")
class RCircleSegment(QObject): def __init__(self, radius: float, center_x: float, center_y: float, start_angle: float, end_angle: float, clockwise, line_width: float, line_color=Qt.black, fill_color=Qt.transparent): super().__init__() # The supporting rectangle if end_angle < start_angle: end_angle += 2 * math.pi start_angle = -start_angle end_angle = -end_angle shift = end_angle - start_angle if clockwise: shift = -shift - 2 * math.pi x, y = center_x - radius, center_y - radius self.rect = QRectF(x, y, 2 * radius, 2 * radius) # The underlying QGraphicsPathItem self.painter_path = QPainterPath( QPointF(center_x + math.cos(start_angle) * radius, center_y - math.sin(start_angle) * radius)) self.painter_path.arcTo(self.rect, math.degrees(start_angle), math.degrees(shift)) self.path = QGraphicsPathItem(self.painter_path) self.path.setBrush(QtGui.QBrush(fill_color)) pen = QPen() pen.setWidthF(line_width) pen.setColor(line_color) self.path.setPen(pen) self._visible = 1 # def x(self): # return self._pos.x() # # def y(self): # return self._pos.y() # # # The following functions are for animation support # # @pyqtProperty(QPointF) # def pos(self): # return self._pos # # @pos.setter # def pos(self, value): # self.rect = QRectF(value.x() - self._radius, value.y() - self._radius, 2 * self._radius, 2 * self._radius) # self.path.setRect(self.rect) # self._pos = value # @pyqtProperty(int) def visible(self): return self._visible @visible.setter def visible(self, value): if (value > 0): self.path.show() else: self.path.hide() self._visible = value
def draw(self, path: qg.QPainterPath): path.arcTo(self.rect, self.start_angle, self.span_angle)
def __init__(self): super(Window, self).__init__() rectPath = QPainterPath() rectPath.moveTo(20.0, 30.0) rectPath.lineTo(80.0, 30.0) rectPath.lineTo(80.0, 70.0) rectPath.lineTo(20.0, 70.0) rectPath.closeSubpath() roundRectPath = QPainterPath() roundRectPath.moveTo(80.0, 35.0) roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0) roundRectPath.lineTo(25.0, 30.0) roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0) roundRectPath.lineTo(20.0, 65.0) roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0) roundRectPath.lineTo(75.0, 70.0) roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0) roundRectPath.closeSubpath() ellipsePath = QPainterPath() ellipsePath.moveTo(80.0, 50.0) ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0) piePath = QPainterPath() piePath.moveTo(50.0, 50.0) piePath.lineTo(65.0, 32.6795) piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0) piePath.closeSubpath() polygonPath = QPainterPath() polygonPath.moveTo(10.0, 80.0) polygonPath.lineTo(20.0, 10.0) polygonPath.lineTo(80.0, 30.0) polygonPath.lineTo(90.0, 70.0) polygonPath.closeSubpath() groupPath = QPainterPath() groupPath.moveTo(60.0, 40.0) groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0) groupPath.moveTo(40.0, 40.0) groupPath.lineTo(40.0, 80.0) groupPath.lineTo(80.0, 80.0) groupPath.lineTo(80.0, 40.0) groupPath.closeSubpath() textPath = QPainterPath() timesFont = QFont('Times', 50) timesFont.setStyleStrategy(QFont.ForceOutline) textPath.addText(10, 70, timesFont, "Qt") bezierPath = QPainterPath() bezierPath.moveTo(20, 30) bezierPath.cubicTo(80, 0, 50, 50, 80, 80) starPath = QPainterPath() starPath.moveTo(90, 50) for i in range(1, 5): starPath.lineTo(50 + 40 * cos(0.8 * i * pi), 50 + 40 * sin(0.8 * i * pi)) starPath.closeSubpath() self.renderAreas = [ RenderArea(rectPath), RenderArea(roundRectPath), RenderArea(ellipsePath), RenderArea(piePath), RenderArea(polygonPath), RenderArea(groupPath), RenderArea(textPath), RenderArea(bezierPath), RenderArea(starPath) ] assert len(self.renderAreas) == 9 self.fillRuleComboBox = QComboBox() self.fillRuleComboBox.addItem("Odd Even", Qt.OddEvenFill) self.fillRuleComboBox.addItem("Winding", Qt.WindingFill) fillRuleLabel = QLabel("Fill &Rule:") fillRuleLabel.setBuddy(self.fillRuleComboBox) self.fillColor1ComboBox = QComboBox() self.populateWithColors(self.fillColor1ComboBox) self.fillColor1ComboBox.setCurrentIndex( self.fillColor1ComboBox.findText("mediumslateblue")) self.fillColor2ComboBox = QComboBox() self.populateWithColors(self.fillColor2ComboBox) self.fillColor2ComboBox.setCurrentIndex( self.fillColor2ComboBox.findText("cornsilk")) fillGradientLabel = QLabel("&Fill Gradient:") fillGradientLabel.setBuddy(self.fillColor1ComboBox) fillToLabel = QLabel("to") fillToLabel.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.penWidthSpinBox = QSpinBox() self.penWidthSpinBox.setRange(0, 20) penWidthLabel = QLabel("&Pen Width:") penWidthLabel.setBuddy(self.penWidthSpinBox) self.penColorComboBox = QComboBox() self.populateWithColors(self.penColorComboBox) self.penColorComboBox.setCurrentIndex( self.penColorComboBox.findText('darkslateblue')) penColorLabel = QLabel("Pen &Color:") penColorLabel.setBuddy(self.penColorComboBox) self.rotationAngleSpinBox = QSpinBox() self.rotationAngleSpinBox.setRange(0, 359) self.rotationAngleSpinBox.setWrapping(True) self.rotationAngleSpinBox.setSuffix(u'\N{DEGREE SIGN}') rotationAngleLabel = QLabel("&Rotation Angle:") rotationAngleLabel.setBuddy(self.rotationAngleSpinBox) self.fillRuleComboBox.activated.connect(self.fillRuleChanged) self.fillColor1ComboBox.activated.connect(self.fillGradientChanged) self.fillColor2ComboBox.activated.connect(self.fillGradientChanged) self.penColorComboBox.activated.connect(self.penColorChanged) for i in range(Window.NumRenderAreas): self.penWidthSpinBox.valueChanged.connect( self.renderAreas[i].setPenWidth) self.rotationAngleSpinBox.valueChanged.connect( self.renderAreas[i].setRotationAngle) topLayout = QGridLayout() for i in range(Window.NumRenderAreas): topLayout.addWidget(self.renderAreas[i], i / 3, i % 3) mainLayout = QGridLayout() mainLayout.addLayout(topLayout, 0, 0, 1, 4) mainLayout.addWidget(fillRuleLabel, 1, 0) mainLayout.addWidget(self.fillRuleComboBox, 1, 1, 1, 3) mainLayout.addWidget(fillGradientLabel, 2, 0) mainLayout.addWidget(self.fillColor1ComboBox, 2, 1) mainLayout.addWidget(fillToLabel, 2, 2) mainLayout.addWidget(self.fillColor2ComboBox, 2, 3) mainLayout.addWidget(penWidthLabel, 3, 0) mainLayout.addWidget(self.penWidthSpinBox, 3, 1, 1, 3) mainLayout.addWidget(penColorLabel, 4, 0) mainLayout.addWidget(self.penColorComboBox, 4, 1, 1, 3) mainLayout.addWidget(rotationAngleLabel, 5, 0) mainLayout.addWidget(self.rotationAngleSpinBox, 5, 1, 1, 3) self.setLayout(mainLayout) self.fillRuleChanged() self.fillGradientChanged() self.penColorChanged() self.penWidthSpinBox.setValue(2) self.setWindowTitle("Painter Paths")