def draw_text(self, style, painter, option, widget, index, item): tr = style.subElementRect(style.SE_ItemViewItemText, option, widget) text = index.data(Qt.DisplayRole) hover = option.state & style.State_MouseOver if hover or gprefs['tag_browser_show_counts']: count = unicode_type(index.data(COUNT_ROLE)) width = painter.fontMetrics().boundingRect(count).width() r = QRect(tr) r.setRight(r.right() - 1), r.setLeft(r.right() - width - 4) painter.drawText(r, Qt.AlignCenter | Qt.TextSingleLine, count) tr.setRight(r.left() - 1) else: tr.setRight(tr.right() - 1) is_rating = item.type == TagTreeItem.TAG and not self.rating_pat.sub('', text) if is_rating: painter.setFont(self.rating_font) flags = Qt.AlignVCenter | Qt.AlignLeft | Qt.TextSingleLine lr = QRect(tr) lr.setRight(lr.right() * 2) br = painter.boundingRect(lr, flags, text) if br.width() > tr.width(): g = QLinearGradient(tr.topLeft(), tr.topRight()) c = option.palette.color(QPalette.WindowText) g.setColorAt(0, c), g.setColorAt(0.8, c) c = QColor(c) c.setAlpha(0) g.setColorAt(1, c) pen = QPen() pen.setBrush(QBrush(g)) painter.setPen(pen) painter.drawText(tr, flags, text)
def draw_snake_spinner(painter, rect, angle, light, dark): painter.setRenderHint(QPainter.Antialiasing) if rect.width() > rect.height(): delta = (rect.width() - rect.height()) // 2 rect = rect.adjusted(delta, 0, -delta, 0) elif rect.height() > rect.width(): delta = (rect.height() - rect.width()) // 2 rect = rect.adjusted(0, delta, 0, -delta) disc_width = max(3, min(rect.width() // 10, 8)) drawing_rect = QRect(rect.x() + disc_width, rect.y() + disc_width, rect.width() - 2 * disc_width, rect.height() - 2 * disc_width) gap = 60 # degrees gradient = QConicalGradient(drawing_rect.center(), angle - gap // 2) gradient.setColorAt((360 - gap // 2) / 360.0, light) gradient.setColorAt(0, dark) pen = QPen(QBrush(gradient), disc_width) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) painter.drawArc(drawing_rect, angle * 16, (360 - gap) * 16)
def draw_snake_spinner(painter, rect, angle, light, dark): painter.setRenderHint(QPainter.Antialiasing) if rect.width() > rect.height(): delta = (rect.width() - rect.height()) // 2 rect = rect.adjusted(delta, 0, -delta, 0) elif rect.height() > rect.width(): delta = (rect.height() - rect.width()) // 2 rect = rect.adjusted(0, delta, 0, -delta) disc_width = max(4, rect.width() // 10) drawing_rect = QRect(rect.x() + disc_width, rect.y() + disc_width, rect.width() - 2 * disc_width, rect.height() - 2 *disc_width) try: angle_for_width = math.degrees(math.atan2(1.3 * disc_width, drawing_rect.width())) except ZeroDivisionError: angle_for_width = 5 gradient = QConicalGradient(drawing_rect.center(), angle - angle_for_width) gradient.setColorAt(1, light) gradient.setColorAt(0, dark) painter.setPen(QPen(light, disc_width)) painter.drawArc(drawing_rect, 0, 360 * 16) pen = QPen(QBrush(gradient), disc_width) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) painter.drawArc(drawing_rect, angle * 16, (360 - 2 * angle_for_width) * 16)
def draw_text(self, style, painter, option, widget, index, item): tr = style.subElementRect(style.SE_ItemViewItemText, option, widget) text = index.data(Qt.DisplayRole) hover = option.state & style.State_MouseOver if hover or gprefs['tag_browser_show_counts']: count = unicode(index.data(COUNT_ROLE)) width = painter.fontMetrics().boundingRect(count).width() r = QRect(tr) r.setRight(r.right() - 1), r.setLeft(r.right() - width - 4) painter.drawText(r, Qt.AlignCenter | Qt.TextSingleLine, count) tr.setRight(r.left() - 1) else: tr.setRight(tr.right() - 1) is_rating = item.type == TagTreeItem.TAG and not self.rating_pat.sub( '', text) if is_rating: painter.setFont(self.rating_font) flags = Qt.AlignVCenter | Qt.AlignLeft | Qt.TextSingleLine lr = QRect(tr) lr.setRight(lr.right() * 2) br = painter.boundingRect(lr, flags, text) if br.width() > tr.width(): g = QLinearGradient(tr.topLeft(), tr.topRight()) c = option.palette.color(QPalette.WindowText) g.setColorAt(0, c), g.setColorAt(0.8, c) c = QColor(c) c.setAlpha(0) g.setColorAt(1, c) pen = QPen() pen.setBrush(QBrush(g)) painter.setPen(pen) painter.drawText(tr, flags, text)
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() ow, oh = w, h cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(int(nw*pmap.devicePixelRatio()), int(nh*pmap.devicePixelRatio()), Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = int(pmap.width()/pmap.devicePixelRatio()), int(pmap.height()/pmap.devicePixelRatio()) x = int(abs(cw - w)/2.) y = int(abs(ch - h)/2.) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.draw_border: pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) p.drawRect(target) if self.show_size: draw_size(p, target, ow, oh) p.end()
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() ow, oh = w, h cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(int(nw * pmap.devicePixelRatio()), int(nh * pmap.devicePixelRatio()), Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = int(pmap.width() / pmap.devicePixelRatio()), int( pmap.height() / pmap.devicePixelRatio()) x = int(abs(cw - w) / 2.) y = int(abs(ch - h) / 2.) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.draw_border: pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) p.drawRect(target) if self.show_size: draw_size(p, target, ow, oh) p.end()
def __init__(self, parent=None): super().__init__(parent) self.__parent = parent self.__board = QPixmap(QSize(440, 440)) self.__board.fill(Qt.transparent) self.__board_old = self.__board.copy() self.__board_old_old = self.__board.copy() self.__board_before_dots = self.__board.copy() self.__thickness = 10 # 默认画笔粗细为10px self.__penColor = QColor(0, 0, 0, 128) self.__painter = QPainter() self.__pen = QPen(self.__penColor, self.__thickness) self.__pen_seg = QPen(QColor(0, 0, 0, 128)) self.__brush = QBrush(QColor(0, 0, 0, 128)) self.__pen.setCapStyle(Qt.RoundCap) #self.__painter.setPen(self.__pen) self.__lastPos = QPoint(0, 0) # 上一次鼠标位置 self.__currentPos = QPoint(0, 0) # 当前的鼠标位置 self.__points = [] # dots模式的点集 self.__mouse_pressed = False self.__can_undo = False self.__has_seg = False self.__mode = 1 self.__ERASE = 0 self.__LINE = 1 self.__RECT = 2 self.__CIRCLE = 3 self.__DOTS = 4 self.__transparent = False self.__trans_board = self.__board.copy() self.__trans_board.fill(Qt.transparent)
def draw_snake_spinner(painter, rect, angle, light, dark): painter.setRenderHint(QPainter.Antialiasing) if rect.width() > rect.height(): delta = (rect.width() - rect.height()) // 2 rect = rect.adjusted(delta, 0, -delta, 0) elif rect.height() > rect.width(): delta = (rect.height() - rect.width()) // 2 rect = rect.adjusted(0, delta, 0, -delta) disc_width = max(4, rect.width() // 10) drawing_rect = QRect(rect.x() + disc_width, rect.y() + disc_width, rect.width() - 2 * disc_width, rect.height() - 2 * disc_width) try: angle_for_width = math.degrees( math.atan2(1.3 * disc_width, drawing_rect.width())) except ZeroDivisionError: angle_for_width = 5 gradient = QConicalGradient(drawing_rect.center(), angle - angle_for_width) gradient.setColorAt(1, light) gradient.setColorAt(0, dark) painter.setPen(QPen(light, disc_width)) painter.drawArc(drawing_rect, 0, 360 * 16) pen = QPen(QBrush(gradient), disc_width) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) painter.drawArc(drawing_rect, angle * 16, (360 - 2 * angle_for_width) * 16)
def drawContents(self, painter): self.drawn_once = True painter.save() painter.setRenderHint(painter.TextAntialiasing, True) painter.setRenderHint(painter.Antialiasing, True) pw = self.LOGO_SIZE height = max(pw, self.total_height) width = self.width() # Draw frame y = (self.height() - height) // 2 bottom = y + height painter.fillRect(0, y, width, height, self.light_brush) painter.fillRect(0, y, width, self.title_height, self.dark_brush) painter.fillRect(0, y, pw, height, self.dark_brush) dy = (height - self.LOGO_SIZE) // 2 painter.drawPixmap(0, y + dy, self.pmap) # Draw number painter.setFont(self.num_font) num_width = painter.boundingRect( 0, 0, 0, 0, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.num_ch).width() + 12 num_x = width - num_width painter.setPen(QPen(QColor('#d6b865'))) painter.drawText( num_x, y, num_width, height, Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine, self.num_ch) # Draw title x = pw + 10 width -= num_width + 5 + x painter.setFont(self.title_font) painter.drawText( x, y, width, self.title_height, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextSingleLine, "CALIBRE") # Draw starting up message y += self.title_height + 5 painter.setPen(QPen(self.dark_brush.color())) painter.setFont(self.body_font) br = painter.drawText( x, y, width, self.line_height, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextSingleLine, _('Starting up, please wait...')) starting_up_bottom = br.bottom() # Draw footer m = self.message() if m and m.strip(): painter.setFont(self.footer_font) b = max(starting_up_bottom + 5, bottom - self.line_height) painter.drawText( x, b, width, self.line_height, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop | Qt.TextFlag.TextSingleLine, m) painter.restore()
def __call__(self, painter, rect, color_theme, title_block, subtitle_block, footer_block): painter.fillRect(rect, self.color1) top = title_block.position.y + 2 extra_spacing = subtitle_block.line_spacing // 2 if subtitle_block.line_spacing else title_block.line_spacing // 3 height = title_block.height + subtitle_block.height + extra_spacing + title_block.leading right = rect.right() - self.hmargin width = right - self.hmargin # Draw main banner p = main = QPainterPath(QPointF(self.hmargin, top)) draw_curved_line(p, rect.width() - 2 * self.hmargin, 0, 0.1, -0.1, 0.9, -0.1) deltax = self.GRADE * height p.lineTo(right + deltax, top + height) right_corner = p.currentPosition() draw_curved_line(p, - width - 2 * deltax, 0, 0.1, 0.05, 0.9, 0.05) left_corner = p.currentPosition() p.closeSubpath() # Draw fold rectangles rwidth = self.fold_width yfrac = 0.1 width23 = int(0.67 * rwidth) rtop = top + height * yfrac def draw_fold(x, m=1, corner=left_corner): ans = p = QPainterPath(QPointF(x, rtop)) draw_curved_line(p, rwidth*m, 0, 0.1, 0.1*m, 0.5, -0.2*m) fold_upper = p.currentPosition() p.lineTo(p.currentPosition() + QPointF(-deltax*m, height)) fold_corner = p.currentPosition() draw_curved_line(p, -rwidth*m, 0, 0.2, -0.1*m, 0.8, -0.1*m) draw_curved_line(p, deltax*m, -height, 0.2, 0.1*m, 0.8, 0.1*m) p = inner_fold = QPainterPath(corner) dp = fold_corner - p.currentPosition() draw_curved_line(p, dp.x(), dp.y(), 0.5, 0.3*m, 1, 0*m) p.lineTo(fold_upper), p.closeSubpath() return ans, inner_fold left_fold, left_inner = draw_fold(self.hmargin - width23) right_fold, right_inner = draw_fold(right + width23, m=-1, corner=right_corner) painter.save() painter.setRenderHint(QPainter.Antialiasing) pen = QPen(self.ccolor2) pen.setWidth(3) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen) for r in (left_fold, right_fold): painter.fillPath(r, QBrush(self.color2)) painter.drawPath(r) for r in (left_inner, right_inner): painter.fillPath(r, QBrush(self.color2.darker())) painter.drawPath(r) painter.fillPath(main, QBrush(self.color2)) painter.drawPath(main) painter.restore() return self.ccolor2, self.ccolor2, self.ccolor1
def __init__(self, font_loader, logger, opts, width=0, height=0, parent=None, x=0, y=0): QGraphicsRectItem.__init__(self, x, y, width, height, parent) self.font_loader, self.logger, self.opts = font_loader, logger, opts self.current_y, self.max_y, self.max_x = 0, height, width self.is_full = False pen = QPen() pen.setStyle(Qt.NoPen) self.setPen(pen) if not hasattr(self, 'children'): self.children = self.childItems
def paintEvent(self, event): w = self.viewport().rect().width() painter = QPainter(self.viewport()) painter.setClipRect(event.rect()) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) floor = event.rect().bottom() ceiling = event.rect().top() fv = self.firstVisibleBlock().blockNumber() origin = self.contentOffset() doc = self.document() lines = [] for num, text in self.headers: top, bot = num, num + 3 if bot < fv: continue y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top)).translated(origin).y() y_bot = self.blockBoundingGeometry(doc.findBlockByNumber(bot)).translated(origin).y() if max(y_top, y_bot) < ceiling: continue if min(y_top, y_bot) > floor: break painter.setFont(self.heading_font) br = painter.drawText(3, y_top, w, y_bot - y_top - 5, Qt.TextSingleLine, text) painter.setPen(QPen(self.palette().text(), 2)) painter.drawLine(0, br.bottom()+3, w, br.bottom()+3) for top, bot, kind in self.changes: if bot < fv: continue y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top)).translated(origin).y() y_bot = self.blockBoundingGeometry(doc.findBlockByNumber(bot)).translated(origin).y() if max(y_top, y_bot) < ceiling: continue if min(y_top, y_bot) > floor: break if y_top != y_bot: painter.fillRect(0, y_top, w, y_bot - y_top, self.diff_backgrounds[kind]) lines.append((y_top, y_bot, kind)) if top in self.images: img, maxw = self.images[top][:2] if bot > top + 1 and not img.isNull(): y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top+1)).translated(origin).y() + 3 y_bot -= 3 scaled, imgw, imgh = fit_image(int(img.width()/img.devicePixelRatio()), int(img.height()/img.devicePixelRatio()), w - 3, y_bot - y_top) painter.drawPixmap(QRect(3, y_top, imgw, imgh), img) painter.end() PlainTextEdit.paintEvent(self, event) painter = QPainter(self.viewport()) painter.setClipRect(event.rect()) for top, bottom, kind in sorted(lines, key=lambda t_b_k:{'replace':0}.get(t_b_k[2], 1)): painter.setPen(QPen(self.diff_foregrounds[kind], 1)) painter.drawLine(0, top, w, top) painter.drawLine(0, bottom - 1, w, bottom - 1)
class Window(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PyQtChart Demo") self.setGeometry(500, 275, 650, 500) self.show() self.createLineChart() def createLineChart(self): self.font = QFont("Arial") self.font.setPixelSize(16) self.font.setBold(True) self.pen = QPen(QColor(0, 153, 0)) self.pen.setWidth(3) self.series = QLineSeries(self) self.series.setPen(self.pen) self.x = np.arange(0, 2 * np.pi, 0.01) for i in self.x: self.series.append(i, np.sin(i)) self.chart = QChart() self.chart.addSeries(self.series) self.chart.setAnimationOptions(QChart.SeriesAnimations) self.chart.setTitleFont(self.font) self.chart.setTitleBrush(QBrush(Qt.blue)) self.chart.setTitle("Line Chart Demo") self.chart.legend().setVisible(True) self.chart.legend().setAlignment(Qt.AlignBottom) self.chartview = QChartView(self.chart) self.chartview.setRenderHint(QPainter.Antialiasing) self.axisX = QValueAxis() self.axisX.setRange(0, 2 * np.pi) self.axisX.setTickCount(6) self.axisX.setLabelFormat("%.1f") self.axisX.setTitleText("x") self.axisY = QValueAxis() self.axisY.setRange(0, 100) self.axisY.setLabelFormat("%d") self.axisY.setTitleText("sin(x)") self.chart.addAxis(self.axisX, Qt.AlignBottom) self.chart.addAxis(self.axisY, Qt.AlignLeft) self.setCentralWidget(self.chartview)
def paint_point(self): self.Painter.begin(self.Board) if not self.EraserMode: # 非橡皮擦模式 self.Painter.setPen(QPen(self.PenColor, self.Thickness)) # 设置画笔颜色,粗细 else: # 橡皮擦模式下画笔为纯白色,粗细为10 self.Painter.setPen(QPen(Qt.white, 10)) # 画线 self.Painter.drawLine(self.LastPos, self.CurrentPos) self.Painter.end()
def paint(self, paint: QPainter, style: QStyleOptionGraphicsItem, widget=None): qpen = QPen() qpen.setColor(Qt.white) # paint.setBackground() paint.setPen(qpen) aspect = self.head_img.width() / self.head_img.height() ratio = self.height * 0.2 target = QRectF(0, 0, aspect * ratio, ratio) source = QRectF(0, 0, self.head_img.width(), self.head_img.height()) paint.drawImage(target, self.head_img, source)
def get_pen(self): """ constructs and returns the set pen """ pen = QPen() pen.setWidth(self.pen[PenEnum.WIDTH]) pen.setColor(QColor(self.pen[PenEnum.COLOR])) pen.setStyle(self.pen[PenEnum.STYLE]) return pen
def mouseMoveEvent(self, mouseEvent): self.__currentPos = mouseEvent.pos() self.__painter.begin(self.__board) if self.EraserMode == False: # 非橡皮擦模式 self.__painter.setPen(QPen(self.__penColor, self.__thickness, Qt.SolidLine, Qt.RoundCap)) # 设置画笔颜色,粗细 else: # 橡皮擦模式下画笔为纯黒色,粗细为20 self.__painter.setPen(QPen(Qt.black, 60, Qt.SolidLine, Qt.RoundCap)) self.__painter.drawLine(self.__lastPos, self.__currentPos) # self.__painter.drawArc() self.__painter.end() self.__lastPos = self.__currentPos self.update() # 更新显示
def createPixmapForSite(self, icon, title, url): ''' @param: icon QIcon @param: title QString @param: url QString @return: QPixmap ''' fontMetrics = QApplication.fontMetrics() padding = 4 text = len(title) > len(url) and title or url maxWidth = fontMetrics.width(text) + 3 * padding + 16 width = min(maxWidth, 150) height = fontMetrics.height() * 2 + fontMetrics.leading() + 2 * padding pixelRatio = gVar.app.devicePixelRatio() pixmap = QPixmap(width * pixelRatio, height * pixelRatio) pixmap.setDevicePixelRatio(pixelRatio) painter = QPainter(pixmap) painter.setRenderHint(QPainter.Antialiasing) # Draw background pen = QPen(Qt.black) pen.setWidth(1) painter.setPen(pen) path = QPainterPath() path.addRect(QRectF(0, 0, width, height)) painter.fillPath(path, Qt.white) painter.drawPath(path) # Draw icon iconRect = QRect(padding, 0, 16, height) icon.paint(painter, iconRect) # Draw title titleRect = QRectF(iconRect.right() + padding, padding, width - padding - iconRect.right(), fontMetrics.height()) painter.drawText(titleRect, fontMetrics.elidedText(title, Qt.ElideRight, titleRect.width())) # Draw url urlRect = QRectF(titleRect.x(), titleRect.bottom() + fontMetrics.leading(), titleRect.width(), titleRect.height()) painter.setPen(QApplication.palette().color(QPalette.Link)) painter.drawText(urlRect, fontMetrics.elidedText(url, Qt.ElideRight, urlRect.width())) return pixmap
def paintEvent(self, event): super().paintEvent(event) if self._is_recording: painter = QPainter(self) painter.setPen(QPen(Qt.red, 4)) painter.drawRect(QRect(2, 2, self.width() - 4, self.height() - 4))
def paintEvent(self, QPaintEvent): pen = QPen() pen.setWidth(self.__circle_width) painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(pen) # Paint line to origin painter.drawPath(self.line_path) # Paint circle q = QColor(Qt.white) painter.setBrush(q) painter.drawEllipse(self.__handle_pos, self.__radius, self.__radius) painter.end()
def mouseMoveEvent(self, mouseEvent): # 鼠标移动时,更新当前位置,并在上一个位置和当前位置间画线 self.__currentPos = mouseEvent.pos() self.__painter.begin(self.__board) if not self.EraserMode: # 非橡皮擦模式 self.__painter.setPen(QPen(self.__penColor, self.__thickness)) # 设置画笔颜色,粗细 else: # 橡皮擦模式下画笔为纯白色,粗细为45 self.__painter.setPen(QPen(Qt.black, 45)) # 画线 self.__painter.drawLine(self.__lastPos, self.__currentPos) self.__painter.end() self.__lastPos = self.__currentPos self.update() # 更新显示
def __init__(self, rl): QGraphicsLineItem.__init__(self, 0, 0, rl.linelength, 0) ContentObject.__init__(self) self.setPen(QPen( COLOR(rl.linecolor, None), rl.linewidth, ))
def mouseMoveEvent(self, mouseEvent): self.__currentPos = mouseEvent.pos() self.__painter.begin(self.__board) if self.EraserMode == False: #非橡皮擦模式 self.__painter.setPen(QPen(self.__penColor,self.__thickness)) #设置画笔颜色,粗细 else: #橡皮擦模式下画笔为纯白色,粗细为10 self.__painter.setPen(QPen(Qt.white,10)) self.__painter.drawLine(self.__lastPos, self.__currentPos) self.__painter.end() self.__lastPos = self.__currentPos self.update() #更新显示
def paint(self, painter, option, index): QStyledItemDelegate.paint(self, painter, option, QModelIndex()) theme = index.data(Qt.UserRole) if not theme: return painter.save() pixmap = index.data(Qt.DecorationRole) if pixmap and not pixmap.isNull(): rect = option.rect.adjusted(0, self.SPACING, COVER_SIZE[0] - option.rect.width(), - self.SPACING) painter.drawPixmap(rect, pixmap) if option.state & QStyle.State_Selected: painter.setPen(QPen(QApplication.instance().palette().highlightedText().color())) bottom = option.rect.bottom() - 2 painter.drawLine(0, bottom, option.rect.right(), bottom) if 'static-text' not in theme: theme['static-text'] = QStaticText(_( ''' <h1>{title}</h1> <p>by <i>{author}</i> with <b>{number}</b> icons [{size}]</p> <p>{description}</p> <p>Version: {version} Number of users: {usage}</p> '''.format(title=theme.get('title', _('Unknown')), author=theme.get('author', _('Unknown')), number=theme.get('number', 0), description=theme.get('description', ''), size=human_readable(theme.get('compressed-size', 0)), version=theme.get('version', 1), usage=theme.get('usage', 0), ))) painter.drawStaticText(COVER_SIZE[0] + self.SPACING, option.rect.top() + self.SPACING, theme['static-text']) painter.restore()
def __init__(self, font_loader, chapter, odd, logger, opts, ruby_tags, link_activated): self.logger, self.opts = logger, opts page_style = chapter.style sidemargin = page_style.oddsidemargin if odd else page_style.evensidemargin width = 2*sidemargin + page_style.textwidth self.content_x = 0 + sidemargin self.text_width = page_style.textwidth self.header_y = page_style.topmargin self.text_y = self.header_y + page_style.headheight + page_style.headsep self.text_height = page_style.textheight self.footer_y = self.text_y + self.text_height + (page_style.footspace - page_style.footheight) _Canvas.__init__(self, font_loader, logger, opts, width=width, height=self.footer_y+page_style.footheight) if opts.visual_debug: self.setPen(QPen(Qt.red, 1, Qt.SolidLine)) header = footer = None if page_style.headheight > 0: try: header = chapter.oddheader if odd else chapter.evenheader except AttributeError: pass if page_style.footheight > 0: try: footer = chapter.oddfooter if odd else chapter.evenfooter except AttributeError: pass if header: header = Header(font_loader, header, page_style, logger, opts, ruby_tags, link_activated) self.layout_canvas(header, self.content_x, self.header_y) if footer: footer = Footer(font_loader, footer, page_style, logger, opts, ruby_tags, link_activated) self.layout_canvas(footer, self.content_x, self.header_y) self.page = None
def paintEvent(self, event): canvas_size = self.rect() width = self.current_pixmap_size.width() extrax = canvas_size.width() - width if extrax < 0: extrax = 0 x = int(extrax/2.) height = self.current_pixmap_size.height() extray = canvas_size.height() - height if extray < 0: extray = 0 y = int(extray/2.) target = QRect(x, y, width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) try: dpr = self.devicePixelRatioF() except AttributeError: dpr = self.devicePixelRatio() spmap = self.pixmap.scaled(target.size() * dpr, Qt.KeepAspectRatio, Qt.SmoothTransformation) spmap.setDevicePixelRatio(dpr) p.drawPixmap(target, spmap) if gprefs['bd_overlay_cover_size']: sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0'%(self.pixmap.width(), self.pixmap.height()) flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255,255,255))) p.drawText(sztgt, flags, sz) p.end()
def paint_non_printing(self, painter, option, charcode): text = self.np_pat.sub(r'\n\1', non_printing[charcode]) painter.drawText( option.rect, Qt.AlignCenter | Qt.TextWordWrap | Qt.TextWrapAnywhere, text) painter.setPen(QPen(Qt.DashLine)) painter.drawRect(option.rect.adjusted(1, 1, -1, -1))
def __init__(self, font_loader, footer, page_style, logger, opts, ruby_tags, link_activated): Canvas.__init__(self, font_loader, footer, logger, opts, ruby_tags, link_activated, page_style.textwidth, page_style.footheight) if opts.visual_debug: self.setPen(QPen(Qt.blue, 1, Qt.DashLine))
def paint(self, painter, option, index): name = index.data(Qt.DisplayRole) sz = human_readable(index.data(Qt.UserRole)) pmap = index.data(Qt.UserRole + 1) irect = option.rect.adjusted(0, 5, 0, -5) irect.setRight(irect.left() + 70) if pmap is None: pmap = QPixmap( current_container().get_file_path_for_processing(name)) scaled, nwidth, nheight = fit_image(pmap.width(), pmap.height(), irect.width(), irect.height()) if scaled: pmap = pmap.scaled(nwidth, nheight, transformMode=Qt.SmoothTransformation) index.model().setData(index, pmap, Qt.UserRole + 1) x, y = (irect.width() - pmap.width()) // 2, (irect.height() - pmap.height()) // 2 r = irect.adjusted(x, y, -x, -y) QStyledItemDelegate.paint(self, painter, option, empty_index) painter.drawPixmap(r, pmap) trect = irect.adjusted(irect.width() + 10, 0, 0, 0) trect.setRight(option.rect.right()) painter.save() if option.state & QStyle.State_Selected: painter.setPen( QPen(option.palette.color(option.palette.HighlightedText))) painter.drawText(trect, Qt.AlignVCenter | Qt.AlignLeft, name + '\n' + sz) painter.restore()
def do_paint(self, painter, option, index): text = str(index.data(Qt.DisplayRole) or '') font = QFont(option.font) font.setPointSize(QFontInfo(font).pointSize() * 1.5) font2 = QFont(font) font2.setFamily(text) system, has_latin = writing_system_for_font(font2) if has_latin: font = font2 r = option.rect if option.state & QStyle.State_Selected: painter.setPen(QPen(option.palette.highlightedText(), 0)) if (option.direction == Qt.RightToLeft): r.setRight(r.right() - 4) else: r.setLeft(r.left() + 4) painter.setFont(font) painter.drawText(r, Qt.AlignVCenter|Qt.AlignLeading|Qt.TextSingleLine, text) if (system != QFontDatabase.Any): w = painter.fontMetrics().width(text + " ") painter.setFont(font2) sample = QFontDatabase().writingSystemSample(system) if (option.direction == Qt.RightToLeft): r.setRight(r.right() - w) else: r.setLeft(r.left() + w) painter.drawText(r, Qt.AlignVCenter|Qt.AlignLeading|Qt.TextSingleLine, sample)
def paintEvent(self, event): pmap = self.blank if self.pixmap is None or self.pixmap.isNull( ) else self.pixmap target = self.rect() scaled, width, height = fit_image(pmap.width(), pmap.height(), target.width(), target.height()) target.setRect(target.x(), target.y(), width, height) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.pixmap is not None and not self.pixmap.isNull(): sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0' % (self.pixmap.width(), self.pixmap.height()) flags = Qt.AlignBottom | Qt.AlignRight | Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255, 255, 255))) p.drawText(sztgt, flags, sz) p.end()
def set_color(self, color: QColor): self.color = color self.line_item.setPen(QPen(color, 4)) if self.stick1 is not None: self.stick1.set_frame_color(self.color) if self.stick2 is not None: self.stick2.set_frame_color(self.color) self.update()
def mouseMoveEvent(self, mouseEvent): self.__currentPose = mouseEvent.pos() self.__painter.begin(self.__board) self.__painter.setPen(QPen(self.__penColor, self.__thickness)) self.__painter.drawLine(self.__lastPose, self.__currentPose) self.__painter.end() self.__lastPose = self.__currentPose self.update()
def __init__(self, parent, start, stop, refobj, slot): QGraphicsRectItem.__init__(self, start, 0, stop-start, parent.height, parent) self.refobj = refobj self.slot = slot self.brush = self.__class__.inactive_brush self.setPen(QPen(Qt.PenStyle.NoPen)) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setAcceptHoverEvents(True)
def failed_img(self): if self._failed_img is None: i = QImage(200, 150, QImage.Format_ARGB32) i.fill(Qt.white) p = QPainter(i) r = i.rect().adjusted(10, 10, -10, -10) n = QPen(Qt.DashLine) n.setColor(Qt.black) p.setPen(n) p.drawRect(r) p.setPen(Qt.black) f = self.font() f.setPixelSize(20) p.setFont(f) p.drawText(r.adjusted(10, 0, -10, 0), Qt.AlignCenter | Qt.TextWordWrap, _('Image could not be rendered')) p.end() self._failed_img = QPixmap.fromImage(i) return self._failed_img
def draw_snake_spinner(painter, rect, angle, light, dark): painter.setRenderHint(QPainter.Antialiasing) if rect.width() > rect.height(): delta = (rect.width() - rect.height()) // 2 rect = rect.adjusted(delta, 0, -delta, 0) elif rect.height() > rect.width(): delta = (rect.height() - rect.width()) // 2 rect = rect.adjusted(0, delta, 0, -delta) disc_width = max(3, min(rect.width() // 10, 8)) drawing_rect = QRect(rect.x() + disc_width, rect.y() + disc_width, rect.width() - 2 * disc_width, rect.height() - 2 *disc_width) gap = 60 # degrees gradient = QConicalGradient(drawing_rect.center(), angle - gap // 2) gradient.setColorAt((360 - gap//2)/360.0, light) gradient.setColorAt(0, dark) pen = QPen(QBrush(gradient), disc_width) pen.setCapStyle(Qt.RoundCap) painter.setPen(pen) painter.drawArc(drawing_rect, angle * 16, (360 - gap) * 16)
def paint (self,painter,option, widget): painter.setRenderHints(QtGui.QPainter.Antialiasing) painter.rotate(self.rotation) painter.translate(-self.size/2,-self.size/2) #painter.scale(300,600) #c = self.model.groupe_color_value[self.heros.groupe().attribs['color']] pen = QPen(QtCore.Qt.black) if self.isSelected() == True : pen.setWidth(2) else: pen.setWidth(1) painter.setPen(pen) radialGradient = QRadialGradient(self.size/2, self.size/2,self.size*0.8, self.size, self.size) radialGradient.setColorAt(0.0, QtCore.Qt.white) #radialGradient.setColorAt(0.2, self.heros.kingdom().color) if self.view.color_mode == ColorMode.Empire : color = self.heros.empire().color elif self.view.color_mode == ColorMode.Kingdom : color = self.heros.kingdom().color else: color = self.model.groupe_color_value[self.heros.groupe().attribs['color']] radialGradient.setColorAt(0.2, color) radialGradient.setColorAt(1.0, QtCore.Qt.black) painter.setBrush(QBrush(radialGradient)) if self.heros.faction().name == "Lumiere": painter.drawEllipse(0,0,self.size,self.size) else: poly = self.getTriangle(self.Size) painter.drawPolygone(poly) if self.name_visible == True : painter.setPen(QPen(QColor('black'))) painter.translate(0,10) self.ratio = 1.2 rect = QRectF(0,0,self.size,self.size*self.ratio) painter.drawText (rect,QtCore.Qt.AlignHCenter|QtCore.Qt.AlignBottom, self.name)
def paint (self,painter,option, widget): painter.setRenderHints(QtGui.QPainter.Antialiasing) pen = QPen(self.color) pen.setWidth(2) pen.setStyle(QtCore.Qt.DashLine) painter.setPen(pen) for action in self.liste_actions.values() : if action.isActive(): if type(action) == ActionMoveToPosition: i = 0 for heros in action.list_left: start_x, start_y = self.scene_coord.LatLonToScene(heros.attribs['latitude'], heros.attribs['longitude']) destination = action.list_right[i] end_x,end_y = self.scene_coord.LatLonToScene(destination.x(), destination.y()) i = (i +1)%len(action.list_right) self.setPos(QPointF(start_x,start_y)) dest = self.view.mapFromScene(QPoint(end_x,end_y)) str = self.view.mapFromScene(QPoint(start_x,start_y)) dest = dest-str painter.drawLine(QPointF(0,0),dest)
def paintEvent(self, event): QWidget.paintEvent(self, event) pmap = self._pixmap if pmap.isNull(): return w, h = pmap.width(), pmap.height() ow, oh = w, h cw, ch = self.rect().width(), self.rect().height() scaled, nw, nh = fit_image(w, h, cw, ch) if scaled: pmap = pmap.scaled(int(nw*pmap.devicePixelRatio()), int(nh*pmap.devicePixelRatio()), Qt.IgnoreAspectRatio, Qt.SmoothTransformation) w, h = int(pmap.width()/pmap.devicePixelRatio()), int(pmap.height()/pmap.devicePixelRatio()) x = int(abs(cw - w)/2.) y = int(abs(ch - h)/2.) target = QRect(x, y, w, h) p = QPainter(self) p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) p.drawPixmap(target, pmap) if self.draw_border: pen = QPen() pen.setWidth(self.BORDER_WIDTH) p.setPen(pen) p.drawRect(target) if self.show_size: sztgt = target.adjusted(0, 0, 0, -4) f = p.font() f.setBold(True) p.setFont(f) sz = u'\u00a0%d x %d\u00a0'%(ow, oh) flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine szrect = p.boundingRect(sztgt, flags, sz) p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200)) p.setPen(QPen(QColor(255,255,255))) p.drawText(sztgt, flags, sz) p.end()
def paint (self,painter,option, widget): painter.setRenderHints(QtGui.QPainter.Antialiasing) painter.rotate(self.rotation) painter.translate(-16,-16) # taille fixe correspondant a la pixmap utilisee pour generer la geometry painter.scale(1.5,1.5) if self.isSelected() == True : pen = QPen(QtCore.Qt.red) pen.setWidth(1) else: pen = QPen(QtCore.Qt.black) pen.setWidth(1) painter.setPen(pen) radialGradient = QRadialGradient(self.size.width()/2, self.size.height()/2,self.size.width()*0.8, self.size.width(), self.size.height()) radialGradient.setColorAt(0.0, QtCore.Qt.white) #radialGradient.setColorAt(0.2, self.heros.kingdom().color) if self.view.color_mode == ColorMode.Empire : color = self.model.empire().color else : color = self.model.kingdom().color radialGradient.setColorAt(0.2, color) radialGradient.setColorAt(1.0, QtCore.Qt.black) painter.setBrush(QBrush(radialGradient)) #brush = QBrush(self.color) #painter.setBrush(brush) geometry = self.model.empire().geometry #qDebug("info : map_item Temple : nombre de polygones %d"%len(geometry['polygon'])) for p in geometry['polygon']: painter.drawPolygon(QPolygon(p)) # # painter.drawPolygon(self.polygon) # path = os.path.join(Config().instance.path_to_icons(),'kingdom','32x32') # path+="/temple_artemis.png" # print ('path icon',path) # pix = QPixmap(path) # # painter.drawPixmap(-pix.width()/2.0,-pix.height()/2.0,pix) if self.name_visible == True : painter.setPen(QPen(QColor('black'))) painter.translate(0,10) painter.drawText (self.boundingRect(),QtCore.Qt.AlignHCenter|QtCore.Qt.AlignBottom, self.name)
def paint(self, painter, option, widget): pen = QPen(Qt.red) pen.setWidth(2) painter.setPen(pen) painter.drawRoundedRect(1, 1, self._width - 3, self._height - 3, 5, 5)
def __init__(self, color, width): QPen.__init__(self, QBrush(Color(color)), width, (Qt.SolidLine if width > 0 else Qt.NoPen))