harbor run -t openthoughts/tasktrove-mix-h1-struggle-zone-v2-mix-h1-struggle-zone-0883harbor download openthoughts/tasktrove-mix-h1-struggle-zone-v2-mix-h1-struggle-zone-0883solution file
import itertools
import random
from typing import Optional, Callable
from PySide6.QtCore import QEasingCurve, QPointF, QAbstractAnimation, \
QParallelAnimationGroup
from PySide6.QtGui import QUndoStack, QUndoCommand
from .common import VT, GraphT, pos_to_view
from .graphscene import GraphScene
from .vitem import VItem, VItemAnimation, VITEM_UNSELECTED_Z, VITEM_SELECTED_Z
class AnimatedUndoStack(QUndoStack):
"""An undo stack that can play animations between actions."""
# Command that has not yet been pushed to the base stack because an
# animation is still playing
queued_cmd: Optional[QUndoCommand] = None
# Animation that is currently playing
running_anim: Optional[QAbstractAnimation] = None
def push(self, cmd: QUndoCommand, anim_before: Optional[QAbstractAnimation] = None,
anim_after: Optional[QAbstractAnimation] = None) -> None:
# Stop previously running animation
if self.running_anim:
self.running_anim.stop()
self.running_anim = None
# If there is still a queued command, perform it first immediately
if self.queued_cmd:
self._push_now(self.queued_cmd)
if anim_before:
self.queued_cmd = cmd
anim_before.finished.connect(lambda: self._push_now(cmd, anim_after))
anim_before.start()
self.running_anim = anim_before
else:
self._push_now(cmd, anim_after)
def undo(self) -> None:
# Stop previously running animation
if self.running_anim:
self.running_anim.stop()
self.running_anim = None
# If there is still a queued command, perform it first immediately
if self.queued_cmd:
self._push_now(self.queued_cmd)
super().undo()
def _push_now(self, cmd: QUndoCommand, anim_after: Optional[QAbstractAnimation] = None) -> None:
self.queued_cmd = None
super().push(cmd)
if anim_after:
anim_after.start()
self.running_anim = anim_after
def scale(it: VItem, target: float, duration: int, ease: QEasingCurve, start: Optional[float] = None) -> VItemAnimation:
anim = VItemAnimation(it, VItem.Properties.Scale)
anim.setDuration(duration)
anim.setStartValue(start or it.scale())
# Important: end value must be a float, otherwise the animation doesn't work because
# start and end have different types
anim.setEndValue(float(target))
anim.setEasingCurve(ease)
return anim
def move(it: VItem, target: QPointF, duration: int, ease: QEasingCurve, start: Optional[QPointF] = None) -> VItemAnimation:
anim = VItemAnimation(it, VItem.Properties.Position, refresh=True)
anim.setDuration(duration)
anim.setStartValue(start or it.pos())
anim.setEndValue(target)
anim.setEasingCurve(ease)
return anim
def morph_graph(start: GraphT, end: GraphT, scene: GraphScene, to_start: Callable[[VT], Optional[VT]],
to_end: Callable[[VT], Optional[VT]], duration: int, ease: QEasingCurve) -> QAbstractAnimation:
"""Morphs a graph into another graph by moving the vertices."""
moves = set()
for v in itertools.chain(iter(start.vertices()), iter(end.vertices())):
if v not in start.graph:
if u := to_start(v):
moves.add((v, u, v))
elif v not in end.graph:
if u := to_end(v):
moves.add((v, v, u))
elif start.row(v) != end.row(v) or start.qubit(v) != end.qubit(v):
moves.add((v, v, v))
group = QParallelAnimationGroup()
for v, start_pos, end_pos in moves:
anim = VItemAnimation(v, VItem.Properties.Position, scene, refresh=True)
anim.setDuration(duration)
anim.setStartValue(QPointF(*pos_to_view(start.row(start_pos), start.qubit(start_pos))))
anim.setEndValue(QPointF(*pos_to_view(end.row(end_pos), end.qubit(end_pos))))
anim.setEasingCurve(ease)
group.addAnimation(anim)
return group
def shake(it: VItem, amount: float, duration: int) -> None:
center = it.pos()
anim = VItemAnimation(it, VItem.Properties.Position, refresh=False)
anim.setLoopCount(-1) # Infinite looping
anim.setEasingCurve(QEasingCurve.Type.InOutExpo)
anim.setDuration(duration)
def set_random_params() -> None:
dx = (2 * random.random() - 1) * amount
dy = (2 * random.random() - 1) * amount
anim.setStartValue(it.pos())
anim.setEndValue(QPointF(center.x() + dx, center.y() + dy))
def state_changed(state: QAbstractAnimation.State) -> None:
if state == QAbstractAnimation.State.Stopped:
it.setPos(center)
set_random_params()
anim.
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py