def paintEvent(self, event): background = QRadialGradient(QPointF(self.rect().topLeft()), 500, QPointF(self.rect().bottomRight())) background.setColorAt(0, self.backgroundColor1) background.setColorAt(1, self.backgroundColor2) painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(event.rect(), QBrush(background)) painter.setPen(self.pen) for bubble in self.bubbles: if QRectF(bubble.position - QPointF(bubble.radius, bubble.radius), QSizeF(2*bubble.radius, 2*bubble.radius)).intersects(QRectF(event.rect())): bubble.drawBubble(painter) if self.newBubble: self.newBubble.drawBubble(painter) painter.end()
def brushChanged(self): style = Qt.BrushStyle(self.brushStyleComboBox.itemData( self.brushStyleComboBox.currentIndex(), IdRole)) if style == Qt.LinearGradientPattern: linearGradient = QLinearGradient(0, 0, 100, 100) linearGradient.setColorAt(0.0, Qt.white) linearGradient.setColorAt(0.2, Qt.green) linearGradient.setColorAt(1.0, Qt.black) self.renderArea.setBrush(QBrush(linearGradient)) elif style == Qt.RadialGradientPattern: radialGradient = QRadialGradient(50, 50, 50, 70, 70) radialGradient.setColorAt(0.0, Qt.white) radialGradient.setColorAt(0.2, Qt.green) radialGradient.setColorAt(1.0, Qt.black) self.renderArea.setBrush(QBrush(radialGradient)) elif style == Qt.ConicalGradientPattern: conicalGradient = QConicalGradient(50, 50, 150) conicalGradient.setColorAt(0.0, Qt.white) conicalGradient.setColorAt(0.2, Qt.green) conicalGradient.setColorAt(1.0, Qt.black) self.renderArea.setBrush(QBrush(conicalGradient)) elif style == Qt.TexturePattern: self.renderArea.setBrush(QBrush(QPixmap(':/images/brick.png'))) else: self.renderArea.setBrush(QBrush(Qt.green, style))
def __init__(self, nx_node, pos): """ Create node in the graph scene :param tuple nx_node: Node info :param x_y: Position of the node """ super().__init__(nx_node, pos) # color around ellipse outline_color = QColor('grey') outline_style = Qt.SolidLine outline_width = 1 if self.status_wallet: outline_color = QColor('black') outline_width = 2 if not self.status_member: outline_color = QColor('red') outline_style = Qt.SolidLine self.setPen(QPen(outline_color, outline_width, outline_style)) # text inside ellipse self.text_item = QGraphicsSimpleTextItem(self) self.text_item.setText(self.text) text_color = QColor('grey') if self.status_wallet == NodeStatus.HIGHLIGHTED: text_color = QColor('black') self.text_item.setBrush(QBrush(text_color)) # center ellipse around text self.setRect( 0, 0, self.text_item.boundingRect().width() * 2, self.text_item.boundingRect().height() * 2 ) # set anchor to the center self.setTransform( QTransform().translate(-self.boundingRect().width() / 2.0, -self.boundingRect().height() / 2.0)) # center text in ellipse self.text_item.setPos(self.boundingRect().width() / 4.0, self.boundingRect().height() / 4.0) # create gradient inside the ellipse gradient = QRadialGradient(QPointF(0, self.boundingRect().height() / 4), self.boundingRect().width()) gradient.setColorAt(0, QColor('white')) gradient.setColorAt(1, QColor('darkgrey')) self.setBrush(QBrush(gradient)) # cursor change on hover self.setAcceptHoverEvents(True) self.setZValue(1)
def paint(self, QPainter, QStyleOptionGraphicsItem, QWidget_widget=None): QPainter.setClipRect(self.rect) center = QPointF(self.rect.width() / 2, self.rect.height() / 2) focalPoint = center grad = QRadialGradient(center, float(self.rect.width() * 0.58929), focalPoint) col = QColor(bConfig['checkColor']) grad.setColorAt(0, col) grad.setColorAt(1, self.brush.color()) col = QColor(bConfig['checkColor']) col.setAlphaF(float(bConfig['effectsAlpha'])) QPainter.setBrush(QBrush(col)) QPainter.setPen(QPen(Qt.NoPen)) QPainter.fillRect(self.rect, grad)
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 setupScene(self): self.m_scene.setSceneRect(-300, -200, 600, 460) linearGrad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100)) linearGrad.setColorAt(0, Qt.darkGreen)#QColor(255, 255, 255)) linearGrad.setColorAt(1, Qt.green)#QQColor(192, 192, 255)) self.setBackgroundBrush(linearGrad) radialGrad = QRadialGradient(30, 30, 30) radialGrad.setColorAt(0, Qt.yellow) radialGrad.setColorAt(0.2, Qt.yellow) radialGrad.setColorAt(1, Qt.transparent) pixmap = QPixmap(60, 60) pixmap.fill(Qt.transparent) painter = QPainter(pixmap) painter.setPen(Qt.NoPen) painter.setBrush(radialGrad) painter.drawEllipse(0, 0, 60, 60) painter.end() self.m_lightSource = self.m_scene.addPixmap(pixmap) self.m_lightSource.setZValue(2) self.proto = ProtoObj(0, 0, 50, 50, self) self.proto.initObj() #self.m_items.append(self.proto.getObj()[0]) self.m_scene.addItem(self.proto.getObj()[0])
def updateBrush(self): gradient = QRadialGradient(QPointF(self.radius, self.radius), self.radius, QPointF(self.radius*0.5, self.radius*0.5)) gradient.setColorAt(0, QColor(255, 255, 255, 255)) gradient.setColorAt(0.25, self.innerColor) gradient.setColorAt(1, self.outerColor) self.brush = QBrush(gradient)
def __init__(self, parent=None, ast=None): ''' Create the INPUT symbol ''' ast = ast or ogAST.Input() self.ast = ast self.branch_entrypoint = None if not ast.pos_y and parent: # Make sure the item is placed below its parent ast.pos_y = parent.y() + parent.boundingRect().height() + 10 super(Input, self).__init__(parent, text=ast.inputString, x=ast.pos_x or 0, y=ast.pos_y or 0, hyperlink=ast.hyperlink) self.set_shape(ast.width, ast.height) gradient = QRadialGradient(50, 50, 50, 50, 50) gradient.setColorAt(0, QColor(255, 240, 170)) gradient.setColorAt(1, Qt.white) self.setBrush(QBrush(gradient)) self.terminal_symbol = False self.parser = ogParser if ast.comment: Comment(parent=self, ast=ast.comment)
def _refresh_colors(self): """ Refresh elements in the node """ # color around ellipse outline_color = QColor('black') outline_style = Qt.SolidLine outline_width = 1 if self.status_wallet: outline_color = QColor('grey') outline_width = 2 if not self.status_member: outline_color = QColor('red') outline_style = Qt.SolidLine self.setPen(QPen(outline_color, outline_width, outline_style)) if self.highlighted: text_color = QColor('grey') else: text_color = QColor('black') if self.status_wallet == NodeStatus.HIGHLIGHTED: text_color = QColor('grey') self.text_item.setBrush(QBrush(text_color)) # create gradient inside the ellipse gradient = QRadialGradient(QPointF(0, self.boundingRect().height() / 4), self.boundingRect().width()) color = QColor() color.setHsv(120 - 60 / self.steps_max * self.steps, 180 + 50 / self.steps_max * self.steps, 60 + 170 / self.steps_max * self.steps) if self.highlighted: color = color.darker(200) color = color.lighter(math.fabs(math.sin(self.loading_counter / 100 * math.pi) * 100) + 100) gradient.setColorAt(0, color) gradient.setColorAt(1, color.darker(150)) self.setBrush(QBrush(gradient))
def setupScene(self): self.m_scene.setSceneRect(-300, -200, 600, 460) linearGrad = QLinearGradient(QPointF(-100, -100), QPointF(100, 100)) linearGrad.setColorAt(0, QColor(255, 255, 255)) linearGrad.setColorAt(1, QColor(192, 192, 255)) self.setBackgroundBrush(linearGrad) radialGrad = QRadialGradient(30, 30, 30) radialGrad.setColorAt(0, Qt.yellow) radialGrad.setColorAt(0.2, Qt.yellow) radialGrad.setColorAt(1, Qt.transparent) pixmap = QPixmap(60, 60) pixmap.fill(Qt.transparent) painter = QPainter(pixmap) painter.setPen(Qt.NoPen) painter.setBrush(radialGrad) painter.drawEllipse(0, 0, 60, 60) painter.end() self.m_lightSource = self.m_scene.addPixmap(pixmap) self.m_lightSource.setZValue(2) for i in range(-2, 3): for j in range(-2, 3): if (i + j) & 1: item = QGraphicsEllipseItem(0, 0, 50, 50) else: item = QGraphicsRectItem(0, 0, 50, 50) item.setPen(QPen(Qt.black, 1)) item.setBrush(QBrush(Qt.white)) effect = QGraphicsDropShadowEffect(self) effect.setBlurRadius(8) item.setGraphicsEffect(effect) item.setZValue(1) item.setPos(i * 80, j * 80) self.m_scene.addItem(item) self.m_items.append(item)
def paintEvent(self, event): painter = QPainter() x = 0 y = 0 if self._alignment & Qt.AlignLeft: x = 0 elif self._alignment & Qt.AlignRight: x = self.width() - self._diameter elif self._alignment & Qt.AlignHCenter: x = (self.width() - self._diameter) / 2 elif self._alignment & Qt.AlignJustify: x = 0 if self._alignment & Qt.AlignTop: y = 0 elif self._alignment & Qt.AlignBottom: y = self.height() - self._diameter elif self._alignment & Qt.AlignVCenter: y = (self.height() - self._diameter) / 2 gradient = QRadialGradient(x + self._diameter / 2, y + self._diameter / 2, self._diameter * 0.4, self._diameter * 0.4, self._diameter * 0.4) gradient.setColorAt(0, Qt.white) if self._state: gradient.setColorAt(1, self._color) else: gradient.setColorAt(1, Qt.black) painter.begin(self) brush = QBrush(gradient) painter.setPen(self._color) painter.setRenderHint(QPainter.Antialiasing, True) painter.setBrush(brush) painter.drawEllipse(x, y, self._diameter - 1, self._diameter - 1) if self._flashRate > 0 and self._flashing: self._timer.start(self._flashRate) else: self._timer.stop() painter.end()
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 _initUI(self): # Set window title and background color self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Set window background color p = QPalette() gradient = QRadialGradient(300, 450, 500) gradient.setColorAt(0.0, QColor(0, 108, 31)) gradient.setColorAt(1.0, QColor(0, 64, 18)) p.setBrush(QPalette.Window, QBrush(gradient)) self.setPalette(p) # Initialize main menu widgets self._init_main_menu_widgets() self._init_blackjack_menu_widgets() self._init_blackjack_game_widgets() self._display_main_menu() # self._display_blackjack_menu() # self._display_blackjack_game() self.show()
def paintEvent(self, _event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) width = self.width() height = self.height() if self.dynamic_resize: knob_radius = self.dynamic_knob_radius else: knob_radius = self.knob_radius # ensure that the center point is in the middle of a pixel to ensure # that exact vertial and horizantal ticks are drawn exactly 1px wide x = math.floor(width / 2.0) + 0.5 y = math.floor(height / 2.0) + 0.5 if DEBUG: painter.fillRect(0, 0, width, height, Qt.yellow) painter.translate(x, y) if self.knob_style == KnobWidget.STYLE_NEEDLE: r = min(x, y) - 1 painter.setPen(Qt.white) painter.setBrush(Qt.white) painter.drawEllipse(QPoint(0, 0), r, r) angle = self.value_factor * self.total_angle - (self.total_angle / 2.0) # draw base knob or needle spike if self.knob_style == KnobWidget.STYLE_ROUND: painter.setPen(self.border_color) if self.pressed: gradient = QRadialGradient(0, 0, knob_radius) gradient.setColorAt(0, self.base_color_pressed) gradient.setColorAt(0.85, self.base_color) gradient.setColorAt(1, self.base_color) painter.setBrush(gradient) else: painter.setBrush(self.base_color) painter.drawEllipse(QPoint(0, 0), knob_radius, knob_radius) elif self.knob_style == KnobWidget.STYLE_NEEDLE: painter.save() painter.rotate(angle) painter.setPen(self.needle_color) painter.setBrush(self.needle_color) needle = QPolygonF() needle.append(QPointF(self.needle_base_radius * 0.6, 0)) needle.append(QPointF(0, -knob_radius)) needle.append(QPointF(-self.needle_base_radius * 0.6, 0)) painter.drawPolygon(needle) painter.restore() # draw knob mark or needle base if self.knob_style == KnobWidget.STYLE_ROUND: painter.save() painter.rotate(angle) painter.setPen(QPen(self.mark_color, 2)) painter.drawLine(0, -knob_radius * 0.4, 0, -knob_radius * 0.8) painter.restore() elif self.knob_style == KnobWidget.STYLE_NEEDLE: painter.setPen(self.border_color) painter.setBrush(self.base_color) painter.drawEllipse(QPoint(0, 0), self.needle_base_radius, self.needle_base_radius) if self.scale_visible: painter.setPen(Qt.black) # draw scale arc if self.scale_arc_visible: painter.drawArc(-knob_radius - self.knob_to_scale, -knob_radius - self.knob_to_scale, knob_radius * 2 + self.knob_to_scale * 2, knob_radius * 2 + self.knob_to_scale * 2, (90 + self.total_angle / 2) * 16, -self.total_angle * 16) # draw scale ticks def value_to_angle(value): return (float(value - self.minimum_value) / self.value_range) * self.total_angle - (self.total_angle / 2.0) value = self.minimum_value while value <= self.maximum_value: angle = value_to_angle(value) painter.save() painter.rotate(value_to_angle(value)) painter.drawLine(0, -knob_radius - self.knob_to_scale, 0, -knob_radius - self.knob_to_scale - self.tick_size_large) if self.scale_text_visible: p = painter.worldTransform().map(QPoint(0, -knob_radius - \ self.knob_to_scale - \ self.tick_size_large - \ self.tick_to_text - \ self.text_radius)) painter.restore() if self.scale_text_visible: if DEBUG: painter.save() painter.setPen(QColor(255, 0, 0, 50)) painter.setBrush(QColor(255, 0, 0, 50)) painter.drawEllipse(QPoint(p.x() - x, p.y() - y), self.text_radius, self.text_radius) painter.restore() painter.drawText(p.x() - x - 30, p.y() - y - 30, 60, 60, Qt.TextDontClip | Qt.AlignHCenter | Qt.AlignVCenter, str(value)) for i in range(1, self.scale_step_divisions): sub_value = value + (float(self.scale_step_size) * i) / self.scale_step_divisions if sub_value > self.maximum_value: break painter.save() painter.rotate(value_to_angle(sub_value)) painter.drawLine(0, -knob_radius - self.knob_to_scale, 0, -knob_radius - self.knob_to_scale - self.tick_size_small) painter.restore() value += self.scale_step_size if self.title_text != None: painter.drawText(-knob_radius, knob_radius - 30, knob_radius * 2, 60, Qt.TextDontClip | Qt.AlignHCenter | Qt.AlignVCenter, self.title_text)
def paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) #self.painter.setPen(self.backgroundColor) size = self.size() brush = QBrush() #rect = QtCore.QRect(2, 2, size.width()-4, size.height()-4) rect = QtCore.QRect(0, 0, size.width(), size.height()) #painter.fillRect(rect, brush) smallestDim = size.width() if smallestDim > size.height(): smallestDim = size.height(); smallestDim = smallestDim/2 smallestDim -= 2 ## rect.moveCenter(QPoint(size.width()/2,size.height()/2)) center_x = size.width()/2 center_y = size.height()/2 centerpoint = QPoint(center_x,center_y) # Draw the border #circle_path = QPainterPath() radius = smallestDim #circle_path.addEllipse(centerpoint,radius,radius) painter.setPen(QPen(QColor('lightgray'),0)) brush.setStyle(Qtc.SolidPattern) radial = QRadialGradient(center_x,center_y/2, radius) #radial = QConicalGradient(centerpoint, 50) # Last number is the angle radial.setColorAt(0, Qtc.white) radial.setColorAt(0.8, Qtc.darkGray) painter.setBrush(QBrush(radial)) painter.drawEllipse(centerpoint,radius,radius) #painter.setBrush(QColor('gray')) # painter.drawPath(circle_path) # Draw the colored center radial = QRadialGradient(center_x,center_y/2, radius) #radial = QRadialGradient(center_x*2/3,center_y*2/3, radius) #radial = QConicalGradient(centerpoint, 0) # Last number is the angle radial.setColorAt(0, Qtc.white) if (self.curState): radial.setColorAt(.7, self.onColor) brush.setColor(self.onColor) painter.setPen(QPen(self.onColor,0)) else: radial.setColorAt(.7, self.offColor) brush.setColor(self.offColor) painter.setPen(QPen(self.offColor,0)) brush.setStyle(Qtc.SolidPattern) #painter.setBrush(brush) painter.setBrush(QBrush(radial)) #radius = radius - 9 if (smallestDim <= 30): radius = radius - 3 elif (smallestDim <= 60): radius = radius - 4 elif (smallestDim <=100): radius = radius - 5 elif (smallestDim <= 200): radius = radius - 6 elif (smallestDim <= 300): radius = radius - 7 else: radius = radius - 9 painter.drawEllipse(centerpoint,radius,radius)
def drawMagnifierOnVideo(width, height, maskPixmap, dragPos, zoomPixmap, surface, painter, offset): ''' Draw Magnifier on Video ''' dim = min(width, height) MAX_MAGNIFIER = 229 magnifierSize = min(MAX_MAGNIFIER, dim * 2 / 3) radius = magnifierSize / 2 ring = radius - 15 box = QSize(magnifierSize, magnifierSize) # reupdate our mask if maskPixmap.size() != box: maskPixmap = QPixmap(box) maskPixmap.fill(Qt.transparent) g = QRadialGradient() g.setCenter(radius, radius) g.setFocalPoint(radius, radius) g.setRadius(radius) g.setColorAt(1.0, QColor(64, 64, 64, 0)) g.setColorAt(0.5, QColor(0, 0, 0, 255)) mask = QPainter(maskPixmap) mask.setRenderHint(QPainter.HighQualityAntialiasing) mask.setCompositionMode(QPainter.CompositionMode_Source) mask.setBrush(g) mask.setPen(Qt.NoPen) mask.drawRect(maskPixmap.rect()) mask.setBrush(QColor(Qt.transparent)) mask.drawEllipse(g.center(), ring, ring) mask.end() center = dragPos - QPoint(0, radius) center += QPoint(0, radius / 2) corner = center - QPoint(radius, radius) xy = center * 2 - QPoint(radius, radius) # only set the dimension to the magnified portion if zoomPixmap.size() != box: zoomPixmap = QPixmap(box) zoomPixmap.fill(Qt.lightGray) if True: painter_p = QPainter(zoomPixmap) painter_p.translate(-xy) largePixmap = QPixmap.fromImage(surface.image) painter_p.drawPixmap(offset, largePixmap) painter_p.end() clipPath = QPainterPath() clipPath.addEllipse(QPointF(center), ring, ring) painter.setClipPath(clipPath) painter.drawPixmap(corner, zoomPixmap) painter.setClipping(False) painter.drawPixmap(corner, maskPixmap) painter.setPen(Qt.gray) painter.drawPath(clipPath) return
__Author__ = 'Irony' __Copyright__ = 'Copyright (c) 2019' # 线性渐变 linearGradient = QLinearGradient(0, 0, 1, 1) linearGradient.setColorAt(0.0, Qt.green) linearGradient.setColorAt(0.2, Qt.white) linearGradient.setColorAt(0.4, Qt.blue) linearGradient.setColorAt(0.6, Qt.red) linearGradient.setColorAt(1.0, Qt.yellow) print(GradientUtils.styleSheetCode(linearGradient)) # 辐射渐变 radialGradient = QRadialGradient(0, 0, 1, 1, 110) radialGradient.setColorAt(0, Qt.green) radialGradient.setColorAt(0.4, Qt.blue) radialGradient.setColorAt(1.0, Qt.yellow) print(GradientUtils.styleSheetCode(radialGradient)) # 弧度渐变 conicalGradient = QConicalGradient(0.5, 0.5, 0) conicalGradient.setAngle(0.5) conicalGradient.setColorAt(0, Qt.green) conicalGradient.setColorAt(0.2, Qt.white) conicalGradient.setColorAt(0.4, Qt.blue) conicalGradient.setColorAt(0.6, Qt.red) conicalGradient.setColorAt(0.8, Qt.yellow)
def __init__(self, x, y, c, r, pos): radialGradient = QRadialGradient(x, y, r) radialGradient.setColorAt(0.0, Qt.white) radialGradient.setColorAt(pos, c) radialGradient.setColorAt(1, QColor(0, 0, 0, 0)) super().__init__(radialGradient)
def gradientCirclePixmap(self): """白色带阴影 """ xy = self.height() / 2 radius = self.height() * 0.8 # 绘制普通状态下圆形的滑块 circleColor = QRadialGradient(xy, xy, radius, xy, xy) circleColor.setColorAt(0.5, QColor(254, 254, 254)) circleColor.setColorAt(0.7, QColor(0, 0, 0, 60)) circleColor.setColorAt(0.7, QColor(0, 0, 0, 30)) circleColor.setColorAt(0.9, QColor(0, 0, 0, 0)) self._imageCircle = QImage(self.height(), self.height(), QImage.Format_ARGB32) self._imageCircle.fill(Qt.transparent) painter = QPainter() painter.begin(self._imageCircle) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setPen(Qt.NoPen) painter.setBrush(circleColor) painter.drawRoundedRect(0, 0, self.height(), self.height(), xy, xy) painter.end() # 绘制悬停状态下圆形的滑块 circleColorHover = QRadialGradient(xy, xy, radius, xy, xy) circleColorHover.setColorAt(0.5, QColor(245, 245, 245)) circleColorHover.setColorAt(0.7, QColor(0, 0, 0, 30)) circleColorHover.setColorAt(0.9, QColor(0, 0, 0, 0)) self._imageCircleHover = QImage(self.height(), self.height(), QImage.Format_ARGB32) self._imageCircleHover.fill(Qt.transparent) painter = QPainter() painter.begin(self._imageCircleHover) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setPen(Qt.NoPen) painter.setBrush(circleColorHover) painter.drawRoundedRect(0, 0, self.height(), self.height(), xy, xy) painter.end()
def createGradient(self): center = QPointF(0, 0) self.gradient = QRadialGradient(center, self._outerRadius, center) self.gradient.setColorAt(0.5, QColor(self._innerColor)) self.gradient.setColorAt(1.0, QColor(self._outerColor))
def __init__(self, metadata, x_y): """ Create node in the graph scene :param dict metadata: Node metadata :param x_y: Position of the node """ # unpack tuple x, y = x_y super(Node, self).__init__() self.metadata = metadata self.id = metadata['id'] self.status_wallet = self.metadata['status'] & NODE_STATUS_HIGHLIGHTED self.status_member = not self.metadata['status'] & NODE_STATUS_OUT self.text = self.metadata['text'] self.setToolTip(self.metadata['tooltip']) self.arcs = [] self.menu = None self.action_sign = None self.action_transaction = None self.action_contact = None self.action_show_member = None # color around ellipse outline_color = QColor('grey') outline_style = Qt.SolidLine outline_width = 1 if self.status_wallet: outline_color = QColor('black') outline_width = 2 if not self.status_member: outline_color = QColor('red') outline_style = Qt.SolidLine self.setPen(QPen(outline_color, outline_width, outline_style)) # text inside ellipse self.text_item = QGraphicsSimpleTextItem(self) self.text_item.setText(self.text) text_color = QColor('grey') if self.status_wallet == NODE_STATUS_HIGHLIGHTED: text_color = QColor('black') self.text_item.setBrush(QBrush(text_color)) # center ellipse around text self.setRect( 0, 0, self.text_item.boundingRect().width() * 2, self.text_item.boundingRect().height() * 2 ) # set anchor to the center self.setTransform( QTransform().translate(-self.boundingRect().width() / 2.0, -self.boundingRect().height() / 2.0)) self.setPos(x, y) # center text in ellipse self.text_item.setPos(self.boundingRect().width() / 4.0, self.boundingRect().height() / 4.0) # create gradient inside the ellipse gradient = QRadialGradient(QPointF(0, self.boundingRect().height() / 4), self.boundingRect().width()) gradient.setColorAt(0, QColor('white')) gradient.setColorAt(1, QColor('darkgrey')) self.setBrush(QBrush(gradient)) # cursor change on hover self.setAcceptHoverEvents(True) self.setZValue(1)
def paint(self, painter, option, widget): if self.is_node1: color = Qt.green dark_color = Qt.darkGreen else: color = Qt.yellow dark_color = Qt.darkYellow gradient = QRadialGradient(-3, -3, 10) if option.state & QStyle.State_MouseOver: gradient.setCenter(3, 3) gradient.setFocalPoint(3, 3) gradient.setColorAt(1, QColor(color).lighter(120)) gradient.setColorAt(0, QColor(dark_color).lighter(120)) else: gradient.setColorAt(0, color) gradient.setColorAt(1, dark_color) painter.setBrush(QBrush(gradient)) painter.setPen(QPen(Qt.black, 0)) painter.drawEllipse(-10, -10, 20, 20) # node text painter.setPen(QPen(QColor(Qt.black), 0)) painter.drawText(13, 4, "%s" % self.node_record.index)
def __paintRound(self): """ Private method to paint a round raised LED. """ # Initialize coordinates, width and height of the LED width = self.__getBestRoundSize() # Calculate the gradient for the LED wh = width / 2 color = self.__led_on and self.__led_color or self.__offcolor gradient = QRadialGradient(wh, wh, wh, 0.8 * wh, 0.8 * wh) gradient.setColorAt(0.0, color.lighter(200)) gradient.setColorAt(0.6, color) if self.__framedLed: gradient.setColorAt(0.9, color.darker()) gradient.setColorAt(1.0, self.palette().color(QPalette.Dark)) else: gradient.setColorAt(1.0, color.darker()) # now do the drawing paint = QPainter(self) paint.setRenderHint(QPainter.Antialiasing, True) paint.setBrush(QBrush(gradient)) paint.setPen(Qt.NoPen) paint.drawEllipse(1, 1, width, width) paint.end()
def paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) size = self.size() brush = QBrush() smallest_dim = size.width() if smallest_dim > size.height(): smallest_dim = size.height() smallest_dim = smallest_dim / 2 smallest_dim -= 2 center_x = size.width() / 2 center_y = size.height() / 2 centerpoint = QPoint(center_x, center_y) radius = smallest_dim painter.setPen(QPen(QColor('lightgray'), 0)) brush.setStyle(Qtc.SolidPattern) radial = QRadialGradient(center_x, center_y / 2, radius) radial.setColorAt(0, Qtc.white) radial.setColorAt(0.8, Qtc.darkGray) painter.setBrush(QBrush(radial)) painter.drawEllipse(centerpoint, radius, radius) # Draw the colored center radial = QRadialGradient(center_x, center_y / 2, radius) radial.setColorAt(0, Qtc.white) if self.curState: radial.setColorAt(.7, self.on_color) brush.setColor(self.on_color) painter.setPen(QPen(self.on_color, 0)) else: radial.setColorAt(.7, self.off_color) brush.setColor(self.off_color) painter.setPen(QPen(self.off_color, 0)) brush.setStyle(Qtc.SolidPattern) painter.setBrush(QBrush(radial)) if smallest_dim <= 30: radius = radius - 3 elif smallest_dim <= 60: radius = radius - 4 elif smallest_dim <= 100: radius = radius - 5 elif smallest_dim <= 200: radius = radius - 6 elif smallest_dim <= 300: radius = radius - 7 else: radius = radius - 9 painter.drawEllipse(centerpoint, radius, radius)
class DeusEx(QGraphicsObject): deusExActivateSignal = pyqtSignal(DeusExSignalData) def __init__(self, config, type, pulseSound, endingSound): super().__init__() self.type = type self.config = config self.width = 200 self.height = 200 self.m_boundingRect = QRectF(0, 0, self.width, self.height) self.m_painterPath = QPainterPath() self.m_painterPath.addEllipse(self.m_boundingRect) # radial gradient settings self.rgcx = self.m_boundingRect.center().x() self.rgcy = self.m_boundingRect.center().y() self.rgMinRadius = 50 self.rgMaxRadius = 300 self.rgCurrentRadius = 50 self.rgfx = self.rgcx self.rgfy = self.rgcy self.rg = QRadialGradient(self.rgcx, self.rgcy, self.rgCurrentRadius, self.rgfx, self.rgfy) if self.type is DeusExTypes.POSITIVE: firstClr = QColor(Qt.green) firstClr.setAlphaF(0.7) secondClr = QColor(Qt.darkGreen) secondClr.setAlphaF(0.7) self.rg.setColorAt(0.0, firstClr) self.rg.setColorAt(1.0, secondClr) else: firstClr = QColor(Qt.red) firstClr.setAlphaF(0.7) secondClr = QColor(Qt.darkRed) secondClr.setAlphaF(0.7) self.rg.setColorAt(0.0, firstClr) self.rg.setColorAt(1.0, secondClr) # pulsing sound self.pulseSound = pulseSound self.endingSound = endingSound # pulsing timer self.pulseTimer = QTimer() self.pulseTimer.setTimerType(Qt.PreciseTimer) self.pulseTimer.timeout.connect(self.pulse) self.pulseTimer.start(100) # pre activate timer self.preActivateTimer = QTimer() self.preActivateTimer.setTimerType(Qt.PreciseTimer) self.preActivateTimer.timeout.connect(self.preActivate) if self.type is DeusExTypes.POSITIVE: self.preActivateTimer.start(10000) else: self.preActivateTimer.start(3000) # activate timer self.activateTimer = QTimer() self.activateTimer.setTimerType(Qt.PreciseTimer) self.activateTimer.timeout.connect(self.endingSoundFinished) def boundingRect(self): return self.m_boundingRect def shape(self): return self.m_painterPath def paint(self, QPainter, QStyleOptionGraphicsItem, widget=None): pen = QPen() if self.type is DeusExTypes.POSITIVE: pen.setColor(Qt.darkGreen) else: pen.setColor(Qt.darkRed) brush = QBrush(self.rg) QPainter.setPen(pen) QPainter.setBrush(brush) QPainter.drawEllipse(self.m_boundingRect) def pulse(self): if self.rgCurrentRadius == 50: self.pulseSound.play() self.rgCurrentRadius += 20 self.rg.setCenterRadius(self.rgCurrentRadius) if self.rgCurrentRadius >= self.rgMaxRadius: self.rgCurrentRadius = self.rgMinRadius def preActivate(self): firstClr = QColor(Qt.yellow) firstClr.setAlphaF(0.7) secondClr = QColor(Qt.darkYellow) secondClr.setAlphaF(0.7) self.rg.setColorAt(0.0, firstClr) self.rg.setColorAt(1.0, secondClr) self.pulseSound.stop() self.pulseTimer.timeout.disconnect() self.preActivateTimer.timeout.disconnect() self.pulseTimer.stop() self.preActivateTimer.stop() # activate self.endingSound.play() self.activateTimer.start() def endingSoundFinished(self): if self.endingSound.get_state() == AL_STOPPED: deusExSignalData = DeusExSignalData(self.type) for obj in self.collidingItems(): if type(obj).__name__ == "Player": deusExSignalData.markedPlayers.append(obj) self.activateTimer.timeout.disconnect() self.activateTimer.stop() self.deusExActivateSignal.emit(deusExSignalData) self.scene().removeItem(self) del self
def __paintRound(self): """ Private method to paint a round raised LED. """ # Initialize coordinates, width and height of the LED width = self.__getBestRoundSize() # Calculate the gradient for the LED wh = width / 2 color = self.__led_on and self.__led_color or self.__offcolor gradient = QRadialGradient(wh, wh, wh, 0.8 * wh, 0.8 * wh) gradient.setColorAt(0.0, color.lighter(200)) gradient.setColorAt(0.6, color) if self.__framedLed: gradient.setColorAt(0.9, color.darker()) gradient.setColorAt(1.0, self.palette().color(QPalette.Dark)) else: gradient.setColorAt(1.0, color.darker()) # now do the drawing paint = QPainter(self) paint.setRenderHint(QPainter.Antialiasing, True) paint.setBrush(QBrush(gradient)) paint.setPen(Qt.NoPen) paint.drawEllipse(1, 1, width, width) paint.end()
def paintEvent(self, event): self._center = np.array((self.width() / 2.0, self.height() / 2.0)) self._radius = min(self.width(), self.height()) * self._args.wheel_ratio / 2 painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.TextAntialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) # color wheel. hue. wheel_box = get_outer_box(self._center, self._radius) painter.setPen( QPen(QColor(*self._args.wheel_ed_color), self._args.wheel_ed_wid)) cgrad = QConicalGradient(*self._center, 0) cgrad.setColorAt(0.00000, QColor(255, 0, 0)) cgrad.setColorAt(0.16667, QColor(255, 0, 255)) cgrad.setColorAt(0.33333, QColor(0, 0, 255)) cgrad.setColorAt(0.50000, QColor(0, 255, 255)) cgrad.setColorAt(0.66667, QColor(0, 255, 0)) cgrad.setColorAt(0.83333, QColor(255, 255, 0)) cgrad.setColorAt(1.00000, QColor(255, 0, 0)) painter.setBrush(cgrad) painter.drawEllipse(*wheel_box) # color wheel. saturation. rgrad = QRadialGradient(*self._center, self._radius) rgrad.setColorAt(0.0, Qt.white) rgrad.setColorAt(1.0, Qt.transparent) painter.setBrush(rgrad) painter.drawEllipse(*wheel_box) # bars. bar_hsv = self._args.sys_color_set[self._args.sys_activated_idx].hsv bar_v = bar_hsv[2] bar_rgb = Color.hsv2rgb((bar_hsv[0], bar_hsv[1], 1.0)) self._v_tag_radius = min(self.width(), self.height()) * self._args.v_tag_radius / 2 re_wid = self.width() * ( 1 - self._args.wheel_ratio) / 2 * self._args.volum_ratio re_wid = self._v_tag_radius * 3 if self._v_tag_radius * 3 < re_wid else re_wid bar_1_center = ((self.width() - self._radius * 2) / 4, self.height() / 2) self._bar_1_box = (bar_1_center[0] - re_wid / 2, bar_1_center[1] - self.height() * self._args.volum_ratio / 2, re_wid, self.height() * self._args.volum_ratio) painter.setPen( QPen(QColor(*self._args.wheel_ed_color), self._args.wheel_ed_wid)) lgrad = QLinearGradient(self._bar_1_box[0], self._bar_1_box[1], self._bar_1_box[0], self._bar_1_box[3]) lgrad.setColorAt(1.0, Qt.white) lgrad.setColorAt(0.0, Qt.black) painter.setBrush(lgrad) painter.drawRect(*self._bar_1_box) self._cir_1_center = (bar_1_center[0], self._bar_1_box[1] + self._bar_1_box[3] * bar_v) cir_1_box = get_outer_box(self._cir_1_center, self._v_tag_radius) painter.setPen( QPen(QColor(*self._args.positive_color), self._args.positive_wid)) painter.setBrush(QBrush(Qt.NoBrush)) painter.drawEllipse(*cir_1_box) bar_2_center = (self.width() - (self.width() - self._radius * 2) / 4, self.height() / 2) self._bar_2_box = (bar_2_center[0] - re_wid / 2, bar_2_center[1] - self.height() * self._args.volum_ratio / 2, re_wid, self.height() * self._args.volum_ratio) painter.setPen( QPen(QColor(*self._args.wheel_ed_color), self._args.wheel_ed_wid)) lgrad = QLinearGradient(self._bar_2_box[0], self._bar_2_box[1], self._bar_2_box[0], self._bar_2_box[3]) lgrad.setColorAt(1.0, QColor(*bar_rgb)) lgrad.setColorAt(0.0, Qt.black) painter.setBrush(lgrad) painter.drawRect(*self._bar_2_box) self._cir_2_center = (bar_2_center[0], self._bar_2_box[1] + self._bar_2_box[3] * bar_v) cir_2_box = get_outer_box(self._cir_2_center, self._v_tag_radius) painter.setPen( QPen(QColor(*self._args.positive_color), self._args.positive_wid)) painter.setBrush(QBrush(Qt.NoBrush)) painter.drawEllipse(*cir_2_box) # color set tags. self._tag_center = [None] * 5 self._tag_radius = min(self.width(), self.height()) * self._args.s_tag_radius / 2 self._idx_seq = list(range(5)) self._idx_seq = self._idx_seq[ self._args.sys_activated_idx + 1:] + self._idx_seq[:self._args.sys_activated_idx + 1] # lines. for idx in self._idx_seq: color_center = np.array([ self._args.sys_color_set[idx].s * self._radius, 0 ]) + self._center color_center = rotate_point_center(self._center, color_center, self._args.sys_color_set[idx].h) self._tag_center[idx] = color_center if idx == self._args.sys_activated_idx: painter.setPen( QPen(QColor(*self._args.positive_color), self._args.positive_wid)) else: painter.setPen( QPen(QColor(*self._args.negative_color), self._args.negative_wid)) painter.drawLine(QPoint(*self._center), QPoint(*color_center)) # dot. dot_box = get_outer_box(self._center, self._args.positive_wid) painter.setPen(QPen(Qt.NoPen)) painter.setBrush(QBrush(QColor(*self._args.positive_color))) painter.drawEllipse(*dot_box) # circles. for idx in self._idx_seq: color_box = get_outer_box(self._tag_center[idx], self._tag_radius) if idx == self._args.sys_activated_idx: painter.setPen( QPen(QColor(*self._args.positive_color), self._args.positive_wid)) else: painter.setPen( QPen(QColor(*self._args.negative_color), self._args.negative_wid)) painter.setBrush(QColor(*self._args.sys_color_set[idx].rgb)) painter.drawEllipse(*color_box) painter.end()
class PolygonWidget(QWidget): """PolygonWidget(QWidget) Provides a custom widget to display a polygon with properties and slots that can be used to customize its appearance. """ def __init__(self, parent=None): super(PolygonWidget, self).__init__(parent) self._sides = 5 self._innerRadius = 20 self._outerRadius = 50 self._angle = 0 self.createPath() self._innerColor = QColor(255, 255, 128) self._outerColor = QColor(255, 0, 128) self.createGradient() def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setBrush(QBrush(QColor(192, 192, 255))) painter.drawRect(event.rect()) painter.translate(self.width()/2.0, self.height()/2.0) painter.rotate(self._angle) painter.setBrush(QBrush(self.gradient)) painter.drawPath(self.path) painter.end() def sizeHint(self): return QSize(2*self._outerRadius + 20, 2*self._outerRadius + 20) def createPath(self): self.path = QPainterPath() angle = 2*math.pi/self._sides self.path.moveTo(self._outerRadius, 0) for step in range(1, self._sides + 1): self.path.lineTo( self._innerRadius * math.cos((step - 0.5) * angle), self._innerRadius * math.sin((step - 0.5) * angle) ) self.path.lineTo( self._outerRadius * math.cos(step * angle), self._outerRadius * math.sin(step * angle) ) self.path.closeSubpath() def createGradient(self): center = QPointF(0, 0) self.gradient = QRadialGradient(center, self._outerRadius, center) self.gradient.setColorAt(0.5, QColor(self._innerColor)) self.gradient.setColorAt(1.0, QColor(self._outerColor)) # The angle property is implemented using the getAngle() and setAngle() # methods. def getAngle(self): return self._angle # The setAngle() setter method is also a slot. @pyqtSlot(int) def setAngle(self, angle): self._angle = min(max(0, angle), 360) self.update() angle = pyqtProperty(int, getAngle, setAngle) # The innerRadius property is implemented using the getInnerRadius() and # setInnerRadius() methods. def getInnerRadius(self): return self._innerRadius # The setInnerRadius() setter method is also a slot. @pyqtSlot(int) def setInnerRadius(self, radius): self._innerRadius = radius self.createPath() self.createGradient() self.update() innerRadius = pyqtProperty(int, getInnerRadius, setInnerRadius) # The outerRadius property is implemented using the getOuterRadius() and # setOuterRadius() methods. def getOuterRadius(self): return self._outerRadius # The setOuterRadius() setter method is also a slot. @pyqtSlot(int) def setOuterRadius(self, radius): self._outerRadius = radius self.createPath() self.createGradient() self.update() outerRadius = pyqtProperty(int, getOuterRadius, setOuterRadius) # The numberOfSides property is implemented using the getNumberOfSides() # and setNumberOfSides() methods. def getNumberOfSides(self): return self._sides # The setNumberOfSides() setter method is also a slot. @pyqtSlot(int) def setNumberOfSides(self, sides): self._sides = max(3, sides) self.createPath() self.update() numberOfSides = pyqtProperty(int, getNumberOfSides, setNumberOfSides) # The innerColor property is implemented using the getInnerColor() and # setInnerColor() methods. def getInnerColor(self): return self._innerColor def setInnerColor(self, color): self._innerColor = max(3, color) self.createGradient() self.update() innerColor = pyqtProperty(QColor, getInnerColor, setInnerColor) # The outerColor property is implemented using the getOuterColor() and # setOuterColor() methods. def getOuterColor(self): return self._outerColor def setOuterColor(self, color): self._outerColor = color self.createGradient() self.update() outerColor = pyqtProperty(QColor, getOuterColor, setOuterColor)
def showWedge(self, angle, color, extended=False, rev_gradient=False, outline_only=False): """Summary Args: angle (TYPE): Description color (TYPE): Description extended (bool, optional): Description rev_gradient (bool, optional): Description outline_only (bool, optional): Description """ # Hack to keep wedge in front # self.setRotation(self.pre_xover_item_group.rotation()) self._last_params = (angle, color, extended, rev_gradient, outline_only) radius = self._radius span = self.pre_xover_item_group.partCrossoverSpanAngle() / 2 radius_adjusted = radius + (_WEDGE_RECT_GAIN / 2) tip = QPointF(radius_adjusted, radius_adjusted) EXT = 1.35 if extended else 1.0 # print("wtf", tip, pos) base_p2 = QPointF(1, 1) line0 = QLineF(tip, QPointF(base_p2)) line1 = QLineF(tip, QPointF(base_p2)) line2 = QLineF(tip, QPointF(base_p2)) quad_scale = 1 + (.22*(span - 5) / 55) # lo+(hi-lo)*(val-min)/(max-min) line0.setLength(radius_adjusted * EXT*quad_scale) # for quadTo control point line1.setLength(radius_adjusted * EXT) line2.setLength(radius_adjusted * EXT) line0.setAngle(angle) line1.setAngle(angle - span) line2.setAngle(angle + span) path = QPainterPath() if outline_only: self.setPen(getPenObj(color, 0.5, alpha=128, capstyle=Qt.RoundCap)) path.moveTo(line1.p2()) path.quadTo(line0.p2(), line2.p2()) else: gradient = QRadialGradient(tip, radius_adjusted * EXT) color1 = getColorObj(color, alpha=80) color2 = getColorObj(color, alpha=0) if rev_gradient: color1, color2 = color2, color1 if extended: gradient.setColorAt(0, color1) gradient.setColorAt(radius_adjusted / (radius_adjusted * EXT), color1) gradient.setColorAt(radius_adjusted / (radius_adjusted * EXT) + 0.01, color2) gradient.setColorAt(1, color2) else: gradient.setColorAt(0, getColorObj(color, alpha=50)) brush = QBrush(gradient) self.setBrush(brush) path.moveTo(line1.p1()) path.lineTo(line1.p2()) path.quadTo(line0.p2(), line2.p2()) path.lineTo(line2.p1()) self.setPath(path) self.show()
def createGradient(self): center = QPointF(0, 0) self.gradient = QRadialGradient(center, self._outerRadius, center) self.gradient.setColorAt(0.5, QColor(self._innerColor)) self.gradient.setColorAt(1.0, QColor(self._outerColor))
def drawBackground(self): side = min(self.width(), self.height()) ''' Keep side size an even number by trunkating odd pixel ''' side &= ~0x01 gradient = QRadialGradient() painter = QPainter(self) pen = QPen(Qt.black) pen.setWidth(1) ''' Initialize painter ''' painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) painter.scale(side / 256.0, side / 256.0) painter.setPen(pen) ''' Draw external circle ''' gradient = QRadialGradient(QPointF(-128, -128), 384, QPointF(-128, -128)) gradient.setColorAt(0, QColor(224, 224, 224)) gradient.setColorAt(1, QColor(28, 28, 28)) painter.setPen(pen) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPoint(0, 0), 125, 125) ''' Draw inner circle ''' gradient = QRadialGradient(QPointF(128, 128), 384, QPointF(128, 128)) gradient.setColorAt(0, QColor(224, 224, 224)) gradient.setColorAt(1, QColor(28, 28, 28)) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPoint(0, 0), 118, 118) ''' Draw inner shield ''' gradient = QRadialGradient(QPointF(-128, -128), 384, QPointF(-128, -128)) gradient.setColorAt(0, QColor(255, 255, 255)) gradient.setColorAt(1, QColor(224, 224, 224)) painter.setBrush(gradient) painter.setPen(Qt.NoPen) painter.drawEllipse(QPoint(0, 0), 115, 115) painter.setPen(pen) painter.setBrush(Qt.black) painter.drawPoint(0, 0) line = 10 ''' Draw scale majorTicks using coords rotation ''' painter.save() painter.setBrush(Qt.black) painter.rotate(self.start_angle) # ''' initial angle (first tick) ''' painter.setBrush(QBrush(Qt.black)) t_rot = self.stop_angle / (self.minorTicks * (self.majorTicks - 1) + self.majorTicks - 1) for i in range( int((self.minorTicks) * (self.majorTicks - 1) + self.majorTicks)): if self.minorTicks: if (self.minorTicks + 1) == 0: painter.drawLine(QPoint(105, 0), QPoint(105 - line, 0)) else: painter.drawLine(QPoint(105, 0), QPoint(105 - line / 3, 0)) else: painter.drawLine(QPoint(105, 0), QPoint(105 - line, 0)) painter.rotate(t_rot) painter.restore() ''' Draw scale numbers using vector rotation ''' ''' x' = xcos(a)-ysin(a) ''' ''' y' = xsin(a)-ycos(a) ''' painter.save() rotation = (self.start_angle / 360) * 2 * pi # ''' Initial rotation ''' painter.setFont(self.digitFont) for i in range(int(self.majorTicks)): point = QPointF((70 * cos(rotation)), 70 * sin(rotation)) value = self.scaleFormat.format(self.wmin + i * self.step) size = painter.fontMetrics().size(Qt.TextSingleLine, value) point.setX(point.x() - size.width() / 2) # += int() point.setY(point.y() + size.height() / 4) painter.drawText(point, value) rotation += self.rot_rad painter.restore() labela = self.labelFormat.format(self.value) painter.setFont(self.labelFont) point = QPointF() size = painter.fontMetrics().size(Qt.TextSingleLine, labela) point.setX(point.x() - size.width() / 2) point.setY(point.y() + size.height() / 4 + self.labelOffset) painter.drawText(point, labela) if len(self.label) > 0: # Draw meter label painter.setFont(self.labelFont) point = QPointF() size = painter.fontMetrics().size(Qt.TextSingleLine, self.label) point.setX(point.x() - size.width() / 2) point.setY(point.y() + size.height() / 4 + self.labelOffset + 24) painter.drawText(point, self.label)
class PolygonWidget(QWidget): """PolygonWidget(QWidget) Provides a custom widget to display a polygon with properties and slots that can be used to customize its appearance. """ def __init__(self, parent=None): super(PolygonWidget, self).__init__(parent) self._sides = 5 self._innerRadius = 20 self._outerRadius = 50 self._angle = 0 self.createPath() self._innerColor = QColor(255, 255, 128) self._outerColor = QColor(255, 0, 128) self.createGradient() def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setBrush(QBrush(QColor(192, 192, 255))) painter.drawRect(event.rect()) painter.translate(self.width() / 2.0, self.height() / 2.0) painter.rotate(self._angle) painter.setBrush(QBrush(self.gradient)) painter.drawPath(self.path) painter.end() def sizeHint(self): return QSize(2 * self._outerRadius + 20, 2 * self._outerRadius + 20) def createPath(self): self.path = QPainterPath() angle = 2 * math.pi / self._sides self.path.moveTo(self._outerRadius, 0) for step in range(1, self._sides + 1): self.path.lineTo( self._innerRadius * math.cos((step - 0.5) * angle), self._innerRadius * math.sin((step - 0.5) * angle)) self.path.lineTo(self._outerRadius * math.cos(step * angle), self._outerRadius * math.sin(step * angle)) self.path.closeSubpath() def createGradient(self): center = QPointF(0, 0) self.gradient = QRadialGradient(center, self._outerRadius, center) self.gradient.setColorAt(0.5, QColor(self._innerColor)) self.gradient.setColorAt(1.0, QColor(self._outerColor)) # The angle property is implemented using the getAngle() and setAngle() # methods. def getAngle(self): return self._angle # The setAngle() setter method is also a slot. @pyqtSlot(int) def setAngle(self, angle): self._angle = min(max(0, angle), 360) self.update() angle = pyqtProperty(int, getAngle, setAngle) # The innerRadius property is implemented using the getInnerRadius() and # setInnerRadius() methods. def getInnerRadius(self): return self._innerRadius # The setInnerRadius() setter method is also a slot. @pyqtSlot(int) def setInnerRadius(self, radius): self._innerRadius = radius self.createPath() self.createGradient() self.update() innerRadius = pyqtProperty(int, getInnerRadius, setInnerRadius) # The outerRadius property is implemented using the getOuterRadius() and # setOuterRadius() methods. def getOuterRadius(self): return self._outerRadius # The setOuterRadius() setter method is also a slot. @pyqtSlot(int) def setOuterRadius(self, radius): self._outerRadius = radius self.createPath() self.createGradient() self.update() outerRadius = pyqtProperty(int, getOuterRadius, setOuterRadius) # The numberOfSides property is implemented using the getNumberOfSides() # and setNumberOfSides() methods. def getNumberOfSides(self): return self._sides # The setNumberOfSides() setter method is also a slot. @pyqtSlot(int) def setNumberOfSides(self, sides): self._sides = max(3, sides) self.createPath() self.update() numberOfSides = pyqtProperty(int, getNumberOfSides, setNumberOfSides) # The innerColor property is implemented using the getInnerColor() and # setInnerColor() methods. def getInnerColor(self): return self._innerColor def setInnerColor(self, color): self._innerColor = color self.createGradient() self.update() innerColor = pyqtProperty(QColor, getInnerColor, setInnerColor) # The outerColor property is implemented using the getOuterColor() and # setOuterColor() methods. def getOuterColor(self): return self._outerColor def setOuterColor(self, color): self._outerColor = color self.createGradient() self.update() outerColor = pyqtProperty(QColor, getOuterColor, setOuterColor)
def paintEvent(self, QPaintEvent): realSize = min(self.width(), self.height()) painter = QPainter(self) pen = QPen(Qt.black) pen.setWidth(1) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) painter.scale(realSize / self.scaledSize, realSize / self.scaledSize) gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500)) gradient.setColorAt(0, QColor(224, 224, 224)) gradient.setColorAt(1, QColor(28, 28, 28)) painter.setPen(pen) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPointF(0, 0), 500, 500) gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500)) gradient.setColorAt(0, QColor(224, 224, 224)) gradient.setColorAt(1, QColor(28, 28, 28)) painter.setPen(pen) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPointF(0, 0), 450, 450) painter.setPen(pen) if self.isChecked(): gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500)) gradient.setColorAt(0, self.on_color_1) gradient.setColorAt(1, self.on_color_2) else: gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500)) gradient.setColorAt(0, self.off_color_1) gradient.setColorAt(1, self.off_color_2) painter.setBrush(gradient) painter.drawEllipse(QPointF(0, 0), 400, 400)
def draw_piecess(): """绘制棋子""" winstep = self.g.getWinnerStep() #绘制黑棋子 qp.setPen(QPen(QColor(0, 0, 0), 1, Qt.SolidLine)) #qp.setBrush(QColor(0,0,0)) for x in range(15): for y in range(15): if self.g.g_map[x][y] == 1: if winstep != None: if (x, y) in winstep: if self.display == True: radial = QRadialGradient( 40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) radial.setColorAt(0, QColor(96, 96, 96)) radial.setColorAt(1, QColor(0, 0, 0)) qp.drawEllipse( QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) else: radial = QRadialGradient( 40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) radial.setColorAt(0, QColor(96, 96, 96)) radial.setColorAt(1, QColor(0, 0, 0)) qp.drawEllipse( QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) else: radial = QRadialGradient(40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) radial.setColorAt(0, QColor(96, 96, 96)) radial.setColorAt(1, QColor(0, 0, 0)) qp.drawEllipse(QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) #绘制白棋子 qp.setPen(QPen(QColor(160, 160, 160), 1, Qt.SolidLine)) qp.setBrush(QColor(255, 255, 255)) for x in range(15): for y in range(15): if self.g.g_map[x][y] == 2: if winstep != None: if (x, y) in winstep: if self.display == True: radial = QRadialGradient( 40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) radial.setColorAt(0, QColor(255, 255, 255)) radial.setColorAt(1, QColor(255, 255, 255)) qp.drawEllipse( QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) else: radial = QRadialGradient( 40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) radial.setColorAt(0, QColor(255, 255, 255)) radial.setColorAt(1, QColor(255, 255, 255)) qp.drawEllipse( QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15) else: radial = QRadialGradient(40 * (x + 1), 40 * (y + 1), 15, 40 * x + 35, 40 * y + 35) radial.setColorAt(0, QColor(255, 255, 255)) radial.setColorAt(1, QColor(255, 255, 255)) qp.drawEllipse(QPoint(40 * (x + 1), 40 * (y + 1)), 15, 15)
def rebuildDataBrushIfNeeded(self): if not self.m_rebuildBrush or not self.m_gradientData or self.m_barStyle == self.BarStyle.LINE: return self.m_rebuildBrush = False p = self.palette() if self.m_barStyle == self.BarStyle.EXPAND: dataBrush = QRadialGradient(0.5, 0.5, 0.5, 0.5, 0.5) dataBrush.setCoordinateMode(QGradient.StretchToDeviceMode) for i in range(0, len(self.m_gradientData)): dataBrush.setColorAt(self.m_gradientData[i][0], self.m_gradientData[i][1]) p.setBrush(QPalette.Highlight, dataBrush) else: dataBrush = QConicalGradient(QPointF(0.5, 0.5), self.m_nullPosition) dataBrush.setCoordinateMode(QGradient.StretchToDeviceMode) for i in range(0, len(self.m_gradientData)): dataBrush.setColorAt(1 - self.m_gradientData[i][0], self.m_gradientData[i][1]) p.setBrush(QPalette.Highlight, dataBrush) self.setPalette(p)
def drawBackground(self, painter, widget): painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.NoPen) p1 = QPointF(80, 80) g = QRadialGradient(p1 * 0.2, 80 * 1.1) g.setColorAt(0.0, widget.palette().light().color()) g.setColorAt(1.0, widget.palette().dark().color()) painter.setBrush(g) painter.drawEllipse(0, 0, 80, 80) p2 = QPointF(40, 40) g = QRadialGradient(p2, 70 * 1.3) g.setColorAt(0.0, widget.palette().midlight().color()) g.setColorAt(1.0, widget.palette().dark().color()) painter.setBrush(g) painter.drawEllipse(7.5, 7.5, 65, 65)
def paint(self, painter, option, widget): painter.setPen(Qt.NoPen) painter.setBrush(Qt.darkGray) painter.drawEllipse(-7, -7, 20, 20) gradient = QRadialGradient(-3, -3, 10) if option.state & QStyle.State_Sunken: gradient.setCenter(3, 3) gradient.setFocalPoint(3, 3) gradient.setColorAt(1, QColor(Qt.red).lighter(120)) gradient.setColorAt(0, QColor(Qt.darkRed).lighter(120)) else: gradient.setColorAt(0, Qt.cyan) gradient.setColorAt(1, Qt.darkCyan) painter.setBrush(QBrush(gradient)) painter.setPen(QPen(Qt.black, 0)) painter.drawEllipse(-10, -10, 20, 20)
def showWedge(self, angle, color, extended=False, rev_gradient=False, outline_only=False): """Summary Args: angle (TYPE): Description color (TYPE): Description extended (bool, optional): Description rev_gradient (bool, optional): Description outline_only (bool, optional): Description """ # Hack to keep wedge in front # self.setRotation(self.pre_xover_item_group.rotation()) self._last_params = (angle, color, extended, rev_gradient, outline_only) radius = self._radius span = self.pre_xover_item_group.partCrossoverSpanAngle() / 2 radius_adjusted = radius + (_WEDGE_RECT_GAIN / 2) tip = QPointF(radius_adjusted, radius_adjusted) EXT = 1.35 if extended else 1.0 # print("wtf", tip, pos) base_p2 = QPointF(1, 1) line0 = QLineF(tip, QPointF(base_p2)) line1 = QLineF(tip, QPointF(base_p2)) line2 = QLineF(tip, QPointF(base_p2)) quad_scale = 1 + (.22 * (span - 5) / 55) # lo+(hi-lo)*(val-min)/(max-min) line0.setLength(radius_adjusted * EXT * quad_scale) # for quadTo control point line1.setLength(radius_adjusted * EXT) line2.setLength(radius_adjusted * EXT) line0.setAngle(angle) line1.setAngle(angle - span) line2.setAngle(angle + span) path = QPainterPath() if outline_only: self.setPen(getPenObj(color, 0.5, alpha=128, capstyle=Qt.RoundCap)) path.moveTo(line1.p2()) path.quadTo(line0.p2(), line2.p2()) else: gradient = QRadialGradient(tip, radius_adjusted * EXT) color1 = getColorObj(color, alpha=80) color2 = getColorObj(color, alpha=0) if rev_gradient: color1, color2 = color2, color1 if extended: gradient.setColorAt(0, color1) gradient.setColorAt(radius_adjusted / (radius_adjusted * EXT), color1) gradient.setColorAt( radius_adjusted / (radius_adjusted * EXT) + 0.01, color2) gradient.setColorAt(1, color2) else: gradient.setColorAt(0, getColorObj(color, alpha=50)) brush = QBrush(gradient) self.setBrush(brush) path.moveTo(line1.p1()) path.lineTo(line1.p2()) path.quadTo(line0.p2(), line2.p2()) path.lineTo(line2.p1()) self.setPath(path) self.show()
def draw_background(self, painter): self.initCoordinateSystem(painter) pen = QPen(QColor(0, 0, 0)) pen.setWidth(4) painter.setPen(pen) back1 = QRadialGradient(QPointF(0.0, 0.0), 180.0, QPointF(-35.0, 145.0)) back1.setColorAt(0.0, QColor(250, 250, 250)) back1.setColorAt(1.0, QColor(20, 20, 20)) back2 = QRadialGradient(QPointF(0.0, 0.0), 225.0, QPointF(76.5, 135.0)) back2.setColorAt(0.0, QColor(10, 10, 10)) back2.setColorAt(1.0, QColor(250, 250, 250)) painter.setBrush(QBrush(back1)) painter.drawEllipse(-162, -162, 324, 324) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(back2)) painter.drawEllipse(-152, -152, 304, 304) shield = QRadialGradient(QPointF(0, 0), 182, QPointF(-12.0, -15.0)) shield.setColorAt(0.0, Qt.white) shield.setColorAt(0.5, QColor(240, 240, 240)) shield.setColorAt(1.0, QColor(215, 215, 215)) painter.setBrush(QBrush(shield)) painter.setPen(pen) painter.drawEllipse(-142, -142, 284, 284) painter.setPen(Qt.NoPen) painter.setBrush(QColor(0, 200, 0)) angle = int( (3840 * (self.mPointer.nominal() - self.mPointer.minimal())) / (self.mPointer.maximal() - self.mPointer.minimal())) painter.drawPie(QRect(-141, -141, 282, 282), -480, 3840 - angle % 5760) painter.setBrush(QBrush(Qt.red)) angle = int( (3840 * (self.mPointer.critical() - self.mPointer.minimal())) / (self.mPointer.maximal() - self.mPointer.minimal())) painter.drawPie(QRect(-141, -141, 282, 282), -480, 3840 - angle % 5760) painter.setBrush(QBrush(shield)) painter.drawEllipse(-129, -129, 258, 258) painter.rotate(60) painter.save() painter.setBrush(QBrush(Qt.black)) line_length = 10 scaleTriangle = [QPoint(-6, 141), QPoint(6, 141), QPoint(0, 129)] for i in range(33): painter.setPen(pen) if i % 4: painter.drawLine(0, 140, 0, 140 - line_length) else: painter.setPen(Qt.NoPen) painter.drawConvexPolygon(QPolygon(scaleTriangle)) painter.rotate(7.5) pen.setWidth(3) if i % 2: line_length = 10 else: line_length = 5 painter.restore() painter.setPen(Qt.black) painter.rotate(-60.0) painter.setFont(self.mDigitFont) for i in range(9): v = self.mPointer.minimal() + i * (self.mPointer.maximal() - self.mPointer.minimal()) / 8.0 if abs(v) < 0.000001: v = 0.0 val = "{}".format(v) Size = painter.fontMetrics().size(Qt.TextSingleLine, val) painter.save() painter.translate( self.mDigitOffset * math.cos((5 + i) * math.pi / 6.0), self.mDigitOffset * math.sin((5 + i) * math.pi / 6.0)) painter.drawText(QPointF(Size.width() / -2.0, Size.height() / 4.0), val) painter.restore()
def __init__(self, metadata, x_y): """ Create node in the graph scene :param dict metadata: Node metadata :param x_y: Position of the node """ # unpack tuple x, y = x_y super(Node, self).__init__() self.metadata = metadata self.id = metadata['id'] self.status_wallet = self.metadata['status'] & NODE_STATUS_HIGHLIGHTED self.status_member = not self.metadata['status'] & NODE_STATUS_OUT self.text = self.metadata['text'] self.setToolTip(self.metadata['tooltip']) self.arcs = [] self.menu = None self.action_sign = None self.action_transaction = None self.action_contact = None self.action_show_member = None # color around ellipse outline_color = QColor('grey') outline_style = Qt.SolidLine outline_width = 1 if self.status_wallet: outline_color = QColor('black') outline_width = 2 if not self.status_member: outline_color = QColor('red') outline_style = Qt.SolidLine self.setPen(QPen(outline_color, outline_width, outline_style)) # text inside ellipse self.text_item = QGraphicsSimpleTextItem(self) self.text_item.setText(self.text) text_color = QColor('grey') if self.status_wallet == NODE_STATUS_HIGHLIGHTED: text_color = QColor('black') self.text_item.setBrush(QBrush(text_color)) # center ellipse around text self.setRect(0, 0, self.text_item.boundingRect().width() * 2, self.text_item.boundingRect().height() * 2) # set anchor to the center self.setTransform(QTransform().translate( -self.boundingRect().width() / 2.0, -self.boundingRect().height() / 2.0)) self.setPos(x, y) # center text in ellipse self.text_item.setPos(self.boundingRect().width() / 4.0, self.boundingRect().height() / 4.0) # create gradient inside the ellipse gradient = QRadialGradient( QPointF(0, self.boundingRect().height() / 4), self.boundingRect().width()) gradient.setColorAt(0, QColor('white')) gradient.setColorAt(1, QColor('darkgrey')) self.setBrush(QBrush(gradient)) # cursor change on hover self.setAcceptHoverEvents(True) self.setZValue(1)
def paint(self, painter, option, widget): start_draw = -self._draw_size end_draw = self._draw_size painter.setPen(Qt.NoPen) painter.setBrush(Qt.darkGray) painter.drawEllipse(start_draw + 3, start_draw + 3, end_draw, end_draw) gradient = QRadialGradient(start_draw * 0.6, start_draw * 0.6, end_draw / 2) if option.state & QStyle.State_Sunken: gradient.setCenter(3, 3) gradient.setFocalPoint(3, 3) if self.state == State.INITIAL: gradient.setColorAt(1, QColor(Qt.darkMagenta)) #.light(120)) gradient.setColorAt(0, QColor(Qt.magenta)) #.light(120)) elif self.state == State.TRANSITION: gradient.setColorAt(1, QColor(Qt.darkCyan)) # .light(120)) gradient.setColorAt(0, QColor(Qt.cyan)) # .light(120)) elif self.state == State.FINAL: gradient.setColorAt(1, QColor(Qt.darkGreen)) # .light(120)) gradient.setColorAt(0, QColor(Qt.green)) # .light(120)) elif self.state == State.INITIAL_FINAL: gradient.setColorAt(1, QColor(Qt.darkRed)) # .light(120)) gradient.setColorAt(0, QColor(Qt.red)) # .light(120)) else: if self.state == State.INITIAL: gradient.setColorAt(1, QColor(Qt.darkMagenta)) #.light(120)) gradient.setColorAt(0, QColor(Qt.magenta)) #.light(120)) elif self.state == State.TRANSITION: gradient.setColorAt(1, QColor(Qt.darkCyan)) # .light(120)) gradient.setColorAt(0, QColor(Qt.cyan)) # .light(120)) elif self.state == State.FINAL: gradient.setColorAt(1, QColor(Qt.darkGreen)) # .light(120)) gradient.setColorAt(0, QColor(Qt.green)) # .light(120)) elif self.state == State.INITIAL_FINAL: gradient.setColorAt(1, QColor(Qt.darkRed)) # .light(120)) gradient.setColorAt(0, QColor(Qt.red)) # .light(120)) painter.setBrush(QBrush(gradient)) painter.setPen(QPen(Qt.black, 0)) painter.drawEllipse(start_draw, start_draw, end_draw, end_draw)
def paintEvent(self, evt): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.setPen(QPen(Qt.black, 1)) realSize = min(self.width(), self.height()) # 窗口的短边 painter.translate(self.width() / 2.0, self.height() / 2.0) # 原点平移到窗口中心 painter.scale(realSize / self.scaledSize, realSize / self.scaledSize) # 缩放,窗口的短边值映射为self.scaledSize gradient = QRadialGradient(QPointF(0, 0), self.scaledSize / 2.0, QPointF(0, 0)) # 辐射渐变 # 画边框外圈和内圈 for color, radius in [(self.colorBorderOut, self.radiusBorderOut), # 边框外圈 (self.colorBorderIn, self.radiusBorderIn)]: # 边框内圈 gradient.setColorAt(1, color) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPointF(0, 0), radius, radius) # 画内圆 if self.state == 'off': gradient.setColorAt(0, self.colorOffBegin) gradient.setColorAt(1, self.colorOffEnd) else: gradient.setColorAt(0, self.colorOnBegin) gradient.setColorAt(1, self.colorOnEnd) painter.setBrush(QBrush(gradient)) painter.drawEllipse(QPointF(0, 0), self.radiusCircle, self.radiusCircle)
def paintEvent(self, event): self.gt = GetGCPGeoTransform() painter = QPainter(self) if (self.surface.isActive()): videoRect = self.surface.videoRect() if not videoRect.contains(event.rect()): region = event.region() region.subtracted(QRegion(videoRect)) brush = self.palette().window() for rect in region.rects(): painter.fillRect(rect, brush) try: self.surface.paint(painter) except Exception: None else: painter.fillRect(event.rect(), self.palette().window()) try: SetImageSize(self.surface.currentFrame.width(), self.surface.currentFrame.height()) except: None # Magnifier Glass if self.zoomed and magnifier: dim = min(self.width(), self.height()) magnifierSize = min(MAX_MAGNIFIER, dim * 2 / 3) radius = magnifierSize / 2 ring = radius - 15 box = QSize(magnifierSize, magnifierSize) # reupdate our mask if self.maskPixmap.size() != box: self.maskPixmap = QPixmap(box) self.maskPixmap.fill(Qt.transparent) g = QRadialGradient() g.setCenter(radius, radius) g.setFocalPoint(radius, radius) g.setRadius(radius) g.setColorAt(1.0, QColor(64, 64, 64, 0)) g.setColorAt(0.5, QColor(0, 0, 0, 255)) mask = QPainter(self.maskPixmap) mask.setRenderHint(QPainter.Antialiasing) mask.setCompositionMode(QPainter.CompositionMode_Source) mask.setBrush(g) mask.setPen(Qt.NoPen) mask.drawRect(self.maskPixmap.rect()) mask.setBrush(QColor(Qt.transparent)) mask.drawEllipse(g.center(), ring, ring) mask.end() center = self.dragPos - QPoint(0, radius) center += QPoint(0, radius / 2) corner = center - QPoint(radius, radius) xy = center * 2 - QPoint(radius, radius) # only set the dimension to the magnified portion if self.zoomPixmap.size() != box: self.zoomPixmap = QPixmap(box) self.zoomPixmap.fill(Qt.lightGray) if True: painter = QPainter(self.zoomPixmap) painter.translate(-xy) self.largePixmap = QPixmap.fromImage(self.surface.image) painter.drawPixmap(self.offset, self.largePixmap) painter.end() clipPath = QPainterPath() clipPath.addEllipse(QPointF(center), ring, ring) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setClipPath(clipPath) painter.drawPixmap(corner, self.zoomPixmap) painter.drawPixmap(corner, self.maskPixmap) painter.setPen(Qt.gray) painter.drawPath(clipPath) return
def paintEvent(self, _event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) width = self.width() height = self.height() if self.dynamic_resize: knob_radius = self.dynamic_knob_radius else: knob_radius = self.knob_radius # ensure that the center point is in the middle of a pixel to ensure # that exact vertial and horizantal ticks are drawn exactly 1px wide x = math.floor(width / 2.0) + 0.5 y = math.floor(height / 2.0) + 0.5 if DEBUG: painter.fillRect(0, 0, width, height, Qt.yellow) painter.translate(x, y) if self.knob_style == KnobWidget.STYLE_NEEDLE: r = min(x, y) - 1 painter.setPen(Qt.white) painter.setBrush(Qt.white) painter.drawEllipse(QPoint(0, 0), r, r) angle = self.value_factor * self.total_angle - (self.total_angle / 2.0) # draw base knob or needle spike if self.knob_style == KnobWidget.STYLE_ROUND: painter.setPen(self.border_color) if self.pressed: gradient = QRadialGradient(0, 0, knob_radius) gradient.setColorAt(0, self.base_color_pressed) gradient.setColorAt(0.85, self.base_color) gradient.setColorAt(1, self.base_color) painter.setBrush(gradient) else: painter.setBrush(self.base_color) painter.drawEllipse(QPoint(0, 0), knob_radius, knob_radius) elif self.knob_style == KnobWidget.STYLE_NEEDLE: painter.save() painter.rotate(angle) painter.setPen(self.needle_color) painter.setBrush(self.needle_color) needle = QPolygonF() needle.append(QPointF(self.needle_base_radius * 0.6, 0)) needle.append(QPointF(0, -knob_radius)) needle.append(QPointF(-self.needle_base_radius * 0.6, 0)) painter.drawPolygon(needle) painter.restore() # draw knob mark or needle base if self.knob_style == KnobWidget.STYLE_ROUND: painter.save() painter.rotate(angle) painter.setPen(QPen(self.mark_color, 2)) painter.drawLine(0, -knob_radius * 0.4, 0, -knob_radius * 0.8) painter.restore() elif self.knob_style == KnobWidget.STYLE_NEEDLE: painter.setPen(self.border_color) painter.setBrush(self.base_color) painter.drawEllipse(QPoint(0, 0), self.needle_base_radius, self.needle_base_radius) if self.scale_visible: painter.setPen(Qt.black) # draw scale arc if self.scale_arc_visible: painter.drawArc(-knob_radius - self.knob_to_scale, -knob_radius - self.knob_to_scale, knob_radius * 2 + self.knob_to_scale * 2, knob_radius * 2 + self.knob_to_scale * 2, (90 + self.total_angle / 2) * 16, -self.total_angle * 16) # draw scale ticks def value_to_angle(value): return (float(value - self.minimum_value) / self.value_range ) * self.total_angle - (self.total_angle / 2.0) value = self.minimum_value while value <= self.maximum_value: angle = value_to_angle(value) painter.save() painter.rotate(value_to_angle(value)) painter.drawLine( 0, -knob_radius - self.knob_to_scale, 0, -knob_radius - self.knob_to_scale - self.tick_size_large) if self.scale_text_visible: p = painter.worldTransform().map(QPoint(0, -knob_radius - \ self.knob_to_scale - \ self.tick_size_large - \ self.tick_to_text - \ self.text_radius)) painter.restore() if self.scale_text_visible: if DEBUG: painter.save() painter.setPen(QColor(255, 0, 0, 50)) painter.setBrush(QColor(255, 0, 0, 50)) painter.drawEllipse(QPoint(p.x() - x, p.y() - y), self.text_radius, self.text_radius) painter.restore() painter.drawText( p.x() - x - 30, p.y() - y - 30, 60, 60, Qt.TextDontClip | Qt.AlignHCenter | Qt.AlignVCenter, str(value)) for i in range(1, self.scale_step_divisions): sub_value = value + (float(self.scale_step_size) * i) / self.scale_step_divisions if sub_value > self.maximum_value: break painter.save() painter.rotate(value_to_angle(sub_value)) painter.drawLine( 0, -knob_radius - self.knob_to_scale, 0, -knob_radius - self.knob_to_scale - self.tick_size_small) painter.restore() value += self.scale_step_size if self.title_text != None: painter.drawText( -knob_radius, knob_radius - 30, knob_radius * 2, 60, Qt.TextDontClip | Qt.AlignHCenter | Qt.AlignVCenter, self.title_text)
def paint(self, painter, option, widget): painter.setPen(Qt.NoPen) painter.setBrush(Qt.darkGray) painter.drawEllipse(-7, -7, 20, 20) gradient = QRadialGradient(-3, -3, 10) if option.state & QStyle.State_Sunken: gradient.setCenter(3, 3) gradient.setFocalPoint(3, 3) gradient.setColorAt(1, QColor(Qt.yellow).lighter(120)) gradient.setColorAt(0, QColor(Qt.darkYellow).lighter(120)) else: gradient.setColorAt(0, Qt.yellow) gradient.setColorAt(1, Qt.darkYellow) painter.setBrush(QBrush(gradient)) painter.setPen(QPen(Qt.black, 0)) painter.drawEllipse(-10, -10, 20, 20)
def paintEvent(self, event): p = QPainter() p.begin(self) self._normalMap.render(p, event.rect()) p.setPen(Qt.black) p.drawText(self.rect(), Qt.AlignBottom | Qt.TextWordWrap, "Map data CCBYSA 2009 OpenStreetMap.org contributors") p.end() if self.zoomed: dim = min(self.width(), self.height()) magnifierSize = min(MAX_MAGNIFIER, dim * 2 / 3) radius = magnifierSize / 2 ring = radius - 15 box = QSize(magnifierSize, magnifierSize) # reupdate our mask if self.maskPixmap.size() != box: self.maskPixmap = QPixmap(box) self.maskPixmap.fill(Qt.transparent) g = QRadialGradient() g.setCenter(radius, radius) g.setFocalPoint(radius, radius) g.setRadius(radius) g.setColorAt(1.0, QColor(255, 255, 255, 0)) g.setColorAt(0.5, QColor(128, 128, 128, 255)) mask = QPainter(self.maskPixmap) mask.setRenderHint(QPainter.Antialiasing) mask.setCompositionMode(QPainter.CompositionMode_Source) mask.setBrush(g) mask.setPen(Qt.NoPen) mask.drawRect(self.maskPixmap.rect()) mask.setBrush(QColor(Qt.transparent)) mask.drawEllipse(g.center(), ring, ring) mask.end() center = self.dragPos - QPoint(0, radius) center += QPoint(0, radius / 2) corner = center - QPoint(radius, radius) xy = center * 2 - QPoint(radius, radius) # only set the dimension to the magnified portion if self.zoomPixmap.size() != box: self.zoomPixmap = QPixmap(box) self.zoomPixmap.fill(Qt.lightGray) if True: p = QPainter(self.zoomPixmap) p.translate(-xy) self._largeMap.render(p, QRect(xy, box)) p.end() clipPath = QPainterPath() clipPath.addEllipse(QPointF(center), ring, ring) p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setClipPath(clipPath) p.drawPixmap(corner, self.zoomPixmap) p.setClipping(False) p.drawPixmap(corner, self.maskPixmap) p.setPen(Qt.gray) p.drawPath(clipPath) if self.invert: p = QPainter(self) p.setCompositionMode(QPainter.CompositionMode_Difference) p.fillRect(event.rect(), Qt.white) p.end()
def paintEvent(self, objQPaintEvent): p = QPainter() p.begin(self) pen = QPen(Qt.black, 2, Qt.SolidLine) p.setPen(pen) p.drawLine(20, 15, 150, 15) pen.setStyle(Qt.DashLine) p.setPen(pen) p.drawLine(20, 35, 150, 35) pen.setStyle(Qt.DotLine) p.setPen(pen) p.drawLine(20, 55, 150, 55) pen.setStyle(Qt.DashDotLine) p.setPen(pen) p.drawLine(20, 75, 150, 75) pen.setStyle(Qt.DashDotDotLine) p.setPen(pen) p.drawLine(20, 95, 150, 95) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) p.setPen(pen) p.drawLine(20, 115, 150, 115) pen.setWidth(1) pen.setStyle(Qt.SolidLine) p.setPen(pen) brush = QBrush(Qt.SolidPattern) p.setBrush(brush) p.drawRect(180, 10, 40, 30) brush = QBrush(Qt.Dense5Pattern) p.setBrush(brush) p.drawRect(240, 10, 40, 30) brush = QBrush(Qt.Dense7Pattern) p.setBrush(brush) p.drawRect(300, 10, 40, 30) brush = QBrush(Qt.green, Qt.HorPattern) p.setBrush(brush) p.drawRect(180, 50, 40, 30) brush = QBrush(Qt.green, Qt.VerPattern) p.setBrush(brush) p.drawRect(240, 50, 40, 30) brush = QBrush(Qt.green, Qt.Dense6Pattern) brush = QBrush(Qt.green, Qt.CrossPattern) p.setBrush(brush) p.drawRect(300, 50, 40, 30) brush = QBrush(Qt.blue, Qt.BDiagPattern) p.setBrush(brush) p.drawRect(180, 90, 40, 30) brush = QBrush(Qt.blue, Qt.FDiagPattern) p.setBrush(brush) p.drawRect(240, 90, 40, 30) brush = QBrush(Qt.blue, Qt.DiagCrossPattern) p.setBrush(brush) p.drawRect(300, 90, 40, 30) g = QLinearGradient(180, 130, 220, 160) g.setColorAt(0, Qt.red) g.setColorAt(1, Qt.blue) brush = QBrush(g) p.setBrush(brush) p.drawRect(180, 130, 40, 30) g = QRadialGradient(260, 145, 20) g.setColorAt(0, Qt.red) g.setColorAt(0.5, Qt.yellow) g.setColorAt(1, Qt.blue) p.setBrush(g) p.drawRect(240, 130, 40, 30) g = QConicalGradient(320, 145, 0) g.setColorAt(0, Qt.red) g.setColorAt(0.4, Qt.yellow) g.setColorAt(0.8, Qt.blue) g.setColorAt(1, Qt.red) p.setBrush(g) p.drawRect(300, 130, 40, 30) brush = QBrush() brush.setTexture(QPixmap(":appres.img/texture.jpg")) p.setBrush(brush) pen.setColor(Qt.transparent) p.setPen(pen) p.drawRect(15, 130, 135, 35) p.end()
def paintEvent(self, event): p = QPainter() p.begin(self) self._normalMap.render(p, event.rect()) p.setPen(Qt.black) p.drawText(self.rect(), Qt.AlignBottom | Qt.TextWordWrap, "Map data CCBYSA 2009 OpenStreetMap.org contributors") p.end() if self.zoomed: dim = min(self.width(), self.height()) magnifierSize = min(MAX_MAGNIFIER, dim * 2 / 3) radius = magnifierSize / 2 ring = radius - 15 box = QSize(magnifierSize, magnifierSize) # reupdate our mask if self.maskPixmap.size() != box: self.maskPixmap = QPixmap(box) self.maskPixmap.fill(Qt.transparent) g = QRadialGradient() g.setCenter(radius, radius) g.setFocalPoint(radius, radius) g.setRadius(radius) g.setColorAt(1.0, QColor(255, 255, 255, 0)) g.setColorAt(0.5, QColor(128, 128, 128, 255)) mask = QPainter(self.maskPixmap) mask.setRenderHint(QPainter.Antialiasing) mask.setCompositionMode(QPainter.CompositionMode_Source) mask.setBrush(g) mask.setPen(Qt.NoPen) mask.drawRect(self.maskPixmap.rect()) mask.setBrush(QColor(Qt.transparent)) mask.drawEllipse(g.center(), ring, ring) mask.end() center = self.dragPos - QPoint(0, radius) center += QPoint(0, radius / 2) corner = center - QPoint(radius, radius) xy = center * 2 - QPoint(radius, radius) # only set the dimension to the magnified portion if self.zoomPixmap.size() != box: self.zoomPixmap = QPixmap(box) self.zoomPixmap.fill(Qt.lightGray) if True: p = QPainter(self.zoomPixmap) p.translate(-xy) self._largeMap.render(p, QRect(xy, box)) p.end() clipPath = QPainterPath() clipPath.addEllipse(QPointF(center), ring, ring) p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setClipPath(clipPath) p.drawPixmap(corner, self.zoomPixmap) p.setClipping(False) p.drawPixmap(corner, self.maskPixmap) p.setPen(Qt.gray) p.drawPath(clipPath) if self.invert: p = QPainter(self) p.setCompositionMode(QPainter.CompositionMode_Difference) p.fillRect(event.rect(), Qt.white) p.end()
def paintEvent(self, event): hourHand = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40)] minuteHand = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70)] secondHand = [QPoint(0, 8), QPoint(0, -80)] hourColor = QColor(127, 0, 127) minuteColor = QColor(0, 127, 127, 191) secondColor = QColor(255, 0, 0, 191) side = min(self.width(), self.height()) time = QTime.currentTime() # static function time = time.addSecs(self.timeZoneOffset) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.translate(self.width() / 2, self.height() / 2) painter.scale(side / 200.0, side / 200.0) # Draw circle radialGradient = QRadialGradient(0, 0, 100, -40, -40) # center, radius, focalPoint radialGradient.setColorAt(0.0, Qt.white) radialGradient.setColorAt(1., self.backgroundColor) painter.setBrush(radialGradient) painter.setPen(QPen(Qt.darkGray, 0)) # darkGray cosmetic pen painter.drawEllipse(QRectF(-97, -97, 194, 194)) # Draw minute tick painter.setPen(minuteColor) for j in range(60): if (j % 5) != 0: painter.drawLine(92, 0, 96, 0) painter.rotate(6.0) # draw hour hand painter.setPen(Qt.NoPen) painter.setBrush(hourColor) painter.save() painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) painter.drawConvexPolygon(hourHand) painter.restore() # draw hour tick painter.setPen(hourColor) for i in range(12): painter.drawLine(88, 0, 96, 0) painter.rotate(30.0) # draw mimute hand painter.setPen(Qt.NoPen) painter.setBrush(minuteColor) painter.save() painter.rotate(6.8 * (time.minute() + time.second() / 60.0)) painter.drawConvexPolygon(minuteHand) painter.restore() # Draw second hand painter.setPen(secondColor) painter.save() painter.rotate(6.0 * time.second()) painter.drawLine(secondHand[0], secondHand[1]) painter.restore() self.updated.emit(time)