def qwtDrawStar1Symbols(painter, points, numPoints, symbol): size = symbol.size() painter.setPen(symbol.pen()) sqrt1_2 = np.sqrt(0.5) r = QRectF(0, 0, size.width(), size.height()) for pos in points: r.moveCenter(pos.toPoint()) c = QPointF(r.center()) d1 = r.width() / 2.0 * (1.0 - sqrt1_2) painter.drawLine(r.left() + d1, r.top() + d1, r.right() - d1, r.bottom() - d1) painter.drawLine(r.left() + d1, r.bottom() - d1, r.right() - d1, r.top() + d1) painter.drawLine(c.x(), r.top(), c.x(), r.bottom()) painter.drawLine(r.left(), c.y(), r.right(), c.y())
def round_position(point: QPointF, pixels=5): """ 圆整位置。 :param point: :param pixels:圆整的单位(最好是1,2,5,10,20,...) :return: """ x, y = point.x(), point.y() x_cor, y_cor = round(x * 1.0 / pixels) * pixels, round(y * 1.0 / pixels) * pixels return QPointF(x_cor, y_cor)
def set_zoom(self, zoom: int) -> None: """Update zoom factor.""" zoom_old = self.zoom self.zoom = zoom / 50. zoom_old -= self.zoom if self.zoomby == ZoomBy.CANVAS: pos = self.mapFromGlobal(QCursor.pos()) else: pos = QPointF(self.width() / 2, self.height() / 2) self.ox += (pos.x() - self.ox) / self.zoom * zoom_old self.oy += (pos.y() - self.oy) / self.zoom * zoom_old self.update()
def paint(self, painter, option, widget): arc_rect = 10 connector_length = 5 painter.setPen(self.pen) path = QtGui.QPainterPath() if self.source.x() == self.dest.x(): path.moveTo(self.source.x(), self.source.y()) path.lineTo(self.dest.x(), self.dest.y()) painter.drawPath(path) else: # Define points starting from source point1 = QPointF(self.source.x(), self.source.y()) point2 = QPointF(point1.x(), point1.y() - connector_length) point3 = QPointF(point2.x() + arc_rect, point2.y() - arc_rect) # Define points starting from dest point4 = QPointF(self.dest.x(), self.dest.y()) point5 = QPointF(point4.x(), point3.y() - arc_rect) point6 = QPointF(point5.x() - arc_rect, point5.y() + arc_rect) start_angle_arc1 = 180 span_angle_arc1 = 90 start_angle_arc2 = 90 span_angle_arc2 = -90 # If the dest is at the left of the source, then we # need to reverse some values if self.source.x() > self.dest.x(): point5 = QPointF(point4.x(), point4.y() + connector_length) point6 = QPointF(point5.x() + arc_rect, point5.y() + arc_rect) point3 = QPointF(self.source.x() - arc_rect, point6.y()) point2 = QPointF(self.source.x(), point3.y() + arc_rect) span_angle_arc1 = 90 path.moveTo(point1) path.lineTo(point2) path.arcTo(QRectF(point2, point3), start_angle_arc1, span_angle_arc1) path.lineTo(point6) path.arcTo(QRectF(point6, point5), start_angle_arc2, span_angle_arc2) path.lineTo(point4) painter.drawPath(path)
def qwtDrawSvgSymbols(painter, points, numPoints, renderer, symbol): if renderer is None or not renderer.isValid(): return viewBox = QRectF(renderer.viewBoxF()) if viewBox.isEmpty(): return sz = QSizeF(symbol.size()) if not sz.isValid(): sz = viewBox.size() sx = sz.width() / viewBox.width() sy = sz.height() / viewBox.height() pinPoint = QPointF(viewBox.center()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() dx = sx * (pinPoint.x() - viewBox.left()) dy = sy * (pinPoint.y() - viewBox.top()) for pos in points: x = pos.x() - dx y = pos.y() - dy renderer.render(painter, QRectF(x, y, sz.width(), sz.height()))
def qwtDrawGraphicSymbols(painter, points, numPoint, graphic, symbol): pointRect = QRectF(graphic.controlPointRect()) if pointRect.isEmpty(): return sx = 1.0 sy = 1.0 sz = symbol.size() if sz.isValid(): sx = sz.width() / pointRect.width() sy = sz.height() / pointRect.height() pinPoint = QPointF(pointRect.center()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() transform = QTransform(painter.transform()) for pos in points: tr = QTransform(transform) tr.translate(pos.x(), pos.y()) tr.scale(sx, sy) tr.translate(-pinPoint.x(), -pinPoint.y()) painter.setTransform(tr) graphic.render(painter) painter.setTransform(transform)
class QCircRectF(QRectF): def __init__(self, center=(0.0, 0.0), radius=1.0, rect=None): self._scale = 1.0 if rect is not None: self.center = rect.center() super(QCircRectF, self).__init__(rect) else: self.center = QPointF(*center) left = self.center.x() - radius top = self.center.y() - radius bottom = self.center.y() + radius right = self.center.x() + radius super(QCircRectF, self).__init__(QPointF(left, top), QPointF(right, bottom)) @property def scale(self): return self._scale @scale.setter def scale(self, value): self._scale = value self.radius *= value self.setLeft(self.center.x() - self._radius) self.setTop(self.center.y() - self._radius) self.setBottom(self.center.y() + self._radius) self.setRight(self.center.x() + self._radius) @property def radius(self): return (self.right() - self.left()) * 0.5 @radius.setter def radius(self, radius): self.setLeft(self.center.x() - radius) self.setTop(self.center.y() - radius) self.setBottom(self.center.y() + radius) self.setRight(self.center.x() + radius)
def drawLabel(self, painter, canvasRect, pos): """ Align and draw the text label of the marker :param QPainter painter: Painter :param QRectF canvasRect: Contents rectangle of the canvas in painter coordinates :param QPointF pos: Position of the marker, translated into widget coordinates .. seealso:: :py:meth:`drawLabel()`, :py:meth:`qwt.symbol.QwtSymbol.drawSymbol()` """ if self.__data.label.isEmpty(): return align = Qt.Alignment(self.__data.labelAlignment) alignPos = QPointF(pos) symbolOff = QSizeF(0, 0) if self.__data.style == QwtPlotMarker.VLine: # In VLine-style the y-position is pointless and # the alignment flags are relative to the canvas if bool(self.__data.labelAlignment & Qt.AlignTop): alignPos.setY(canvasRect.top()) align &= ~Qt.AlignTop align |= Qt.AlignBottom elif bool(self.__data.labelAlignment & Qt.AlignBottom): # In HLine-style the x-position is pointless and # the alignment flags are relative to the canvas alignPos.setY(canvasRect.bottom() - 1) align &= ~Qt.AlignBottom align |= Qt.AlignTop else: alignPos.setY(canvasRect.center().y()) elif self.__data.style == QwtPlotMarker.HLine: if bool(self.__data.labelAlignment & Qt.AlignLeft): alignPos.setX(canvasRect.left()) align &= ~Qt.AlignLeft align |= Qt.AlignRight elif bool(self.__data.labelAlignment & Qt.AlignRight): alignPos.setX(canvasRect.right() - 1) align &= ~Qt.AlignRight align |= Qt.AlignLeft else: alignPos.setX(canvasRect.center().x()) else: if self.__data.symbol and self.__data.symbol.style( ) != QwtSymbol.NoSymbol: symbolOff = self.__data.symbol.size() + QSizeF(1, 1) symbolOff /= 2 pw2 = self.__data.pen.widthF() / 2.0 if pw2 == 0.0: pw2 = 0.5 spacing = self.__data.spacing xOff = max([pw2, symbolOff.width()]) yOff = max([pw2, symbolOff.height()]) textSize = self.__data.label.textSize(painter.font()) if align & Qt.AlignLeft: alignPos.setX(alignPos.x() - (xOff + spacing)) if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height()) else: alignPos.setX(alignPos.x() - textSize.width()) elif align & Qt.AlignRight: alignPos.setX(alignPos.x() + xOff + spacing) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setX(alignPos.x() - textSize.height() / 2) else: alignPos.setX(alignPos.x() - textSize.width() / 2) if align & Qt.AlignTop: alignPos.setY(alignPos.y() - (yOff + spacing)) if self.__data.labelOrientation != Qt.Vertical: alignPos.setY(alignPos.y() - textSize.height()) elif align & Qt.AlignBottom: alignPos.setY(alignPos.y() + yOff + spacing) if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width()) else: if self.__data.labelOrientation == Qt.Vertical: alignPos.setY(alignPos.y() + textSize.width() / 2) else: alignPos.setY(alignPos.y() - textSize.height() / 2) painter.translate(alignPos.x(), alignPos.y()) if self.__data.labelOrientation == Qt.Vertical: painter.rotate(-90.0) textRect = QRectF(0, 0, textSize.width(), textSize.height()) self.__data.label.draw(painter, textRect)
def round_position(point: QPointF, pixels=5): x, y = point.x(), point.y() x_cor, y_cor = round(x * 1.0 / pixels) * pixels, round( y * 1.0 / pixels) * pixels return QPointF(x_cor, y_cor)
class ConnectionGeometry: def __init__(self, style): # local object coordinates self._in = QPointF(0, 0) self._out = QPointF(0, 0) # self._animationPhase = 0 self._line_width = 3.0 self._hovered = False self._point_diameter = style.connection.point_diameter def get_end_point(self, port_type: PortType) -> QPointF: """ Get end point Parameters ---------- port_type : PortType Returns ------- value : QPointF """ assert port_type != PortType.none return (self._out if port_type == PortType.output else self._in) def set_end_point(self, port_type: PortType, point: QPointF): """ Set end point Parameters ---------- port_type : PortType point : QPointF """ if port_type == PortType.output: self._out = point elif port_type == PortType.input: self._in = point else: raise ValueError(port_type) def move_end_point(self, port_type: PortType, offset: QPointF): """ Move end point Parameters ---------- port_type : PortType offset : QPointF """ if port_type == PortType.output: self._out += offset elif port_type == PortType.input: self._in += offset else: raise ValueError(port_type) @property def bounding_rect(self) -> QRectF: """ Bounding rect Returns ------- value : QRectF """ c1, c2 = self.points_c1_c2() basic_rect = QRectF(self._out, self._in).normalized() c1c2_rect = QRectF(c1, c2).normalized() common_rect = basic_rect.united(c1c2_rect) corner_offset = QPointF(self._point_diameter, self._point_diameter) common_rect.setTopLeft(common_rect.topLeft() - corner_offset) common_rect.setBottomRight(common_rect.bottomRight() + 2 * corner_offset) return common_rect def points_c1_c2(self) -> tuple: """ Connection points (c1, c2) Returns ------- c1: QPointF The first point c2: QPointF The second point """ x_distance = self._in.x() - self._out.x() default_offset = 200.0 x_offset = min((default_offset, abs(x_distance))) y_offset = 0 x_ratio = 0.5 if x_distance <= 0: y_distance = self._in.y() - self._out.y() + 20 y_direction = (-1.0 if y_distance < 0 else 1.0) y_offset = y_direction * min((default_offset, abs(y_distance))) x_ratio = 1.0 x_offset *= x_ratio return (QPointF(self._out.x() + x_offset, self._out.y() + y_offset), QPointF(self._in.x() - x_offset, self._in.y() - y_offset)) @property def source(self) -> QPointF: """ Source Returns ------- value : QPointF """ return self._out @property def sink(self) -> QPointF: """ Sink Returns ------- value : QPointF """ return self._in def line_width(self) -> float: """ Line width Returns ------- value : double """ return self._line_width @property def hovered(self) -> bool: """ Hovered Returns ------- value : bool """ return self._hovered @hovered.setter def hovered(self, hovered: bool): self._hovered = hovered