我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用PyQt4.QtCore.QLineF()。
def setEndPos(self, endpos): '''Callback method to keep `pos2` up to date. :param endpos: `QtCore.QPointF()`. ''' rect = self.srcConnector.rect() if self.dstConnector is not None: pos = self.mapFromItem( self.dstConnector, self.dstConnector.position ) self.pos2 = QtCore.QPointF(pos.x() + rect.width()/2, pos.y() + rect.height()/2 ) else: self.pos2 = endpos self.arcLine.setLine(QtCore.QLineF(self.pos1, self.pos2)) self.setName(self.name) #------------------------------------------------------------------------------------------------
def setBeginPos(self, pos1): '''Callback method to keep `pos1` up to date. :param pos1: `QtCore.QPointF()`. ''' rect = self.srcConnector.rect() pos = self.mapFromItem( self.srcConnector, self.srcConnector.position ) self.pos1 = QtCore.QPointF(pos.x() + rect.width()/2, pos.y() + rect.height()/2 ) # self.pos1 = pos1 self.setName(self.name) self.arcLine.setLine(QtCore.QLineF(self.pos1, self.pos2)) #------------------------------------------------------------------------------------------------
def __init__(self): super(MyArrow, self).__init__() self.source = QPointF(0, 250) self.dest = QPointF(120, 120) self.line = QLineF(self.source, self.dest) self.line.setLength(self.line.length() - 20)
def _setupGrid(self): def do_them(lines, width=self.line_width, grid2=False): for line in lines: color = QtGui.QColor() color.setNamedColor(line.style) pen = QtGui.QPen(color, width) pen.setJoinStyle(QtCore.Qt.RoundJoin) pen.setCapStyle(QtCore.Qt.RoundCap) start = self.mapFromLonLat(QtCore.QPointF(line.coordinates[0][1], line.coordinates[0][0])) for pt in range(1, len(line.coordinates)): end = self.mapFromLonLat(QtCore.QPointF(line.coordinates[pt][1], line.coordinates[pt][0])) ln = QtGui.QGraphicsLineItem(QtCore.QLineF(start, end)) ln.setPen(pen) ln.setZValue(0) self.addItem(ln) if grid2: self._gridGroup2.addToGroup(ln) else: self._gridGroup.addToGroup(ln) start = end return self.lines = Grid() do_them(self.lines.lines) self.grid_lines = len(self.lines.lines) lines = Grid_Boundary() if len(lines.lines) > 0: lines.lines[0].style = self.colors['grid_boundary'] do_them(lines.lines, width=0) if self.existing_grid2: lines2 = Grid(grid2=True) do_them(lines2.lines, grid2=True)
def addLine(self): l=myLineItem(QtCore.QLineF(0,0,2000,-200)) self.addItem(l)
def updatePosition(self): line = QtCore.QLineF(self.mapFromItem(self.myStartItem, 0, 0), self.mapFromItem(self.myEndItem, 0, 0)) self.setLine(line)
def mousePressEvent(self, mouseEvent): if (mouseEvent.button() != QtCore.Qt.LeftButton): return if self.myMode == self.InsertItem: item = DiagramItem(self.myItemType, self.myItemMenu) item.setBrush(self.myItemColor) self.addItem(item) item.setPos(mouseEvent.scenePos()) self.itemInserted.emit(item) elif self.myMode == self.InsertLine: self.line = QtGui.QGraphicsLineItem(QtCore.QLineF(mouseEvent.scenePos(), mouseEvent.scenePos())) self.line.setPen(QtGui.QPen(self.myLineColor, 2)) self.addItem(self.line) elif self.myMode == self.InsertText: textItem = DiagramTextItem() textItem.setFont(self.myFont) textItem.setTextInteractionFlags(QtCore.Qt.TextEditorInteraction) textItem.setZValue(1000.0) textItem.lostFocus.connect(self.editorLostFocus) textItem.selectedChange.connect(self.itemSelected) self.addItem(textItem) textItem.setDefaultTextColor(self.myTextColor) textItem.setPos(mouseEvent.scenePos()) self.textInserted.emit(textItem) super(DiagramScene, self).mousePressEvent(mouseEvent)
def mouseMoveEvent(self, mouseEvent): if self.myMode == self.InsertLine and self.line: newLine = QtCore.QLineF(self.line.line().p1(), mouseEvent.scenePos()) self.line.setLine(newLine) elif self.myMode == self.MoveItem: super(DiagramScene, self).mouseMoveEvent(mouseEvent)
def __init__(self): super(RectLikeLine, self).__init__() # self._line = QtCore.QLineF() self._pen_width = 0.0
def getClosestPoint( self, poly, pt ): closest = (-1,-1) distTh = 4.0 dist = 1e9 # should be enough for i in range(poly.size()): curDist = self.ptDist(poly[i],pt) if curDist < dist: closest = (i,i) dist = curDist # Close enough? if dist <= distTh: return closest # Otherwise see if the polygon is closed, but a line is close enough if self.drawPolyClosed and poly.size() >= 2: for i in range(poly.size()): pt1 = poly[i] j = i+1 if j == poly.size(): j = 0 pt2 = poly[j] edge = QtCore.QLineF(pt1,pt2) normal = edge.normalVector() normalThroughMouse = QtCore.QLineF( pt.x(),pt.y(),pt.x()+normal.dx(),pt.y()+normal.dy() ) intersectionPt = QtCore.QPointF() intersectionType = edge.intersect( normalThroughMouse , intersectionPt ) if intersectionType == QtCore.QLineF.BoundedIntersection: curDist = self.ptDist(intersectionPt,pt) if curDist < dist: closest = (i,j) dist = curDist # Close enough? if dist <= distTh: return closest # If we didnt return yet, we didnt find anything return (-1,-1) # Get distance between two points
def ptDist( self, pt1, pt2 ): # A line between both line = QtCore.QLineF( pt1 , pt2 ) # Length lineLength = line.length() return lineLength # Determine if the given point closes the drawn polygon (snapping)
def mousePressEvent(self, event): item = self.itemAt(event.scenePos()) if event.button() == QtCore.Qt.LeftButton and (isinstance(item, ConnectRigNode)): self.line = QtGui.QGraphicsLineItem(QtCore.QLineF(event.scenePos(), event.scenePos())) self.addItem(self.line) #if item is None: # cmds.select(clear=True) super(SceneView, self).mousePressEvent(event)
def mouseMoveEvent(self, event): if self.line: newLine = QtCore.QLineF(self.line.line().p1(), event.scenePos()) self.line.setLine(newLine) super(SceneView, self).mouseMoveEvent(event) self.update()
def mouseReleaseEvent(self, event): if self.line: startItems = self.items(self.line.line().p1()) if len(startItems) and startItems[0] == self.line: startItems.pop(0) endItems = self.items(self.line.line().p2()) if len(endItems) and endItems[0] == self.line: endItems.pop(0) self.removeItem(self.line) # If this is true a successful line was created if self.connectionTest(startItems, endItems): if len(startItems[0].connectedLine) is not 0: #remove the start item node from the original end item's list of child nodes originalEndNode = startItems[0].connectedLine[0].myEndItem.parentItem() originalEndNode.childNodes[:] = [n for n in originalEndNode.childNodes if n is not startItems[0].parentItem()] startItems[0].connectedLine[0].deleteLine() endItems[0].parentItem().childNodes.append(startItems[0].parentItem()) # Creates a line that is basically of 0 length, just to put a line into the scene connectionLine = lc.LineClass(startItems[0], endItems[0], QtCore.QLineF(startItems[0].scenePos(), endItems[0].scenePos())) self.addItem(connectionLine) # Now use that previous line created and update its position, giving it the proper length and etc... connectionLine.updatePosition() # Sending the data downstream. The start item is the upstream node ALWAYS. The end item is the downstream node ALWAYS. #connectionLine.getEndItem().getWidgetMenu().receiveFrom(connectionLine.getStartItem(), delete=False) #connectionLine.getStartItem().getWidgetMenu().sendData(connectionLine.getStartItem().getWidgetMenu().packageData()) # Emitting the "justConnected" signal (That is on all connection points) connectionLine.myEndItem.lineConnected.emit() connectionLine.myStartItem.lineConnected.emit() newNode = startItems[0].parentItem() if newNode.nodeType == 'rig' or newNode.nodeType == 'globalComponent' or newNode.nodeType == 'hipComponent': newNode.incrementVersion() self.rigChangedSignal.emit() self.line = None super(SceneView, self).mouseReleaseEvent(event)
def __init__(self, *args, **kwargs): QtGui.QGraphicsView.__init__(self, *args, **kwargs) self.setScene(QtGui.QGraphicsScene()) self.setRenderHints(QtGui.QPainter.Antialiasing) self.setBackgroundBrush(QtGui.QColor(32, 32, 32)) self.boundingRect = QtCore.QRectF() self.slice_transform = QtGui.QTransform().shear(0, 1) self.delta_x = 8192 self.delta_y = 12288 slice0 = QtGui.QGraphicsRectItem(0, 0, 128 * self.delta_x, pow21) slice0.setPen(self.cube_pen) slice0.setTransform(self.slice_transform) slice1 = QtGui.QGraphicsRectItem(0, 0, 128 * self.delta_x, pow21) slice1.setPen(self.cube_pen) slice1.setZValue(-200) slice1.setTransform(self.slice_transform) slice1.setPos(63 * self.delta_x, -63 * self.delta_y) self.boundingRect = slice0.sceneBoundingRect().united(slice1.sceneBoundingRect()) height = self.boundingRect.height() self.boundingRect.setTop(-height * .25) self.boundingRect.setBottom(height * .85) self.scene().setSceneRect(self.boundingRect) #add nice 3D cube self.scene().addItem(slice0) self.scene().addItem(slice1) l = self.scene().addLine(slice0.sceneBoundingRect().x(), slice0.sceneBoundingRect().y(), slice1.sceneBoundingRect().x(), slice1.sceneBoundingRect().y()) l.setPen(self.cube_pen) l = self.scene().addLine(QtCore.QLineF(slice0.mapToScene(slice0.boundingRect().topRight()), slice1.mapToScene(slice1.boundingRect().topRight()))) l.setPen(self.cube_pen) l = self.scene().addLine(QtCore.QLineF(slice0.mapToScene(slice0.boundingRect().bottomRight()), slice1.mapToScene(slice1.boundingRect().bottomRight()))) l.setPen(self.cube_pen) l = self.scene().addLine(QtCore.QLineF(slice0.mapToScene(slice0.boundingRect().bottomLeft()), slice1.mapToScene(slice1.boundingRect().bottomLeft()))) l.setZValue(-200) l.setPen(self.cube_pen) self.currentWave = None
def traceGrid(self, station, coords=None): self.clear_Trace() if self.scene().load_centre is None and coords is None: return 0 self.trace_items = [] color = QtGui.QColor() color.setNamedColor((self.scene().colors['grid_trace'])) pen = QtGui.QPen(color, self.scene().line_width) pen.setJoinStyle(QtCore.Qt.RoundJoin) pen.setCapStyle(QtCore.Qt.RoundCap) if coords is None: for li in range(len(self.scene().lines.lines)): if self.scene().lines.lines[li].name == station.name: break else: return nearest = 99999 j = -1 dims = self.scene().lines.lines[li].coordinates[0] for i in range(len(self.scene().load_centre)): thisone = self.scene().lines.actualDistance(self.scene().load_centre[i][1], self.scene().load_centre[i][2], dims[0], dims[1]) if thisone < nearest: nearest = thisone j = i path = Shortest(self.scene().lines.lines, self.scene().lines.lines[li].coordinates[0], [self.scene().load_centre[j][1], self.scene().load_centre[j][2]], self.scene().grid_lines) route = path.getPath() # check we don't go through another load_centre if len(self.scene().load_centre) > 1: for co in range(len(route) - 1, 0, -1): for i in range(len(self.scene().load_centre)): if route[co][0] == self.scene().load_centre[i][1] and \ route[co][1] == self.scene().load_centre[i][2]: route = route[i:] break else: route = coords grid_path_len = 0. st_scn = self.mapFromLonLat(QtCore.QPointF(route[0][1], route[0][0])) for i in range(1, len(route)): en_scn = self.mapFromLonLat(QtCore.QPointF(route[i][1], route[i][0])) grid_path_len += self.scene().lines.actualDistance(route[i - 1][0], route[i - 1][1], route[i][0], route[i][1]) self.trace_items.append(QtGui.QGraphicsLineItem(QtCore.QLineF(st_scn, en_scn))) self.trace_items[-1].setPen(pen) self.trace_items[-1].setZValue(3) self.scene().addItem(self.trace_items[-1]) st_scn = en_scn return grid_path_len
def paint(self, painter, option, widget=None): if (self.myStartItem.collidesWithItem(self.myEndItem)): return myStartItem = self.myStartItem myEndItem = self.myEndItem myColor = self.myColor myPen = self.pen() myPen.setColor(self.myColor) arrowSize = 20.0 painter.setPen(myPen) painter.setBrush(self.myColor) centerLine = QtCore.QLineF(myStartItem.pos(), myEndItem.pos()) endPolygon = myEndItem.polygon() p1 = endPolygon.first() + myEndItem.pos() intersectPoint = QtCore.QPointF() for i in endPolygon: p2 = i + myEndItem.pos() polyLine = QtCore.QLineF(p1, p2) intersectType = polyLine.intersect(centerLine, intersectPoint) if intersectType == QtCore.QLineF.BoundedIntersection: break p1 = p2 self.setLine(QtCore.QLineF(intersectPoint, myStartItem.pos())) line = self.line() angle = math.acos(line.dx() / line.length()) if line.dy() >= 0: angle = (math.pi * 2.0) - angle arrowP1 = line.p1() + QtCore.QPointF(math.sin(angle + math.pi / 3.0) * arrowSize, math.cos(angle + math.pi / 3) * arrowSize) arrowP2 = line.p1() + QtCore.QPointF(math.sin(angle + math.pi - math.pi / 3.0) * arrowSize, math.cos(angle + math.pi - math.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) if self.isSelected(): painter.setPen(QtGui.QPen(myColor, 1, QtCore.Qt.DashLine)) myLine = QtCore.QLineF(line) myLine.translate(0, 4.0) painter.drawLine(myLine) myLine.translate(0,-8.0) painter.drawLine(myLine)