Beispiel #1
0
    def paintEvent(self, event):
        rect = QRect(10, 20, 80, 60)

        path = QPainterPath()
        path.moveTo(20, 80)
        path.lineTo(20, 30)
        path.cubicTo(80, 0, 50, 50, 80, 80)

        startAngle = 30 * 16
        arcLength = 120 * 16

        painter = QPainter(self)
        painter.setPen(self.pen)
        painter.setBrush(self.brush)
        if self.antialiased:
            painter.setRenderHint(QPainter.Antialiasing)

        for x in range(0, self.width(), 100):
            for y in range(0, self.height(), 100):
                painter.save()
                painter.translate(x, y)
                if self.transformed:
                    painter.translate(50, 50)
                    painter.rotate(60.0)
                    painter.scale(0.6, 0.9)
                    painter.translate(-50, -50)

                if self.shape == RenderArea.Line:
                    painter.drawLine(rect.bottomLeft(), rect.topRight())
                elif self.shape == RenderArea.Points:
                    painter.drawPoints(RenderArea.points)
                elif self.shape == RenderArea.Polyline:
                    painter.drawPolyline(RenderArea.points)
                elif self.shape == RenderArea.Polygon:
                    painter.drawPolygon(RenderArea.points)
                elif self.shape == RenderArea.Rect:
                    painter.drawRect(rect)
                elif self.shape == RenderArea.RoundedRect:
                    painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize)
                elif self.shape == RenderArea.Ellipse:
                    painter.drawEllipse(rect)
                elif self.shape == RenderArea.Arc:
                    painter.drawArc(rect, startAngle, arcLength)
                elif self.shape == RenderArea.Chord:
                    painter.drawChord(rect, startAngle, arcLength)
                elif self.shape == RenderArea.Pie:
                    painter.drawPie(rect, startAngle, arcLength)
                elif self.shape == RenderArea.Path:
                    painter.drawPath(path)
                elif self.shape == RenderArea.Text:
                    painter.drawText(rect, Qt.AlignCenter,
                                     "PySide 2\nQt %s" % qVersion())
                elif self.shape == RenderArea.Pixmap:
                    painter.drawPixmap(10, 10, self.pixmap)

                painter.restore()

        painter.setPen(self.palette().dark().color())
        painter.setBrush(Qt.NoBrush)
        painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1))
    def drawValue(self, p: QPainter, baseRect: QRectF, value: float,
                  delta: float):
        if value == self.m_min:
            return

        if self.m_barStyle == self.BarStyle.EXPAND:
            p.setBrush(self.palette().highlight())
            p.setPen(QPen(self.palette().shadow().color(),
                          self.m_dataPenWidth))
            radius = (baseRect.height() / 2) / delta
            p.drawEllipse(baseRect.center(), radius, radius)
            return
        elif self.m_barStyle == self.BarStyle.LINE:
            p.setPen(
                QPen(self.palette().highlight().color(), self.m_dataPenWidth))
            p.setBrush(Qt.NoBrush)

            if value == self.m_max:
                p.drawEllipse(
                    baseRect.adjusted(self.m_outlinePenWidth / 2,
                                      self.m_outlinePenWidth / 2,
                                      -self.m_outlinePenWidth / 2,
                                      -self.m_outlinePenWidth / 2))
            else:
                arcLength = 360 / delta
                p.drawArc(
                    baseRect.adjusted(self.m_outlinePenWidth / 2,
                                      self.m_outlinePenWidth / 2,
                                      -self.m_outlinePenWidth / 2,
                                      -self.m_outlinePenWidth / 2),
                    int(self.m_nullPosition * 16), int(-arcLength * 16))

            return

        dataPath = QPainterPath()
        dataPath.setFillRule(Qt.WindingFill)

        if value == self.m_max:
            dataPath.addEllipse(baseRect)
        else:
            arcLength = 360 / delta
            dataPath.moveTo(baseRect.center())
            dataPath.arcTo(baseRect, self.m_nullPosition, -arcLength)
            dataPath.lineTo(baseRect.center())

        p.setBrush(self.palette().highlight())
        p.setPen(QPen(self.palette().shadow().color(), self.m_dataPenWidth))
        p.drawPath(dataPath)
