PySide 缩放缩放

问题描述 投票:0回答:0

我正在做一个小的个人项目,以在工作时娱乐自己。这是一个用 Python 编写的小型绘图应用程序。画布设置为在 GUI 内缩放,这样它就不会超出导致它扩展的 GUI 的边界。

我想要完成的是保持默认缩放比例,同时能够根据需要手动缩放画布,这样如果有人想放大以向绘图添加细节,他们可以这样做。我也想做反转或缩小。我认为每个实例以 10% 的增量放大或缩小是有意义的。

要控制缩放比例,我想使用 Ctrl+ 和 Ctrl-。

我已经尝试了一些东西,比如定义放大/缩小,试图将它连接到 self.scaling_factor,但无济于事,它不起作用。我曾经能够让它工作得非常糟糕,但它似乎也没有朝着正确的方向发展。画布基本上可以重新缩放画布,扩展 GUI,然后打破整个程序。

这里是参考代码:

class Canvas(QMainWindow):
     def __init__(self, canvas_width, canvas_height):
        super(Canvas, self).__init__()

        # Canvas information
        self.setWindowTitle("Assam 2023")

        # Max canvas size
        max_size = QSize(1920, 1080)

        # Calc
        ratio = min(canvas_width / max_size.width(), canvas_height / max_size.height())
        self.scaling_factor = 0.5 if ratio <= 1 else 0.5 / ratio

        # Canvas window size policy
        self.canvas_widget = QWidget()
        self.canvas_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # Canvas gui creation
        screen = QApplication.primaryScreen()
        rect = screen.availableGeometry()
        self.setGeometry(rect)

        # Canvas
        self.scene = QGraphicsScene()
        self.scene.setBackgroundBrush(QBrush(Qt.white))
        self.view = QGraphicsView(self.scene)
        self.view.setRenderHint(QPainter.Antialiasing)
        self.view.setFixedSize(canvas_width * self.scaling_factor, canvas_height * self.scaling_factor)
        self.view.setFrameShape(QFrame.NoFrame)
        self.view.setTransform(QTransform().scale(self.scaling_factor, self.scaling_factor))

        # More canvas setup
        layout = QVBoxLayout()
        layout.addWidget(self.view, alignment=Qt.AlignCenter)
        self.canvas_widget.setLayout(layout)

        # Add canvas widget to main window
        self.setCentralWidget(self.canvas_widget)

class Window(QWidget):
    def __init__(self):
        super().__init__()

        # Gui information
        self.setWindowTitle("New Canvas")
        self.setFixedSize(300, 250)

        # Add a combo box for selecting the unit of measurement
        unit_label = QLabel("Unit:")
        self.unit_combobox = QComboBox()
        self.unit_combobox.addItems(["Pixels", "Inches", "Centimeters"])
        self.unit_combobox.currentTextChanged.connect(self.update_unit_label)

        # Add labels and text boxes for entering width and height
        width_label = QLabel("Width:")
        self.width_textbox = QLineEdit() 
        height_label = QLabel("Height:")
        self.height_textbox = QLineEdit()
        # Add a label for displaying the selected unit of measurement
        self.unit__display_label = QLabel("Pixels")

        # Add a text box for entering the PPI value
        self.ppi_label = QLabel("PPI:")
        self.ppi_textbox = QLineEdit()
        self.ppi_textbox.setEnabled(False)

        # Add a button for creating the canvas
        open_canvas = QPushButton("Open")
        create_canvas_button = QPushButton("Create Canvas")

        # Add a layout to organize the widgets
        form_layout = QFormLayout()
        form_layout.addRow(unit_label, self.unit_combobox)
        form_layout.addRow(self.ppi_label, self.ppi_textbox)
        form_layout.addRow(width_label, self.width_textbox)
        form_layout.addRow(height_label, self.height_textbox)
        form_layout.addRow(QLabel(""), self.unit__display_label)

        layout = QVBoxLayout()
        layout.addLayout(form_layout)
        layout.addWidget(create_canvas_button)
        layout.addWidget(open_canvas)
        self.setLayout(layout)

        # Connect the button click to create_canvas function
        open_canvas.clicked.connect(self.open_canvas)
        create_canvas_button.clicked.connect(self.create_canvas)

    def update_unit_label(self, text):
        self.unit__display_label.setText(text)
        if text == "Pixels":
            self.ppi_textbox.setEnabled(False)
            self.ppi_label.setText("PPI:")
        elif text == "Inches":
            self.ppi_textbox.setEnabled(True)
            self.ppi_label.setText("PPI:")
        elif text == "Centimeters":
            self.ppi_textbox.setEnabled(True)
            self.ppi_label.setText("PPcm:")
        
    def open_canvas(self):    
        options = QFileDialog.Options()
        #options |=  QFileDialog.DontUseNativeDialog
        file_name, _ = QFileDialog.getOpenFileName(self, "Open Canvas", "", "Images (*.jpg)", options=options)
        if file_name:
            with open(file_name, 'r') as file:
                canvas_size = file.read().split(",")
            if len(canvas_size) == 2:
                canvas_width, canvas_height = canvas_size
                self.canvas = Canvas(int(canvas_width), int(canvas_height))
                self.canvas.show()    
    
    def create_canvas(self):
        # Get the selected unit from the combo box
        selected_unit = self.unit_combobox.currentText()

        # Get the values of width and height from the text boxes
        width = int(self.width_textbox.text())
        height = int(self.height_textbox.text())

        # Calculate the canvas size based on the selected unit of measurement
        if selected_unit == "Pixels":
            canvas_width = width
            canvas_height = height
        elif selected_unit == "Inches":
            ppi = int(self.ppi_textbox.text()) if self.ppi_textbox.text() else 96 # default PPI value is 96
            canvas_width = width * ppi
            canvas_height = height * ppi
        elif selected_unit == "Centimeters":
            ppi = int(self.ppi_textbox.text()) if self.ppi_textbox.text() else 96 # default PPI value is 96
            canvas_width = width * 37.8 * ppi / 96
            canvas_height = height * 37.8 * ppi / 96

        # Create a new canvas with the specified dimensions
        self.canvas = Canvas(canvas_width, canvas_height)
        self.canvas.show()

        # Close the new canvas window
        self.close()
python drawing
© www.soinside.com 2019 - 2024. All rights reserved.