From 057fb8f46b70b559d0811963dd418e66eef3bc38 Mon Sep 17 00:00:00 2001 From: David Yonge-Mallo Date: Thu, 16 Nov 2023 13:33:20 +0100 Subject: [PATCH] Transfer ownership of variables used only in `create_new_rewrite` to that function. --- zxlive/dialogs.py | 35 ++++++++++++++++++----------------- zxlive/mainwindow.py | 6 +----- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/zxlive/dialogs.py b/zxlive/dialogs.py index e60eb883..fec0c4e3 100644 --- a/zxlive/dialogs.py +++ b/zxlive/dialogs.py @@ -248,13 +248,13 @@ def export_proof_dialog(parent: QWidget) -> Optional[str]: def get_lemma_name_and_description(parent: MainWindow) -> tuple[Optional[str], Optional[str]]: dialog = QDialog() - parent.rewrite_form = QFormLayout(dialog) + rewrite_form = QFormLayout(dialog) name = QLineEdit() - parent.rewrite_form.addRow("Name", name) + rewrite_form.addRow("Name", name) description = QTextEdit() - parent.rewrite_form.addRow("Description", description) + rewrite_form.addRow("Description", description) button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) - parent.rewrite_form.addRow(button_box) + rewrite_form.addRow(button_box) button_box.accepted.connect(dialog.accept) button_box.rejected.connect(dialog.reject) if dialog.exec() == QDialog.DialogCode.Accepted: @@ -263,36 +263,37 @@ def get_lemma_name_and_description(parent: MainWindow) -> tuple[Optional[str], O def create_new_rewrite(parent: MainWindow) -> None: dialog = QDialog() - parent.rewrite_form = QFormLayout(dialog) + rewrite_form = QFormLayout(dialog) name = QLineEdit() - parent.rewrite_form.addRow("Name", name) + rewrite_form.addRow("Name", name) description = QTextEdit() - parent.rewrite_form.addRow("Description", description) + rewrite_form.addRow("Description", description) left_button = QPushButton("Left-hand side of the rule") right_button = QPushButton("Right-hand side of the rule") - parent.left_graph = None - parent.right_graph = None + left_graph = None + right_graph = None def get_file(self: MainWindow, button: QPushButton, side: str) -> None: + nonlocal left_graph, right_graph out = import_diagram_dialog(self) if out is not None and isinstance(out, ImportGraphOutput): button.setText(out.file_path) if side == "left": - self.left_graph = out.g + left_graph = out.g else: - self.right_graph = out.g + right_graph = out.g left_button.clicked.connect(lambda: get_file(parent, left_button, "left")) right_button.clicked.connect(lambda: get_file(parent, right_button, "right")) - parent.rewrite_form.addRow(left_button) - parent.rewrite_form.addRow(right_button) + rewrite_form.addRow(left_button) + rewrite_form.addRow(right_button) button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) - parent.rewrite_form.addRow(button_box) + rewrite_form.addRow(button_box) def add_rewrite() -> None: - if parent.left_graph is None or parent.right_graph is None or \ - name.text() == "" or description.toPlainText() == "": + nonlocal left_graph, right_graph + if left_graph is None or right_graph is None or name.text() == "" or description.toPlainText() == "": return - rule = CustomRule(parent.left_graph, parent.right_graph, name.text(), description.toPlainText()) + rule = CustomRule(left_graph, right_graph, name.text(), description.toPlainText()) check_rule(rule, show_error=True) if safe_rule_dialog(rule, parent): dialog.accept() diff --git a/zxlive/mainwindow.py b/zxlive/mainwindow.py index 8480772f..51576c45 100644 --- a/zxlive/mainwindow.py +++ b/zxlive/mainwindow.py @@ -48,10 +48,6 @@ class MainWindow(QMainWindow): """The main window of the ZXLive application.""" - rewrite_form: QFormLayout - left_graph: Optional[GraphT] - right_graph: Optional[GraphT] - def __init__(self) -> None: super().__init__() self.settings = QSettings("zxlive", "zxlive") @@ -546,4 +542,4 @@ def proof_as_lemma(self) -> None: def update_colors(self) -> None: if self.active_panel is not None: - self.active_panel.update_colors() \ No newline at end of file + self.active_panel.update_colors()