def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QPen(Qt.NoPen)) painter.setBrush(QBrush(self.palette().color(QPalette.Highlight))) num = 8 painter.translate(self.width()/2, self.height()/2) painter.rotate(360.0/num * (self.counter % num)) for i in range(num): s = 25 + i x = 50 * math.cos(2.0 * math.pi * i / num) - s/2.0 y = 50 * math.sin(2.0 * math.pi * i / num) - s/2.0 painter.drawEllipse( x, y, s, s) painter.end()
def paintEvent(self, event): if not self.displayedWhenStopped and not self.isAnimated(): return width = min(self.width(), self.height()) p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) outerRadius = (width-1) * 0.5 innerRadius = (width-1) * 0.5 * 0.38 capsuleHeight = outerRadius - innerRadius capsuleWidth = capsuleHeight * 0.23 if width > 32 else capsuleHeight * 0.35 capsuleRadius = capsuleWidth / 2 for i in range(12): color = QColor(self.color) color.setAlphaF(float(1.0 - float(i / 12.0))) p.setPen(Qt.NoPen) p.setBrush(color) p.save() p.translate(self.rect().center()) p.rotate(self.angle - float(i * 30.0)) p.drawRoundedRect(-capsuleWidth * 0.5,\ -(innerRadius + capsuleHeight),\ capsuleWidth,\ capsuleHeight,\ capsuleRadius,\ capsuleRadius) p.restore()
def paintEvent(self, _): s = QToolButton.sizeHint(self) r = 0 p = QPoint() if self.direction == QBoxLayout.TopToBottom: r = 90 p = QPoint(0, -s.height()) elif self.direction == QBoxLayout.BottomToTop: r = -90 p = QPoint(-s.width(), 0) pixmap = QPixmap(s) pixmap.fill(QColor(0, 0, 0, 0)) o = QStyleOptionToolButton() self.initStyleOption(o) o.rect.setSize(s) pixpainter = QPainter() pixpainter.begin(pixmap) self.style().drawComplexControl(QStyle.CC_ToolButton, o, pixpainter, self) pixpainter.end() painter = QPainter(self) painter.rotate(r) painter.drawPixmap(p, pixmap)
def paintEvent(self, event): """ custom paint event to draw vertical text """ painter = QPainter(self) # draw box around arc_size, line_width=10, 1 pen = QPen(QtCore.Qt.gray) pen.setWidth(line_width) painter.setPen(pen) painter.drawLine(arc_size,0, self.width(), 0) painter.drawLine(0, arc_size,0, self.height()-arc_size) painter.drawLine(arc_size-5,self.height()-1,self.width(), self.height()-1) painter.drawArc(0,0, arc_size*2, arc_size*2, 180*16, -90*16) painter.drawArc(0,self.height()-arc_size*2, arc_size*2, arc_size*2, 180*16, 90*16) # draw box around #if (self.isEnabled()): if self.selected: painter.setPen(QtCore.Qt.black) else: painter.setPen(QtCore.Qt.gray) painter.translate(self.width(), self.height()) painter.rotate(270) painter.drawText(QtCore.QPointF(10,- self.width()/3), self.text()) # destroy del painter
def paintEvent(self, event): """ custom paint event to draw vertical text """ painter = QPainter(self) # draw box around arc_size, line_width = 10, 1 pen = QPen(QtCore.Qt.gray) pen.setWidth(line_width) painter.setPen(pen) painter.drawLine(arc_size, 0, self.width(), 0) painter.drawLine(0, arc_size, 0, self.height() - arc_size) painter.drawLine(arc_size - 5, self.height() - 1, self.width(), self.height() - 1) painter.drawArc(0, 0, arc_size * 2, arc_size * 2, 180 * 16, -90 * 16) painter.drawArc(0, self.height() - arc_size * 2, arc_size * 2, arc_size * 2, 180 * 16, 90 * 16) # draw box around #if (self.isEnabled()): if self.selected: painter.setPen(QtCore.Qt.black) else: painter.setPen(QtCore.Qt.gray) painter.translate(self.width(), self.height()) painter.rotate(270) painter.drawText(QtCore.QPointF(10, -self.width() / 3), self.text()) # destroy del painter
def paintEvent(self, event): painter=QPainter(self) angleSpan = self.m_endAngle - self.m_startAngle valueSpan = self.m_maximum - self.m_minimum rotate = (self.m_value - self.m_minimum) / valueSpan * angleSpan + self.m_startAngle targetRect = self.availableRect(self.m_backgroundRenderer) painter.translate((self.width() - targetRect.width()) / 2.0, (self.height() - targetRect.height()) / 2.0) painter.save() self.m_backgroundRenderer.render(painter, targetRect) targetRect = self.availableRect(self.m_needleRenderer) targetRect.moveTopLeft(QPointF(-targetRect.width() * self.m_originX, -targetRect.height() * self.m_originY)) painter.translate(targetRect.width() * self.m_originX, targetRect.height() * self.m_originY) painter.save() painter.translate(2, 4) painter.rotate(rotate) self.m_needleShadowRenderer.render(painter, targetRect) painter.restore() painter.rotate(rotate) self.m_needleRenderer.render(painter, targetRect) painter.restore(); if self.m_showOverlay: targetRect = self.availableRect(self.m_overlayRenderer) self.m_overlayRenderer.render(painter, targetRect)
def paintEvent(self, event): 'Paint semi-transparent background, animated pattern, background text' QWidget.paintEvent(self, event) # make a painter p = QPainter(self) p.setRenderHint(QPainter.TextAntialiasing) p.setRenderHint(QPainter.HighQualityAntialiasing) # fill a rectangle with transparent painting p.fillRect(event.rect(), Qt.transparent) # animated random dots background pattern for i in range(4096): x = randint(9, self.size().width() - 9) y = randint(9, self.size().height() - 9) p.setPen(QPen(QColor(randint(200, 255), randint(200, 255), 255), 1)) p.drawPoint(x, y) # set pen to use white color p.setPen(QPen(QColor(randint(9, 255), randint(9, 255), 255), 1)) # Rotate painter 45 Degree p.rotate(35) # Set painter Font for text p.setFont(QFont('Ubuntu', 300)) # draw the background text, with antialiasing p.drawText(99, 199, "Radio") # Rotate -45 the QPen back ! p.rotate(-35) # set the pen to no pen p.setPen(Qt.NoPen) # Background Color p.setBrush(QColor(0, 0, 0)) # Background Opacity p.setOpacity(0.75) # Background Rounded Borders p.drawRoundedRect(self.rect(), 50, 50) # finalize the painter p.end()
def create_major_ticks(self): """ Painter of the major ticks """ painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(*self.center_p()) # painter.setPen(Qt.NoPen) # Major ticks color self.pen = QPen(QColor(0, 0, 0, 255)) self.pen.setWidth(2) # # if outline_pen_with > 0: painter.setPen(self.pen) painter.rotate(self.scale_angle_start_value - self.angle_offset) steps_size = (float(self.scale_angle_size) / float(self.scala_main_count)) scale_line_outer_start = self.widget_diameter / 2 scale_line_lenght = (self.widget_diameter / 2) - (self.widget_diameter / 20) # print(stepszize) for i in range(self.scala_main_count + 1): painter.drawLine(scale_line_lenght, 0, scale_line_outer_start, 0) painter.rotate(steps_size)
def paintEvent(self, event): painter = QPainter(self) rect = self.geometry() text_rect = QRect(0, 0, rect.width(), rect.height()) painter.translate(text_rect.bottomLeft()) painter.rotate(-90) painter.drawText(QRect(QPoint(0, 0), QSize(rect.height(), rect.width())), Qt.AlignCenter, self.text()) painter.end()
def paintEvent(self, event): painter = QPainter(self) rect = self.geometry() text_rect = QRect(0, 0, rect.width(), rect.height()) painter.translate(text_rect.bottomLeft()) painter.rotate(-90) painter.drawText( QRect(QPoint(0, 0), QSize(rect.height(), rect.width())), Qt.AlignCenter, self.text()) painter.end()
def drawPatchQt(self, pos, turn, invert, patch_type, image, size, foreColor, backColor, penwidth): # pylint: disable=unused-argument """ :param size: patch size """ path = self.PATH_SET[patch_type] if not path: # blank patch invert = not invert path = [(0., 0.), (1., 0.), (1., 1.), (0., 1.), (0., 0.)] polygon = QPolygonF([QPointF(x * size, y * size) for x, y in path]) rot = turn % 4 rect = [ QPointF(0., 0.), QPointF(size, 0.), QPointF(size, size), QPointF(0., size) ] rotation = [0, 90, 180, 270] nopen = QtGui.QPen(foreColor, Qt.NoPen) foreBrush = QtGui.QBrush(foreColor, Qt.SolidPattern) if penwidth > 0: pen_color = QtGui.QColor(255, 255, 255) pen = QtGui.QPen(pen_color, Qt.SolidPattern) pen.setWidth(penwidth) painter = QPainter() painter.begin(image) painter.setPen(nopen) painter.translate(pos[0] * size + penwidth / 2, pos[1] * size + penwidth / 2) painter.translate(rect[rot]) painter.rotate(rotation[rot]) if invert: # subtract the actual polygon from a rectangle to invert it poly_rect = QPolygonF(rect) polygon = poly_rect.subtracted(polygon) painter.setBrush(foreBrush) if penwidth > 0: # draw the borders painter.setPen(pen) painter.drawPolygon(polygon, Qt.WindingFill) # draw the fill painter.setPen(nopen) painter.drawPolygon(polygon, Qt.WindingFill) painter.end() return image
def draw_needle(self): painter = QPainter(self) # painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(self.width() / 2, self.height() / 2) painter.setPen(Qt.NoPen) painter.setBrush(self.NeedleColor) painter.rotate(((self.value - self.value_offset - self.value_min) * self.scale_angle_size / (self.value_max - self.value_min)) + 90 + self.scale_angle_start_value) painter.drawConvexPolygon(self.value_needle[0])
def create_fine_scaled_marker(self): # Description_dict = 0 my_painter = QPainter(self) my_painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen my_painter.translate(self.width() / 2, self.height() / 2) my_painter.setPen(Qt.black) my_painter.rotate(self.scale_angle_start_value - self.angle_offset) steps_size = (float(self.scale_angle_size) / float(self.scala_main_count * self.scala_subdiv_count)) scale_line_outer_start = self.widget_diameter/2 scale_line_lenght = (self.widget_diameter / 2) - (self.widget_diameter / 25) for i in range((self.scala_main_count * self.scala_subdiv_count)+1): my_painter.drawLine(scale_line_lenght, 0, scale_line_outer_start, 0) my_painter.rotate(steps_size)
def paintEvent(self, event): """Paint a nice primitive arrow.""" opt = QStyleOption() opt.initFrom(self) p = QPainter(self) #painter.translate(20, 10) p.rotate(-45) if self._direction == QArrow.UP: primitive = QStyle.PE_IndicatorArrowUp elif self._direction == QArrow.DOWN: primitive = QStyle.PE_IndicatorArrowDown elif self._direction == QArrow.LEFT: primitive = QStyle.PE_IndicatorArrowLeft else: primitive = QStyle.PE_IndicatorArrowRight self.style().drawPrimitive(primitive, opt, p, self)
def paintEvent(self, event): qp = QPainter() qp.begin(self) qp.translate(self.width()/2, self.height()/2); for i in range(24): qp.drawLine(88, 0, 96, 0); qp.rotate(15.0); qp.rotate(((self.count % 24)*15+270)%360) qp.drawLine(50, 0, 80, 0) if self.pressed: qp.setBrush(QBrush(Qt.red)) qp.drawEllipse(-50, -50, 100, 100) qp.end()
def create_minor_ticks(self): # Description_dict = 0 painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(*self.center_p()) painter.setPen(Qt.black) painter.rotate(self.scale_angle_start_value - self.angle_offset) steps_size = (float(self.scale_angle_size) / float(self.scala_main_count * self.scala_subdiv_count)) scale_line_outer_start = self.widget_diameter / 2 scale_line_lenght = (self.widget_diameter / 2) - (self.widget_diameter / 40) for i in range((self.scala_main_count * self.scala_subdiv_count) + 1): painter.drawLine(scale_line_lenght, 0, scale_line_outer_start, 0) painter.rotate(steps_size)
def paintEvent(self, e): time = QTime.currentTime() qp = QPainter() qp.begin(self) #qp.setRenderHint(QPainter.Antialiasing) # 开启这个抗锯齿,会很占cpu的! qp.translate(self.width() / 2, self.height() / 2) qp.scale(self.side / 200.0, self.side / 200.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.hourColor) qp.save() qp.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) qp.drawConvexPolygon(self.hourHand) qp.restore() qp.setPen(self.hourColor) for i in range(12): qp.drawLine(88, 0, 96, 0) qp.rotate(30.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.minuteColor) qp.save() qp.rotate(6.0 * ((time.minute() + (time.second() + time.msec() / 1000.0) / 60.0))) qp.drawConvexPolygon(self.minuteHand) qp.restore() qp.setPen(self.minuteColor) for i in range(60): if (i % 5) is not 0: qp.drawLine(92, 0, 96, 0) qp.rotate(6.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.secondColor) qp.save() qp.rotate(6.0 * (time.second() + time.msec() / 1000.0)) qp.drawConvexPolygon(self.secondHand) qp.restore() qp.end()
def paintEvent(self, e): time = QTime.currentTime() qp = QPainter() qp.begin(self) #qp.setRenderHint(QPainter.Antialiasing) # 开启这个抗锯齿,会很占cpu的! qp.translate(self.width() / 2, self.height() / 2) qp.scale(self.side / 200.0, self.side / 200.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.hourColor) qp.save() qp.rotate(30.0 * ((time.hour() + time.minute()/ 60.0))) qp.drawConvexPolygon(self.hourHand) qp.restore() qp.setPen(self.hourColor) for i in range(12): qp.drawLine(88, 0, 96, 0) qp.rotate(30.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.minuteColor) qp.save() qp.rotate(6.0 * ((time.minute() + (time.second()+time.msec()/1000.0) / 60.0))) qp.drawConvexPolygon(self.minuteHand) qp.restore() qp.setPen(self.minuteColor) for i in range(60): if (i % 5) is not 0: qp.drawLine(92, 0, 96, 0) qp.rotate(6.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.secondColor) qp.save() qp.rotate(6.0*(time.second()+time.msec()/1000.0)) qp.drawConvexPolygon(self.secondHand) qp.restore() qp.end()
def draw_big_scaled_markter(self): my_painter = QPainter(self) my_painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen my_painter.translate(self.width() / 2, self.height() / 2) # my_painter.setPen(Qt.NoPen) self.pen = QPen(QColor(0, 0, 0, 255)) self.pen.setWidth(2) # # if outline_pen_with > 0: my_painter.setPen(self.pen) my_painter.rotate(self.scale_angle_start_value - self.angle_offset) steps_size = (float(self.scale_angle_size) / float(self.scala_main_count)) scale_line_outer_start = self.widget_diameter/2 scale_line_lenght = (self.widget_diameter / 2) - (self.widget_diameter / 20) # print(stepszize) for i in range(self.scala_main_count+1): my_painter.drawLine(scale_line_lenght, 0, scale_line_outer_start, 0) my_painter.rotate(steps_size)
def draw_needle(self): painter = QPainter(self) # painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(self.width() / 2, self.height() / 2) painter.setPen(Qt.NoPen) painter.setBrush(self.NeedleColor) # painter.rotate(((self.value - self.value_offset - self.value_min) * self.scale_angle_size / # (self.value_max - self.value_min)) + 90 + self.scale_angle_start_value) frac, whole = math.modf(self.value) if frac >= 0: frac = 1-frac rotFraction = ((self.value - self.value_offset - self.value_min)/(self.value_max - self.value_min)) else: frac = 1+frac rotFraction = (((whole-frac) - self.value_offset - self.value_min)/(self.value_max - self.value_min)) painter.rotate((1-rotFraction) * self.scale_angle_size + 90 + self.scale_angle_start_value) painter.drawConvexPolygon(self.value_needle[0])
def paintEvent(self, event): """Paint semi-transparent background,animated pattern,background text""" if not A11Y: p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setRenderHint(QPainter.TextAntialiasing) p.setRenderHint(QPainter.HighQualityAntialiasing) p.fillRect(event.rect(), Qt.transparent) # animated random dots background pattern for i in range(4096): x = randint(25, self.size().width() - 25) y = randint(25, self.size().height() - 25) # p.setPen(QPen(QColor(randint(9, 255), 255, 255), 1)) p.drawPoint(x, y) p.setPen(QPen(Qt.white, 1)) p.rotate(40) p.setFont(QFont('Ubuntu', 250)) p.drawText(200, 99, "Nuitka") p.rotate(-40) p.setPen(Qt.NoPen) p.setBrush(QColor(0, 0, 0)) p.setOpacity(0.8) p.drawRoundedRect(self.rect(), 9, 9) p.end()
def paintEvent(self, event): if self._isRunning: anglestep = 360. / self._steps fillsteps = self._fillsteps factor = min(self.width(), self.height()) / 16. bw = self._bw p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.scale(factor, factor) p.setPen(Qt.NoPen) for i in range(self._steps): x1, y1 = self._coords[i] c = fillsteps[self._steps - 1 - i] a = anglestep * i p.setBrush(QBrush(QColor.fromRgbF(bw, bw, bw, c))) p.save() p.translate(x1 - 2, y1 - 1) p.translate(2, 1) p.rotate(a) p.translate(-2, -1) p.drawPath(self._path) p.restore()
class Meter(QtGui.QWidget): """ a PyQt instance of QtMeter from Qt example code """ def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.value = 0 self.minValue = 0 self.maxValue = 100 self.logo = "" self.scaleMajor = 10 self.scaleMijor = 10 self.startAngle = 60 self.endAngle = 60 self.crownColor = Qt.blue self.foreground = Qt.green self.background = Qt.black self.timer = QTimer() self.timer.timeout.connect(self.update) self.timer.start(200) self.resize(200, 200) def updateValue(self): pass def paintEvent(self, QPaintEvent): self.updateValue() self.side = min(self.width(), self.height()) self.painter = QPainter() self.painter.begin(self) #self.painter.setRenderHint(QPainter.Antialiasing) self.painter.translate(self.width() / 2, self.height() / 2) self.painter.scale(self.side / 200.0, self.side / 200.0) self.painter.setPen(Qt.NoPen) self.drawCrown() self.drawBackgroud() self.drawLogo() self.drawScale() self.drawScaleNum() self.drawNumbericValue() self.drawPointer() self.painter.end() def setValue(self, updatefun): self.value = updatefun() def setLogo(self, logo): self.logo = logo def drawCrown(self): self.painter.save() self.painter.setPen(QtGui.QPen(self.crownColor, 3)) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawBackgroud(self): self.painter.save() self.painter.setBrush(self.background) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawScale(self): self.painter.save() self.painter.rotate(self.startAngle) self.painter.setPen(self.foreground) steps = self.scaleMajor * self.scaleMijor angleStep = (360.0 - self.startAngle - self.endAngle) / steps pen = QtGui.QPen(self.painter.pen()) for i in xrange(steps + 1): if i % self.scaleMajor == 0: pen.setWidth(1) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 72) else: pen.setWidth(0) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 65) self.painter.rotate(angleStep) self.painter.restore() def drawScaleNum(self): self.painter.save() self.painter.setPen(self.foreground) startRad = (360 - self.startAngle - 90) * (3.14 / 180) deltaRad = (360 - self.startAngle - self.endAngle) * (3.14 / 180) / self.scaleMajor fm = QtGui.QFontMetricsF(self.font()) for i in xrange(self.scaleMajor + 1): sina = sin(startRad - i * deltaRad) cosa = cos(startRad - i * deltaRad) tmpVal = 1.0 * i * ((self.maxValue - self.minValue) / self.scaleMajor) + self.minValue numstr = QString("%1").arg(tmpVal) w = fm.size(Qt.TextSingleLine, numstr).width() h = fm.size(Qt.TextSingleLine, numstr).height() x = 82 * cosa - w / 2 y = -82 * sina + h / 4 self.painter.drawText(x, y, numstr) self.painter.restore() def drawLogo(self): self.painter.save() self.painter.setPen(self.foreground) self.painter.setBrush(self.foreground) logostr = QString(self.logo) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine, logostr).width() self.painter.drawText(-w / 2, -30, logostr) self.painter.restore() def drawNumbericValue(self): self.painter.save() color = QtGui.QColor(150, 150, 200) pen = self.painter.pen() pen.setWidth(3) self.painter.setPen(pen) self.painter.setPen(color) self.painter.drawRect(-30, 30, 60, 14) cpustr = QString("%1").arg(self.value) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine, cpustr).width() self.painter.setPen(self.foreground) self.painter.drawText(-w / 2, 42, cpustr) self.painter.restore() def drawPointer(self): self.painter.save() self.pointerHand = QPolygon([-2, 0, 2, 0, 0, 60]) self.pointerColor = QColor(127, 0, 127) self.painter.setBrush(self.pointerColor) self.painter.rotate(self.startAngle) degRotate = (360.0 - self.startAngle - self.endAngle) / ( self.maxValue - self.minValue) * (self.value - self.minValue) self.painter.rotate(degRotate) self.painter.drawConvexPolygon(self.pointerHand) self.painter.restore()
def paintEvent(self, event): # Initialize QPainter properties painter = QPainter() painter.begin(self) painter.setRenderHint(QPainter.Antialiasing) if self.height() <= self.width() / self.ref_aspect_ratio: v_scale = self.height() h_scale = v_scale * self.ref_aspect_ratio else: h_scale = self.width() v_scale = h_scale / self.ref_aspect_ratio # Scale all objects proportionate to window size painter.scale(h_scale / self.width_ref, v_scale / self.height_ref) painter.setClipPath(self.dial) # Don't allow objects or text to extend outside of main dial shape painter.save() # First draw main gauge background pen = QPen(painter.pen()) pen.setWidth(1) pen.setColor(Qt.black) painter.setPen(pen) painter.setBrush(QColor(100, 100, 100, 255)) # self.dial_bg) painter.drawPath(self.dial) # Add Minor and Major Alarm limit bars pen.setWidth(16) pen.setCapStyle(Qt.FlatCap) pen.setJoinStyle(Qt.BevelJoin) pen.setColor(Qt.yellow) painter.setPen(pen) painter.setBrush(Qt.NoBrush) painter.drawPath(self.low_arc) painter.drawPath(self.high_arc) pen.setColor(Qt.red) painter.setPen(pen) painter.drawPath(self.lolo_arc) painter.drawPath(self.hihi_arc) painter.restore() # Display PV current value painter.save() font = QFont() font.setPixelSize(45) painter.setFont(font) sevr = self.channel.sevr.lower() if sevr == 'major': color = Qt.red elif sevr == 'minor': color = Qt.yellow elif sevr == 'invalid': color = Qt.magenta else: color = Qt.green pen.setColor(color) painter.setPen(pen) font_metric = QFontMetrics(font) painter.translate(self.dial_width / 2, self.dial_height / 2) label = self.format_label(self.channel_value) painter.drawText(QPointF(0.0 - font_metric.width(label) / 2.0, font_metric.height() / 2.0), label) # Display PV name painter.setFont(self.pv_label_font) pen.setColor(Qt.black) # Qt.darkCyan) pen.setWidth(1) painter.setPen(pen) # brush = QBrush(Qt.darkCyan) # painter.setBrush(brush) font_metric = QFontMetrics(self.pv_label_font) pv_label = self.channel.egu # self.channel.name + ' (' + self.channel.egu + ')' painter.drawText(QPointF(0.0 - font_metric.width(pv_label) / 2.0, (self.dial_height / 2.0) + (font_metric.height() * 1.5)), pv_label) # painter.drawPath(self.pv_label_path) painter.restore() # Next add division markers painter.save() painter.translate(self.dial_width / 2, self.dial_height * 0.98) pen.setColor(Qt.black) # Qt.cyan) pen.setWidth(2) painter.setPen(pen) for i in range(0, 31): if (i % 5) != 0: painter.drawLine(-self.dial_width / 2.1, 0.0, -self.dial_width / 2.2, 0.0) else: painter.drawLine(-self.dial_width / 2.1, 0.0, -self.dial_width / 2.3, 0.0) painter.rotate(6.0) painter.restore() # Layout division text labels painter.save() painter.translate(self.dial_width / 2, self.dial_height * 0.98) pen.setColor(Qt.black) # Qt.cyan) painter.setPen(pen) font = QFont() font.setPixelSize(18) painter.setFont(font) font_metric = QFontMetrics(font) labels = linspace(self.lim_low, self.lim_hi, 7) painter.rotate(-90) for i in range(0, 7): label = self.format_label(labels[i]) painter.drawText(QPointF(0.0 - font_metric.width(label) / 2.0, -self.dial_height * 0.75), label) painter.rotate(30) painter.restore() # Draw needle at appropriate angle for data painter.save() painter.translate(self.dial_width / 2, self.dial_height * 0.98) painter.rotate(-180 * (1.0 - self.percentage)) pen.setColor(QColor(self.needle_color).darker(200)) pen.setWidth(1) painter.setPen(pen) painter.setBrush(self.needle_color) painter.drawPolygon(self.needle) painter.restore() # if self.percentage <= 0.5: # shadow = max(490 * self.percentage, 127) # needle_left_color = QColor(0, shadow, shadow) # Qt.darkCyan # QColor(80,80,80,255) # needle_right_color = Qt.cyan # QColor(230,230,230,255) # else: # shadow = max(125 / self.percentage, 127) # needle_left_color = Qt.cyan # QColor(230,230,230,255) # needle_right_color = QColor(0, shadow, shadow) # Qt.darkCyan # QColor(80,80,80,255) # # # Draw Highlight side of needle # pen.setWidth(1) # pen.setColor(Qt.gray) # needle_left_color) # painter.setPen(pen) # painter.setBrush(Qt.gray) # needle_left_color) # painter.drawPolygon(self.needle_left) # # # Draw shadow side of needle # pen.setColor(Qt.gray) # needle_right_color) # painter.setPen(pen) # painter.setBrush(Qt.gray) # needle_right_color) # painter.drawPolygon(self.needle_right) # painter.restore() # Draw needle axel pin painter.save() pen.setWidth(1) pen.setColor(Qt.black) painter.setPen(pen) painter.setBrush(QColor(50, 50, 50, 255)) # self.pin_bg) painter.translate(self.dial_width / 2, self.dial_height * 0.98) painter.drawEllipse(self.pin_rect) painter.restore() # Draw glass reflection and shadow effects # painter.save() # painter.translate(self.dial_width / 2.0, self.dial_height / 2.0) # painter.setPen(Qt.NoPen) # painter.setBrush(QColor(0, 0, 0, 20)) # painter.drawEllipse(self.shadow_rect) # painter.setBrush(self.gloss_gradient) # painter.drawEllipse(self.gloss_rect) # painter.restore() painter.end()
class QtRenderer(Renderer): def __init__(self, pd): """Creates a new renderer based on a QPaintDevice pd """ self._defpose = Pose() # The pose in the bottom-left corner self._zoom = 1 # The zooming factor self._zoom_c = False # Whether the scaling is done from center Renderer.__init__(self, (pd.width(), pd.height()), pd) def set_canvas(self, canvas): """Tell the renderer to draw on canvas The type of canvas is implementation-dependent """ self._paintdevice = canvas self._painter = QPainter(canvas) self._painter.setRenderHint(QPainter.Antialiasing) # invert the y axis self._painter.scale(1,-1) self._painter.translate(0,-canvas.height()) self.set_pen(None) # push the default state self._painter.save() self._painter.save() def set_zoom(self, zoom_level): self._zoom = float(zoom_level) self.__update_default_state() def __update_default_state(self): self._painter.restore() # Reset state self._painter.restore() # Set zoom to 1 self._painter.save() # Re-save the zoom-1 if self._zoom_c: self._painter.translate(self.size[0]/2,self.size[1]/2) self._painter.scale(self._zoom,self._zoom) self._painter.rotate(degrees(-self._defpose.theta)) self._painter.translate(-self._defpose.x, -self._defpose.y) self._painter.save() # Save the zoomed state self.clear_screen() def __set_scr_pose(self,pose): self._defpose = pose self.__update_default_state() self.clear_screen() def set_screen_pose(self, pose): self._zoom_c = False self.__set_scr_pose(pose) def set_screen_center_pose(self, pose): self._zoom_c = True self.__set_scr_pose(pose) def clear_screen(self): self._painter.save() self._painter.resetTransform() self.set_pen(0xFFFFFF) self.set_brush(0xFFFFFF) self.draw_rectangle(0,0,self.size[0],self.size[1]) self._painter.restore() def __delete__(self): self._painter.restore() self._painter.restore() def reset_pose(self): """Resets the renderer to world coordinates """ self._painter.restore() self._painter.save() def add_pose(self,pose): """Add a pose transformation to the current transformation """ self._painter.translate(pose.x,pose.y) self._painter.rotate(degrees(pose.theta)) @staticmethod def __qcolor(color): """Returns qcolor for a given ARGB color """ c = QColor(color) if color > 0xFFFFFF: c.setAlpha((color >> 24) & 0xFF) return c def set_pen(self,color): """Sets the line color. Color is interpreted as 0xAARRGGBB. """ if color is None: self._painter.setPen(Qt.NoPen) else: self._painter.setPen(self.__qcolor(color)) def set_brush(self,color): """Sets the fill color. Color is interpreted as 0xAARRGGBB. """ if color is None: self._painter.setBrush(Qt.NoBrush) else: self._painter.setBrush(self.__qcolor(color)) def draw_polygon(self,points): """Draws a polygon. Expects a list of points as a list of tuples or as a numpy array. """ self._painter.drawPolygon(QPolygonF([QPointF(*point[:2]) for point in points])) def draw_ellipse(self, x, y, w, h): """Draws an ellipse. """ self._painter.drawEllipse(x,y,w,h) def draw_rectangle(self, x, y, w, h): """Draws a rectangle. """ self._painter.drawRect(x,y,w,h) def draw_text(self, text, x, y, bgcolor = 0): """Draws a text string at the defined position. """ pass def draw_line(self, x1, y1, x2, y2): """Draws a line using the current pen from (x1,y1) to (x2,y2) """ pass
class QtRenderer(Renderer): """An implementation of :class:`~renderer.Renderer` for PyQt4. This renderer will draw on any `QPaintDevice` """ def __init__(self, paint_device): """Creates a new renderer based on a QPaintDevice pd""" self._grid_pen = QPen(QColor(0x808080)) self._grid_pen.setStyle(Qt.DashLine) self._painter = None Renderer.__init__(self, paint_device) def set_canvas(self, canvas): """Tell the renderer to draw on canvas The type of canvas is implementation-dependent""" if self._painter is not None: self._painter.restore() self._painter.restore() self._painter.end() self._paintdevice = canvas self._painter = QPainter(canvas) self._painter.setRenderHint(QPainter.Antialiasing) # invert the y axis self._painter.scale(1,-1) self._painter.translate(0,-canvas.height()) Renderer.set_canvas(self,canvas) def _get_canvas_size(self,pd): """Get the canvas size tuple (width,height)""" return (pd.width(), pd.height()) def push_state(self): """Store the current state on the stack. Current state includes default pose, pen and brush""" ### FIXME store things self._painter.save() def pop_state(self): """Restore the last saved state from the stack The state includes default pose, pen and brush""" ### FIXME store things self._painter.restore() def _calculate_bounds(self): transform = self._painter.worldTransform().inverted()[0] xs,ys = zip( transform.map(0.0,0.0), transform.map(0.0,float(self.size[1])), transform.map(float(self.size[0]),float(self.size[1])), transform.map(float(self.size[0]),0.0) ) self._bounds = (min(xs), min(ys), max(xs), max(ys)) def _draw_grid(self): self.reset_pose() self._painter.setPen(self._grid_pen) xmin, ymin, xmax, ymax = self._bounds # Determine min/max x & y line indices: x_ticks = (int(xmin//self._grid_spacing), int(xmax//self._grid_spacing + 1)) y_ticks = (int(ymin//self._grid_spacing), int(ymax//self._grid_spacing + 1)) self._painter.drawLines( [QLineF(xmin, i * self._grid_spacing, xmax, i * self._grid_spacing) for i in range(*y_ticks)]) self._painter.drawLines( [QLineF(i * self._grid_spacing, ymin, i * self._grid_spacing, ymax) for i in range(*x_ticks)]) def scale(self, factor): """Scale drawing operations by factor To be implemented in subclasses.""" self._painter.scale(factor,factor) def rotate(self, angle): """Rotate canvas by angle (in radians) To be implemented in subclasses.""" self._painter.rotate(degrees(angle)) def translate(self, dx, dy): """Translate canvas by dx, dy To be implemented in subclasses.""" self._painter.translate(dx,dy) def clear_screen(self): """Erases the current screen with a white brush""" self._painter.save() self._painter.resetTransform() self.set_pen(0xFFFFFF) self.set_brush(0xFFFFFF) self.draw_rectangle(0,0,self.size[0],self.size[1]) self._painter.restore() Renderer.clear_screen(self) @staticmethod def __qcolor(color): """Returns qcolor for a given ARGB color""" c = QColor(color) if color > 0xFFFFFF: c.setAlpha((color >> 24) & 0xFF) return c def set_pen(self,color=0, thickness=0): """Sets the line color and thickness. Color is interpreted as 0xAARRGGBB.""" if color is None: self._painter.setPen(Qt.NoPen) else: self._painter.setPen(QPen(self.__qcolor(color),thickness)) def set_brush(self,color): """Sets the fill color. Color is interpreted as 0xAARRGGBB.""" if color is None: self._painter.setBrush(Qt.NoBrush) else: self._painter.setBrush(self.__qcolor(color)) def draw_polygon(self,points): """Draws a polygon. Expects a list of points as a list of tuples or as a numpy array.""" self._painter.drawPolygon(QPolygonF([QPointF(*point[:2]) for point in points])) def draw_ellipse(self, cx, cy, ra, rb = None): """Draws an ellipse.""" if rb is None: rb = ra self._painter.drawEllipse(QRectF(cx-ra,cy-ra,2*ra,2*rb)) def draw_rectangle(self, x, y, w, h): """Draws a rectangle.""" self._painter.drawRect(QRectF(x,y,w,h)) def draw_text(self, text, x, y, bgcolor = 0): """Draws a text string at the defined position.""" pass def draw_line(self, x1, y1, x2, y2): """Draws a line using the current pen from (x1,y1) to (x2,y2)""" self._painter.drawLine(QLineF(x1,y1,x2,y2))
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)
class Meter(QtGui.QWidget): """ a PyQt instance of QtMeter from Qt example code """ def __init__(self,parent=None): QtGui.QWidget.__init__(self,parent) self.value = 0 self.minValue = 0 self.maxValue = 100 self.logo = "" self.scaleMajor = 10 self.scaleMijor = 10 self.startAngle = 60 self.endAngle = 60 self.crownColor = Qt.blue self.foreground = Qt.green self.background = Qt.black self.timer = QTimer() self.timer.timeout.connect(self.update) self.timer.start(200) self.resize(200,200) def updateValue(self): pass def paintEvent(self,QPaintEvent): self.updateValue() self.side = min(self.width(),self.height()) self.painter = QPainter() self.painter.begin(self) #self.painter.setRenderHint(QPainter.Antialiasing) self.painter.translate(self.width()/2,self.height()/2) self.painter.scale(self.side / 200.0, self.side / 200.0) self.painter.setPen(Qt.NoPen) self.drawCrown() self.drawBackgroud() self.drawLogo() self.drawScale() self.drawScaleNum() self.drawNumbericValue() self.drawPointer() self.painter.end() def setValue(self,updatefun): self.value = updatefun() def setLogo(self,logo): self.logo = logo def drawCrown(self): self.painter.save() self.painter.setPen(QtGui.QPen(self.crownColor, 3)) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawBackgroud(self): self.painter.save() self.painter.setBrush(self.background) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawScale(self): self.painter.save() self.painter.rotate(self.startAngle) self.painter.setPen(self.foreground) steps = self.scaleMajor * self.scaleMijor angleStep = (360.0 - self.startAngle - self.endAngle) /steps pen = QtGui.QPen(self.painter.pen()) for i in xrange(steps+1): if i % self.scaleMajor == 0: pen.setWidth(1) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 72) else: pen.setWidth(0) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 65) self.painter.rotate(angleStep) self.painter.restore() def drawScaleNum(self): self.painter.save() self.painter.setPen(self.foreground) startRad = (360 - self.startAngle - 90) * (3.14 / 180) deltaRad = (360 - self.startAngle - self.endAngle) * (3.14 / 180) / self.scaleMajor fm = QtGui.QFontMetricsF(self.font()) for i in xrange(self.scaleMajor+1): sina = sin(startRad - i * deltaRad) cosa = cos(startRad - i * deltaRad) tmpVal = 1.0 * i *((self.maxValue - self.minValue) / self.scaleMajor) + self.minValue numstr = QString( "%1" ).arg(tmpVal) w = fm.size(Qt.TextSingleLine,numstr).width() h = fm.size(Qt.TextSingleLine,numstr).height() x = 82 * cosa - w / 2 y = -82 * sina + h / 4 self.painter.drawText(x, y, numstr) self.painter.restore() def drawLogo(self): self.painter.save() self.painter.setPen(self.foreground) self.painter.setBrush(self.foreground) logostr = QString(self.logo) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine,logostr).width() self.painter.drawText(-w / 2, -30, logostr) self.painter.restore() def drawNumbericValue(self): self.painter.save() color = QtGui.QColor(150, 150, 200) pen = self.painter.pen() pen.setWidth(3) self.painter.setPen(pen) self.painter.setPen(color) self.painter.drawRect(-30, 30, 60, 14) cpustr = QString("%1").arg(self.value) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine,cpustr).width() self.painter.setPen(self.foreground) self.painter.drawText(-w / 2, 42, cpustr) self.painter.restore() def drawPointer(self): self.painter.save() self.pointerHand=QPolygon([-2,0, 2,0, 0,60]) self.pointerColor = QColor(127 , 0, 127) self.painter.setBrush(self.pointerColor) self.painter.rotate(self.startAngle) degRotate = (360.0 - self.startAngle - self.endAngle)/(self.maxValue - self.minValue)*(self.value - self.minValue) self.painter.rotate(degRotate) self.painter.drawConvexPolygon(self.pointerHand) self.painter.restore()
class QtRenderer(Renderer): """An implementation of :class:`~renderer.Renderer` for PyQt4. This renderer will draw on any `QPaintDevice` """ def __init__(self, paint_device): """Creates a new renderer based on a QPaintDevice pd""" self._grid_pen = QPen(QColor(0x808080)) self._grid_pen.setStyle(Qt.DashLine) self._painter = None Renderer.__init__(self, paint_device) def set_canvas(self, canvas): """Tell the renderer to draw on canvas The type of canvas is implementation-dependent""" if self._painter is not None: self._painter.restore() self._painter.restore() self._painter.end() self._paintdevice = canvas self._painter = QPainter(canvas) self._painter.setRenderHint(QPainter.Antialiasing) # invert the y axis self._painter.scale(1, -1) self._painter.translate(0, -canvas.height()) Renderer.set_canvas(self, canvas) def _get_canvas_size(self, pd): """Get the canvas size tuple (width,height)""" return (pd.width(), pd.height()) def push_state(self): """Store the current state on the stack. Current state includes default pose, pen and brush""" ### FIXME store things self._painter.save() def pop_state(self): """Restore the last saved state from the stack The state includes default pose, pen and brush""" ### FIXME store things self._painter.restore() def _calculate_bounds(self): transform = self._painter.worldTransform().inverted()[0] xs, ys = zip(transform.map(0.0, 0.0), transform.map(0.0, float(self.size[1])), transform.map(float(self.size[0]), float(self.size[1])), transform.map(float(self.size[0]), 0.0)) self._bounds = (min(xs), min(ys), max(xs), max(ys)) def _draw_grid(self): self.reset_pose() self._painter.setPen(self._grid_pen) xmin, ymin, xmax, ymax = self._bounds # Determine min/max x & y line indices: x_ticks = (int(xmin // self._grid_spacing), int(xmax // self._grid_spacing + 1)) y_ticks = (int(ymin // self._grid_spacing), int(ymax // self._grid_spacing + 1)) self._painter.drawLines([ QLineF(xmin, i * self._grid_spacing, xmax, i * self._grid_spacing) for i in range(*y_ticks) ]) self._painter.drawLines([ QLineF(i * self._grid_spacing, ymin, i * self._grid_spacing, ymax) for i in range(*x_ticks) ]) def scale(self, factor): """Scale drawing operations by factor To be implemented in subclasses.""" self._painter.scale(factor, factor) def rotate(self, angle): """Rotate canvas by angle (in radians) To be implemented in subclasses.""" self._painter.rotate(degrees(angle)) def translate(self, dx, dy): """Translate canvas by dx, dy To be implemented in subclasses.""" self._painter.translate(dx, dy) def clear_screen(self): """Erases the current screen with a white brush""" self._painter.save() self._painter.resetTransform() self.set_pen(0xFFFFFF) self.set_brush(0xFFFFFF) self.draw_rectangle(0, 0, self.size[0], self.size[1]) self._painter.restore() Renderer.clear_screen(self) @staticmethod def __qcolor(color): """Returns qcolor for a given ARGB color""" c = QColor(color) if color > 0xFFFFFF: c.setAlpha((color >> 24) & 0xFF) return c def set_pen(self, color): """Sets the line color. Color is interpreted as 0xAARRGGBB.""" if color is None: self._painter.setPen(Qt.NoPen) else: self._painter.setPen(self.__qcolor(color)) def set_brush(self, color): """Sets the fill color. Color is interpreted as 0xAARRGGBB.""" if color is None: self._painter.setBrush(Qt.NoBrush) else: self._painter.setBrush(self.__qcolor(color)) def draw_polygon(self, points): """Draws a polygon. Expects a list of points as a list of tuples or as a numpy array.""" self._painter.drawPolygon( QPolygonF([QPointF(*point[:2]) for point in points])) def draw_ellipse(self, cx, cy, ra, rb=None): """Draws an ellipse.""" if rb is None: rb = ra self._painter.drawEllipse(QRectF(cx - ra, cy - ra, 2 * ra, 2 * rb)) def draw_rectangle(self, x, y, w, h): """Draws a rectangle.""" self._painter.drawRect(QRectF(x, y, w, h)) def draw_text(self, text, x, y, bgcolor=0): """Draws a text string at the defined position.""" pass def draw_line(self, x1, y1, x2, y2): """Draws a line using the current pen from (x1,y1) to (x2,y2)""" self._painter.drawLine(QLineF(x1, y1, x2, y2))
def paintEvent(self, event): """Adapted from http://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html""" HOURHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -55)]) MINUTEHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -87)]) HOURCOLOR = QColor(Qt.black) MINUTECOLOR = QColor(0x11, 0x11, 0x11, 0xAA) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) SIDE = 200 side = min(self.width(), self.height()) painter.scale(side / SIDE, side / SIDE) # Background (night/day) if self._time_to is not None: time = self._time_to.time() hour_offset = time.hour() + time.minute() / 60 DAY, NIGHT = QColor(Qt.white), QColor('#5555ff') if 7 <= hour_offset <= 19: background = DAY elif 6 <= hour_offset <= 7: palette = GradientPaletteGenerator(NIGHT, DAY) background = palette[(hour_offset - 6) / (7 - 6)] elif 19 <= hour_offset <= 20: palette = GradientPaletteGenerator(DAY, NIGHT) background = palette[(hour_offset - 19) / (20 - 19)] else: assert hour_offset < 7 or hour_offset > 20 background = NIGHT painter.setBrush(QBrush(background)) painter.setPen(HOURCOLOR) painter.drawEllipse(-SIDE / 2, -SIDE / 2, SIDE, SIDE) # Minute tickmarks painter.save() painter.setPen(MINUTECOLOR) for j in range(60): painter.drawLine(94, 0, 97, 0) painter.rotate(6) painter.restore() # Hour tickmarks painter.save() painter.setPen(HOURCOLOR) for _ in range(12): painter.drawLine(88, 0, 98, 0) painter.rotate(30) painter.restore() # Hour span if self._time_from is not None: time_from = self._time_from.time() time_to = self._time_to.time() if time_from.secsTo(time_to) / 3600 > .2: # Don't draw really small intervals hour_from = (time_from.hour() + time_from.minute() / 60) % 12 - 3 hour_to = (time_to.hour() + time_to.minute() / 60) % 12 - 3 startAngle = -hour_to * 30 * 16 spanAngle = -hour_from * 30 * 16 - startAngle color = QColor(0xFF, 0xFF, 0, 0xAA) painter.save() painter.setBrush(QBrush(color, Qt.DiagCrossPattern)) painter.setPen(color.darker(180)) painter.drawPie(-SIDE / 2, -SIDE / 2, SIDE, SIDE, startAngle, spanAngle) painter.restore() # Hour and minute hand if self._time_to is not None: time = self._time_to.time() painter.setPen(Qt.NoPen) painter.save() painter.setBrush(HOURCOLOR) painter.rotate(30 * (time.hour() + time.minute() / 60)) painter.drawConvexPolygon(HOURHAND) painter.restore() painter.save() painter.setBrush(MINUTECOLOR) painter.rotate(6 * (time.minute() + time.second() / 60)) painter.drawConvexPolygon(MINUTEHAND) painter.restore()
def paintEvent(self, event): """Adapted from http://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html""" HOURHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -55)]) MINUTEHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -87)]) HOURCOLOR = QColor(Qt.black) MINUTECOLOR = QColor(0x11, 0x11, 0x11, 0xAA) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) SIDE = 200 side = min(self.width(), self.height()) painter.scale(side / SIDE, side / SIDE) # Background (night/day) if self._time_to is not None: time = self._time_to.time() hour_offset = time.hour() + time.minute() / 60 DAY, NIGHT = QColor(Qt.white), QColor('#5555ff') if 7 <= hour_offset <= 19: background = DAY elif 6 <= hour_offset <= 7: palette = GradientPaletteGenerator(NIGHT, DAY) background = palette[(hour_offset - 6) / (7 - 6)] elif 19 <= hour_offset <= 20: palette = GradientPaletteGenerator(DAY, NIGHT) background = palette[(hour_offset - 19) / (20 - 19)] else: assert hour_offset < 7 or hour_offset > 20 background = NIGHT painter.setBrush(QBrush(background)) painter.setPen(HOURCOLOR) painter.drawEllipse(-SIDE / 2, -SIDE / 2, SIDE, SIDE) # Minute tickmarks painter.save() painter.setPen(MINUTECOLOR) for j in range(60): painter.drawLine(94, 0, 97, 0) painter.rotate(6) painter.restore() # Hour tickmarks painter.save() painter.setPen(HOURCOLOR) for _ in range(12): painter.drawLine(88, 0, 98, 0) painter.rotate(30) painter.restore() # Hour span if self._time_from is not None: time_from = self._time_from.time() time_to = self._time_to.time() if time_from.secsTo( time_to) / 3600 > .2: # Don't draw really small intervals hour_from = (time_from.hour() + time_from.minute() / 60) % 12 - 3 hour_to = (time_to.hour() + time_to.minute() / 60) % 12 - 3 startAngle = -hour_to * 30 * 16 spanAngle = -hour_from * 30 * 16 - startAngle color = QColor(0xFF, 0xFF, 0, 0xAA) painter.save() painter.setBrush(QBrush(color, Qt.DiagCrossPattern)) painter.setPen(color.darker(180)) painter.drawPie(-SIDE / 2, -SIDE / 2, SIDE, SIDE, startAngle, spanAngle) painter.restore() # Hour and minute hand if self._time_to is not None: time = self._time_to.time() painter.setPen(Qt.NoPen) painter.save() painter.setBrush(HOURCOLOR) painter.rotate(30 * (time.hour() + time.minute() / 60)) painter.drawConvexPolygon(HOURHAND) painter.restore() painter.save() painter.setBrush(MINUTECOLOR) painter.rotate(6 * (time.minute() + time.second() / 60)) painter.drawConvexPolygon(MINUTEHAND) painter.restore()