diff --git a/zxlive/dialogs.py b/zxlive/dialogs.py index e60eb883..1458ae8c 100644 --- a/zxlive/dialogs.py +++ b/zxlive/dialogs.py @@ -220,25 +220,20 @@ def save_diagram_dialog(graph: GraphT, parent: QWidget) -> Optional[tuple[str, F return file_path, selected_format -def safe_proof_dialog(proof_model: ProofModel, parent: QWidget) -> Optional[tuple[str, FileFormat]]: - file_path_and_format = get_file_path_and_format(parent, FileFormat.ZXProof.filter) +def _save_rule_or_proof_dialog(data: str, parent: QWidget, filter: str) -> Optional[tuple[str, FileFormat]]: + file_path_and_format = get_file_path_and_format(parent, filter) if file_path_and_format is None or not file_path_and_format[0]: return None file_path, selected_format = file_path_and_format - data = proof_model.to_json() if not write_to_file(file_path, data): return None return file_path, selected_format -def safe_rule_dialog(rule: CustomRule, parent: QWidget) -> Optional[tuple[str, FileFormat]]: - file_path_and_format = get_file_path_and_format(parent, FileFormat.ZXRule.filter) - if file_path_and_format is None or not file_path_and_format[0]: - return None - file_path, selected_format = file_path_and_format - data = rule.to_json() - if not write_to_file(file_path, data): - return None - return file_path, selected_format +def save_proof_dialog(proof_model: ProofModel, parent: QWidget) -> Optional[tuple[str, FileFormat]]: + return _save_rule_or_proof_dialog(proof_model.to_json(), parent, FileFormat.ZXProof.filter) + +def save_rule_dialog(rule: CustomRule, parent: QWidget) -> Optional[tuple[str, FileFormat]]: + return _save_rule_or_proof_dialog(rule.to_json(), parent, FileFormat.ZXRule.filter) def export_proof_dialog(parent: QWidget) -> Optional[str]: file_path_and_format = get_file_path_and_format(parent, FileFormat.TikZ.filter) @@ -294,7 +289,7 @@ def add_rewrite() -> None: return rule = CustomRule(parent.left_graph, parent.right_graph, name.text(), description.toPlainText()) check_rule(rule, show_error=True) - if safe_rule_dialog(rule, parent): + if save_rule_dialog(rule, parent): dialog.accept() button_box.accepted.connect(add_rewrite) button_box.rejected.connect(dialog.reject) diff --git a/zxlive/mainwindow.py b/zxlive/mainwindow.py index 655332de..b4db2792 100644 --- a/zxlive/mainwindow.py +++ b/zxlive/mainwindow.py @@ -36,8 +36,8 @@ from .custom_rule import CustomRule, check_rule from .dialogs import (FileFormat, ImportGraphOutput, ImportProofOutput, ImportRuleOutput, create_new_rewrite, - save_diagram_dialog, safe_proof_dialog, - safe_rule_dialog, get_lemma_name_and_description, + save_diagram_dialog, save_proof_dialog, + save_rule_dialog, get_lemma_name_and_description, import_diagram_dialog, import_diagram_from_file, show_error_msg, export_proof_dialog) from zxlive.settings_dialog import open_settings_dialog @@ -365,10 +365,10 @@ def handle_save_file_action(self) -> bool: def handle_save_as_action(self) -> bool: assert self.active_panel is not None if isinstance(self.active_panel, ProofPanel): - out = safe_proof_dialog(self.active_panel.proof_model, self) + out = save_proof_dialog(self.active_panel.proof_model, self) elif isinstance(self.active_panel, RulePanel): check_rule(self.active_panel.get_rule(), show_error=True) - out = safe_rule_dialog(self.active_panel.get_rule(), self) + out = save_rule_dialog(self.active_panel.get_rule(), self) else: out = save_diagram_dialog(self.active_panel.graph_scene.g, self) if out is None: return False @@ -548,8 +548,8 @@ def proof_as_lemma(self) -> None: lhs_graph = self.active_panel.proof_model.graphs[0] rhs_graph = self.active_panel.proof_model.graphs[-1] rule = CustomRule(lhs_graph, rhs_graph, name, description) - safe_rule_dialog(rule, self) + save_rule_dialog(rule, self) 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()