aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/animation/easing/easing.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/animation/easing/easing.py')
-rw-r--r--examples/widgets/animation/easing/easing.py250
1 files changed, 130 insertions, 120 deletions
diff --git a/examples/widgets/animation/easing/easing.py b/examples/widgets/animation/easing/easing.py
index 647fbeb34..b3d6f0dff 100644
--- a/examples/widgets/animation/easing/easing.py
+++ b/examples/widgets/animation/easing/easing.py
@@ -2,7 +2,7 @@
#############################################################################
##
## Copyright (C) 2010 Riverbank Computing Limited.
-## Copyright (C) 2016 The Qt Company Ltd.
+## Copyright (C) 2021 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -40,30 +40,41 @@
##
#############################################################################
-from PySide6 import QtCore, QtGui, QtWidgets
+from enum import IntEnum
+import sys
+
+from PySide6.QtCore import (Property, QEasingCurve, QObject, QPropertyAnimation,
+ QPoint, QPointF, QRect, QRectF, QSize, Qt)
+from PySide6.QtGui import (QBrush, QColor, QIcon, QLinearGradient, QPainter,
+ QPainterPath, QPen, QPixmap)
+from PySide6.QtWidgets import (QApplication, QGraphicsPixmapItem,
+ QGraphicsItem, QGraphicsScene, QGraphicsView,
+ QListWidget, QListWidgetItem, QWidget)
-import easing_rc
from ui_form import Ui_Form
-class Animation(QtCore.QPropertyAnimation):
- LinearPath, CirclePath = range(2)
+class PathType(IntEnum):
+ LINEAR_PATH = 0
+ CIRCLE_PATH = 1
+
+class Animation(QPropertyAnimation):
def __init__(self, target, prop):
super(Animation, self).__init__(target, prop)
- self.setPathType(Animation.LinearPath)
+ self.set_path_type(PathType.LINEAR_PATH)
- def setPathType(self, pathType):
- self.m_pathType = pathType
- self.m_path = QtGui.QPainterPath()
+ def set_path_type(self, pathType):
+ self._pathType = pathType
+ self._path = QPainterPath()
def updateCurrentTime(self, currentTime):
- if self.m_pathType == Animation.CirclePath:
- if self.m_path.isEmpty():
+ if self._pathType == PathType.CIRCLE_PATH:
+ if self._path.isEmpty():
end = self.endValue()
start = self.startValue()
- self.m_path.moveTo(start)
- self.m_path.addEllipse(QtCore.QRectF(start, end))
+ self._path.moveTo(start)
+ self._path.addEllipse(QRectF(start, end))
dura = self.duration()
if dura == 0:
@@ -71,26 +82,27 @@ class Animation(QtCore.QPropertyAnimation):
else:
progress = (((currentTime - 1) % dura) + 1) / float(dura)
- easedProgress = self.easingCurve().valueForProgress(progress)
- if easedProgress > 1.0:
- easedProgress -= 1.0
- elif easedProgress < 0:
- easedProgress += 1.0
+ eased_progress = self.easingCurve().valueForProgress(progress)
+ if eased_progress > 1.0:
+ eased_progress -= 1.0
+ elif eased_progress < 0:
+ eased_progress += 1.0
- pt = self.m_path.pointAtPercent(easedProgress)
+ pt = self._path.pointAtPercent(eased_progress)
self.updateCurrentValue(pt)
self.valueChanged.emit(pt)
else:
super(Animation, self).updateCurrentTime(currentTime)
+
# PySide6 doesn't support deriving from more than one wrapped class so we use
# composition and delegate the property.
-class Pixmap(QtCore.QObject):
+class Pixmap(QObject):
def __init__(self, pix):
super(Pixmap, self).__init__()
- self.pixmap_item = QtWidgets.QGraphicsPixmapItem(pix)
- self.pixmap_item.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache)
+ self.pixmap_item = QGraphicsPixmapItem(pix)
+ self.pixmap_item.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
def set_pos(self, pos):
self.pixmap_item.setPos(pos)
@@ -98,161 +110,159 @@ class Pixmap(QtCore.QObject):
def get_pos(self):
return self.pixmap_item.pos()
- pos = QtCore.Property(QtCore.QPointF, get_pos, set_pos)
+ pos = Property(QPointF, get_pos, set_pos)
-class Window(QtWidgets.QWidget):
+class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
- self.m_iconSize = QtCore.QSize(64, 64)
- self.m_scene = QtWidgets.QGraphicsScene()
+ self._iconSize = QSize(64, 64)
+ self._scene = QGraphicsScene()
m_ui = Ui_Form()
m_ui.setupUi(self)
- m_ui.easingCurvePicker.setIconSize(self.m_iconSize)
- m_ui.easingCurvePicker.setMinimumHeight(self.m_iconSize.height() + 50)
+ m_ui.easingCurvePicker.setIconSize(self._iconSize)
+ m_ui.easingCurvePicker.setMinimumHeight(self._iconSize.height() + 50)
m_ui.buttonGroup.setId(m_ui.lineRadio, 0)
m_ui.buttonGroup.setId(m_ui.circleRadio, 1)
- dummy = QtCore.QEasingCurve()
+ dummy = QEasingCurve()
m_ui.periodSpinBox.setValue(dummy.period())
m_ui.amplitudeSpinBox.setValue(dummy.amplitude())
m_ui.overshootSpinBox.setValue(dummy.overshoot())
- m_ui.easingCurvePicker.currentRowChanged.connect(self.curveChanged)
- m_ui.buttonGroup.idClicked.connect(self.pathChanged)
- m_ui.periodSpinBox.valueChanged.connect(self.periodChanged)
- m_ui.amplitudeSpinBox.valueChanged.connect(self.amplitudeChanged)
- m_ui.overshootSpinBox.valueChanged.connect(self.overshootChanged)
+ m_ui.easingCurvePicker.currentRowChanged.connect(self.curve_changed)
+ m_ui.buttonGroup.idClicked.connect(self.path_changed)
+ m_ui.periodSpinBox.valueChanged.connect(self.period_changed)
+ m_ui.amplitudeSpinBox.valueChanged.connect(self.amplitude_changed)
+ m_ui.overshootSpinBox.valueChanged.connect(self.overshoot_changed)
- self.m_ui = m_ui
- self.createCurveIcons()
+ self._ui = m_ui
+ self.create_curve_icons()
- pix = QtGui.QPixmap(':/images/qt-logo.png')
- self.m_item = Pixmap(pix)
- self.m_scene.addItem(self.m_item.pixmap_item)
- self.m_ui.graphicsView.setScene(self.m_scene)
+ pix = QPixmap(':/qt-project.org/logos/pysidelogo.png')
+ self._item = Pixmap(pix)
+ self._scene.addItem(self._item.pixmap_item)
+ self._ui.graphicsView.setScene(self._scene)
- self.m_anim = Animation(self.m_item, b'pos')
- self.m_anim.setEasingCurve(QtCore.QEasingCurve.OutBounce)
- self.m_ui.easingCurvePicker.setCurrentRow(int(QtCore.QEasingCurve.OutBounce))
+ self._anim = Animation(self._item, b'pos')
+ self._anim.setEasingCurve(QEasingCurve.OutBounce)
+ self._ui.easingCurvePicker.setCurrentRow(int(QEasingCurve.OutBounce))
- self.startAnimation()
+ self.start_animation()
- def createCurveIcons(self):
- pix = QtGui.QPixmap(self.m_iconSize)
- painter = QtGui.QPainter()
+ def create_curve_icons(self):
+ pix = QPixmap(self._iconSize)
+ painter = QPainter()
- gradient = QtGui.QLinearGradient(0, 0, 0, self.m_iconSize.height())
- gradient.setColorAt(0.0, QtGui.QColor(240, 240, 240))
- gradient.setColorAt(1.0, QtGui.QColor(224, 224, 224))
+ gradient = QLinearGradient(0, 0, 0, self._iconSize.height())
+ gradient.setColorAt(0.0, QColor(240, 240, 240))
+ gradient.setColorAt(1.0, QColor(224, 224, 224))
- brush = QtGui.QBrush(gradient)
+ brush = QBrush(gradient)
# The original C++ code uses undocumented calls to get the names of the
# different curve types. We do the Python equivalant (but without
# cheating)
- curve_types = [(n, c) for n, c in QtCore.QEasingCurve.__dict__.items()
- if isinstance(c, QtCore.QEasingCurve.Type) \
- and c != QtCore.QEasingCurve.Custom \
- and c != QtCore.QEasingCurve.NCurveTypes \
- and c != QtCore.QEasingCurve.TCBSpline]
+ curve_types = [(n, c) for n, c in QEasingCurve.__dict__.items()
+ if isinstance(c, QEasingCurve.Type) \
+ and c != QEasingCurve.Custom \
+ and c != QEasingCurve.NCurveTypes \
+ and c != QEasingCurve.TCBSpline]
curve_types.sort(key=lambda ct: ct[1])
painter.begin(pix)
for curve_name, curve_type in curve_types:
- painter.fillRect(QtCore.QRect(QtCore.QPoint(0, 0), self.m_iconSize), brush)
- curve = QtCore.QEasingCurve(curve_type)
+ painter.fillRect(QRect(QPoint(0, 0), self._iconSize), brush)
+ curve = QEasingCurve(curve_type)
- painter.setPen(QtGui.QColor(0, 0, 255, 64))
- xAxis = self.m_iconSize.height() / 1.5
- yAxis = self.m_iconSize.width() / 3.0
- painter.drawLine(0, xAxis, self.m_iconSize.width(), xAxis)
- painter.drawLine(yAxis, 0, yAxis, self.m_iconSize.height())
+ painter.setPen(QColor(0, 0, 255, 64))
+ x_axis = self._iconSize.height() / 1.5
+ y_axis = self._iconSize.width() / 3.0
+ painter.drawLine(0, x_axis, self._iconSize.width(), x_axis)
+ painter.drawLine(y_axis, 0, y_axis, self._iconSize.height())
- curveScale = self.m_iconSize.height() / 2.0
+ curve_scale = self._iconSize.height() / 2.0
- painter.setPen(QtCore.Qt.NoPen)
+ painter.setPen(Qt.NoPen)
# Start point.
- painter.setBrush(QtCore.Qt.red)
- start = QtCore.QPoint(yAxis,
- xAxis - curveScale * curve.valueForProgress(0))
+ painter.setBrush(Qt.red)
+ start = QPoint(y_axis,
+ x_axis - curve_scale * curve.valueForProgress(0))
painter.drawRect(start.x() - 1, start.y() - 1, 3, 3)
# End point.
- painter.setBrush(QtCore.Qt.blue)
- end = QtCore.QPoint(yAxis + curveScale,
- xAxis - curveScale * curve.valueForProgress(1))
+ painter.setBrush(Qt.blue)
+ end = QPoint(y_axis + curve_scale,
+ x_axis - curve_scale * curve.valueForProgress(1))
painter.drawRect(end.x() - 1, end.y() - 1, 3, 3)
- curvePath = QtGui.QPainterPath()
- curvePath.moveTo(QtCore.QPointF(start))
+ curve_path = QPainterPath()
+ curve_path.moveTo(QPointF(start))
t = 0.0
while t <= 1.0:
- to = QtCore.QPointF(yAxis + curveScale * t,
- xAxis - curveScale * curve.valueForProgress(t))
- curvePath.lineTo(to)
- t += 1.0 / curveScale
+ to = QPointF(y_axis + curve_scale * t,
+ x_axis - curve_scale * curve.valueForProgress(t))
+ curve_path.lineTo(to)
+ t += 1.0 / curve_scale
- painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
- painter.strokePath(curvePath, QtGui.QColor(32, 32, 32))
- painter.setRenderHint(QtGui.QPainter.Antialiasing, False)
+ painter.setRenderHint(QPainter.Antialiasing, True)
+ painter.strokePath(curve_path, QColor(32, 32, 32))
+ painter.setRenderHint(QPainter.Antialiasing, False)
- item = QtWidgets.QListWidgetItem()
- item.setIcon(QtGui.QIcon(pix))
+ item = QListWidgetItem()
+ item.setIcon(QIcon(pix))
item.setText(curve_name)
- self.m_ui.easingCurvePicker.addItem(item)
+ self._ui.easingCurvePicker.addItem(item)
painter.end()
- def startAnimation(self):
- self.m_anim.setStartValue(QtCore.QPointF(0, 0))
- self.m_anim.setEndValue(QtCore.QPointF(100, 100))
- self.m_anim.setDuration(2000)
- self.m_anim.setLoopCount(-1)
- self.m_anim.start()
-
- def curveChanged(self, row):
- curveType = QtCore.QEasingCurve.Type(row)
- self.m_anim.setEasingCurve(curveType)
- self.m_anim.setCurrentTime(0)
-
- isElastic = (curveType >= QtCore.QEasingCurve.InElastic
- and curveType <= QtCore.QEasingCurve.OutInElastic)
- isBounce = (curveType >= QtCore.QEasingCurve.InBounce
- and curveType <= QtCore.QEasingCurve.OutInBounce)
-
- self.m_ui.periodSpinBox.setEnabled(isElastic)
- self.m_ui.amplitudeSpinBox.setEnabled(isElastic or isBounce)
- self.m_ui.overshootSpinBox.setEnabled(curveType >= QtCore.QEasingCurve.InBack
- and curveType <= QtCore.QEasingCurve.OutInBack)
-
- def pathChanged(self, index):
- self.m_anim.setPathType(index)
-
- def periodChanged(self, value):
- curve = self.m_anim.easingCurve()
+ def start_animation(self):
+ self._anim.setStartValue(QPointF(0, 0))
+ self._anim.setEndValue(QPointF(100, 100))
+ self._anim.setDuration(2000)
+ self._anim.setLoopCount(-1)
+ self._anim.start()
+
+ def curve_changed(self, row):
+ curve_type = QEasingCurve.Type(row)
+ self._anim.setEasingCurve(curve_type)
+ self._anim.setCurrentTime(0)
+
+ is_elastic = (curve_type >= QEasingCurve.InElastic
+ and curve_type <= QEasingCurve.OutInElastic)
+ is_bounce = (curve_type >= QEasingCurve.InBounce
+ and curve_type <= QEasingCurve.OutInBounce)
+
+ self._ui.periodSpinBox.setEnabled(is_elastic)
+ self._ui.amplitudeSpinBox.setEnabled(is_elastic or is_bounce)
+ self._ui.overshootSpinBox.setEnabled(curve_type >= QEasingCurve.InBack
+ and curve_type <= QEasingCurve.OutInBack)
+
+ def path_changed(self, index):
+ self._anim.set_path_type(index)
+
+ def period_changed(self, value):
+ curve = self._anim.easingCurve()
curve.setPeriod(value)
- self.m_anim.setEasingCurve(curve)
+ self._anim.setEasingCurve(curve)
- def amplitudeChanged(self, value):
- curve = self.m_anim.easingCurve()
+ def amplitude_changed(self, value):
+ curve = self._anim.easingCurve()
curve.setAmplitude(value)
- self.m_anim.setEasingCurve(curve)
+ self._anim.setEasingCurve(curve)
- def overshootChanged(self, value):
- curve = self.m_anim.easingCurve()
+ def overshoot_changed(self, value):
+ curve = self._anim.easingCurve()
curve.setOvershoot(value)
- self.m_anim.setEasingCurve(curve)
+ self._anim.setEasingCurve(curve)
if __name__ == '__main__':
-
- import sys
- app = QtWidgets.QApplication(sys.argv)
+ app = QApplication(sys.argv)
w = Window()
w.resize(600, 600)
w.show()