Beispiel #3
0
    def paintEvent(self, pe) -> None:
        if not self.inhibit_paint:

            extent = 1.5 * np.pi
            offset = 1.25 * np.pi

            painter = QPainter(self)

            # So that we can use the background color
            painter.setBackgroundMode(Qt.OpaqueMode)

            # Smooth out the circle
            painter.setRenderHint(QPainter.Antialiasing)

            # Use background color
            bgColor = painter.background().color()
            painter.setBrush(painter.background())
            if self._text not in implementedKnobs:
                painter.setBrush(QtGui.QBrush(QtGui.QColor(int("0xcccccc",
                                                               0))))

            # Store color from stylesheet, pen will be overridden
            pointColor = QColor(painter.pen().color())

            # print(QDial.width(self), QDial.height(self))

            # draw widget borders
            pen = QPen(QColor(self._ringColor), 1)
            pen.setCapStyle(Qt.SquareCap)
            painter.setPen(pen)
            # uncomment the following line to draw outer rect
            # painter.drawRect(0, 0, np.floor(QDial.width(self)), QDial.height(self))

            # No border
            painter.setPen(QPen(Qt.NoPen))

            # the heignt of the widget is 2*radius + 2*fontsize1 + 2*fontsize2
            # where fontsize1 = .4radius and fontsize2 = .9*.4*radius
            # so QDial.height = radius * (2+.4*2+.4*.9*2)
            #

            fontsize1factor = .4
            fontsize2reduction = .9
            fontsize2factor = fontsize1factor * fontsize2reduction

            center_x = QDial.width(self) / 2.0
            center_y = QDial.height(self) / 2.0

            if not self._hasFixedSize:
                if not self._hasFixedFontSize:
                    radius = np.min(
                        (QDial.width(self) / 2. - self._knobMargin,
                         QDial.height(self) /
                         (2. + 2 * fontsize1factor + 2 * fontsize2factor) -
                         self._knobMargin))
                    radius = np.max((radius, 1))
                    # print("Radius = ", radius, ", height = ", QDial.height(self), ", width = ", QDial.width(self))
                    center_y = center_y - radius * (fontsize1factor +
                                                    fontsize2factor)
                else:
                    radius = np.min(
                        (QDial.width(self) / 2. - self._knobMargin,
                         (QDial.height(self) - 4 * self._fixedFontSize) / 2. -
                         self._knobMargin))
                    radius = np.max((radius, 1))
                    center_y = center_y - (self._fixedFontSize *
                                           (1 + fontsize2reduction))
            else:
                radius = self._fixedSize / 2.
                radius = np.max((radius, 1))
                center_y = center_y - radius * (fontsize1factor +
                                                fontsize2factor)

            self.radius = radius

            # Draw arc
            rectangle = QtCore.QRectF(center_x - radius, center_y - radius,
                                      2 * radius, 2 * radius)
            """The startAngle and spanAngle must be specified in 1/16th of a degree, 
			i.e. a full circle equals 5760 (16 * 360). 
			Positive values for the angles mean counter-clockwise 
			while negative values mean the clockwise direction. 
			Zero degrees is at the 3 o'clock position."""

            linewidth = radius / 30. * 2

            # linewidth = 1
            pen = QPen(QColor(self._ringColor), linewidth)
            pen.setCapStyle(Qt.RoundCap)
            # pen.setCapStyle(Qt.FlatCap)

            painter.setPen(pen)

            # adapt to linewidth to make it more pleasant to the eye
            capRadius = linewidth / 4
            angleCap = np.arcsin(capRadius / radius)

            start_deg = (90 - np.rad2deg(extent / 2)) + np.rad2deg(angleCap)
            start_16deg = start_deg * 16

            extent_deg = np.rad2deg(extent) - 2 * np.rad2deg(angleCap)
            extent_16deg = extent_deg * 16

            painter.drawArc(rectangle, start_16deg, extent_16deg)

            #draw inner circle
            pen = QPen(QColor(pointColor), linewidth)
            pen.setCapStyle(Qt.RoundCap)

            painter.setPen(pen)
            painter.setBrush(QtGui.QColor(bgColor))

            radius_inner = 15. / 20. * radius
            painter.drawEllipse(QtCore.QPointF(center_x, center_y),
                                radius_inner, radius_inner)

            self.center = QtCore.QPointF(center_x, center_y)
            """
			# Get ratio between current value and maximum to calculate angle
			if (param != NULL):
				if (param->value != this->value()) 
					param->setValue(this->value())
			"""
            ratio = (QDial.value(self) - QDial.minimum(self)) / (
                QDial.maximum(self) - QDial.minimum(self))

            # The maximum amount of degrees is 270, offset by 225
            angle = ratio * extent - offset

            # Draw the indicator
            painter.setBrush(QBrush(pointColor))

            a_y = center_y + np.sin(angle) * (radius - .1)
            a_x = center_x + np.cos(angle) * (radius - .1)

            pen = QPen(pointColor, linewidth)
            pen.setCapStyle(Qt.RoundCap)
            painter.setPen(pen)

            painter.drawLine(a_x, a_y, np.round(center_x), center_y)

            if not self._hasFixedFontSize:
                fontsize1 = radius * fontsize1factor
                if self.sizeType == 1 and fontsize1 != Knob.fontsize1:
                    Knob.fontsize1 = fontsize1
                else:
                    fontsize1 = Knob.fontsize1
                fontsize2 = fontsize1 * fontsize2reduction
            else:
                fontsize1 = self._fixedFontSize
                fontsize2 = fontsize1 * fontsize2reduction

            self.fontsize1 = fontsize1

            textRect_ = QtCore.QRectF(0, center_y + radius, QDial.width(self),
                                      2 * fontsize1)

            if self.coloredTitle:
                painter.setPen(QColor(int(titleColor, 0)))

            f = painter.font()
            f.setPointSizeF(fontsize1)
            painter.setFont(f)
            # painter.drawRect(textRect_)
            painter.drawText(textRect_, Qt.AlignHCenter | Qt.AlignTop,
                             self._text)
            # painter.drawText(textRect_, Qt.AlignHCenter | Qt.AlignTop, str(fontsize1))

            textRect_ = QtCore.QRectF(0, center_y + radius + fontsize1 * 2,
                                      QDial.width(self), 2 * fontsize2)

            if self.hasFocus():
                painter.setPen(QtGui.QColor("red"))

            f.setPointSizeF(fontsize2)
            painter.setFont(f)
            # painter.drawRect(textRect_)
            painter.drawText(textRect_, Qt.AlignHCenter | Qt.AlignTop,
                             str(QDial.value(self)))

            painter.end()