Skip to content

Commit

Permalink
Merge pull request #237 from dlyongemallo/dialogs_parent
Browse files Browse the repository at this point in the history
Make a bunch of dialog windows modal by passing in parent parameter.
  • Loading branch information
jvdwetering authored Apr 20, 2024
2 parents eadb4c5 + d4a0f4f commit 177792f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
25 changes: 13 additions & 12 deletions zxlive/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def import_diagram_dialog(parent: QWidget) -> Optional[ImportGraphOutput | Impor
# This happens if the user clicks on cancel
return None

return import_diagram_from_file(file_path, selected_filter)
return import_diagram_from_file(file_path, selected_filter, parent)


def create_circuit_dialog(explanation: str, example: str, parent: QWidget) -> Optional[str]:
Expand Down Expand Up @@ -127,7 +127,7 @@ def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.A
try:
selected_format = next(f for f in FileFormat if f.extension == ext)
except StopIteration:
show_error_msg("Failed to import file", f"Couldn't determine filetype: {file_path}.")
show_error_msg("Failed to import file", f"Couldn't determine filetype: {file_path}.", parent=parent)
return None

# TODO: This would be nicer with match statements (requires python 3.10 though)...
Expand Down Expand Up @@ -157,17 +157,18 @@ def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.A
try:
return ImportGraphOutput(FileFormat.TikZ, file_path, GraphT.from_tikz(data)) # type: ignore
except:
show_error_msg(f"Failed to import {selected_format.name} file", f"Couldn't determine filetype: {file_path}.")
show_error_msg(f"Failed to import {selected_format.name} file",
f"Couldn't determine filetype: {file_path}.", parent=parent)
return None

except Exception as e:
show_error_msg(f"Failed to import {selected_format.name} file: {file_path}", str(e))
show_error_msg(f"Failed to import {selected_format.name} file: {file_path}", str(e), parent=parent)
return None

def write_to_file(file_path: str, data: str) -> bool:
def write_to_file(file_path: str, data: str, parent: QWidget) -> bool:
file = QFile(file_path)
if not file.open(QIODevice.OpenModeFlag.WriteOnly | QIODevice.OpenModeFlag.Text):
show_error_msg("Could not write to file")
show_error_msg("Could not write to file", parent=parent)
return False
out = QTextStream(file)
out << data
Expand All @@ -192,7 +193,7 @@ def get_file_path_and_format(parent: QWidget, filter: str, default_input: str =
ext = file_path.split(".")[-1]
selected_format = next(f for f in FileFormat if f.extension == ext)
except StopIteration:
show_error_msg("Unable to determine file format.")
show_error_msg("Unable to determine file format.", parent=parent)
return None

# Add file extension if it's not already there
Expand All @@ -213,14 +214,14 @@ def save_diagram_dialog(graph: GraphT, parent: QWidget) -> Optional[tuple[str, F
try:
circuit = extract_circuit(graph)
except Exception as e:
show_error_msg("Failed to convert the diagram to a circuit", str(e))
show_error_msg("Failed to convert the diagram to a circuit", str(e), parent=parent)
return None
data = circuit.to_qasm()
else:
assert selected_format == FileFormat.TikZ
data = graph.to_tikz()

if not write_to_file(file_path, data):
if not write_to_file(file_path, data, parent):
return None

return file_path, selected_format
Expand All @@ -230,7 +231,7 @@ def _save_rule_or_proof_dialog(data: str, parent: QWidget, filter: str, filename
if file_path_and_format is None or not file_path_and_format[0]:
return None
file_path, selected_format = file_path_and_format
if not write_to_file(file_path, data):
if not write_to_file(file_path, data, parent):
return None
return file_path, selected_format

Expand All @@ -247,7 +248,7 @@ def export_proof_dialog(parent: QWidget) -> Optional[str]:
return file_path_and_format[0]

def get_lemma_name_and_description(parent: MainWindow) -> tuple[Optional[str], Optional[str]]:
dialog = QDialog()
dialog = QDialog(parent)
rewrite_form = QFormLayout(dialog)
name = QLineEdit()
rewrite_form.addRow("Name", name)
Expand All @@ -262,7 +263,7 @@ def get_lemma_name_and_description(parent: MainWindow) -> tuple[Optional[str], O
return None, None

def create_new_rewrite(parent: MainWindow) -> None:
dialog = QDialog()
dialog = QDialog(parent)
rewrite_form = QFormLayout(dialog)
name = QLineEdit()
rewrite_form.addRow("Name", name)
Expand Down
7 changes: 4 additions & 3 deletions zxlive/edit_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _toolbar_sections(self) -> Iterator[ToolbarSection]:

def _start_derivation(self) -> None:
if not self.graph_scene.g.is_well_formed():
show_error_msg("Graph is not well-formed")
show_error_msg("Graph is not well-formed", parent=self)
return
new_g: GraphT = copy.deepcopy(self.graph_scene.g)
for vert in new_g.vertices():
Expand Down Expand Up @@ -96,10 +96,11 @@ def _input_circuit(self) -> None:
else:
circ = QASMParser().parse(qasm, strict=False).to_graph()
except TypeError as err:
show_error_msg("Invalid circuit", str(err))
show_error_msg("Invalid circuit", str(err), parent=self)
return
except Exception:
show_error_msg("Invalid circuit", f"Couldn't parse code as {input_circuit_formats[circuit_format]}.")
show_error_msg("Invalid circuit",
f"Couldn't parse code as {input_circuit_formats[circuit_format]}.", parent=self)
return

new_verts, new_edges = new_g.merge(circ)
Expand Down
2 changes: 1 addition & 1 deletion zxlive/editor_base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def vert_double_clicked(self, v: VT) -> None:
try:
new_phase = string_to_complex(input_) if phase_is_complex else string_to_phase(input_, graph)
except ValueError:
show_error_msg("Invalid Input", error_msg)
show_error_msg("Invalid Input", error_msg, parent=self)
return None
cmd = ChangePhase(self.graph_view, v, new_phase)
self.undo_stack.push(cmd)
Expand Down
18 changes: 10 additions & 8 deletions zxlive/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def open_file(self) -> None:
self._open_file_from_output(out)

def open_file_from_path(self, file_path: str) -> None:
out = import_diagram_from_file(file_path)
out = import_diagram_from_file(file_path, parent=self)
if out is not None:
self._open_file_from_output(out)

Expand Down Expand Up @@ -335,7 +335,8 @@ def handle_save_file_action(self) -> bool:
return self.handle_save_as_action()
if self.active_panel.file_type == FileFormat.QASM:
show_error_msg("Can't save to circuit file",
"You imported this file from a circuit description. You can currently only save it in a graph format.")
"You imported this file from a circuit description. You can currently only save it in a graph format.",
parent=self)
return self.handle_save_as_action()

if isinstance(self.active_panel, ProofPanel):
Expand All @@ -352,7 +353,7 @@ def handle_save_file_action(self) -> bool:

file = QFile(self.active_panel.file_path)
if not file.open(QIODevice.OpenModeFlag.WriteOnly | QIODevice.OpenModeFlag.Text):
show_error_msg("Could not write to file")
show_error_msg("Could not write to file", parent=self)
return False
out = QTextStream(file)
out << data
Expand Down Expand Up @@ -384,7 +385,7 @@ def handle_export_tikz_proof_action(self) -> bool:
assert isinstance(self.active_panel, ProofPanel)
path = export_proof_dialog(self)
if path is None:
show_error_msg("Export failed", "Invalid path")
show_error_msg("Export failed", "Invalid path", parent=self)
return False
with open(path, "w") as f:
f.write(proof_to_tikz(self.active_panel.proof_model))
Expand Down Expand Up @@ -414,7 +415,7 @@ def paste_graph(self) -> None:
if (isinstance(self.active_panel, GraphEditPanel) or isinstance(self.active_panel, RulePanel)) \
and self.copied_graph is not None:
self.active_panel.paste_graph(self.copied_graph)

def paste_graph_from_clipboard(self) -> None:
assert self.active_panel is not None
if isinstance(self.active_panel, GraphEditPanel) or isinstance(self.active_panel, RulePanel):
Expand Down Expand Up @@ -519,12 +520,13 @@ def format_str(c: complex) -> str:
self.active_panel.graph.auto_detect_io()
matrix = self.active_panel.graph.to_matrix()
except AttributeError:
show_error_msg("Can't show matrix", "Showing matrices for parametrized diagrams is not supported yet.")
show_error_msg("Can't show matrix",
"Showing matrices for parametrized diagrams is not supported yet.", parent=self)
return
except Exception as e:
show_error_msg("Can't show matrix", str(e))
show_error_msg("Can't show matrix", str(e), parent=self)
return
dialog = QDialog()
dialog = QDialog(self)
dialog.setWindowTitle("Matrix")
table = QTableWidget()
table.setRowCount(matrix.shape[0])
Expand Down
2 changes: 1 addition & 1 deletion zxlive/proof_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def cross(a: QPointF, b: QPointF) -> float:
try:
phase = string_to_complex(text) if phase_is_complex else string_to_phase(text, self.graph)
except ValueError:
show_error_msg("Invalid Input", error_msg)
show_error_msg("Invalid Input", error_msg, parent=self)
return False
elif self.graph.type(vertex) != VertexType.W_OUTPUT:
if self.graph.type(vertex) == VertexType.Z_BOX:
Expand Down

0 comments on commit 177792f

Please sign in to comment.