Python源码示例:PyQt5.QtGui.QPolygon()

示例1
def selectpoly_copy(self):
        """
        Copy a polygon region from the current image, returning it.

        Create a mask for the selected area, and use it to blank
        out non-selected regions. Then get the bounding rect of the
        selection and crop to produce the smallest possible image.

        :return: QPixmap of the copied region.
        """
        self.timer_cleanup()

        pixmap = self.pixmap().copy()
        bitmap = QBitmap(*CANVAS_DIMENSIONS)
        bitmap.clear()  # Starts with random data visible.

        p = QPainter(bitmap)
        # Construct a mask where the user selected area will be kept, 
        # the rest removed from the image is transparent.
        userpoly = QPolygon(self.history_pos + [self.current_pos])
        p.setPen(QPen(Qt.color1))
        p.setBrush(QBrush(Qt.color1))  # Solid color, Qt.color1 == bit on.
        p.drawPolygon(userpoly)
        p.end()

        # Set our created mask on the image.
        pixmap.setMask(bitmap)

        # Calculate the bounding rect and return a copy of that region.
        return pixmap.copy(userpoly.boundingRect())

    # Select rectangle events 
示例2
def clip(self, p, w, h):
        """Clip image before painting

        Parameters
        ----------
        p :
            QPainter object used for painting
        w :
            image width
        h :
            image height

        Returns
        -------

        """
        if self.clip_mask is not None:
            if isinstance(self.clip_mask, float):  # centered circle
                a = QRegion(
                    w / 2 - self.clip_mask * w,
                    h / 2 - self.clip_mask * h,
                    self.clip_mask * w * 2,
                    self.clip_mask * h * 2,
                    type=QRegion.Ellipse,
                )
                p.setClipRegion(a)
            elif isinstance(self.clip_mask[0], tuple):  # polygon
                points = [QPoint(int(w * x), int(h * y)) for (x, y) in self.clip_mask]
                p.setClipRegion(QRegion(QPolygon(points)))
            else:
                p.setClipRect(
                    self.clip_mask[0] * w,
                    self.clip_mask[1] * h,
                    self.clip_mask[2] * w,
                    self.clip_mask[3] * h,
                ) 
示例3
def draw_block(self, p, point, w, h):
        # Painting settings:
        p.setPen(Qt.NoPen)
        p.setRenderHint(QPainter.Antialiasing)

        # Here for changing black with another color (to be debugged)
        # p.setBrush(QBrush(QColor(*self.color_2)))
        # # p.drawRect(QRect(-1, -1, (w + 2)*1.5, (h + 2)*1.5))

        p.setBrush(QBrush(QColor(*self.color)))

        # To draw a windmill, a set of consecutive triangles will be painted:
        mid_x = int(w / 2)  # calculate image center
        mid_y = int(h / 2)

        # calculate angles for each triangle:
        angles = np.arange(0, np.pi * 2, (np.pi * 2) / self.n_arms)
        angles += np.pi / 2 + np.pi / (2 * self.n_arms)
        # angular width of the white arms, by default equal to dark ones
        size = np.pi / self.n_arms
        # radius of triangles (much larger than frame)
        rad = (w ** 2 + h ** 2) ** (1 / 2)
        # loop over angles and draw consecutive triangles
        for deg in np.array(angles):
            polyg_points = [
                QPoint(mid_x, mid_y),
                QPoint(int(mid_x + rad * np.cos(deg)), int(mid_y + rad * np.sin(deg))),
                QPoint(
                    int(mid_x + rad * np.cos(deg + size)),
                    int(mid_y + rad * np.sin(deg + size)),
                ),
            ]
            polygon = QPolygon(polyg_points)
            p.drawPolygon(polygon) 
示例4
def draw_connection_line(painter, x1, y1, x2, y2, h1, h2):
        # Account for list view headers.
        y1 += h1
        y2 += h2

        # Invisible output ports don't get a connecting dot.
        if y1 > h1:
            painter.drawLine(x1, y1, x1 + 4, y1)

        # Setup control points
        spline = QPolygon(4)
        cp = int((x2 - x1 - 8) * 0.4)
        spline.setPoints(x1 + 4, y1,
                         x1 + 4 + cp, y1,
                         x2 - 4 - cp, y2,
                         x2 - 4, y2)
        # The connection line
        path = QPainterPath()
        path.moveTo(spline.at(0))
        path.cubicTo(spline.at(1), spline.at(2), spline.at(3))
        painter.strokePath(path, painter.pen())

        # painter.drawLine(x1 + 4, y1, x2 - 4, y2)

        # Invisible input ports don't get a connecting dot.
        if y2 > h2:
            painter.drawLine(x2 - 4, y2, x2, y2) 
示例5
def paint(self, p, w, h):
        p.setPen(Qt.NoPen)
        p.setBrush(QBrush(QColor(*self.color)))
        p.setRenderHint(QPainter.Antialiasing)

        points = []
        if self.left:
            dtheta = np.pi / 2
        else:
            dtheta = -np.pi / 2

        theta = self.theta

        sx = (
            self.x
            + h / 2 * np.cos(theta)
            + self.center_dist * np.cos(theta - np.pi / 2)
        )
        sy = (
            self.y
            + h / 2 * np.sin(theta)
            + self.center_dist * np.sin(theta - np.pi / 2)
        )
        points.append(QPoint(sx, sy))
        theta += dtheta

        sx += w * np.cos(theta)
        sy += w * np.sin(theta)
        points.append(QPoint(sx, sy))
        theta += dtheta

        sx += h * np.cos(theta)
        sy += h * np.sin(theta)
        points.append(QPoint(sx, sy))
        theta += dtheta

        sx += w * np.cos(theta)
        sy += w * np.sin(theta)
        points.append(QPoint(sx, sy))
        theta += dtheta

        sx += h * np.cos(theta)
        sy += h * np.sin(theta)
        points.append(QPoint(sx, sy))

        poly = QPolygon(points)
        p.drawPolygon(poly)