Exemple #1
0
 def draw_cursor(self, event_x, event_y, drawing_tool_radius,
                 new_circle=False):
     """
     Draws a blue circle where the user clicked. :param event_x:
     QGraphicsScene event attribute: event.scenePos().x() :param event_y:
     QGraphicsScene event attribute: event.scenePos().y() :param
     drawing_tool_radius: the current radius of the drawing tool :param
     new_circle: True when the circle object is being created rather than
     updated.
     """
     self.draw_tool_radius = drawing_tool_radius
     self.current_cursor_x = event_x - self.draw_tool_radius
     self.current_cursor_y = event_y - self.draw_tool_radius
     if new_circle:
         self.cursor = QGraphicsEllipseItem(self.current_cursor_x,
                                            self.current_cursor_y,
                                            self.draw_tool_radius * 2,
                                            self.draw_tool_radius * 2)
         pen = QPen(QColor("blue"))
         pen.setWidth(0)
         self.cursor.setPen(pen)
         self.cursor.setZValue(1)
         self.addItem(self.cursor)
     elif self.cursor is not None:
         self.cursor.setRect(self.current_cursor_x, self.current_cursor_y,
                             self.draw_tool_radius * 2,
                             self.draw_tool_radius * 2)
Exemple #2
0
 def __init__(self, name="?", r=10):
     super().__init__()
     self.setFlag(QGraphicsItem.ItemIsMovable)
     self.setFlag(QGraphicsItem.ItemIsFocusable)
     self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
     self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
     self.setAcceptHoverEvents(True)
     # define circle shape:
     w = 2 * r + 2
     self.el = QGraphicsEllipseItem(0, 0, w, w)
     self.el.setBrush(QBrush(QColor("white")))
     shadow = QGraphicsDropShadowEffect()
     shadow.setOffset(4)
     self.el.setGraphicsEffect(shadow)
     self.el.setParentItem(self)
     # define node label shape:
     self.label = QGraphicsTextItem(name)
     self.label.setDefaultTextColor(QColor("blue"))
     self.label.setFlag(QGraphicsItem.ItemIsSelectable)
     self.label.setParentItem(self)
     self.el.setZValue(1.0)
     self.label.setZValue(2.0)
     center = self.center() - self.label.boundingRect().center()
     self.label.setPos(self.mapFromScene(center))
     self.setZValue(1.0)
     self.cx = []
 def load_items(self, file_name: str, gim):
     input_file = QFile(file_name)
     if input_file.open(QIODevice.ReadOnly):
         input_stream = QDataStream(input_file)
         while not input_stream.atEnd():
             item_type = input_stream.readInt8()
             item_name = input_stream.readString()
             if item_type == QGraphicsRectItem.type(QGraphicsRectItem()):
                 rect = QGraphicsRectItem()
                 self.input_to_rect_item(input_stream, rect)
                 self.scene.addItem(rect)
                 gim.append_shape(rect, rect.type(), rect.pen(),
                                  rect.brush())
                 print("Rectangle loaded")
             elif item_type == QGraphicsEllipseItem.type(
                     QGraphicsEllipseItem()):
                 ellipse = QGraphicsEllipseItem()
                 self.input_to_rect_item(input_stream, ellipse)
                 self.scene.addItem(ellipse)
                 gim.append_shape(ellipse, ellipse.type(), ellipse.pen(),
                                  ellipse.brush())
                 print("Ellipse loaded")
             elif item_type == QGraphicsPolygonItem.type(
                     QGraphicsPolygonItem()):
                 polygon = QGraphicsPolygonItem()
                 self.input_to_polygon_item(input_stream, polygon)
                 self.scene.addItem(polygon)
                 gim.append_shape(polygon, polygon.type(), polygon.pen(),
                                  polygon.brush())
                 print("l polygon")
             elif item_type == QGraphicsLineItem.type(QGraphicsLineItem()):
                 line = QGraphicsLineItem()
                 self.input_to_line_item(input_stream, line)
                 self.scene.addItem(line)
                 gim.append_shape(line, line.type(), line.pen(),
                                  QBrush(Qt.black))
                 print("Line loaded")
             gim.return_shapes()[-1].name = item_name
             ItemsInputOutput.set_item_flags(self.scene)
         input_file.close()
 def __init__(self, shape, item_type, pixmap):
     self.shape: QGraphicsItem = shape
     self.name: str = "undefined"
     if item_type == QGraphicsRectItem.type(QGraphicsRectItem()):
         self.name: str = "rectangle"
     elif item_type == QGraphicsEllipseItem.type(QGraphicsEllipseItem()):
         self.name: str = "ellipse"
     elif item_type == QGraphicsPolygonItem.type(QGraphicsPolygonItem()):
         if shape.polygon().size() == 3:
             self.name: str = "triangle"
         else:
             self.name: str = "polygon"
     elif item_type == QGraphicsLineItem.type(QGraphicsLineItem()):
         self.name: str = "line"
     self.pixmap: QPixmap = pixmap
 def shape_to_pixelmap(item_type, pen, brush, shape) -> QPixmap:
     pixmap = QPixmap(50, 50)
     pixmap.fill(Qt.transparent)
     painter = QPainter(pixmap)
     painter.setRenderHint(QPainter.Antialiasing)
     painter.setPen(pen)
     painter.setBrush(brush)
     if item_type == QGraphicsRectItem.type(QGraphicsRectItem()):
         painter.drawRect(QRect(10, 15, 30, 20))
     elif item_type == QGraphicsEllipseItem.type(QGraphicsEllipseItem()):
         painter.drawEllipse(QRect(10, 10, 30, 30))
     elif item_type == QGraphicsPolygonItem.type(QGraphicsPolygonItem()):
         if shape.polygon().size() == 3:
             painter.drawPolygon(QPolygon([QPoint(10, 40), QPoint(40, 40), QPoint(25, 10)]))
         else:
             painter.drawPolygon(QPolygon([QPoint(12, 40), QPoint(23, 36),
                                           QPoint(37, 24), QPoint(23, 12), QPoint(7, 16)]))
     elif item_type == QGraphicsLineItem.type(QGraphicsLineItem()):
         painter.drawLine(QLine(10, 40, 40, 10))
     return pixmap
