Skip to content

Commit

Permalink
making renaming undoable
Browse files Browse the repository at this point in the history
  • Loading branch information
RazinShaikh committed Jul 4, 2024
1 parent 138ff48 commit ec13b1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
31 changes: 8 additions & 23 deletions zxlive/proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ def data(self, index: Union[QModelIndex, QPersistentModelIndex], role: int=Qt.It
elif role == Qt.ItemDataRole.FontRole:
return QFont("monospace", 12)

def setData(self, index: Union[QModelIndex, QPersistentModelIndex], value: Any, role: int=Qt.ItemDataRole.EditRole) -> bool:
if role == Qt.ItemDataRole.EditRole:
self.rename_step(index.row()-1, value)
return True
return False

def flags(self, index: Union[QModelIndex, QPersistentModelIndex]) -> Qt.ItemFlag:
if index.row() == 0:
return super().flags(index)
Expand Down Expand Up @@ -227,7 +221,6 @@ def __init__(self, parent: 'ProofPanel'):
self.viewport().setAttribute(Qt.WidgetAttribute.WA_Hover)
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_context_menu)
# self.doubleClicked.connect(self.double_click_handler)
self.selectionModel().selectionChanged.connect(self.proof_step_selected)

# overriding this method to change the return type and stop mypy from complaining
Expand Down Expand Up @@ -258,7 +251,7 @@ def show_context_menu(self, position: QPoint) -> None:
action_function_map[group_action] = self.group_selected_steps
elif index != 0:
rename_action = context_menu.addAction("Rename Step")
action_function_map[rename_action] = lambda: self.rename_proof_step(index - 1)
action_function_map[rename_action] = lambda: self.edit(selected_indexes[0])
if self.model().steps[index - 1].grouped_rewrites is not None:
ungroup_action = context_menu.addAction("Ungroup Steps")
action_function_map[ungroup_action] = self.ungroup_selected_step
Expand All @@ -267,22 +260,14 @@ def show_context_menu(self, position: QPoint) -> None:
if action in action_function_map:
action_function_map[action]()

# def double_click_handler(self, index: Union[QModelIndex, QPersistentModelIndex]) -> None:
# # The first row in the item list is the START step, which is not interactive
# if index.row() == 0:
# return
# self.rename_proof_step(index.row()-1)

def rename_proof_step(self, index: int) -> None:
def rename_proof_step(self, new_name: str, index: int) -> None:
from .commands import UndoableChange
new_name, ok = QInputDialog.getText(self, "Rename proof step", "Enter new name")
if ok:
old_name = self.model().steps[index].display_name
cmd = UndoableChange(self.graph_view,
lambda: self.model().rename_step(index, old_name),
lambda: self.model().rename_step(index, new_name)
)
self.undo_stack.push(cmd)
old_name = self.model().steps[index].display_name
cmd = UndoableChange(self.graph_view,
lambda: self.model().rename_step(index, old_name),
lambda: self.model().rename_step(index, new_name)
)
self.undo_stack.push(cmd)

def proof_step_selected(self, selected: QItemSelection, deselected: QItemSelection) -> None:
if not selected or not deselected:
Expand Down
6 changes: 4 additions & 2 deletions zxlive/proof_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, graph: GraphT, *actions: QAction) -> None:
self.graph_scene.edge_dragged.connect(self.change_edge_curves)

self.step_view = ProofStepView(self)
self.step_view.setItemDelegate(ProofStepItemDelegate())
self.step_view.setItemDelegate(ProofStepItemDelegate(self.step_view))

self.splitter.addWidget(self.step_view)

Expand Down Expand Up @@ -473,5 +473,7 @@ def setEditorData(self, editor: QWidget, index: Union[QModelIndex, QPersistentMo
editor.setText(str(value))

def setModelData(self, editor: QWidget, model: QAbstractItemModel, index: Union[QModelIndex, QPersistentModelIndex]) -> None:
step_view = self.parent()
assert isinstance(step_view, ProofStepView)
assert isinstance(editor, QLineEdit)
model.setData(index, editor.text(), Qt.ItemDataRole.EditRole)
step_view.rename_proof_step(editor.text(), index.row() - 1)

0 comments on commit ec13b1d

Please sign in to comment.