コード例 #1
0
    def paintEvent(self, QPaintEvent):
        p = QPainter(self.viewport())

        clipPath = QPainterPath()
        clipPath.addEllipse(0, 0, 100, 100)

        p.setRenderHint(QPainter.Antialiasing)
        p.setClipPath(clipPath)
        p.setClipping(False)
        p.setPen(Qt.gray)
        p.drawPath(clipPath)
        self.update()
コード例 #2
0
    def updatePixmap(self, rect):
        """ Update the pixmap for the current transition.

        This method sets a radial clipping region on the output pixmap
        and draws in the relevant portion of the ending pixamp.

        """
        x = rect.x()
        y = rect.y()
        rx = rect.width()
        ry = rect.height()
        path = QPainterPath()
        path.addEllipse(QPointF(x, y), float(rx), float(ry))
        painter = QPainter(self.outPixmap())
        painter.setClipPath(path)
        painter.drawPixmap(QPoint(0, 0), self.endPixmap())
コード例 #3
0
ファイル: mybutton.py プロジェクト: lange5959/qkt_code
    def paintEvent(self, event):
        painter = QPainter(self)
        btnRect = self.geometry()
        iconRect = self.iconSize()

        color = QColor(Qt.black)
        if self.hovered:
            color = self.color
        if self.pressed:
            color = self.color.darker(120)

        painter.setPen(QPen(QColor(Qt.lightGray), 2))
        outline = QPainterPath()
        outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0)
        painter.setOpacity(1)
        painter.drawPath(outline)

        painter.setBrush(QBrush(color))
        painter.setOpacity(self.opacity)
        painter_path = QPainterPath()
        painter_path.addRoundedRect(1, 1,
                                    btnRect.width() - 2,
                                    btnRect.height() - 2, 0, 0)
        if self.hovered:
            painter.setClipPath(painter_path)
            painter.drawRoundedRect(1, 1,
                                    btnRect.width() - 2,
                                    btnRect.height() - 2, 0, 0)

        painter.setOpacity(1)

        iconPos, textPos = self.calIconTextPos(btnRect, iconRect)
        # 重画文本
        if not self.text().isNull():
            painter.setFont(self.font())
            painter.setPen(QPen(QColor(Qt.black), 2))
            painter.drawText(textPos.x(), textPos.y(), textPos.width(),
                             textPos.height(), Qt.AlignCenter, self.text())
            # 重画图标
        if not self.icon().isNull():
            painter.drawPixmap(iconPos,
                               QPixmap(self.icon().pixmap(self.iconSize())))
コード例 #4
0
ファイル: buttons.py プロジェクト: webodf/blink-qt
    def pixmap(self, mode=QIcon.Normal, state=QIcon.Off):
        pixmap = self.icon().pixmap(self.iconSize(), mode, state)
        if pixmap.isNull():
            return pixmap

        size = max(pixmap.width(), pixmap.height())
        offset_x = (size - pixmap.width())/2
        offset_y = (size - pixmap.height())/2

        new_pixmap = QPixmap(size, size)
        new_pixmap.fill(Qt.transparent)
        path = QPainterPath()
        path.addRoundedRect(0, 0, size, size, 3.7, 3.7)
        painter = QPainter(new_pixmap)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setCompositionMode(QPainter.CompositionMode_SourceOver)
        painter.setClipPath(path)
        painter.drawPixmap(offset_x, offset_y, pixmap)
        painter.end()

        return new_pixmap
コード例 #5
0
 def paintEvent(self,event):
     painter = QPainter(self)
     btnRect = self.geometry()
     
     color = QColor(Qt.black)
     if self.hovered:
         color = self.color
     if self.pressed:
         color = self.color.darker(120)
         
     painter.setBrush(QBrush(color)) 
     painter_path = QPainterPath()
     painter_path.addRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0)
     
     if self.hovered:
         painter.setPen(QPen(color,2))
         outline = QPainterPath()
         outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0)
         painter.setOpacity(1)
         painter.drawPath(outline)
         painter.setClipPath(painter_path)
         painter.drawRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0)
     
     iconWidth = self.iconSize().width()*3/5
     iconHeight = self.iconSize().height()*3/5
     iconX = (btnRect.width()-iconWidth)/2
     iconY = (btnRect.height()-iconHeight)/2
     
     if self.pressed:
         iconX += 2
         iconY += 2
     
     iconPos = QRect()
     iconPos.setX(iconX)
     iconPos.setY(iconY)
     iconPos.setWidth(iconWidth)
     iconPos.setHeight(iconHeight)
     
     painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize())))
コード例 #6
0
 def paintEvent(self,event):
     return
     painter = QPainter(self)
     btnRect = self.geometry()
     iconRect = self.iconSize()
     
     color = QColor(Qt.black)
     if self.hovered:
         color = self.color
     if self.pressed:
         color = self.color.darker(120)
     
     painter.setPen(QPen(QColor(Qt.lightGray),2))
     outline = QPainterPath()
     outline.addRoundedRect(0, 0, btnRect.width(), btnRect.height(), 0, 0)
     painter.setOpacity(1)
     painter.drawPath(outline)
    
     painter.setBrush(QBrush(color)) 
     painter.setOpacity(self.opacity)
     painter_path = QPainterPath()
     painter_path.addRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0)
     if self.hovered:
         painter.setClipPath(painter_path)
         painter.drawRoundedRect(1, 1, btnRect.width() - 2, btnRect.height() - 2, 0, 0)
     
     painter.setOpacity(1)       
     
     iconPos,textPos = self.calIconTextPos(btnRect, iconRect)
     # 重画文本
     if not self.text().isNull():
         painter.setFont(self.font())
         painter.setPen(QPen(QColor(Qt.black),2))
         painter.drawText(textPos.x(), textPos.y(), textPos.width(), textPos.height(), Qt.AlignCenter, self.text())
         # 重画图标
     if not self.icon().isNull():
         painter.drawPixmap(iconPos, QPixmap(self.icon().pixmap(self.iconSize())))
コード例 #7
0
    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()