Exemple #6
0
 def draw_simple_shape_to_scene(view: QGraphicsView,
                                p_prev: QPointF,
                                p_act: QPointF,
                                to_gim: bool = True) -> None:
     if view.actual_selection.value in [2, 4]:
         width = math.fabs(p_prev.x() - p_act.x())
         height = math.fabs(p_prev.y() - p_act.y())
         xmin = min(p_prev.x(), p_act.x())
         ymin = min(p_prev.y(), p_act.y())
         item: QAbstractGraphicsShapeItem
         if view.actual_selection.value == 2:
             item = QGraphicsRectItem(0, 0, width, height)
         else:
             item = QGraphicsEllipseItem(0, 0, width, height)
         item.setPos(QPointF(xmin, ymin))
         item.setPen(view.actual_pen)
         item.setBrush(view.actual_brush)
         DrawTools.set_item_flags(item)
         view.scene().addItem(item)
         view.gim.append_shape(item, item.type(), view.actual_pen,
                               view.actual_brush)
     elif view.actual_selection.value in [3, 5, 6]:
         x = p_act.x() - p_prev.x()
         y = p_act.y() - p_prev.y()
         item: QGraphicsItem
         if view.actual_selection.value == 3:
             item = QGraphicsPolygonItem(
                 QPolygonF(
                     [QPointF(0, 0),
                      QPointF(0 + x, 0),
                      QPointF(x / 2, y)]))
             item.setBrush(view.actual_brush)
         else:
             item = QGraphicsLineItem(QLineF(0, 0, x, y))
         item.setPos(p_prev)
         item.setPen(view.actual_pen)
         DrawTools.set_item_flags(item)
         view.scene().addItem(item)
         if to_gim:
             view.gim.append_shape(item, item.type(), view.actual_pen,
                                   view.actual_brush)