def draw_event(self): rnd = random.Random() if not self.isDrawn: item = QtWidgets.QGraphicsEllipseItem(*self.get_params()) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsEllipseItem(*self.get_params()) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsEllipseItem(*self.get_params()) item.setStartAngle(90) item.setSpanAngle(random.randrange(10, 100)) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsEllipseItem(*self.get_params()) item.setStartAngle(90 * 20) item.setSpanAngle(random.randrange(10, 100) * 20) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsRectItem(*self.get_params()) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsRectItem(*self.get_params()) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsLineItem() item.setLine( random.randrange( 0, self.graphicsView.frameGeometry().width() - MAX_WIDTH), random.randrange( 0, self.graphicsView.frameGeometry().height() - MAX_HEIGHT), random.randrange( 0, self.graphicsView.frameGeometry().width() - MAX_WIDTH), random.randrange( 0, self.graphicsView.frameGeometry().height() - MAX_HEIGHT)) self.graphicsScene.addItem(item) item = QtWidgets.QGraphicsLineItem() item.setLine( random.randrange( 0, self.graphicsView.frameGeometry().width() - MAX_WIDTH), random.randrange( 0, self.graphicsView.frameGeometry().height() - MAX_HEIGHT), random.randrange( 0, self.graphicsView.frameGeometry().width() - MAX_WIDTH), random.randrange( 0, self.graphicsView.frameGeometry().height() - MAX_HEIGHT)) self.graphicsScene.addItem(item) self.isDrawn = True
def drawGrid(w, h, s): for i in range(0, w, 50): gridLines.insert(0, QtWidgets.QGraphicsLineItem(i, 0, i, sceneHeight)) gridLines[0].setPen(QtGui.QPen(QtCore.Qt.lightGray)) s.addItem(gridLines[0]) for j in range(0, h, 50): gridLines.insert(0, QtWidgets.QGraphicsLineItem(0, j, w, j)) gridLines[0].setPen(QtGui.QPen(QtCore.Qt.lightGray)) s.addItem(gridLines[0])
def drawLine(): nonlocal p1 nonlocal angle nonlocal line1 nonlocal line2 nonlocal origin if p1 == None: angle2 = angle + 20 p1 = QtCore.QPointF(radius * math.cos(angle * math.pi / 180.0), radius * math.sin(angle * math.pi / 180.0)) p2 = QtCore.QPointF(radius * math.cos(angle2 * math.pi / 180.0), radius * math.sin(angle2 * math.pi / 180.0)) line1 = QtWidgets.QGraphicsLineItem(p1.x(), p1.y(), 0, 0) line2 = QtWidgets.QGraphicsLineItem(0, 0, p2.x(), p2.y()) # conePath.moveTo(line1) # conePath.lineTo(line2) # conePath.closeSubpath() # scene.addPath(conePath) scene.addItem(line1) scene.addItem(line2) else: scene.removeItem(line1) scene.removeItem(line2) # scene.removeItem(conePath) angle += 1 angle2 = angle + 20 p1 = QtCore.QPointF(radius * math.cos(angle * math.pi / 180.0), radius * math.sin(angle * math.pi / 180.0)) p2 = QtCore.QPointF(radius * math.cos(angle2 * math.pi / 180.0), radius * math.sin(angle2 * math.pi / 180.0)) line1 = QtWidgets.QGraphicsLineItem(p1.x(), p1.y(), 0, 0) line2 = QtWidgets.QGraphicsLineItem(0, 0, p2.x(), p2.y()) # conePath.moveTo(line1) # conePath.lineTo(line2) # conePath.closeSubpath() # scene.addPath(conePath) scene.addItem(line1) scene.addItem(line2)
def setSelectedUnit(self, selected_cp: QMapControlPoint): self.state = QLiberationMapState.MOVING_UNIT self.selected_cp = selected_cp position = self._transform_point(selected_cp.control_point.position) self.movement_line = QtWidgets.QGraphicsLineItem( QLineF(QPointF(*position), QPointF(*position))) self.scene().addItem(self.movement_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 = QtWidgets.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 __init__(self, parent, api_obj, diagramScene): """ :param parent: :param api_obj: """ super(ShuntGraphicItem, self).__init__(parent) self.w = 15.0 self.h = 30.0 self.parent = parent self.api_object = api_obj self.diagramScene = diagramScene self.width = 4 if self.api_object is not None: if self.api_object.active: self.style = ACTIVE['style'] self.color = ACTIVE['color'] else: self.style = DEACTIVATED['style'] self.color = DEACTIVATED['color'] else: self.style = OTHER['style'] self.color = OTHER['color'] pen = QPen(self.color, self.width, self.style) # Properties of the container: self.setFlags(self.ItemIsSelectable | self.ItemIsMovable) # self.setCursor(QCursor(Qt.PointingHandCursor)) # line to tie this object with the original bus (the parent) self.nexus = QtWidgets.QGraphicsLineItem() self.nexus.setPen(QPen(self.color, self.width, self.style)) parent.scene().addItem(self.nexus) self.lines = list() self.lines.append(QLineF(QPointF(self.w / 2, 0), QPointF(self.w / 2, self.h * 0.4))) self.lines.append(QLineF(QPointF(0, self.h * 0.4), QPointF(self.w, self.h * 0.4))) self.lines.append(QLineF(QPointF(0, self.h * 0.6), QPointF(self.w, self.h * 0.6))) self.lines.append(QLineF(QPointF(self.w / 2, self.h * 0.6), QPointF(self.w / 2, self.h))) self.lines.append(QLineF(QPointF(0, self.h * 1), QPointF(self.w, self.h * 1))) self.lines.append(QLineF(QPointF(self.w * 0.15, self.h * 1.1), QPointF(self.w * 0.85, self.h * 1.1))) self.lines.append(QLineF(QPointF(self.w * 0.3, self.h * 1.2), QPointF(self.w * 0.7, self.h * 1.2))) for l in self.lines: l1 = QLine(self) l1.setLine(l) l1.setPen(pen) self.addToGroup(l1) self.setPos(self.parent.x(), self.parent.y() + 100) self.update_line(self.pos())
def __init__(self): QtWidgets.QGraphicsView.__init__(self) self.setWindowTitle("QGraphicsView") scene = QtWidgets.QGraphicsScene(self) scene.setSceneRect(0, 0, 160, 120) self.setScene(scene) line = QtCore.QLineF(10, 10, 100, 100) node = QtWidgets.QGraphicsLineItem(line) scene.addItem(node)
def add_edge(self): selection = self.scene.selectedItems() if not selection: self.status_bar.showMessage("No node selected. No edge added.") else: first = selection[0] edge = QtWidgets.QGraphicsLineItem(0, 0, first.x(), first.y()) edge.setPen(self.edge_pen) self.scene.addItem(edge) self.maybe_save = True self.status_bar.showMessage( f"Added edge from (0, 0) to ({first.x()}, {first.y()})")
def __init__(self, game_model: GameModel): super(QLiberationMap, self).__init__() QLiberationMap.instance = self self.game_model = game_model self.game: Optional[Game] = game_model.game self.state = QLiberationMapState.NORMAL self.waypoint_info_font = QFont() self.waypoint_info_font.setPointSize(12) self.flight_path_items: List[QGraphicsItem] = [] # A tuple of (package index, flight index), or none. self.selected_flight: Optional[Tuple[int, int]] = None self.setMinimumSize(800, 600) self.setMaximumHeight(2160) self._zoom = 0 self.factor = 1 self.factorized = 1 self.init_scene() self.setGame(game_model.game) # Object displayed when unit is selected self.movement_line = QtWidgets.QGraphicsLineItem( QtCore.QLineF(QPointF(0, 0), QPointF(0, 0))) self.movement_line.setPen(QPen(CONST.COLORS["orange"], width=10.0)) self.selected_cp: QMapControlPoint = None GameUpdateSignal.get_instance().flight_paths_changed.connect( lambda: self.draw_flight_plans(self.scene())) def update_package_selection(index: int) -> None: # Optional[int] isn't a valid type for a Qt signal. None will be # converted to zero automatically. We use -1 to indicate no # selection. if index == -1: self.selected_flight = None else: self.selected_flight = index, 0 self.draw_flight_plans(self.scene()) GameUpdateSignal.get_instance().package_selection_changed.connect( update_package_selection) def update_flight_selection(index: int) -> None: if self.selected_flight is None: if index != -1: # We don't know what order update_package_selection and # update_flight_selection will be called in when the last # package is removed. If no flight is selected, it's not a # problem to also have no package selected. logging.error( "Flight was selected with no package selected") return # Optional[int] isn't a valid type for a Qt signal. None will be # converted to zero automatically. We use -1 to indicate no # selection. if index == -1: self.selected_flight = self.selected_flight[0], None self.selected_flight = self.selected_flight[0], index self.draw_flight_plans(self.scene()) GameUpdateSignal.get_instance().flight_selection_changed.connect( update_flight_selection) self.nm_to_pixel_ratio: int = 0 self.navmesh_highlight: Optional[QPolygonF] = None self.shortest_path_segments: List[QLineF] = []
if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) # Create a graphics scene sceneWidth = 400 sceneHeight = 400 scene = QtWidgets.QGraphicsScene( QtCore.QRectF(0, 0, sceneWidth, sceneHeight)) rect = scene.sceneRect() gridLines = list() drawGrid(sceneWidth, sceneHeight, scene) line1 = QtWidgets.QGraphicsLineItem(sceneWidth / 2, 0, sceneWidth / 2, sceneHeight) line1.setPen(QtGui.QPen(QtCore.Qt.black, 2)) scene.addItem(line1) line2 = QtWidgets.QGraphicsLineItem(0, sceneHeight / 2, sceneWidth, sceneHeight / 2) line2.setPen(QtGui.QPen(QtCore.Qt.red, 2)) scene.addItem(line2) line3 = QtWidgets.QGraphicsLineItem(-50, -50, sceneWidth, sceneHeight) line3.setPen(QtGui.QPen(QtCore.Qt.blue, 2)) line3.setFlag(QtWidgets.QGraphicsItem.ItemClipsChildrenToShape, True) scene.addItem(line3) scene.addItem(QtWidgets.QGraphicsRectItem(rect)) # Create a graphics view