def find_display_size(display: GameDisplay, view: QGraphicsView, target_size: QSize) -> QSize: max_width = None max_height = None min_width = min_height = 1 display_width = display.width() display_height = display.height() while True: scene_size = view.contentsRect().size() if scene_size.width() == target_size.width(): min_width = max_width = display_width elif scene_size.width() < target_size.width(): min_width = display_width + 1 else: max_width = display_width - 1 if scene_size.height() == target_size.height(): min_height = max_height = display_height elif scene_size.height() < target_size.height(): min_height = display_height + 1 else: max_height = display_height - 1 if max_width is None: display_width *= 2 else: display_width = (min_width + max_width) // 2 if max_height is None: display_height *= 2 else: display_height = (min_height + max_height) // 2 if min_width == max_width and min_height == max_height: return QSize(display_width, display_height) display.resize(display_width, display_height) view.grab() # Force layout recalculation.
def mouseMoveEvent(self, event): pos = self._chart.mapToValue(event.position().toPoint()) x = pos.x() y = pos.y() self._coordX.setText(f"X: {x:.2f}") self._coordY.setText(f"Y: {y:.2f}") QGraphicsView.mouseMoveEvent(self, event)
def resizeEvent(self, event): if self.scene(): self.scene().setSceneRect(QRectF(QPointF(0, 0), event.size())) self._chart.resize(event.size()) self._coordX.setPos(self._chart.size().width() / 2 - 50, self._chart.size().height() - 20) self._coordY.setPos(self._chart.size().width() / 2 + 50, self._chart.size().height() - 20) for callout in self._callouts: callout.updateGeometry() QGraphicsView.resizeEvent(self, event)
def draw_polygon(view: QGraphicsView, polygon_list: list[QPointF]): item = QGraphicsPolygonItem(QPolygonF(polygon_list)) item.setPen(view.actual_pen) item.setBrush(view.actual_brush) items = view.scene().items() for i in range(len(polygon_list)): view.scene().removeItem(items[i]) DrawTools.set_item_flags(item) view.scene().addItem(item) view.gim.append_shape(item, item.type(), view.actual_pen, view.actual_brush)
def __init__(self, scene: QGraphicsScene, parent: QMainWindow): QGraphicsView.__init__(self, scene, parent) self.crop_rect = QRect(QPoint(0, 0), QSize(0, 0)) self.g_rect = QGraphicsRectItem(QRectF(self.crop_rect)) self.setParent(parent) self.app = self.parent() self.crop_btn = self.parent().crop_button self.mouse_down = False self.g_rect.setPen(QPen(Qt.red, 1, Qt.SolidLine)) self.g_rect.setBrush(QBrush(Qt.red, Qt.Dense6Pattern)) self.mouse_pos = QPoint(0, 0) self.adjustment = '' self.annotation = False
def zoom(view: QGraphicsView, e: QWheelEvent, mode: str): zoom_in_factor = 1.25 zoom_out_factor = 1 / zoom_in_factor view.setTransformationAnchor(QGraphicsView.NoAnchor) view.setResizeAnchor(QGraphicsView.NoAnchor) old_pos = view.mapToScene(int(e.position().x()), int(e.position().y())) if mode == "zoom-in": zoom_factor = zoom_in_factor else: zoom_factor = zoom_out_factor view.scale(zoom_factor, zoom_factor) new_pos = view.mapToScene(int(e.position().x()), int(e.position().y())) delta = new_pos - old_pos view.translate(delta.x(), delta.y())
def mousePressEvent(self, event: QMouseEvent): # pylint: disable=invalid-name """Mouse event handler; begins a crop action""" self.mouse_down = True self.mouse_pos = self.mapToScene(event.pos()).toPoint() if self.crop_btn.isChecked() or self.annotation: self.crop_rect.setTopLeft(self.mapToScene(event.pos()).toPoint()) self.scene().addItem(self.g_rect) if self.annotation: self.g_rect.setPen(QPen(Qt.magenta, 1, Qt.SolidLine)) self.g_rect.setBrush(QBrush(Qt.magenta, Qt.Dense4Pattern)) elif self.hasMouseTracking() and self.g_rect.isUnderMouse: self.adjustment = self.edge(event.pos)[1] else: QGraphicsView.mousePressEvent(self, event)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.processing = False self.rectangle = None self.setMinimumHeight(405) self.layout = QVBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) self.scene = QGraphicsScene(self) self.scene.setBackgroundBrush(QBrush(Qt.black)) self.view = QGraphicsView(self.scene) self.viewfinder = QGraphicsVideoItem() self.scene.addItem(self.viewfinder) self.layout.addWidget(self.view) self.setLayout(self.layout) self.camera = None self.captureSession = None self.imageCapture = None self.captureTimer = None
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)
def change_view_zoom(view: QGraphicsView, e: QWheelEvent): if e.angleDelta().y() > 0 and view.transform().m11() < 1000.0: ViewTools.zoom(view, e, "zoom-in") elif e.angleDelta().y() < 0 and view.transform().m11() > 0.01: ViewTools.zoom(view, e, "zoom-out")
def trigger_resize(view: QGraphicsView, width: int, height: int): event = QResizeEvent(QSize(width, height), QSize(1, 1)) view.resizeEvent(event)
def setupUi(self, Img): if not Img.objectName(): Img.setObjectName(u"Img") Img.resize(620, 559) self.gridLayout = QGridLayout(Img) self.gridLayout.setObjectName(u"gridLayout") self.graphicsView = QGraphicsView(Img) self.graphicsView.setObjectName(u"graphicsView") self.gridLayout.addWidget(self.graphicsView, 0, 0, 1, 1) self.verticalLayout = QVBoxLayout() self.verticalLayout.setObjectName(u"verticalLayout") self.line_3 = QFrame(Img) self.line_3.setObjectName(u"line_3") self.line_3.setFrameShape(QFrame.VLine) self.line_3.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_3) self.verticalLayout_2 = QVBoxLayout() self.verticalLayout_2.setObjectName(u"verticalLayout_2") self.line_5 = QFrame(Img) self.line_5.setObjectName(u"line_5") self.line_5.setFrameShape(QFrame.VLine) self.line_5.setFrameShadow(QFrame.Sunken) self.verticalLayout_2.addWidget(self.line_5) self.checkBox = QCheckBox(Img) self.checkBox.setObjectName(u"checkBox") self.checkBox.setMaximumSize(QSize(100, 16777215)) self.checkBox.setChecked(True) self.verticalLayout_2.addWidget(self.checkBox) self.ttaModel = QCheckBox(Img) self.ttaModel.setObjectName(u"ttaModel") self.ttaModel.setMaximumSize(QSize(70, 16777215)) self.verticalLayout_2.addWidget(self.ttaModel) self.horizontalLayout_3 = QHBoxLayout() self.horizontalLayout_3.setObjectName(u"horizontalLayout_3") self.scaleRadio = QRadioButton(Img) self.buttonGroup_2 = QButtonGroup(Img) self.buttonGroup_2.setObjectName(u"buttonGroup_2") self.buttonGroup_2.addButton(self.scaleRadio) self.scaleRadio.setObjectName(u"scaleRadio") self.scaleRadio.setMaximumSize(QSize(80, 16777215)) self.scaleRadio.setChecked(True) self.horizontalLayout_3.addWidget(self.scaleRadio) self.scaleEdit = QLineEdit(Img) self.scaleEdit.setObjectName(u"scaleEdit") self.scaleEdit.setMaximumSize(QSize(160, 16777215)) self.scaleEdit.setAlignment(Qt.AlignCenter) self.horizontalLayout_3.addWidget(self.scaleEdit) self.verticalLayout_2.addLayout(self.horizontalLayout_3) self.horizontalLayout_4 = QHBoxLayout() self.horizontalLayout_4.setObjectName(u"horizontalLayout_4") self.heighRadio = QRadioButton(Img) self.buttonGroup_2.addButton(self.heighRadio) self.heighRadio.setObjectName(u"heighRadio") self.heighRadio.setMaximumSize(QSize(80, 16777215)) self.horizontalLayout_4.addWidget(self.heighRadio) self.widthEdit = QLineEdit(Img) self.widthEdit.setObjectName(u"widthEdit") self.widthEdit.setEnabled(False) self.widthEdit.setMaximumSize(QSize(60, 16777215)) self.widthEdit.setAlignment(Qt.AlignCenter) self.horizontalLayout_4.addWidget(self.widthEdit) self.label_2 = QLabel(Img) self.label_2.setObjectName(u"label_2") self.label_2.setMaximumSize(QSize(20, 16777215)) self.horizontalLayout_4.addWidget(self.label_2) self.heighEdit = QLineEdit(Img) self.heighEdit.setObjectName(u"heighEdit") self.heighEdit.setEnabled(False) self.heighEdit.setMaximumSize(QSize(60, 16777215)) self.heighEdit.setAlignment(Qt.AlignCenter) self.horizontalLayout_4.addWidget(self.heighEdit) self.verticalLayout_2.addLayout(self.horizontalLayout_4) self.horizontalLayout_5 = QHBoxLayout() self.horizontalLayout_5.setObjectName(u"horizontalLayout_5") self.label_4 = QLabel(Img) self.label_4.setObjectName(u"label_4") self.label_4.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_5.addWidget(self.label_4) self.noiseCombox = QComboBox(Img) self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.setObjectName(u"noiseCombox") self.noiseCombox.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_5.addWidget(self.noiseCombox) self.verticalLayout_2.addLayout(self.horizontalLayout_5) self.horizontalLayout_6 = QHBoxLayout() self.horizontalLayout_6.setObjectName(u"horizontalLayout_6") self.label_5 = QLabel(Img) self.label_5.setObjectName(u"label_5") self.label_5.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_6.addWidget(self.label_5) self.comboBox = QComboBox(Img) self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.setObjectName(u"comboBox") self.comboBox.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_6.addWidget(self.comboBox) self.verticalLayout_2.addLayout(self.horizontalLayout_6) self.horizontalLayout_7 = QHBoxLayout() self.horizontalLayout_7.setObjectName(u"horizontalLayout_7") self.changeJpg = QPushButton(Img) self.changeJpg.setObjectName(u"changeJpg") self.changeJpg.setMaximumSize(QSize(100, 16777215)) self.horizontalLayout_7.addWidget(self.changeJpg) self.changePng = QPushButton(Img) self.changePng.setObjectName(u"changePng") self.changePng.setMaximumSize(QSize(100, 16777215)) self.horizontalLayout_7.addWidget(self.changePng) self.changeLabel = QLabel(Img) self.changeLabel.setObjectName(u"changeLabel") self.changeLabel.setMaximumSize(QSize(100, 16777215)) self.changeLabel.setAlignment(Qt.AlignCenter) self.horizontalLayout_7.addWidget(self.changeLabel) self.verticalLayout_2.addLayout(self.horizontalLayout_7) self.horizontalLayout_11 = QHBoxLayout() self.horizontalLayout_11.setObjectName(u"horizontalLayout_11") self.verticalLayout_2.addLayout(self.horizontalLayout_11) self.verticalLayout.addLayout(self.verticalLayout_2) self.line = QFrame(Img) self.line.setObjectName(u"line") self.line.setFrameShape(QFrame.VLine) self.line.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line) self.line_4 = QFrame(Img) self.line_4.setObjectName(u"line_4") self.line_4.setFrameShape(QFrame.HLine) self.line_4.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_4) self.verticalLayout_3 = QVBoxLayout() self.verticalLayout_3.setObjectName(u"verticalLayout_3") self.horizontalLayout_8 = QHBoxLayout() self.horizontalLayout_8.setObjectName(u"horizontalLayout_8") self.label_8 = QLabel(Img) self.label_8.setObjectName(u"label_8") self.label_8.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_8.addWidget(self.label_8) self.resolutionLabel = QLabel(Img) self.resolutionLabel.setObjectName(u"resolutionLabel") self.resolutionLabel.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_8.addWidget(self.resolutionLabel) self.verticalLayout_3.addLayout(self.horizontalLayout_8) self.horizontalLayout_9 = QHBoxLayout() self.horizontalLayout_9.setObjectName(u"horizontalLayout_9") self.label_10 = QLabel(Img) self.label_10.setObjectName(u"label_10") self.label_10.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_9.addWidget(self.label_10) self.sizeLabel = QLabel(Img) self.sizeLabel.setObjectName(u"sizeLabel") self.sizeLabel.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_9.addWidget(self.sizeLabel) self.verticalLayout_3.addLayout(self.horizontalLayout_9) self.horizontalLayout = QHBoxLayout() self.horizontalLayout.setObjectName(u"horizontalLayout") self.label = QLabel(Img) self.label.setObjectName(u"label") self.label.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout.addWidget(self.label) self.gpuName = QLabel(Img) self.gpuName.setObjectName(u"gpuName") self.gpuName.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout.addWidget(self.gpuName) self.verticalLayout_3.addLayout(self.horizontalLayout) self.horizontalLayout_10 = QHBoxLayout() self.horizontalLayout_10.setObjectName(u"horizontalLayout_10") self.label_6 = QLabel(Img) self.label_6.setObjectName(u"label_6") self.label_6.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_10.addWidget(self.label_6) self.tickLabel = QLabel(Img) self.tickLabel.setObjectName(u"tickLabel") self.tickLabel.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_10.addWidget(self.tickLabel) self.verticalLayout_3.addLayout(self.horizontalLayout_10) self.oepnButton = QPushButton(Img) self.oepnButton.setObjectName(u"oepnButton") self.oepnButton.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.oepnButton) self.horizontalLayout_2 = QHBoxLayout() self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") self.verticalLayout_3.addLayout(self.horizontalLayout_2) self.pushButton_3 = QPushButton(Img) self.pushButton_3.setObjectName(u"pushButton_3") self.pushButton_3.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.pushButton_3) self.pushButton = QPushButton(Img) self.pushButton.setObjectName(u"pushButton") self.pushButton.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.pushButton) self.saveButton = QPushButton(Img) self.saveButton.setObjectName(u"saveButton") self.saveButton.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.saveButton) self.verticalLayout.addLayout(self.verticalLayout_3) self.line_6 = QFrame(Img) self.line_6.setObjectName(u"line_6") self.line_6.setFrameShape(QFrame.HLine) self.line_6.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_6) self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.verticalLayout.addItem(self.verticalSpacer) self.line_2 = QFrame(Img) self.line_2.setObjectName(u"line_2") self.line_2.setFrameShape(QFrame.VLine) self.line_2.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_2) self.gridLayout.addLayout(self.verticalLayout, 0, 1, 1, 1) self.retranslateUi(Img) self.checkBox.clicked.connect(Img.SwithPicture) self.saveButton.clicked.connect(Img.SavePicture) self.heighEdit.textChanged.connect(Img.CheckScaleRadio) self.pushButton_3.clicked.connect(Img.ReduceScalePic) self.heighRadio.clicked.connect(Img.CheckScaleRadio) self.ttaModel.clicked.connect(Img.CheckScaleRadio) self.oepnButton.clicked.connect(Img.OpenPicture) self.widthEdit.textChanged.connect(Img.CheckScaleRadio) self.pushButton.clicked.connect(Img.AddScalePic) self.scaleEdit.textChanged.connect(Img.CheckScaleRadio) self.scaleRadio.clicked.connect(Img.CheckScaleRadio) self.comboBox.currentIndexChanged.connect(Img.ChangeModel) self.changeJpg.clicked.connect(Img.StartWaifu2x) self.noiseCombox.currentIndexChanged.connect(Img.CheckScaleRadio) self.changePng.clicked.connect(Img.StartWaifu2xPng) self.changeJpg.clicked.connect(Img.StartWaifu2xJPG) QMetaObject.connectSlotsByName(Img)
def setupUi(self, GridControls): if not GridControls.objectName(): GridControls.setObjectName(u"GridControls") GridControls.resize(400, 300) self.grid_layout = QGridLayout(GridControls) self.grid_layout.setObjectName(u"grid_layout") self.game_display = QGraphicsView(GridControls) self.game_display.setObjectName(u"game_display") self.grid_layout.addWidget(self.game_display, 0, 0, 5, 1) self.black_count = ScaledLabel(GridControls) self.black_count.setObjectName(u"black_count") sizePolicy = QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.black_count.sizePolicy().hasHeightForWidth()) self.black_count.setSizePolicy(sizePolicy) self.black_count.setScaledContents(True) self.black_count.setAlignment(Qt.AlignHCenter | Qt.AlignTop) self.grid_layout.addWidget(self.black_count, 1, 1, 1, 1) self.black_count_pixmap = ScaledLabel(GridControls) self.black_count_pixmap.setObjectName(u"black_count_pixmap") sizePolicy.setHeightForWidth( self.black_count_pixmap.sizePolicy().hasHeightForWidth()) self.black_count_pixmap.setSizePolicy(sizePolicy) self.black_count_pixmap.setScaledContents(True) self.black_count_pixmap.setAlignment(Qt.AlignBottom | Qt.AlignHCenter) self.grid_layout.addWidget(self.black_count_pixmap, 0, 1, 1, 1) self.white_count_pixmap = ScaledLabel(GridControls) self.white_count_pixmap.setObjectName(u"white_count_pixmap") sizePolicy.setHeightForWidth( self.white_count_pixmap.sizePolicy().hasHeightForWidth()) self.white_count_pixmap.setSizePolicy(sizePolicy) self.white_count_pixmap.setScaledContents(True) self.white_count_pixmap.setAlignment(Qt.AlignBottom | Qt.AlignHCenter) self.grid_layout.addWidget(self.white_count_pixmap, 0, 2, 1, 1) self.white_count = ScaledLabel(GridControls) self.white_count.setObjectName(u"white_count") sizePolicy.setHeightForWidth( self.white_count.sizePolicy().hasHeightForWidth()) self.white_count.setSizePolicy(sizePolicy) self.white_count.setScaledContents(True) self.white_count.setAlignment(Qt.AlignHCenter | Qt.AlignTop) self.grid_layout.addWidget(self.white_count, 1, 2, 1, 1) self.player_pixmap = ScaledLabel(GridControls) self.player_pixmap.setObjectName(u"player_pixmap") sizePolicy.setHeightForWidth( self.player_pixmap.sizePolicy().hasHeightForWidth()) self.player_pixmap.setSizePolicy(sizePolicy) self.player_pixmap.setScaledContents(True) self.player_pixmap.setAlignment(Qt.AlignBottom | Qt.AlignHCenter) self.grid_layout.addWidget(self.player_pixmap, 2, 1, 1, 2) self.move_text = ScaledLabel(GridControls) self.move_text.setObjectName(u"move_text") sizePolicy.setHeightForWidth( self.move_text.sizePolicy().hasHeightForWidth()) self.move_text.setSizePolicy(sizePolicy) self.move_text.setScaledContents(True) self.move_text.setAlignment(Qt.AlignHCenter | Qt.AlignTop) self.grid_layout.addWidget(self.move_text, 3, 1, 1, 2) self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.grid_layout.addItem(self.verticalSpacer, 4, 1, 1, 2) self.grid_layout.setRowStretch(0, 1) self.grid_layout.setRowStretch(1, 1) self.grid_layout.setRowStretch(2, 5) self.grid_layout.setRowStretch(3, 1) self.grid_layout.setRowStretch(4, 5) self.grid_layout.setColumnStretch(0, 10) self.grid_layout.setColumnStretch(1, 1) self.grid_layout.setColumnStretch(2, 1) self.retranslateUi(GridControls) QMetaObject.connectSlotsByName(GridControls)
def mouseMoveEvent(self, event): self._coordX.setText("X: {0:.2f}" .format(self._chart.mapToValue(event.pos()).x())) self._coordY.setText("Y: {0:.2f}" .format(self._chart.mapToValue(event.pos()).y())) QGraphicsView.mouseMoveEvent(self, event)
def __init__(self, stages: list[Stage], *args, **kwargs): QMainWindow.__init__(self, *args, **kwargs) if not self.objectName(): self.setObjectName("MainWindow") self.resize(800, 600) size_policy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) size_policy.setHorizontalStretch(0) size_policy.setVerticalStretch(0) size_policy.setHeightForWidth(self.sizePolicy().hasHeightForWidth()) self.setSizePolicy(size_policy) self.setWindowTitle("Interfan") self.central_widget = QWidget(self) self.central_widget.setObjectName("central_widget") self.main_horizontal_layout = QHBoxLayout(self.central_widget) self.main_horizontal_layout.setObjectName("main_horizontal_layout") self.left_grid_layout = QGridLayout() self.left_grid_layout.setObjectName("left_grid_layout") self.slice_slider = QSlider(self.central_widget) self.slice_slider.setObjectName("slice_slider") self.slice_slider.setEnabled(False) self.slice_slider.setOrientation(Qt.Horizontal) self.left_grid_layout.addWidget(self.slice_slider, 1, 0, 1, 1) self.slice_label = QLabel(self.central_widget) self.slice_label.setObjectName("slice_label") self.slice_label.setGeometry(QRect(0, 10, 58, 18)) self.slice_label.setText("0") self.left_grid_layout.addWidget(self.slice_label, 1, 1, 1, 1) self.main_image_scene = QGraphicsScene() self.main_image_view = QGraphicsView(self.main_image_scene) self.main_image_view.setObjectName("main_image_view") self.left_grid_layout.addWidget(self.main_image_view, 0, 0, 1, 1) self.slice_image_scene = QGraphicsScene() self.slice_image_view = QGraphicsView(self.slice_image_scene) self.slice_image_view.setObjectName("slice_image_view") self.left_grid_layout.addWidget(self.slice_image_view, 0, 1, 1, 1) self.left_grid_layout.setRowStretch(0, 12) self.left_grid_layout.setRowStretch(1, 1) self.left_grid_layout.setColumnStretch(0, 6) self.left_grid_layout.setColumnStretch(1, 1) self.main_horizontal_layout.addLayout(self.left_grid_layout) self.right_vertical_layout = QVBoxLayout() self.right_vertical_layout.setObjectName("right_vertical_layout") self.settings_widget = SettingWidget(self.central_widget) self.right_vertical_layout.addWidget(self.settings_widget.group_box) self.proceed_button = QPushButton(self.central_widget) self.proceed_button.setObjectName("proceed_button") self.proceed_button.setText("Выполнить") self.proceed_button.setEnabled(False) self.right_vertical_layout.addWidget(self.proceed_button) self.right_down_layout = QHBoxLayout() self.right_down_layout.setObjectName("right_down_layout") self.stages_layout = QVBoxLayout() self.stages_layout.setObjectName("stages_layout") self.stages_label = QLabel(self.central_widget) self.stages_label.setObjectName("stages_label") self.stages_label.setText("Этапы") self.stages_layout.addWidget(self.stages_label, 1) self.stages_radio_buttons = [] for s in stages: rb = QRadioButton(self.central_widget) rb.setObjectName("stages_radio_button") rb.setEnabled(False) rb.setText(s.name) self.stages_layout.addWidget(rb, 1) self.stages_radio_buttons.append(rb) self.stages_radio_buttons[0].setEnabled(True) self.stages_radio_buttons[0].setChecked(True) self.set_stage(stages[0]) self.right_down_layout.addLayout(self.stages_layout) self.history_layout = QVBoxLayout() self.history_layout.setObjectName("history_layout") self.history_header_layout = QHBoxLayout() self.history_header_layout.setObjectName("history_header_layout") self.history_label = QLabel(self.central_widget) self.history_label.setObjectName("history_label") self.history_label.setText("История операций") self.history_header_layout.addWidget(self.history_label) self.history_minus_button = QToolButton(self.central_widget) self.history_minus_button.setObjectName("history_minus_button") self.history_minus_button.setEnabled(False) self.history_minus_button.setText("-") self.history_header_layout.addWidget(self.history_minus_button) self.history_header_layout.setStretch(0, 8) self.history_header_layout.setStretch(1, 1) self.history_layout.addLayout(self.history_header_layout) self.history_list_view = QListView(self.central_widget) self.history_list_view.setObjectName("history_list_view") self.history_layout.addWidget(self.history_list_view) self.history_script_button = QPushButton(self.central_widget) self.history_script_button.setObjectName("history_script_button") self.history_script_button.setText("Просмотреть как скрипт") self.history_layout.addWidget(self.history_script_button) self.right_down_layout.addLayout(self.history_layout) self.right_down_layout.setStretch(0, 1) self.right_down_layout.setStretch(1, 1) self.right_vertical_layout.addLayout(self.right_down_layout) self.right_vertical_layout.setStretch(0, 1) self.right_vertical_layout.setStretch(1, 0) self.right_vertical_layout.setStretch(2, 1) self.main_horizontal_layout.addLayout(self.right_vertical_layout) self.main_horizontal_layout.setStretch(0, 1) self.main_horizontal_layout.setStretch(1, 1) self.setCentralWidget(self.central_widget) self.menu_bar = QMenuBar(self) self.menu_bar.setObjectName("menu_bar") self.menu_bar.setGeometry(QRect(0, 0, 800, 30)) self.menu = QMenu(self.menu_bar) self.menu.setObjectName("men") self.menu.setTitle("Меню") self.setMenuBar(self.menu_bar) self.open_file = QAction(self) self.open_file.setObjectName("open_file") self.open_file.setText("Открыть файл") self.menu.addAction(self.open_file) self.status_bar = QStatusBar(self) self.status_bar.setObjectName("status_bar") self.setStatusBar(self.status_bar) self.menu_bar.addAction(self.menu.menuAction()) QMetaObject.connectSlotsByName(self)
class Ui_Img(object): def setupUi(self, Img): if not Img.objectName(): Img.setObjectName(u"Img") Img.resize(620, 559) self.gridLayout = QGridLayout(Img) self.gridLayout.setObjectName(u"gridLayout") self.graphicsView = QGraphicsView(Img) self.graphicsView.setObjectName(u"graphicsView") self.gridLayout.addWidget(self.graphicsView, 0, 0, 1, 1) self.verticalLayout = QVBoxLayout() self.verticalLayout.setObjectName(u"verticalLayout") self.line_3 = QFrame(Img) self.line_3.setObjectName(u"line_3") self.line_3.setFrameShape(QFrame.VLine) self.line_3.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_3) self.verticalLayout_2 = QVBoxLayout() self.verticalLayout_2.setObjectName(u"verticalLayout_2") self.line_5 = QFrame(Img) self.line_5.setObjectName(u"line_5") self.line_5.setFrameShape(QFrame.VLine) self.line_5.setFrameShadow(QFrame.Sunken) self.verticalLayout_2.addWidget(self.line_5) self.checkBox = QCheckBox(Img) self.checkBox.setObjectName(u"checkBox") self.checkBox.setMaximumSize(QSize(100, 16777215)) self.checkBox.setChecked(True) self.verticalLayout_2.addWidget(self.checkBox) self.ttaModel = QCheckBox(Img) self.ttaModel.setObjectName(u"ttaModel") self.ttaModel.setMaximumSize(QSize(70, 16777215)) self.verticalLayout_2.addWidget(self.ttaModel) self.horizontalLayout_3 = QHBoxLayout() self.horizontalLayout_3.setObjectName(u"horizontalLayout_3") self.scaleRadio = QRadioButton(Img) self.buttonGroup_2 = QButtonGroup(Img) self.buttonGroup_2.setObjectName(u"buttonGroup_2") self.buttonGroup_2.addButton(self.scaleRadio) self.scaleRadio.setObjectName(u"scaleRadio") self.scaleRadio.setMaximumSize(QSize(80, 16777215)) self.scaleRadio.setChecked(True) self.horizontalLayout_3.addWidget(self.scaleRadio) self.scaleEdit = QLineEdit(Img) self.scaleEdit.setObjectName(u"scaleEdit") self.scaleEdit.setMaximumSize(QSize(160, 16777215)) self.scaleEdit.setAlignment(Qt.AlignCenter) self.horizontalLayout_3.addWidget(self.scaleEdit) self.verticalLayout_2.addLayout(self.horizontalLayout_3) self.horizontalLayout_4 = QHBoxLayout() self.horizontalLayout_4.setObjectName(u"horizontalLayout_4") self.heighRadio = QRadioButton(Img) self.buttonGroup_2.addButton(self.heighRadio) self.heighRadio.setObjectName(u"heighRadio") self.heighRadio.setMaximumSize(QSize(80, 16777215)) self.horizontalLayout_4.addWidget(self.heighRadio) self.widthEdit = QLineEdit(Img) self.widthEdit.setObjectName(u"widthEdit") self.widthEdit.setEnabled(False) self.widthEdit.setMaximumSize(QSize(60, 16777215)) self.widthEdit.setAlignment(Qt.AlignCenter) self.horizontalLayout_4.addWidget(self.widthEdit) self.label_2 = QLabel(Img) self.label_2.setObjectName(u"label_2") self.label_2.setMaximumSize(QSize(20, 16777215)) self.horizontalLayout_4.addWidget(self.label_2) self.heighEdit = QLineEdit(Img) self.heighEdit.setObjectName(u"heighEdit") self.heighEdit.setEnabled(False) self.heighEdit.setMaximumSize(QSize(60, 16777215)) self.heighEdit.setAlignment(Qt.AlignCenter) self.horizontalLayout_4.addWidget(self.heighEdit) self.verticalLayout_2.addLayout(self.horizontalLayout_4) self.horizontalLayout_5 = QHBoxLayout() self.horizontalLayout_5.setObjectName(u"horizontalLayout_5") self.label_4 = QLabel(Img) self.label_4.setObjectName(u"label_4") self.label_4.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_5.addWidget(self.label_4) self.noiseCombox = QComboBox(Img) self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.addItem("") self.noiseCombox.setObjectName(u"noiseCombox") self.noiseCombox.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_5.addWidget(self.noiseCombox) self.verticalLayout_2.addLayout(self.horizontalLayout_5) self.horizontalLayout_6 = QHBoxLayout() self.horizontalLayout_6.setObjectName(u"horizontalLayout_6") self.label_5 = QLabel(Img) self.label_5.setObjectName(u"label_5") self.label_5.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_6.addWidget(self.label_5) self.comboBox = QComboBox(Img) self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.setObjectName(u"comboBox") self.comboBox.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_6.addWidget(self.comboBox) self.verticalLayout_2.addLayout(self.horizontalLayout_6) self.horizontalLayout_7 = QHBoxLayout() self.horizontalLayout_7.setObjectName(u"horizontalLayout_7") self.changeJpg = QPushButton(Img) self.changeJpg.setObjectName(u"changeJpg") self.changeJpg.setMaximumSize(QSize(100, 16777215)) self.horizontalLayout_7.addWidget(self.changeJpg) self.changePng = QPushButton(Img) self.changePng.setObjectName(u"changePng") self.changePng.setMaximumSize(QSize(100, 16777215)) self.horizontalLayout_7.addWidget(self.changePng) self.changeLabel = QLabel(Img) self.changeLabel.setObjectName(u"changeLabel") self.changeLabel.setMaximumSize(QSize(100, 16777215)) self.changeLabel.setAlignment(Qt.AlignCenter) self.horizontalLayout_7.addWidget(self.changeLabel) self.verticalLayout_2.addLayout(self.horizontalLayout_7) self.horizontalLayout_11 = QHBoxLayout() self.horizontalLayout_11.setObjectName(u"horizontalLayout_11") self.verticalLayout_2.addLayout(self.horizontalLayout_11) self.verticalLayout.addLayout(self.verticalLayout_2) self.line = QFrame(Img) self.line.setObjectName(u"line") self.line.setFrameShape(QFrame.VLine) self.line.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line) self.line_4 = QFrame(Img) self.line_4.setObjectName(u"line_4") self.line_4.setFrameShape(QFrame.HLine) self.line_4.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_4) self.verticalLayout_3 = QVBoxLayout() self.verticalLayout_3.setObjectName(u"verticalLayout_3") self.horizontalLayout_8 = QHBoxLayout() self.horizontalLayout_8.setObjectName(u"horizontalLayout_8") self.label_8 = QLabel(Img) self.label_8.setObjectName(u"label_8") self.label_8.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_8.addWidget(self.label_8) self.resolutionLabel = QLabel(Img) self.resolutionLabel.setObjectName(u"resolutionLabel") self.resolutionLabel.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_8.addWidget(self.resolutionLabel) self.verticalLayout_3.addLayout(self.horizontalLayout_8) self.horizontalLayout_9 = QHBoxLayout() self.horizontalLayout_9.setObjectName(u"horizontalLayout_9") self.label_10 = QLabel(Img) self.label_10.setObjectName(u"label_10") self.label_10.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_9.addWidget(self.label_10) self.sizeLabel = QLabel(Img) self.sizeLabel.setObjectName(u"sizeLabel") self.sizeLabel.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_9.addWidget(self.sizeLabel) self.verticalLayout_3.addLayout(self.horizontalLayout_9) self.horizontalLayout = QHBoxLayout() self.horizontalLayout.setObjectName(u"horizontalLayout") self.label = QLabel(Img) self.label.setObjectName(u"label") self.label.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout.addWidget(self.label) self.gpuName = QLabel(Img) self.gpuName.setObjectName(u"gpuName") self.gpuName.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout.addWidget(self.gpuName) self.verticalLayout_3.addLayout(self.horizontalLayout) self.horizontalLayout_10 = QHBoxLayout() self.horizontalLayout_10.setObjectName(u"horizontalLayout_10") self.label_6 = QLabel(Img) self.label_6.setObjectName(u"label_6") self.label_6.setMaximumSize(QSize(60, 16777215)) self.horizontalLayout_10.addWidget(self.label_6) self.tickLabel = QLabel(Img) self.tickLabel.setObjectName(u"tickLabel") self.tickLabel.setMaximumSize(QSize(160, 16777215)) self.horizontalLayout_10.addWidget(self.tickLabel) self.verticalLayout_3.addLayout(self.horizontalLayout_10) self.oepnButton = QPushButton(Img) self.oepnButton.setObjectName(u"oepnButton") self.oepnButton.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.oepnButton) self.horizontalLayout_2 = QHBoxLayout() self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") self.verticalLayout_3.addLayout(self.horizontalLayout_2) self.pushButton_3 = QPushButton(Img) self.pushButton_3.setObjectName(u"pushButton_3") self.pushButton_3.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.pushButton_3) self.pushButton = QPushButton(Img) self.pushButton.setObjectName(u"pushButton") self.pushButton.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.pushButton) self.saveButton = QPushButton(Img) self.saveButton.setObjectName(u"saveButton") self.saveButton.setMaximumSize(QSize(100, 16777215)) self.verticalLayout_3.addWidget(self.saveButton) self.verticalLayout.addLayout(self.verticalLayout_3) self.line_6 = QFrame(Img) self.line_6.setObjectName(u"line_6") self.line_6.setFrameShape(QFrame.HLine) self.line_6.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_6) self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.verticalLayout.addItem(self.verticalSpacer) self.line_2 = QFrame(Img) self.line_2.setObjectName(u"line_2") self.line_2.setFrameShape(QFrame.VLine) self.line_2.setFrameShadow(QFrame.Sunken) self.verticalLayout.addWidget(self.line_2) self.gridLayout.addLayout(self.verticalLayout, 0, 1, 1, 1) self.retranslateUi(Img) self.checkBox.clicked.connect(Img.SwithPicture) self.saveButton.clicked.connect(Img.SavePicture) self.heighEdit.textChanged.connect(Img.CheckScaleRadio) self.pushButton_3.clicked.connect(Img.ReduceScalePic) self.heighRadio.clicked.connect(Img.CheckScaleRadio) self.ttaModel.clicked.connect(Img.CheckScaleRadio) self.oepnButton.clicked.connect(Img.OpenPicture) self.widthEdit.textChanged.connect(Img.CheckScaleRadio) self.pushButton.clicked.connect(Img.AddScalePic) self.scaleEdit.textChanged.connect(Img.CheckScaleRadio) self.scaleRadio.clicked.connect(Img.CheckScaleRadio) self.comboBox.currentIndexChanged.connect(Img.ChangeModel) self.changeJpg.clicked.connect(Img.StartWaifu2x) self.noiseCombox.currentIndexChanged.connect(Img.CheckScaleRadio) self.changePng.clicked.connect(Img.StartWaifu2xPng) self.changeJpg.clicked.connect(Img.StartWaifu2xJPG) QMetaObject.connectSlotsByName(Img) # setupUi def retranslateUi(self, Img): Img.setWindowTitle(QCoreApplication.translate("Img", u"Form", None)) self.checkBox.setText( QCoreApplication.translate("Img", u"waifu2x", None)) #if QT_CONFIG(tooltip) self.ttaModel.setToolTip( QCoreApplication.translate( "Img", u"\u753b\u8d28\u63d0\u5347\uff0c\u8017\u65f6\u589e\u52a0", None)) #endif // QT_CONFIG(tooltip) self.ttaModel.setText( QCoreApplication.translate("Img", u"tta\u6a21\u5f0f", None)) self.scaleRadio.setText( QCoreApplication.translate("Img", u"\u500d\u6570\u653e\u5927", None)) self.scaleEdit.setText(QCoreApplication.translate("Img", u"2", None)) self.heighRadio.setText( QCoreApplication.translate("Img", u"\u56fa\u5b9a\u957f\u5bbd", None)) self.label_2.setText(QCoreApplication.translate("Img", u"x", None)) self.label_4.setText( QCoreApplication.translate("Img", u"\u964d\u566a\uff1a", None)) self.noiseCombox.setItemText( 0, QCoreApplication.translate("Img", u"3", None)) self.noiseCombox.setItemText( 1, QCoreApplication.translate("Img", u"2", None)) self.noiseCombox.setItemText( 2, QCoreApplication.translate("Img", u"1", None)) self.noiseCombox.setItemText( 3, QCoreApplication.translate("Img", u"0", None)) self.noiseCombox.setItemText( 4, QCoreApplication.translate("Img", u"-1", None)) self.label_5.setText( QCoreApplication.translate("Img", u"\u6a21\u578b\uff1a", None)) self.comboBox.setItemText( 0, QCoreApplication.translate("Img", u"cunet", None)) self.comboBox.setItemText( 1, QCoreApplication.translate("Img", u"photo", None)) self.comboBox.setItemText( 2, QCoreApplication.translate("Img", u"anime_style_art_rgb", None)) self.changeJpg.setText( QCoreApplication.translate("Img", u"\u8f6c\u6362JPG", None)) self.changePng.setText( QCoreApplication.translate("Img", u"\u8f6c\u6362PNG", None)) self.changeLabel.setText("") self.label_8.setText( QCoreApplication.translate("Img", u"\u5206\u8fa8\u7387\uff1a", None)) self.resolutionLabel.setText( QCoreApplication.translate("Img", u"TextLabel", None)) self.label_10.setText( QCoreApplication.translate("Img", u"\u5927 \u5c0f\uff1a", None)) self.sizeLabel.setText( QCoreApplication.translate("Img", u"TextLabel", None)) self.label.setText(QCoreApplication.translate("Img", u"GPU:", None)) self.gpuName.setText( QCoreApplication.translate("Img", u"TextLabel", None)) self.label_6.setText( QCoreApplication.translate("Img", u"\u8017\u65f6\uff1a", None)) self.tickLabel.setText("") self.oepnButton.setText( QCoreApplication.translate("Img", u"\u6253\u5f00\u56fe\u7247", None)) self.pushButton_3.setText( QCoreApplication.translate("Img", u"\u7f29\u5c0f", None)) self.pushButton.setText( QCoreApplication.translate("Img", u"\u653e\u5927", None)) self.saveButton.setText( QCoreApplication.translate("Img", u"\u4fdd\u5b58\u56fe\u7247", None))
class QRScanner(QWidget): QR_SIZE = 0.75 # Size of rectangle for QR capture readyForCapture = Signal(bool) decodedQR = Signal(str) def __init__(self, parent=None): QWidget.__init__(self, parent) self.processing = False self.rectangle = None self.setMinimumHeight(405) self.layout = QVBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) self.scene = QGraphicsScene(self) self.scene.setBackgroundBrush(QBrush(Qt.black)) self.view = QGraphicsView(self.scene) self.viewfinder = QGraphicsVideoItem() self.scene.addItem(self.viewfinder) self.layout.addWidget(self.view) self.setLayout(self.layout) self.camera = None self.captureSession = None self.imageCapture = None self.captureTimer = None def startScan(self): if len(QMediaDevices.videoInputs()) == 0: logging.warning(self.tr("There are no cameras available")) return self.processing = True # disable any capture while camera is starting self.camera = QCamera(QMediaDevices.defaultVideoInput()) self.captureSession = QMediaCaptureSession() self.imageCapture = QImageCapture(self.camera) self.captureSession.setCamera(self.camera) self.captureSession.setVideoOutput(self.viewfinder) self.captureSession.setImageCapture(self.imageCapture) self.camera.errorOccurred.connect(self.onCameraError) self.readyForCapture.connect(self.onReadyForCapture) self.imageCapture.errorOccurred.connect(self.onCaptureError) self.imageCapture.readyForCaptureChanged.connect( self.onReadyForCapture) self.imageCapture.imageCaptured.connect(self.onImageCaptured) self.viewfinder.nativeSizeChanged.connect(self.onVideoSizeChanged) self.camera.start() self.processing = False self.readyForCapture.emit(self.imageCapture.isReadyForCapture()) def stopScan(self): if self.camera is None: return self.processing = True # disable capture self.camera.stop() self.camera = None self.captureSession = None self.imageCapture = None self.captureTimer = None def onVideoSizeChanged(self, _size): self.resizeEvent(None) # Take QImage or QRect (object with 'width' and 'height' properties and calculate position and size # of the square with side of self.QR_SIZE from minimum of height or width def calculate_center_square(self, img_rect) -> QRectF: a = self.QR_SIZE * min(img_rect.height(), img_rect.width()) # Size of square side x = (img_rect.width() - a) / 2 # Position of the square inside rectangle y = (img_rect.height() - a) / 2 if type(img_rect ) != QImage: # if we have a bounding rectangle, not an image x += img_rect.left( ) # then we need to shift our square inside this rectangle y += img_rect.top() return QRectF(x, y, a, a) def resizeEvent(self, event): bounds = self.scene.itemsBoundingRect() if bounds.width() <= 0 or bounds.height() <= 0: return # do nothing if size is zero self.view.fitInView(bounds, Qt.KeepAspectRatio) if self.rectangle is not None: self.scene.removeItem(self.rectangle) pen = QPen(Qt.green) pen.setWidth(0) pen.setStyle(Qt.DashLine) self.rectangle = self.scene.addRect( self.calculate_center_square(bounds), pen) self.view.centerOn(0, 0) self.view.raise_() def onCaptureError(self, _id, error, error_str): self.processing = False self.onCameraError(error, error_str) def onCameraError(self, error, error_str): logging.error( self.tr("Camera error: " + str(error) + " / " + error_str)) def onReadyForCapture(self, ready: bool): if ready and not self.processing: self.imageCapture.capture() self.processing = True def onImageCaptured(self, _id: int, img: QImage): self.decodeQR(img) self.processing = False if self.imageCapture is not None: self.readyForCapture.emit(self.imageCapture.isReadyForCapture()) def decodeQR(self, qr_image: QImage): cropped = qr_image.copy( self.calculate_center_square(qr_image).toRect()) # TODO: the same code is present in slips.py -> move to one place buffer = QBuffer() buffer.open(QBuffer.ReadWrite) cropped.save(buffer, "BMP") try: pillow_image = Image.open(io.BytesIO(buffer.data())) except UnidentifiedImageError: print("Image format isn't supported") return barcodes = pyzbar.decode(pillow_image, symbols=[pyzbar.ZBarSymbol.QRCODE]) if barcodes: self.decodedQR.emit(barcodes[0].data.decode('utf-8'))