def render(self, painter: QPainter): if painter is None: return super().render(painter) vertices = self.geometry().data() self.set_boundary(painter, color=self._correspondence_color) self.set_filling(painter) for idx, correspondence_coord in \ enumerate(self._geometry.extra_coords()): src_point = QPointF(*vertices[idx, :]) dst_point = QPointF(*correspondence_coord) painter.drawLine(src_point, dst_point) # self.set_boundary(painter, color=QColor(155,0,191,255)) # self.set_filling(painter, color=QColor(155, 0, 191, 255)) if self._filled: for idx in range(len(self._geometry.extra_coords())-1): path = QPainterPath() point_0 = QPointF(*vertices[idx, :]) path.moveTo(point_0) point_1 = QPointF(*vertices[idx + 1, :]) path.lineTo(point_1) point_2 = QPointF(*self._geometry.extra_coords()[idx + 1, :]) path.lineTo(point_2) point_3 = QPointF(*self._geometry.extra_coords()[idx, :]) path.lineTo(point_3) path.lineTo(point_0) painter.fillPath(path, QColor(0, 191, 255, 100))
def paintEvent(self, event): # 圆角以及背景色 super(NotificationItem, self).paintEvent(event) painter = QPainter(self) path = QPainterPath() path.addRoundedRect(QRectF(self.rect()), 6, 6) painter.fillPath(path, Qt.white)
def paintEvent(self, event: QPaintEvent): """ 重写绘图事件 :param event: :return: """ try: backPath = QPainterPath() backPath.addRect(0, 0, self.width(), self.height()) fillPath = QPainterPath() if hasattr(self, "startPoint") and hasattr(self, "endPoint"): movePath = QPainterPath() movePath.addRect(QRectF(self.startPoint, self.endPoint)) fillPath = backPath.subtracted(movePath) else: fillPath = backPath # 创建绘图设备 painter = QPainter(self) # 绘制背景图 painter.drawImage(QPoint(0, 0), self.backImg) painter.setPen(QPen(QColor(87, 170, 255), 5, Qt.SolidLine)) # painter.drawPath(fillPath) # 填充非选择区域 painter.fillPath(fillPath, QColor(0, 0, 0, 100)) except Exception as e: traceback.print_exc()
def paintEvent(self, e): w = self.width() h = self.height() if self.enable_pulse == True: l = (w - 3) * 0.2 bar_start = ((w - 3) - l) * self.value else: l = (w - 3) * self.value bar_start = 0 qp = QPainter() qp.begin(self) color = QColor(0, 0, 0) color.setNamedColor('#d4d4d4') qp.setBrush(color) path = QPainterPath() path.addRoundedRect(QRectF(0, 0, w, h), 0, 0) qp.fillPath(path, QColor(206, 206, 206)) path = QPainterPath() path.addRoundedRect(QRectF(bar_start, 3, l, h - 6), 5, 5) qp.fillPath(path, QColor(71, 142, 216)) #path.addPolygon(QPolygonF([QPoint(0,0), QPoint(10,0), QPoint(10,10), QPoint(10,0)])); #qp.setBrush(QColor(71 , 142, 216)) #path.addPolygon(QPolygonF([QPoint(0,0), QPoint(50,0), QPoint(40,10), QPoint(10,0)])) #qp.setBrush(QColor(71 , 142, 216)) #qp.setBrush(QColor(206 , 207, 206)) qp.end()
def paintEvent(self, event): # TODO: maybe use self.upm*(1+2*BufferHeight) for the denominator as in # fontView scale = self.height() / (self.upm * 1.2) x_offset = \ (self.width() - self.maxWidth * scale - self.padding * 2) / 2 if x_offset < 0: scale *= 1 + 2 * x_offset / (self.maxWidth * scale) x_offset = 0 painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.padding, self.padding + (self.ascender * 1.2) * scale) painter.scale(scale, -scale) col = QColor(Qt.black) col.setAlphaF(.2) for glyph in self.glyphs: if self.alignRight: dist = self.maxWidth - glyph.width else: dist = 0 glyphPath = glyph.getRepresentation("defconQt.QPainterPath") painter.save() painter.translate(x_offset + dist, 0) painter.fillPath(glyphPath, col) painter.restore()
def paintEvent(self, event): super(FramelessWindow, self).paintEvent(event) m = 9 path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addRect(m, m, self.width() - m * 2, self.height() - m * 2) painter = QPainter(self) painter.fillPath(path, QBrush(Qt.white)) color = QColor(100, 100, 100, 100) for i in range(m): path = QPainterPath() path.setFillRule(Qt.WindingFill) path.addRoundedRect(m - i, m - i, self.width() - (m - i) * 2, self.height() - (m - i) * 2, 1, 1) color.setAlpha(100 - 10 * i) painter.setPen(QPen(color, 1, Qt.SolidLine)) painter.drawRoundedRect( QRect(m - i, m - i, self.width() - (m - i) * 2, self.height() - (m - i) * 2), 0, 0)
def paintEvent(self, ev: QtGui.QPaintEvent): painter = QPainter(self) pen = QPen(Qt.blue) pen.setCosmetic(True) painter.setPen(pen) painter.fillPath(self.path, Qt.red)
def drawString(self, content, fontSize=256, color=QColor("red")): icon = self.icon() pixmap = icon.pixmap(512, 512) pixSize = pixmap.rect() painter = QPainter(pixmap) font = painter.font() font.setPixelSize(fontSize) painter.setFont(font) path = QPainterPath() path.addText(0, 0, font, content) pathBBox = path.boundingRect() xOffset = (pixSize.width() - pathBBox.width()) / 2 - pathBBox.left() yOffset = (pixSize.height() + pathBBox.height()) / 2 path.translate(xOffset, yOffset) ## paint shadow pathPen = QPen(QColor(0, 0, 0, 200)) pathPen.setWidth(180) painter.strokePath(path, pathPen) painter.fillPath(path, QBrush(color)) ## make number bolder pathPen = QPen(color) pathPen.setWidth(20) painter.strokePath(path, pathPen) painter.end() self.setIcon(QIcon(pixmap))
def paintEvent(self, event): rect = event.rect() painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(rect, self._backgroundColor) if not self._glyphs: return painter.translate(0, self.height()) painter.scale(1, -1) availableHeight = self.height() - 2 * self._buffer availableWidth = self.width() - 2 * self._buffer scale = availableHeight / (self._upm * 1.2) yOffset = abs(self._descender * scale) + self._buffer scale = min(scale, availableWidth / self._maxWidth) for glyph in self._glyphs: if self._alignment == "left": xOffset = self._buffer elif self._alignment == "center": xOffset = (self.width() - (glyph.width * scale)) / 2 else: xOffset = availableWidth + self._buffer - glyph.width * scale path = glyph.getRepresentation("defconQt.QPainterPath") painter.save() painter.translate(xOffset, yOffset) painter.scale(scale, scale) painter.fillPath(path, self._glyphColor) painter.restore()
def draw_polygons(self, event): if self.polygons: painter = QPainter(self) # painter.drawEllipse(0, 0, self.width()/2, self.width()/2) for (pa, pb, pc), color in self.polygons: a = QPoint(pa.x, pa.y) b = QPoint(pb.x, pb.y) c = QPoint(pc.x, pc.y) pen = QPen() if color: pen.setColor(color) else: pen.setColor(QColor(0, 0, 0)) painter.setPen(pen) polygon = QPolygonF([a, b, c]) painter.drawPolygon(polygon) if color: path = QPainterPath() path.addPolygon(polygon) # painter.setBrush(QBrush(color)) painter.fillPath(path, QBrush(color))
def _paint_tooltip(self, painter: QPainter, point: QPoint, y: int, x: int, plot_name: str, color: QColor, vertical_offset: int) -> int: painter.setPen(QPen(QColor(40, 40, 40))) res = self._tooltip_func(y, x, plot_name) painter.setBrush(self._default_tooltip_brush) point = QPoint(point.x() + self._tooltip_horizontal_offset, point.y() + vertical_offset) color = QColor(min(color.red() * 1.4, 255), min(color.green() * 1.4, 255), min(color.blue() * 1.4, 255), 200) if res is not None: lines: List = res.split('\n') lengths = [len(l) for l in lines] fm = QFontMetrics(self.font) width = fm.width(lines[lengths.index(max(lengths))]) height = fm.height() * len(lines) path = QPainterPath() path.addRoundedRect( QRectF(QPointF(point.x() - 5, point.y() - 5), QSizeF(width + 10, height + 10)), 10., 10.) painter.fillPath(path, color) painter.drawPath(path) painter.drawText(QRect(point, QSize(width, height)), xor(Qt.AlignLeft, Qt.AlignTop), res) return height + 11 return 0
def drawMarker(self, x, y, qp: QtGui.QPainter, color: QtGui.QColor, number=0): if self.markerAtTip: y -= self.markerSize pen = QtGui.QPen(color) qp.setPen(pen) qpp = QtGui.QPainterPath() qpp.moveTo(x, y + self.markerSize) qpp.lineTo(x - self.markerSize, y - self.markerSize) qpp.lineTo(x + self.markerSize, y - self.markerSize) qpp.lineTo(x, y + self.markerSize) if self.filledMarkers: qp.fillPath(qpp, color) else: qp.drawPath(qpp) if self.drawMarkerNumbers: number_x = x - 3 number_y = y - self.markerSize - 3 qp.drawText(number_x, number_y, str(number))
def draw_tags(self, painter: QPainter, from_ind: int, to_ind: int) -> None: """ Draw the tags between two particular indices. """ for ind in range(from_ind, to_ind): i_r = self.tags[ind].rect text_pos = i_r.topLeft() + QPointF( TAG_TEXT_HORIZONTAL_PADDING, self.fontMetrics().ascent() + ((i_r.height() - self.fontMetrics().height()) // 2), ) # draw rect painter.setPen(EDIT_TAG_BORDER_COLOR) path = QPainterPath() path.addRoundedRect(i_r, TAG_HEIGHT // 2, TAG_HEIGHT // 2) painter.fillPath(path, EDIT_TAG_BACKGROUND_COLOR) painter.drawPath(path) # draw text painter.setPen(EDIT_TAG_TEXT_COLOR) painter.drawText(text_pos, self.tags[ind].text) # calc cross rect i_cross_r = TagsLineEdit.compute_cross_rect(i_r) pen = painter.pen() pen.setWidth(2) painter.setPen(pen) painter.drawLine( QLineF(i_cross_r.topLeft(), i_cross_r.bottomRight())) painter.drawLine( QLineF(i_cross_r.bottomLeft(), i_cross_r.topRight()))
def paintEvent(self, event): # 设置阴影 painter_path = QPainterPath() painter_path.setFillRule(Qt.WindingFill) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillPath(painter_path, QBrush(Qt.white)) # 阴影颜色 color = QColor(0, 250, 255, 50) for i in range(10): i_path = QPainterPath() i_path.setFillRule(Qt.WindingFill) ref = QRectF(10 - i, 10 - i, self.width() - (10 - i) * 2, self.height() - (10 - i) * 2) i_path.addRoundedRect(ref, 20, 20) color.setAlpha(150 - i**0.5 * 50.0) painter.setPen(color) painter.drawPath(i_path) # 圆角 painter_rect = QPainter(self) painter_rect.setRenderHint(QPainter.Antialiasing) # 抗锯齿 color_bg = QColor(170, 248, 248, 240) # 设置背警色 painter_rect.setBrush(color_bg) painter_rect.setPen(Qt.transparent) self._rect = self.rect() self._rect.setLeft(15) self._rect.setTop(15) self._rect.setWidth(self._rect.width() - 15) self._rect.setHeight(self._rect.height() - 15) painter_rect.drawRoundedRect(self._rect, 15, 15)
def road_extraction(self): """ 道路跟踪检测后进行道路的提取: 图像二值化--道路部分为白色,非道路部分为黑色 形态学过滤--进行开操作把圆形种子之间的缝隙填上 ....(更多优化的方案) 道路中心线的划分 道路模型的建立 导出结果 ---- 矢量图,普通的像素图、数据表格 """ # 图像二值化 # 创建一个空白的黑色图片 road_image = QImage(self._image.shape[1], self._image.shape[0], QImage.Format_RGB888) painter = QPainter(road_image) for circle_seed in self._seed_list: # type: CircleSeedNp path = get_circle_path(circle_seed.position, circle_seed.radius) painter.fillPath(path, Qt.white) cv_image = qimage2cvmat(road_image) cv2.imshow("road image", cv_image) # 形态学过滤 morph_filtered_img = self.morphologically_filtering(cv_image) cv2.imshow("morph_filtered road image", morph_filtered_img) cv2.waitKey() cv2.destroyAllWindows() # 道路中心线的划分 self.road_centre_lines() # 建立道路模型 self.build_road_model()
def paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) width = self.width() height = self.height() path = QPainterPath() path.addRoundedRect(2, 0, width - 4, height, 5, 5) # taskBgColor = monthcalendar.get_task_bgcolor( self.task ) selected = self.isSelected() taskBgColor = get_task_bgcolor(self.task, selected) painter.fillPath(path, taskBgColor) pathPen = QPen(QColor("black")) pathPen.setWidth(2) painter.strokePath(path, pathPen) pen = painter.pen() pen.setColor(QColor("black")) painter.setPen(pen) if height < 32: painter.drawText( 6, 0, width - 12, height, Qt.TextSingleLine | Qt.AlignVCenter | Qt.AlignLeft, self.task.title) else: painter.drawText( 6, 0, width - 12, 32, Qt.TextSingleLine | Qt.AlignVCenter | Qt.AlignLeft, self.task.title)
def paintEvent(self, event): # Закругленные углы и цвет фона super(NotificationItem, self).paintEvent(event) painter = QPainter(self) path = QPainterPath() path.addRoundedRect(QRectF(self.rect()), 6, 6) painter.fillPath(path, Qt.white)
def draw_head_arrow( painter: QPainter, up: bool, center_x: int, center_y: int, width: int, height: int, brush: QBrush ): painter.save() pen = QPen() pen.setBrush(brush) painter.setBrush(brush) painter.setPen(pen) path = QPainterPath() path.moveTo(center_x + width // 2, center_y) path.lineTo(center_x - width // 2, center_y) path.lineTo(center_x, center_y - height if up else center_y + height) path.lineTo(center_x + width // 2, center_y) painter.fillPath(path, brush) painter.drawPath(path) painter.restore()
def paintEvent(self, ev): path = QPainterPath() painter = QPainter(self) painter.setPen(QPen(QColor(255, 0, 0, 127), 6)) painter.setRenderHint(QPainter.Antialiasing) path.addRoundedRect(self.edge, 15, 15) painter.drawPath(path) painter.fillPath(path, self.bgColor)
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 paintEvent(self, event): # create round rect super(NotificationItem, self).paintEvent(event) painter = QPainter(self) path = QPainterPath() path.addRoundedRect(QRectF(self.rect()), 6, 6) # background color setting painter.fillPath(path, self.bg_color)
def paintEvent(self, event: QtGui.QPaintEvent) -> None: painter = QPainter(self) painter.drawImage(self._image.rect(), self._image) for circle_seed in self._init_seeds: # type: CircleSeedNp if self._current_circle and circle_seed == self._current_circle: painter.fillPath(circle_seed.road_path, QColor(0, 255, 0, 200)) else: painter.fillPath(circle_seed.road_path, QColor(255, 0, 0, 200)) super().paintEvent(event)
def _draw_slider(self, p: QPainter): path = QPainterPath() path.addRoundedRect( QRectF(self._padding, self.height() / 2 - self._slider_width / 2, self.width() - self._padding * 2, self._slider_width), 3, 3) # pen = QPen(self._path_color, 2) # p.setPen(pen) p.fillPath(path, self._rect_color) p.drawPath(path)
def _draw_rect(self, x: float, p: QPainter): path = QPainterPath() path.addRoundedRect( QRectF(x - self._rect_width / 2, self.height() / 2 - self._rect_height / 2, self._rect_width, self._rect_height), 1, 1) # pen = QPen(self._path_color, 2) # p.setPen(pen) p.fillPath(path, self._rect_color) p.drawPath(path)
def export_result(self, path, project: Project, size: QSize, parent): tif_result = QImage(size, QImage.Format_RGB32) mark_items = project.get_mark_items() painter = QPainter() painter.begin(tif_result) for mark_item in mark_items: painter.fillPath(mark_item.get_outline(), mark_item.color) painter.end() tif_result_array = qimage2numpy(tif_result) Image.fromarray(tif_result_array).save(path)
def paintEvent(self, ev): super().paintEvent(ev) p = QPainter(self) p.setPen(self._pen) p.setRenderHint(QPainter.Antialiasing, True) if self._currentState == State.COLLAPSED: p.fillPath(self._collapsedPath, self._pen.brush()) elif self._currentState == State.EXPANDED: p.fillPath(self._expandedPath, self._pen.brush())
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) path = QPainterPath() path.addRoundedRect(QRectF(self.rect()), 10, 10) mask = QRegion(path.toFillPolygon().toPolygon()) pen = QPen(Qt.white, 1) painter.setPen(pen) painter.fillPath(path, Qt.white) painter.drawPath(path) painter.end()
def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...): path = QPainterPath() path.addEllipse(QRectF(self.x1 - 10, self.y1 - 10, 20, 20)) if self.hovered: self.pen.setColor(QColor(255,100,100,250)) else: self.pen.setColor(QColor(255,100,100,150)) painter.setPen(self.pen) painter.drawLine(self.x1, self.y1, self.x2, self.y2) painter.fillPath(path, QColor(255, 160, 47, 255)) painter.drawPath(path) self.scene().update()
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) diameter = min(self.width(), self.height()) - 8 radius = diameter / 2 path = QPainterPath() path.addRoundedRect(-radius, -radius, diameter, diameter, radius, radius) painter.translate(self.rect().center()) painter.fillPath(path, self._color)
def paintEvent(self, event): painter = QPainter(self) if self.border: path = QPainterPath() path.addRoundedRect( QRectF(0, 0, self.border_length, self.border_length), int(self.border_length // 16), int(self.border_length // 16)) color = QColor(self.color) painter.setPen(QPen(color)) painter.drawPath(path) color.setAlpha(40) painter.fillPath(path, color) painter.drawPixmap(self.padding, self.padding, self.picture)
def __init__(self, width, height, color, border=None): """ Initialize the icon. :type width: T <= int | float :type height: T <= int | float :type color: str :type border: str """ pixmap = QPixmap(width, height) painter = QPainter(pixmap) painter.setRenderHint(QPainter.Antialiasing) path = QPainterPath() path.addRect(QRectF(QPointF(0, 0), QPointF(width, height))) painter.fillPath(path, QBrush(QColor(color))) if border: painter.setPen(QPen(QColor(border), 0, Qt.SolidLine)) painter.drawPath(path) painter.end() super().__init__(pixmap)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) self._optionsRects = {} w, h = self.width(), self.height() metrics = self.fontMetrics() hphp = 2 * _hPad painter.save() path = QPainterPath() path.addRoundedRect(.5, .5, w - 1, h - 1, 4, 4) painter.fillPath(path, QColor(250, 250, 250)) x = 0 linePath = QPainterPath() for text in self._options[:-1]: x += hphp + metrics.width(text) linePath.moveTo(x, 0) linePath.lineTo(x, h) pen = painter.pen() pen.setColor(QColor(218, 218, 218)) pen.setWidth(0) painter.setPen(pen) painter.drawPath(path) painter.setRenderHint(QPainter.Antialiasing, False) painter.drawPath(linePath) painter.restore() painter.translate(_hPad, _vPad + metrics.ascent()) left = 0 for index, text in enumerate(self._options): if index in self._selection: color = QColor(20, 146, 230) else: color = QColor(63, 63, 63) painter.setPen(color) painter.drawText(0, 0, text) textWidth = metrics.width(text) rectWidth = textWidth + hphp rect = (left, 0, rectWidth, h) self._optionsRects[index] = rect painter.translate(rectWidth, 0) left += rectWidth
def create_rounded_image(pixmap): color = QColor(0, 0, 0, 0) pix = QPixmap(QSize(pixmap.width(), pixmap.height())) pix.fill(color) rect = QRectF(0.0, 0.0, pixmap.width(), pixmap.height()) painter = QPainter() painter.begin(pix) painter.setRenderHints(QPainter.Antialiasing, True) path = QPainterPath() path.addRoundedRect(rect, pixmap.width() / 2, pixmap.height() / 2) painter.drawPath(path) brush = QtGui.QBrush() brush.setTexture(pixmap) painter.fillPath(path, brush) painter.end() return pix
def paintEvent(self, e): w=self.width() h=self.height() if self.enable_pulse==True: l=(w-3)*0.2 bar_start=((w-3)-l)*self.value else: l=(w-3)*self.value bar_start=0 qp = QPainter() qp.begin(self) color = QColor(0, 0, 0) color.setNamedColor('#d4d4d4') qp.setBrush(color) path=QPainterPath() path.addRoundedRect(QRectF(0, 0, w, h), 0, 0) qp.fillPath(path,QColor(206 , 206, 206)); path=QPainterPath() path.addRoundedRect(QRectF(bar_start, 3, l, h-6), 5, 5) qp.fillPath(path,QColor(71 , 142, 216)); #path.addPolygon(QPolygonF([QPoint(0,0), QPoint(10,0), QPoint(10,10), QPoint(10,0)])); #qp.setBrush(QColor(71 , 142, 216)) #path.addPolygon(QPolygonF([QPoint(0,0), QPoint(50,0), QPoint(40,10), QPoint(10,0)])) #qp.setBrush(QColor(71 , 142, 216)) #qp.setBrush(QColor(206 , 207, 206)) qp.end()
def paintEvent(self, event): self._alignmentPaths = [] painter = QPainter(self) painter.setPen(QColor(45, 45, 45)) circleRadius = self._circleRadius padding = self._padding rect = event.rect() size = min(rect.height(), rect.width()) offset = .5 * (rect.width() - size) painter.translate(offset, 0) borderRect = rect.__class__( rect.left() + circleRadius + padding, rect.top() + circleRadius + padding, size - 2 * (circleRadius + padding), size - 2 * (circleRadius + padding)) borderPath = QPainterPath() borderPath.addRect(*borderRect.getRect()) columnCount = 3 radioPath = QPainterPath() selectedPath = QPainterPath() for row in range(columnCount): for col in range(columnCount): index = row * columnCount + col path = QPainterPath() path.addEllipse( padding + col * .5 * borderRect.width(), padding + row * .5 * borderRect.height(), 2 * circleRadius, 2 * circleRadius) if self._alignment == index: selectedPath = path self._alignmentPaths.append(path.translated(offset, 0)) radioPath.addPath(path) painter.drawPath(borderPath - radioPath) painter.setRenderHint(QPainter.Antialiasing) painter.drawPath(radioPath) painter.fillPath(selectedPath, Qt.black)
def paintEvent(self, event): painter = QPainter(self) visibleRect = event.rect() columnCount = self._columnCount extra = self._cellWidthExtra cellWidth, cellHeight = self._cellWidth + 2 * extra, self._cellHeight glyphCount = len(self._glyphs) if columnCount: paintWidth = min(glyphCount, columnCount) * cellWidth else: paintWidth = 0 left = 0 top = cellHeight painter.fillRect(visibleRect, Qt.white) for index, glyph in enumerate(self._glyphs): t = top - cellHeight rect = (left, t, cellWidth, cellHeight) if visibleRect.intersects(visibleRect.__class__(*rect)): if index in self._selection: palette = self.palette() active = palette.currentColorGroup() != QPalette.Inactive opacityMultiplier = platformSpecific.colorOpacityMultiplier() selectionColor = palette.color(QPalette.Highlight) # TODO: alpha values somewhat arbitrary (here and in # glyphLineView) selectionColor.setAlphaF( .2 * opacityMultiplier if active else .7) painter.fillRect(QRectF( left, t, cellWidth, cellHeight), selectionColor) pixmap = self._getCurrentRepresentation(glyph) painter.drawPixmap(left, t, pixmap) # XXX: this hacks around the repr internals if index in self._selection and \ cellHeight >= GlyphCellMinHeightForHeader: painter.fillRect(QRectF( left, t + cellHeight - GlyphCellHeaderHeight, cellWidth, GlyphCellHeaderHeight), selectionColor) left += cellWidth if left + cellWidth > paintWidth: left = 0 top += cellHeight # drop insertion position dropIndex = self._currentDropIndex if dropIndex is not None: if columnCount: x = (dropIndex % columnCount) * cellWidth y = (dropIndex // columnCount) * cellHeight # special-case the end-column if dropIndex == glyphCount and \ glyphCount < self.width() // self._cellWidth or \ self.mapFromGlobal(QCursor.pos()).y() < y: x = columnCount * cellWidth y -= cellHeight else: x = y = 0 path = QPainterPath() path.addRect(x - 2, y, 3, cellHeight) path.addEllipse(x - 5, y - 5, 9, 9) path.addEllipse(x - 5, y + cellHeight - 5, 9, 9) path.setFillRule(Qt.WindingFill) pen = painter.pen() pen.setColor(Qt.white) pen.setWidth(2) painter.setPen(pen) painter.setRenderHint(QPainter.Antialiasing) painter.drawPath(path) painter.fillPath(path, insertionPositionColor)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) redrawRect = event.rect() beginRow = redrawRect.top() // self.squareSize endRow = redrawRect.bottom() // self.squareSize # XXX: do we need to maintain self._column when we have (endColumn - # beginColumn)? beginColumn = redrawRect.left() // self.squareSize endColumn = redrawRect.right() // self.squareSize gradient = QLinearGradient(0, 0, 0, GlyphCellHeaderHeight) gradient.setColorAt(0.0, cellHeaderBaseColor) gradient.setColorAt(1.0, cellHeaderLineColor) dirtyGradient = QLinearGradient(0, 0, 0, GlyphCellHeaderHeight) dirtyGradient.setColorAt(0.0, cellHeaderBaseColor.darker(125)) dirtyGradient.setColorAt(1.0, cellHeaderLineColor.darker(125)) markGradient = QLinearGradient(0, 0, 0, self.squareSize-GlyphCellHeaderHeight) for row in range(beginRow, endRow + 1): for column in range(beginColumn, endColumn + 1): key = row * self._columns + column if key >= len(self._glyphs): break glyph = self._glyphs[key] painter.save() painter.translate(column * self.squareSize, row * self.squareSize) painter.fillRect(0, 0, self.squareSize, self.squareSize, Qt.white) # prepare header colors brushColor = gradient linesColor = cellHeaderHighlightLineColor # mark color if not glyph.template: if glyph.markColor is not None: markColor = QColor.fromRgbF(*tuple(glyph.markColor)) markGradient.setColorAt(1.0, markColor) markGradient.setColorAt(0.0, markColor.lighter(125)) painter.fillRect(0, GlyphCellHeaderHeight, self.squareSize, self.squareSize - GlyphCellHeaderHeight, QBrush(markGradient)) if glyph.dirty: brushColor = dirtyGradient linesColor = cellHeaderHighlightLineColor.darker(110) # header gradient painter.fillRect(0, 0, self.squareSize, GlyphCellHeaderHeight, QBrush(brushColor)) # header lines painter.setPen(linesColor) minOffset = painter.pen().width() # disable antialiasing to avoid lines bleeding over background painter.setRenderHint(QPainter.Antialiasing, False) painter.drawLine(0, 0, 0, GlyphCellHeaderHeight - 1) painter.drawLine(self.squareSize - 2, 0, self.squareSize - 2, GlyphCellHeaderHeight -1) painter.setPen(QColor(170, 170, 170)) painter.drawLine(0, GlyphCellHeaderHeight, self.squareSize, GlyphCellHeaderHeight) painter.setRenderHint(QPainter.Antialiasing) # header text painter.setFont(headerFont) painter.setPen(QColor(80, 80, 80)) name = metrics.elidedText(glyph.name, Qt.ElideRight, self.squareSize - 2) painter.drawText(1, 0, self.squareSize - 2, GlyphCellHeaderHeight - minOffset, Qt.TextSingleLine | Qt.AlignCenter, name) painter.restore() painter.setPen(cellGridColor) rightEdgeX = column * self.squareSize + self.squareSize bottomEdgeY = row * self.squareSize + self.squareSize painter.drawLine(rightEdgeX, row * self.squareSize + 1, rightEdgeX, bottomEdgeY) painter.drawLine(rightEdgeX, bottomEdgeY, column * self.squareSize + 1, bottomEdgeY) if self._currentDropIndex is not None: painter.setPen(Qt.green) if self._currentDropIndex == key: painter.drawLine(column * self.squareSize, row * self.squareSize, column * self.squareSize, bottomEdgeY) # special-case the end-column elif column == endColumn and self._currentDropIndex == key+1: yPos = self.mapFromGlobal(QCursor.pos()).y() if row == yPos // self.squareSize: painter.drawLine(rightEdgeX - 1, row * self.squareSize, rightEdgeX - 1, bottomEdgeY) # selection code if key in self._selection: painter.setRenderHint(QPainter.Antialiasing, False) painter.fillRect(column * self.squareSize + 1, row * self.squareSize + 1, self.squareSize - 3, self.squareSize - 3, cellSelectionColor) painter.setRenderHint(QPainter.Antialiasing) if not glyph.template: font = glyph.getParent() outline = glyph.getRepresentation("defconQt.QPainterPath") uPM = font.info.unitsPerEm if uPM is None or not uPM > 0: uPM = 1000 descender = font.info.descender if descender is None or not descender < 0: descender = -250 factor = (self.squareSize-GlyphCellHeaderHeight) / (uPM*(1+2*GlyphCellBufferHeight)) x_offset = (self.squareSize-glyph.width*factor)/2 # If the glyph overflows horizontally we need to adjust the scaling factor if x_offset < 0: factor *= 1+2*x_offset/(glyph.width*factor) x_offset = 0 # TODO: the * 1.8 below is somewhat artificial y_offset = descender*factor * 1.8 painter.save() painter.setClipRect(column * self.squareSize, row * self.squareSize+GlyphCellHeaderHeight, self.squareSize, self.squareSize-GlyphCellHeaderHeight) painter.translate(column * self.squareSize + x_offset, row * self.squareSize + self.squareSize + y_offset) painter.scale(factor, -factor) painter.fillPath(outline, Qt.black) painter.restore() else: painter.save() painter.setFont(voidFont) painter.setPen(QPen(Qt.lightGray)) rect = QRectF(column * self.squareSize, row * self.squareSize+GlyphCellHeaderHeight, self.squareSize, self.squareSize-GlyphCellHeaderHeight) # TODO: need to flag template glyphs as to whether they have unicodings or not if glyph.unicode is not None: text = chr(glyph.unicode) else: text = "✌" painter.drawText(rect, Qt.AlignCenter, text) painter.restore()
def paintEvent(self, event): linePen = QPen(Qt.black) linePen.setWidth(3) width = self.width() / self.scale def paintLineMarks(painter): painter.save() painter.scale(self.scale, yDirection * self.scale) painter.setPen(linePen) painter.drawLine(0, self.ascender, width, self.ascender) painter.drawLine(0, 0, width, 0) painter.drawLine(0, self.descender, width, self.descender) painter.restore() painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(0, 0, self.width(), self.height(), Qt.white) if self._verticalFlip: baselineShift = -self.descender yDirection = 1 else: baselineShift = self.ascender yDirection = -1 painter.translate(self.padding, self.padding + baselineShift * self.scale * self._lineHeight) # TODO: scale painter here to avoid g*scale everywhere below cur_width = 0 if self._showMetrics: paintLineMarks(painter) for index, glyph in enumerate(self.glyphs): # line wrapping gWidth = glyph.width * self.scale doKern = index > 0 and self._showKerning and cur_width > 0 if doKern: kern = self.lookupKerningValue( self.glyphs[index - 1].name, glyph.name) * self.scale else: kern = 0 if (self._wrapLines and cur_width + gWidth + kern + 2 * self.padding > self.width()) or glyph.unicode == 2029: painter.translate(-cur_width, self.ptSize * self._lineHeight) if self._showMetrics: paintLineMarks(painter) cur_width = gWidth else: if doKern: painter.translate(kern, 0) cur_width += gWidth + kern glyphPath = glyph.getRepresentation("defconQt.QPainterPath") painter.save() painter.scale(self.scale, yDirection * self.scale) if self._showMetrics: halfDescent = self.descender / 2 painter.drawLine(0, 0, 0, halfDescent) painter.drawLine(glyph.width, 0, glyph.width, halfDescent) if self._selected is not None and index == self._selected: painter.fillRect(0, self.descender, glyph.width, self.upm, glyphSelectionColor) painter.fillPath(glyphPath, Qt.black) painter.restore() painter.translate(gWidth, 0)
def paintEvent(self, event): linePen = QPen(Qt.black) linePen.setWidth(3) width = self.width() / self.scale def paintLineMarks(painter): painter.save() painter.scale(self.scale, yDirection * self.scale) painter.setPen(linePen) painter.drawLine(0, self.ascender, width, self.ascender) painter.drawLine(0, 0, width, 0) painter.drawLine(0, self.descender, width, self.descender) painter.restore() painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(0, 0, self.width(), self.height(), Qt.white) if self._verticalFlip: baselineShift = -self.descender yDirection = 1 else: baselineShift = self.ascender yDirection = -1 painter.translate(self.padding, self.padding + baselineShift * self.scale * self._lineHeight) # TODO: scale painter here to avoid g*scale everywhere below cur_width = 0 lines = 1 self._positions = [[]] if self._showMetrics: paintLineMarks(painter) for index, glyph in enumerate(self.glyphs): # line wrapping gWidth = glyph.width * self.scale doKern = index > 0 and self._showKerning and cur_width > 0 if doKern: kern = self.lookupKerningValue(self.glyphs[index - 1].name, glyph.name) * self.scale else: kern = 0 if self._wrapLines and cur_width + gWidth + kern + 2 * self.padding > self.width(): painter.translate(-cur_width, self.ptSize * self._lineHeight) if self._showMetrics: paintLineMarks(painter) self._positions.append([(0, gWidth)]) cur_width = gWidth lines += 1 else: if doKern: painter.translate(kern, 0) self._positions[-1].append((cur_width, gWidth)) cur_width += gWidth + kern glyphPath = glyph.getRepresentation("defconQt.QPainterPath") painter.save() painter.scale(self.scale, yDirection * self.scale) if self._showMetrics: halfDescent = self.descender / 2 painter.drawLine(0, 0, 0, halfDescent) painter.drawLine(glyph.width, 0, glyph.width, halfDescent) if self._selected is not None and index == self._selected: painter.fillRect(0, self.descender, glyph.width, self.upm, glyphSelectionColor) painter.fillPath(glyphPath, Qt.black) painter.restore() painter.translate(gWidth, 0) scrollMargins = self._scrollArea.contentsMargins() innerHeight = self._scrollArea.height() - scrollMargins.top() - scrollMargins.bottom() if not self._wrapLines: innerWidth = self._scrollArea.width() - scrollMargins.left() - scrollMargins.right() width = max(innerWidth, cur_width + self.padding * 2) else: width = self.width() self.resize(width, max(innerHeight, lines * self.ptSize * self._lineHeight + 2 * self.padding))