Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: Moving from tab based rewrite buttons to tree based side panel #187

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions zxlive/animations.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from __future__ import annotations

import itertools
import random
from typing import Optional, Callable
from typing import Optional, Callable, TYPE_CHECKING

from PySide6.QtCore import QEasingCurve, QPointF, QAbstractAnimation, \
QParallelAnimationGroup
from PySide6.QtGui import QUndoStack, QUndoCommand
from pyzx.utils import vertex_is_w

from .common import VT, GraphT, pos_to_view
from .custom_rule import CustomRule
from .rewrite_data import operations
from .common import VT, GraphT, pos_to_view, ANIMATION_DURATION
from .graphscene import GraphScene
from .vitem import VItem, VItemAnimation, VITEM_UNSELECTED_Z, VITEM_SELECTED_Z, get_w_partner_vitem

if TYPE_CHECKING:
from .proof_panel import ProofPanel
from .rewrite_action import RewriteAction


class AnimatedUndoStack(QUndoStack):
"""An undo stack that can play animations between actions."""
Expand Down Expand Up @@ -256,3 +264,55 @@ def unfuse(before: GraphT, after: GraphT, src: VT, scene: GraphScene) -> QAbstra
return morph_graph(before, after, scene, to_start=lambda _: src, to_end=lambda _: None,
duration=700, ease=QEasingCurve(QEasingCurve.Type.OutElastic))


def make_animation(self: RewriteAction, panel: ProofPanel, g, matches, rem_verts) -> tuple:
anim_before = None
anim_after = None
if self.name == operations['spider']['text'] or self.name == operations['fuse_w']['text']:
anim_before = QParallelAnimationGroup()
for v1, v2 in matches:
if v1 in rem_verts:
v1, v2 = v2, v1
anim_before.addAnimation(fuse(panel.graph_scene.vertex_map[v2], panel.graph_scene.vertex_map[v1]))
elif self.name == operations['to_z']['text']:
print('To do: animate ' + self.name)
elif self.name == operations['to_x']['text']:
print('To do: animate ' + self.name)
elif self.name == operations['rem_id']['text']:
anim_before = QParallelAnimationGroup()
for m in matches:
anim_before.addAnimation(remove_id(panel.graph_scene.vertex_map[m[0]]))
elif self.name == operations['copy']['text']:
anim_before = QParallelAnimationGroup()
for m in matches:
anim_before.addAnimation(fuse(panel.graph_scene.vertex_map[m[0]],
panel.graph_scene.vertex_map[m[1]]))
anim_after = QParallelAnimationGroup()
for m in matches:
anim_after.addAnimation(strong_comp(panel.graph, g, m[1], panel.graph_scene))
elif self.name == operations['pauli']['text']:
print('To do: animate ' + self.name)
elif self.name == operations['bialgebra']['text']:
anim_before = QParallelAnimationGroup()
for v1, v2 in matches:
anim_before.addAnimation(fuse(panel.graph_scene.vertex_map[v1],
panel.graph_scene.vertex_map[v2], meet_halfway=True))
anim_after = QParallelAnimationGroup()
for v1, v2 in matches:
v2_row, v2_qubit = panel.graph.row(v2), panel.graph.qubit(v2)
panel.graph.set_row(v2, (panel.graph.row(v1) + v2_row) / 2)
panel.graph.set_qubit(v2, (panel.graph.qubit(v1) + v2_qubit) / 2)
anim_after.addAnimation(strong_comp(panel.graph, g, v2, panel.graph_scene))
panel.graph.set_row(v2, v2_row)
panel.graph.set_qubit(v2, v2_qubit)
elif isinstance(self.rule, CustomRule) and self.rule.last_rewrite_center is not None:
center = self.rule.last_rewrite_center
duration = ANIMATION_DURATION / 2
anim_before = morph_graph_to_center(panel.graph, lambda v: v not in g.graph,
panel.graph_scene, center, duration,
QEasingCurve(QEasingCurve.Type.InQuad))
anim_after = morph_graph_from_center(g, lambda v: v not in panel.graph.graph,
panel.graph_scene, center, duration,
QEasingCurve(QEasingCurve.Type.OutQuad))

return anim_before, anim_after
9 changes: 5 additions & 4 deletions zxlive/custom_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .common import ET, VT, GraphT

if TYPE_CHECKING:
from .proof_actions import ProofAction
from .rewrite_data import RewriteData

class CustomRule:
def __init__(self, lhs_graph: GraphT, rhs_graph: GraphT, name: str, description: str) -> None:
Expand Down Expand Up @@ -109,9 +109,10 @@ def from_json(cls, json_str: str) -> "CustomRule":
assert (isinstance(lhs_graph, GraphT) and isinstance(rhs_graph, GraphT)) # type: ignore
return cls(lhs_graph, rhs_graph, d['name'], d['description'])

def to_proof_action(self) -> "ProofAction":
from .proof_actions import MATCHES_VERTICES, ProofAction
return ProofAction(self.name, self.matcher, self, MATCHES_VERTICES, self.description)
def to_rewrite_data(self) -> "RewriteData":
from .rewrite_data import MATCHES_VERTICES
return {"text": self.name, "matcher": self.matcher, "rule": self, "type": MATCHES_VERTICES,
"tooltip": self.description, 'copy_first': False, 'returns_new_graph': False}


def get_linear(v):
Expand Down
Loading
Loading