Skip to content

Commit

Permalink
Refactor and combine save_rule_dialog and save_proof_dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlyongemallo committed Nov 20, 2023
1 parent 4be636f commit 35e104b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
21 changes: 8 additions & 13 deletions zxlive/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions zxlive/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
self.active_panel.update_colors()

0 comments on commit 35e104b

Please sign in to comment.