我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用PyQt5.QtCore.QLineF()。
def adjust(self): if not self.source or not self.dest: return line = QLineF(self.mapFromItem(self.source, 0, 0), self.mapFromItem(self.dest, 0, 0)) length = line.length() self.prepareGeometryChange() if length > 20.0: edgeOffset = QPointF((line.dx() * 10) / length, (line.dy() * 10) / length) self.sourcePoint = line.p1() + edgeOffset self.destPoint = line.p2() - edgeOffset else: self.sourcePoint = line.p1() self.destPoint = line.p1()
def _init_border(self): rect = self.scene().sceneRect() pen = QtGui.QPen(QtGui.QColor(self.border_color), self.border_width) lines = [ QtCore.QLineF(rect.topLeft(), rect.topRight()), QtCore.QLineF(rect.topLeft(), rect.bottomLeft()), QtCore.QLineF(rect.topRight(), rect.bottomRight()), QtCore.QLineF(rect.bottomLeft(), rect.bottomRight()) ] for line in lines: self.scene().addLine(line, pen)
def paint(self, painter, option, widget): if not self.source or not self.dest: return # Draw the line itself. line = QLineF(self.sourcePoint, self.destPoint) if line.length() == 0.0: return painter.setPen(QPen(Qt.black, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) painter.drawLine(line) # Draw the arrows if there's enough room. angle = math.acos(line.dx() / line.length()) if line.dy() >= 0: angle = VGraphEdge.TwoPi - angle sourceArrowP1 = self.sourcePoint + QPointF(math.sin(angle + VGraphEdge.Pi / 3) * self.arrowSize, math.cos(angle + VGraphEdge.Pi / 3) * self.arrowSize) sourceArrowP2 = self.sourcePoint + QPointF(math.sin(angle + VGraphEdge.Pi - VGraphEdge.Pi / 3) * self.arrowSize, math.cos(angle + VGraphEdge.Pi - VGraphEdge.Pi / 3) * self.arrowSize); destArrowP1 = self.destPoint + QPointF(math.sin(angle - VGraphEdge.Pi / 3) * self.arrowSize, math.cos(angle - VGraphEdge.Pi / 3) * self.arrowSize) destArrowP2 = self.destPoint + QPointF(math.sin(angle - VGraphEdge.Pi + VGraphEdge.Pi / 3) * self.arrowSize, math.cos(angle - VGraphEdge.Pi + VGraphEdge.Pi / 3) * self.arrowSize) painter.setBrush(Qt.black) painter.drawPolygon(QPolygonF([line.p1(), sourceArrowP1, sourceArrowP2])) painter.drawPolygon(QPolygonF([line.p2(), destArrowP1, destArrowP2]))
def __init__(self, line_or_point, follows=None): super(GuideLine, self).__init__(follows) if isinstance(line_or_point, QLineF): self.line = line_or_point elif follows is not None: self.line = QLineF(self.prevGuide.endPos(), line_or_point) else: self.line = QLineF(QPointF(0, 0), line_or_point)
def setupGuides(self): x = 0 y = 20 self.qtGuide1 = GuideCircle(QRectF(x, y, 260, 260), -36, 342) GuideLine(QPointF(x + 240, y + 268), self.qtGuide1) GuideLine(QPointF(x + 265, y + 246), self.qtGuide1) GuideLine(QPointF(x + 158, y + 134), self.qtGuide1) GuideLine(QPointF(x + 184, y + 109), self.qtGuide1) GuideLine(QPointF(x + 160, y + 82), self.qtGuide1) GuideLine(QPointF(x + 77, y + 163), self.qtGuide1) GuideLine(QPointF(x + 100, y + 190), self.qtGuide1) GuideLine(QPointF(x + 132, y + 159), self.qtGuide1) GuideLine(QPointF(x + 188, y + 211), self.qtGuide1) GuideCircle(QRectF(x + 30, y + 30, 200, 200), -30, 336, GuideCircle.CW, self.qtGuide1) GuideLine(QPointF(x + 238, y + 201), self.qtGuide1) y = 30 self.qtGuide2 = GuideCircle(QRectF(x + 30, y + 30, 200, 200), 135, 270, GuideCircle.CCW) GuideLine(QPointF(x + 222, y + 38), self.qtGuide2) GuideCircle(QRectF(x, y, 260, 260), 135, 270, GuideCircle.CW, self.qtGuide2) GuideLine(QPointF(x + 59, y + 59), self.qtGuide2) x = 115 y = 10 self.qtGuide3 = GuideLine(QLineF(x, y, x + 30, y)) GuideLine(QPointF(x + 30, y + 170), self.qtGuide3) GuideLine(QPointF(x, y + 170), self.qtGuide3) GuideLine(QPointF(x, y), self.qtGuide3) self.qtGuide1.setFence(QRectF(0, 0, 800, 600)) self.qtGuide2.setFence(QRectF(0, 0, 800, 600)) self.qtGuide3.setFence(QRectF(0, 0, 800, 600))
def move(self, item, dest, moveSpeed): walkLine = QLineF(item.getGuidedPos(), dest) if moveSpeed >= 0 and walkLine.length() > moveSpeed: # The item is too far away from it's destination point so we move # it towards it instead. dx = walkLine.dx() dy = walkLine.dy() if abs(dx) > abs(dy): # Walk along x-axis. if dx != 0: d = moveSpeed * dy / abs(dx) if dx > 0: s = moveSpeed else: s = -moveSpeed dest.setX(item.getGuidedPos().x() + s) dest.setY(item.getGuidedPos().y() + d) else: # Walk along y-axis. if dy != 0: d = moveSpeed * dx / abs(dy) if dy > 0: s = moveSpeed else: s = -moveSpeed dest.setX(item.getGuidedPos().x() + d) dest.setY(item.getGuidedPos().y() + s) item.setGuidedPos(dest)
def createStaffLines(self, lengthInPixel = 0): """By default creates 5 stafflines. But it can be 10; 5 extra below the origin-staff. This is NOT a double-system like a piano but just a staff with more lines that happens to have the range of e.g. treble + bass clef.""" def createLine(yOffset): line = QtWidgets.QGraphicsLineItem(QtCore.QLineF(0, 0, lengthInPixel, 0)) line.setParentItem(self) line.setPen(cosmeticPen) self.staffLines.append(line) line.setPos(0, yOffset*constantsAndConfigs.stafflineGap) line.setZValue(-5) #This is the z value within GuiTrack for l in self.staffLines: self.parentScore.removeWhenIdle(l) self.staffLines = [] lengthInPixel += 25 #a bonus that gives the hint that you can write after the last object. for i in range(-2, 3): #the normal 5 line system. We have a lower and upper range/position. The middle line is at position 0 createLine(i) if self.staticExportItem["double"]: #add more stuffs below (user-perspective. positive Qt values) for i in range(4, 9): #i is now 3. Make a gap: createLine(i)
def createBarlines(self, barlinesTickList): """and measure numbers""" for bl in self.barLines: self.parentScore.removeWhenIdle(bl) self.barLines = [] if self.staticExportItem["double"]: h = 10 * constantsAndConfigs.stafflineGap else: h = 4 * constantsAndConfigs.stafflineGap #if barlinesTickList[0] == 0: #happens when there is a metrical instruction at tick 0. # del barlinesTickList[0] last = None offset = 0 for barnumber, barlineTick in enumerate(barlinesTickList): if barlineTick == last: #print ("warning. Double barline at", barlineTick) offset += 1 continue #don't draw the double barline last = barlineTick line = QtWidgets.QGraphicsLineItem(QtCore.QLineF(0, 0, 0, h)) line.setParentItem(self) self.barLines.append(line) line.setPos(barlineTick / constantsAndConfigs.ticksToPixelRatio, -2*constantsAndConfigs.stafflineGap) number = QtWidgets.QGraphicsSimpleTextItem(str(barnumber+1-offset)) number.setScale(0.75) number.setParentItem(line) number.setPos(-2, -3*constantsAndConfigs.stafflineGap) #-2 on X for a little fine tuning.
def drawBackground(self, painter, rect): oldTransform = painter.transform() painter.fillRect(rect, self._backgroundColor) left = int(rect.left()) - (int(rect.left()) % self._gridSizeFine) top = int(rect.top()) - (int(rect.top()) % self._gridSizeFine) # Draw horizontal fine lines gridLines = [] painter.setPen(self._gridPenS) y = float(top) while y < float(rect.bottom()): gridLines.append(QtCore.QLineF(rect.left(), y, rect.right(), y)) y += self._gridSizeFine painter.drawLines(gridLines) # Draw vertical fine lines gridLines = [] painter.setPen(self._gridPenS) x = float(left) while x < float(rect.right()): gridLines.append(QtCore.QLineF(x, rect.top(), x, rect.bottom())) x += self._gridSizeFine painter.drawLines(gridLines) # Draw thick grid left = int(rect.left()) - (int(rect.left()) % self._gridSizeCourse) top = int(rect.top()) - (int(rect.top()) % self._gridSizeCourse) # Draw vertical thick lines gridLines = [] painter.setPen(self._gridPenL) x = left while x < rect.right(): gridLines.append(QtCore.QLineF(x, rect.top(), x, rect.bottom())) x += self._gridSizeCourse painter.drawLines(gridLines) # Draw horizontal thick lines gridLines = [] painter.setPen(self._gridPenL) y = top while y < rect.bottom(): gridLines.append(QtCore.QLineF(rect.left(), y, rect.right(), y)) y += self._gridSizeCourse painter.drawLines(gridLines) return super(GraphView, self).drawBackground(painter, rect)
def paint(self, painter, option, widget=None): """ Method for painting the arrow. """ demat = painter.deviceTransform() self.sc = demat.m11() if self.endp is None: dx = cos(self.angle) * self.length / self.sc dy = sin(self.angle) * self.length / self.sc endp = QtCore.QPointF(self.startp.x() - dx, self.startp.y() + dy) else: endp = QtCore.QPointF(self.endp.x, -self.endp.y) arrowSize = self.arrowSize / self.sc painter.setPen(self.pen) painter.setBrush(self.myColor) self.setLine(QtCore.QLineF(endp, self.startp)) line = self.line() if line.length() != 0: angle = acos(line.dx() / line.length()) if line.dy() >= 0: angle = (pi * 2.0) - angle if self.startarrow: arrowP1 = line.p2() - QtCore.QPointF(sin(angle + pi / 3.0) * arrowSize, cos(angle + pi / 3.0) * arrowSize) arrowP2 = line.p2() - QtCore.QPointF(sin(angle + pi - pi / 3.0) * arrowSize, cos(angle + pi - pi / 3.0) * arrowSize) self.arrowHead.clear() for Point in [line.p2(), arrowP1, arrowP2]: self.arrowHead.append(Point) else: arrowP1 = line.p1() + QtCore.QPointF(sin(angle + pi / 3.0) * arrowSize, cos(angle + pi / 3.0) * arrowSize) arrowP2 = line.p1() + QtCore.QPointF(sin(angle + pi - pi / 3.0) * arrowSize, cos(angle + pi - pi / 3.0) * arrowSize) self.arrowHead.clear() for Point in [line.p1(), arrowP1, arrowP2]: self.arrowHead.append(Point) painter.drawLine(line) painter.drawPolygon(self.arrowHead)
def drawBackground(self, painter: QPainter, rect: QRectF): # freqs = np.fft.fftfreq(len(w), 1 / self.sample_rate) if self.draw_grid and len(self.frequencies) > 0: painter.setPen(QPen(painter.pen().color(), Qt.FlatCap)) parent_width = self.parent().width() if hasattr(self.parent(), "width") else 750 view_rect = self.parent().view_rect() if hasattr(self.parent(), "view_rect") else rect font_width = self.font_metrics.width(Formatter.big_value_with_suffix(self.center_freq) + " ") x_grid_size = int(view_rect.width() / parent_width * font_width) # x_grid_size = int(0.1 * view_rect.width()) if 0.1 * view_rect.width() > 1 else 1 y_grid_size = 1 x_mid = np.where(self.frequencies == 0)[0] x_mid = int(x_mid[0]) if len(x_mid) > 0 else 0 left = int(rect.left()) - (int(rect.left()) % x_grid_size) left = left if left > 0 else 0 top = rect.top() - (rect.top() % y_grid_size) bottom = rect.bottom() - (rect.bottom() % y_grid_size) right_border = int(rect.right()) if rect.right() < len(self.frequencies) else len(self.frequencies) x_range = list(range(x_mid, left, -x_grid_size)) + list(range(x_mid, right_border, x_grid_size)) lines = [QLineF(x, rect.top(), x, bottom) for x in x_range] \ + [QLineF(rect.left(), y, rect.right(), y) for y in np.arange(top, bottom, y_grid_size)] painter.drawLines(lines) scale_x, scale_y = self.__calc_x_y_scale(rect) painter.scale(scale_x, scale_y) counter = -1 # Counter for Label for every second line for x in x_range: freq = self.frequencies[x] counter += 1 if freq != 0 and (counter % 2 != 0): # Label for every second line continue if freq != 0: prefix = "+" if freq > 0 else "" value = prefix+Formatter.big_value_with_suffix(freq, 2) else: counter = 0 value = Formatter.big_value_with_suffix(self.center_freq) font_width = self.font_metrics.width(value) painter.drawText(x / scale_x - font_width / 2, bottom / scale_y, value)