def rounded_rectangle(self, x, y, w, h, c, radius, border_color=None, border_width=1.0): if border_color is not None: self.qpainter.setPen(QPen(border_color, border_width)) else: self.qpainter.setPen(QPen(QColor(0, 0, 0, 0))) self.qpainter.setBrush(QBrush(c)) self.qpainter.drawRoundedRect(x, y, w, h, radius, radius)
def draw_vertical_gradient(self, x, y, w, h, c1, c2): gradient = QLinearGradient(0, 0, 0, h) gradient.setColorAt(0.0, c1) gradient.setColorAt(1.0, c2) self.qpainter.setPen(QPen(QColor(0, 0, 0, 0))) self.qpainter.setBrush(QBrush(gradient)) self.qpainter.drawRect(x, y, w, h)
def render(self, text, _, color): if self.font is None: return "", (0, 0) fm = QFontMetrics(self.font) rect = fm.boundingRect(text) im = QImage( rect.x() + rect.width(), rect.height(), QImage.Format_ARGB32_Premultiplied, ) im.fill(QColor(0, 0, 0, 0)) painter = QPainter() painter.begin(im) painter.setPen(QPen(QColor(*color))) painter.setFont(self.font) painter.drawText(QPoint(0 - rect.x(), 0 - rect.y()), text) painter.end() bits = im.bits() try: pixels = bits.tobytes() except AttributeError: bits.setsize(im.byteCount()) pixels = bytes(bits) return pixels, (rect.x() + rect.width(), rect.height())
def draw_text(self, text, x, y): # self.qpainter.drawText(QPoint(x, y), text) self.qpainter.setPen(QPen(self.text_color)) self.qpainter.drawText(x, y, 10000, 1000, Qt.AlignLeft | Qt.AlignTop, text)
def draw_rectangle(self, x, y, w, h, c): self.qpainter.setPen(QPen(QColor(0, 0, 0, 0))) self.qpainter.setBrush(QBrush(c)) self.qpainter.drawRect(x, y, w, h)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) pen = QPen(QColor(0x80, 0x80, 0x80)) pen.setWidth(2) painter.setPen(pen) painter.setBrush(QBrush(Qt.white)) x = 1 y = 1 w = self.width() - 2 h = self.height() - 2 rect = QRect(x, y, w, h) painter.drawEllipse(rect) cx = x + w / 2 cy = y + h / 2 a = w / 2 * 0.85 b = h / 2 * 0.85 pen.setWidth(0) painter.setPen(pen) painter.setBrush(QBrush(Qt.black)) for i in range(12): px = cx + a * math.cos(2 * math.pi * i / 12) py = cy + b * math.sin(2 * math.pi * i / 12) painter.drawEllipse(px - 3, py - 3, 6, 6) hours, minutes, seconds = self.time[3:] minutes += seconds / 60.0 hours += minutes / 60.0 a = w / 2 * 0.6 b = h / 2 * 0.6 pen.setWidth(4) painter.setPen(pen) self.draw_hand_line(painter, w, h, cx, cy, a, b, 2.0 * math.pi * hours / 12) a = w / 2 * 0.8 b = h / 2 * 0.8 pen.setWidth(3) painter.setPen(pen) self.draw_hand_line(painter, w, h, cx, cy, a, b, 2.0 * math.pi * minutes / 60) pen = QPen(QColor(0xFF, 0x00, 0x00)) pen.setWidth(2) painter.setPen(pen) self.draw_hand_line(painter, w, h, cx, cy, a, b, 2.0 * math.pi * seconds / 60)