def create_qr_scene(self, scene, qr_pixel_map, qr_text, backgound_pixmap) : pixmap_item = QGraphicsPixmapItem(qr_pixel_map) pixmap_item.setY(100) pixmap_item.setX(100) background_item = QGraphicsPixmapItem(backgound_pixmap) url_item = QGraphicsTextItem(qr_text) url_item.setFont(QFont('Arial', 50)) url_item.setTextWidth(820) url_item.setY(450) url_item.setX(1150) url_item.setZValue(1) pixmap_item.setZValue(1) background_item.setZValue(-1) scene.addItem(pixmap_item) scene.addItem(url_item) scene.addItem(background_item) return scene
class ScaleLine: def __init__(self, parent, color, text_color, text_start="", margins=(0, 0, 0, 0), dp=1, is_upload=False): self._parent = parent self._color = color self._text_color = text_color self._text_start = text_start self._left, self._top, self._right, self._bottom = margins self._dp = dp self._is_upload = is_upload self._line = QGraphicsLineItem(self._parent) self._line.setZValue(12) pen = QPen(self._color) pen.setWidth(1) pen.setStyle(Qt.DashLine) self._line.setPen(pen) if not self._is_upload: self._text_item = QGraphicsTextItem(self._parent) self._text_item.setZValue(11) self._text_item.setDefaultTextColor(self._text_color) font = self._text_item.font() font_size = 10 * self._dp if font_size > 0: self._text_item.setFont(QFont(font.family(), font_size)) def set_line(self, max_value=0, resize=False): height = self._parent.size().height() + self._top + self._bottom shift = int(height - height / 1.1) y = -self._top + shift if not self._is_upload: value = 0 max_value = int(max_value / 1.1) megabyte = 1024 * 1024 if max_value > megabyte: value = "{} MB".format(round(max_value / megabyte, 1)) elif max_value > 1024: max_value //= 1024 if max_value >= 10: max_value = max_value // 10 * 10 value = "{} KB".format(max_value) elif max_value > 0: if max_value >= 10: max_value = max_value // 10 * 10 value = "{} B".format(max_value) scale_text = self._text_start if not value \ else "{}{}/s".format(self._text_start, value) font_height = QFontMetrics(self._text_item.font())\ .boundingRect(scale_text).height() x = 10 self._text_item.setPos(x, y - font_height - 10) self._text_item.setPlainText(scale_text) if not resize: return self._line.setLine(QLineF(0, y, self._parent.size().width() + 30, y))
def eventFilter(self, obj, event): if event.type() == QEvent.GraphicsSceneMousePress: if event.button() == Qt.MouseButton.LeftButton: self.mouse_pressed = True self.mouse_pressed_x, self.mouse_pressed_y = event.pos().x( ), event.pos().y() if self.draw_ellipse: ellipsis = QGraphicsEllipseItem(self.chart) ellipsis.setZValue(12) ellipsis.setBrush(QBrush(QColor(244, 67, 54, 50))) ellipsis.setPen(QPen(Qt.transparent)) self.ellipses.append(ellipsis) elif self.write_text: for t in self.texts: r = QRectF() r.setTopLeft(t.pos()) r.setWidth(t.boundingRect().width()) r.setHeight(t.boundingRect().height()) if r.contains(self.mouse_pressed_x, self.mouse_pressed_y): return True """ The user clicked over an area where there is no text. So we create one. """ text = QGraphicsTextItem(self.chart) text.setZValue(12) text.setPos( QPointF(self.mouse_pressed_x, self.mouse_pressed_y)) text.setPlainText("label") text.setAcceptHoverEvents(True) text.setTabChangesFocus(True) text.setFlags(QGraphicsTextItem.ItemIsMovable) text.installEventFilter(self.text_event_filter) self.texts.append(text) return True elif event.button() == Qt.MouseButton.RightButton: x, y = event.pos().x(), event.pos().y() for e in self.ellipses: if e.rect().contains(x, y): e.hide() self.ellipses.remove(e) for t in self.texts: r = QRectF() r.setTopLeft(t.pos()) r.setWidth(t.boundingRect().width()) r.setHeight(t.boundingRect().height()) if r.contains(x, y): t.hide() self.texts.remove(t) return True return QObject.eventFilter(self, obj, event) elif event.type() == QEvent.GraphicsSceneMouseRelease: self.mouse_pressed = False return True elif event.type() == QEvent.GraphicsSceneMouseMove: if self.mouse_pressed: if self.draw_ellipse: x, y = event.pos().x(), event.pos().y() width = x - self.mouse_pressed_x height = y - self.mouse_pressed_y self.ellipses[-1].setRect(self.mouse_pressed_x, self.mouse_pressed_y, width, height) return True return QObject.eventFilter(self, obj, event) return QObject.eventFilter(self, obj, event)