def paint(self, painter, styleoptions, widget=None): try: width, realsize, label, fontsize = self._calc_size() except ZeroDivisionError: return mapunits = self.canvas.mapUnits() # painter.drawRect(self.boundingRect()) array = QPolygon() canvasheight = self.canvas.height() canvaswidth = self.canvas.width() margin = 20 originy = 0 originx = 0 self.setPos(margin, canvasheight - margin) x1, y1 = originx, originy x2, y2 = originx, originy + self.ticksize x3, y3 = originx + width, originy + self.ticksize midx, midy = originx + width / 2, originy + self.ticksize / 2 x4, y4 = originx + width, originy for pen in self.pens: painter.setPen(pen) # Drwa the scale bar painter.drawLine(x1, y1, x2, y2) painter.drawLine(x2, y2, x3, y3) painter.drawLine(x3, y3, x4, y4) painter.drawLine(midx, midy, midx, y1) # Draw the text fontwidth = self.metrics.width("0") fontheight = self.metrics.height() fontheight /= 2 fontwidth /= 2 path = QPainterPath() point = QPointF(x1 - fontwidth, y1 - fontheight) path.addText(point, self.font, "0") painter.setPen(self.whitepen) painter.setBrush(self.blackbrush) painter.setRenderHints(QPainter.Antialiasing) painter.setFont(self.font) painter.drawPath(path) fontwidth = self.metrics.width(label) fontheight = self.metrics.height() fontheight /= 2 fontwidth /= 2 point = QPointF(x4 - fontwidth, y4 - fontheight) path.addText(point, self.font, label) painter.drawPath(path)
def paintEvent(self, _): p = QPainter(self) points = [ QPoint(self.width(), -1), QPoint(self.width(), self.height()), QPoint(0, self.height() / 2), QPoint(0, self.height() / 2 - 1) ] grad = QLinearGradient(0, 0, 0, self.height()) grad.setColorAt(0, self.color1) grad.setColorAt(1, self.color2) p.setBrush(grad) p.setPen(QPen(Qt.NoPen)) p.drawPolygon(QPolygon(points))
def hexagon_mask(image, hexa=False, **kwargs): """ Appliquer un découpage du masque de l'image en hexagone arrondi :param image: image PIL :param hexa: False ou le rayon des arrondis de l'hexagone en pixels :return: Une image PIL dont le contour est un haxagone aux angles arrondis ou l'image originale """ if hexa is not False: try: qimage = _pil_to_qt(image) # Créer une image de la même taille que l'originale newimage = _qt_canvas(image) painter = QPainter(newimage) painter.setRenderHint(QPainter.Antialiasing, True) # Dessiner un hexagone offset = hexa * 2.0 width, height, bottom, top, t, l = qimage.width( ) - offset * 2, qimage.height( ) - offset * 2, 0.759, 0.241, offset, offset points = [ l + width / 2.0, t + 0, l + width, t + height * top, l + width, t + height * bottom, l + width / 2.0, t + height, l + 0, t + height * bottom, l + 0, t + height * top ] brush = QBrush(qimage) pen = QPen(brush, offset * 2.0, cap=Qt.RoundCap, join=Qt.RoundJoin) pen.setColor(QColor(0, 0, 0, 0)) painter.setBrush(brush) painter.setPen(pen) painter.drawPolygon(QPolygon(points)) painter.end() return _qt_to_pil(newimage) except Exception: return image else: return image
def rescale_method(self): # print("slotMethod") ysize = 1.7 * self.height() if self.width() <= ysize: self.widget_diameter = self.width() else: self.widget_diameter = ysize self.change_value_needle_style([ QPolygon([ QPoint(4, 30), QPoint(-4, 30), QPoint(-2, -self.widget_diameter / 2 * self.needle_scale_factor), QPoint( 0, -self.widget_diameter / 2 * self.needle_scale_factor - 6), QPoint(2, -self.widget_diameter / 2 * self.needle_scale_factor) ]) ]) # needle = [QPolygon([ # QPoint(4, 4), # QPoint(-4, 4), # QPoint(-3, -120), # QPoint(0, -126), # QPoint(3, -120)])] # print(str(type(needle)).split("'")[1]) # # needle = [2] # print(str(type(needle[0])).split("'")[1]) self.scale_fontsize = self.initial_scale_fontsize * self.widget_diameter / 250 self.value_fontsize = self.initial_value_fontsize * self.widget_diameter / 400 # print("slotMethod end") pass
def drawArrow(element, painter, option): """ :type element: QStyle.PrimitiveElement :type painter: QPainter :type option: QStyleOption """ # From windows style but modified to enable AA if option.rect.width() <= 1 or option.rect.height() <= 1: return r = option.rect size = min(r.height(), r.width()) pixmap = QPixmap() pixmapName = "arrow-{}-{}-{}-{}-{}".format("$qt_ia", int(option.state), element, size, option.palette.cacheKey()) if not QPixmapCache.find(pixmapName, pixmap): border = int(size / 5) sqsize = int(2 * (size / 2)) image = QImage(sqsize, sqsize, QImage.Format_ARGB32) image.fill(Qt.transparent) imagePainter = QPainter(image) imagePainter.setRenderHint(QPainter.Antialiasing, True) imagePainter.translate(0.5, 0.5) a = QPolygon() if element == QStyle.PE_IndicatorArrowUp: a.setPoints(3, border, sqsize / 2, sqsize / 2, border, sqsize - border, sqsize / 2) elif element == QStyle.PE_IndicatorArrowDown: a.setPoints(3, border, sqsize / 2, sqsize / 2, sqsize - border, sqsize - border, sqsize / 2) elif element == QStyle.PE_IndicatorArrowRight: a.setPoints(3, sqsize - border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border) elif element == QStyle.PE_IndicatorArrowLeft: a.setPoints(3, border, sqsize / 2, sqsize / 2, border, sqsize / 2, sqsize - border) bsx = 0 bsy = 0 if option.state & QStyle.State_Sunken: bsx = QApplication.style().pixelMetric(QStyle.PM_ButtonShiftHorizontal) bsy = QApplication.style().pixelMetric(QStyle.PM_ButtonShiftVertical) bounds = a.boundingRect() sx = int(sqsize / 2 - bounds.center().x() - 1) sy = int(sqsize / 2 - bounds.center().y() - 1) imagePainter.translate(sx + bsx, sy + bsy) if not (option.state & QStyle.State_Enabled): foreGround = QColor(150, 150, 150, 150) imagePainter.setBrush(option.palette.mid().color()) imagePainter.setPen(option.palette.mid().color()) else: shadow = QColor(0, 0, 0, 100) imagePainter.translate(0, 1) imagePainter.setPen(shadow) imagePainter.setBrush(shadow) foreGround = QColor(255, 255, 255, 210) imagePainter.drawPolygon(a) imagePainter.translate(0, -1) imagePainter.setPen(foreGround) imagePainter.setBrush(foreGround) imagePainter.drawPolygon(a) imagePainter.end() pixmap = QPixmap.fromImage(image) QPixmapCache.insert(pixmapName, pixmap) xOffset = int(r.x() + (r.width() - size) / 2) yOffset = int(r.y() + (r.height() - size) / 2) painter.drawPixmap(xOffset, yOffset, pixmap)
import math import time from PyQt4.QtGui import QWidget, QImage, QApplication, QPainter,QColor,QPolygon,QPen,QFont,QBrush, QInputDialog from PyQt4 import QtCore from naoqi import ALProxy import grabSmall import grabLarge import grabBall import grabBottle import speechOutput as dialog # To get the constants relative to the video. import vision_definitions arrowPolygon= QPolygon() arrowPolygon <<QtCore.QPoint(18,24)<<QtCore.QPoint( 18,3)<<QtCore.QPoint(12,9)<<QtCore.QPoint(24,9)<<QtCore.QPoint(18,3) class ImageWidget(QWidget): """ Tiny widget to display camera images from Naoqi. """ def __init__(self, IP, PORT, CameraID,attitude, parent=None): """ Initialization. """ QWidget.__init__(self, parent) self._image = QImage() self.setWindowTitle('Nao')
def paintEvent(self, event=None): LogicalSize = 100.0 def logicalFromPhysical(length, side): return (length / side) * LogicalSize fm = QFontMetricsF(self.font()) ymargin = ( (LogicalSize / 30.0) + logicalFromPhysical(self.leftSpinBox.height(), self.height())) ymax = (LogicalSize - logicalFromPhysical(fm.height() * 2, self.height())) width = LogicalSize / 4.0 cx, cy = LogicalSize / 2.0, LogicalSize / 3.0 ax, ay = cx - (2 * width), ymargin bx, by = cx - width, ay dx, dy = cx + width, ay ex, ey = cx + (2 * width), ymargin fx, fy = cx + (width / 2), cx + (LogicalSize / 24.0) gx, gy = fx, ymax hx, hy = cx - (width / 2), ymax ix, iy = hx, fy painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) side = min(self.width(), self.height()) painter.setViewport((self.width() - side) / 2, (self.height() - side) / 2, side, side) painter.setWindow(0, 0, LogicalSize, LogicalSize) painter.setPen(Qt.NoPen) gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 100)) gradient.setColorAt(0, Qt.white) a = self.leftSpinBox.value() gradient.setColorAt(1, (Qt.red if a != 0 else Qt.white)) painter.setBrush(QBrush(gradient)) painter.drawPolygon(QPolygon([ax, ay, bx, by, cx, cy, ix, iy])) gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 100)) gradient.setColorAt(0, Qt.white) b = self.rightSpinBox.value() gradient.setColorAt(1, (Qt.blue if b != 0 else Qt.white)) painter.setBrush(QBrush(gradient)) painter.drawPolygon(QPolygon([cx, cy, dx, dy, ex, ey, fx, fy])) if (a + b) == 0: color = QColor(Qt.white) else: ashare = (a / (a + b)) * 255.0 bshare = 255.0 - ashare color = QColor(ashare, 0, bshare) gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 100)) gradient.setColorAt(0, Qt.white) gradient.setColorAt(1, color) painter.setBrush(QBrush(gradient)) painter.drawPolygon(QPolygon([cx, cy, fx, fy, gx, gy, hx, hy, ix, iy])) painter.setPen(Qt.black) painter.drawPolyline(QPolygon([ax, ay, ix, iy, hx, hy])) painter.drawPolyline(QPolygon([gx, gy, fx, fy, ex, ey])) painter.drawPolyline(QPolygon([bx, by, cx, cy, dx, dy]))
def __init__(self, parent=None): super(AnalogGaugeWidget, self).__init__(parent) self.use_timer_event = False self.black = QColor(0, 0, 0, 255) # self.valueColor = QColor(50, 50, 50, 255) # self.set_valueColor(50, 50, 50, 255) # self.NeedleColor = QColor(50, 50, 50, 255) self.set_NeedleColor(50, 50, 50, 255) self.NeedleColorReleased = self.NeedleColor # self.NeedleColorDrag = QColor(255, 0, 00, 255) self.set_NeedleColorDrag(255, 0, 00, 255) self.set_ScaleValueColor(50, 50, 50, 255) self.set_DisplayValueColor(50, 50, 50, 255) # self.CenterPointColor = QColor(50, 50, 50, 255) self.set_CenterPointColor(50, 50, 50, 255) # self.valueColor = black # self.black = QColor(0, 0, 0, 255) self.value_needle_count = 1 self.value_needle = QObject self.change_value_needle_style([QPolygon([ QPoint(4, 4), QPoint(-4, 4), QPoint(-3, -120), QPoint(0, -126), QPoint(3, -120) ])]) self.value_min = 0 self.value_max = 1000 self.value = self.value_min self.value_offset = 0 self.value_needle_snapzone = 0.05 self.last_value = 0 # self.value2 = 0 # self.value2Color = QColor(0, 0, 0, 255) self.gauge_color_outer_radius_factor = 1 self.gauge_color_inner_radius_factor = 0.95 self.center_horizontal_value = 0 self.center_vertical_value = 0 self.debug1 = None self.debug2 = None self.scale_angle_start_value = 135 self.scale_angle_size = 270 self.angle_offset = 0 # self.scala_main_count = 10 self.set_scala_main_count(10) self.scala_subdiv_count = 5 self.pen = QPen(QColor(0, 0, 0)) self.font = QFont('Decorative', 20) self.scale_polygon_colors = [] self.set_scale_polygon_colors([[.00, Qt.red], [.1, Qt.yellow], [.15, Qt.green], [1, Qt.transparent]]) # initialize Scale value text # self.enable_scale_text = True self.set_enable_ScaleText(True) self.scale_fontname = "Decorative" self.initial_scale_fontsize = 15 self.scale_fontsize = self.initial_scale_fontsize # initialize Main value text self.enable_value_text = True self.value_fontname = "Decorative" self.initial_value_fontsize = 40 self.value_fontsize = self.initial_value_fontsize self.text_radius_factor = 0.7 # En/disable scale / fill # self.enable_barGraph = True self.set_enable_barGraph(True) # self.enable_filled_Polygon = True self.set_enable_filled_Polygon(True) self.enable_CenterPoint = True self.enable_fine_scaled_marker = True self.enable_big_scaled_marker = True self.needle_scale_factor = 0.8 self.enable_Needle_Polygon = True # necessary for resize self.setMouseTracking(False) # QTimer sorgt für neu Darstellung alle X ms # evtl performance hier verbessern mit self.update() und self.use_timer_event = False # todo: self.update als default ohne ueberpruefung, ob self.use_timer_event gesetzt ist oder nicht # Timer startet alle 10ms das event paintEvent if self.use_timer_event: timer = QTimer(self) timer.timeout.connect(self.update) timer.start(10) else: self.update() self.setWindowTitle("Analog Gauge") # self.connect(self, SIGNAL("resize()"), self.rescaleMethod) # self.resize(300 , 300) self.rescale_method()
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 _draw_polyline(self, painter, coord): polygone = QPolygon([QPoint(x, y) for x, y in coord]) painter.drawPolygon(polygone)
def paintEvent(self, event): QWidget.paintEvent(self, event) width, height = self.width(), self.height() polygon = QPolygon() for i, rate in enumerate(self.loads): x = width - i * self.pointDistance y = height - rate * height if x < self.boxWidth: break polygon.append(QPoint(x, y)) painter = QPainter(self) pen = QPen() pen.setColor(Qt.darkGreen) painter.setPen(pen) painter.setRenderHint(QPainter.Antialiasing, True) #画网格 painter.setOpacity(0.5) gridSize = self.pointDistance * 4 deltaX = (width - self.boxWidth) % gridSize + self.boxWidth deltaY = height % gridSize for i in range(int(width / gridSize)): x = deltaX + gridSize * i painter.drawLine(x, 0, x, height) for j in range(int(height / gridSize)): y = j * gridSize + deltaY painter.drawLine(self.boxWidth, y, width, y) #画折线 pen.setColor(Qt.darkCyan) pen.setWidth(2) painter.setPen(pen) painter.setOpacity(1) painter.drawPolyline(polygon) #画展示框 if len(self.loads) > 0: rate = self.loads[0] else: rate = 1.0 rect1 = QRect(4, height * 0.05, self.boxWidth - 9, height * 0.7) rect2 = QRect(4, height * 0.8, self.boxWidth - 9, height * 0.2) centerX = int(rect1.width() / 2) + 1 pen.setWidth(1) for i in range(rect1.height()): if i % 4 == 0: continue if (rect1.height() - i) / rect1.height() > rate: pen.setColor(Qt.darkGreen) else: pen.setColor(Qt.green) painter.setPen(pen) for j in range(rect1.width()): if centerX - 1 <= j <= centerX + 1: continue painter.drawPoint(rect1.x() + j, rect1.y() + i) pen.setColor(Qt.black) painter.setPen(pen) painter.drawText(rect2, Qt.AlignHCenter | Qt.AlignVCenter, str(int(rate * 100)) + "%")