Exemplo n.º 1
0
def nearestPosOnRect(pos: QtCore.QPoint, rect: QtCore.QRect):
    """
    Finds the position on a rectangle nearest to the given point.
    """

    # Bounds: xmax, ymax, xmin, ymin
    bounds = (rect.right(), rect.bottom(), rect.left(), rect.top())

    # Line origin point
    c = rect.center()

    # Clipped line: returns (x1, y1, x2, y2)
    clipped = cohenSutherlandClip(c.x(), c.y(), pos.x(), pos.y(), *bounds)

    # "clipped" should only be None if both points lie outside the
    # bounding area. This should never happen, since one point
    # is always the center of the area.
    assert clipped is not None

    # The nearest position to the given point, on the rectangle, is
    # the second point returned by the clipping function.
    _, _, x2, y2 = clipped
    return QtCore.QPoint(x2, y2)
Exemplo n.º 2
0
    def fit_solution_in_view(self, solution):
        if not solution.rect:
            return

        solution_rect = QRect(
            QPoint(solution.rect.left, solution.rect.top),
            QPoint(solution.rect.right, solution.rect.bottom))

        viewport_rect = self.viewport.rect()

        # compute zoom level
        if solution_rect.width() > solution_rect.height():
            zoom = viewport_rect.width() / solution_rect.width(
            ) if viewport_rect.width() else 1
        else:
            zoom = viewport_rect.height() / solution_rect.height(
            ) if viewport_rect.height() else 1

        self.zoom = 0.80 * zoom if zoom else 1
        self.zoom = clamp(self.zoom, MIN_ZOOM, MAX_ZOOM)  # limit zoom level

        # center view on rect center
        self.pos = -QPointF(solution_rect.center())
Exemplo n.º 3
0
 def draw_icon(self, painter: QPainter, rect: QRect, icon: QIcon):
     r = QRect(0, 0, 20, 20)
     r.moveCenter(rect.center())
     painter.drawPixmap(r, icon.pixmap(20, 20))